
// This Script verifies user-input dates
// Joseph P. Russell
// Progressive Systems Technology Inc.
// Changed by Tom Healy, Jump Technolgies 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 dateVerification(dateField,messageText){
	
	addLeadingDigits(dateField);
	
	var dateString = dateField.value;
	
   if (verifyDate(dateString) == 1) {
		alert("There is a problem with the MONTH portion of your " + messageText + " date.  Please correct to continue. (Format = MM/dd/yyyy)");
		return 0;
	}
	else if (verifyDate(dateString) == 2) {
		alert("There is a problem with the DAY portion of your " + messageText + " date.  Please correct to continue. (Format = mm/DD/yyyy)");
		return 0;
	}
	else if (verifyDate(dateString) == 3) {
		alert("There is a problem with the YEAR portion of your " + messageText + " date.  Please correct to continue. (Format = mm/dd/YYYY)");
		return 0;
	}
	else if (verifyDate(dateString) == 4) {
		alert("There is a problem with your " + messageText + " date.  Please correct to continue. (Format = MM/DD/YYYY)");
		return 0;
	}
	return 1;
}

function addLeadingDigits(dateField){
	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
   var fromDateString = dateField.value;
   
	for (slash1; slash1 < fromDateString.length && numSlashes == 0; slash1++) {
      if (fromDateString.charAt(slash1) == '/' || fromDateString.charAt(slash1) == '-') {
	     numSlashes++;
	   }
   }

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

   if (numSlashes == 2) {
	  
	  	theMonth = fromDateString.substring(0, slash1 - 1);
	  	theDay = fromDateString.substring(slash1, slash2 - 1);
	  	theYear = fromDateString.substring(slash2, fromDateString.length);
		if (theDay.length == 1) {
			theDay = "0" + theDay;	
		}
		if (theMonth.length == 1) {
			theMonth = "0" + theMonth;	
		}
		if (theYear.length == 2) {
			theYear = "20" + theYear;
		}
		dateField.value = theMonth + "/" + theDay + "/" + theYear;
   }
	
}

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

   theMonth = enteredMonth;
   theDay = enteredDay;
   theYear = 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) {
	  
	  theMonth = eval(dateString.substring(0, slash1 - 1));
	  theDay = 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 >= 1951 && theYear <= 2050) 
      retVal = 1;
   else 
	  retVal = 0;

   return retVal;
}

//end JavaScript -->

