﻿// JScript File

var isNav4 = false, isNav5 = false, isIE4 = true;
var strSeperator = "/"; 
// If you are using any Java validation on the back side you will want to use the / because 
// Java date validations do not recognize the dash as a valid date separator.
var vDateType = 3; // Global value for type of date format
//                1 = mm/dd/yyyy
//                2 = yyyy/dd/mm  (Unable to do date check at this time)
//                3 = dd/mm/yyyy
var vYearType = 4; //Set to 2 or 4 for number of digits in the year for Netscape
var vYearLength = 4; // Set to 4 if you want to force the user to enter 4 digits for the year before validating.
var err = 0; // Set the error code to a default of zero

function DateFormat(vDateName, vDateValue, e, dateCheck, dateType) {
	vDateType = dateType;
	if (e.keyCode == 3 || e.keyCode == 13)
		return;
	// vDateName = object name
	// vDateValue = value in the field being checked
	// e = event
	// dateCheck 
	// True  = Verify that the vDateValue is a valid date
	// False = Format values being entered into vDateValue only
	// vDateType
	// 1 = mm/dd/yyyy
	// 2 = yyyy/mm/dd
	// 3 = dd/mm/yyyy
	//Enter a tilde sign for the first number and you can check the variable information.
	var whichCode = (window.Event) ? e.which : e.keyCode;
	// Check to see if a seperator is already present.
	// bypass the date if a seperator is present and the length greater than 8
	//Eliminate all the ASCII codes that are not valid
	if (e.keyCode == 8)
	    return;
	var re = "0123456789/";		
	var retval = 0;
	for (var i = 0; i < vDateValue.length; i++) {
		var fval = vDateValue.substr (i,1);
		if (! (re.indexOf(fval) >= 0)) {
			if (i == 0) {
				vDateName.value = "";
				vDateName.focus ();
				vDateName.select ();
				return;
			}
			else {
				vDateName.value = retval;
				vDateName.focus ();
				return;
			}
		} else { 
		    if (i == 0)
				retval = fval;
			else
				retval += fval;
		}
	}

	if (! dateCheck) {
		if (vDateValue.length == 2) 
			vDateName.value = vDateValue+strSeperator;
		if (vDateValue.length == 5) 
			vDateName.value = vDateValue+strSeperator;
	}
	if (dateCheck) {
	   if (vDateValue.length == 0) {
	       vDateName.value = "00/00/0000";
	       vDateName.focus ();
	       vDateName.select ();
	       return;
	   }
	   if (vDateValue.length != 10) {
		   alert ("Invalid Date: mm/dd/yyyy required");
		   vDateName.focus ();
		   vDateName.select ();
		   return;
	   }
	   if (!dateValid(vDateValue)) {
		   alert ("Invalid Date");
		   vDateName.focus ();
		   vDateName.select ();
	   }
	}
}
function dateValid(objName) {
	var test_obj = "00/00/0000";
// zero dates ok for some fields
	if (objName == test_obj)
		return true;
	var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	var booFound = false;
	var datefield = objName;
	var strSeparatorArray = new Array("-"," ","/",".");
	var intElementNr;
	// var err = 0;

	//strDate = datefield.value;
	strDate = objName;
	if (strDate.length < 1) {
		return true;
	}

	if (strDate.indexOf(strSeparatorArray[2]) != -1) {
		strDateArray = strDate.split(strSeparatorArray[2]);
		var len = strDateArray.length;
		if (strDateArray.length != 3) 
			return false;
		else {
			strDay = strDateArray[0];
			strMonth = strDateArray[1];
			strYear = strDateArray[2];
		}
	}

//Adjustment for short years entered

	strTemp = strDay;
	strDay = strMonth;
	strMonth = strTemp;
	intday = parseInt(strDay, 10);

	intMonth = parseInt(strMonth, 10);

	intYear = parseInt(strYear, 10);

	if (intMonth>12 || intMonth<1) 
		return false;

	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) 
		return false;

	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) 
		return false;

	if (intMonth == 2) {
		if (intday < 1) {
			err = 8;
			return false;
		}
		if (LeapYear(intYear) == true) {
			if (intday > 29) {
				err = 9;
				return false;
		   }
		}
		else {
			if (intday > 28) {
				err = 10;
				return false;
			}
		}
	}
	
	return true;
}
function LeapYear(intYear) {
if (intYear % 100 == 0) {
if (intYear % 400 == 0) { return true; }
}
else {
if ((intYear % 4) == 0) { return true; }
}
return false;
}
