// 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.");
		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 ch;
    	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 validateMailingEntry()
{
    var thisObject;
    var thisField;

    thisObject = document.getElementById( "firstName" );
    if (thisObject==null)
    	return true;  // not relevant?

    thisField = trim( thisObject.value );
    if (thisField == "")  // Required
    {
    	alert("Please enter First Name.");
    	thisObject.focus();
    	return false;
    }

    thisObject = document.getElementById( "lastName" );
    thisField = trim( thisObject.value );
    if (thisField == "")
    {
    	alert("Please enter Last Name.");
    	thisObject.focus();
    	return false;
    }

	// 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();
    	return false;
    }

	// Communities of interest: At least one must be checked.
	var checkedCount = 0;
	var formElements = document.getElementsByName("communitiesInterested[]");
	var listLength = formElements.length;

	var i;
	for (i=0; i<listLength; i++)
	{
		if (formElements[i].checked==true)
		{
			checkedCount++;
			break;
		}
	}
	if (checkedCount == 0)
	{
		alert("Please select at least one Community.");
		return false;
	}

	// Address - Optional
    thisObject = document.getElementById( "address" );
    thisField = trim( thisObject.value );
    thisObject.value = thisField;
    if (thisField == "")
    {
    	alert("Please enter your Address.");
    	thisObject.focus();
    	return false;
    }

    thisObject = document.getElementById( "address2" );
    thisField = trim( thisObject.value );
    thisObject.value = thisField;

    thisObject = document.getElementById( "city" );
    thisField = trim( thisObject.value );
    thisObject.value = thisField;
    if (thisField == "")
    {
    	alert("Please enter your City.");
    	thisObject.focus();
    	return false;
    }

    thisObject = document.getElementById( "zip" );
    thisField = trim( thisObject.value );
    thisObject.value = thisField;
    if (thisField == "")
    {
    	alert("Please enter your Zip Code");
    	thisObject.focus();
    	return false;
	}
	else {
		if (validateZIP(thisField)==false)
		{
			thisObject.focus();
			return false;
		}
    }

	// Phone - Required
    thisObject = document.getElementById( "phone" );
    thisField = trim( thisObject.value );
    thisObject.value = thisField;
	if (thisField != "")
	{
		if (validatePhoneNumber(thisField)==false)
		{
			thisObject.focus();
			thisObject.select();
			return false;
		}
    }

	// Disable submit and return true
	thisObject = document.getElementById( "submit" );
	thisObject.disabled = true;

	return true;

}  // validateMailingEntry


function validateEmailFriendEntry()
{
    var thisObject;
    var thisField;
	
	// Referral Email - Required
    thisObject = document.getElementById( "ref_email" );
    thisField = trim( thisObject.value );
    thisObject.value = thisField;
    if (thisField == "")
    {
    	alert("Please enter your friend's email.");
    	thisObject.focus();
    	return false;
    }

    if (validateEmail(thisField)==false)
    {
    	thisObject.focus();
    	return false;
    }


	//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();
    	return false;
    }
	

    thisObject = document.getElementById( "message" );
    if (thisObject==null)
    	return true;  // not relevant?

    thisField = trim( thisObject.value );
    if (thisField == "")  // Required
    {
    	alert("Please enter a message.");
    	thisObject.focus();
    	return false;
    }
	
	
	// Disable submit and return true
	thisObject = document.getElementById( "submit" );
	thisObject.disabled = true;

	return true;

}  // validateEmailFriendEntry


function validateContactEntry()
{
    var thisObject;
    var 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();
    	return false;
    }
	
	//Validate  Phone
    thisObject = document.getElementById( "phone" );
    thisField = trim( thisObject.value );
    thisObject.value = thisField;
    if (thisField != "")
    {
		if (validatePhoneNumber(thisField)==false)
		{
			thisObject.focus();
			return false;
		}
	}
	
	
    thisObject = document.getElementById( "message" );
    if (thisObject==null)
    	return true;  // not relevant?

    thisField = trim( thisObject.value );
    if (thisField == "")  // Required
    {
    	alert("Please enter a message.");
    	thisObject.focus();
    	return false;
    }
	
	
	// Disable submit and return true
	thisObject = document.getElementById( "submit" );
	thisObject.disabled = true;

	return true;

}  // validateContactEntry


function validateServiceRequestEntry()
{
    var thisObject;
    var thisField;


	//Validate First Name - Required
    thisObject = document.getElementById( "firstName" );
    if (thisObject==null)
    	return true;  // not relevant?

    thisField = trim( thisObject.value );
    if (thisField == "")  // Required
    {
    	alert("Please enter your first name.");
    	thisObject.focus();
    	return false;
    }
	
	//Validate Last Name - Required
    thisObject = document.getElementById( "lastName" );
    if (thisObject==null)
    	return true;  // not relevant?

    thisField = trim( thisObject.value );
    if (thisField == "")  // Required
    {
    	alert("Please enter your last name.");
    	thisObject.focus();
    	return false;
    }
	
	//Validate Daytime Phone - Required/phone
    thisObject = document.getElementById( "phone" );
    if (thisObject==null)
    	return true;  // not relevant?

    thisField = trim( thisObject.value );
    if (thisField == "")  // Required
    {
    	alert("Please enter your daytime phone.");
    	thisObject.focus();
    	return false;
    }
	else {
		if (validatePhoneNumber(thisField)==false)
		{
			thisObject.focus();
			thisObject.select();
			return false;
		}
	}

	//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();
    	return false;
    }
	
	//Validate Secondary Phone
    thisObject = document.getElementById( "phone_secondary" );
    thisField = trim( thisObject.value );
    thisObject.value = thisField;
    if (thisField != "")
    {
		if (validatePhoneNumber(thisField)==false)
		{
			thisObject.focus();
			return false;
		}
	}

	//Validate Secondary Email
    thisObject = document.getElementById( "email_secondary" );
    thisField = trim( thisObject.value );
    thisObject.value = thisField;
    if (thisField != "")
    {
		if (validateEmail(thisField)==false)
		{
			thisObject.focus();
			return false;
		}
	}
	

	//Address - Required
    thisObject = document.getElementById( "address" );
    if (thisObject==null)
    	return true;  // not relevant?

    thisField = trim( thisObject.value );
    if (thisField == "")  // Required
    {
    	alert("Please enter your address.");
    	thisObject.focus();
    	return false;
    }
	
	
	//City - Required
    thisObject = document.getElementById( "city" );
    if (thisObject==null)
    	return true;  // not relevant?

    thisField = trim( thisObject.value );
    if (thisField == "")  // Required
    {
    	alert("Please enter your city.");
    	thisObject.focus();
    	return false;
    }
	
	//Zip - Required
    thisObject = document.getElementById( "zip" );
    if (thisObject==null)
    	return true;  // not relevant?

    thisField = trim( thisObject.value );
    if (thisField == "")  // Required
    {
    	alert("Please enter your zip.");
    	thisObject.focus();
    	return false;
    }
	if (validateZIP(thisField)==false)
	{
		thisObject.focus();
		return false;
	}
	
	//Community - Required
    thisObject = document.getElementById( "community" );
	if (thisObject.options[0].selected == true)
	{
    	alert("Please enter your community.");
		return false;
	}
	
	//Closing Month - Required
    thisObject = document.getElementById( "closingMonth" );
	if (thisObject.options[0].selected == true)
	{
    	alert("Please enter your month of home closing.");
		return false;
	}
	
	//Closing Year - Required
    thisObject = document.getElementById( "closingYear" );
	if (thisObject.options[0].selected == true)
	{
    	alert("Please enter your year of home closing.");
		return false;
	}
	
	//Service Time - Required
    thisObject = document.getElementById( "preferredServiceTime" );
    if (thisObject==null)
    	return true;  // not relevant?

    thisField = trim( thisObject.value );
    if (thisField == "")  // Required
    {
    	alert("Please enter your preferred time of service.");
    	thisObject.focus();
    	return false;
    }

	//Validate Message
    thisObject = document.getElementById( "message" );
    thisField = trim( thisObject.value );
    if (thisObject==null)
    	return true;  // not relevant?

    thisField = trim( thisObject.value );
    if (thisField == "")  // Required
    {
    	alert("Please enter a message.");
    	thisObject.focus();
    	return false;
    }
	
	
	// Disable submit and return true
	thisObject = document.getElementById( "submit" );
	thisObject.disabled = true;

	return true;

}  // validateServiceRequestEntry
