// Verify Form 2.1
// Author: Scott Jackman
// 14/2/07
/////////////////////////////////////////////////////////////////////////////////
//
//		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.	New field 'output' can specify where the error messages show up. If present they will be written to a div. If not
//			the normal alert box will show.
//
/////////////////////////////////////////////////////////////////////////////////
function verifyform(check_scope,a,disp,output) {
// note: when disp is true the class of the field will NOT be changed
// note: if 'output' is given (this will be an object), the script will output any messages to this object instead of an alert
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 = check_scope.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 += "   Required: "+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 password confirmation
			if (fldVal !== "" && fldVfy.indexOf("P1")!=-1) {
				var p1 = document.getElementById('vf#mP1');
				var p2 = document.getElementById('vf#mP2');
				if(!p1){ // if password not mandatory it will have a different id
					var p1 = document.getElementById('vf#P1');
					var p2 = document.getElementById('vf#P2');
					}
				if((p1.value != '' && p2.value != '') || (p1.value != '' && p2.value == '')){
					if(p1.value != p2.value){
						error += "   Password not confirmed: Please retype.\n";
						p2.value = '';
						p1.className = 'inputerr';
						p2.className = 'inputerr';
						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;
					}
				}
			// If field ok, set colour to normal
				if (thiserror!=1) {
					if(document.getElementById('usernameerror')){
						if(!disp){
							// special check for username first ( we dont want to change color if its an unavalable username according to checkuser.js );
							if(frmObjs[i].name != 'username' && document.getElementById('usernameerror').value != ''){
								frmObjs[i].className = "input";
								}						
							}
						} else {
						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;
			if(!output){
				alert(error);
			} else {
			var formatted_output = error.replace(/\n/g,'<br />');
			var formatted_output = formatted_output.replace(/Required:/g,'<b>Required:</b>');
			var formatted_output = formatted_output.replace(/Phone numbers only:/g,'<b>Phone numbers only:</b>');
			var formatted_output = formatted_output.replace(/Numbers only:/g,'<b>Numbers only:</b>');
			var formatted_output = formatted_output.replace(/Numbers not allowed:/g,'<b>Numbers not allowed:</b>');
			var formatted_output = formatted_output.replace(/Incorrect email address:/g,'<b>Incorrect email address:</b>');
			var formatted_output = formatted_output.replace(/Unticked checkbox:/g,'<b>Unticked checkbox:</b>');
			var formatted_output = formatted_output.replace(/Password not confirmed:/g,'<b>Password not confirmed:</b>');
			document.getElementById(output).innerHTML = '<br /><br />'+formatted_output;
			}
			return false;
			} else {
			if(output){
				document.getElementById(output).innerHTML = '';
				}			
			return true;
			}
	}
}