<!-- start JavaScript here
// This Script verifies user-input dates
// Joseph P. Russell
// Progressive Systems Technology Inc.

var theDate = new Date(); //Date object defined in JavaScript
var theMonth = 0;      // set to 0 to initialize as integer variables
var theDay = 0;
var theYear = 0;
var dateCode = 0;      // set this variable to accept the verifyDate()
                       // return value


// ***** verifyDate()     ******************************************
// This function accepts three numbers that represent a date. month,
// day and year. It verifies whether the date is a valid one. This
// function callse isMonthOK(), isDayOK(), and isYearOK().
// if this function is called with only one parameter (a whole date
// string like 1/27/1998), this function will call seperateDates()
// and will function properly.
// This function's return values are as follows:
//       RETURN 0 - THE DATE IS VALID
//       RETURN 1 - THE MONTH IS NOT VALID
//       RETURN 2 - THE DAY IS NOT VALID
//       RETURN 3 - THE YEAR IS NOT VALID
//     RETURN 4 - THE DATE IS UNREADABLE

function verifyDate(enteredMonth, enteredDay, enteredYear) {
   retVal = 0;

   theMonth = eval(enteredMonth);
   theDay = eval(enteredDay);
   theYear = eval(enteredYear);

   if(enteredDay == null && enteredYear == null)
      retVal = seperateDateString(enteredMonth);
   else if (isMonthOK() == 0)
      retVal = 1;
   else if (isDayOK() == 0)
      retVal = 2;
   else if (isYearOK() == 0)
      retVal = 3;

   return retVal;
}


// ***** seperateDateString()     *******************************************
// This function is called by verifyDate() when it is passed only one
// parameter. It seperates the date mm/dd/yyyy into it's individual date
// components and then re-calls verifyDate() with the three date parameters.

function seperateDateString(dateString) {
   var retVal = 0;
   var slash1 = 0;     //character index of 1st slash
   var slash2 = 0;     //character index of 2nd slash
   var numSlashes = 0; //make sure there are two slashes

   for (slash1; slash1 < dateString.length && numSlashes == 0; slash1++) {
      if (dateString.charAt(slash1) == '/' || dateString.charAt(slash1) == '-')
       numSlashes++;
   }

   for (slash2 = slash1; slash2 < dateString.length && numSlashes == 1; slash2++) {
      if (dateString.charAt(slash2) == '/' || dateString.charAt(slash2) == '-')
       numSlashes++;
   }

   if (numSlashes == 2) {

    theDay = eval(dateString.substring(0, slash1 - 1));
    theMonth = eval(dateString.substring(slash1, slash2 - 1));
    theYear = eval(dateString.substring(slash2, dateString.length));

      retVal = verifyDate(theMonth, theDay, theYear);
   }
   else
     retVal = 4;

   return retVal;
}


// ***** isMonthOK()     *******************************************
// This function checks theMonth variable set by verifyDate(). It
// returns 0 if the month is invalid, 1 if the month IS valid.
// This function should only be called by verifyDate()
function isMonthOK() {
   var retVal = 0;

   if(theMonth <= 12 && theMonth != 0)
    retVal = 1;
   else
     retVal = 0;

   return retVal;
}

// ***** isDayOK()     *********************************************
// This function checks theDay variable set by verifyDate(). It
// returns 0 if the day is invalid, 1 if the day IS valid.
// theMonth variable is considered because there are different number
// of days in each month. Leap-year is also considered (divisible by 4).
// This function should only be called by verifyDate()
function isDayOK() {
   var retVal = 0;

   if(theMonth == 1 || theMonth == 3 || theMonth == 5 || theMonth == 7
       || theMonth == 8 || theMonth == 10 || theMonth == 12) {
     if (theDay >= 1 && theDay <= 31)
        retVal = 1;
     else
         retVal = 0;
   }
   else if (theMonth == 2) {
      if (theDay >= 1 && theDay <=28)
       retVal = 1;
    else if (theDay == 29 && (theYear % 4) == 0)
       retVal = 1;  // valid leap-year
      else if (theDay == 29 && (theYear % 4) != 0)
     retVal = 0;
    else
     retVal = 0;
   }
   else {
      if (theDay >= 1 && theDay <= 30)
       retVal = 1;
    else
         retVal = 0;
   }
   return retVal;
}

// ***** isYearOK()     ****************************************************
// This function checks theYear variable set by verifyDate(). It
// returns 0 if the year is invalid, 1 if the year IS valid.
// This function should only be called by verifyDate()
function isYearOK() {
   var retVal = 0;

   if (theYear <= 50 && theYear >= 0)
      theYear += 2000;
   else if (theYear <= 99 && theYear >= 51)
      theYear += 1900;

   if (theYear >= 1951 && theYear <= 2050)
      retVal = 1;
   else
    retVal = 0;

   return retVal;
}

//end JavaScript -->

