/* Wildfire Foundation Classes
   Input Validation Routines
   Code written by Joel Kozikowski, Developer at Wildfire Internet Services,
   (http://www.wildfire.net) and/or gathered from various places around the web.
   Feel free to use in your own code.
*/


  // Returns TRUE if the specified value is empty...
  function wfv_isEmpty(strVal) {
    if (strVal == null || strVal == "")
      return true;
    else
      return false;
  }


  // Returns TRUE if the input field inputObj has no value
  function wfv_textIsEmpty(inputObj) {
    return wfv_isEmpty(inputObj.value);
  }


  // Returns FALSE if the input field is empty, after displaying
  // a message to the user.
  function wfv_validateTextNotEmpty(inputObj, fieldDesc) {
    if (wfv_textIsEmpty(inputObj)) {
      if (fieldDesc == null)
        fieldDesc = inputObj.name;
      alert(fieldDesc + " can not be blank. Please supply a value.");
      inputObj.focus();
      return false;
    }
    else
      return true;
  }


  // Returns TRUE if radioBtnObj has NO values selected...
  function wfv_radioIsEmpty(radioBtnObj) {
    for (var i = 0; i < radioBtnObj.length; i++) {
      if (radioBtnObj[i].checked == true)
        return false;
    } // for
    return true;
  }


  // Returns FALSE if the radio array has no selection, after displaying
  // a message to the user.
  function wfv_validateRadioSelected(radioBtnObj, fieldDesc) {
    if (wfv_radioIsEmpty(radioBtnObj)) {
      if (fieldDesc == null)
        fieldDesc = radioBtnObj[0].name;
      alert(fieldDesc + " must be selected. Please select a value.");
      radioBtnObj[0].focus();
      return false;
    }
    else
      return true;
  }


  // Returns TRUE if selectObj has NO values selected. "No value"
  // is defined as one or more options with a specified "value"
  // attribute with length >= 0, or and option with an
  // empty value, but whose text starts with the "--" characters.
  function wfv_selectIsEmpty(selectObj) {
    for (var i = 0; i < selectObj.length; i++) {
      if (selectObj.options[i].selected == true) {
        if (selectObj.options[i].value != "")
          return false;
        if (selectObj.options[i].text.length > 0 &&
            selectObj.options[i].text.substr(0,2) != "--")
          return false;
      }
    } // for
    return true;
  }


  // Returns FALSE if the select object has no selection, after displaying
  // a message to the user.
  function wfv_validateOptionSelected(selectObj, fieldDesc) {
    if (wfv_selectIsEmpty(selectObj)) {
      if (fieldDesc == null)
        fieldDesc = selectObj.name;
      alert(fieldDesc + " must have an option selected. Please select a value.");
      selectObj.focus();
      return false;
    }
    else
      return true;
  }




  // Returns TRUE if the input field inputObj represents an Integer value...
  function wfv_textIsInteger(inputObj) {

    var strVal = inputObj.value;
    if (strVal == null) {
      // Allow empty values to pass...
      return true;
    }

    for (var i = 0; i < strVal.length; i++) {
      var nextChar = strVal.charAt(i);
      if (i == 0 && nextChar == '-')
        continue;

      if (nextChar < '0' || nextChar > '9')
        return false;

    } // for

    return true;
  }

  // Returns TRUE if the value stored in inputObj is within the specified
  // numeric range...
  function wvf_textIntegerInRange(inputObj, minVal, maxVal) {

     var currentVal = parseInt(inputObj.value);
     if (isNaN(currentVal) ||
         currentVal < minVal ||
         (arguments.length > 2 && currentVal > maxVal))
       return false;
     else
       return true;
  }



  // Returns FALSE if the input field is not a valid Integer, after displaying
  // a message to the user. If minVal and maxVal are not null,
  // the value is also checked to insure it is in range.
  function wfv_validateTextIsInteger(inputObj, fieldDesc, minVal, maxVal) {

    if (wfv_textIsEmpty(inputObj)) {
      // Blanks are verified by calling wfv_validateTextNotEmpty()
      return true;
    }

    if (fieldDesc == null)
      fieldDesc = inputObj.name;

    if (wfv_textIsInteger(inputObj) == true) {
      if (arguments.length == 3) {
          if (wvf_textIntegerInRange(inputObj, minVal) == false) {
            alert(fieldDesc + " must be greater than or equal to " + minVal);
            inputObj.focus();
            return false;
          }
      }
      else if (arguments.length == 4) {
          if (wvf_textIntegerInRange(inputObj, minVal, maxVal) == false) {
            alert(fieldDesc + " must be between " + minVal + " and " + maxVal);
            inputObj.focus();
            return false;
          }
      }
    }
    else {
      alert("Invalid numeric format for " + fieldDesc + ". Please supply a valid value. Note that only whole numbers (no decimals) can be entered.");
      inputObj.focus();
      return false;
    }

    return true;

  }



  // Returns TRUE if the input field inputObj represents a number, which
  // can include decimal points...
  function wfv_textIsNumber(inputObj) {

    var strVal = inputObj.value;
    if (strVal == null) {
      // Allow empty values to pass...
      return true;
    }

    var foundDecimal = false;
    for (var i = 0; i < strVal.length; i++) {
      var nextChar = strVal.charAt(i);
      if (i == 0 && nextChar == '-')
        continue;

      if (nextChar == '.' && foundDecimal == false) {
        foundDecimal = true;
        continue;
      }

      if (nextChar < '0' || nextChar > '9')
        return false;

    } // for

    return true;
  }


  // Returns TRUE if the value stored in inputObj is within the specified
  // numeric range...
  function wvf_textNumberInRange(inputObj, minVal, maxVal) {

     var currentVal = parseFloat(inputObj.value);
     if (isNaN(currentVal) ||
         currentVal < minVal ||
         (arguments.length > 2 && currentVal > maxVal))
       return false;
     else
       return true;
  }




  // Returns FALSE if the input field is not a valid number, after displaying
  // a message to the user. If minVal and maxVal are not null,
  // the value is also checked to insure it is in range.
  function wfv_validateTextIsNumber(inputObj, fieldDesc, minVal, maxVal) {

    if (wfv_textIsEmpty(inputObj)) {
      // Blanks are verified by calling wfv_validateTextNotEmpty()
      return true;
    }

    if (fieldDesc == null)
      fieldDesc = inputObj.name;

    if (wfv_textIsNumber(inputObj) == true) {
      if (arguments.length == 3) {
          if (wvf_textNumberInRange(inputObj, minVal) == false) {
            alert(fieldDesc + " must be greater than or equal to " + minVal);
            inputObj.focus();
            return false;
          }
      }
      else if (arguments.length == 4) {
          if (wvf_textNumberInRange(inputObj, minVal, maxVal) == false) {
            alert(fieldDesc + " must be between " + minVal + " and " + maxVal);
            inputObj.focus();
            return false;
          }
      }
    }
    else {
      alert("Invalid numeric format for " + fieldDesc + ". Please supply a valid value.");
      inputObj.focus();
      return false;
    }

    return true;

  }






  // Returns a Date() object that represents dateStr. If dateStr is
  // not a valid date, null is returned.  Note: Dates prior to
  // Jan 1, 1970 are not supported by Javascript 1.2 and earlier!
  function wfv_parseDate(dateStr) {

    if (dateStr == null)
      return null;

    // Next, change "slashes" to "dashes"...
    dateStr = dateStr.replace(/\//gi, "-");

    // Next, get the location of the dashes...
    var firstSlash = dateStr.indexOf("-");
    var lastSlash = dateStr.lastIndexOf("-");
    if (firstSlash == -1 || firstSlash == lastSlash)
      return null;

    // Get the components...
    var mm = parseInt(dateStr.substring(0, firstSlash), 10);
    var dd = parseInt(dateStr.substring(firstSlash+1, lastSlash), 10);
    var yy = parseInt(dateStr.substring(lastSlash+1, dateStr.length), 10);

    // Insure they are numbers...
    if (isNaN(mm) || isNaN(dd) || isNaN(yy))
      return null;

    // Insure they are in a valid range...
    if (mm < 1 || mm > 12 || dd < 1 || dd > 31 || yy < 0)
      return null;

    if ((mm == 4 || mm == 6 || mm == 9 || mm == 11) && dd > 30)
      return null;

    if (yy < 100) {
      if (yy >= 20)
        yy += 1900;
      else
        yy += 2000;
    }

    if (mm == 2) {
      if ((yy % 4) == 0) {
        // Leap year...
        if (dd > 29)
          return null;
      }
      else {
        // NOT leap year...
        if (dd > 28)
          return null;
      }
    }

    // If we get here, all is OK!
    return new Date(yy, mm, dd);

  }


  // Returns TRUE if the input field inputObj represents a valid
  // date.
  function wfv_textIsDate(inputObj) {
    var dateStr = inputObj.value;
    if (dateStr == null) {
      // Allow empty values to pass...
      return true;
    }

    // Next, change "slashes" to "dashes"...
    dateStr = dateStr.replace(/\//gi, "-");

    // Next, get the location of the dashes...
    var firstSlash = dateStr.indexOf("-");
    var lastSlash = dateStr.lastIndexOf("-");
    if (firstSlash == -1 || firstSlash == lastSlash)
      return false;

    // Get the components...
    var mm = parseInt(dateStr.substring(0, firstSlash), 10);
    var dd = parseInt(dateStr.substring(firstSlash+1, lastSlash), 10);
    var yy = parseInt(dateStr.substring(lastSlash+1, dateStr.length), 10);

    // Insure they are numbers...
    if (isNaN(mm) || isNaN(dd) || isNaN(yy))
      return false;

    // Insure they are in a valid range...
    if (mm < 1 || mm > 12 || dd < 1 || dd > 31 || yy < 0)
      return false;

    if ((mm == 4 || mm == 6 || mm == 9 || mm == 11) && dd > 30)
      return false;

    if (yy < 100) {
      if (yy >= 20)
        yy += 1900;
      else
        yy += 2000;
    }

    if (mm == 2) {
      if ((yy % 4) == 0) {
        // Leap year...
        if (dd > 29)
          return false;
      }
      else {
        // NOT leap year...
        if (dd > 28)
          return false;
      }
    }

    // If we get here, all is OK!
    return true;

  }


  // Returns TRUE if the value stored in inputObj is within the specified
  // date range. minVal and maxVal (if specified) can be either a string,
  // or a Date() object.
  function wvf_textDateInRange(inputObj, minVal, maxVal) {

     var currentVal = wfv_parseDate(inputObj.value);

     if (minVal != null) {
       if (typeof(minVal) == 'string')
         minVal = wfv_parseDate(minVal);
     }

     if (maxVal != null) {
       if (typeof(maxVal) == 'string')
         maxVal = wfv_parseDate(maxVal);
     }

     if (currentVal == null ||
         currentVal.getTime() < minVal.getTime() ||
         (arguments.length > 2 && currentVal.getTime() > maxVal.getTime()))
       return false;
     else
       return true;
  }



  // Returns FALSE if the input field is not a valid date, after displaying
  // a message to the user. If minVal and maxVal are not null,
  // the value is also checked to insure it is in range.
  function wfv_validateTextIsDate(inputObj, fieldDesc, minVal, maxVal) {

    if (wfv_textIsEmpty(inputObj)) {
      // Blanks are verified by calling wfv_validateTextNotEmpty()
      return true;
    }

    if (fieldDesc == null)
      fieldDesc = inputObj.name;

    if (wfv_textIsDate(inputObj) == true) {
      if (arguments.length == 3) {
          if (wvf_textDateInRange(inputObj, minVal) == false) {
            alert(fieldDesc + " must be greater than or equal to " + minVal.toString());
            inputObj.focus();
            return false;
          }
      }
      else if (arguments.length == 4) {
          if (wvf_textDateInRange(inputObj, minVal, maxVal) == false) {
            alert(fieldDesc + " must be between " + minVal.toString() + " and " + maxVal.toString());
            inputObj.focus();
            return false;
          }
      }
    }
    else {
      alert("Invalid date format for " + fieldDesc + ". Dates should be in the format mm-dd-yyyy.");
      inputObj.focus();
      return false;
    }

    return true;

  }



