var mCIDate = "";
var mCODate = "";

function makeDate(md)
{
	return new Date(md.getMonth() + 1 + "/" + md.getDate() + "/" + md.getYear());
}

function validCheckDates()
{
	if ((f.ciDateDay.selectedIndex > 0) && (f.ciDateMonth.selectedIndex > 0))
	{
		//setDates(f.ciDateMonth);

		var checkIn = dateStrToDateObj(mCIDate);
		var checkOut = dateStrToDateObj(mCODate);

		if (checkIn != null)
		{
			var datesDelta = checkOut.getTime() - checkIn.getTime();
			var nowDelta = (makeDate(checkIn).getTime() - makeDate(new Date()).getTime()) / (1000*60*60*24);
			var datesEqual = (checkIn.getTime() == checkOut.getTime());

			//alert("datesCheck(" + checkIn + ", " + checkOut);

			// begin 550 day check
			var today = new Date();
			today.setHours(0);
			today.setMinutes(0);
			today.setSeconds(0);
			today.setMilliseconds(0);
			var limit = new Date(550 * 24 * 60 * 60 * 1000);
			/*have to add the 1 hr in millis because of the possibility of daylight savings time removing an hour.*/
			var isTooLong = (checkIn.getTime() - today.getTime() >= (limit.getTime() - (60*60*1000))) ? true : false;
			// end 550 day check

			if (nowDelta < 0 )
			{
				// Error : checkin date before today

				setErrCode(9);
				setErrCode(1);
			}
			else if (datesEqual) {
			// Error : checkin date = checkout date
			setErrCode(11);
			setErrCode(1);
			setErrCode(2);
			} else if (isTooLong) {
			// Error : too far out in the future (> 550 days or 18 months)
			setErrCode(13);
			setErrCode(1);
			setErrCode(2);
			} else if (datesDelta < 0) {
			// Error : checkin date after checkout date
			setErrCode(10);
			setErrCode(1);
			setErrCode(2);
			} else if (datesDelta > 1000*60*60*24*30) {
			// Error : stay too long (> 30 days or 1 month)
			setErrCode(12);
			setErrCode(1);
			setErrCode(2);
			}
			else
			{
				f.arrivalDate.value = mCIDate;
				f.departureDate.value = mCODate;
			}
		}
	}
}

function validForm(f,action,pageType) {
	validCheckDates();
    updateLengthOfStay(f.arrivalDate.value, f.departureDate.value);
    checkForErrors(f,action,pageType);
}

function updateLengthOfStay(checkIn, checkOut) {

    var checkInArray = checkIn.split("-");
    var checkInDate = new Date(checkInArray[0], checkInArray[1]-1, checkInArray[2]);
    var checkOutArray = checkOut.split("-");
    //var checkOutDate = new Date(checkOutArray[0], checkOutArray[1]-1, parseInt(checkOutArray[2]));
    var checkOutDate = new Date(checkOutArray[0], checkOutArray[1]-1, checkOutArray[2]);
    //document.write(checkOutDate);

    var los = getDiffDays(checkInDate, checkOutDate)

    if (f.lengthOfStay != null) {
        f.lengthOfStay.value = los;
    }
}

function setDatesToday()
{
	if ((f.ciDateDay.selectedIndex == 0) && (f.ciDateMonth.selectedIndex == 0) && (f.coDateDay.selectedIndex == 0) && (f.coDateMonth.selectedIndex == 0))
	{
		okReset(f);
		var loCheckinDate = getNextDate(new Date());
		var loCheckoutDate = getNextDate(loCheckinDate);
		f.arrivalDate.value = loCheckinDate.getFullYear() + "-" + paddZero(loCheckinDate.getMonth() + 1) + "-" + paddZero(loCheckinDate.getDate());
		f.departureDate.value = loCheckoutDate.getFullYear() + "-" + paddZero(loCheckoutDate.getMonth() + 1) + "-" + paddZero(loCheckoutDate.getDate());
		f.lengthOfStay.value = 1
	}
	else
	{
		setDates(f.ciDateMonth);
	}
}

function dateStrToDateObj(pDate)
{
	var loDateObj = null;

    if(pDate != '')
	{
    	var lsDateString = pDate;
        var loDateArray = new Array();

		loDateArray = lsDateString.split("-");
        loDateObj = new Date(loDateArray[1] + "/" + loDateArray[2] + "/" + new Number(loDateArray[0]));
    }

	return loDateObj;
}

function ciDateSetCo()
{
	var loCheckIn = dateStrToDateObj(mCIDate);
	var loCheckOut = dateStrToDateObj(mCODate);

	if (loCheckOut == null || loCheckOut == loCheckIn ) {loCheckOut = new Date();}

	loCheckOut = loCheckIn;
	loCheckOut.setDate(loCheckIn.getDate()+1);

	writeDateToDropDowns(f.coDateMonth, loCheckOut);
	writeDateToDropDowns(f.coDateDay, loCheckOut);

	mCODate = f.coDateMonth.value.slice(3,9)+ "-" +f.coDateMonth.value.slice(0,2)+ "-" + f.coDateDay.value;

}

function writeDateToDropDowns(formField, date)
{
	var month = date.getMonth()+1;

	month = paddZero(month);

	var day = date.getDate();

	day = paddZero(day);

	var year = date.getFullYear();
	var sep = "-";

	var monthYr = month + sep + year;

	if (formField.name == "ciDateMonth" || formField.name == "coDateMonth")
	{
		selectInOpList(formField, formField, monthYr);
	}
	else {selectInOpList(formField, formField, day);}
}

function selectInOpList(fromList, inList, val)
{
	var x = 0;
	for ( x=0 ; x < fromList.length; x++ )
		if (inList.options[x].value == val)
		{
			inList.options[x].selected = true;
			break;
		}
}

function setDates(dateDropDown)
{
	var loMonth;
	var loDay;
	var loYear;
	var tempDate;

	if (dateDropDown.name == "ciDateDay" || dateDropDown.name == "ciDateMonth")
	{
		if ((f.ciDateDay.selectedIndex > 0) && (f.ciDateMonth.selectedIndex > 0))
		{

			loMonth = f.ciDateMonth.value.slice(0,2) - 1;
			loDay = f.ciDateDay.value;
			loYear = f.ciDateMonth.value.slice(3,9);

			tempDate = new Date(loYear, loMonth, loDay);

			mCIDate = tempDate.getFullYear() + "-" + paddZero(tempDate.getMonth() + 1) + "-" + paddZero(tempDate.getDate());

			writeDateToDropDowns(f.ciDateMonth, tempDate);
			writeDateToDropDowns(f.ciDateDay, tempDate);

			if (mCODate == "")
			{
				ciDateSetCo();
			}
			else
			{
				var loCheckIn = dateStrToDateObj(mCIDate);
				var loCheckOut = dateStrToDateObj(mCODate);

				if ((loCheckOut.getTime() <= loCheckIn.getTime()))
				{
					ciDateSetCo();
				}
			}

		}
	}
	else
	{
		if ((f.coDateDay.selectedIndex > 0) && (f.coDateMonth.selectedIndex > 0))
		{
			loMonth = f.coDateMonth.value.slice(0,2) - 1;
			loDay = f.coDateDay.value;
			loYear = f.coDateMonth.value.slice(3,9);

			tempDate = new Date(loYear, loMonth, loDay);

			mCODate = tempDate.getFullYear() + "-" + paddZero(tempDate.getMonth() + 1) + "-" + paddZero(tempDate.getDate());

			writeDateToDropDowns(f.coDateMonth, tempDate);
			writeDateToDropDowns(f.coDateDay, tempDate);
		}
	}
}

function setDateDropDowns(mn,dy,yr)
{
	theDate = new Date(yr, mn, dy);

	if (aod=="dep")
	{
		writeDateToDropDowns(f.coDateMonth, theDate);
		writeDateToDropDowns(f.coDateDay, theDate);

		mCODate = yr + "-" + paddZero(theDate.getMonth() + 1) + "-" + paddZero(dy.toString());
	}
	else if (aod=="arr")
	{
		writeDateToDropDowns(f.ciDateMonth, theDate);
		writeDateToDropDowns(f.ciDateDay, theDate);

		mCIDate = yr + "-" + paddZero(theDate.getMonth() + 1) + "-" + paddZero(dy.toString());
		setDates(f.ciDateMonth);
	}

}


function openCalDD(calType,f)
{
	if (calType == "arrival")
	{
		dMax = 550;
		dMin = 0;
		aod = "arr";
		document.SearchForm.inputDate.value = mCIDate;
		sameDate = true;
	}
	else
	{
		dMax = 579;
		dMin = 1;
		aod = "dep";
		document.SearchForm.inputDate.value = mCODate;
	}

	var mybrand = f.theBrand.value;

	myFloater = window.open('','myWindow','scrollbars=no,status=no,width=220,height=200,top=450,left=250,resizable=no');

	if ( myFloater.opener == null )
		myFloater.opener = self;

	myFloater.location.href = "/dp/en_US/common/search/calendarDD.jsp?brand=" + mybrand;

}

