var errmsg
var hold

var CheckFormFlag=false;
function SetCheckForm()
{
	CheckFormFlag=true;
}
function ClarCheckForm()
{
	CheckFormFlag=false;
}

function CheckForm()
	{
	if (CheckFormFlag==false)
		return true;
	

	 errmsg=""
	
		// Verify that comments were entered
		 
		if  (comments.comment.value=="" )
			{
			errmsg=errmsg+" - Please enter a comment. \n"
			}
		
		// verify that email address is valid

		if  (comments.email.value!="" )
			{
				FieldValue = "email address";
				retval = CheckEmail(comments.email, FieldValue);
			}

		// verify name is valid
		if  (comments.fname.value!="")
			{
				FieldValue = "first name";
				retval=AlphaValidation(comments.fname,FieldValue);
			}
		if  (comments.lname.value!="")
			{
				FieldValue = "last name"
				retval=AlphaValidation(comments.lname,FieldValue);
			}
		if  (comments.initial.value!="" )
			{
				FieldValue = "middle initial"
				retval=AlphaValidation(comments.initial,FieldValue);
			}
			
		// verify Company Name is valid
		if  (comments.org.value!="")
			{
				FieldValue = "company or organization name"
				retval=AlphaNumeric(comments.org,FieldValue);
			}			

		// verify Street Address is valid
		if  (comments.address1.value!="")
			{
				FieldValue = "street address"
				retval=AlphaNumeric(comments.address1,FieldValue);
			}
		if  (comments.address2.value!="")
			{
				FieldValue = "street address"
				retval=AlphaNumeric(comments.address2,FieldValue);
			}	

		//validate city/state
		if  (comments.city.value!="" )
			{
				FieldValue = "city"
				retval=AlphaValidation(comments.city,FieldValue);
			}
		if  (comments.state.value!="" )
			{
				FieldValue = "state"
				retval=AlphaValidation(comments.state,FieldValue);
			}
		//validate phone number
		if  (comments.phone.value!="" )
			{
				FieldValue = "phone"
				retval=Phone(comments.phone,FieldValue);
			}

		//validate zip code
		if  (comments.zip.value!="")
			{
				FieldValue = "zip code"
				retval=ZipCode(comments.zip,FieldValue);
			}	
		// if any of the above returned an error message	
		if (!errmsg=="")
			{
				alert("There were problems with your submission:\n" + errmsg);
				ClarCheckForm();
				return false;
			}	
				
	}

	function AlphaValidation(field,PrettyName)	
	{
		return testRE("^[-A-Za-z_ ]{0,49}$",field.value," - Please enter a valid " + PrettyName + ".");
	} 
	
	function AlphaNumeric(field,PrettyName)
	{
		return testRE("^[A-Za-z0-9 \.\-]{0,100}$",field.value," - Please enter a valid " + PrettyName + ".");
	} 

	
	function CheckEmail(field,PrettyName)	
	{
	
		return testRE("^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$",field.value," - Please enter a valid " + PrettyName + ".");
	} 

	function ZipCode(field,PrettyName)
	{
	
		return testRE("^[0-9]{5}$|^[0-9]{5}\-[0-9]{4}$",field.value," - Please enter a valid " + PrettyName + ".");
	}

	function Phone(field,PrettyName)
	{
	
		return testRE("^[0-9]{7}$|^[0-9]{10}$|^[0-9]{3}\-[0-9]{4}$|^[0-9]{3}.[0-9]{4}$|^[0-9]{3}\-[0-9]{3}\-[0-9]{4}$|^[0-9]{3}.[0-9]{3}.[0-9]{4}$",field.value," - Please enter a valid " + PrettyName + ".");
	}

function testRE(regExpString, testvalue,ErrorMessage)
{
		var re = new RegExp(regExpString, "g");
		if (re.test(testvalue)) 
		{
			return true;
		}
		else
		{
			errmsg = errmsg + ErrorMessage + "\n";
			return false;
		}
}



