//
// Maintenance History
//
// 12/04/2006 (LaSalle) - added isEmpty edit
// 11/19/2006 (LaSalle) - added valid date edit
//

// regular expression(s)
var reDate = new RegExp("[a-zA-Z]{3} [0-9]{1,2}, [0-9]{4}");  // Dates: Jan 01, 2002
var onlySpaceRegexp = /^\s*$/;

function setFocus() {
  var allForms = document.forms;
  var found = false;
  
  for (var i = 0; i <= allForms.length && !found; i++) {
    var currentForm = allForms[i];
    if (currentForm) {
      var fields = currentForm.all;
    
      for (var j = 0; j <= fields.length && !found; j++) {
        var field = fields.item(j);
        if (((field.type == "text") || 
            (field.type == "textarea") || 
            (field.type == "select-one") || 
            (field.type == "password")) && (field.disabled != true)) {
          field.focus();
          found = true;
        }
      }
    }
  }
}

/************************************************
DESCRIPTION: Validates that a string contains only
    valid dates with 2 digit month, 2 digit day,
    4 digit year. Date separator can be ., -, or /.
    Uses combination of regular expressions and
    string parsing to validate date.
    Ex. mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.

REMARKS:
   Avoids some of the limitations of the Date.parse()
   method such as the date separator character.
*************************************************/
function isValidDate( strValue ) {
   var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/

   if (strValue != "") {
      //check to see if in correct format
      if (!objRegExp.test(strValue))
         return false; //doesn't match pattern, bad date
      else {
         //var strSeparator = strValue.substring(2,3) //find date separator
         var strSeparator = "/";
         var arrayDate = strValue.split(strSeparator); //split date into month, day, year
         var intMonth = parseFloat(arrayDate[0]);
         var intDay = parseFloat(arrayDate[1]);
         var intYear = parseFloat(arrayDate[2]);

         //create a lookup for months not equal to Feb.
         var arrayLookup = { 1:31, 3:31, 4:30, 5:31, 6:30, 7:31, 8:31, 9:30, 10:31, 11:30, 12:31}

         //check if month value and day value agree
         if (arrayLookup[intMonth] != null) {
            if (intDay <= arrayLookup[intMonth] && intDay != 0)
               return true; //found in lookup table, good date
         }

         //check for February (bugfix 20050322)
         if (intMonth == 2) {
            if( ((intYear % 4 == 0 && intDay <= 29) || (intYear % 4 != 0 && intDay <=28)) && intDay !=0)
               return true; //Feb. had valid number of days
         }
     }
     return false; //any other values, bad date
  }
  else {
     return true;
  }
}

function isEmpty(val) {
   if (onlySpaceRegexp.test(val) || val == "") {
      return true;
   } else {
      return false;
   }
}
