/*	Form Validation script (c) Jan 2008 by 3 Roads Media (www.3roadsmedia.com)
	This script is freely distributable provided these 2 lines remain intact. */

function checkForm(theForm) {
	// trim extra whitespace from form inputs so they can be properly checked with regex
	var fname = trim(theForm.firstName.value);
	var lname = trim(theForm.lastName.value);
	var email = trim(theForm.email.value);
	
	// validate name and email inputs -- name: letters only, email: name@domain.ext format
	var validName = /^([a-zA-Z]+)$|([a-zA-z]+)\s([a-zA-Z]+)$/;
	var validEmail = /^[^@]+@[^@.]+\.[^@]*\w\w$/;
	
	// if a field is empty or doesn't pass regex requirements, show an error
	if (fname == "" || validName.test(fname) != true) {
		showErr("fnErr", "firstName");
		return false;
	} else if (lname == "" || validName.test(lname) != true) {
		showErr("lnErr", "lastName");
		return false;
	} else if (email == "" || validEmail.test(email) != true) {
		showErr("emErr", "email");
		return false;
	}
	return true;
}

function trim(str) {
	// Special thanks to Breaking Par (www.breakingpar.com) for this function
	var temp = str;
	var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
	if (obj.test(temp)) {
		temp = temp.replace(obj, '$2');
	}
	var obj = /  /g;
	while (temp.match(obj)) {
		temp = temp.replace(obj, " ");
	}
	return temp;
}

function showErr(error, field) {
	hideErr();
	document.getElementById(error).style.display = "inline";
	document.getElementById(field).style.border = "1px solid red";
	document.getElementById(field).select();
	document.getElementById(field).focus();
}

function hideErr() {
	document.getElementById("fnErr").style.display = "none"
	document.getElementById("lnErr").style.display = "none"
	document.getElementById("emErr").style.display = "none"
	document.getElementById("firstName").style.border = "1px solid #a5acb2";
	document.getElementById("lastName").style.border = "1px solid #a5acb2";
	document.getElementById("email").style.border = "1px solid #a5acb2";
}
