/* ------- validateform.js, version 1.0  [7/23/2008]  ---------------------------------------
	
	These functions are all called from a master function on the webpage.  The master 
	function is called from the form's onSubmit function.  In the master function, 
	"theForm.something" is used where something is the name of the field to validate.
	
	JavaScript in the Head of the page: --------------------------
		function validateFormOnSubmit(theForm) {
			setColor(window.normalColor, window.highlightColor);
			
			var reason = "";
			reason += validateUsername(theForm.username);
			reason += validatePassword(theForm.pwd);
			reason += validateEmail(theForm.email_a);
			reason += validateEmail(theForm.email_a, theForm.email_b);
			reason += validatePhone(theForm.phone);
			reason += validateEmpty(theForm.from);
				  
			if (reason != "") {
				alert("Please revise the following field(s):<br />" + reason);
				return false;
			}
		
			return true;
		}
	
	JavaScript function call from the Form: -----------------------
		<form onsubmit="return validateFormOnSubmit(this);" action="send.asp" method="post">
	
	
 -------------------------------------------------------------------------------------------- */


function setColor(normal, highlight) {
	window.normalColor = normal;
	window.highlightColor = highlight;
}


function trim(thisStr) {
	var trimStr = thisStr.replace(/^\s+|\s+$/, '');
	return trimStr;
}


function validateEmail(field) {
	var error="";
	var tfield = trim(field.value);// value of field with whitespace trimmed off
	var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
	var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
	
	if (tfield == "") {
		error = "Please enter an email address.<br />";
		field.style.background = window.highlightColor;
	} else if (!emailFilter.test(tfield)) {  //test email for illegal characters
		error = "Please enter a valid email address.<br />";
		field.style.background = window.highlightColor;
	} else if (field.value.match(illegalChars)) {
		error = "The email address contains illegal characters.<br />";
		field.style.background = window.highlightColor;
	} else {
		field.style.background = window.normalColor;
	}
	return error;
}


function validateEmailAgreement(field1, field2) {
	var error="";
	
	if (field1.value != field2.value) {
		error = "The email addresses are not the same.<br />";
		field1.style.background = window.highlightColor;
		field2.style.background = window.highlightColor;
	} else {
		field1.style.background = window.normalColor;
		field2.style.background = window.normalColor;
	}
	return error;
}


function validateEmpty(field) {
	var error = "";

	if (field.value.length == 0) {
		error = "Please enter your "+field.name+".<br />"
		field.style.background = window.highlightColor; 
	} else {
		field.style.background = window.normalColor;
	}
	return error;  
}


function validatePassword(field) {
	var error = "";
	var illegalChars = /[\W_]/; // allow only letters and numbers 
	 
	if (field.value == "") {
		error = "You didn't enter a password.<br />";
		field.style.background = window.highlightColor;
	} else if ((field.value.length < 7) || (field.value.length > 15)) {
		error = "The password is the wrong length. <br />";
		field.style.background = window.highlightColor;
	} else if (illegalChars.test(field.value)) {
		error = "The password must contain only letter, numbers, and underscores.<br />";
		field.style.background = window.highlightColor;
	} else if ( (fld.value.search(/[a-zA-Z]+/)==-1) || (fld.value.search(/[0-9]+/)==-1)  ) {
		//Possible (original) option:
		//!((field.value.search(/(a-z)+/)) && (field.value.search(/(0-9)+/)))
		error = "The password must contain at least one numeral.<br />";
		field.style.background = window.highlightColor;
	} else {
		field.style.background = window.normalColor;
	}
	return error;
}


function validatePhone(field) {
	var error = "";
	var stripped = field.value.replace(/[\(\)\.\-\ ]/g, '');
	
	if (field.value == "") {
		error = "You didn't enter a phone number.<br />";
		field.style.background = window.highlightColor;
	} else if (isNaN(parseInt(stripped))) {
		error = "The phone number contains illegal characters.<br />";
		field.style.background = window.highlightColor;
	} else if (!(stripped.length == 10)) {
		error = "The phone number is the wrong length. Make sure you included an area code.<br />";
		field.style.background = window.highlightColor;
	}
	return error;
}


function validateUsername(field) {
	var error = "";
	// allow letters, numbers, and underscores
	var illegalChars = /\W/; 
	
	if (field.value == "") {
		error = "You didn't enter a username.<br />";
		field.style.background = window.highlightColor; 
	} else if ((field.value.length < 5) || (field.value.length > 15)) {
		error = "The username is the wrong length.<br />";
		field.style.background = window.highlightColor; 
	} else if (illegalChars.test(field.value)) {
		error = "The username contains illegal characters.<br />";
		field.style.background = window.highlightColor; 
	} else {
		field.style.background = window.normalColor;
	}
	return error;
}
