   //US Zip Code validation:
   //This requires the user to enter either a 5 or a 9 digit Zip code in following respective formats  ##### OR #####-####

   function UsZipCodeFormat(formfield){
    var lastField;
    var lastFieldIsValid = true;

    var currentValue = new String(formfield.value);
    var id = formfield.id;
    var currentStrippedValue = ReplaceAll(currentValue,"-","");  //remove the dashes from the zip code for testing

    lastField = id;
    if(currentStrippedValue.length > 0)
    {
     if(isNaN(currentStrippedValue)){
      alert("Please Enter Numeric Values Only for ZipCode");  //Make sure the zip code is numeric only
      formfield.value = "";
      formfield.select();
      lastFieldIsValid = false;
      return;
     }else{
      if(currentStrippedValue.length < 5 || currentStrippedValue.length > 9)  //Make sure zip code is 5 or 9 numbers long
      {
       alert("Zip Code Must Be 5 (#####) or 9 (#####-####) Digits Long");
       formfield.select();
       lastFieldIsValid = false;
       return;
      }
      else
       lastFieldIsValid = true;  //If requirements are met, the field is valid

      if((currentStrippedValue.length < 9 && currentStrippedValue.length > 5) || currentStrippedValue.length == 5)  //Display the string correctly for 5 or 9
       formfield.value = currentStrippedValue.substring(0,5);
      else
       formfield.value = currentStrippedValue.substring(0,5) + "-" + currentStrippedValue.substring(5,9);
     }
    }
   }
   //US Telephone Number validation:
   //This requires the user to enter their phone number and Area code in following format (###) ###-####

   function PhoneNumberFormat(formfield){

    var lastField;
    var lastFieldIsValid = true;

    var currentValue = new String(formfield.value);
    var id = formfield.id;
    var currentStrippedValue = ReplaceAll(ReplaceAll(ReplaceAll(ReplaceAll(currentValue,"-","")," ",""),"(",""),")","");  //strip the dashes or parentheses from phone number
    lastField = id;

    if(currentStrippedValue.length > 0)
    {
     if(isNaN(currentStrippedValue)){
      alert("Please Enter Numeric Values Only (### ###-####)");  //Make sure only numbers are entered
      formfield.value = "";
      formfield.select();
      lastFieldIsValid = false;
      return;
     }else{
      if(currentStrippedValue.length != 10)  //Verify that the area code and telephone number are entered
      {
       alert("Phone # Must Be 10 Numbers Long (### ###-####)");
       formfield.value = "";
       formfield.select();
       lastFieldIsValid = false;
       return;
      }
      else
      {
       lastFieldIsValid = true;
       formfield.value = "(" + currentStrippedValue.substring(0,3) + ") " + currentStrippedValue.substring(3,6) + "-" + currentStrippedValue.substring(6,10); //Format the string for correct display
      }
     }
    }
   }


   //Used to simulate ReplaceAll and not just the first Instance of the item to be replaced
   function ReplaceAll(checkMe,toberep,repwith){
    var temp = checkMe;
    var i = temp.indexOf(toberep);
    while(i > -1){ //Loop through and replace all instances
     temp = temp.replace(toberep, repwith);
     i = temp.indexOf(toberep);
    }
    return temp;
   }

   //Email Address validation:
   //This requires the user to enter their email address in the following format name@ext.ext

   function checkemail(formfield)
   {
    var testresults
    var str=new String(formfield.value);
    var filter=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/

    if (filter.test(str))
       testresults=true
    else
    {
       alert("Please input a valid email address!")
       testresults=false
    }
    return (testresults)
   }
