document.write('<script type="text/javascript" language="JavaScript" src="checkDate.js"></script>');

// A utility function that returns true if a string contains only 
// whitespace characters.
function isblank(s) {
    for(var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    }
    return true;
}

// This is the function that performs form verification. It is invoked
// from the onsubmit event handler. The handler should return whatever
// value this function returns.
function verify(f) {
    var msg;
    var empty_fields = "";
    var errors = "";

    // Loop through the elements of the form, looking for all 
    // text and textarea elements that don't have an "optional" property
    // defined. Then, check for fields that are empty and make a list of them.
    // Also, if any of these elements have a "min" or a "max" property defined,
    // verify that they are numbers and in the right range.
    // If the element has a "numeric" property defined, verify that
    // it is a number, but don't check its range.
    // Put together error messages for fields that are wrong.
    for(var i = 0; i < f.length; i++) {
        var e = f.elements[i];
        
        // only consider those element that have a datatype attribute
        if (e.datatype == null) {
        
          // trying to figure out how to deal with radio buttons
          /*
          if (e.type == 'radio') {
            r = eval('document.' + f.name + '.' + e.name);
            if (r.datatype == null) {
              continue;
            }
            o = eval('document.' + f.name + '.' + e.name + '[0]');
            for(var j = 0; j < r.length; j++) {
              s = eval('document.' + f.name + '.' + e.name + '[' + j + ']');
              alert(!r.optional + '  ' + s.checked + '  ' + s.value);
            }
          }
          */
          
          continue;
        }

        //alert(e.name + ': ' + e.datatype + ' = ' + e.value + ' | optional: ' + e.optional);

        // skip the optional elements that have no value
        if (e.optional && !e.value.length) {
          continue;
        }
        
        // verify that element has a value if it is not optional
        if (!e.optional && isblank(e.value)) {
          //alert(e.label + ' field is not optional');
          empty_fields += "\n          " + e.label;
          continue;
        }

        // date validation
        if (e.datatype == 'date') {
          if (e.min != null) {
            a = e.min.split('/');  // split separates string into an array of strings using a delimiter
            var dateMin = new Date(a[2], a[0] - 1, a[1]);
          }
          if (e.max != null) {
            a = e.max.split('/');  // split separates string into an array of strings using a delimiter
            var dateMax = new Date(a[2], a[0] - 1, a[1]);
          }
          a = e.value.split('/');  // split separates string into an array of strings using a delimiter
          var dateTmp = new Date(a[2], a[0] - 1, a[1]);
          
          //alert(e.name + ' Is date: ' + checkDate(e.value));

          if (!checkDate(e.value) || ((e.min != null) && (dateTmp < dateMin)) || ((e.max != null) && (dateTmp > dateMax))) {
            errors += "- The field " + e.label + " must be a date";
            if (e.min != null) 
                errors += " that is greater than " + e.min;
            if (e.max != null && e.min != null) 
                errors += " and less than " + e.max;
            else if (e.max != null)
                errors += " that is less than " + e.max;
            errors += ".\n";
            continue;
          }
        }
        
        // string validation
        if (e.datatype == 'string') {
          if ((e.min != null && (e.value.length < e.min)) || (e.max != null && (e.value.length > e.max))) {
            errors += "- The field " + e.label + " must have";
            if (e.min != null) 
                errors += " at least " + e.min;
            if (e.max != null && e.min != null) 
                errors += " and no more than " + e.max;
            else if (e.max != null)
                errors += " no more than " + e.max;
            errors += " characters.\n";
            continue;
          }
          
          // validate element value against regular expression pattern if present
          if (e.re != null && !e.re.test(e.value)) {
            //alert(e.re.test(e.value));
            errors += "- The field " + e.label + " must match the pattern: " + e.pattern;
          }
          //alert(e.value);
        }

        // number validation
        if (e.datatype == 'number') {
            var v = parseFloat(e.value);
            if (isNaN(v) || 
                ((e.min != null) && (v < e.min)) || 
                ((e.max != null) && (v > e.max))) {
                errors += "- The field " + e.label + " must be a number";
                if (e.min != null) 
                    errors += " that is greater than " + e.min;
                if (e.max != null && e.min != null) 
                    errors += " and less than " + e.max;
                else if (e.max != null)
                    errors += " that is less than " + e.max;
                errors += ".\n";
            }
        }
    }

    // Now, if there were any errors, display the messages, and
    // return false to prevent the form from being submitted. 
    // Otherwise return true.
    if (!empty_fields && !errors) return true;

    msg  = "______________________________________________________\n\n"
    msg += "The form was not submitted because of the following error(s).\n";
    msg += "Please correct these error(s) and re-submit.\n";
    msg += "______________________________________________________\n\n"

    if (empty_fields) {
      msg += "- The following required field(s) are empty:" 
              + empty_fields + "\n";
      if (errors) msg += "\n";
    }
    msg += errors;
    alert(msg);
    return false;
}

function checkDate(date) {
  // use regular expression to test proper date format (i.e. 3/7/1966)
  re = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
  if (!re.test(date)) {
    return false;
  }
  
  var tmpMonth, tmpDay, tmpYear;
  a = date.split('/');  // split separates string into an array of strings using a delimiter
  tmpMonth = a[0];
  tmpDay = a[1];
  tmpYear = a[2];

  /* CHECK THAT MONTH IS BETWEEN 1 AND 12 */
  if ((tmpMonth > 12) || (tmpMonth == 0)) {
  		return false;
  }

  /* CHECK THAT DAY IS NOT 0 */
  if (tmpDay == 0) {
  		return false;
  }

  /* CHECK FOR 31 DAY MONTHS */
  if ((tmpDay > 31) && ((tmpMonth == 1) || (tmpMonth == 3) || (tmpMonth == 5) || (tmpMonth == 7) || (tmpMonth == 8) || (tmpMonth == 10) || (tmpMonth == 12))) {
    return false;
  }
  /* CHECK FOR 30 DAY MONTHS */
  if ((tmpDay > 30) && ((tmpMonth == 4) || (tmpMonth == 6) || (tmpMonth == 9) || (tmpMonth == 11))) {
    return false;
  }

  // CHECK FOR 28 DAY February
  if ((tmpMonth == 2)) {
    // CHECK FOR LEAP YEAR (every 4 years except for every 400th year)
    if ((tmpYear%4 == 0) && (tmpYear%400 != 0)) {
    	if (tmpDay > 29) {
        return false;
    	}
  	} else {
    	if (tmpDay > 28) {
        return false;
    	}
    }
  }

  return true;
}

