
function isEmailAddress(email)
{
	var result = false
	var theStr = new String(email)
	var index = theStr.indexOf("@");
	if (index > 0)
	{
		var pindex = theStr.indexOf(".",index);
		if ((pindex > index+1) && (theStr.length > pindex+1))
		result = true;
	}
	return result;
}
function trim(inputString)
{
	// Removes leading and trailing spaces from the passed string. Also removes
	// consecutive spaces and replaces it with one space. If something besides
	// a string is passed in (null, custom object, etc.) then return the input.
	if (typeof inputString != "string")
	{
		return inputString;
	}
	var retValue = inputString;
	var ch = retValue.substring(0, 1);
	while (ch == " ")
	{ // Check for spaces at the beginning of the string
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}
	ch = retValue.substring(retValue.length-1, retValue.length);
	while (ch == " ")
	{ // Check for spaces at the end of the string
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}
	while (retValue.indexOf("  ") != -1)
	{ // Note that there are two spaces in the string - look for multiple spaces within the string
		retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
	}
	return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

function onFormSubmit() {
     var ret = false;     
     //alert('here');
     try {
          ret = Validate();
          //alert(ret);
          if (ret) {
               document.surveyForm.submit();
          }
     }
     catch(err) {
          alert(err.description);
          ret = false;          
     }
     return ret;     
}

function Validate()
{
     var ret = false;
	var strMessage = "";
	var strFocus = "";
	if (trim(document.surveyForm.FirstName.value) == "" || document.surveyForm.FirstName.value.length < 1)
	{
		strMessage = strMessage + "First Name is a required field. \n";
		if (strFocus == "")
		{
			strFocus = "FirstName"
		}
	}

	if (trim(document.surveyForm.LastName.value) == "" || document.surveyForm.LastName.value.length < 1)
	{
		strMessage = strMessage + "Last Name is a required field. \n";
		if (strFocus == "")
		{
			strFocus = "LastName";
		}
	}


	if (trim(document.surveyForm.Address1.value) == "" || document.surveyForm.Address1.value.length < 1)
	{
		strMessage = strMessage + "Address 1 is a required field. \n";
		if (strFocus == "")
		{
			strFocus = "Address1";
		}
	}
	if (trim(document.surveyForm.City.value) == "" || document.surveyForm.City.value.length < 1)
	{
		strMessage = strMessage + "City is a required field. \n";
		if (strFocus == "")
		{
			strFocus = "City";
		}
	}
	if (document.surveyForm.State.value == "")
	{
		strMessage = strMessage + "State is a required field. \n";
		if (strFocus == "")
		{
			strFocus = "State";
		}
	}
	if (trim(document.surveyForm.Zip.value) == "" || document.surveyForm.Zip.value.length < 1)
	{
		strMessage = strMessage + "Zip is a required field. \n";
		if (strFocus == "")
		{
			strFocus = "Zip";
		}
	}
	else
	{
		if (document.surveyForm.Zip.value.length  < 5)
		{
			strMessage = strMessage + "Zip should be 5 numbers long. \n";
			if (strFocus == "")
			{
				strFocus = "Zip";
			}
		}
		if (isNaN(document.surveyForm.Zip.value))
		{
			strMessage = strMessage + "Zip should be all numeric. \n";
			if (strFocus == "")
			{
				strFocus = "Zip";
			}
		}
	}
	if (trim(document.surveyForm.Email.value) == "" || document.surveyForm.Email.value.length < 1)
	{
		strMessage = strMessage + "Email is a required field. \n";
		if (strFocus == "")
		{
			strFocus = "Email";
		}
	}
	else
	{
		if (!isEmailAddress(document.surveyForm.Email.value))
		{
			strMessage = strMessage + "Email is not valid. \n";
			if (strFocus == "")
			{
				strFocus = "Email"
			}
		}
	}
	if (document.surveyForm.FulFillTypeID.value == "")
	{
		strMessage = strMessage + "Recipe Booklet is a required field. \n";
		if (strFocus == "")
		{
			strFocus = "FulFillTypeID";
		}
	}


	if (strMessage != "")
	{
		alert("Please correct the following errors. \n" + strMessage);
		switch (strFocus)
		{
			case "FirstName": document.surveyForm.FirstName.focus();
				break;
			case "LastName": document.surveyForm.LastName.focus();
				break;
			case "Address1": document.surveyForm.Address1.focus();
				break;
			case "City": document.surveyForm.City.focus();
				break;
			case "State": document.surveyForm.State.focus();
				break;
			case "Zip": document.surveyForm.Zip.focus();
				break;
			case "Email": document.surveyForm.Email.focus();
				break;
			case "FulFillTypeID": document.surveyForm.FulFillTypeID.focus();
				break;
			}
			ret = false;
	}
	else
	{
	     ret = true
	}
	return ret;
}
			
