// Content
// var whitespace
// function isWhitespace (s)
// function isEmpty(s)
// function charInString (c, s)
// var defaultEmptyOK
// function isEmail (s)
// function Checkbetweenwhitespace(s)
// function alltrim(obj)
// function isPhoneN(s)
// function isZip(s)
// var USStates
// function isUSState(s)
// function isNum(s) {
// function isPhoneEx(s){
// function isValidDate(f) {

  // Returns true if string s is empty or whitespace characters only.

  // whitespace characters
  var whitespace = " \t\n\r";

  function isWhitespace (s) {
    var i;
    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++) {
      // Check that current character isn't whitespace.
      var c = s.charAt(i);

      if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
  }


  // Returns true if string s is empty 

  // Check whether string s is empty.
  function isEmpty(s) {
    return ((s == null) || (s.length == 0))
  }


  // Returns true if single character c (actually a string)
  // is contained within string s.
  function charInString (c, s) {
    for (i = 0; i < s.length; i++) {
      if (s.charAt(i) == c) return true;
    }
    return false
  }

  var defaultEmptyOK = false

  function isEmail (s) {
    var s= alltrim(s);
    if (isEmpty(s)) 
      if (isEmail.arguments.length == 1) return defaultEmptyOK;
      else return (isEmail.arguments[1] == true);

    // is s whitespace?
    if (isWhitespace(s)) return false;

    // Check the white space in the email address
     if (Checkbetweenwhitespace(s)) return false;

    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@")) {
      i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != ".")) {
      i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;

    // Check there is no invalid chars in the address
    var specialChars="<>,;:()"
    for (i = 0; i < s.length; i++) {
      if (charInString(s.charAt(i), specialChars)) // Returns true if single character c (actually a string)
        return false;
    }
  // Check there is only 1 @ in the address
    j = 0;
    for (i = 0; i < s.length; i++) {
      if (s.charAt(i) == "@") j++
    }
    if (j > 1) return false;
  // Check we do not have .. in the address
    for (i = 0; i < s.length-1; i++) {
      if (s.charAt(i) == "@" && s.charAt(i+1) == ".") return false;
      if (s.charAt(i) == "." && s.charAt(i+1) == ".") return false;
    }

  // Check that the end part of the email address looks correct    
    var suffix=s.substring(s.lastIndexOf(".")+1,s.length)
    suffix=suffix.toUpperCase()
    if (!(suffix == "NET" || suffix == "COM" || suffix == "EDU" || suffix == "ORG" || suffix == "GOV" || suffix == "MIL" || suffix == "US" || suffix == "CA" || suffix == "UK")) {
      var bchoice = window.confirm("The end part of the email address is not one of the more common endings (e.g. COM, NET etc.).\nPlease check it is correct, Press 'OK' to continue or 'Cancel' to correct it.");
      if (!bchoice == true)
        return false;
    }

    return true;
  }

  function Checkbetweenwhitespace(s) {
    for (i = 0; i < s.length; i++) {
      // Check that current character isn't whitespace.
      var c = s.charAt(i);
      if (whitespace.indexOf(c) == "") return true;
    }
  // For WebTV users we needed to default return false if we found no bad characters
    return false;
  }

  function alltrim(obj){

     var trimmedstring="";
     var startpos=0; 
     var endpos=obj.length - 1

/*
     while (startpos<=obj.length && obj.substring(startpos, ¬startpos+1)==" ") {
       startpos++ ;
     }
*/
     /* null string */
      if (endpos == -1) {
       endpos=0
     }
     /* find end position of string */
     while (endpos >= 0 && obj.substring(endpos,endpos+1)==" ") {
       endpos--
     }
     /* replace value with trimmed string */
     obj=obj.substring(startpos,endpos+1)
     return obj
  }

  function isPhoneN(s){
    if(s.length < 10) {
      alert("The telephone must be 10 digits including the area code");
      return false;
    }
    var sPhone = "";
    var oneChar = "";
    for(i=0; i< s.value.length; i++){
      oneChar = s.value.substring(i,i+1);
      if(oneChar >="0" && oneChar<= "9") {
        sPhone = sPhone + oneChar;
        //alert(sPhone);
      }
    }
    if(sPhone.length != 10) {
      alert("The telephone must be 10 digits including the area code");
      return false;
    }
    s.value = sPhone; // Write back edited value of phone with '(', '-' or ' ' removed.
    return true;
  }


  function isZip(s) {
    if (s.length !=5)
      return false;
    if (isWhitespace(s))
      return false;
    if ((Number(s) > 0))
      return true;
    return false;
  }


  // States array DC included as a State
  var USStates = new Array(52)
  USStates["AL"] = "ALABAMA"
  USStates["AK"] = "ALASKA"
  USStates["AZ"] = "ARIZONA"
  USStates["AR"] = "ARKANSAS"
  USStates["CA"] = "CALIFORNIA"
  USStates["CO"] = "COLORADO"
  USStates["CT"] = "CONNECTICUT"
  USStates["DE"] = "DELAWARE"
  USStates["DC"] = "DISTRICT OF COLUMBIA"
  USStates["FL"] = "FLORIDA"
  USStates["GA"] = "GEORGIA"
  USStates["HI"] = "HAWAII"
  USStates["ID"] = "IDAHO"
  USStates["IL"] = "ILLINOIS"
  USStates["IN"] = "INDIANA"
  USStates["IA"] = "IOWA"
  USStates["KS"] = "KANSAS"
  USStates["KY"] = "KENTUCKY"
  USStates["LA"] = "LOUISIANA"
  USStates["ME"] = "MAINE"
  USStates["MD"] = "MARYLAND"
  USStates["MA"] = "MASSACHUSETTS"
  USStates["MI"] = "MICHIGAN"
  USStates["MN"] = "MINNESOTA"
  USStates["MS"] = "MISSISSIPPI"
  USStates["MO"] = "MISSOURI"
  USStates["MT"] = "MONTANA"
  USStates["NE"] = "NEBRASKA"
  USStates["NV"] = "NEVADA"
  USStates["NH"] = "NEW HAMPSHIRE"
  USStates["NJ"] = "NEW JERSEY"
  USStates["NM"] = "NEW MEXICO"
  USStates["NY"] = "NEW YORK"
  USStates["NC"] = "NORTH CAROLINA"
  USStates["ND"] = "NORTH DAKOTA"
  USStates["OH"] = "OHIO"
  USStates["OK"] = "OKLAHOMA"
  USStates["OR"] = "OREGON"
  USStates["PA"] = "PENNSYLVANIA"
  USStates["PR"] = "PUERTO RICO"
  USStates["RI"] = "RHODE ISLAND"
  USStates["SC"] = "SOUTH CAROLINA"
  USStates["SD"] = "SOUTH DAKOTA"
  USStates["TN"] = "TENNESSEE"
  USStates["TX"] = "TEXAS"
  USStates["UT"] = "UTAH"
  USStates["VT"] = "VERMONT"
  USStates["VA"] = "VIRGINIA"
  USStates["WA"] = "WASHINGTON"
  USStates["WV"] = "WEST VIRGINIA"
  USStates["WI"] = "WISCONSIN"
  USStates["WY"] = "WYOMING"

  // input value is a U.S. state abbreviation; set entered value to all uppercase
  //this is not used below
  // also set companion field (NAME="<xxx>_expand") to full state name

  function isUSState(s) {
    var inputStr = s.toUpperCase()
    if (inputStr.length > 0 && USStates[inputStr] == null) {
      var msg = ""
      var firstChar = inputStr.charAt(0)
      if (firstChar == "A") {
        msg += "\n(Alabama = AL; Alaska = AK; Arizona = AZ; Arkansas = AR)"
      }
      if (firstChar == "D") {
        msg += "\n(Delaware = DE; District of Columbia = DC)"
      }
      if (firstChar == "I") {
        msg += "\n(Idaho = ID; Illinois = IL; Indiana = IN; Iowa = IA)"
      }
      if (firstChar == "M") {
        msg += "\n(Maine = ME; Maryland = MD; Massachusetts = MA; Michigan = MI; Minnesota = MN; Mississippi = MS; Missouri = MO; Montana = MT)"
      }
      if (firstChar == "N") {
        msg += "\n(Nebraska = NE; Nevada = NV; New Jersey = NJ; New York = NY)"
      }
      alert("Check the spelling of the state abbreviation." + msg);
      return false;
    }
    return true;
  }


  function isNum(s) {
    var i;
    // This function dectects if there is any non-numeric data in a string.
    // Returns true if all numeric, or false if non-numeric found
    for (i = 0; i < s.length; i++) {
      var oneChar = s.charAt(i);
      if (oneChar < "0" || oneChar > "9" ) {
        return false;
      }
    }
    return true;
  }


  function isPhoneEx(s){
    var sPhone = "";
    var oneChar = "";
    for(i=0; i< s.length; i++){
      oneChar = s.substring(i,i+1);
      if(oneChar >="0" && oneChar<= "9") {
        sPhone = sPhone + oneChar;
      }
    }
    if(sPhone.length > 5 ) {
      alert("The telephone extension must be no bigger than 5 digits");
      return false;
    }
    return true;
  }


  function isValidDate(f) {
    slash1 = f.indexOf("/")
    slash2 = f.indexOf("/", slash1 + 1)
    sMonth = f.substring(0,slash1)
    month = parseInt(sMonth,10)
    sDay = f.substring(slash1+1,slash2)
    day = parseInt(sDay,10)
    sYear = f.substring(slash2+1,99)
    year = parseInt(sYear,10)
    var months = new Array("","January","February","March","April","May","June","July","August","September","October","November","December")
    if (slash1 == "" || slash1 == -1) {
      alert("Invalid Date, it must be in the format mm/dd/yyyy");//Must be first slash 
      return false;
    }
    if (slash2 == "" || slash2 == -1) {
      alert("Invalid Date, it must be in the format mm/dd/yyyy");//Must be second slash 
      return false;
    }
    if(!isNum(sMonth)){
      alert("Invalid Month, only numbers please 1 thru 12 in the format mm/dd/yyyy");
      return false;
    }
    if(!isNum(sDay)){
      alert("Invalid Day, only numbers please 1 thru 31 in the format mm/dd/yyyy");
      return false;
    }
    if(!isNum(sYear)){
      alert("Invalid Year, only numbers please 0 thru 9 in the format mm/dd/yyyy");
      return false;
    }
    if(month < 1 || month > 12) { //month is not valid
      alert("Month must be between 1 and 12");
      return false;
    }
    if(day < 1 || day > 31) { //day is not valid
      alert("Day must be between 1 and 31 depending on the month and year");
      return false;
    }
    if(sYear.length < 4) {
      if(sYear.length == 3){
        alert("The year must be four digits");
        return false;
      }
      if(sYear.length == 2) {
        if(parseInt(sYear) < 20){
          sYear = "20" + sYear;
        }else{
          sYear = "19" + sYear
        }
      }
      if(sYear.length == 1) {
        sYear = "200" + sYear;
      }
      if(sYear.length == 0) {
        alert("The year must be four digits");
        return false;
      }
    }
    if(sYear < 1940 || sYear > 2070) {
      alert("The year entered is not valid");
      return false;
    }

    month = parseInt(sMonth)

    if((month == 4 || month == 6 || month == 9 || month == 11) && day > 30) {
      alert(months[month] + " has only 30 days.");
      return false;
    }
    else if (day > 31) {
      alert(months[month] + " has only 31 days.");
      return false;
    }

    if(month==2) { //Check for a leap year
      if((year % 4 ==0 && year % 100 !=0) || year % 400 == 0){
        if(day > 29) {
          alert("February of " + year + " has only 29 days.");
          return false;
        }
      } else if(day > 28) {
        alert("February of " + year + " has only 28 days.");
        return false;
      }
    }
    return true;
  }


 