function getDaysInMonth(iMonth, iYear)
{
	var arrayDaysInMonth = new Array(12)
	
	arrayDaysInMonth[1] = 31;
	arrayDaysInMonth[2] = 28;
	arrayDaysInMonth[3] = 31;
	arrayDaysInMonth[4] = 30;
	arrayDaysInMonth[5] = 31;
	arrayDaysInMonth[6] = 30;
	arrayDaysInMonth[7] = 31;
	arrayDaysInMonth[8] = 31;
	arrayDaysInMonth[9] = 30;
	arrayDaysInMonth[10] = 31;
	arrayDaysInMonth[11] = 30;
	arrayDaysInMonth[12] = 31;

	// Verify leap year
	if ((iYear % 100) == 0)
	{
		if ((iYear % 400) == 0)
		{
			arrayDaysInMonth[2] = 29;
		}
	}
	else
	{
		if ((iYear % 4) == 0)
		{
			arrayDaysInMonth[2] = 29;
		}
	}
	
	return arrayDaysInMonth[iMonth];
}


// ---------------------------------------------------------------------------
// Function isValidDateFormat(domElement, sDateDescr, bDisplayAlert)
// Function verify that date is in the proper format
//      Dates entered in MMDDYYYY format will be converted to MM/DD/YYYY
// Parameters:
//				domElement : integer specifying the date
//				sDateDescr : field description, may be "" if bDisplayAlert is false
//				bDisplayAlert : whether to popup the alert window
// --------------------------------------------------------------------------

function isValidDateFormat(domElement, sDateDescr, bDisplayAlert)
{
	var bResultInd = false;
	var sDateStr = domElement.value;
	var iDateStrLen = sDateStr.length;
	var sAlertMsg = sDateDescr + " must be in format MM/DD/YYYY.";
	var bContinueValidate = false;

	if (iDateStrLen == 0)
		bResultInd = true;				// blank is allowed - No Date!
	
	if (!bResultInd)
	{
		var sMonthStr = "";
		var sDayStr = "";
		var sYearStr = "";
		
		if (isNumeric(sDateStr))
		{
			// Check to see if user enters all numbers
			if (iDateStrLen == 8)
			{
				sMonthStr = sDateStr.substring(0,2);
				sDayStr = sDateStr.substring(2,4);
				sYearStr = sDateStr.substring(4,8);
			
				sDateStr = sDateStr.substring(0,2) + "/" + sDateStr.substring(2,4) + "/" + sDateStr.substring(4,8);
				iDateStrLen = sDateStr.length;
				bContinueValidate = true;
			}
		}
		else
		{	
			var iDelimeterIndex = firstNotIn("0123456789", sDateStr);
			var cDelimeterChar = sDateStr.charAt(iDelimeterIndex);
			
			var sTempStr = "";
			var iTempStrLen = 0;
			var iFirstDelimeterPos = 0;
			var iSecondDelimeterPos = 0;

			if (cDelimeterChar == '/' || cDelimeterChar == '-')			// (!isLetter(cDelimeterChar))
			{
				iFirstDelimeterPos = sDateStr.indexOf(cDelimeterChar);
				sMonthStr = sDateStr.substring(0,iFirstDelimeterPos);
				sTempStr = sDateStr.substring(iFirstDelimeterPos + 1, iDateStrLen);
				iTempStrLen = sTempStr.length;
				
				iDelimeterIndex = firstNotIn("0123456789", sTempStr);
				cDelimeterChar = sTempStr.charAt(iDelimeterIndex);

				if (cDelimeterChar == '/' || cDelimeterChar == '-')		// (!isLetter(cDelimeterChar))
				{
					iSecondDelimeterPos = sTempStr.indexOf(cDelimeterChar);
					sDayStr = sTempStr.substring(0, iSecondDelimeterPos);
					sYearStr = sTempStr.substring(iSecondDelimeterPos + 1, iDateStrLen);
					bContinueValidate = true;
				}
			}
		}

		if (bContinueValidate && (isNumeric(sMonthStr)) && (isNumeric(sDayStr)) && (isNumeric(sYearStr)))
		{
			if (bContinueValidate)
			{
				if (sMonthStr.length > 2)
				{
					bContinueValidate = false;
				}
			}

			if (bContinueValidate)
			{
				if (sDayStr.length > 2)
				{
					bContinueValidate = false;
				}
			}

			if (bContinueValidate)
			{
				if (sYearStr.length != 4)
				{
					bContinueValidate = false;
				}
			}
			
			if (bContinueValidate)
			{
				var iMonth = parseInt(sMonthStr, 10);
				var iDay = parseInt(sDayStr, 10);
				var iYear = parseInt(sYearStr, 10);
				
				if ((iMonth < 1) || (iMonth > 12))
				{
					bContinueValidate = false;
				}
				else
				{
					if (sMonthStr.length < 2)
					{
						sMonthStr = "0" + sMonthStr;
					}
				}
			}
			
			if (bContinueValidate)
			{
				var iDaysInMonth = getDaysInMonth(iMonth, iYear);

				if ((iDay < 1) || (iDay > iDaysInMonth))
				{
					bContinueValidate = false;
				}
				else
				{
					if (sDayStr.length < 2)
					{
						sDayStr = "0" + sDayStr;
					}
				}
				
			}
			
			if ((iYear < 1900) || (iYear > 2050))
			{
				bContinueValidate = false;
			}
		}
		else {
			bContinueValidate = false;
		}
			
		if (!bResultInd)
		{
			bResultInd = bContinueValidate;	
		}
	
		if (bResultInd)
		{
			domElement.value = (sMonthStr + "/" + sDayStr + "/" + sYearStr);
		}
		else
		{
			if (bDisplayAlert)
			{
				alert(sAlertMsg);
			}
		}
	}
	
	return bResultInd;
}


// ---------------------------------------------------------------------------
// Function function getDatePart(cDatePart, sDateStr)
// Function to obtain Day, Month, or Year out of a date string in the format 
//				of MM/DD/YYYY
// Parameters:
//				cDatePart:	'D' or 'd' for Day
//							'M' or 'm' for Month
//							'Y' or 'y' for Year
//				sDateStr:	Date String in the format of MM/DD/YYYY
//
// Comment: Does not validate the date string.  Assume correct format
// --------------------------------------------------------------------------

function getDatePart(cDatePart, sDateStr)
{
	var iDatePart = 0;
	
	if (cDatePart == 'D' || cDatePart == 'd')
		iDatePart = sDateStr.substring(3,5);
		
	if (cDatePart == 'M' || cDatePart == 'm')
		iDatePart = sDateStr.substring(0,2);
		
	if (cDatePart == 'Y' || cDatePart == 'y')
		iDatePart = sDateStr.substring(6,10);

	return iDatePart;
}


// ---------------------------------------------------------------------------
// Function compareDateValues(sDateValue1, sDateValue2)
// Functions to compare the VALUES (strings) of two dates.
// Parameters:
//              sDateValue1:  value of Date1
//              sDateValue2:  value of Date2
// Returns:
//				if Date1 < Date2: -1
//				if Date1 = Date2: 0
//				if Date1 > Date2: 1
// --------------------------------------------------------------------------

function compareDateValues(sDateValue1, sDateValue2)
{
	var iResult = 0;
	var iMonth1 = 0;
	var iMonth2 = 0;
	var iDay1 = 0;
	var iDay2 = 0;
	var iYear1 = 0;
	var iYear2 = 0;
		
	iMonth1 = getDatePart('M', sDateValue1);
	iDay1 =  getDatePart('D', sDateValue1);
	iYear1 = getDatePart('Y', sDateValue1);
	
	iMonth2 = getDatePart('M', sDateValue2);
	iDay2 =  getDatePart('D', sDateValue2);
	iYear2 = getDatePart('Y', sDateValue2);
	
	iMonth1 = iMonth1 - 1;
	iMonth2 = iMonth2 - 1;

	var dDate1 = new Date(iYear1, iMonth1, iDay1);
	var dDate2 = new Date(iYear2, iMonth2, iDay2);

	if (dDate1 == dDate2)
		iResult = 0;
		
	if (dDate1 < dDate2)
		iResult = -1;
		
	if (dDate1 > dDate2)
		iResult = 1;

	return iResult;
}



// ---------------------------------------------------------------------------
// Function setFutureDate(iSourceDateIndex, iIntervalElementIndex, sDestDateIndex)
// Function to add X number of days to a specified date and place the
//      result in another field
// Parameters:
//				iSourceDateIndex : integer specifying the date
//				iIntervalElementIndex : integer specifying the number field
//              sDestDateIndex : integer specifying the result date
// --------------------------------------------------------------------------

function setFutureDate(iSourceDateIndex, iIntervalElementIndex, sDestDateIndex)
{
	if (isValidDateFormat(iSourceDateIndex, "", false))
	{
		var sDateStr = "";

		var MINUTE = 60 * 1000;		//constants
		var HOUR = MINUTE * 60;
		var DAY = HOUR * 24;
		var WEEK = DAY * 7;

		var iMonth1 = document.forms[0].elements[iSourceDateIndex].value.substring(0,2);
		var iDay1 =  document.forms[0].elements[iSourceDateIndex].value.substring(3,5);
		var iYear1 = document.forms[0].elements[iSourceDateIndex].value.substring(6,10);
		var iMonth1js = iMonth1 - 1;
		var iDaysToAdd = document.forms[0].elements[iIntervalElementIndex].value;

		var myDate = new Date(iYear1, iMonth1js, iDay1);
		var dateInMS = myDate.getTime();
		var MSToAdd = iDaysToAdd * DAY;
    	var endDateInMS = dateInMS + MSToAdd;
    
		myDate.setTime(endDateInMS);

		var iEndDay = myDate.getDate();
		var iEndMonth = myDate.getMonth() + 1;
		var iEndYear = myDate.getYear();
	
		if (iEndYear < 2000)
		{
			iEndYear = myDate.getYear() + 1900;
		}

		// Construct the new date string for
		if (iEndMonth < 10)
		{
			sDateStr = "0";
		}

		sDateStr += "" + iEndMonth + "/";

		if (iEndDay  < 10)
		{
			sDateStr += "0";
		}
    
		sDateStr += iEndDay + "/";
		sDateStr += iEndYear;
		document.forms[0].elements[sDestDateIndex].value = sDateStr;
	}
	else
	{
		alert("Invalid Date Format!");
	}
}


