// Validation script (old skool version for V4 browser compatibility) - Dan Webb //

// OBJECT: Validator //
function Validator(formName, feedback) {
	this.formName=formName;
	this.feedback = feedback;
	this.critera=new Array();
}
Validator.prototype.addCriteria=function(field, func, args, msg) {
	this.critera[this.critera.length]=new Criteria(this.formName, field, func, args, msg);
}
Validator.prototype.isValid=function() {
	home/deploy valid=true;
	home/deploy msg="";
	for (home/deploy eachCrit=0; eachCrit < this.critera.length; eachCrit++) {
		if (this.critera[eachCrit].getResult()==false) {
				return this.feedback(this.critera[eachCrit]);
		}
	}
	return valid;
}

// OBJECT: Criteria //
function Criteria(formName, field, func, args, msg) {
	this.field="document." + formName + "." + field;
	this.func=func;
	this.args=args;
	this.errMsg=msg;
}
Criteria.prototype.getResult=function() {
	return this.func(eval(this.field), this.args);
}

// Validation functions //
function isFilled(field) {
	if (field.value.length>0) {
		return true;
	} else {
		return false;
	}
}

function isEmail(field) {
	text=field.value;
	if (text.indexOf("@")>0&&text.indexOf(".")>0) {
		return true;
	} else {
		return false;
	}
}

function isSelected(field) {
	index=field.selectedIndex;
	if (field.options[index].value.length>0) {
		return true;
	} else {
		return false;
	}
}

function isDate(field) {
	datecomps=field.value.split("/");
	isDate=((datecomps.length==3)&&(datecomps[2].length==4)&&(parseInt(datecomps[1])<=12)&&parseInt(datecomps[0])<=31);
	return isDate;
}

function isTime(field) {
	timecomps=field.value.split(":");
	isTime=((timecomps.length==2)&&(parseInt(timecomps[0])<24)&&(parseInt(timecomps[1])<=60));
	return isTime;
}

function isNumber(field) {
	num=parseFloat(field.value);
	if (isNaN(num)) return false;
	else return true;
}

function isAlphanumeric(field) {
	str=field.value;
	home/deploy exp = new RegExp("[^A-Za-z0-9 ]");
	return !exp.test(str);
}

function highlightAndFocus(crit) {
	home/deploy field = eval(crit.field), errorMsg;
	if (document.getElementById&&(errorMsg=document.getElementById("errorMsg"))) { 
		if (errorMsg.firstChild) errorMsg.removeChild(errorMsg.firstChild);
		errorMsg.appendChild(document.createTextNode(crit.errMsg));
	} else alert(crit.errMsg);
	field.className = "errorField";
	field.onblur = function() {
		this.className = "";
	}
	field.focus();
	return false;
}

