// ===================================================================
// validate.js
// Copyright (c) 2004 Kurt Hinton
//
// Author:  Kurt Hinton
// Version: 1.0, 12/2004
// ===================================================================

// ------------------------------------------------------------------
// isNull(value_string)
// Parameters:
//   String value_string
// Returns:
//   true  - if the string value is null or blank, whitespace is ignored
//   false - otherwise
// ------------------------------------------------------------------
    function isNull(aString) {
      if ((aString == null) || (aString == "")) return true;
      for (m=0; m<aString.length; m++) {
        if (aString.charAt(m) != " ") {
          return false;
        }
      }
      return true;
    }

// -------------------------------------------------------------------
// isValidZipcode(value_string, field_required_flag)
// Parameters:
//   String value_string
//   boolean required_flag
// Returns:
//   true  - if the string value matches the pattern for a zipcode
//   false - otherwise
// -------------------------------------------------------------------
    function isValidZipcode(value, isRequired) {
      if (isNull(value) && !isRequired) {
        return true;
      }
      if (value.match(/(^\d{5}$)|(^\d{5}-\d{4}$)/))  {
        return true;
      }
      return false;
    }

// -------------------------------------------------------------------
// isValidSSN(value_string, field_required_flag)
// Parameters:
//   String value_string
//   boolean required_flag
// Returns:
//   true  - if the string value matches the pattern for a zipcode
//   false - otherwise
// -------------------------------------------------------------------
    function isValidSSN(value, isRequired) {
      if (isNull(value) && !isRequired) {
        return true;
      }
      if (value.match(/^\d{3}\-?\d{2}\-?\d{4}$/))  {
        return true;
      }
      return false;
    }

// -------------------------------------------------------------------
// isValidEmail(value_string, field_required_flag)
// Parameters:
//   String value_string
//   boolean required_flag
// Returns:
//   true  - if the string value matches the pattern for an email
//           address and contains no invalid characters
//   false - otherwise
// -------------------------------------------------------------------
    function isValidEmail(value, isRequired) {
      if (isNull(value) && !isRequired) {
        return true;
      }
      if (((value.match(/^.+@.+\..{2,4}$/))) && (!(value.match(/[\(\)\<\>\,\;\:\\\/\"\[\]]/))))  {
        return true;
      }
      return false;
    }

// -------------------------------------------------------------------
// isValidPhone(value_string, field_required_flag)
// Parameters:
//   String value_string
//   boolean required_flag
// Returns:
//   true  - if the string value matches the pattern for an email
//           address and contains no invalid characters
//   false - otherwise
// -------------------------------------------------------------------
    function isValidPhone(value, isRequired) {
      if (isNull(value) && !isRequired) {
        return true;
      }
      //strip out acceptable non-numeric characters
      var stripped = value.replace(/[\(\)\.\-\ ]/g, '');
      if ((!isNaN(parseInt(stripped))) && ((stripped.length == 10) || (stripped.length == 7))) {
        return true;
      }
      return false;
    }

// -------------------------------------------------------------------
// isValidInteger(value, field_required_flag)
// Parameters:
//   String value_string
//   boolean required_flag
// Returns:
//   true  - if the string value only has numeric characters [0-9]
//   false - otherwise
// -------------------------------------------------------------------
    function isValidInteger(value, isRequired) {
      if (isNull(value) && !isRequired) {
        return true;
      }
      var digits="0123456789";
      for (var i=0; i < value.length; i++) {
        if (digits.indexOf(value.charAt(i))==-1) { return false; }
      }
      return true;
    }

// -------------------------------------------------------------------
// isValidFloat(value, field_required_flag)
// Parameters:
//   String value_string
//   boolean required_flag
// Returns:
//   true  - if the string value only has numeric characters [0-9] and
//           one period
//   false - otherwise
// -------------------------------------------------------------------
    function isValidFloat(value, isRequired) {
      if (isNull(value) && !isRequired) {
        return true;
      }
      var digits = "0123456789.";
      var hasPeriod = false;
      for (var i=0; i < value.length; i++) {
        if (digits.indexOf(value.charAt(i))==-1) { return false; }
        if (value.charAt(i) == '.') {
          if (hasPeriod) { return false; }
          hasPeriod = true;
        }
      }
      return true;
    }

// -------------------------------------------------------------------
// addCommas(value)
// Parameters:
//   String value_string
// Returns:
//   String - value with commas added every three digits to the left
//            of the decimal
// -------------------------------------------------------------------
    function addCommas(value) {
      var objRegExp = new RegExp('(-?[0-9]+)([0-9]{3})');
      while(objRegExp.test(value)) {
        value = value.replace(objRegExp, '$1,$2');
      }
      return value;
    }

// -------------------------------------------------------------------
// removeCommas(value)
// Parameters:
//   String value_string
// Returns:
//   String - value with all commas removed
// -------------------------------------------------------------------
    function removeCommas(value) {
      return value.replace(/,/g,'');
    }

// -------------------------------------------------------------------
// isValidNumber(value, field_required_flag)
// Parameters:
//   String value_string
//   boolean required_flag
// Returns:
//   true  - if the string value only has numeric characters [0-9],
//           commas, and one period
//   false - otherwise
// -------------------------------------------------------------------
    function isValidNumber(value, isRequired) {
      if (isNull(value) && !isRequired) {
        return true;
      }
      var tempValue = removeCommas(value);
      if ((!isNaN(tempValue)) && (!isNull(tempValue))) {
        return true;
      }
      return false;
    }

// -------------------------------------------------------------------
// isValidCurrency(value, field_required_flag)
// Parameters:
//   String value_string
//   boolean required_flag
// Returns:
//   true  - if the string value only has numeric characters [0-9],
//           commas, one period, and optionally begins with a $
//   false - otherwise
// -------------------------------------------------------------------
    function isValidCurrency(value, isRequired) {
      if (isNull(value) && !isRequired) {
        return true;
      }
      var tempValue = removeCommas(value);
      if (tempValue.match(/^((\$\d*)|(\$\d*\.\d{1,2})|(\d*)|(\d*\.\d{1,2}))$/)) {
        return true;
      }
      return false;
    }

// -------------------------------------------------------------------
// addDays(dateVal, days)
// Parameters:
//   Date value_date
//   int number of days
// Returns:
//   Date value_date + days
// -------------------------------------------------------------------
    function addDays(dateVal, days) {
      return new Date(dateVal.getTime() + (days*24*60*60*1000));
    }

// -------------------------------------------------------------------
// isValidDate(value, field_required_flag)
// Parameters:
//   String value_string
//   boolean required_flag
// Returns:
//   true  - if the string value matches date formats mm/dd/yy or mm/dd/yyyy
//   false - otherwise
// -------------------------------------------------------------------
    function isValidDate(value, isRequired) {
      if (isNull(value) && !isRequired) {
        return true;
      }
      if (((value.match(/^\d{1,2}\/\d{1,2}\/\d{4}$/)) || (value.match(/^\d{1,2}\/\d{1,2}\/\d{2}$/))) && (!isNaN(Date.parse(value)))) {
        var month = value.substring(0, value.indexOf("/"));
        value = value.substr(month.length + 1);
        var day = value.substring(0, value.indexOf("/"));
        value = value.substr(day.length + 1);
        var year = value;

        if (((month < 1) || (month > 12)) || (day < 1) || (year < 1) || (day > 31) || ((day > 30) && ((month == 4) || (month == 6) || (month == 9) || (month == 11)))) {
          return false;
        }
        var leap = (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) ? true: false;
        if (month == 2) {
          if ((day > 29) || ((!leap) && (day > 28))) {
            return false;
          }
        }
        return true;
      }
      return false;
    }

// -------------------------------------------------------------------
// isValidMonthYear(value, field_required_flag)
// Parameters:
//   String value_string
//   boolean required_flag
// Returns:
//   true  - if the string value matches date formats mm/yy or mm/yyyy
//   false - otherwise
// -------------------------------------------------------------------
    function isValidMonthYear(value, isRequired) {
      if (isNull(value) && !isRequired) {
        return true;
      }
      if ((value.match(/^\d{1,2}\/\d{4}$/)) || (value.match(/^\d{1,2}\/\d{2}$/))) {
        var month = value.substring(0, value.indexOf("/"));
        value = value.substr(month.length + 1);
        var year = value;
        var newdate = new Date(year, month-1, 1);

        if ((isNaN(newdate.getTime())) || ((month < 1) || (month > 12)) || (year < 1)) {
          return false;
        }
        return true;
      }
      return false;
    }
