// 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 Panasonic Chef Ken Megarr has been submitted.');
	}
}
function checkEngineerForSubmission()
{
    var fullQueryString = window.location.search.substring(1);
	var isThanks = getSpecificQueryValue(fullQueryString, 'thanks');
	
	if (isThanks)
	{		
		alert('Thank You. Your email to Panasonic Service Engineer 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 += validateFirstName();
	errorMsg += validateLastName();
	errorMsg += validateEmails();
	errorMsg += validatePhone();
	errorMsg += validateComments();
	
	if (errorMsg != "")
	{
		alert("The following fields require correction before submission:\n\n" + errorMsg);
		return false;
	}
	return(true);
}
function validateEngineerFormAndSubmit()
{
    var errorMsg = "";

	errorMsg += validateFirstName();
	errorMsg += validateLastName();
	errorMsg += validateCompany();
	errorMsg += validateEmails();
	errorMsg += validatePhone();
	errorMsg += validateModel();
	errorMsg += validateSubject();
	errorMsg += validateComments();
	
	if (errorMsg != "")
	{
		alert("The following fields require correction before submission:\n\n" + errorMsg);
		return false;
	}
	return(true);
}
// 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 subject for empty field.
function validateSubject()
{
	if (document.form1.subject.value == '')
	{
		return "* Subject: Please enter.\n";
	}
	return "";
}
// Validates company for empty field.
function validateCompany()
{
	if (document.form1.company.value == '')
	{
		return "* Company: Please enter your question.\n";
	}
	return "";
}
// Validates model for empty field.
function validateModel()
{
	if (document.form1.model.value == '')
	{
		return "* Model: Please enter your question.\n";
	}
	return "";
}
// Validates comments for empty field.
function validateComments()
{
	if (document.form1.comments.value == '')
	{
		return "* Comments: Please enter your question.\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 != '')
	{
	    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 "";
}

