// Verify Form 1.1
// Author: Scott Jackman
// 28/6/03
/////////////////////////////////////////////////////////////////////////////////
//
//		Mark fields in the form you want to verify by giving them an 
//		id of vf# plus any combination of the following letters:
//		m - mandatory field
//		e - correct email address format
//		n - numerals only
//
//		eg. <input type="text" name="Phone" id="mn" value=""> - id="mn" means field
//		must be filled in and must contain only numbers.
//
/////////////////////////////////////////////////////////////////////////////////
function verifyform(frm,a,d) {
var frm = document.getElementById(frm);
var error = "";
var chkFlds = ["input","select","textarea"];
// loop thru field types - chkFlds
for (var j=0; j < chkFlds.length; j++) {
	// get all the fields for the current type into array
	var frmObjs = frm.getElementsByTagName(chkFlds[j]);
	// loop thru type array
	for (var i=0; i < frmObjs.length; i++) {
		var type = frmObjs[i].type;
			// if not resetting, get value and id from current object
			var fldVal = frmObjs[i].value;
			var fldVfy = frmObjs[i].id;
			var thiserror = 0;
			if (fldVfy.indexOf("vf#")!=-1 && frmObjs[i].disabled !== true) {
		// if reset action called, reset current field
		if (a == "reset") {
			if (!(type == "button") && !(type == "hidden")) {
				if(d!=='1'){
					frmObjs[i].className = "input";
					}
				frmObjs[i].value="";
				}
			} else {
			// Check for empty mandatory fields - m
			if (fldVal == "" && fldVfy.indexOf("m")!=-1) {
				if(d!=='1'){
					frmObjs[i].className = "inputerr";
					}
				error += "   Missing field: "+frmObjs[i].name+'\n';
				thiserror = 1;
				}
			// Check for incorrect numeral - n
			var numerror = 0;
			var numerals = "0123456789. ";
			if (fldVal !== "" && fldVfy.indexOf("n")!=-1) {
				for (k=0; k<fldVal.length; k++) {
					var ch = fldVal.charAt(k);
					var x = numerals.indexOf(ch);
					if (x == -1) {
						numerror = 1;
						} 
					}
				if (numerror == 1) {
					if(d!=='1'){
						frmObjs[i].className = "inputerr";
						}
					error += "   Numbers only: "+frmObjs[i].name+'\n';
					thiserror = 1;
					}
				}
			// Check for incorrect email address - e
			if (fldVal !== "" && fldVfy.indexOf("e")!=-1) {
				var at =  fldVal.indexOf("@");
				var dot = fldVal.indexOf(".");
				if (at==-1 || dot==-1) {
					if(d!=='1'){
						frmObjs[i].className = "inputerr";
						}
					error += "   Incorrect Email Address: "+frmObjs[i].name+'\n';
					thiserror = 1;
					}
				}
			// If field ok, set colour to normal
				if (thiserror!=1) {
					if(d!=='1'){
						frmObjs[i].className = "input";
						}
					}
			}
			}
			}
		}
// If errors, give error alert, otherwise, submit the form
		if (a == "verify") {
			if (!error == "") {
				error = "Please correct the following errors:    "+'\n\n'+error;
				alert(error);
				} else {
				frm.submit();
				}
		}
}