// MNB
// 9/29/09

//called on body load
function checkForSubmission()
{
	var fullQueryString = window.location.search.substring(1);
	var isThanks = getSpecificQueryValue(fullQueryString, 'thanks');
	
	if (isThanks)
	{		
		alert('Thank You. Your email to the PHEC ventilation fans division has been submitted.');
	}
}
function getSpecificQueryValue(fullQueryString, id)
{
	var queryStringArray = fullQueryString.split("&");

	for (i=0;i<queryStringArray.length;i++)
	{
		currentIdValuePair = queryStringArray[i].split("=");

		if (currentIdValuePair[0] == id)
		{
			return currentIdValuePair[1];
		}
	}
}
function validateFormAndSubmit()
{
	var errorMsg = "";

	errorMsg += validateCompany();
	errorMsg += validateFirstName();
	errorMsg += validateLastName();
	errorMsg += validateAddress();
	errorMsg += validateCity();
	errorMsg += validateState();
	errorMsg += validateZip();
	errorMsg += validatePhone();
	errorMsg += validateEmails();
	
	if (errorMsg != "")
	{
		alert("The following fields require correction before submission:\n\n" + errorMsg);
		return false;
	}
	return(true);
}
// Validates Company for empty field.
function validateCompany()
{
	if (document.form1.company.value == '')
	{
		return "* Company: Please enter.\n";
	}
	return "";
}
// Validates first name for empty field.
function validateFirstName()
{
	if (document.form1.first_name.value == '')
	{
		return "* First Name: Please enter.\n";
	}
	return "";
}
// Validates last name for empty field.
function validateLastName()
{
	if (document.form1.last_name.value == '')
	{
		return "* Last Name: Please enter.\n";
	}
	return "";
}
// Validates address for empty field.
function validateAddress()
{
	if (document.form1.address_1.value == '')
	{
		return "* Address 1: Please enter.\n";
	}
	return "";
}
// Validates city for empty field.
function validateCity()
{
	if (document.form1.city.value == '')
	{
		return "* City: Please enter.\n";
	}
	return "";
}
// Validates state for lack of selection.
function validateState()
{
	if (document.form1.state.value == '')
	{
		return "* State: Please enter.\n";
	}
	return "";
}
// Validates zip for empty field.
function validateZip()
{
	if (document.form1.zip.value == '')
	{
		return "* Zip: Please enter.\n";
	}
	return "";
}
// Validates phone for empty field as well as ensures proper format.
function validatePhone()
{
	var reg = new RegExp(/^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/);
	var phone = document.form1.phone.value;
	
	if (phone == '')
	{
		return "* Phone: Please enter.\n";
	}
	else if (reg.test(phone) == false) 
	{
		return "* Phone: Must be in format (xxx) xxx-xxxx.\n";
	}
	
	return "";
}
// Validates emails for empty field, confirmation that both emails are the same, as well as ensures proper format.
function validateEmails()
{
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var address = document.form1.email.value;
	
	if (address == '')
	{
		return "* Email: Please enter.\n";
	}
	else if (reg.test(address) == false) 
	{
		return "* Email: Please enter a valid email address.\n";
	}
	
	return "";
}
// Alerts the user with a call number.
function alertPhoneNumber()
{
	alert('Please call us toll-free at 877-958-4372 for assistance.');
}
