var gValidate = Array();

//Sets up form for validation on submission
function FormValidate(form, checks) {
	this.form = form;
	this.checks = checks;
	this.errors = Array();
	gValidate[form.name] = this;
	form.onsubmit = function () { var val = gValidate[this.name]; return val.check(); }
}

//Checks form for passed validation requirements
//If there are any errors, lists errors and returns false to form submission
FormValidate.prototype.check = function() {
	for(var i=0; i<this.checks.length; i++) {
		switch(this.checks[i][0]) {
			case 'req': this.checkReq(this.checks[i]); break;
			case 'reqradio': this.checkReqRadio(this.checks[i]); break;
			case 'num': this.checkNumeric(this.checks[i]); break;
			case 'match': this.checkMatch(this.checks[i][1], this.checks[i][2]); break;
			case 'alpha': this.checkAlpha(this.checks[i]); break;
			case 'email': this.checkEmail(this.checks[i]); break;
			case 'either': this.checkEither(this.checks[i][1], this.checks[i][2], this.checks[i][3]); break;
			case 'complete': this.checkComplete(this.checks[i][1], this.checks[i][2]); break;
		}
	}
	if(this.errors.length)	 {
		var str = "There were errors in the form. Please correct the following error(s).\n";
		for(var i in this.errors)
			str += "    "+(parseInt(i)+1)+".  "+this.errors[i]+"\n";
		alert(str);
		this.errors = new Array();
		return false;
	}
	else
		return true;
}

//All or nothing
FormValidate.prototype.checkComplete = function(fields, message) {
	var elts = this.getElts(fields), f=0;
	for(var i in elts)
		if(elts[i].value.length != 0)
			f++;
	if(f != 0 && f != elts.length)
		this.errors.push(message);
}

//Either or is filled
FormValidate.prototype.checkEither = function(field1, field2, message) {
	if(!this.form.elements[field1].value && !this.form.elements[field2].value)
		this.errors.push(message);
}

FormValidate.prototype.checkAlpha = function(params) {
	var elts = this.getElts(params[1]);
	for(var i in elts) {
		var str = elts[i].value;
		if(str.match(/[^A-Z-\,]/i))
			this.errors.push('The field "'+elts[i].title+'" should be an alphabetical string.');
	}
}

FormValidate.prototype.checkEmail = function(params) {
	var elts = this.getElts(params[1]);
	for(var i in elts)
		if(elts[i].value.length && !elts[i].value.match(/^.+@.+\..{2,3}$/))
			this.errors.push('The field "'+elts[i].title+'" is not a valid email address.');
}

FormValidate.prototype.checkNumeric = function(params) {
	var elts = this.getElts(params[1]);
	for(var i in elts)
		if(elts[i].value.match(/[^0-9_-]/))
			this.errors.push('The field "'+elts[i].title+'" should be numeric.');
}

FormValidate.prototype.checkMatch = function(field1, field2) {
	var f1 = this.form.elements[field1], f2 = this.form.elements[field2];
	if(f1.value.length)
		if(f1.value != f2.value)
			this.errors.push('The fields "'+f1.title+'" and "'+f2.title+'" don\'t match.');
}

//One in radio group is checked
FormValidate.prototype.checkReqRadio = function(params) {
	var fields = params[1].split(",");
	for(var i in fields) {
		var checked = false, grp = this.form[fields[i]];
		for(var j=0; j<grp.length; j++) {
			if(grp[j].checked){
				checked = true;
				break;
			}
		}
		if(!checked) {
			this.errors.push('The field "'+this.form.elements[fields[i]+'0'].title+'" is empty.')
		}
	}
}

//Field is filled
FormValidate.prototype.checkReq = function(params) {
	var elts = this.getElts(params[1]);
	for(var i in elts) {
		var str = this.trim(elts[i].value);
		if(!str.length)
			this.errors.push('The field "'+ elts[i].title + '" is blank.');
	}
}

//Retrieves form elements with comma-delimited name string
FormValidate.prototype.getElts = function(fieldsstr) {
	var fields = fieldsstr.split(",");
	var elts = new Array();
	for(var i in fields)
		elts.push(this.form.elements[fields[i]]);
	return elts;
}

FormValidate.prototype.trim = function(str) {
	if(!str.length)
		return str;
	str = str.replace(/^\s+/, '');
	return str.replace(/\s+$/, '');
}