// 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==null||theform.address.value=="")
  	{
  		msg = msg + "- The Address cannot be blank\n";
  		pass = 0;
  	}
  	else
  	{
  		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==null||theform.city.value=="")
  	{
  		msg = msg + "- The City cannot be blank\n";
  		pass = 0;
  	}
  	else
   	{
 		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==null||theform.state.value=="")
  	{
  		msg = msg + "- The State cannot be blank\n";
  		pass = 0;
  	}
 		
 	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==null||theform.zip.value=="")
  	{
  		msg = msg + "- The Zip Code cannot be blank\n";
  		pass = 0;
  	}

	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 walkeraddress
	var badChars = "~!@$%^*()_+={}[]:;\"<,>?/"; // illegal characters
	if (isIllegalChars(theform.walkeraddress.value,badChars) != "")
	{
		msg = msg + "- The Walker Address cannot have illegal characters (" + badChars + ") \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 cellphone number
   	if (theform.cell.value > "")
 	{
 		if (theform.cell.value.length < 12 || theform.cell.value.length > 12)
 		{
 			msg = msg + "- The Cell Phone Number must be 12 characters\n";  		
 		}
 		
 		if (isFormatedPhoneNumber(theform.cell.value) == false)
  		{
  			msg = msg + "- The Cell Phone Number is not formatted properly (### - ### - ####)\n";
  		}  	
  		
  		var badChars = " ~!@#$%^&*()_+={}[]:;\"'<,>.?/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; // illegal characters
		if (isIllegalChars(theform.cell.value,badChars) != "")
		{
			msg = msg + "- The Cell Phone Number cannot have illegal characters\n";
			msg = msg + "   (" + badChars.slice(0,42) + ")\n";			
			msg = msg + "   (" + badChars.slice(42) + ")\n";

			pass = 0;
		}
	}
  
  //validate the email address
  	if (theform.email.value > "")
  	{
		if (!(isEmail(theform.email.value)))
		{
			msg = msg + "- The email address is not valid\n";
			pass = 0;
		}
  	}
  	
  	

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

// validators ------------------------------------------------------------------

function isEmpty(s)
{
	if (s.value==null||s.value=="")
	{
	    return true;
    }
    else
    {
    	return false;
    }
}	

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 isValidDate (s)
{
	if (s.length > 5) return 1
	if (s.length < 5) return 2
	if (s.substring(0,2) < 1) return 3
	if (s.substring(0,2) > 12) return 4
	if (s.charAt(2) != "/") return 5
	if (s.substring(3) < "10") return 6
	return 0
}

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 isWrongLength(s,len)
{
	if (s.length > len) return true;
	else if (s.length < len) return true
	return false
}

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