var defaultEmptyOK = false

var daysInMonth = makeArray(12);
daysInMonth[1] = 31;
daysInMonth[2] = 29;   // must programmatically check this
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;

function makeArray(n) {
   for (var i = 1; i <= n; i++) {
      this[i] = null;
   }
   return (this);
}

function isEmpty(s) {
   return ((s == null) || (s.length == 0))
}

function isDigit (c) {
   return ((c >= "0") && (c <= "9"))
}

function isInteger (s) {
   var i;

   if (isEmpty(s))
      if (isInteger.arguments.length == 1) return defaultEmptyOK;
      else return (isInteger.arguments[1] == true);

   // Search through string's characters one by one
   // until we find a non-numeric character.
   // When we do, return false; if we don't, return true.

   for (i = 0; i < s.length; i++){
      // Check that current character is number.
      var c = s.charAt(i);
      if (!isDigit(c)) return false;
   }

   // All characters are numbers.
   return true;
}

function isSignedInteger (s) {
   if (isEmpty(s))
      if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
      else return (isSignedInteger.arguments[1] == true);

   else {
       var startPos = 0;
       var secondArg = defaultEmptyOK;

       if (isSignedInteger.arguments.length > 1)
          secondArg = isSignedInteger.arguments[1];

       // skip leading + or -
       if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
          startPos = 1;
       return (isInteger(s.substring(startPos, s.length), secondArg))
   }
}

function isNonnegativeInteger (s) {
   var secondArg = defaultEmptyOK;

   if (isNonnegativeInteger.arguments.length > 1)
      secondArg = isNonnegativeInteger.arguments[1];

   // The next line is a bit byzantine.  What it means is:
   // a) s must be a signed integer, AND
   // b) one of the following must be true:
   //    i)  s is empty and we are supposed to return true for
   //        empty strings
   //    ii) this is a number >= 0

   return (isSignedInteger(s, secondArg) && ( (isEmpty(s) && secondArg)  || (parseInt (s,10) >= 0) ) );
}

function isYear (s) {
   if (isEmpty(s))
      if (isYear.arguments.length == 1) return defaultEmptyOK;
      else return (isYear.arguments[1] == true);
   if (!isNonnegativeInteger(s)) return false;
   return ((s.length == 2) || (s.length == 4));
}

function isMonth (s) {
   if (isEmpty(s))
      if (isMonth.arguments.length == 1) return defaultEmptyOK;
      else return (isMonth.arguments[1] == true);
   return isIntegerInRange (s, 1, 12);
}

function isDay (s) {
   if (isEmpty(s))
      if (isDay.arguments.length == 1) return defaultEmptyOK;
      else return (isDay.arguments[1] == true);
   return isIntegerInRange (s, 1, 31);
}

function daysInFebruary (year) {
   // February has 29 days in any year evenly divisible by four,
   // EXCEPT for centurial years which are not also divisible by 400.
   return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}

function isDate (year, month, day) {
   // catch invalid years (not 2- or 4-digit) and invalid months and days.
   if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false))) return false;

   // Explicitly change type to integer to make code work in both
   // JavaScript 1.1 and JavaScript 1.2.
   var intYear = year;
   var intMonth = month;
   var intDay = day;

   if (intYear.indexOf("0") == 0)
      intYear = intYear.charAt(1);

   intYear = parseInt(intYear,10);

   if (intMonth.indexOf("0") == 0)
      intMonth = intMonth.charAt(1);
      intMonth = parseInt(intMonth,10);

   if (intDay.indexOf("0") == 0)
      intDay = intDay.charAt(1);

   intDay = parseInt(intDay,10);

   // catch invalid days, except for February
   if (intDay > daysInMonth[intMonth]) return false;

   if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

   return true;
}

function warnInvalid (theField, s) {
   alert(s)
   theField.focus()
   return false
}

function checkValidDate (dateField, labelString) {
   var dt = dateField.value;
   if (dt.length == 10) {
      var yr = dt.substring(6,10);
      var day = dt.substring(0,2);
      var mth= dt.substring(3,5);
      var iDate=("Invalid " + labelString);
      if (!isYear(yr)) return warnInvalid (dateField, iDate);
      if (!isMonth(mth)) return warnInvalid (dateField, iDate);
      if (!isDay(day)) return warnInvalid (dateField, iDate);
      if (isDate (yr, mth, day)) return true;
      alert (labelString);
      dateField.focus();
      return false;
   }
   else if (dt.length > 0 && dt.length != 10) {
      alert("Invalid Date Format.");
      dateField.focus();
      return false;
   }
   else	{
      return true;
   }
}

function formatDate(i, delKey,direction) {
   if (i.value.length < 10) {
      if (delKey!=9) { //tab
         if (delKey!=8 && delKey!=46 && delKey!=16 &&  !(delKey>36 && delKey<41)) {
            //if the delete, backspace, shift, are not the keys that caused the keyup event.
            var fieldLen = i.value.length

            if ((delKey >= 48 && delKey <= 57) || (delKey >= 96 && delKey <=105)) {
               if (fieldLen == 2 || fieldLen == 5) {
                  i.value = i.value + "/";
               }
            }
            else {
               if (direction == "up") {
                  if (i.value.length == 0) {
                     i.value = ""
                  }
                  else {
                     i.value = i.value.substring(0,i.value.length-1)
                  }
               }
            }
  	    i.focus()
         }
      }
      else {
         if (direction == "down") {
           checkValidDate(i)
         }
      }
   }
}

function isIntegerInRange (s, a, b){
  if (isEmpty(s)) 
    if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
    else return (isIntegerInRange.arguments[1] == true);
  if (!isInteger(s, false)) return false;
  var num = parseInt (s,10);
  return ((num >= a) && (num <= b));
}


