// Trim off spaces at the beginning and end.
function trim( inputString )
{
	var trimmedString;
	var ch;

	if (typeof inputString != "string")
	{
		return inputString;
	}

	trimmedString = inputString;
	ch = trimmedString.substring(0, 1);

	// Check for spaces at the beginning of the string: \t\n\r
	while ((ch == " ") || (ch == "\n") || (ch == "\n") || (ch == "\r") || (ch == "\t"))
	{
		trimmedString = trimmedString.substring(1, trimmedString.length);
		ch = trimmedString.substring(0, 1);
	}

	// Check for spaces at the end of the string
	ch = trimmedString.substring(trimmedString.length-1, trimmedString.length);
	while (ch == " ")
	{
		trimmedString = trimmedString.substring(0, trimmedString.length-1);
		ch = trimmedString.substring(trimmedString.length-1, trimmedString.length);
	}

	return trimmedString;

} // trim


function validateZIP(zipField)
{
	var valid = "0123456789-";
	var hyphenCount = 0;

	if (zipField.length!=5 && zipField.length!=10)
	{
		alert("Please enter your 5 digit (12345) or 5 digit+4 (12345-6789) zip code. Thank you!");
		return false;
	}

	for (var i=0; i < zipField.length; i++)
	{
		temp = "" + zipField.substring(i, i+1);
		if (temp == "-") hyphenCount++;
		if (valid.indexOf(temp) == "-1")
		{
			alert("Invalid characters in your zip code. Please try again.");
			return false;
		}
		if ((hyphenCount > 1) || ((zipField.length==10) && ""+zipField.charAt(5)!="-"))
		{
			alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'. Please try again.");
			return false;
	   	}
	}
	return true;

}  // validateZIP


function validateEmail(emailField)
{
	// Legal email characters per RFC2821 and RFC2822
  	var allowableChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.-+@";
  	var firstAt = 0;

    // Check for validity of email format, allowing for one letter domain names
    // e.g. a@b.com; and for @ characters anywhere after the first character.
    if ((emailField.indexOf(".") > 2) && ((firstAt = emailField.indexOf("@")) > 0))
    {
    	// See if there are more than one @
		if (emailField.indexOf("@", firstAt+1) != -1)
		{
			alert("Email address has more than one @.");
			return false;
		}

    	// See if there are contiguous periods
		if (emailField.indexOf("..") != -1)
		{
			alert("Email address has contiguous periods.");
			return false;
		}

    	var i, j;
		for (i = 0;  i < emailField.length;  i++)
		{
			ch = emailField.charAt(i);
			for (j = 0; j < allowableChars.length; j++)
			{
				if (ch == allowableChars.charAt(j))
						break;
				else
				{
					if (j < (allowableChars.length - 1)) continue;
					alert("Email address contains invalid character(s).");
					return false;
				}
			}
		}

		return true;
	}

	alert( "Please enter a valid email address such as yourname@companyname.com." );
	return false;

}  // validateEmail



function validatePhoneNumber(usPhoneNumber)
{
    if(usPhoneNumber.search(/^(\d\d\d)\-?(\d\d\d)\-?(\d\d\d\d)$/)==-1)    {
		alert( "Please enter a valid phone number. Valid phone numbers should only include numbers such as 123-456-7890." );
    	return false;
    }

    return true;

} // validatePhoneNumber


function validateContactFields()
{
    var thisObject;
    var thisField;

    thisObject = document.getElementById( "firstName" );
    thisField = trim( thisObject.value );
    if (thisField == "")  // Required
    {
    	alert("Please enter First Name.");
    	thisObject.focus();
		thisObject.select();
    	return false;
    }
	thisObject.value = thisField;

    thisObject = document.getElementById( "lastName" );
    if (thisObject==null)
    	return true;  // not relevant?

    thisField = trim( thisObject.value );
    if (thisField == "")
    {
    	alert("Please enter Last Name.");
    	thisObject.focus();
		thisObject.select();
    	return false;
    }
	thisObject.value = thisField;

	// Email - Required
    thisObject = document.getElementById( "email" );
    thisField = trim( thisObject.value );
    thisObject.value = thisField;
    if (thisField == "")
    {
    	alert("Please enter your Email Address.");
    	thisObject.focus();
    	return false;
    }

    if (validateEmail(thisField)==false)
    {
    	thisObject.focus();
		thisObject.select();
    	return false;
    }
	thisObject.value = thisField;

	// Phone
    thisObject = document.getElementById( "phoneNumber" );
    thisField = trim( thisObject.value );
    thisObject.value = thisField;
    if (thisField != "")
    {
		if (validatePhoneNumber(thisField)==false)
		{
			thisObject.focus();
			thisObject.select();
			return false;
		}
	}

	// Address
	thisObject = document.getElementById( "address1" );
	thisField = trim( thisObject.value );
	thisObject.value = thisField;

	thisObject = document.getElementById( "address2" );
	thisField = trim( thisObject.value );
	thisObject.value = thisField;

 	// City
	thisObject = document.getElementById( "city" );
	thisField = trim( thisObject.value );
	thisObject.value = thisField;

	// State
    thisObject = document.getElementById( "state" );
    thisField = trim( thisObject.value );
    thisObject.value = thisField;
    if ((thisField.length != 2) && (thisField != ""))
    {
    	alert("Please enter your 2 letter State Code.");
    	thisObject.focus();
		thisObject.select();
    	return false;
    }

	// Zip
    thisObject = document.getElementById( "zip" );
    thisField = trim( thisObject.value );
    thisObject.value = thisField;
    if (thisField != "")
    {
		if (validateZIP(thisField)==false)
		{
			thisObject.focus();
			thisObject.select();
			return false;
		}
	}

	// Comments
    thisObject = document.getElementById( "comments" );
    thisField = trim( thisObject.value );
    thisObject.value = thisField;

}  // validateContactFields
