
function validate(){
var msg = "The form you are trying to submit has the following errors:\n\n";
	var error = false;
		
		if (!(myphoneCheck(document.contact.phone)))
		{return false;}

		if (!(myemailCheck(document.contact.email)))
		{return false;}
		
		if (!(validateZIP(document.contact.zip)))
		{return false;}
		
		if (!(validateState()))
		{return false;}
		
		if (document.contact.name.value=="") {
			msg = msg + "\tName must be entered.\n";
			error = true;
		}
	
		if (document.contact.email.value=="") {
			msg = msg + "\tEmail address must be entered.\n";
			error = true;
		}
	
		if (document.contact.category.options[document.contact.category.options.selectedIndex].value=="") {
			msg = msg + "\tCategory must be entered.\n";
			error = true;
		}

		if (document.contact.inquiry.value=="") {
			msg = msg + "\tA question must be entered.\n";
			error = true;
		}
		
	msg = msg + "\nPlease correct these error(s) and submit the form again.";

	if (error) {
		alert(msg);
		return false;
	}
}


function myphoneCheck (phoneStr) 
{
  if (phoneStr.value !="")
  {
    if (!(phoneCheck(phoneStr.value)))
	{	
		alert("Please fill in a valid phone number.\nExample: 123-456-7890");
		phoneStr.focus();
		phoneStr.select();
		return false;
	} 
	else
	{
		return true;
	}
  }
  else
  {
  	return true;
  }	 
}

function myemailCheck (emailStr) 
{
  if (emailStr.value !="")
  {
    if (!(emailCheck(emailStr.value)))
	{	
		alert("Please fill in a valid email address.\nExample: person@somewhere.com");
		emailStr.focus();
		emailStr.select();
		return false;
	}
	else
	{
		return true;
	}
  }
  else
  {
  	return true;
  }	 
}

function validateState()
{
	if (document.contact.state.value !="")
	{
		var sState = trim(document.contact.state.value.toUpperCase());
		
		if (!((sState == "AK") || (sState == "AL") || (sState == "AR") || (sState == "AS") || (sState == "AZ") ||
			(sState == "CA") || (sState == "CO") || (sState == "CT") || (sState == "DC") || (sState == "DE") ||
			(sState == "FL") || (sState == "GA") || (sState == "HI") || (sState == "IA") || (sState == "ID") ||
			(sState == "IL") || (sState == "IN") || (sState == "KS") || (sState == "KY") || (sState == "LA") ||
			(sState == "MA") || (sState == "MD") || (sState == "ME") || (sState == "MI") || (sState == "MN") ||
			(sState == "MO") || (sState == "MS") || (sState == "MT") || (sState == "NC") || (sState == "ND") ||
			(sState == "NE") || (sState == "NH") || (sState == "NJ") || (sState == "NM") || (sState == "NV") ||
			(sState == "NY") || (sState == "OH") || (sState == "OK") || (sState == "OR") || (sState == "PA") ||
			(sState == "PR") || (sState == "RI") || (sState == "SC") || (sState == "SD") || (sState == "TN") ||
			(sState == "TX") || (sState == "UT") || (sState == "VA") || (sState == "VT") || (sState == "WA") ||
			(sState == "WI") || (sState == "WV") || (sState == "WY")) ) 
		{
			alert("State is not a valid state code.");
			document.contact.state.focus();
			document.contact.state.select();
			return false;	
		}
		else
		{
			return true;
		}
	}
	else
	{
		return true;
	}
}

function validateZIP(field) 
{
	if (field.value !="") 
	{
		var valid = "0123456789-";
		var hyphencount = 0;
		var msg = "The zip code you entered is invalid.  Please correct the following error(s):\n"
		var error
		var errorTemp = false;
		var errorHyphen = false;
		
		if (field.value.length!=5 && field.value.length!=10) 
		{
			msg = msg + "\tEnter a 5 digit or 5 digit+4 zip code.\n";
			error = true;
		}
		//alert(field.value.length);
		for (var i=0; i < field.value.length; i++) {
			temp = "" + field.value.substring(i, i+1);
			//alert(temp);
			if (temp == "-") hyphencount++;
			if (valid.indexOf(temp) == "-1") 
			{
				errorTemp = true;
			}
			
			if ((hyphencount > 1) || ((field.value.length==10) && ""+field.value.charAt(5)!="-")) 
			{
				errorHyphen = true;
			}
		}
			
			if (errorTemp==true)
			{
				msg = msg + "\tInvalid characters in your zip code.\n";
				error = true;
			}
			
			if (errorHyphen==true)
			{
				msg = msg + "\tThe hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.\n";
				error = true;
			}
			
			if (error) 
			{
				msg = msg + "Please try again."
				alert(msg)
				field.focus();
				field.select();
				return false;
			}
			else
			{
				return true;
			}
	}
	else
	{
		return true;
	}
}

function checkDesc(desc)
{
	var pattern = /^\s{1,2000}$/;
	if (desc.value!="")
	{
		if (desc.value.length>2000)
		{
			alert("Please limit your description to 2000 characters.");
			desc.focus();
			return false;
		}
		else if (desc.value.match(pattern))
		{
			alert("Please enter a question.");
			desc.focus();
			desc.select();
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{
		return true;
	}
}
