// Validation script (C) DW Limehouse Interactive //
// OBJECT: Validator //
function Validator() {
	this.critera=new Array();
}
Validator.prototype.addCriteria=function(field, func, args, msg) {
	this.critera[this.critera.length]=new Criteria(field, func, args, msg);
}
Validator.prototype.isValid=function() {
	var valid=true;
	var msg="";
	if (document.getElementById) {
		for (eachCrit in this.critera) {
			if (this.critera[eachCrit].getResult()==false) {
				valid=false;
				msg+=this.critera[eachCrit].errMsg + "\n";
			}
		}
		if (msg.length>0) alert("Please correct the following errors: \n\n" + msg);
	}
	return valid;
}

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

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

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

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

function isDate(field) {
	datecomps=document.getElementById(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=document.getElementById(field).value.split(":");
	isTime=((timecomps.length==2)&&(parseInt(timecomps[0])<24)&&(parseInt(timecomps[1])<=60));
	return isTime;
}

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

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