<!--

/*
 *	Define date range constants to prevent dates being created that won't 
 *	fit in the smalldatetime database data type.
 */
var loDate = new Date(1900, 0, 1, 0, 0, 0, 0);
var hiDate = new Date(2079, 6, 6, 0, 0, 0, 0);

/*
 *  Validate the supplied Text box's contents for valid date in the format
 *  MM/DD/YYYY, returning either null if an error was shown, or a Date 
 *  object, if the supplied element contained a valid date.
 */
function getValidDate(dateElement) {

	var strDate = dateElement.value;
	if (strDate.length < 1) {
		writeError(dateElement, "You must enter a valid date in the format MM/DD/YYYY into this field");
		return null;
	}
	if (strDate.indexOf("/") == -1) {
		writeError(dateElement, "Date must be in the format MM/DD/YYYY");
		return null;
	}
	var strDateArray = strDate.split("/");
	if (strDateArray.length != 3) {
		writeError(dateElement, "Date must be in the format MM/DD/YYYY");
		return null;
	}
	var strMonth = strDateArray[0];
	var strDay   = strDateArray[1];
	var strYear  = strDateArray[2];
	if (strYear.length < 4) {
		writeError(dateElement, "Please enter a 4-digit year");
		return null;
	}
	var intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth)) {
		writeError(dateElement, "Month entered is not a valid number");
		return null;
	}
	var intDay = parseInt(strDay, 10);
	if (isNaN(intDay)) {
		writeError(dateElement, "Day entered is not a valid number");
		return null;
	}
	var intYear = parseInt(strYear, 10);
	if (isNaN(intYear)) {
		writeError(dateElement, "Year entered is not a valid number");
		return null;
	}
	if (intMonth < 1 || intMonth > 12) {
		writeError(dateElement, "Month entered must be between 1 and 12");
		return null;
	}
	var maxDay = getMaxDays(intMonth, intYear);
	if (intDay < 1 || intDay > maxDay) {
		writeError(dateElement, "Day entered is not valid for Month entered - must be between 1 and " + maxDay);
		return null;
	}
	if (strMonth.length < 2) {
		strMonth = "0" + strMonth;
	}
	if (strDay.length < 2) {
		strDay = "0" + strDay;
	}
	dateElement.value = strMonth + "/" + strDay + "/" + strYear;
	dateObj = new Date(intYear, (intMonth - 1), intDay);
	// Add check to ensure date is in range of smalldatetype data type limitations
	if (dateObj < loDate || dateObj > hiDate) {
		writeError(dateElement, "Date entered must be between 1/1/1900 and 6/6/2079");
		return null;
	}
	return dateObj;
	
}
	
/*
 *  Return the maximum number of days in the supplied month/year
 */
function getMaxDays(intMonth, intYear) {

	if (intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) {
		return 30;
	} else if (intMonth == 2) {
		if (LeapYear(intYear)) {
			return 29;
		} else {
			return 28;
		}
	}
	return 31;
	
}

/*
 *  Return a boolean indicating whether or not the supplied year is a leap year
 */
function LeapYear(intYear) {

	if ((intYear % 100) == 0) {
		if ((intYear % 400) == 0) { 
			return true; 
		}
	} else {
		if ((intYear % 4) == 0) { 
			return true; 
		}
	}
	return false;
	
}

/*
 *  Show the supplied message in an alert box, and focus on and select the text
 *  for the supplied form element.
 */	
function writeError(dateElement, message) {
	
	alert(message);
	dateElement.focus();
	dateElement.select();
	
}

function getTextToDate(strDate) {

	if (strDate.length < 1) {
		alert("You must enter a valid date in the format MM/DD/YYYY into this field");
		return null;
	}
	if (strDate.indexOf("/") == -1) {
		alert("Date must be in the format MM/DD/YYYY");
		return null;
	}
	var strDateArray = strDate.split("/");
	if (strDateArray.length != 3) {
		alert("Date must be in the format MM/DD/YYYY");
		return null;
	}
	var strMonth = strDateArray[0];
	var strDay   = strDateArray[1];
	var strYear  = strDateArray[2];
	if (strYear.length < 4) {
		alert("Please enter a 4-digit year");
		return null;
	}
	var intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth)) {
		alert("Month entered is not a valid number");
		return null;
	}
	var intDay = parseInt(strDay, 10);
	if (isNaN(intDay)) {
		alert("Day entered is not a valid number");
		return null;
	}
	var intYear = parseInt(strYear, 10);
	if (isNaN(intYear)) {
		alert("Year entered is not a valid number");
		return null;
	}
	if (intMonth < 1 || intMonth > 12) {
		alert("Month entered must be between 1 and 12");
		return null;
	}
	var maxDay = getMaxDays(intMonth, intYear);
	if (intDay < 1 || intDay > maxDay) {
		alert("Day entered is not valid for Month entered - must be between 1 and " + maxDay);
		return null;
	}
	if (strMonth.length < 2) {
		strMonth = "0" + strMonth;
	}
	if (strDay.length < 2) {
		strDay = "0" + strDay;
	}
	strDate.value = strMonth + "/" + strDay + "/" + strYear;
	dateObj = new Date(intYear, (intMonth - 1), intDay);
	// Add check to ensure date is in range of smalldatetype data type limitations
	if (dateObj < loDate || dateObj > hiDate) {
		alert("Date entered must be between 1/1/1900 and 6/6/2079");
		return null;
	}

	return dateObj;
	
}

function isLeapYear(strDate) {
	var strDateArray = strDate.split("/");
	var year  = strDateArray[2];
	year = parseInt(year);
	if(year%4 == 0) {
		if(year%100 != 0) {
			return true;
		} else {
			if(year%400 == 0) {
				return true;
			} else {
				return false;
			}
		}
	}
	return false;
}

function isFebruary(strDate) {
	var strDateArray = strDate.split("/");
	var strMonth = strDateArray[0];
	if(strMonth == '02') {
		return true;
	}
	return false;
}

function getBeginnningDate(beginningDate) {
	var strDateArray = beginningDate.split("/");
	var strDate = strDateArray[1];
	return strDate;  
}

// -->
