// Field Validation Library
//
// The purpose of this library is to home general field validation routines.
//
// The following properties are valid for text, textarea and password fields within a form.
//
//	.notContain:	Specify what charcters are not allowed
//	.minLen:	minimum string length of a field
//	.minValue	minimum numeric value
//	.maxValue	maximum numeric value
//  .isEmail	checks string conforms to email syntax
//
// If two password fields are present on a page assume its for a verify and do a comapare
//
// v1.0	 7th September 2000	Initial Version
// v1.1  10th October 2000	.notContain removed from code and exclusions hardcoded
// v1.2  18th December 2003  .minValue &. maxValue now check that the field is a number
// v1.21 30th December 2003  extra code added to ensure min & max values are only checked
//							if minValue or maxValue are not null.
// v1.22 15th January 2004	hidden fields now always mandatory unless marked as optional
// v1.23 24th November 2004 password match check did not work if password object was first on form.
//							.notContain added back
// v1.24 25th May 2008		.isEmail email check with reg expresion
//
// Rob Langley
// rob.langley@bizzie.biz

// Validate Form
function validateForm (formname) {

	// Set variables
	var msg;
	var notContains = "";
	var emptyFields = "";
	var numberRange ="";
	var email="";
	var passwordMatch = "";
	var pwdPos=-1;
	var containError=0;

//    alert("Entering Form Validation Routine")
	// Loop through all elements in a form, looking for all text, textarea 
	// and password elements that do not have an optional property set.  Then check
	// there length exceeds the minimum defined with the 'minLen' property.

	// alert ("Number of Elements......." + formname.length);

	for (var i = 0; i < formname.length; i++){
		var e = formname.elements[i];
		if (((e.type == "text") || (e.type == "textarea") || (e.type == "password") || (e.type == "hidden")) && !e.optional) {
		// Field is 'text' or 'textarea' or 'password' and not optional.
			notAllow=e.notContain;
			
			if ((e.name=="username") || (e.name=="password")){
				txt=e.value
				if(txt.indexOf("'")!=-1){
					containError = 1;}
				if(txt.indexOf(" ")!=-1){
					containError=1;}
					
			}
			if(contains(e.value,e.notContain)){
				notContains += "\n     " + e.name + " should not contain " + e.notContain + " character(s)";
			}

			
			if (containError == 1){
				notContains += "\n     " + e.name + " should not contain spaces or ' character(s)";
				containError=0;
			}
			

			if ((e.value == null) || (e.value == "") || isblank(e.value)){
				emptyFields += "\n     " + e.name + " is Mandatory";
			}
			
			if (e.value.length < e.minLen){
				emptyFields += "\n     " + e.name + " must be at least " + e.minLen + " character(s)";

			}
  // alert ("e.value = " + e.value + "\n e.minValue = " + e.minValue);
			if ((e.minValue != null) && ((e.value < e.minValue) || (isNaN(e.value)))){
				numberRange += "\n     " + e.name + " must be a number of no less than " + e.minValue;
			}

			if ((e.maxValue != null) && ((e.value > e.maxValue) || (isNaN(e.value)))){
				numberRange += "\n     " + e.name + " must be a number no more than " + e.maxValue;
			}
			

			if (e.type == "password"){
			// Ensure passwords match
// alert ("i=" + i + " data: " + e.value + "  type: " + e.type);
 				if (pwdPos != -1){
				// If this is not the first password field read then compare it to previous
				// else make a note of its element position.
					if (e.value != formname.elements[pwdPos].value){
						passwordMatch = "\n- Passwords must match";
					}
				}
				else{
					pwdPos = i;
				}
			}
			// Now check for valid email syntax
//			if (e.isEmail){
//				alert ("field is email.. i=" + i + " data: " + e.value + "  type: " + e.type);
//				email += "\n     " + e.name + " email address not valid " 
//			}
		}

			
	}



	// Display any errors and return false to prevent submit
	if (!emptyFields && !passwordMatch && !numberRange && !notContains) return true;
	msg  = "------------------------------------------------------------------------------------\n";
	msg += "The form was not submitted because of the following\n";
	msg += "error(s). Please correct and re submit.\n";
	msg += "------------------------------------------------------------------------------------\n\n";

	if (notContains) {
		msg += "- Illegal characters" + notContains + "\n";
	}

	if (emptyFields) {
		msg += "- The following field(s) where shorter than the required length" + emptyFields + "\n";
	}
	if (passwordMatch) {
		msg += passwordMatch + "\n";
	}
	if (numberRange) {
		msg += "- The following field(s) where outside the required range" + numberRange + "\n";
	}
	if (email) {
		msg += "- The following field(s) contained an invalid email address" + email + "\n";
	}

	alert(msg);
	return false;
}

// Function that returns true if string only contains white space

function isblank(s)
{
	for(var i=0; i < s.length; i++){
		var c = s.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
	}
	return true;
}

// Returns true if string contains the variable in it.
function contains(string,sub){
	slen=string.length;
	str="";
	for (z=0;z<slen;z++){
		str = string.substr(z,1);
//		alert("Checking for disallowed characters....." + str + " Searching for:" + sub);
		if(sub == str) return true;
	}
	return false;
}