//Combined script - Eliminate Spiders
//Set the value of a hidden text field in a form to "Go."  Since spiders don't process javascript(yet), server side conditional should 
//prevent emails, db entries, etc.
//Include this on future forms for validation.
function ValidateForm(theForm)
{
	var ch;
	var i;
	theForm.didJavascript.value = "Go";
	if (theForm.id == "customerSurvey")
	{
		if (button != "submit")
			{
			return(true);
			}
		if (theForm.FirstName.value == "")
			{
				alert("The First Name field must not be blank.");
				theForm.FirstName.focus();
				return (false);
			}	
			
		if (theForm.LastName.value == "")
			{
				alert("The Last Name field must not be blank.");
				theForm.LastName.focus();
				return (false);
			}		
		if (theForm.Email.value == "")
			{
				alert("The Email field must not be blank.");
				theForm.Email.focus();
				return (false);
			}
		if(IsCharInvalid(theForm.TestimonialText.value))
			{
			alert("Certain characters are not allowed in the Comments/Testimonial field.")
			theForm.TestimonialText.focus();
			return (false);
			}
		if (theForm.Email.value != "")
			{
			if (!validEmail(theForm.Email.value))
				{
				alert("The E-mail you entered is invalid. Please try again.");
				theForm.Email.focus();
				return (false);
				}	
			}
	}
}

function validEmail(email) 
{
	invalidChars = " /:,;"
	
	if (email == "") {						// cannot be empty
		return false
	}
	for (i=0; i<invalidChars.length; i++) {	// does it contain any invalid characters?
		badChar = invalidChars.charAt(i)
		if (email.indexOf(badChar,0) > -1) {
			return false
		}
	}
	atPos = email.indexOf("@",1)			// there must be one "@" symbol
	if (atPos == -1) {
		return false
	}
	if (email.indexOf("@",atPos+1) != -1) {	// and only one "@" symbol
		return false
	}
	periodPos = email.indexOf(".",atPos)
	if (periodPos == -1) {					// and at least one "." after the "@"
		return false
	}
	if (periodPos+3 > email.length)	{		// must be at least 2 characters after the "."
		return false
	}
	return true
}

function isInteger (s)
{   var i;

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

// Returns true if character c is a digit 
// (0 .. 9).

function isDigit (c)
{   
	return ((c >= "0") && (c <= "9"))
}

// Check whether string s is empty.

function SetSubmit()
{
	button = "submit";
}

function IsCharInvalid(theString)
{
	var valChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()-+=_’~[]{};:'<>?,./` ";
	valChars = valChars + '"';
	valChars = valChars + "\n";
	valChars = valChars + "\r";
	for (i=0; i < theString.length; i++) 
	{
		if (valChars.indexOf(theString.charAt(i),0) < 0)
		{
				alert("Character not allowed: " + theString.charAt(i));
				return true;
		}
	}
}