/**
* This script validates Dates and puts them in the format of mm/dd/yyyy
* it only does this when leaving the field, because inline typing/editing 
* causes too much errors
* To use
* 1) Import this javascript file:  <script src="DateFormat.js"></script>
* 2) Your input form field should look like this:
*  <input type=text name="SomeDate" maxlength="10" onBlur="formatDateAfterExit(this);">
*
*  - written by Karen Karp 3/27/2003
*/

/**
* check for valid characters.  Only accept 1234567890/
*/
function validateDateCharacters(strValue) {
	// change and - to /
	strValue = strValue.replace('-', '/');
	var alphaCheck = "1234567890/";  // list of valid date characters
	// check each position
	var i = 0;
	while (i<strValue.length) {
		var c = strValue.charAt(i);
		if (alphaCheck.indexOf(c) == -1) {
			strValue = strValue.replace(c, '');
		}
		else // move on to next character
			i++;
	}
	return strValue;
}

/**
* this removes invalid characters, then formats the date
* Then it calls validate date to validate the formated date
*/
function formatDateAfterExit(o) {
	if (!o) return;

	var theDate = validateDateCharacters(o.value);
	
	// now see what the 'month' is, do we need a 0 in front?
	var iMonth = theDate.substring(0,1);
	if (iMonth > 1) // 2-9, so insert a 0
		theDate = '0' + theDate;
	else if (iMonth == 1 && theDate.indexOf('/') == 1)
		theDate = '0' + theDate;
		
		
	// don't know the best way to 'patternize' a string, so just looping through
	var i = 0;
	while (i<theDate.length) {
	 	var theEnd = theDate.length;	

		// see if there are already // in there, and if Date is only 1 char
		var iPos1 = theDate.indexOf('/');
		var iPos2 = theDate.lastIndexOf('/');
		if (iPos1 == 2 && iPos2 == 4) {
			iDay = theDate.substring(3,4);
			if (iDay < 10)
				theDate = theDate.substring(0,3) + '0' + theDate.substring(3,theEnd);
		}
		theEnd = theDate.length;
		var iPos1 = theDate.indexOf('/');
		var iPos2 = theDate.lastIndexOf('/');
		if (iPos1 == 2 && iPos2 == 2 && theEnd>4) {
			if (theEnd >=5 ) iDay = theDate.substring(3,5);
			else iDay = theDate.substring(3,theEnd);
			if (iDay > 31) {
				theDate = theDate.substring(0,3) + '0' + theDate.substring(3,4)
					+ '/' + theDate.substring(4,theEnd);
			}
		}
		
		var c = theDate.charAt(i);
		theEnd = theDate.length;	
		var bLoopAgain=false;
		

		switch (i) {
			case 2:  // needs to be /
			case 5:  // needs to be /
				 if (c != '/') {
				 	theDate = theDate.substring(0,i) + '/' + theDate.substring(i,theEnd);
					bLoopAgain = true;
				 }
				 break;
			default:  // everywhere else needs to be a number
				if ( c == '/') { // remove it
				 	theDate = theDate.substring(0,i) + theDate.substring((i+1),theEnd);
					bLoopAgain = true;
				}
				break;				 
		}
		if (bLoopAgain) i=0; 
		else i++;
		
	}	
	// now fix the year
	var theEnd = theDate.length;	
	if (theEnd > 6) {
		var iYear = theDate.substring(6,theEnd);
		if (theEnd == 7) iYear = "200" + iYear;
		else if (theEnd == 8) iYear = "20" + iYear;
		else if (theEnd == 9) iYear = "2" + iYear;
		theDate = theDate.substring(0,6) + iYear;
	}
	
	o.value = theDate;

	//return true;
	return validateDate(o);
}

function validateDate(o) {
	var theDate = validateDateCharacters(o.value);
	if (theDate.length == 0) {return;}
	
	var iYear;
	if (theDate.length != 10 ) {
		alert("Invalid Date\nPlease Re-Enter in 'MM/DD/YYYY' format");
		o.value="";  
//		setCursorPosition(o, o.value.length, o.value.length);
		o.focus();
		return false;
	}

	var iMonth = theDate.substring(0,2);
	var iDay = theDate.substring(3,5);

	if (theDate.length == 10)
		iYear = theDate.substring(6,10);
	else if (theDate.length == 7) {		
		iYear = "200" + theDate.substring(6,7);
		o.value = iMonth + "/" + iDay + "/" + iYear;
	}
	else if (theDate.length == 8) {
		iYear = "20" + theDate.substring(6,10);
		o.value = iMonth + "/" + iDay + "/" + iYear;
	}
	
	if (iMonth < 1 || iMonth > 12) {
		alert("Invalid Month in Date\nPlease Re-Enter");
//		o.value = '/' + iDay + '/' + iYear;
		o.value="";  
//		setCursorPosition(o,0,0);
		o.focus();
		return false;		
	}
	// days in month 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
	var iMaxDay = 31;
	switch (iMonth) {
		case "04": //Apr
		case "06": //Jun
		case "09": //Sep
		case "11": //Nov
			iMaxDay = 30;
			break;
		case "02": // Feb
			if (checkLeapYear(iYear)) iMaxDay = 29;
			else iMaxDay = 28;
			break;
	}
//alert("day is " + iDay + " Max day for month " + iMonth + " is " + iMaxDay);	
	
	if (iDay < 1 || iDay > iMaxDay) {
		alert("Invalid Day in Date\nPlease Re-Enter");
//		o.value = iMonth + "//" + iYear;
		o.value="";  
//		setCursorPosition(o,3,0);
		o.focus();
		return false;
	}

	
	return true;
}

function setCursorPosition (o, curPosStart, selectLength) {
	if (o.createTextRange) {
		var v = o.value;
		var r = o.createTextRange();
		var curPosEnd = 0 - (v.length - (curPosStart + selectLength))
		r.moveStart('character', curPosStart);
		r.moveEnd('character', curPosEnd);
		r.select();
	}
}


function checkLeapYear(iYear) {
	if (iYear % 100 == 0) {
		if (iYear % 400 == 0) { return true; }
	}
	else if (iYear % 4 == 0) { return true; }
	return false;
}

function LeapYear(intYear) {
	 return checkLeapYear(iYear);
}


