/**** COPY this to page and moodify appropriately to work with validation functions

function MapName (fieldobj) {
	// returns a user friendly name based on the field name. used for message boxes.
	if (fieldobj.name=="cp_name") {return "Company Name"};
	if (fieldobj.name=="cp_tel") {return "Company Telephone"};
	if (fieldobj.name=="cp_street1") {return "Street 1"};
	if (fieldobj.name=="cp_city") {return "City"};
	if (fieldobj.name=="cp_province") {return "Province"};
	if (fieldobj.name=="cp_zip") {return "Postal Code"};
	if (fieldobj.name=="contact_name") {return "Main Contact Name"};
	if (fieldobj.name=="contact_tel") {return "Main Contact Telephone"};
	if (fieldobj.name=="contact_email") {return "Main Contact E-mail"};
	if (fieldobj.name=="contact_user_name") {return "Login Username"};
	if (fieldobj.name=="contact_password") {return "Login Password"};
	if (fieldobj.name=="contact_password1") {return "Login Password Verification"};
}

function Validate(formobj) {
	//Form validation routine called on submit of form. Form objcet must be passed (e.g. Validate(this); )


	// do not validate if commit = false

	if (formobj.commit == "false") { return true; }

	// ************** General Check Routines *********************

	// ******************* Check Company Form

	if (IsEmpty(formobj.cp_name)) { return false; }
	TrimSpace(formobj.cp_name);

	if (IsEmpty(formobj.cp_tel)) { return false; }
	TrimSpace(formobj.cp_tel);

	if (IsEmpty(formobj.cp_street1)) { return false; }
	TrimSpace(formobj.cp_street1);

	if (IsEmpty(formobj.cp_city)) { return false; }
	TrimSpace(formobj.cp_city);

	if (IsEmpty(formobj.cp_province)) { return false; }
	TrimSpace(formobj.cp_province);

	if (IsEmpty(formobj.cp_zip)) { return false; }
	TrimSpace(formobj.cp_zip);

	// Validate Postal Code
	if (!IsValidZip(formobj.cp_zip)) { return false; }


	// ******************* Check Contact Form

	if (IsEmpty(formobj.contact_name)) { return false; }
	TrimSpace(formobj.contact_name);

	if (IsEmpty(formobj.contact_tel)) { return false; }
	TrimSpace(formobj.contact_tel);

	if (IsEmpty(formobj.contact_email)) { return false; }
	TrimSpace(formobj.contact_email);

	// Validate E-mail Address
	if (!IsValidEmail(formobj.contact_email)) { return false; }


	// ******************* Check Login Form

	if (IsEmpty(formobj.contact_user_name)) { return false; }
	TrimSpace(formobj.contact_user_name);
	if (!IsAlphaNum(formobj.contact_user_name)) { return false; }

	if (formobj.contact_user_name.value.length < 6) {
		alert("Validation Error: Username must be at least 6 characters long.");
		return false;
	}


	if (IsEmpty(formobj.contact_password)) { return false; }
	TrimSpace(formobj.contact_password);
	if (!IsAlphaNum(formobj.contact_password)) { return false; }

	if (formobj.contact_password.value.length < 6) {
		alert("Validation Error: Password must be at least 6 characters long.");
		return false;
	}


	if (IsEmpty(formobj.contact_password1)) { return false; }
	TrimSpace(formobj.contact_password1);

	// Check if confirmation password matches password
	if (formobj.contact_password.value != formobj.contact_password1.value) {
		alert("Validation Error: Pasword and confirmation password do not match.");
		return false;
	}

	// If function gets here, submit

	return true;

}

*/

function IsEmpty(fieldobj) {

	// Checks if field value is empty or contains only space

	var emptyexp = /^\s*$/

	if(fieldobj.value.search(emptyexp) != -1) {
    	// the string contains just spaces or is empty
		alert("The " + MapName(fieldobj) + " field must contain a non-empty, non-space value.");
		return true;
	} else {
    	// not empty
    	return false;
	}
}

function IsEmptyNoAlert(fieldobj) {

	// Checks if field value is empty or contains only space

	var emptyexp = /^\s*$/

	if(fieldobj.value.search(emptyexp) != -1) {
    	// the string contains just spaces or is empty
		return true;
	} else {
    	// not empty
    	return false;
	}
}

function IsAlphaNum(fieldobj) {

	// Checks for non-alphanumeric characters. returns false if any non-alphanumeric chars found and true if all alphanumeric.

	var notalphaexp = /\W/g

	if (fieldobj.value.search(notalphaexp) != -1) {
		alert("The " + MapName(fieldobj) + " field value must be alphanumeric.");
		return false;
	} else {
		return true;
	}
}

function IsNumeric(fieldobj) {

	// Checks for non-numeric characters. returns false if any non-numeric chars found and true if all numeric.

	var notalphaexp = /\D/g

	if (fieldobj.value.search(notalphaexp) != -1) {
		alert("The " + MapName(fieldobj) + " field value must be numeric.");
		return false;
	} else {
		return true;
	}
}


function StripSpaces(fieldobj) {

	// Strips all whitespace characters from field value

	var stripexp = /\s/g

	fieldobj.value = fieldobj.value.replace(stripexp,"");
}

function StripNonAlphaNumChars(fieldobj) {

	// Strips all whitespace characters from field value

	var stripexp = /\W/g

	fieldobj.value = fieldobj.value.replace(stripexp,"");
}


function TrimSpace(fieldobj) {

	// Trims all whitespace characters from field beginning and end of field value

	var leftexp = /^\s+/
	var rightexp = /\s+$/

	//Trim left side
	fieldobj.value = fieldobj.value.replace(leftexp,"");

	//Trim right side
	fieldobj.value = fieldobj.value.replace(rightexp,"");

}

function IsValidZip(fieldobj) {

	// Checks for valid postal code (currently Canada & US Only)

	var postexpCAN = /^([a-zA-Z]{1})(\d{1})([a-zA-Z]{1})(\d{1})([a-zA-Z]{1})(\d{1})$/
	var postexpUS = /^\d{5}$/
	var tempval = null;

	StripSpaces(fieldobj);

	// try matching with canadian pattern
	var tempval=fieldobj.value.match(postexpCAN);

	if (tempval==null) {
		// Does not match Canadian. Try US version.

		tempval=fieldobj.value.match(postexpUS);

		if (tempval==null) {
			// Still null. Not a match with US either.
			alert("The " + MapName(fieldobj) + " field does not match standard Canadian or US Postal Code (Zip) format.");
			return false;
		} else {
			// Matches US pattern
			return true;
		}

	} else {
		// matches Canadian pattern
		return true;
	}

}


function IsValidEmail(fieldobj) {

	// Pattern to check entered e-mail address for user@domain format.
	var emailPat=/^(.+)@(.+)$/
	// Unwanted special characters. These include ( ) < > @ , ; : \ " . [ ]
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
	// String representing which chars aren't allowed.
	var validChars="\[^\\s" + specialChars + "\]";
	// Pattern applies if the "user" is a quoted string
	var quotedUser="(\"[^\"]*\")";
	// Pattern applies for domains that are IP addresses. (E.g. joe@[123.124.233.4]) NOTE: The square brackets are required.
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	// String representing an atom (a series of non-special characters.)
	var atom=validChars + '+';
	// String representing one word in the typical username. For example, in john.doe@somewhere.com
	var word="(" + atom + "|" + quotedUser + ")";
	// Pattern describing the structure of the user
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	// Pattern describing the structure of a normal domain name, as opposed to ipDomainPat, shown above.
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

	// Start Validation

	// Begin with the coarse pattern to simply break up user@domain intodifferent pieces that are easy to analyze.

	var matchArray=fieldobj.value.match(emailPat);

	if (matchArray==null) {
	  // Too many/few @'s or something; basically, this address doesn't even fit the general mold of a valid e-mail address.
		alert("The email address provided appears invalid. Please check for proper email structure (@ and .'s)");
		return false;
	}

	var user=matchArray[1];
	var domain=matchArray[2];

	// See if "user" is valid
	if (user.match(userPat)==null) {
	    // user is not valid
	    alert("The email address provided appears invalid. Please check the username portion (before the @) for validity.");
    	return false;
	}

	/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
	var IPArray=domain.match(ipDomainPat);

	if (IPArray!=null) {
    	// this is an IP address
		  for (var i=1;i<=4;i++) {
		    if (IPArray[i]>255) {
		        alert("The email address provided appears invalid. Please ensure the IP address has been properly specified.");
				return false;
		    }
	    }
	    return true;
	}

	// Domain is symbolic name
	var domainArray=domain.match(domainPat);

	if (domainArray==null) {
		alert("The email address provided appears invalid. Please ensure the domain name has been properly specified.");
    	return false;
	}

	/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   representing country (uk, nl), and that there's a hostname preceding
   the domain or country. */

	/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
	var atomPat=new RegExp(atom,"g");
	var domArr=domain.match(atomPat);
	var len=domArr.length;

	if (domArr[domArr.length-1].length<2 ||
    	domArr[domArr.length-1].length>3) {
   		// the address must end in a two letter or three letter word.
   		alert("The email address provided appears invalid. The address must end with a three-letter domain, or two letter country.");
   		return false;
	}

	// Make sure there's a host name preceding the domain.
	if (len<2) {
   		var errStr="The email address provided appears invalid. Please ensure a valid hostname has been provided (after the @)."
   		alert(errStr);
		return false;
	}

	// If we've gotten this far, everything's valid!
	return true;

}
