String.prototype.toSentenceCase = function () {
	return (this==null) ? null : this.charAt(0).toUpperCase() + this.substr(1);
}

String.prototype.toSpaces = function() {
	text = this.toSentenceCase();
	var re = /([\u0041-\u005A])/g;
	text = text.replace(re,' $1');
	if (text.charAt(0)==' ') text = text.substr(1);
	return text;
}

String.prototype.sprintf = function () {
	var re = /%s/, text = this;
	for (var i=0;i<arguments.length;i++) {
		text = text.replace(re,arguments[i]);
	}
	return text;
}


/*
** NEEDS validations.js OR validationsCompact.js by Matt Kruse (c)
*/

function Validator(frm) {
	this.form = frm;

	this.onsubmit = function(frm) { 
		this.form = frm.tagName && frm.tagName=='FORM' ? frm : frm.form; 
		return this.customCheckBefore(this.form) && this.validateForm(this.form) && this.customCheckAfter(this.form);
	};

	this.submitIfOk = function(frm) {
		if (this.onsubmit(frm)) {
			this.form.submit();
		}
		return false;
	}
	this.skipStandardCheck = false;

	this.validateForm = function(frm) {
		if (!frm) frm = this.form;
		if (this.skipStandardCheck) return true;
		var elem, doValidate;
		for (var i=0;i<frm.length;i++) {
			elem=frm[i];
			// trim text fields
			if (elem.type=='text') elem.value = Trim(elem.value);
			if ('hidden submit reset button none'.indexOf(elem.type)!=-1 || elem.disabled || elem.readonly || !attrTrue(elem,'validate',true)) continue;
			if (getAttr(frm,'validate')=='all' || attrTrue(elem,'validate') || elem.getAttribute('validateLabel') || elem.getAttribute('validateType')) if (!this.validateField(elem)) return false;
		}
		return true;
	};
	
	this.customCheckAfter  = function() { return true };
	this.customCheckBefore = function() { return true };
	
	//

	this.getRoutine = function(elem) { 
		return this.routines[elem.getAttribute('validateType') ? elem.getAttribute('validateType') : 'nonempty'];
	}
	
	this.defaultAction = function(elem) {
		var fldName = elem.getAttribute('validateLabel') ? elem.getAttribute('validateLabel') : elem.name.toSpaces();
		var routine = this.getRoutine ? this.getRoutine(elem) : this;
		alert(routine.msg.sprintf(fldName)); 
		elem.focus(); 
		return false;
	}
	
	this.validateField = function(elem) {
		var routine = this.getRoutine(elem);
		if (!routine) {
			alert('Unknown validateType setting for the form element '+elem.name+': "'+elem.getAttribute('validateType')+'"');
			return false;
		}
		if(routine.check(elem)) {
			routine.onfail(elem);
			return false;
		} else return true;
	}
	
	// validation routines
	
	this.routines = new Array();
	this.routines.nonempty = {
		msg:'The field "%s" cannot be blank!',
		check: function(elem) { 
			if (elem.type=='radio') {
				var btns = elem.form.elements[elem.name];
				for (i=0;i<btns.length;i++) if (btns[i].checked) return false;
				return true;
			} else 
			return elem.type!='checkbox' && isBlank(getInputValue(elem)) ;
		},
		onfail: this.defaultAction
		};
	this.routines.integer = {
		msg:'The field "%s" must contain a number!',
		check: function(elem) { return !isInteger(getInputValue(elem)) },
		onfail: this.defaultAction
		};
	this.routines.date = {
		msg:'The field "%s" must contain a valid date!',
		check: function(elem) { return !isValidDate(getInputValue(elem)) },
		onfail: this.defaultAction
		};
}


var validator = new Validator();


function isValidDate(ddmmyy){
	if (typeof(ddmmyy)!='string' || ddmmyy.length<2+1+2+1+4) return false;
	var day = ddmmyy.substr(0,2);
	var month = ddmmyy.substr(3,2);
	var year = ddmmyy.substr(6);
	month = month-1;
	var dteDate=new Date(year,month,day);
	/*
	Javascript Dates are a little too forgiving and will change the date to a reasonable guess if it's invalid. We'll use this to our advantage by creating the date object and then comparing it to the details we put it. If the Date object is different, then it must have been an invalid date to start with...
	*/
	return ((day==dteDate.getDate()) && (month==dteDate.getMonth()) && (year==dteDate.getFullYear()));
}


