/********************************************************************/
/*	
	utils functions used for form validation
	file includes functions
		checkEmail
			paremeters:
				pEmail -> String, the value to check if a proper
				email address
				returns TRUE : FALSE								
		isNumeric
			parameters:
				pText -> String, value to check if number or not
				returns TRUE : FALSE								
		isStringWhitespace
			paremeters:
				str -> value to check if anything other than a 
				space is present
				returns TRUE : FALSE	
																	*/
/********************************************************************/
function validateForm(f)
{
	errorMsg = "Please enter the following required fields:\n";
	isError = false;
	strFirstName	= trim(f.firstName.value);
	strLastName		= trim(f.lastName.value);
	strAddress		= trim(f.streetAddress.value);
	strCity			= trim(f.city.value);
	strState		= trim(f.state.value);
	strZip			= trim(f.zipCode.value);
	strCountry		= trim(f.country.value);
	strEmail		= trim(f.emailAddress.value);
	strPhone1		= trim(f.phone1.value);
	strPhone2		= trim(f.phone2.value);
	strPhone3		= trim(f.phone3.value);
	strComments		= trim(f.comments.value);

	if(isStringWhitespace(strFirstName)){
		errorMsg += "First Name\n";
		isError = true; }
	if(isStringWhitespace(strLastName)){
		errorMsg += "Last Name\n";
		isError = true; }
	if(isStringWhitespace(strAddress)){
		errorMsg += "Address\n";
		isError = true; }
	if(isStringWhitespace(strCity)){
		errorMsg += "City\n";
		isError = true; }
	if(isStringWhitespace(strState)){
		errorMsg += "State\n";
		isError = true; }
	if(isStringWhitespace(strZip) || !isNumeric(strZip) || strZip.length < 5){
		errorMsg += "Zip Code\n";
		isError = true; }
	if(isStringWhitespace(strCountry)){
		errorMsg += "Country\n";
		isError = true; }
	if(!checkEmail(strEmail)){
		errorMsg += "Email Address\n";
		isError = true; }
	if(isStringWhitespace(strPhone1) || !isNumeric(strPhone1) || strPhone1.length != 3){
		errorMsg += "Area Code\n";
		isError = true; }
	if(isStringWhitespace(strPhone2) || !isNumeric(strPhone2) || strPhone2.length != 3){
		errorMsg += "Phone Prefix\n";
		isError = true; }
	if(isStringWhitespace(strPhone3) || !isNumeric(strPhone3)|| strPhone3.length != 4){
		errorMsg += "Phone Suffix\n";
		isError = true; }
	if(isStringWhitespace(strComments)){
		errorMsg += "Enter your comments or question\n";
		isError = true; }
	if(isError){
		alert(errorMsg);
		return false; }
}

function checkEmail(pEmail) 
{	
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(pEmail)){
		return (true)}
	return false;
}

function isNumeric(pText)
{
	var ValidChars = "0123456789";
	var isNumber = true;
	var Char;

		for (i = 0; i < pText.length && isNumber == true; i++) 
		{ 
			Char = pText.charAt(i); 
			if (ValidChars.indexOf(Char) == -1) 
			{
				isNumber = false;
			}
		}
	return isNumber;
}

var whitespace = " \t\n\r";
function isEmpty(s){   
	return ((s == null) || (s.length == 0));
}
function isStringWhitespace(str){   
	var i;
	if (isEmpty(str)) return true;
	for (i = 0; i < str.length; i++){
		var c = str.charAt(i);
		if (whitespace.indexOf(c) == -1) return false;
	}
	return true;
}

function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}