
function validate(theForm)
{

	//First Name
	if (theForm.first_name.value.length < 2)
	{
		alert("Oops!  You forgot to provide your first name.");
		theForm.first_name.focus();
		theForm.first_name.select();
		theForm.action = "";
		return false;
	}
	
	//Last Name
	if (theForm.last_name.value.length < 2)
	{
		alert("Oops!  You forgot to provide your last name.");
		theForm.last_name.focus();
		theForm.last_name.select();
		theForm.action = "";
		return false;
	}
	
	//Street
	if (theForm.street.value.length < 1)
	{
		alert("Oops!  You forgot to provide your street address.");
		theForm.street.focus();
		theForm.street.select();
		theForm.action = "";
		return false;
	}
	
	//City Validation
	if (theForm.city.value.length < 1)
	{
		alert("Oops!  You forgot to list your City.");
		theForm.city.focus();
		theForm.city.select();
		theForm.action = "";
		return false;
	}
	
	//State Validation
	if (theForm.country.value == "United States")
	{
		var rgxAmericanStateShort = /(AL|AK|AZ|AR|CA|CO|CT|DE|DC|FL|GA|HI|ID|IL|IN|IA|KS|KY|LA|ME|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VT|VA|WA|WV|WI|WY)/im;
										
		var rgxAmericanState = /(Alabama|Alaska|Arizona|Arkansas|California|Colorado|Connecticut|Delaware|District of Columbia|Florida|Georgia|Hawaii|Idaho|Illinois|Indiana|Iowa|Kansas|Kentucky|Louisiana|Maine|Maryland|Massachusetts|Michigan|Minnesota|Mississippi|Missouri|Montana|Nebraska|Nevada|New Hampshire|New Jersey|New Mexico|New York|North Carolina|North Dakota|Ohio|Oklahoma|Oregon|Pennsylvania|Rhode Island|South Carolina|South Dakota|Tennessee|Texas|Utah|Vermont|Virginia|Washington|West Virginia|Wisconsin|Wyoming)/im;
		
		if (theForm.state.value.length == 2)
		{
			if (theForm.state.value.search(rgxAmericanStateShort) == -1)
			{
				alert("Oops!  The value you provided for state is not recognized as a US state.  Please provide a valid US State.");
				theForm.state.focus();
				theForm.state.select();
				theForm.action = "";
				return false;
			}
		}
		else
		{
			if (theForm.state.value.search(rgxAmericanState) == -1)
			{
				alert("Oops!  The value you provided for state is not recognized as a US state.  Please provide a valid US State.");
				theForm.state.focus();
				theForm.state.select();
				theForm.action = "";
				return false;
			}
		}
	}
	else
	{
		if (theForm.state.value.length < 1)
		{
			alert("Oops!  You forgot to list your state or province.");
			theForm.state.focus();
			theForm.state.select();
			theForm.action = "";
			return false;
		}
	}
	
	//Zip Code Validation
	if (theForm.zip.value.length < 1)
	{
		alert("Oops!  You forgot to list your Zip or Postal code.");
		theForm.zip.focus();
		theForm.zip.select();
		theForm.action = "";
		return false;
	}
	
	//Email Validation
	
	var rgxEmailAddress = /^[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$/img;
	
	if (theForm.email.value.search(rgxEmailAddress) == -1)
	{
		alert("Oops! There seems to be a problem with the email address you have provided.  Please check that you have entered the address correctly.");
		theForm.email.focus();
		theForm.email.select();
		theForm.action = "";
		return false;
	}
	
	//Telephone Validation
	if (theForm.country.value == "United States")
	{
		if (!validateAmericanPhoneNumber(theForm.phone.value))
		{
			alert("Oops! The phone number you provided doesn't appear to be a valid US phone number.  Please provide your full phone number with area code.");
			theForm.phone.focus();
			theForm.phone.select();
			theForm.action = "";
			return false;
		}
		
		if(isBadPhoneNumber(theForm.phone.value))
		{
			alert("Oops! The phone number you provided doesn't appear to be a valid US phone number.  Please provide a real US phone number with area code.");
			theForm.phone.focus();
			theForm.phone.select();
			theForm.action = "";
			return false;
		}			
	}
	else
	{
		if (theForm.phone.value.length < 1)
		{
			alert("Oops! Please provide a phone number (with country code) where we can contact you.");
			theForm.phone.focus();
			theForm.phone.select();
			theForm.action = "";
			return false;
		}
	}

	theForm.action = "https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8";
	return true;
	
}

function validateAmericanPhoneNumber(phoneNumber)
{
	var rgxPhone1 = /\(?\b[0-9]{3}\)?[\-. ]?[0-9]{3}[\-. ]?[0-9]{4}\b/img;
	
	if (phoneNumber.search(rgxPhone1) == -1)
	{
		return false;
	}
	else 
	{
		return true;
	}
}

function isBadPhoneNumber(phoneNumber)
{
	var rgxBogusPhoneNumbers = /(1234567890|0987654321|9876543210|0{10}|1{10}|2{10}|3{10}|4{10}|5{10}|6{10}|7{10}|8{10}|9{10}|[0-9]{3}555[0-9]{4})/im;
	
	cleanNumber = phoneNumber.replace(/(\D+)/img, "");
	
	//alert("Clean Number: " + cleanNumber + "\r\n" + cleanNumber.search(rgxBogusPhoneNumbers));
	
	if (cleanNumber.search(rgxBogusPhoneNumbers) != -1)
	{
		return true;
	}
	else return false;
	
}
	
