/**
 * FUNCTION: fixString
 *
 * INPUT: str (string) - the string to be fixed.
 *
 * RETURN: A fixed version of the string.
 *
 * PLATFORMS: Netscape Navigator 3.01 and higher,
 *            Microsoft Internet Explorer 3.02 and higher,
 *            Netscape Enterprise Server 3.0,
 *            Microsoft IIS/ASP 3.0.
 */
function fixString(str) {
  if (isBlank(str)) {
    return "";
  }
  return str;
}

/**
 * FUNCTION: isAlpha
 *
 * INPUT: str (string) - the string to be tested.
 *
 * RETURN: true, if the string contains only alphabetic characters 
 *         false, otherwise.
 *
 * PLATFORMS: Netscape Navigator 3.01 and higher,
 *            Microsoft Internet Explorer 3.02 and higher,
 *            Netscape Enterprise Server 3.0,
 *            Microsoft IIS/ASP 3.0.
 */
function isAlpha(str) {
  // Return immediately if an invalid value was passed in.
  if (str+"" == "undefined" || str+"" == "null" || str+"" == "") {
    return false;
  }

  var isValid = true; // assume valid.

  str += ""; // convert to a string for performing string comparisons.

  // Loop through string one character at time,  breaking out of for
  // loop when an non Alpha character is found.
  for (i = 0; i < str.length; i++) {
    // Alpha must be between "A"-"Z", or "a"-"z".
    if (!(((str.charAt(i) >= "a") && (str.charAt(i) <= "z")) ||
          ((str.charAt(i) >= "A") && (str.charAt(i) <= "Z")))) {
      isValid = false;
      break;
    }
  }
  return isValid;
}

/**
 * FUNCTION: isAlphaNum
 *
 * INPUT: str (string) - a string that will be tested to ensure that
 *                       each character is a digit or a letter.
 *
 * RETURN: true, if all characters in the string are a character from 0-9
 *         or a-z or A-Z; false, otherwise.
 *
 * PLATFORMS: Netscape Navigator 3.01 and higher,
 *            Microsoft Internet Explorer 3.02 and higher,
 *            Netscape Enterprise Server 3.0,
 *            Microsoft IIS/ASP 3.0.
 */
function isAlphaNum(str) {
  // Return immediately if an invalid value was passed in
  if (str+"" == "undefined" || str+"" == "null" || str+"" == "") {
    return false;
  }

  var isValid = true; // assume valid.
  
  // convert to a string for performing string comparisons.
  str += "";  

  // Loop through length of string and test for any alpha numeric 
  // characters.
  for (i = 0; i < str.length; i++) {
    // Alphanumeric must be between "0"-"9", "A"-"Z", or "a"-"z"
    if (!(((str.charAt(i) >= "0") && (str.charAt(i) <= "9")) || 
         ((str.charAt(i) >= "a") && (str.charAt(i) <= "z")) ||
         ((str.charAt(i) >= "A") && (str.charAt(i) <= "Z")))) {
      isValid = false;
      break;
    }  
  }
  return isValid;
}

/**
 * FUNCTION: isAlphaNumOrUnderscore
 *
 * INPUT: str (string) - the string to be tested.
 *
 * RETURN: true, if the string contains only alphanumeric characters
 *         or underscores false, otherwise.
 *
 * PLATFORMS: Netscape Navigator 3.01 and higher,
 *            Microsoft Internet Explorer 3.02 and higher,
 *            Netscape Enterprise Server 3.0,
 *            Microsoft IIS/ASP 3.0.
 */
function isAlphaNumOrUnderscore(str) {
  // Return immediately if an invalid value was passed in
  if (str+"" == "undefined" || str+"" == "null" || str+"" == "") {
    return false;
  }

  var isValid = true; // assume valid.

  str += "";  // convert to a string for performing string comparisons.

  // Loop through string one character at a time. If non-alpha numeric
  // is found then, break out of loop and return a false result
  for (i = 0; i < str.length; i++) {
    // Alphanumeric must be between "0"-"9", "A"-"Z", or "a"-"z"
    if (!(((str.charAt(i) >= "0") && (str.charAt(i) <= "9")) || 
          ((str.charAt(i) >= "a") && (str.charAt(i) <= "z")) ||
          ((str.charAt(i) >= "A") && (str.charAt(i) <= "Z")) ||
          (str.charAt(i) == "_"))) {
      isValid = false;
      break;
    }
  }
  return isValid;
}

/**
 * FUNCTION: isBlank
 *
 * INPUT: val - the value to be tested.
 *
 * RETURN: true, if the string is null, undefined or an empty string, ""
 *         false, otherwise.
 *
 * CALLS: isNull(), isUndef() which are defined elsewhere in the Script Library.
 * 
 * PLATFORMS: Netscape Navigator 3.01 and higher,
 *            Microsoft Internet Explorer 3.02 and higher,
 *            Netscape Enterprise Server 3.0,
 *            Microsoft IIS/ASP 3.0.
 */
function isBlank(str) {
  var isValid = false;
  if (isNull(str) || isUndef(str) || (str+"" == "")) {
    isValid = true;
  }
  return isValid;
}

/**
 * FUNCTION: isInt
 *
 * INPUT: numstr (string/number) - the string that will be tested to ensure 
 *                                 that each character is a digit.
 *        allowNegatives (boolean) - (optional) when true, allows numstr to be
 *                                   negative (contain a '-').  When false,
 *                                   any negative number or a string starting
 *                                   with a '-' will be considered invalid.
 *
 * RETURN: true, if all characters in the string are a character from 0-9,
 *         regardless of value for allowNegatives true, if allowNegatives
 *         is true and the string starts with a '-', and all other
 *         characters are 0-9.  False, otherwise.
 *
 * PLATFORMS: Netscape Navigator 3.01 and higher,
 *            Microsoft Internet Explorer 3.02 and higher,
 *            Netscape Enterprise Server 3.0,
 *            Microsoft IIS/ASP 3.0.
 */
function isInt(numstr, allowNegatives) {
  // Return immediately if an invalid value was passed in
  if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "") {
    return false;
  }

  // Default allowNegatives to true when undefined or null
  if (allowNegatives+"" == "undefined" || allowNegatives+"" == "null") {
    allowNegatives = true;
  }

  var isValid = true;

  // convert to a string for performing string comparisons.
  numstr += "";

  // Loop through string and test each character. If any
  // character is not a number, return a false result.
  // Include special case for negative numbers (first char == '-').   
  for (i = 0; i < numstr.length; i++) {
    if (!((numstr.charAt(i) >= "0") &&
          (numstr.charAt(i) <= "9") ||
          (numstr.charAt(i) == "-"))) {
      isValid = false;
      break;
    }
    else if ((numstr.charAt(i) == "-" && i != 0) || 
             (numstr.charAt(i) == "-" && !allowNegatives)) {
      isValid = false;
      break;
    }
  }
  return isValid;
}

/**
 * FUNCTION: isNull
 *
 * INPUT: val - the value to be tested.
 *
 * RETURN: true, if the value is null; false, otherwise.
 *
 * PLATFORMS: Netscape Navigator 3.01 and higher,
 *            Microsoft Internet Explorer 3.02 and higher,
 *            Netscape Enterprise Server 3.0,
 *            Microsoft IIS/ASP 3.0.
 */
function isNull(val) {
  var isValid = false;
  if (val+"" == "null") {
    isValid = true;
  }
  return isValid;
}

function isNumField(field, allowNull) {
    if (!isNum(field, allowNull))
    {
        return false;
    }
    return true;
}

/**
 * FUNCTION: isNum
 *
 * INPUT: numstr (string/number) - the string that will be tested to ensure 
 *                                 that the value is a number (int or float).
 *
 * RETURN: true, if all characters represent a valid integer or float
 *         false, otherwise.
 *
 * PLATFORMS: Netscape Navigator 3.01 and higher,
 *            Microsoft Internet Explorer 3.02 and higher,
 *            Netscape Enterprise Server 3.0,
 *            Microsoft IIS/ASP 3.0.
 */
function isNum(numstr, allowNull) {
  // Return immediately if an invalid value was passed in
  if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "") {
      return allowNull;
  }
  var isValid = true;
  var decCount = 0; // number of decimal points in the string

  // convert to a string for performing string comparisons.
  numstr += "";

  // Loop through string and test each character. If any
  // character is not a number, return a false result.
  // Include special cases for negative numbers (first char == '-')
  // and a single decimal point (any one char in string == '.').   
  for (i = 0; i < numstr.length; i++) {
    // track number of decimal points
    if (numstr.charAt(i) == ".") {
      decCount++;
    }

    if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9") || 
          (numstr.charAt(i) == "-") || (numstr.charAt(i) == "."))) {
      isValid = false;
      break;
    }
    else if ((numstr.charAt(i) == "-" && i != 0) ||
             (numstr.charAt(i) == "." && numstr.length == 1) ||
             (numstr.charAt(i) == "." && decCount > 1)) {
      isValid = false;
      break;
    }                             
  }
  return isValid;
}

/**
 * FUNCTION: isUndef
 *
 * INPUT: val - the value to be tested.
 *
 * RETURN: true, if the value is undefined, false otherwise.
 *
 * PLATFORMS: Netscape Navigator 3.01 and higher,
 *            Microsoft Internet Explorer 3.02 and higher,
 *            Netscape Enterprise Server 3.0,
 *            Microsoft IIS/ASP 3.0.
 */
function isUndef(val) {
  var isValid = false;
  if (val+"" == "undefined") {
    isValid = true;
  }
  return isValid;
}

/**
 * FUNCTION: isValid5DigitZip
 *
 * INPUT: str (string) - a 5-digit zip code to be tested.
 *
 * RETURN: true, if the string is 5-digits long, false otherwise.
 *
 * CALLS: isBlank(), isInt() which are defined elsewhere in the Script Library.
 *
 * PLATFORMS: Netscape Navigator 3.01 and higher,
 *            Microsoft Internet Explorer 3.02 and higher,
 *            Netscape Enterprise Server 3.0,
 *            Microsoft IIS/ASP 3.0.
 */
function isValid5DigitZip(str) {
  // Return immediately if an invalid value was passed in
  if (str+"" == "undefined" || str+"" == "null" || str+"" == "") {
    return false;
  }

  var isValid = true;
  str += "";

  // Rules: zipstr must be 5 characters long, and can only contain numbers from
  // 0 through 9
  if (isBlank(str) || (str.length != 5) || !isInt(str, false)) {
    isValid = false;
  }
  return isValid;
}

/**
 * FUNCTION: isValid5Plus4DigitZip
 *
 * INPUT: str (string) - a 5+4 digit zip code to be tested.
 *
 * RETURN: true, if the string contains 5-digits followed by a dash
 *         followed by 4 digits, false otherwise.
 *
 * CALLS: isBlank(), isInt() which are defined elsewhere in the Script Library.
 *
 * PLATFORMS: Netscape Navigator 3.01 and higher,
 *            Microsoft Internet Explorer 3.02 and higher,
 *            Netscape Enterprise Server 3.0,
 *            Microsoft IIS/ASP 3.0.
 */
function isValid5Plus4DigitZip(str) {
  // Return immediately if an invalid value was passed in
  if (str+"" == "undefined" || str+"" == "null" || str+"" == "") {
    return false;
  }

  var isValid = true;
  str += "";

  // Rules: The first five characters may contain only digits 0-9,
  // the 6th character must be a dash, '-', and 
  // the last four characters may contain only digits 0-9.
  // The total string length must be 10 characters.
  if (isBlank(str) || (str.length != 10) || str.charAt(5) != '-' ||
      !isInt(str.substring(0,5), false) ||
      !isInt(str.substring(6,10), false)) {
    isValid = false;
  }
  return isValid;
}

/**
 * FUNCTION: isValidEmail
 * 
 * INPUT: str (string) - an e-mail address to be tested
 * 
 * RETURN: true, if the string contains a valid e-mail address which is a string
 *         plus an '@' character followed by another string containing at least 
 *         one '.' and ending in an alpha (non-punctuation) character.
 *         false, otherwise
 * 
 * CALLS: isBlank(), isAlpha() which are defined elsewhere in the Script Library.
 * 
 * PLATFORMS: Netscape Navigator 3.01 and higher,
 *            Microsoft Internet Explorer 3.02 and higher,
 *            Netscape Enterprise Server 3.0,
 *            Microsoft IIS/ASP 3.0.
 */
function isValidEmail(str) {
  // Return immediately if an invalid value was passed in
  if (str+"" == "undefined" || str+"" == "null" || str+"" == "") {
    return false;
  }

  var isValid = true;
  str += "";

  namestr = str.substring(0, str.indexOf("@")); // everything before the '@'
  domainstr = str.substring(str.indexOf("@") + 1, str.length); // everything after the '@'

  // Rules: namestr cannot be empty, or that would indicate no characters before the '@',
  // domainstr must contain a period that is not the first character (i.e. right after
  // the '@').  The last character must be an alpha.
  if (isBlank(str) || (namestr.length == 0) || 
      (domainstr.indexOf(".") <= 0) ||
      (domainstr.indexOf("@") != -1) ||
      !isAlpha(str.charAt(str.length-1))) {
    isValid = false;
  }
  return isValid;
}

/**
 * FUNCTION: isValidPhone
 * 
 * INPUT: str (string) - an phone number to be tested
 *        incAreaCode (boolean) - if true, area code is included (10-digits);
 *                                if false or undefined, area code not included
 *
 * RETURN: true, if the string contains a 7-digit phone number and 
 *         incAreaCode == false or is undefined true, if the string contains
 *         a 10-digit phone number and incAreaCode == true; false otherwise.
 *
 * CALLS: stripNonNumeric(), which is defined elsewhere in the Script Library.
 *
 * PLATFORMS: Netscape Navigator 3.01 and higher,
 *            Microsoft Internet Explorer 3.02 and higher,
 *            Netscape Enterprise Server 3.0,
 *            Microsoft IIS/ASP 3.0.
 */
function isValidPhone(str, incAreaCode) {
  // Return immediately if an invalid value was passed in.
  if (str+"" == "undefined" || str+"" == "null" || str+"" == "") {
    return false;
  }

  // Set default value for incAreaCode to false, if undefined or null
  if (incAreaCode+"" == "undefined" || incAreaCode+"" == "null") {
    incAreaCode = false;
  }

  var isValid = true;
  str += "";

  // After stripping out non-numeric characters, such as dashes, the
  // phone number should contain 7 digits (no area code) or 10 digits (area code)
  str = stripNonNumeric(str+"");
  if (incAreaCode && str.length != 10) {
    isValid = false;
  }
  if (!incAreaCode && str.length != 7) {
    isValid = false;
  }
  return isValid;
}

/**
 * Validates the phone number entered.
 */
function validateNumber(number, areacode) {
    var returnValue = true;
    var len = 8;
    if (areacode) {
        len = len + 6;
    }

    if (number.length == len) {
        for (x = 0; x < len; x++) {
            var digit = number.substring(x, x + 1);
            if (areacode) {
                if ((x == 0) && (digit != "(")) {
                    returnValue = false;
                    break;
                } else if ((x == 4) && (digit != ")")) {
                    returnValue = false;
                    break;
                } else if ((x == 5) && (digit != " ")) {
                    returnValue = false;
                    break;
                } else if ((x == 9) && (digit != "-")) {
                    returnValue = false;
                    break;
                } else if (((x != 0) && (x != 4) &&
                            (x != 5) && (x != 9)) && !isNum(digit)) {
                    returnValue = false;
                    break;
                }
            } else {
                if ((x == 3) && (digit != "-")) {
                    returnValue = false;
                    break;
                } else if ((x != 3) && !isNum(digit)) {
                    returnValue = false;
                    break;
                }
            }
        }
    } else {
        returnValue = false;
    }
    return returnValue;
}

/**
 * FUNCTION: isValidSSN
 * 
 * INPUT: str (string) - an phone number to be tested
 *        incDashes (boolean) - if true, str includes dashes (e.g. 111-12-3456);
 *                              if false, str contains only digits
 * 
 * RETURN: true, if the string contains digits and dashes in the form 111-12-3456;
 *         true, if the string contains a 9-digit number and incDashes is false;
 *         false, otherwise
 *
 * CALLS: isInt(), which is defined elsewhere in the Script Library.
 * 
 * PLATFORMS: Netscape Navigator 3.01 and higher,
 *            Microsoft Internet Explorer 3.02 and higher,
 *            Netscape Enterprise Server 3.0,
 *            Microsoft IIS/ASP 3.0.
 */
function isValidSSN(str, incDashes) {
  // Return immediately if an invalid value was passed in
  if (str+"" == "undefined" || str+"" == "null" || str+"" == "") {
    return false;
  }

  var isValid = true;

  // Set default value for incDashes to true, if undefined or null
  if (incDashes+"" == "undefined" || incDashes+"" == "null") {
    incDashes = true;
  }

  str += "";  // make sure it's a string

  if (!incDashes && (!isNum(str) || str.length != 9)) {
    isValid = false;
  }

  var part1 = str.substring(0, 3);
  var part2 = str.substring(4, 6);
  var part3 = str.substring(7, str.length);

  // Ensure that the first part is a number and 3 digits long,
  // the second part is a number and 2 digits long,
  // the third part is a number and 4 digits long, e.g. 111-22-3333
  if (incDashes && ((!isInt(part1, false) || part1.length != 3) ||
      (!IsInt(part2, false) || part2.length != 2) || 
      (!IsInt(part3, false) || part3.length != 4))) {
    isValid = false;
  }
  return isValid;
}

/**
 * Returns true if the specified date field parses to a valid date.
 *
 * @param field the html form date field to validate.
 */
function checkDate(field) {
    return checkDate(field, false);
}

/**
 * Returns true if the specified date field parses to a valid date.
 *
 * @param field the html form date field to validate.
 * @param lenient If true then it will accept dates in the form of
 * MM/yyyy or MM/dd/yyyy, otherwise just MM/dd/yyyy is accepted.
 */
function checkDate(field, lenient) {
    var i1 = field.value.indexOf("/", 0);
    var i2 = field.value.indexOf("/", i1+1);

    if ((i2 == -1) && lenient) {
        i2 = i1;
    }

    var day = 1;
    var month = field.value.substring(0, i1);
    if (i1 != i2) {
        day = field.value.substring(i1 + 1, i2);
    }
    var year = field.value.substring(i2 + 1, field.value.length);

    if (field.value == "") {
        return true;
    } else if (field.value.toUpperCase() == "TODAY") {
        today = new Date();
        if (today.getYear() < 1900) {
            var year = today.getYear() + 1900;
        } else {
            var year = today.getYear();
        }
        field.value = today.getMonth() + 1 + "/" + today.getDate() + "/" + year;
        return true;
    } else if (!validDate(month, day, year)) {
        return false;
    }
    return true;
}

/**
 * Returns true if the specified date fields are valid.
 *
 * @param month the date's month
 * @param day the date's day
 * @param year the date's year
 */
function validDate(month, day, year) {
  if(year < 100) year = year + 2000;
  if(month >=1 && month <=12 && year >= 1900 && year <= 9999 && day >= 1 && day <=31) {
    if(month == 2) {
      if(day > 28) {
        if(day != 29 || ((year%400) && ((year%4) || !(year%100)))) {
          return false;
        }
        else {
          return true;
        }
      }
      else {
        return true;
      }
    }
    else if((month == 4) || (month == 6) || (month == 9) || (month == 11)) {
      if(day > 30) {
        return false;
      }
      else {
        return true;
      }
    }
    else {
      return true;
    }
  }
  else {
    return false;
  }
}

/**
 * Returns -1 if the first date field is later, 0 if the date fields are equal,
 * or +1 if the second date field is later.
 *
 * @param field1 the first html form date field to validate.
 * @param field2 the second html form date field to validate.
 */
function compareDates(field1, field2) {
  var i1_1 = field1.value.indexOf("/", 0);
  var i1_2 = field1.value.indexOf("/", i1_1+1);

  var i2_1 = field2.value.indexOf("/", 0);
  var i2_2 = field2.value.indexOf("/", i2_1+1);

  var month1 = field1.value.substring(0, i1_1);
  var day1 = field1.value.substring(i1_1 + 1, i1_2);
  var year1 = field1.value.substring(i1_2 + 1, field1.value.length);

  var month2 = field2.value.substring(0, i2_1);
  var day2 = field2.value.substring(i2_1 + 1, i2_2);
  var year2 = field2.value.substring(i2_2 + 1, field2.value.length);

  return compareTheDates(month1, day1, year1, month2, day2, year2);
}

/**
 * Returns -1 if the first date fields are later, 0 if the date fields are equal,
 * or +1 if the second date fields are later.
 *
 * @param month1 the first date's month
 * @param day1 the first date's day
 * @param year1 the first date's year
 * @param month2 the second date's month
 * @param day2 the second date's day
 * @param year2 the second date's year
 */
function compareTheDates(month1, day1, year1, month2, day2, year2) {
  if (year1 > year2) {
    return -1;
  }
  else if (year1 == year2) {
    if (month1 > month2) {
      return -1;
    }
    else if (month1 == month2) {
      if (day1 > day2) {
        return -1;
      }
      else if (day1 == day2) {
        return 0;
      }
      else {
        return 1;
      }
    }
    else {
      return 1;
    }
  }
  else {
    return 1;
  }
}

/** Constant specifying Microsoft I.E. browser. **/
var MSIE = "MSIE";

/**
 * Returns the user's browser name.
 */
function getBrowserName() {
  return navigator.userAgent;
}

/**
 * Returns the user's browser version.
 */
function getBrowserVersion() {
  var browserName = getBrowserName();
  var browserVersion = navigator.appVersion;
  var index1 = browserName.indexOf(MSIE);
  if (index1 > -1) {
    var index2 = browserName.indexOf(";", index1);
    browserVersion = browserName.substring(index1 + 5, index2); 
  }
  return browserVersion;
}

