

//////////////////////////////////////////////////////////////////////////
// This function does form-checking                                     //
//////////////////////////////////////////////////////////////////////////
// This script was written by Anh Gross for Powell Internet (pint.com)  //
//////////////////////////////////////////////////////////////////////////
function stripNotAllowable(s, allowable)
{ 
	var returnString = "";

  // Search through string's characters one by one.  If character is allowable, append to returnString.	
	for (var i = 0; i < s.length; i++)
	{   
	   var c = s.charAt(i);
	   if (allowable.indexOf(c) != -1) 
		 	returnString += c;
	}
	
	return returnString;
}

var cm = "0";
function checkForm()
{
	var errorNote = "";
	var error_message = "This form has not been sent because:\n\n";
	
	if (document.forms['theForm'].fName.value == "")
		errorNote += "* You did not provide a first name.\n\n";
	
	if (document.forms['theForm'].lName.value == "")
		errorNote += "* You did not provide a last name.\n\n";
	
	if (document.forms['theForm'].email.value == "")
		errorNote += "* You did not provide a reply-to email address.\n\n";
		
	if (document.forms['theForm'].company.value == "")
		errorNote += "* You did not provide a company name.\n\n";
		
	if (document.forms['theForm'].url.value == "")
		errorNote += "* You did not provide a corporate URL.\n\n";
		
	else
	{
		// Check validity of email addresses by looping through the array
		var email = document.forms['theForm'].email.value;
		var emailSize = email.length;
			
		// check for illegal characters in email
		var illegalChars = stripNotAllowable(email, "'\"\\/()`~!#$%^&*+}{|:;?><,[]");
		var firstChar = stripNotAllowable(email.charAt(0), ".@");
		var lastChar = stripNotAllowable(email.charAt(emailSize - 1), ".@");
			
		// email length must be > 5, there must be an @ and a . and 
		// they can not be at the end of the email string
		if (emailSize < 5 || email.indexOf('@') == -1 || firstChar != "" || lastChar != "")
	  		var badformat = true;
		else
		{
			var firstAt = email.indexOf('@');
			var afterAt = email.substring(firstAt +1, emailSize);
			
			var Atcounts = stripNotAllowable(afterAt, "@");
			var Pcounts	 = stripNotAllowable(afterAt, ".");
			
			// more @ after first @ or no . after first @ or period 
			// is found right after first @ set badformat to true
			badformat = (Atcounts.length > 0 || Pcounts == 0 || email.charAt(firstAt+1)== '.' || illegalChars != '')? true:false;
		}
		
		if (badformat == true)
			errorNote += "* your email: " + email + ": wrong format\n\n";	
	}
	
	if (document.forms['theForm'].subject.value == "")
		errorNote += "* You did not provide a subject.\n\n";
	
	if (document.forms['theForm'].email.value == "")
		errorNote += "* You did not provide a message.\n\n";
		
	if (errorNote != "")   alert(error_message + errorNote + "Please go back and correct the information.");
	else 
	{
		document.forms['theForm'].action = "sendmail.php";
		document.forms['theForm'].submit();
	}
}