var whitespace = "\t\r\n"


function validate()
{
	var requiredFields = new Array("fname", "lname","email","comment");
	var fieldNames = new Array("First Name", "Last Name","Email Address","Comments");
	var missedFields = "Please enter values in the following fields:\n\n" ;
	var f = document.contactform;
	var fieldsGood = true;

	for(var i=0; i<requiredFields.length; i++)
	{
	if(trim(f.elements[requiredFields[i]].value) == "")
	{
		missedFields = missedFields += fieldNames[i]+"\n";
		fieldsGood = false;   
	}
	} 
	
    
	if(fieldsGood == true)
	{
	 if ((trim(f.areacode.value) != "") && (trim(f.prefix.value) != "") && (trim(f.suffix.value) != ""))
	 {
	  if (buildPhone(f.areacode.value,f.prefix.value,f.suffix.value) == false) 
	  {
	   alert("The phone number entered is invalid");
	   return false;
	  } 
	 }
	
	 if(!isEmail(f.email.value))
	 { 
		alert("The email address entered is invalid.");
		f.email.focus();
		return false;
	 } //end isEmail if statement
     return true;
	}
	else
	{
	 alert(missedFields);
	 return false;
	}
 }

	  function buildPhone(a,p,s)
	     {
	      if((a.length != 3) || (isNaN(a)))
	      {
	       return false;
	      }
	     
	      if((p.length != 3) || (isNaN(p)))
	      {
	       return false;
	      }
	      
	      if((s.length != 4) || (isNaN(s)))
	      {
								return false;
							}
							//alert("Phone Number: " + a + " " + p + " " + s)
							document.contactform.fullphone.value = "(" +a+ ") " +p+ "-" +s 
							return true;
	     }//end buildPhone
	     
function trim(s)
{//trims whitespace and carrige returns from beginnings and ends values
 s = s.replace(/\s*$/, "");
 s = s.replace(/^\s*/, "");
 return s; 
}//end function trim



function isEmail(x)
{
 if((x == "") || (x == null)){return false;}
	      
 if(hasWhitespace(x)){return false;}
	      
 var i = 1;
 while((i<x.length) && (x.charAt(i) != "@"))
 {
   i++;
 }
    
 if((i>x.length) || (x.charAt(i) != "@"))
 {
  return false;
 }
 else
 {
  i += 2;
 }
	      
 while((i<x.length) && (x.charAt(i) != "."))
 {
   i++;
 }
	      
 if((i>x.length) || (x.charAt(i) != "."))
 {
  return false;
 }
 else
 {
  return true;
 }
}//end isEmail function

function hasWhitespace(x)
{     	      
 for(var i=0;i<x.length;i++)
 {
  var c = x.charAt(i)
  if(whitespace.indexOf(c) == -1){return false;}
 }
 
 return true;
}//end whitespace function
