//
//	MODULE:  $HeadURL$
//  VERSION: $LastChangedRevision$
//
//	DESCRIPTION: Javascript date functions that are called by the form for en_US
// 

//==========================================================================================
//==========================================================================================
//
//	FUNCTION:	padOutDate()
//
//	DESCRIPTION
/**
 *	This function is used for day and month fields in date entry. The function will take the
 *	input field object and concatenate a zero on to the front of the entry allowing for more
 *	accurate entry of dates.
 */
//==========================================================================================
//==========================================================================================
function padOutDate(textField)
{
    if((textField < 10) && (textField >= 0) && (textField.length < 2))
    {
       textField = "0" + textField;
    }
	
	return textField;
}// End of padOutDate()

//==========================================================================================
//==========================================================================================
//
//	FUNCTION:	checkValidDate()
//
//	DESCRIPTION
/**
 *	This function is used to check if the given parameters form a valid date.
 */
//==========================================================================================
//==========================================================================================
function checkValidDate(datePart, formDay, formMonth, formYear) 
{
    var day = formDay;
    var month = formMonth;
    var year = formYear;

	if (month < 1 || month > 12)
	{
		alert("Month must be between 1 and 12.");
		strErr=1;
		datePart.focus();
		return false;
	}

	if (day < 1 || day > 31) 
	{
		alert("Day must be between 1 and 31.");
		strErr=1;
		datePart.focus();
		return false;
	}

	if (day == 31)
	{
		switch(month)
		{
			case "04": 
				alert("April doesn't have 31 days. \nPlease correct the date.");
				datePart.focus();
				break;
			case "06":
				alert("June doesn't have 31 days. \nPlease correct the date.");
				datePart.focus();
				break;
			case "09":
				alert("September doesn't have 31 days. \nPlease correct the date.");
				datePart.focus();
				break;
			case "11":
				alert("November doesn't have 31 days. \nPlease correct the date.");
				datePart.focus();
				break;
			case "02":
				break;
			default:
				break;
		}
	}

	if (month == 2) 
	{
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) 
		{
			alert("February " + year + " doesn't have " + day + " days. \nPlease correct the date");
			strErr=1;
			datePart.focus();
			return false;
		}
	}

	return true;

}// End of checkValidDate()


//==========================================================================================
//==========================================================================================
//
//	FUNCTION:	isValidDate()
//
//	DESCRIPTION
/**
 *	This function is used to format a correct date.
 *
 *	Edited by KG on 02/08/04 regarding PDF_01169
 */
//==========================================================================================
//==========================================================================================
function isValidDate(dateField)
{
	var fullExp = /^\d{1,2}(\/)\d{1,2}(\/)\d{2,4}$/
	var noslashExp = /^\d{6,8}$/

	var dString = dateField.value;
	
	if ( (dString == null) || (dString == "") )
	{
		return;
	}

	if (fullExp.test(dString))
	{
		var split_date=dString.split("/");

		if (split_date[2].length == 4)
		{
			dateField.value = padOutDate(split_date[0]) + '/' +
				padOutDate(split_date[1]) + '/' + split_date[2];
			checkValidDate(dateField, padOutDate(split_date[1]),
				padOutDate(split_date[0]), split_date[2]);
		}
		else if (split_date[2].length == 3)
		{
			// y2k check
			if (split_date[2] <= 40)
			{
				dateField.value = padOutDate(split_date[0]) + '/' +
					padOutDate(split_date[1]) + '/2' + split_date[2];
				checkValidDate(dateField, padOutDate(split_date[1]),
					padOutDate(split_date[0]), '2' + split_date[2]);
				return;
			}
			else
			{
				dateField.value = padOutDate(split_date[0]) + '/' +
					padOutDate(split_date[1]) + '/1' + split_date[2];
				checkValidDate(dateField, padOutDate(split_date[1]),
					padOutDate(split_date[0]), '1' + split_date[2]);
				return;
			}
		}
		else // length must be 2
		{
			// y2k check
			if (split_date[2] <= 40)
			{
				dateField.value = padOutDate(split_date[0]) + '/' +
					padOutDate(split_date[1]) + '/20' + split_date[2];
				checkValidDate(dateField, padOutDate(split_date[1]),
					padOutDate(split_date[0]), '20' + split_date[2]);
				return;
			}
			else
			{
				dateField.value = padOutDate(split_date[0]) + '/' +
					padOutDate(split_date[1]) + '/19' + split_date[2];
				checkValidDate(dateField, padOutDate(split_date[1]),
					padOutDate(split_date[0]), '19' + split_date[2]);
				return;
			}
		}
	}
	else if (noslashExp.test(dString))
	{
		// layout is 6 or 8 digits.

		if (dString.length == 6)
		{
			// must be MMDDYY.
			if (dString.substring(4,6) <= 40)
			{
				
				dateField.value = dString.substring(0,2) + '/' +
					dString.substring(2,4) + '/20' + dString.substring(4,6);
				checkValidDate(dateField, dString.substring(2,4),
					dString.substring(0,2), '20' + dString.substring(4,6));
				return;
			}
			else
			{
				if ((dString.substring(2,3) == "/") && (dString.substring(5,6) == "/"))
				{
					alert('Not a valid date');
					return;
				}
				else
				{
					dateField.value = dString.substring(0,2) + '/' +
						dString.substring(2,4) + '/19' + dString.substring(4,6);
					checkValidDate(dateField, dString.substring(2,4),
						dString.substring(0,2), '19' + dString.substring(4,6));
					return;
				}
			}
		}
		else if (dString.length == 8)
		{
			// must be DDMMYYYY.
			dateField.value = dString.substring(0,2) + '/' +
				dString.substring(2,4) + '/' + dString.substring(4,8);
			checkValidDate(dateField, dString.substring(2,4),
				dString.substring(0,2), '19' + dString.substring(4,6));
			return;
		}
		else
		{
			alert('Not a valid date');
			dateField.focus();
			return;
		}
	}
	else
	{
		alert('Not a valid date');
		dateField.focus();
		return;
	}
}


