// Verify Form 2.5
// Author: Scott Jackman
// 14/2/07
//	Modded slightly by Mark Wheatman 18-03-09
/////////////////////////////////////////////////////////////////////////////////
//
//		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
//		p - phone - allows the inclusion of brackets () common in phone numbers
//
//		eg. <input type="text" name="Phone" id="mn" value=""> - id="mn" means field
//		must be filled in and must contain only numbers.
//
//		** Changes made since last version:
//		
//		1.	Script will now report missing fields using the 'title' attribute as the name of the field.
//			This allows you to have a mandatory field name like "users_first_name", but have the report
//			return "First Name" as the missing field. Script will look for title first and use name if 
//			title is not present.
//
//		2.	Phone number added to check - allows the inclusion of brackets () common in phone numbers
//
//		3.	All fields are checked for links - if any are found then an error is returned
//
/////////////////////////////////////////////////////////////////////////////////
function verifyform(frm,a,disp) {
// note: when disp is true the class of the field will NOT be changed
var frm = document.getElementById(frm);
var error = "";
var chkFlds = ["input","select","textarea","checkbox"];
// 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 displayname = (frmObjs[i].title == "")?frmObjs[i].name:frmObjs[i].title;
		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(!disp){
					frmObjs[i].className = "input";
					}
				frmObjs[i].value="";
				}
			} else {
			// Check for empty mandatory fields - m
			if (fldVal == "" && fldVfy.indexOf("m")!=-1) {
				if(!disp){
					frmObjs[i].className = "inputerr";    
					}
				error += "   Missing field: "+displayname+'\n';
				thiserror = 1;
				}
			// check for unticked checkbox - m
			if (type == "checkbox" && fldVfy.indexOf("m")!=-1 && frmObjs[i].checked !== true) {
				if(!disp){
					frmObjs[i].className = "inputerr";    
					}
				error += "   Unticked checkbox: "+displayname+'\n';
				thiserror = 1;
				}
				
			// Check for non numeral - n
			var numerror = 0;
			var numerals = "0123456789. \r\n";
			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(!disp){
						frmObjs[i].className = "inputerr";    
						}
					error += "   Numbers only: "+displayname+'\n';
					thiserror = 1;
					}
				}

			// Check for non phone number - p
			var phoneerror = 0;
			var numerals = "0123456789.() \r\n";
			if (fldVal !== "" && fldVfy.indexOf("p")!=-1) {
				for (k=0; k<fldVal.length; k++) {
					var ch = fldVal.charAt(k);
					var x = numerals.indexOf(ch);
					if (x == -1) {
						phoneerror = 1;
						} 
					}
				if (phoneerror == 1) {
					if(!disp){
						frmObjs[i].className = "inputerr";    
						}
					error += "   Phone numbers only: "+displayname+'\n';
					thiserror = 1;
					}
				}


			// Check for non alpha - a
			var alphaerror = 0;
			var numerals = "0123456789. ";
			if (fldVal !== "" && fldVfy.indexOf("a")!=-1) {
				for (k=0; k<fldVal.length; k++) {
					var ch = fldVal.charAt(k);
					var x = numerals.indexOf(ch);
					if (x !== -1) {
						alphaerror = 1;
						} 
					}
				if (alphaerror == 1) {
					if(!disp){
						frmObjs[i].className = "inputerr";    
						}
					error += "   Numbers not allowed: "+displayname+'\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(!disp){
						frmObjs[i].className = "inputerr";    
						}
					error += "   Incorrect Email Address: "+displayname+'\n';
					thiserror = 1;
					}
				}
			
			
			// Check for links - normally just spam.
			if (fldVal !== "") {
				if(fldVal.search('tp://')!=-1){
					error += "   No Links: "+displayname+'\n';
					thiserror = 1;
					if(!disp){
						frmObjs[i].className = "inputerr";    
					}
				}
			}

				
			// If field ok, set colour to normal
				if (thiserror!=1) {
					if(!disp){
						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);
			return false;
			} else {
			return true;
			}
	}
}
