//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 == "createReview")
	{
		if (button != "submit")
			{
			return(true);
			}
		if (theForm.rvwemail.value == "")
			{
				alert("E-mail must be entered");
				theForm.rvwemail.focus();
				return (false);
			}	
		if (theForm.rvwname.value == "")
			{
				alert("Name must be entered");
				theForm.rvwname.focus();
				return (false);
			}
		if (theForm.detail.value == "")
			{
				alert("Review text is required");
				theForm.detail.focus();
				return (false);
			}			
		if (theForm.summary.value == "")
			{
				alert("Review title is required");
				theForm.summary.focus();
				return (false);
			}
		if (theForm.rvwmiles.value == "")
			{
				alert("Choose miles, km, or hours");
				theForm.summary.focus();
				return (false);
			}
		if(IsCharInvalid(theForm.detail.value))
		{
			alert("Characters not allowed.")
			theForm.detail.focus();
			return (false);
		}
		if(IsCharInvalid(theForm.summary.value))
		{
			alert("Characters not allowed.")
			theForm.summary.focus();
			return (false);
		}
		if (theForm.rvwemail.value != "")
		{
			if (!validEmail(theForm.rvwemail.value))
			{
				alert("Invalid E-mail address, please verify");
				theForm.rvwemail.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;
		}
	}
}
