// This function will validate a form
function validateForm(theform)
{
	pass = 1; //assume everything is ok
  	msg = "The following problems where found in trying to submit this form:\n\n";

  //validate the name
   	if (theform.name.value==null||theform.name.value=="")
  	{
    	msg = msg + "- The Name cannot be blank\n";
  	 	pass = 0;
  	}
  	else
  	{
 		var badChars = "~!@#$%^*()_-+={}[]:;\"'<,>.?/"; // illegal characters
		if (isIllegalChars(theform.name.value,badChars) == true)
		{
			msg = msg + "- The Name cannot have illegal characters (" + badChars + ") \n";
			pass = 0;
		}
	}	
 
 //validate the address
  	if (theform.address.value > "")
  	{
  		var badChars = "~!@$%^*()_+={}[]:;\"<,>?/"; // illegal characters
		if (isIllegalChars(theform.address.value,badChars) != "")
		{
			msg = msg + "- The Address cannot have illegal characters (" + badChars + ") \n";
			pass = 0;
		}
	}	  
  
 //validate the city
  	if (theform.city.value > "")
   	{
 		var badChars = "~!@#$%^&*()_+={}[]:;\"'<,>.?/"; // illegal characters
		if (isIllegalChars(theform.city.value,badChars) != "")
		{
			msg = msg + "- The City cannot have illegal characters (" + badChars + ") \n";
			pass = 0;
		}
	}	
 
   //validate the state
  	if (theform.state.value > "")
 	{	
	 	if (theform.state.value.length < 2 || theform.state.value.length > 2)
	 	{
	 		msg = msg + "- The State must be 2 characters\n";
	 	}
	 
	 	var badChars = "~!@#$%^&*()_-+={}[]:;\"'<,>.?/0123456789"; // illegal characters
		if (isIllegalChars(theform.state.value,badChars) != "")
		{
			msg = msg + "- The State cannot have illegal characters (" + badChars + ") \n";
			pass = 0;
		}
	}
	
   //validate the zip
  	if (theform.zip.value > "")
  	{
		if (theform.zip.value.length < 5 || theform.zip.value.length > 5)
		{
			msg = msg + "- The Zip Code must be 5 characters\n";
		}
 
		var badChars = "~!@#$%^&*()_-+={}[]:;\"'<,>.?/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; // illegal characters
		if (isIllegalChars(theform.zip.value,badChars) != "")
		{
			msg = msg + "- The Zip Code cannot have illegal characters (" + badChars + ") \n";
			pass = 0;
		}
	}
	
  //validate the email address
   	if (theform.email.value==null||theform.email.value=="")
  	{
    	msg = msg + "- The Email Address cannot be blank\n";
  	 	pass = 0;
  	}
  	else
  	{
		if (isEmail(theform.email.value)==false)
		{
			msg = msg + "- The email address is not valid\n";
			pass = 0;
		}
  	}
  	
 //validate the phone number
   	if (theform.phone.value > "")
 	{
  		if (theform.phone.value.length < 12 || theform.phone.value.length > 12)
 		{
 			msg = msg + "- The Phone Number must be 12 characters\n";  		
 		}
 		
 		if (isFormatedPhoneNumber(theform.phone.value) == false)
  		{
  			msg = msg + "- The Phone Number is not formatted properly (### - ### - ####)\n";
  		}  	
  		
  		var badChars = " ~!@#$%^&*()_+={}[]:;\"'<,>.?/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; // illegal characters
		if (isIllegalChars(theform.phone.value,badChars) != "")
		{
			msg = msg + "- The Phone Number cannot have illegal characters\n";
			msg = msg + "   (" + badChars.slice(0,42) + ")\n";			
			msg = msg + "   (" + badChars.slice(42) + ")\n";

			pass = 0;
		}
	}

  //validate the subject
   	if (theform.subject.value==null||theform.subject.value=="")
  	{
    	msg = msg + "- The Subject cannot be blank\n";
  	 	pass = 0;
  	}
  	else
  	{
  		if (theform.subject.value.length > 60)
 		{
 			msg = msg + "- The Subject cannot exceed 60 characters\n";  		
 		}
	}

  //validate the note
   	if (theform.note.value==null||theform.note.value=="")
  	{
    	msg = msg + "- The Message cannot be blank\n";
  	 	pass = 0;
  	}
  	else
  	{
 		if (theform.message.value.length  > 500)
		{
			msg = msg + "- The Message cannot exceed 500 characters\n";
		}
	}

	if (pass == 1)
	{
		return true;
	}
	else
	{
		alert(msg);
		return false;
	}
}

// validators ------------------------------------------------------------------
	
function isEmpty (s) 
{
	var p = /\S+/;
	return !p.test(s);
}

function isEmail(string) 
{
    if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
        return true;
    else
        return false;
}

function isAlphaNum(string) 
{
    if (string.search(/^[A-Za-z0-9]+$/) != -1)
        return true;
    else
        return false;
}

function checknumber(string)
{
	var anum=/(^\d+$)|(^\d+\.\d+$)/
	if (anum.test(string)) 
		testresult=true 
	else 
		testresult=false
	return (testresult)
}

function isExecutable (s) 
{
	var p = /\.(bat|com|dll|exe|vbs)$/i;
	return p.test(s);
}

function isImage (s) 
{
	var p = /\.(gif|jpg)$/i;
	return p.test(s);
}

function isUrl (s) 
{
	var p = /^(http|https|ftp):\/\/\S+\.[^\.\s]{2,4}(\/\S*)?$/i;
	return p.test(s);
}

function isIllegalChars(s,illegalChars) 
{
    for (i = 0; i <= illegalChars.length - 1; i++)
    {
 		for (j = 0; j <= s.length - 1; j++)
 		{
 			if (s.charAt(j) == illegalChars.charAt(i))
 			{
 				return true
 			}
 		}
    }
    return false
}

function isFormatedPhoneNumber(s)
{
	if (s.charAt(3) != "-") return false
	if (s.charAt(7) != "-") return false
	else return true
}

