//------------------------------------------------------------------------------
//  Author:			Sally Xu
//  Procedure Name:	Date_Format
//  Description:	Formats a date value to a given date format
//  Arguments:		datevalue	- the date value to be formatted
//		
//  Return Value:	A formatted date string
//  Comments:		(none)
//------------------------------------------------------------------------------
function Date_Format(datevalue) {
	var idxM = 0;
	var idxD = 0;
	var idxY = 0;
	var separator = "";
	var month = 0;
	var day = 0;
	var year = 0;
	var sDate = "";

	
	var format = window.userDateFormat;

	if (format == null) {
		format = window.parent.userDateFormat;
		if (format == null)
			format = window.opener.parent.userDateFormat;
	}
	
	//If the datevalue is empty or contains true, return an empty string
	if (datevalue == "" || datevalue == true) {
		return "";
	}

	//Determine the structure of the current date format
	idxM = format.indexOf("M", 0);
	idxD = format.indexOf("D", 0);
	idxY = format.indexOf("Y", 0);

	//Determine the separator value if the format is in MM/DD/YYYY form
	if (idxM < idxD && idxD < idxY) {
		separator = format.substring(idxD - 1, idxD);
	}

	//Determine the separator value if the format is in DD/MM/YYYY form
	if (idxD < idxM && idxM < idxY) {
		separator = format.substring(idxM - 1, idxM);
	}

	//Determine the separator value if the format is in YYYY/MM/DD form
	if (idxY < idxM && idxM < idxD) {
		separator = format.substring(idxM - 1, idxM);
	}

	//Determine the month, day and year values of the date to be formatted
	month = datevalue.getMonth() + 1;
	day = datevalue.getDate();
	year = datevalue.getFullYear();	


	//Pad the month value with a leading zero if necessary
	if (month < 10 && format.substring(idxM, idxM + 2) == "MM") {
		month = "0" + month;
	}

	//Pad the day value with a leading zero if necessary
	if (day < 10 && format.substring(idxD, idxD + 2) == "DD") {
		day = "0" + day;
	}

	//Format and return the given date value
	if (idxM < idxD && idxD < idxY) {
		sDate = month + separator + day + separator + year;
	}
	if (idxD < idxM && idxM < idxY) {
		sDate = day + separator + month + separator + year;
	}
	if (idxY < idxM && idxM < idxD) {
		sDate = year + separator + month + separator + day;
	}

	return sDate;

}


//------------------------------------------------------------------------------
//  Author:			Sally Xu, Brian Tauke
//  Procedure Name:	Date_onInput
//  Description:	Examines a character entered into a date field and determines
//					whether or not to allow the character to be appended to the 
//					existing date value
//  Arguments:		field - the name of the HTML form field containing the date
//  Return Value:	Boolean - True if the entered character is allowed
//							  False if the entered character is invalid
//  Comments:		This function assumes that the form field in question has a
//					populated format attribute
//------------------------------------------------------------------------------
function Date_onKeyPress(field) {
	var inputChar = 0;
	var sInput = "";
	var idxM = 0;
	var idxD = 0;
	var idxY = 0;
	var separator = "";
	var sep1 = 0;
	var sep2 = 0;

	//Identify the character that was entered by the user
	inputChar = window.event.keyCode;

	if(IsMac())//Allow Tab for MAC
	{
		if (inputChar == 9) {
			return true;
		}		
	}
	//Allow the backspace character
	if (inputChar == 8) {
		return true;
	}

	//Read in the field value and format strings
	sInput = field.value;
	var format = window.userDateFormat;
	if (format == null) {
		format = window.parent.userDateFormat;
		if (format == null)
			format = window.opener.parent.userDateFormat;
	}
	
	//Defect: 183453
	// Don't allow more characters than the length of the format with Safari
	if (IsMac()) {
		var selection = field.selectionEnd - field.selectionStart;
		// Check selection to make sure they haven't selected any text that they're attempting to overwrite
		if (sInput.length >= format.length && selection == 0) {
			return false;	
		}	
	}
	
	// Don't allow more characters than the length of the format
	if (!IsMac()) {
		// Check document.selection to make sure they haven't selected any text that they're attempting to overwrite
		if (sInput.length >= format.length && document.selection.type.toLowerCase() != "text") {
			return false;	
		}	
	}
	//Determine the structure of the current date format
	idxM = format.indexOf("M", 0);
	idxD = format.indexOf("D", 0);
	idxY = format.indexOf("Y", 0);

	//Determine the separator value if the format is in MM/DD/YYYY form
	if (idxM < idxD && idxD < idxY) {
		separator = format.substring(idxD - 1, idxD);
	}

	//Determine the separator value if the format is in DD/MM/YYYY form
	if (idxD < idxM && idxM < idxY) {
		separator = format.substring(idxM - 1, idxM);
	}

	//Determine the separator value if the format is in YYYY/MM/DD form
	if (idxY < idxM && idxM < idxD) {
		separator = format.substring(idxM - 1, idxM);
	}

	//Only the separator value or numeric digits are allowed to be input
	if ((String.fromCharCode(inputChar) != separator) && (inputChar < 48 || inputChar > 57)) {
		return false;
	}

	//The separator value is not allowed to be the first character of the date
	if ((String.fromCharCode(inputChar) == separator) && (sInput.length == 0)) {
		return false;
	}

	//Consecutive separator values are not allowed
	if ((String.fromCharCode(inputChar) == separator) && (sInput.lastIndexOf(separator) == sInput.length -1)) {
		return false;
	}

	//No more than two separator values are allowed
	sep1 = sInput.indexOf(separator);
	sep2 = sInput.lastIndexOf(separator);	
	if ((sep1 > 0) &&
		(sep2 > 0) &&
		(sep1 != sep2) &&
		(String.fromCharCode(inputChar) == separator)) {
			return false;
	}

	//The date may not begin with "0/" or "00"
	if (((sInput.length == 1) && (parseInt(sInput) == 0) && (inputChar == 48)) ||
		((sInput.length == 1) && (parseInt(sInput) == 0) && (String.fromCharCode(inputChar) == separator))) {
		return false;
	}

	//Validate the input character if the format is in MM/DD/YYYY form
	if (idxM < idxD && idxD < idxY)	{
		if ((String.fromCharCode(inputChar) != separator) &&
		   (((sInput.length == 1) && (parseInt(sInput) > 1)) ||
			((sInput.length == 1) && (parseInt(sInput) == 1) && (inputChar > 50)) ||
			((sInput.length == 2) && (sInput.indexOf(separator) < 0)) ||
			((sInput.length == 3) && (parseInt(sInput.substring(sInput.length - 1) + String.fromCharCode(inputChar)) > 31)) ||
			((sInput.length == 4) && (parseInt(sInput.substring(sInput.lastIndexOf(separator) + 1) + String.fromCharCode(inputChar)) > 31)) ||
			((sInput.length == 4) && (sInput.lastIndexOf(separator) + 2 < sInput.length)) ||
			((sInput.length == 5) && (sInput.lastIndexOf(separator) + 2 < sInput.length)))) {
				field.value = field.value + separator;
				//Defect: 183453
				if(IsMac())
				{
					field.selectionStart = field.selectionStart + 1;
				}
		}
	}

	//Validate the input character if the format is in DD/MM/YYYY form
	if (idxD < idxM && idxM < idxY)	{
		if ((String.fromCharCode(inputChar) != separator) &&
		   (((sInput.length == 1) && (parseInt(sInput + String.fromCharCode(inputChar)) > 31)) ||
			((sInput.length == 2) && (sInput.indexOf(separator) < 0)) ||
			((sInput.length == 3) && (parseInt(sInput.substring(sInput.lastIndexOf(separator) + 1)) == 1) && (inputChar > 50)) ||
			((sInput.length == 3) && (parseInt(sInput.substring(sInput.lastIndexOf(separator) + 1)) > 1)) ||
			((sInput.length == 4) && (sInput.lastIndexOf(separator) + 2 < sInput.length)) ||
			((sInput.length == 5) && (sInput.lastIndexOf(separator) + 2 < sInput.length)))) {
				field.value = field.value + separator;
				//Defect: 183453
				if(IsMac())
				{
					field.selectionStart = field.selectionStart + 1;
				}
		}
	}

	//Validate the input character if the format is in YYYY/MM/DD form
	if (idxY < idxM && idxM < idxD)	{
		if ((String.fromCharCode(inputChar) != separator) &&
		   (((sInput.length == 4) && (sInput.indexOf(separator) < 0)) ||
			((sInput.indexOf(separator) > 0) && (sInput.lastIndexOf(separator) < 0) && (parseInt(sInput.substring(sInput.indexOf(separator) + 1) + String.fromCharCode(inputChar)) > 12)) ||
			(sInput.length == 7) ||
			((sInput.indexOf(separator) != sInput.lastIndexOf(separator)) && (parseInt(sInput.substring(sInput.lastIndexOf(separator) + 1) + String.fromCharCode(inputChar)) > 31)))) {
				field.value = field.value + separator;
				//Defect: 183453
				if(IsMac())
				{
					field.selectionStart = field.selectionStart + 1;
				}
		}
	}

	//If no errors are found, return true to allow the input character
	return true;

}


//------------------------------------------------------------------------------
//  Author:			Sally Xu
//  Procedure Name:	Date_Pick
//  Description:	Opens a pop-up calendar window for the user so a date can be
//					selected
//  Arguments:		field - the name of the HTML form field that will contain the
//							selected date
//  Return Value:	(none)
//  Comments:		The calendar window will only be opened if the specified form 
//					field has not been disabled, and the date the user selects will
//					automatically be stored in the specified form field.  This 
//					function assumes that the form field in question has a populated 
//					rpath attribute that represents the relative path of the HTML
//					document that contains the form field in question.
//------------------------------------------------------------------------------
function Date_Pick(datefield, buttonfield)	
{	
	var doc = document;
	while(doc.body.id !="pagebody")
	{
		doc = parent.document;
	}
	var dF=doc.getElementById('CalendarFrame');
	
	if(typeof(datefield)!="object")
		datefield = document.getElementById(datefield);
	if(typeof(buttonfield) != "object")
		buttonfield = document.getElementById(buttonfield);
	
	if(datefield) 	
	{
		if (IsMac()) {
			dF.setDateField(datefield);
		}
		else	
			dF.datefield = datefield;
		
	}
	if(dF.loaded && dF.style.visibility =="visible" && dF.button == buttonfield)
	{
		dF.button = null;
		dF.style.visibility="hidden"	
		return;
	}
	if (buttonfield) 
		dF.button = buttonfield;
	ShowFrame(dF, buttonfield, "auto");

	var theFrame = null;
	if (IsMac())
		theFrame = doc.getElementById("CalendarFrame");
	else
		theFrame = doc.frames.CalendarFrame;

	if (!theFrame.goMonth)//page not fully loaded yet
	{
		setTimeout("Date_Pick()", 10);//give it more time to load the page
		return;
	}
	dF.loaded = true;
	theFrame.dateField = dF.datefield;	
	
	if (IsMac()) {
		var field = dF.getDateField();
		if(field.value != "" && Date_Validate(field, true))
			theFrame.setSelectedDate(new Date(Date_Unformat(field.value)));
		else
			theFrame.setSelectedDate(new Date());
	} else {
		if(dF.datefield.value != "" && Date_Validate(dF.datefield, true))
			theFrame.selectedDate = new Date(Date_Unformat(dF.datefield.value));
		else
			theFrame.selectedDate = new Date();						
	}			
	theFrame.goMonth(0);									
}



//------------------------------------------------------------------------------
//  Author:			Curt Knapp
//  Procedure Name:	Date_Pick_Simple
//  Description:	Opens a pop-up calendar window for the user so a date can be
//					selected
//  Arguments:		field - the name of the HTML form field that will contain the
//							selected date
//  Return Value:	(none)
//  Comments:		The calendar window will only be opened if the specified form 
//					field has not been disabled, and the date the user selects will
//					automatically be stored in the specified form field.  This 
//					function assumes that the form field in question has a populated 
//					rpath attribute that represents the relative path of the HTML
//					document that contains the form field in question.
//------------------------------------------------------------------------------
function Date_Pick_Simple(field){ 
 	var URL = ""
 	window.dateField = field;

 	if (field.disabled == false) {
 		var languageID = GetCookie(g_LoginCookieName, "LanguageID");
		if (languageID == "") {
			languageID = 1;
		}	
		URL = RelativePath + 'SimpleCalendar_' + languageID + '.htm'
		openWindow(URL, 'Calendar', 265, 230, 'scrollbars=no', false);
	}
}
			
//------------------------------------------------------------------------------
//  Author:			Sally Xu
//  Procedure Name:	Date_Unformat
//  Description:	Formats an already formatted date into a new date format
//  Arguments:		sInput	 - the date value to be reformatted
//					format	 - the old format that was used
//					unformat - the new format to be used
//  Return Value:	A reformatted date string
//	Comments:		(none)
//------------------------------------------------------------------------------
function Date_Unformat(sInput) {
	var idxM = 0;
	var idxD = 0;
	var idxY = 0;
	var separator = "";
	var month = 0;
	var day = 0;
	var year = 0;
	var newidxM = 0;
	var newidxD = 0;
	var newidxY = 0;
	var newseparator = "";
	var sDate = "";
	
	var format = window.userDateFormat;
	if (format == null) {
		format = window.parent.userDateFormat;
		if (format == null)
			format = window.opener.parent.userDateFormat;
	}
	
	var unformat = window.serverDateFormat;
	if (unformat == null) {
		unformat = window.parent.serverDateFormat;
		if (unformat == null)
			unformat = window.opener.parent.serverDateFormat;
	}
	
	//If the date value is empty, return an empty string
	if (sInput == "") {
		return "";
	}

	//Determine the structure of the previous date format
	idxM = format.indexOf("M", 0);
	idxD = format.indexOf("D", 0);
	idxY = format.indexOf("Y", 0);

	//Determine the separator, month, day and year values if the input is in MM/DD/YYYY format
	if (idxM < idxD && idxD < idxY) {
		separator = format.substring(idxD - 1, idxD);
		month = sInput.substring(0, sInput.indexOf(separator));
		if (month.substring(0,1) == '0') {
			month = parseInt(month.substring(1, sInput.indexOf(separator)));
		}
		else {
			month = parseInt(month);
		}
		day = sInput.substring(sInput.indexOf(separator) + 1, sInput.lastIndexOf(separator));
		if (day.substring(0,1) == '0') {
			day = day.substring(1, sInput.lastIndexOf(separator));
		}
		else {
			day = parseInt(day)
		}
		year = parseInt(sInput.substring(sInput.lastIndexOf(separator) + 1));
	}

	//Determine the separator, day, month and year values if the input is in DD/MM/YYYY format
	if (idxD < idxM && idxM < idxY) {
		separator = format.substring(idxM - 1, idxM);
		day = sInput.substring(0, sInput.indexOf(separator));
		if (day.substring(0,1) == '0') {
			day = parseInt(day.substring(1, sInput.indexOf(separator)));
		}
		else {
			day = parseInt(day);
		}
		month = sInput.substring(sInput.indexOf(separator) + 1, sInput.lastIndexOf(separator));
		if (month.substring(0,1) == '0') {
			month = parseInt(month.substring(1, sInput.lastIndexOf(separator)));
		}
		else {
			month = parseInt(month);
		}
		year = parseInt(sInput.substring(sInput.lastIndexOf(separator) + 1));
	}

	//Determine the separator, year, month and day values if the input is in YYYY/MM/DD format
	if (idxY < idxM && idxM < idxD) {
		separator = format.substring(idxM - 1, idxM);
		year = parseInt(sInput.substring(0, sInput.indexOf(separator)));
		month = sInput.substring(sInput.indexOf(separator) + 1, sInput.lastIndexOf(separator));
		if (month.substring(0,1) == '0') {
			month = parseInt(month.substring(1, sInput.lastIndexOf(separator)));
		}
		else {
			month = parseInt(month);
		}
		day = sInput.substring(sInput.lastIndexOf(separator) + 1);
		if (day.substring(0,1) == '0') {
			day = parseInt(day.substring(1, sInput.lastIndexOf(separator) + 1));
		}
		else {
			day = parseInt(day);
		}
	}

	//Determine the structure of the new date format
	newidxM = unformat.indexOf("M", 0);
	newidxD = unformat.indexOf("D", 0);
	newidxY = unformat.indexOf("Y", 0);

	//Determine the separator value of the new format
	if (newidxM < newidxD && newidxD < newidxY) {
		newseparator = unformat.substring(newidxD - 1, newidxD);
	}
	if (newidxD < newidxM && newidxM < newidxY) {
		newseparator = unformat.substring(newidxM - 1, newidxM);
	}
	if (newidxY < newidxM && newidxM < newidxD) {
		newseparator = unformat.substring(newidxM - 1, newidxM);
	}

	//Reformat and return the given date value
	if (newidxM < newidxD && newidxD < newidxY) {
		sDate = month + newseparator + day + newseparator + year;
	}
	if (newidxD < newidxM && newidxM < newidxY) {
		sDate = day + newseparator + month + newseparator + year;
	}
	if (newidxY < newidxM && newidxM < newidxD) {
		sDate = year + newseparator + month + newseparator + day;
	}

	return sDate;

}
//------------------------------------------------------------------------------
//  Author:			Sally Xu
//  Procedure Name:	Date_onChange
//  Description:	Checks the validity of a date value string, and if the date is
//					valid it is formatted to a given date format
//  Arguments:		field - the name of the HTML form field containing the date
//  Return Value:	The form field specified will be updated with the formatted date
//  Comments:		This function assumes that the form field in question has a
//					populated format attribute
//------------------------------------------------------------------------------
//DO NOT DELETE USED FOR INBOUND FORM FIELDS
function Date_onChange(field) {	

	//If valid, format the date according to the user's preferred format
	if (Date_Validate(field) != false) {
		field.value = Date_Format(Date_Validate(field), field.format);
	}

}

//------------------------------------------------------------------------------
//  Author:			Sally Xu, Brian Tauke
//  Procedure Name:	Date_Validate
//  Description:	Verifies that a given HTML form field contains a valid date
//					string
//  Arguments:		field - the name of the HTML form field containing the date
//  Return Value:	True if the field contains an empty string
//					False if the field contains an invalid date
//					A date variable if the field contains a valid date
//  Comments:		This function assumes that the form field in question has a
//					populated format attribute
//------------------------------------------------------------------------------
//DO NOT DELETE USED FOR INBOUND FORM FIELDS
function Date_Validate(field, hideMsg) {
	var sInput = "";
	var sDisplay = "";
	var idxM = 0;
	var idxD = 0;
	var idxY = 0;
	var separator = "";
	var month = "0";
	var day = "0";
	var year = "0";
	var thischar = "";
	
	if (!field)
		return true;
	if (!hideMsg)
		hideMsg = false;
		
	//Read in the field value and format strings
	sInput = field.value;
	var format = window.userDateFormat;
	if (format == null) {
		format = window.parent.userDateFormat;
		if (format == null)
			format = window.opener.parent.userDateFormat;
	}
	

	//TODO Do we need this
	if (field.display) {
		sDisplay = field.display;
	} else {
		sDisplay = format;	
	}	

	//If the field is empty, return true
	if (sInput.length == 0) {
		return true;
	}

	//Determine the structure of the current date format
	idxM = format.indexOf("M", 0);
	idxD = format.indexOf("D", 0);
	idxY = format.indexOf("Y", 0);

	//Determine the separator, month, day and year values if the input is in MM/DD/YYYY format
	if (idxM < idxD && idxD < idxY) {
		separator = format.substring(idxD - 1, idxD);
		month = sInput.substring(0, sInput.indexOf(separator));
		if (month.substring(0,1) == '0') {
			month = parseInt(month.substring(1, sInput.indexOf(separator)));
		}
		else {
			month = parseInt(month);
		}
		day = sInput.substring(sInput.indexOf(separator) + 1, sInput.lastIndexOf(separator));
		if (day.substring(0,1) == '0') {
			day = day.substring(1, sInput.lastIndexOf(separator));
		}
		else {
			day = parseInt(day)
		}
		year = parseInt(sInput.substring(sInput.lastIndexOf(separator) + 1));
	}

	//Determine the separator, day, month and year values if the input is in DD/MM/YYYY format
	if (idxD < idxM && idxM < idxY) {
		separator = format.substring(idxM - 1, idxM);
		day = sInput.substring(0, sInput.indexOf(separator));
		if (day.substring(0,1) == '0') {
			day = parseInt(day.substring(1, sInput.indexOf(separator)));
		}
		else {
			day = parseInt(day);
		}
		month = sInput.substring(sInput.indexOf(separator) + 1, sInput.lastIndexOf(separator));
		if (month.substring(0,1) == '0') {
			month = parseInt(month.substring(1, sInput.lastIndexOf(separator)));
		}
		else {
			month = parseInt(month);
		}
		year = parseInt(sInput.substring(sInput.lastIndexOf(separator) + 1));
	}

	//Determine the separator, year, month and day values if the input is in YYYY/MM/DD format
	if (idxY < idxM && idxM < idxD) {
		separator = format.substring(idxM - 1, idxM);
		year = parseInt(sInput.substring(0, sInput.indexOf(separator)));
		month = sInput.substring(sInput.indexOf(separator) + 1, sInput.lastIndexOf(separator));
		if (month.substring(0,1) == '0') {
			month = parseInt(month.substring(1, sInput.lastIndexOf(separator)));
		}
		else {
			month = parseInt(month);
		}
		day = sInput.substring(sInput.lastIndexOf(separator) + 1);
		if (day.substring(0,1) == '0') {
			day = parseInt(day.substring(1, sInput.lastIndexOf(separator) + 1));
		}
		else {
			day = parseInt(day);
		}
	}

	//Verify that only numerics and separator values are used in the date
	for (x=0; x < sInput.length; x++) {
		thischar = sInput.substring(x, x+1);
		if ((isNaN(thischar)) && (thischar != separator)) {
			if (!hideMsg)
				popMessage(ResourceID504 + " " + sDisplay, field);
			return false;
		}
	}

	//If the month, day or year values are not numeric, display an error
	if (isNaN(month) || isNaN(day) || isNaN(year)) {
		if (!hideMsg) {
			popMessage(ResourceID504 + " " + sDisplay, field);
		}
		return false;
	}

	//If the month or day values are too small or too large, display an error
	if ((month == 0) || 
		(month > 12) || 
		(day == 0) || 
		(day > getDaysInMonth(month, year))) {
			if (!hideMsg)
				popMessage(ResourceID504 + " " + sDisplay, field);
			return false;
	}

	//If the year value is not 4 digits long, display an error
	if (String(year).length != 4) {
		if (!hideMsg)
			popMessage(ResourceID505, field);
		return false;
	}
	
	/*
	//If the year value is too small, display an error
	if (year < 1900) {
		if (!hideMsg)
			popMessage(ResourceID507, field);
		return false;
	}
	*/
	
	//If the year value is too large, display an error
	if (year > 3000) {
		if (!hideMsg)
			popMessage(ResourceID506, field);
		return false;
	}

	//If no errors are found, return the valid date as a JavaScript date variable
	return new Date(year, month - 1, day);

}


//------------------------------------------------------------------------------
//  Author:			Sally Xu, Brian Tauke
//  Procedure Name:	getDaysInMonth
//  Description:	Determines the number of days in a given month in a given year
//  Arguments:		month - the month to be used in the calculation
//					year  - the year to be used in the calculation
//  Return Value:	Number of days in the given month in the given year
//  Comments:		(none)
//------------------------------------------------------------------------------
function getDaysInMonth(month, year) {
	var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

	//If the month is February, return a value taking leap year into account
	if (month == 2) {
		if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
			return 29;
		}
		else {
			return 28;
		}
	}
	//Otherwise, return the appropriate array value
	else {
		return daysInMonth[month - 1];
	}

}

function validateDaysInMonth(dateValue)
{
	var sInput = "";
	var sDisplay = "";
	var idxM = 0;
	var idxD = 0;
	var idxY = 0;
	var separator = "";
	var month = "0";
	var day = "0";
	var year = "0";
	var thischar = "";

	
	//Read in the field value and format strings
	sInput = dateValue;
	var format = window.userDateFormat;
	if (format == null) {
		format = window.parent.userDateFormat;
		if (format == null)
			format = window.opener.parent.userDateFormat;
	}
	
	sDisplay = format;

	//If the field is empty, return true
	if (sInput.length == 0) {
		return true;
	}

	//Determine the structure of the current date format
	idxM = format.indexOf("M", 0);
	idxD = format.indexOf("D", 0);
	idxY = format.indexOf("Y", 0);

	//Determine the separator, month, day and year values if the input is in MM/DD/YYYY format
	if (idxM < idxD && idxD < idxY) {
		separator = format.substring(idxD - 1, idxD);
		month = sInput.substring(0, sInput.indexOf(separator));
		if (month.substring(0,1) == '0') {
			month = parseInt(month.substring(1, sInput.indexOf(separator)));
		}
		else {
			month = parseInt(month);
		}
		day = sInput.substring(sInput.indexOf(separator) + 1, sInput.lastIndexOf(separator));
		if (day.substring(0,1) == '0') {
			day = day.substring(1, sInput.lastIndexOf(separator));
		}
		else {
			day = parseInt(day)
		}
		year = parseInt(sInput.substring(sInput.lastIndexOf(separator) + 1));
	}

	//Determine the separator, day, month and year values if the input is in DD/MM/YYYY format
	if (idxD < idxM && idxM < idxY) {
		separator = format.substring(idxM - 1, idxM);
		day = sInput.substring(0, sInput.indexOf(separator));
		if (day.substring(0,1) == '0') {
			day = parseInt(day.substring(1, sInput.indexOf(separator)));
		}
		else {
			day = parseInt(day);
		}
		month = sInput.substring(sInput.indexOf(separator) + 1, sInput.lastIndexOf(separator));
		if (month.substring(0,1) == '0') {
			month = parseInt(month.substring(1, sInput.lastIndexOf(separator)));
		}
		else {
			month = parseInt(month);
		}
		year = parseInt(sInput.substring(sInput.lastIndexOf(separator) + 1));
	}

	//Determine the separator, year, month and day values if the input is in YYYY/MM/DD format
	if (idxY < idxM && idxM < idxD) {
		separator = format.substring(idxM - 1, idxM);
		year = parseInt(sInput.substring(0, sInput.indexOf(separator)));
		month = sInput.substring(sInput.indexOf(separator) + 1, sInput.lastIndexOf(separator));
		if (month.substring(0,1) == '0') {
			month = parseInt(month.substring(1, sInput.lastIndexOf(separator)));
		}
		else {
			month = parseInt(month);
		}
		day = sInput.substring(sInput.lastIndexOf(separator) + 1);
		if (day.substring(0,1) == '0') {
			day = parseInt(day.substring(1, sInput.lastIndexOf(separator) + 1));
		}
		else {
			day = parseInt(day);
		}
	}

	// Only validation left to do, rest is done by regular expression
	if (day > getDaysInMonth(month, year)) {
		return false;
	}

	return true;
}

//------------------------------------------------------------------------------
//  Author:			(original may be)Sally Xu, Brian Tauke, (additionally) Sam Evard
//  Procedure Name:	comparedatetime
//  Description:	compares two dates and times
//  Arguments:		startDateField - the start date field
//					endDateField  - the end date field, null if we are using today
//					startTimeField - the start time field
//					endTimeField - the end date field
//					ignoreSeconds - to validate the time properly
//  Return Value:	true - if valid comparison
//					false - if start DateTime is after end DateTime
//  Comments:		This function comment box was not originally here, so the
//					origonal authors are unkown
//------------------------------------------------------------------------------
function comparedatetime(startDateField, endDateField, startTimeField, endTimeField, ignoreSeconds) {
	var sBeginDate = "";
	var sEndDate = "";
	var sDate = "";
	
	
	var sBeginTime = "";
	var sEndTime = "";

	
	
	if(typeof(startDateField) != "object") //it is an ID
		startDateField = FormField(startDateField);
	
	if(typeof(endDateField) != "object") //it is an ID
		endDateField = FormField(endDateField);
		
	if(typeof(startTimeField) != "object") //it is an ID
		startTimeField = FormField(startTimeField);
		
	if(typeof(endTimeField) != "object") //it is an ID
		endTimeField = FormField(endTimeField);
	
	// Make sure the fields have valid values.  If not, don't do this validation check.
	if (!Date_Validate(startDateField) || !Date_Validate(endDateField) ||
	    !Time_Validate(startTimeField, ignoreSeconds) || !Time_Validate(endTimeField, ignoreSeconds)) {	
	 	return true;   	
	}    	
		
	//Read in the date values
	if (!startDateField)
		sBeginDate = Date_Format(new Date);
	else
		sBeginDate = startDateField.value;
	if (!endDateField)
		sEndDate = Date_Format(new Date);
	else
		sEndDate = endDateField.value;
	
	if (startTimeField != null) {		

		format = window.userTimeFormat;
		format = format.replace(/TT/i,"AMPM");
	
		//Read in the time values and the current time format
		sBeginTime = startTimeField.value;
		if(!endTimeField)
			sEndTime = Time_Format(new Date(), false);
		else
			sEndTime = endTimeField.value;
	}
	return CompareDateTimeValues(sBeginDate,sEndDate,sBeginTime,sEndTime,ignoreSeconds);
}

function CompareDateTimeValues(sBeginDate,sEndDate,sBeginTime,sEndTime,ignoreSeconds){
	
	var month = 0;
	var day = 0;
	var year = 0;
	var dateval;
	var BeginVal = new Date(2000, 1, 1);
	var EndVal = new Date(2000, 1, 1);
	var separator = "";
	var idxM = 0;
	var idxD = 0;
	var idxY = 0;
	// Determine the user's date format
	var format = window.userDateFormat;
	if (format == null) {
		format = window.parent.userDateFormat;
		if (format == null)
			format = window.opener.parent.userDateFormat;
	}
	
	//Assign the date format index variables
	idxM = format.indexOf("M", 0);
	idxD = format.indexOf("D", 0);
	idxY = format.indexOf("Y", 0);
	//Determine the separator string and the month, day and year values for the dates
	for (i=0; i <= 1; i++) {

		//Process the startdate first and the enddate second
		if (i==0) {
			sDate = sBeginDate;
			dateval = BeginVal;
		}
		else {
			sDate = sEndDate;
			dateval = EndVal;
		}

		//Process dates in the MM/DD/YY format
		if (idxM < idxD && idxD < idxY) {
			separator = format.substring(idxD - 1, idxD);
			month = sDate.substring(0, sDate.indexOf(separator));			
			day = sDate.substring(sDate.indexOf(separator) + 1,sDate.lastIndexOf(separator));				
			year = sDate.substring(sDate.lastIndexOf(separator) + 1);
		}

		//Determine the separator, day, month and year values if the input is in DD/MM/YYYY format
		if (idxD < idxM && idxM < idxY) {
			separator = format.substring(idxM - 1, idxM);
			day = sDate.substring(0, sDate.indexOf(separator));				
			month = sDate.substring(sDate.indexOf(separator) + 1,sDate.lastIndexOf(separator));						
			year = parseInt(sDate.substring(sDate.lastIndexOf(separator) + 1));
		}

		//Determine the separator, year, month and day values if the input is in YYYY/MM/DD format
		if (idxY < idxM && idxM < idxD) {
			separator = format.substring(idxM - 1, idxM);
			year = sDate.substring(0, sDate.indexOf(separator));
			month = sDate.substring(sDate.indexOf(separator) + 1, sDate.lastIndexOf(separator));
			day = sDate.substring(sDate.lastIndexOf(separator) + 1);
		}
		
		if (month.substring(0, 1) == '0' && month.length > 1){
			month = month.substring(1, 2);	
		}
								
		if (day.substring(0, 1) == '0' && day.length > 1){		
			day = day.substring(1, 2);
		}
		year = parseInt(year);
		month = parseInt(month);
		day = parseInt(day);
								
		//Set the BeginVal or EndVal dates for later comparison
		dateval.setFullYear(year);
		dateval.setMonth(month-1);
		dateval.setDate(day);
	}
	//If the BeginDate is after the EndDate, display an error and give focus to the enddate field
	if (EndVal < BeginVal) {
		return false;
	}

	//If the startdate and enddate are equal and time values exist, continue validation
	
	
	if (sBeginTime && sBeginTime != "" && (EndVal.valueOf() == BeginVal.valueOf())) {
		var idxH = 0;
		var idxM = 0;
		var idxS = 0;
		var idxT = 0;
		var sTime = "";
		var timeval;
		var hour = 0;
		var minute = 0;
		var second = 0;
		
		format = window.userTimeFormat;
		if (format == null) {
			format = window.parent.userTimeFormat;
			if (format == null)
				format = window.opener.parent.userTimeFormat;
		}
		format = format.replace(/TT/i,"AMPM");
		
		//Assign the time format index variables
		idxH = format.indexOf("H", 0);
		idxM = format.indexOf("M", 0);
		idxS = format.indexOf("S", 0);
	
		//Determine the time format separator and the AM/PM index
		separator = format.substring(idxM - 1, idxM);
		idxT = format.indexOf("AMPM", 0);
			
		//Determine the hour, minute and second values for the times
		for (i=0; i <= 1; i++) {
			//Process the starttime first and the endtime second
			if (i==0) {
				sTime = sBeginTime;
				timeval = BeginVal;
			}
			else {
				sTime = sEndTime;
				timeval = EndVal;
			}
				
			//Find the hour and minute values based on the current time format
			if (sTime.indexOf(separator) > -1) {
				hour = sTime.substring(0, sTime.indexOf(separator));								
				if (sTime.lastIndexOf(separator) != sTime.indexOf(separator)) {
					minute = sTime.substring(sTime.indexOf(separator) + 1, sTime.lastIndexOf(separator));				
				}
				else {
					minute = sTime.substring(sTime.indexOf(separator) + 1);						
				}					
			}
			else {
				hour = sTime;		
				minute = "00";		
			}
			//Find the seconds value if it exists
			if (sTime.indexOf(separator) != sTime.lastIndexOf(separator)) {
				if ((idxT == -1) || (sTime.indexOf(" ") == -1)) {
					second = sTime.substring(sTime.lastIndexOf(separator) + 1);					
				}
				else {
					second = sTime.substring(sTime.lastIndexOf(separator) + 1, sTime.indexOf(" "));					
				}				
			} else {
				second = "00";	
			}	
			if (hour.substring(0,1) == '0' && hour.length > 1){ // if hour is like 04
				hour = hour.substring(1, 2);
			}
			if (minute.substring(0,1) == '0' && minute.length > 1){	// if minute is like 04
				minute = minute.substring(1, 2);
			}
			if (second.substring(0,1) == '0' && second.length > 1){ // if second is like 04
				second = second.substring(1, 2);
			}
			hour = parseInt(hour);
			minute = parseInt(minute);
			second = parseInt(second);

			//Convert the time into a 24 hour value
			if (sTime.indexOf(" PM") > 0 && hour != 12) {
				hour = hour + 12;
			}
			if (sTime.indexOf(" AM") > 0 && hour == 12) {
				hour = hour - 12;
			}

			//Set the BeginVal or EndVal times for later comparison
			timeval.setHours(hour);
			timeval.setMinutes(minute);
			timeval.setSeconds(second);
		}
	}
	//If the BeginTime is after the EndTime, display an error and give focus to the endtime field
	if (EndVal < BeginVal) {
		return false;
	}

	//If no errors were found, return True to indicate the dates are valid
	return true;

}

//********************************************************************************/
//*  Author:		Curt Knapp
//*  Procedure Name:	IsValidDate
//*  Description:	Determines whether a date is valid or not.				
//*  Arguments:		value -  the date to evaluate
//*					format - the format that dates will be in
//*  Return Value:	boolean, indicating if the date was valid or not.
//*  Comments:		
//********************************************************************************/		
function IsValidDate(value) {
	var sInput = "";
	var idxM = 0;
	var idxD = 0;
	var idxY = 0;
	var separator = "";
	var month = 0;
	var day = 0;
	var year = 0;
	var thischar = "";
	//Read in the field value and format strings
	sInput = value;

	//If the field is empty, return true
	if (sInput.length == 0) {
		return false;
	}
	
	var format = window.userDateFormat;
	
	if (format == null) {
		format = window.parent.userDateFormat;
		if (format == null)
			format = window.opener.parent.userDateFormat;
	}


	//Determine the structure of the current date format
	idxM = format.indexOf("M", 0);
	idxD = format.indexOf("D", 0);
	idxY = format.indexOf("Y", 0);

	//Determine the separator, month, day and year values if the input is in MM/DD/YYYY format
	if (idxM < idxD && idxD < idxY) {
		separator = format.substring(idxD - 1, idxD);
		month = sInput.substring(0, sInput.indexOf(separator));
		if (month.substring(0,1) == '0')
			month = parseInt(month.substring(1, month));
		else
			month = parseInt(month);
		day = sInput.substring(sInput.indexOf(separator) + 1, sInput.lastIndexOf(separator));
		if (day.substring(0,1) == '0')
			day = parseInt(day.substring(1, day));
		else
			day = parseInt(day);	
		year = parseInt(sInput.substring(sInput.lastIndexOf(separator) + 1));
	}

	//Determine the separator, day, month and year values if the input is in DD/MM/YYYY format
	if (idxD < idxM && idxM < idxY) {
		separator = format.substring(idxM - 1, idxM);
		day = sInput.substring(0, sInput.indexOf(separator));
		if (day.substring(0,1) == '0')
			day = parseInt(day.substring(1, day));
		else
			day = parseInt(day);					
		month = sInput.substring(sInput.indexOf(separator) + 1, sInput.lastIndexOf(separator));
		if (month.substring(0,1) == '0')
			month = parseInt(month.substring(1, month));
		else
			month = parseInt(month);				
		year = parseInt(sInput.substring(sInput.lastIndexOf(separator) + 1));
	}

	//Determine the separator, year, month and day values if the input is in YYYY/MM/DD format
	if (idxY < idxM && idxM < idxD) {
		separator = format.substring(idxM - 1, idxM);
		year = parseInt(sInput.substring(0, sInput.indexOf(separator)));
		month = sInput.substring(sInput.indexOf(separator) + 1, sInput.lastIndexOf(separator));
		if (month.substring(0,1) == '0')
			month = parseInt(month.substring(1, month));
		else
			month = parseInt(month);
		day = sInput.substring(sInput.lastIndexOf(separator) + 1);
		if (day.substring(0,1) == '0')
			day = parseInt(day.substring(1, day));
		else
			day = parseInt(day);							
	}

	// If the separator is not present, return false
	if (sInput.indexOf(separator) <= 0) {
		return false;
	}
	
	//Verify that only numerics and separator values are used in the date
	for (x=0; x < sInput.length; x++) {
		thischar = sInput.substring(x, x+1);
		if ((isNaN(thischar)) && (thischar != separator)) {
			return false;
		}
	}

	//If the month or day values are too small or too large, display an error
	if ((month == 0) || 
		(month > 12) || 
		(day == 0) || 
		(day > getDaysInMonth(month, year))) {
			return false;
	}

	//If the year value is not 4 digits long, display an error
	if (String(year).length != 4) {
		return false;
	}

	//If the year value is too small, display an error
	if (year < 1900) {
		return false;
	}

	//If the year value is too large, display an error
	if (year > 3000) {
		return false;
	}

	//If no errors are found, return the valid date as a JavaScript date variable
	return true;

}

// Needed for Date Difference, this was the easiest place to put it...
function DateType_OnChange(value, sbSuffix)
{
	// Check which control to show
	if (value == "2") {
		// Showing Date Constants
		document.getElementById("trDateConstant" + sbSuffix).style.display = "";
		document.getElementById("trDateAttrs" + sbSuffix).style.display = "none";
	} else {
		// Showing Attributes	
		document.getElementById("trDateConstant" + sbSuffix).style.display = "none";
		document.getElementById("trDateAttrs" + sbSuffix).style.display = "";
	}		
}