var RE_EMAIL      = new RegExp(/^[A-Za-z0-9](([_|\.|\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([_|\.|\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$/);
var RE_ZIP        = new RegExp(/^[0-9]{5}(([\-\ ])?[0-9]{4})?$/);
var RE_PHONE      = new RegExp(/^((\d\d\d)|(\(\d\d\d\)))?\s*[\.-]?\s*(\d\d\d)\s*[\.-]?\s*(\d\d\d\d)$/);

var FNAME_ERR     = "Please enter a value for the 'First Name' field.";
var LNAME_ERR     = "Please enter a value for the 'Last Name' field.";
var ADDRESS_ERR   = "Please enter a value for the 'Address' field.";
var CITY_ERR      = "Please enter a value for the 'City' field.";
var STATE_ERR     = "Please enter a value for the 'State' field.";
var ZIP_ERR       = "Please enter a valid zip code.";
var TEL_ERR       = "Please enter a valid telephone number.";
var EMAIL_ERR     = "Please enter a valid email address.";

function validateForm(theForm)
{
   trimValues(theForm)
  
   if (!theForm.first_name.value)
   {
      alert(FNAME_ERR);
      theForm.first_name.focus();
      return false;
   }   
   if (!theForm.last_name.value)
   {
      alert(LNAME_ERR);
      theForm.last_name.focus();
      return false;
   }
   if (!theForm.address.value)
   {
      alert(ADDRESS_ERR);
      theForm.address.focus();
      return false;
   }
    if (!theForm.city.value)
   {
      alert(CITY_ERR);
      theForm.city.focus();
      return false;
   }   
   if (theForm.state.value == 'value')
   {
      alert(STATE_ERR);
      theForm.state.focus();
      return false;
   }   
   if (!theForm.zip.value ||!RE_ZIP.test(theForm.zip.value))
   {
      alert(ZIP_ERR);
      theForm.zip.value = '';
      theForm.zip.focus();
      return false;
   }
   if (!theForm.email.value ||!RE_EMAIL.test(theForm.email.value))
   {
      alert(EMAIL_ERR);
      theForm.email.value = '';
      theForm.email.focus();
      return false;
   }
   if (!RE_PHONE.test(theForm.telephone.value))
   {
      alert(TEL_ERR);
      theForm.telephone.focus();
      return false;
   }
   
 

    return true;
}

function trim(val)
{
     return val.replace(/^\s+|\s+$/, '');
}

function trimValues(theForm)
{
    theForm.first_name.value = trim(theForm.first_name.value);
    theForm.last_name.value  = trim(theForm.last_name.value);
    theForm.address.value    = trim(theForm.address.value);
    theForm.city.value       = trim(theForm.city.value);
    theForm.zip.value        = trim(theForm.zip.value);
    theForm.telephone.value  = trim(theForm.telephone.value);
    theForm.email.value      = trim(theForm.email.value);
}
