// define number of days for month - except february which is checked
var daysInMonth = new Array(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;
// isYear returns true if string s is a valid 
// Year number.  Must be 4 digits only.
function isYear (s)
{
	if (!isIntegerInRange (s, 1900, 2100)) return false;
    return (s.length == 4);

}
// isIntegerInRange returns true if string s is an integer 
// within the range of integer arguments a and b, inclusive.
function isIntegerInRange (s, a, b)
{
    // Catch non-integer strings to avoid creating a NaN below,
    // which isn't available on JavaScript 1.0 for Windows.
    if (!isInteger(s, false)) return false;
	// check if leading 0 and remove
	if ((s.substring(0,1) == "0") && (s.length > 1))
		s = s.substring(1,s.length);
	
    // Now, explicitly change the type to integer via parseInt
    // for JS 1.1/1.2 compatability
	var num = parseInt (s,10);
    return ((num >= a) && (num <= b));
}
// isMonth returns true if string s is a valid 
// month number between 1 and 12.
//
function isMonth (s)
{
   return isIntegerInRange (s, 1, 12);
}
// isDay returns true if string s is a valid 
// day number between 1 and 31.
// 
function isDay (s)
{
    return isIntegerInRange (s, 1, 31);
}
// daysInFebruary (INTEGER year)
// 
// returns number of days in February of that year.
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 );
}
// isDate checks if string arguments year, month, and day 
// form a valid date.  Error string returned with/out data.
// 
function isDate (year, month, day, fieldLabel, noBlank, showalert)
{
    var errorStr="";
	// check for missing values & exit
    if ((year.length == 0) || (month.length== 0) || (day.length == 0)) {
    	if (noBlank == true) {
        errorStr += fieldLabel + " cannot be blank.\n";
        if (showalert == true)
		alert(errorStr);
	}
	  return errorStr;
    }
    // catch invalid years (not 2- or 4-digit) and invalid months and days.
    if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false))) {
        errorStr += "Please enter " + fieldLabel + " in mm/dd/yyyy format.\nFor example, 08/01/1999.\n";
        if (showalert == true)
		alert(errorStr);
	  return errorStr
    }
    // Explicitly change type to integer to make code work in both
    // JavaScript 1.1 and JavaScript 1.2.
    var intYear = parseInt(year,10);
    var intMonth = parseInt(month,10);
    var intDay = parseInt(day,10);
    // catch invalid days, except for February
    if (intDay > daysInMonth[intMonth]) {
        errorStr += "Please enter " + fieldLabel + " in mm/dd/yyyy format.\nFor example, 08/01/1999.\n";
        if (showalert == true)
		alert(errorStr);
	  return errorStr
    }
    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) {
        errorStr += "Please enter " + fieldLabel + " in mm/dd/yyyy format.\nFor example, 08/01/1999.\n";
        if (showalert == true)
		alert(errorStr);
	  return errorStr
    }
    return errorStr;
}
function errDate(thefield, fieldLabel, noBlank, showalert) {
	var errorStr="";
	var isplit=0;
	var sMonth="";
	var sDay="";
	var sYear="";
			
	if (thefield.value.length == 0) {
	if (noBlank == true) {
        errorStr += "Please enter " + fieldLabel + " in mm/dd/yyyy format.\nFor example, 08/01/1999.\n";
        if (showalert == true)
		alert(errorStr);
      }
	  return errorStr;
	}
	//Returns true if value is a date in the mm/dd/yyyy format
	isplit = thefield.value.indexOf('/');

	// kent 9/26/00 added the 2nd 'OR' clause below to catch dates added with '/' as the first character
	if (isplit == -1 || isplit == thefield.value.length || thefield.value.substring(0,1) == "/"){
        errorStr += "Please enter " + fieldLabel + " in mm/dd/yyyy format.\nFor example, 08/01/1999.\n";
        if (showalert == true)
		alert(errorStr);
        return errorStr;
	}

	sMonth = thefield.value.substring(0, isplit);
	isplit = thefield.value.indexOf('/', isplit + 1);

	if (isplit == -1 || (isplit + 1 ) == thefield.value.length){
        errorStr += "Please enter " + fieldLabel + " in mm/dd/yyyy format.\nFor example, 08/01/1999.\n";
        if (showalert == true)
		alert(errorStr);
        return errorStr;
	}
	sDay = thefield.value.substring((sMonth.length + 1), isplit);
	sYear = thefield.value.substring(isplit + 1);
	// added by kent 9/12/2000 if a date is added in the following formats current errdate will not pick it up:
	//  0101//2000 OR //01012000
	//	001/1/2000 OR 1/001/2000
	//	0101//2000 OR /0101/2000
	if (thefield.value.indexOf('//')>-1){
        errorStr += "Please enter " + fieldLabel + " in mm/dd/yyyy format.\nFor example, 08/01/1999.\n";
        if (showalert == true)
		alert(errorStr);
        return errorStr;
	}
	if (sMonth.length ==3 || sMonth.length ==4 || sDay.length ==3 || sDay.length ==4){
        errorStr += "Please enter " + fieldLabel + " in mm/dd/yyyy format.\nFor example, 08/01/1999.\n";
        if (showalert == true)
		alert(errorStr);
        return errorStr;
	}
	// end add by kent	
	return isDate (sYear, sMonth, sDay, fieldLabel, noBlank, showalert);
    }
// Compares two dates in format 01/01/1999 and returns -1 if first one less, 0 if same, 
// and 1 if second greater.  Assumes that dates have already been validated
// if firstDate < secondDate function returns the number of years as negative
function CompareDates(firstDate, secondDate)
{
	var isplit=0;
	var s1Month="";
	var s1Day="";
	var s1Year="";
	var s2Month="";
	var s2Day="";
	var s2Year="";

	isplit = firstDate.value.indexOf('/');
	s1Month = firstDate.value.substring(0, isplit);
	isplit = firstDate.value.indexOf('/', isplit + 1);
	s1Day = firstDate.value.substring((s1Month.length + 1), isplit);
	s1Year = firstDate.value.substring(isplit + 1);
	
	isplit = secondDate.value.indexOf('/');
	s2Month = secondDate.value.substring(0, isplit);
	isplit = secondDate.value.indexOf('/', isplit + 1);
	s2Day = secondDate.value.substring((s2Month.length + 1), isplit);
	s2Year = secondDate.value.substring(isplit + 1);
	
	date1 = new Date(s1Year,s1Month-1,s1Day);
	date2 = new Date(s2Year,s2Month-1,s2Day);
	
	if (date1 < date2) 
		return 	-1*Math.round(.51+(date2-date1)/31536000000);

	if (date1 > date2)
		return 1;
	else
		return 0;
}
// Compares two dates in format 01/01/1999 and returns -1 if first one less, 0 if same, 
// and 1 if second greater.  Returns -999 if firstDate has error, +999 if second has error
// if firstDate < secondDate function returns the number of years as negative
function errCompareDates(firstDate, secondDate)
{
	if (firstDate.value.length > 0) {
		isplit = firstDate.value.indexOf('/');
		if (isplit == -1 || isplit == firstDate.value.length)
			return -999;
		s1Month = firstDate.value.substring(0, isplit);
		isplit = firstDate.value.indexOf('/', isplit + 1);
		if (isplit == -1 || (isplit + 1 ) == firstDate.value.length)
			return -999;
		s1Day = firstDate.value.substring((s1Month.length + 1), isplit);
		s1Year = firstDate.value.substring(isplit + 1);
	    if (! (isYear(s1Year, false) && isMonth(s1Month, false) && isDay(s1Day, false)))
			return -999;
	    var intYear = parseInt(s1Year,10);
    	var intMonth = parseInt(s1Month,10);
    	var intDay = parseInt(s1Day,10);
  		if (intDay > daysInMonth[intMonth])
			return -999;
		if ((intMonth == 2) && (intDay > daysInFebruary(intYear)))
			return -999;
		date1 = new Date(s1Year,s1Month-1,s1Day);
	}	
	if (secondDate.value.length > 0) {
		isplit = secondDate.value.indexOf('/');
		if (isplit == -1 || isplit == secondDate.value.length)
			return 999;
		s2Month = secondDate.value.substring(0, isplit);
		isplit = secondDate.value.indexOf('/', isplit + 1);
		if (isplit == -1 || isplit == secondDate.value.length)
			return 999;
		s2Day = secondDate.value.substring((s2Month.length + 1), isplit);
		s2Year = secondDate.value.substring(isplit + 1);
	    if (! (isYear(s2Year, false) && isMonth(s2Month, false) && isDay(s2Day, false)))
			return 999;
	    var intYear = parseInt(s2Year,10);
    	var intMonth = parseInt(s2Month,10);
    	var intDay = parseInt(s2Day,10);
  		if (intDay > daysInMonth[intMonth])
			return 999;
		if ((intMonth == 2) && (intDay > daysInFebruary(intYear)))
			return 999;
		date2 = new Date(s2Year,s2Month-1,s2Day);
	}
	if ((firstDate.value.length > 2) && (secondDate.value.length > 2)) {
		if (date1 < date2) 
			return 	-1*Math.round(.51+(date2-date1)/31536000000);
		if (date1 > date2)
			return 1;
	}
	return 0;
}
