String.prototype.trim = function() {
   var m = this.toString().match(/^\s*(\S+(\s+\S+)*)\s*$/);
   return (m == null) ? "" : m[1];
} 

function illegalEmail(str) {
  var temp
  var username, domainname
  if ((temp = str.indexOf("@")) == -1)
    return true
  username = str.substring(0, temp).toLowerCase()
  domainname = str.substring(temp + 1).toLowerCase()

  var i, ch
  for (i = 0; i < username.length; i++) {
    ch = username.charAt(i)
    if ("abcdefghijklmnopqrstuvwxyz0123456789_-.".indexOf(ch) == -1)
      return true
    }

  for (i = 0; i < domainname.length; i++) {
    ch = domainname.charAt(i)
    if ("abcdefghijklmnopqrstuvwxyz0123456789_-.".indexOf(ch) == -1)
      return true
    }

  if ((temp = domainname.indexOf(".")) == -1)
    return true;

  if (temp == 0)
    return true

  return false
}

function validator(theForm) {
  if (theForm.Last_Name.value.trim() == "")
  {
    alert("Please enter a Last Name.");
    theForm.Last_Name.focus();
    return (false);
  }
  if (theForm.First_Name.value.trim() == "")
  {
    alert("Please enter a First Name.");
    theForm.First_Name.focus();
    return (false);
  }
  if (theForm.address1.value.trim() == "")
  {
    alert("Please enter an Address.");
    theForm.address1.focus();
    return (false);
  }
  if (theForm.city.value.trim() == "")
  {
    alert("Please enter a City.");
    theForm.city.focus();
    return (false);
  }
  if (theForm.state.value.trim() == "")
  {
    alert("Please enter a State.");
    theForm.state.focus();
    return (false);
  }
  if (!theForm.zip.value.trim().match(/^\d{5}(-\d{4})?$/))
  {
    alert("Please enter a valid zip code.");
    theForm.zip.focus();
    return (false);
  }  
  if (theForm.Primary_Phone_Type.selectedIndex <= 0)
  {
    alert("Please select one of the Primary Phone type options.");
    theForm.Primary_Phone_Type.focus();
    return (false);
  }
  if (!theForm.Primary_Phone.value.trim().match(/^\d{3}\-\d{3}\-\d{4}$/))
  {
    alert("Please enter a valid Primary Phone Number: 999-999-9999.");
    theForm.Primary_Phone.focus();
    return (false);
  }
  if (theForm.Second_Phone.value.trim() != "" && 
      !theForm.Second_Phone.value.trim().match(/^\d{3}\-\d{3}\-\d{4}$/))
  {
    alert("Please enter a valid Secondary Phone Number: 999-999-9999.");
    theForm.Second_Phone.focus();
    return (false);
  }
  if (theForm.fax.value.trim() != "" && 
      !theForm.fax.value.trim().match(/^\d{3}\-\d{3}\-\d{4}$/))
  {
    alert("Please enter a valid FAX Number: 999-999-9999.");
    theForm.fax.focus();
    return (false);
  }
  if (illegalEmail(theForm.email.value.trim()))
  {
    alert("Please enter a valid EMAIL.");
    theForm.email.focus();
    return (false);
  }     
  theForm.Javascript.value = "Javascript"
  return (true);
}
