// Copyright (c) 2002 Life Cycle Solutions, Inc.   All Rights Reserved

///////////////////////////////////////////////////////////////////
// Methods for validating form elements.
///////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////
/** Validates a single select box. */
/**
 *  Function <code>valRequiredSingleSelect</code>
 *  Check if a value has been selected from the "Options" array associated to
 *  a specified "select" object. If a value has not been selected an Alert box
 *  is presented to the user.
 *
 *  @param     select -  the HTML <code>Select</code> object in which to check for a selected option
 *                       ex. document.MAINFORM.seasonYear
 *  @param     name   -  a  <code>String</code> used to present the label associated to the specified
 *                       Select object
 *                       ex. "Season Year"
 *  @return    boolean - false the value was not valid
 *                       true if the value is valid
 **/

function valRequiredSingleSelect(select, name, checkZero){
   var val = select.options[select.selectedIndex].value;
   if(!hasContent(val) || (checkZero && '0' == val) ){
      alert("You must select a value for '" + name + "'");
      select.focus();
      return false;
   }
   return true;
}
////////////////////////////////////////////////////////////////////
function valRequiredMOAInput(chosen, optionsList, name){
   var size = chosen.options.length;;
   if(size < 1){
      alert("You must select a value for '" + name + "'");
      optionsList.focus();
      return false;
   }
   return true;
}
////////////////////////////////////////////////////////////////////

/**
 *  Function <code>valRequiredStandardInput</code>
 *  Verify that a value has been entered by checking the length of
 *  the string (input.value)
 *
 *  @param     input -  the HTML <code>Input</code> object in which to check that
 *                      a value has been entered
 *                       ex. document.MAINFORM.name
 *  @param     name   -  a  <code>String</code> used to present the label associated
 *                       to the specified Input object
 *                       ex. "Name of Author"
 *  @return    boolean - false the value was not valid
 *                       true if the value is valid
**/

function valRequiredStandardInput(input, name){
   var val = input.value;
   if(trim(val).length < 1){
      alert("You must enter a value for '" + name + "'");
      input.focus();
      return false;
   }
   return true;
}

function isValidTextRange(input, inputLabel, min, max, checkMax, checkMin){

   if(!input){ return false };
   var val = trim(input.value);
   input.value = val;
   if(!val){ return false; }
   // alert("Min = >" + min + "<   Max = >" + max + "<");

   if( (checkMin && checkMax && min && max) && (val.length != min) && (min == max)  ){
      alert("Input Error " + "\r" +
      "Field Label: " + inputLabel + "\r" + "\r" +
      "  The value for [ " + inputLabel +" ] must be:" + "\r" +
      "  equal to [" + min + "] characters in length" + "\r \r" +
      "  The Current value is [ " + val + " ]" );
      input.focus();
      return false;
   }

   if((checkMin && checkMax && min && max) && ((val.length < min) || (val.length > max))){
      alert("Input Error " + "\r" +
      "Field Label: " + inputLabel + "\r" + "\r" +
      "  The value for [ " + inputLabel +" ] must be:" + "\r" +
      "  equal to [" + min + "] or [" + max + "]" + "\r" +
      "  or between [" + min + "] and [" + max + "] characters in length." + "\r \r" +
      "  The Current length is [ " + val.length + " ]" + "\r" +
      "  The Current value is [ " + val + " ]" );
      input.focus();
      return false;
   }

   if((!checkMax && checkMin) && (min > 1) && (val.length < min)){
      alert("Input Error " + "\r" +
      "Field Label: " + inputLabel + "\r" + "\r" +
      "  The value for [ " + inputLabel +" ] must be:" + "\r" +
      "  equal to or greater than " + "\r" +
      "  [" + min + "] characters in length." + "\r \r" +
      "  The Current length is [ " + val.length + " ]" + "\r" +
      "  The Current value is [ " + val + " ]" );
      input.focus();
      return false;
   }

   if((!checkMin && checkMax) && (max > 1) && (val.length > max)){
      alert("Input Error " + "\r" +
      "Field Label: " + inputLabel + "\r" + "\r" +
      "  The value for [ " + inputLabel +" ] must be:" + "\r" +
      "  equal to or less than " + "\r" +
      "  [" + max + "] characters in length." + "\r \r" +
      "  The Current length is [ " + val.length + " ]" + "\r" +
      "  The Current value is [ " + val + " ]" );
      input.focus();
      return false;
   }
}

////////////////////////////////////////////////////////////////////
/** Validates a standard input box. */
/**
 *  Function <code>valRequiredStandardInput</code>
 *  Verify that a value has been entered by checking the length of
 *  the string (input.value)
 *
 *  @param     input -  the HTML <code>Input</code> object in which to check that
 *                      a value has been entered
 *                       ex. document.MAINFORM.name
 *  @param     name   -  a  <code>String</code> used to present the label associated
 *                       to the specified Input object
 *                       ex. "Name of Author"
 *  @return    boolean - false the value was not valid
 *                       true if the value is valid
**/
function valRequiredHiddenInput(input, name){
   var val = input.value;
   if(trim(val).length < 1){
      alert("You must enter a value for '" + name + "'");
      return false;
   }
   return true;
}

////////////////////////////////////////////////////////////////////
/** Validates a standard input box. */
/**
 *  Function <code>valRequiredNumericStandardInput</code>
 *  Verify that a value has been entered by checking the length of
 *  the string (input.value)
 *
 *  @param     input -  the HTML <code>Input</code> object in which to check that
 *                      a value has been entered
 *                       ex. document.MAINFORM.name
 *  @param     name   -  a  <code>String</code> used to present the label associated
 *                       to the specified Input object
 *                       ex. "Name of Author"
 *  @return    boolean - false the value was not valid
 *                       true if the value is valid
**/

function valRequiredNumericStandardInput(input, name){
   var val = input.value;
   if(trim(val).length < 1){
      alert("You must enter a value for '" + name + "'");
                input.focus();
      return false;
   }
   return true;
}

////////////////////////////////////////////////////////////////////
/** Validates a input box that requires an numeric value. */
/**
 *  Function <code>valInputWithNumericValue</code>
 *  Verify that a numeric value has been entered by checking the length of
 *  the string (input.value) and if the value converts to a number.
 *
 *  @param     input -  the HTML <code>Input</code> object in which to check that
 *                      a value has been entered
 *                       ex. document.MAINFORM.numberOfChairs
 *  @param     name   -  a  <code>String</code> used to present the label associated
 *                       to the specified Input object
 *                       ex. "Number Of Chairs"
 *  @return    boolean - false the value was not a number
 *                       true if the value is a number
**/

function valInputWithNumericValue(input, name){
   var val = input.value;
   if(val.length > 1 && !isNumber(val)){
      alert("You must enter a numeric value for '" + name + "'");
                input.focus();
      return false;
   }
   return true;
}
////////////////////////////////////////////////////////////////////
/** Validates a input box that requires an numeric value and the value must
 *  be in a specific range. */
/**
 *  Function <code>valInputWithNumericRangeValue</code>
 *  Verify that a numeric value has been entered by checking the length of
 *  the string (input.value) and if the value converts to a number, and if
 *  the value is between a min and max specified value.
 *
 *  @param     input -  the HTML <code>Input</code> object in which to check that
 *                      a value has been entered
 *                       ex. document.MAINFORM.numberOfChairs
 *  @param     name   -  a  <code>String</code> used to present the label associated
 *                       to the specified Input object
 *                       ex. "Number Of Chairs"
 *  @param     min    -  the minimum allowed value
 *  @param     max    -  the maximum allowed value
 *  @return    boolean - false the value was not a number
 *                       true if the value is a number
**/
function valInputWithNumericRangeValue(input, name, min, max){
   var val = input.value;
   var intval = parseInt(val,10);
   if(val.length > 1 && !isNumber(val)) {
      alert("You must enter a numeric value between " + min + " and " + max + " for '" + name + "'");
                input.focus();
      return false;
   }
   if (val.length == 0 || intval < min || intval > max) {
      alert("You must enter a numeric value between " + min + " and " + max + " for '" + name + "'");
                input.focus();
      return false;
   }

   return true;
}
////////////////////////////////////////////////////////////////////
/** I am not sure what this does */
/**
 *  Function <code>valCompositeValue</code>
 *  Check if a value has been selected from the "Options" array associated to
 *  a specified "select" object. If a value has not been selected an Alert box
 *  is presented to the user.
 *
 *  @param     field -  the HTML <code>Input</code> object in which to check that
 *                      a value has been entered
 *                       ex. document.MAINFORM.name
 *  @param     name   -  a  <code>String</code> used to present the label associated
 *                       to the specified Input object
 *                       ex. "Name of Author"
 *  @return    boolean - false the value was not valid
 *                       true if the value is valid
**/

function valCompositeValue(field, name){
   var compositeMOA = field.value;
   var total = tallyCompositMOA(compositeMOA);
   if(total != 100){
      alert("'" + name + "' must add up to 100, it is currently " + total);
      return false;
   }
   return true;
}

function valCompositeValueNotRequired(field, name){
   var compositeMOA = field.value;
   var total = tallyCompositMOA(compositeMOA);
   if(total > 0 && total != 100){
      alert("'" + name + "' must add up to 100, it is currently " + total);
      return false;
   }
   return true;
}


/////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////
/** Validates a input box that requires an Currency as a value. */
/**
 *  Function <code>isValidcurrency</code>
 *  Check if a value converts to a number using the JS system isNaN. If
 *  the number does not have a trail decimal point and/or at least one
 *  number after the decimal point then a decimal point is added to
 *  the string and two trailing zeros
 *
 *  @param   val    -  a <code>String</code> representation of a number
 *  @param   inputLabel    -  a  <code>String</code> the label assigned to the filed
 *  @param   inputFieldName    -  a  <code>String</code> name of the input field
 *
 *  @return    boolean - false the value was not a number
 *                       true if the value is a number
**/

function isValidCurrency(inputObj, inputLabel, numOfDecimals){
   var val = inputObj.value;
   var name = inputObj.name;
   var newVal = "";
   var decimalPos = 0;
   var alertStr = "";
   var numOfDecimalPoints = getNumOfOccurancesOfaChar(val, ".");
   var numOfCommas = getNumOfOccurancesOfaChar(val, ",");
   var numOfDenomination = getNumOfOccurancesOfaChar(val, "$");

   if(isNaN(val)){
      alertStr = "Input Error " + "\r" +
            "Field Label: " + inputLabel + "\r" + "\r" +
            "  The value [ " + val +" ] entered is not a number." + "\r" +
            "  Only floating point numbers are allowed in this field \r \r";

      if( numOfDecimalPoints > 1){
        alertStr = alertStr + " - There are [ " + numOfDecimalPoints + " ] decimal points in the number. \r" +
                   "   Remove all except 1. \r \r";
      }
      if(numOfDenomination != 0){
        alertStr = alertStr + " - The value contains the dollar sign [$]. \r" +
                              "   Remove the dollar sign. \r";
      }
      if(numOfCommas != 0){
        alertStr = alertStr + " - The value contains commas [,]. \r" +
                              "   Remove all commas. \r";
      }

      alert(alertStr);

      inputObj.focus();
      return false;
   }
   else{
    if(val != ""){
      newVal = val;
      if(numOfDecimalPoints == 0){newVal = newVal + ".";}
      newVal = addZerosToDecimal(newVal, numOfDecimals);
      inputObj.value = newVal;
      return true;
    }
  }
}


function isValidCurrencyRange(inputObj, inputLabel, sigDig, minVal, maxVal, checkMax, checkMin){

   if(!isValidCurrency(inputObj, inputLabel, sigDig)){ return false; }
   var val = inputObj.value;
   var name = inputObj.name;
   if(val == "")return true;

   var valNum = parseFloat(val);

   if((!checkMax && checkMin) && (valNum < minVal) ){
      alert("Input Error " + "\r" +
      "Field Label: " + inputLabel + "\r" + "\r" +
      "  The value for [ " + inputLabel +" ] must be:" + "\r" +
      "  equal to or greater than [ " + minVal +" ]" + "\r \r" +
      "  The Current value is [ " + val + " ]" );
      inputObj.focus();
      return false;
   }

   if( (!checkMin && checkMax) && (valNum > maxVal) ){
      alert("Input Error " + "\r" +
      "Field Label: " + inputLabel + "\r" + "\r" +
      "  The value for [ " + inputLabel +" ] must be:" + "\r" +
      "  equal to or less than [ " + maxVal +" ]" + "\r \r" +
      "  The Current value is [ " + val + " ]" );
      inputObj.focus();
      return false;
   }

   if((checkMin && checkMax) && (valNum < minVal || valNum > maxVal) ){
      alert("Input Error " + "\r" +
      "Field Label: " + inputLabel + "\r" + "\r" +
      "  The value for [ " + inputLabel +" ] must be:" + "\r" +
      "  equal to [" + minVal + "] or [" + maxVal + "]" +
      "  or between [" + minVal + "] and [" + maxVal + "]" + "\r \r" +
      "  The Current value is [ " + val + " ]" );
      inputObj.focus();
      return false;
   }
   return true;
}

function addZerosToDecimal(val, numOfDecimals){
  var decimalPos = 0;
  var newVal = "";
  decimalPos = val.indexOf(".");
  zerosToAdd = numOfDecimals - ((val.length - 1) - decimalPos)
  newVal = val;
  for(var i = 0; i < zerosToAdd; i++ ){
    newVal = newVal + "0";
  }
  return newVal;
}

function getNumOfOccurancesOfaChar(strVal, strChar){
   var pos = 0;
   var numOfDecimalPoints = -1;
   var i = -1;
   while(pos != -1){
      pos = strVal.indexOf(strChar, i + 1);
      numOfDecimalPoints = numOfDecimalPoints + 1;
      i = pos;
   }
   return numOfDecimalPoints;
}



////////////////////////////////////////////////////////////////////
/** Validates a input box that requires an Floating point as a value. */
/**
 *  Function <code>isValidfloat</code>
 *  Check if a value converts to a number using the JS system isNaN. If
 *  the number does not have a trail decimal point and/or at least one
 *  number after the decimal point then a decimal point is added to
 *  the string and a trailing zero
 *
 *  @param   val    -  a <code>String</code> representation of a number
 *  @param   inputLabel    -  a  <code>String</code> the label assigned to the filed
 *  @param   inputFieldName    -  a  <code>String</code> name of the input field
 *
 *  @return    boolean - false the value was not a number
 *                       true if the value is a number
**/

function isValidFloat(inputObj, inputLabel, numOfDecimals){
   var val = inputObj.value;

   if(!val || val.length == 0){
      return;
      // inputObj.value = 0.0;
   }
   var name = inputObj.name;
   var newVal = "";
   var decimalPos = 0;
   var alertStr = "";
   var numOfDecimalPoints = getNumOfOccurancesOfaChar(val, ".");
   var numOfCommas = getNumOfOccurancesOfaChar(val, ",");
   if(isNaN(val)){
      alertStr = "Input Error " + "\r" +
            "Field Label: " + inputLabel + "\r" + "\r" +
            "  The value [ " + val +" ] entered is not a number." + "\r" +
            "  Only floating point numbers are allowed in this field \r \r";

      if( numOfDecimalPoints > 1){
        alertStr = alertStr + " - There are [ " + numOfDecimalPoints + " ] decimal points in the number. \r" +
                   "   Remove all except 1. \r \r";
      }
      if(numOfCommas != 0){
        alertStr = alertStr + " - The value contains commas [,]. \r" +
                              "   Remove all commas. \r";
      }

      alert(alertStr);

      inputObj.focus();
      return false;
   }
   else{
    if(val != ""){
       newVal = val;
       if(numOfDecimalPoints == 0){newVal = newVal + ".";}
       newVal = addZerosToDecimal(newVal, numOfDecimals);
       inputObj.value = newVal;
       return true;
    }
   }
}

////////////////////////////////////////////////////////////////////
/** Validates a input box that requires an Integer as a value. */
/**
 *  Function <code>isValidInteger</code>
 *  Check if a value converts to a number using the JS system isNaN
 *
 *  @param     val    -  a  <code>String</code> representation of a number
 *
 *  @return    boolean - false the value was not a number
 *                       true if the value is a number
**/

function isValidInteger(inputObj, inputLabel){
   var val = inputObj.value;
   if(!val || val.length == 0){
      return;
      // inputObj.value = 0;
   }

   var name = inputObj.name;
   if(isNaN(val)){
      alert("Input Error " + "\r" +
            "Field Label: " + inputLabel + "\r" + "\r" +
            "  The value [ " + val +" ] entered is not an integer." + "\r" +
            "  Only numbers are allowed in this field \r");
      inputObj.value = "";
      inputObj.focus();

      return false;
   }
   else{
      if( val.indexOf(".") == -1){
        inputObj.value = val;
        return true;
       } else {
        alert("Input Error " + "\r" +
              "Field Label: " + inputLabel + "\r" + "\r" +
              "  The value [ " + val +" ] entered can not contain a " + "\r" +
              "  decimal point. Only Integers are allowed in this field");
        inputObj.value = "";
        inputObj.focus();
        return false;
      }
   }
}

function checkSetRGBColor(inputObj, inputLabel, minVal, maxVal){



}

////////////////////////////////////////////////////////////////////
/** Validates a input box that requires an Integer as a value. */
/**
 *  Function <code>isValidInteger</code>
 *  Check if a value converts to a number using the JS system isNaN
 *
 *  @param     val    -  a  <code>String</code> representation of a number
 *
 *  @return    boolean - false the value was not a number
 *                       true if the value is a number
**/

function isValidIntegerRange(inputObj, inputLabel, minVal, maxVal, checkMax, checkMin){

   if(!isValidInteger(inputObj, inputLabel)){ return false; }
   var val = inputObj.value;
   var name = inputObj.name;
   if(val == "")return true;

   var valNum = parseInt(val,10);

   if((!checkMax && checkMin) && (valNum < minVal) ){
      alert("Input Error " + "\r" +
      "Field Label: " + inputLabel + "\r" + "\r" +
      "  The value for [ " + inputLabel +" ] must be:" + "\r" +
      "  equal to or greater than [ " + minVal +" ]" + "\r \r" +
      "  The Current value is [ " + val + " ]" );
      inputObj.focus();
      return false;
   }

   if((!checkMin && checkMax) && (valNum > maxVal) ){
      alert("Input Error " + "\r" +
      "Field Label: " + inputLabel + "\r" + "\r" +
      "  The value for [ " + inputLabel +" ] must be:" + "\r" +
      "  equal to or less than [ " + maxVal +" ]" + "\r \r" +
      "  The Current value is [ " + val + " ]" );
      inputObj.focus();
      return false;
   }

   if((checkMin && checkMax) && (valNum < minVal || valNum > maxVal) ){
      alert("Input Error " + "\r" +
      "Field Label: " + inputLabel + "\r" + "\r" +
      "  The value for [ " + inputLabel +" ] must be:" + "\r" +
      "  equal to [" + minVal + "] or [" + maxVal + "]" +
      "  or between [" + minVal + "] and [" + maxVal + "]" + "\r \r" +
      "  The Current value is [ " + val + " ]" );
      inputObj.focus();
      return false;
   }

   return true;
}


function isValidFloatRange(inputObj, inputLabel, sigDig, minVal, maxVal, checkMax, checkMin){

   if(!isValidFloat(inputObj, inputLabel, sigDig)){ return false; }
   var val = inputObj.value;
   var name = inputObj.name;
   if(val == "")return true;
   var valNum = parseFloat(val);

   if((!checkMax && checkMin) && (valNum < minVal) ){
      alert("Input Error " + "\r" +
      "Field Label: " + inputLabel + "\r" + "\r" +
      "  The value for [ " + inputLabel +" ] must be:" + "\r" +
      "  equal to or greater than [ " + minVal +" ]" + "\r \r" +
      "  The Current value is [ " + val + " ]" );
      inputObj.focus();
      return false;
   }

   if((!checkMin && checkMax) && (valNum > maxVal )){
      alert("Input Error " + "\r" +
      "Field Label: " + inputLabel + "\r" + "\r" +
      "  The value for [ " + inputLabel +" ] must be:" + "\r" +
      "  equal to or less than [ " + maxVal +" ]" + "\r \r" +
      "  The Current value is [ " + val + " ]" );
      inputObj.focus();
      return false;
   }

   if((checkMin && checkMax) && ((valNum < minVal) || (valNum > maxVal)) ){
      alert("Input Error " + "\r" +
      "Field Label: " + inputLabel + "\r" + "\r" +
      "  The value for [ " + inputLabel +" ] must be:" + "\r" +
      "  equal to [" + minVal + "] or [" + maxVal + "]" +
      "  or between [" + minVal + "] and [" + maxVal + "]" + "\r \r" +
      "  The Current value is [ " + val + " ]" );
      inputObj.focus();
      return false;
   }


   return true;
}



////////////////////////////////////////////////////////////////////
/** Validates a input box that requires an numeric value. */
/**
 *  Function <code>isNumber</code>
 *  Check if a value converts to a number using the JS system isNaN
 *
 *  @param     val    -  a  <code>String</code> representation of a number
 *
 *  @return    boolean - false the value was not a number
 *                       true if the value is a number
**/

function isNumber(val){
   if(isNaN(val)){
      return false;
   }
   else{
      return true;
   }
}

////////////////////////////////////////////////////////////////////
/** Checks the month Abrev . */
/**
 *  Function <code>isMonthValidTextAbrev</code>
 *  Check if a string is a valid Month Adreviation
 *
 *  @param     month3LetterName  -  a  <code>String</code> the text Adreviation of the month
 *  @param     monthAbrevArray   -  a  <code>Array</code> of text month abreviations
 *
 *  @return    string -  the value found in upper case
 *                       or "" if the value was not found
**/

function isMonthValidTextAbrev(month3LetterName, monthAbrevArray){
   var monthFound = "";
   month3LetterName = month3LetterName.toUpperCase();
   for(var i = 0; i < monthAbrevArray.length ; ++i){
     if (month3LetterName == monthAbrevArray[i]){
        monthFound = monthAbrevArray[i];
     }
   }
   return monthFound;
}

////////////////////////////////////////////////////////////////////
/** Checks the month Abrev . */
/**
 *  Function <code>getMonthTextAbrevStrIndex</code>
 *  Converts the Month Adreviation to a integer String Code 01 - 12
 *
 *  @param     month3LetterName  -  a  <code>String</code> the text Adreviation of the month
 *  @param     monthAbrevArray   -  a  <code>Array</code> of text month abreviations
 *
 *  @return    string -  the indexed value of the position of the Month in the Array
 *                       if the position is a single digit ex. 1 a zero will be added
 *                       to the string
**/

function getMonthTextAbrevStrIndex(month3LetterName, monthAbrevArray){
   var monthStrPosition = "";
   var monthIntPosition = 0;
   month3LetterName = month3LetterName.toUpperCase();
   for(var i = 0; i < monthAbrevArray.length ; ++i){
     if (month3LetterName == monthAbrevArray[i]){
        monthIntPosition = i + 1;
        if (monthIntPosition < 10){
           monthStrPosition = "0" + monthIntPosition.toString();
         } else {
           monthStrPosition = monthIntPosition.toString();
        }
     }
   }
   return monthStrPosition;
}

////////////////////////////////////////////////////////////////////
/** Validates a input box that requires an numeric value. */
/**
 *  Function <code>isNumber</code>
 *  Check if a value converts to a number using the JS system isNaN
 *
 *  @param     val    -  a  <code>String</code> representation of a number
 *
 *  @return    boolean - false the value was not a number
 *                       true if the value is a number
**/

function isValidDate(inputObj, inputLabel, validationMode){
   var dateString = inputObj.value;
   var inputFieldName = inputObj.name;
   var alertString = "The following errors have been detected in the format of the Date";
   dateReturnString = "";
   if(dateString != ""){
      dateReturnString = chkDateString(dateString, validationMode);
      //alert("dateReturnString >" + dateReturnString);
      if(dateReturnString.substring(0,3) == "err"){
			
         alert("Format of '" + inputLabel + "' should be: MM/DD/YYYY");
         inputObj.focus();
         return false;
      }
      else{
         inputObj.value = dateReturnString;
         return true;
      }
   }
}

////////////////////////////////////////////////////////////////////
/**
 *  Function <code>isValidDate</code>
 *  Check to see if the format of a date, entered as a string, is valid.
 *   The following features are built into this function:
 *     1) takes a date string and places it in a form being used by
 *         the oracle procedures Month-Day-Year.
 *     2) The function will operate on dates with either a "-" or a "/"
 *         as a dilimiter
 *     3) The date formats handled are listed under the parameter description
 *         of dateString
 *     4) Any numeric month ids will be replaced with their Alpha equivlaent
 *     5) The number of days in a month are validated
 *     6) Leap years are accounted for for the month of February
 *   rev. 12 Dec 02
 *     7) The type of validation applied can be specified by a validationMode
 *         interger flag
 *
 *  @param  dateString -  a  <code>String</code> a string in the format of a date
 *                        The following patterns are interpreted by this function
 *                        where the format is Month-Day-Year
 *                        3/7/2002, 03/7/2002, 03/07/2002, MAR/7/2002,  MAR/07/2002
 *                        3-7-2002, 03-7-2002, 03-07-2002, MAR-7-2002,  MAR-07-2002
 *
 *  @param  inputLabel -  a  <code>String</code> the input field label
 *
 *  @param  inputFieldName -  a  <code>String</code> the input field name defined in the
 *                            HTML page
 *
 *  @param  validationMode -  an <code>Integer</code>
 *                        0 - validation occurs for and enforces month-day-year
 *                                                  mm-dd-yyyy, mm-dd-yy
 *                        1 - validation occurs for and enforces day-month-year
 *                                                  dd-mm-yyyy,  dd-mm-yy
 *                        10 - validation occurs for and enforces month-day-year
 *                                                  mm-dd-yyyy, MMM-dd-yyyy,
 *                                                  mm-dd-yy  , MMM-dd-yy,
 *                        11 - validation occurs for and enforces day-month-year
 *                                                  dd-mm-yyyy, dd-MMM-yyyy,
 *                                                  dd-mm-yy  , dd-MMM-yy,
 *                       // Option 2 is not available Yet
 *                        2 - validation occurs for possible permutations
 *                                                  dd-mm-yyyy, dd-MMM-yyyy,
 *                                                  dd-mm-yy  , dd-MMM-yy,
 *                                                  mm-dd-yyyy, MMM-dd-yyyy,
 *                                                  mm-dd-yy  , MMM-dd-yy,
 *
 *  @return  dateToUse -  a <code>String</code> used to present a date, The date will always
 *                        be returned in the form JAN-2-2002 OR JAN-02-2002
**/



function chkDateString(dateString, validationMode){
   var temp = 0;
   var delimPos1 = -1;
   var delimPos2 = -1;
   var element1 = "";
   var tempE1 = 0;
   var tempIdE1 = "";
   var element2 = "";
   var tempE2 = 0;
   var tempIdE2 = "";
   var day = "";
   var chkDay = "";
   var tempd = 0;
   var month = "";
   var monthName = "";
   var chkMonth = "";
   var tempm = 0;
   var year = "";
   var chkYear = "";
   var tempy = 0;
   var leapYear = false;
   var delimiter = "/";
   var monthNames = new Array("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC");
   // The month abrevation array in the future can be assigned as an "isValidDate" parameter
   // so that language differences can be accomodated.
   var monthAbrevArray = new Array("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC");
   var monthFound = false;
   // var yearStr = "";
   var divisor = 0;
   var dateToUse = "";
   var dateFormatType = 0; // specifies dd-mm-yyyy or mm-dd-yyyy
                           // dd-MMM-yyyy or MMM-dd-yyyy
   var dateConstruction = "dmy";
   var alertFormat = "";
   var formatAlert = "";
   var yearAlert = "";
   var monthAlert = "";
   var dayAlert = "";

   // check the boundaries of the string length
   if((dateString.length < 6) || (dateString.length > 12)){
      formatAlert = "The length of the date entered is not valid \r";
      // alert(formatAlert);
      // return dateToUse;
     }
    else{

      // check for the default delimiter "\" and acquire its first position
      // If the default delimiter does not exist set the delimiter variable
      //   to the Alternate delimiter "\"
      delimPos1 = dateString.indexOf(delimiter);
      if (delimPos1 == -1) {
          delimiter = "-";
          delimPos1 = dateString.indexOf(delimiter);
      }

      // check for the first occurance Alternate delimiter "-"
      if ( delimPos1 == -1) {
          formatAlert = "The date delimiters / or - were not found in the date entered \r";
          // return dateToUse;
        }else{
         // check for the position of the second delimiter
         delimPos2 = dateString.indexOf(delimiter, delimPos1 + 1);
      }


      // if the second delimiter exists
      if ((delimPos1 == -1) || (delimPos2 == -1)){
          formatAlert = "The date delimiters / or - were not found in the date entered \r";
          // return dateToUse;
        }
       else {
         if ((delimPos1 >= 1) && (delimPos1 <= 3)){
           //alert("checkDate 1 , dateString = " + dateString + "  validationMode = " + validationMode);
            element1 = dateString.substring(0,delimPos1);
            element2 = dateString.substring(delimPos1 + 1, delimPos2);
            chkYear = dateString.substring(delimPos2 + 1 , dateString.length );

            // validationMode == 0 , enforce Mouth-Day-Year
            if ((validationMode == 0) || (validationMode == 10)){  //  mm-dd-yyyy
           //alert("checkDate 2 , dateString = " + dateString);
               alertFormat = " Date Format Should be: \r   Mouth-Day-Year [mm-dd-yyyy] as number only";
               chkMonth = element1;
               chkDay = element2;
             } else if ((validationMode == 1) || (validationMode == 11)){ // dd-mm-yyyy
           //alert("checkDate 3 , dateString = " + dateString);
               alertFormat = " Date Format Should be: \r   Day-Mouth-Year [dd-mm-yyyy] as number only";
               chkMonth = element2;
               chkDay = element1;
             } else { // default to mm-dd-yyyy
               if(isNumber(element1)){
                 tempE1 = parseInt(element1,10);
                 if(tempE1 > 0 && tempE1 < 13)
                   { tempIdE1 = "N_MorD";}
                  else if((tempE1 > 12) && (tempE1 < 31))
                   { tempIdE1 = "N_D";}
                }else{
                   { tempIdE1 = "A_M";}
               }
               if(isNumber(element2)){
                 tempE2 = parseInt(element2,10);
                 if((tempE2 > 0) && (tempE2 < 13)){
                    tempIdE2 = "N_MorD";}
                  else if(tempE2 > 12 && tempE2 < 31){
                    tempIdE2 = "N_D";
                 }
                }else{
                  tempIdE2 = "A_M";
               }

               if((tempIdE1 == "N_MorD") && (tempIdE2 == "N_MorD")){
                // Use International, dd-mm-yyyy or dd-MMM-yyyy
                  chkMonth = element2;
                  chkDay = element1;
                  dateConstruction = "dmy";
                }
               else if(((tempIdE1 == "N_D") && (tempIdE2 == "N_M"))||
                       ((tempIdE1 == "N_D") && (tempIdE2 == "A_M")) )
                // dd-mm-yyyy or dd-MMM-yyyy
                { chkMonth = element2;
                  chkDay = element1;
                  dateConstruction = "dmy";
                }
               else if(((tempIdE1 == "N_M") && (tempIdE2 == "N_D"))||
                       ((tempIdE1 == "A_M") && (tempIdE2 == "N_D")) )
                // mm-dd-yyyy or MMM-dd-yyyy
                { chkMonth = element1;
                  chkDay = element2;
                  dateConstruction = "mdy";
                }
               else
                { return dateToUse;}

            }

          // alert("checkDate 4 , element1 = " + element1 + "   element2 = " + element2);
          //alert("checkDate 4a, chkYear = " + chkYear + "   chkMonth = " + chkMonth+ "   chkDay = " + chkDay);

            if(isNumber(chkYear)){
              tempy = parseInt(chkYear,10);
              //allow for the input of 02 instead of 2002
              //
              if((tempy > 0) && (tempy < 100)){
                tempy = 2000 + tempy;
                chkYear = tempy.toString();
              }
              if((tempy < 1900) || (tempy > 2100)){
                 yearAlert = "The Year element of the date can only contain numbers between 1900 and 2100. \r";
                 // return dateToUse;
              }
              // check to see if the year entered is a leap year
              divisor = (tempy - 1896) / 4;
              divisorInt = parseInt(divisor.toString(), 10);
              if(divisor == divisorInt){
                //alert("divisor check - It is leapyear");
                leapYear = true;
              }
              if (yearAlert == ""){
                 year = chkYear;
              }
             }else{
              //error alphas are not allowed
              yearAlert = "The Year element of the date can only contain numbers \r";
              // return dateToUse;
            }


          //alert("checkDate 5 ");
          //alert("checkDate 5, year = " + year + "   chkMonth = " + chkMonth+ "   chkDay = " + chkDay);
           // deal with chkMonth
            if(!isNumber(chkMonth)){
               monthName = isMonthValidTextAbrev(chkMonth, monthAbrevArray )
               if (monthName == ""){
                  monthAlert = "The month abrevreation entered is not valid \r";
                } else {
                 if((validationMode == 0) || (validationMode == 1)){
                   // convert ex. "FEB" to "02"
                    month = getMonthTextAbrevStrIndex(monthName, monthAbrevArray)
                  } else {
                   month = monthName;
                 }
               }
              } else {
               tempm = parseInt(chkMonth,10);
               // alert("checkDate 5a -  tempm = " + tempm)
               if((tempm < 1) || (tempm > 12)){
                  monthAlert = "The month date element must be between 1 and 12 \r";
                  //return dateToUse;
                 }
                else {
                  if (tempm < 10){
                    month = "0" + tempm.toString();
                  } else {
                    month = tempm.toString();
                  }
               }
            }

           //alert("checkDate 6 ");
            // deal with chkDay next
            // Check for days between 1 - 30, 1 - 31, 1 - 28
            //  depending on month value,
            // If the month is Feb use  1 and 30
            // If the month is April, June, Sept, or Nov then use
            //   1 and 30
            // All others use 1 - 31
            if(isNumber(chkDay)){
              tempd = parseInt(chkDay,10);
              // if the month is April, June, Sept, or Nov
              if ( tempm == 4 || tempm == 6 || tempm == 9 || tempm == 11) {
                 if(tempd < 1 || tempd > 30){
                   dayAlert = "The months April, June, Sept, or Nov only 30 days \r";
                   // return dateToUse;
                 }
                }
               // if the month is February and check for a leap year
               else if ( tempm == 2) {
                  if(leapYear){
                     if((tempd < 1) || (tempd > 29)){
                        dayAlert = "The year entered is a leap year. Only 29 days can be entered for Feb.\r";
                        // return dateToUse;
                     }
                    }
                   else {
                     if((tempd < 1) || (tempd > 28)){
                        dayAlert = "The month of Feb. can only have 29 days.\r";
                        // return dateToUse;
                     }
                  }
                }
               else {
                  //alert("Other Months =" + tempm);
                  if(tempd < 1 || tempd > 31){
                     dayAlert = "The month " + month + " can only have up to 31 days.\r";
                     // return dateToUse;
                  }
               }

               if (tempd < 10){
                 day = "0" + tempd.toString();
                } else {
                 day = tempd.toString();
               }
             } else {
               //error alphas are not allowed
               dayAlert = "Only numbers are allowed in the day element of the date.";
               // return dateToUse;
            }

            //alert("checkDate setDate 1 , year = " + year + " month = " + month + "   day = " + day);
         } // end delimPos1 == 1 || delimPos1 == 2
      } // main else

   }
   if ((formatAlert + yearAlert + monthAlert + dayAlert) == "") {
   // Build the dateToUse String  dateConstruction = "mdy"
     if ((validationMode == 0) || (validationMode == 10)){  //  mm-dd-yyyy
        dateToUse = month + "/" + day + "/" + year;
      } else if ((validationMode == 1) || (validationMode == 11)){ // dd-mm-yyyy
        dateToUse = day  + "/" + month + "/" + year;
      } else if (validationMode == 2){ // dd-mm-yyyy
        if (dateConstruction = "mdy"){ dateToUse = day  + "-" + month + "-" + year;}
        if (dateConstruction = "dmy"){ dateToUse = month + "-" + day  + "-" +  year;}
      } else { // default to mm-dd-yyyy
        dateToUse = month + "/" + day + "/" + year;
     }
    } else {
     dateToUse = "err" + alertFormat + "\r" + "\r";
     if (formatAlert != ""){
        dateToUse = dateToUse + "Errors in the Date Format \r" + "   " + formatAlert;
      }
     if (dayAlert != ""){
        dateToUse = dateToUse + "Errors in the Day Element \r" + "   " + dayAlert;
      }
     if (monthAlert != ""){
        dateToUse = dateToUse + "Errors in the Month Element \r" + "   " + monthAlert;
      }
     if (yearAlert != ""){
        dateToUse = dateToUse + "Errors in the Year \r" + "   " + yearAlert;
     }

   }
   // alert("checkDate setDate 2 , dateToUse = " + dateToUse);
   return dateToUse;
}
////////////////////////////////////////////////////////////////////
function valRequiredChooser(input, name){
   var val = input.value;
   if(trim(val).length < 1){
      alert("You must choose a '" + name + "'");
      return false;
   }
   return true;
}
////////////////////////////////////////////////////////////////////
/** Validates a From/To range search widget from searches.
 */
function validateDateRange(fromField, toField, label){
   var fromVal = fromField.value;
   var toVal = toField.value;

   if(toVal && !fromVal){
      alert("If 'To' value is specified, a 'From' value must also be specified.");
      toField.focus();
      return false;
   }
   return true;
}
////////////////////////////////////////////////////////////////////

