//© 2010 Starwood Hotels & Resorts Worldwide, Inc.
//  All rights reserved.
function okCheckDates(f) {
	if (f.ciDate.value != dateFormatString){
		var checkIn = new validDate(f.ciDate);
		if (!checkIn.valid ) {
			// check-in date not valid
			setErrCode(19);
			setErrCode(1);
		}
	}

	if (f.coDate.value != dateFormatString){
		var checkOut = new validDate(f.coDate);
		if (!checkOut.valid) {
			// check-out date not valid
			setErrCode(20);
			setErrCode(2);
		}
	}

	if (f.ciDate.value != dateFormatString && f.coDate.value != dateFormatString){
		if ( (checkIn.valid) & (checkOut.valid) ) {
			// check for other date validation
			//alert("datesCheck(" + checkIn.d + ", " + checkOut.d);
			datesCheck(checkIn, checkOut);
             var openingDateObj = validateOpeningDate_ratelist(f);
             if (openingDateObj!=null){
                var openDelta = checkIn.diffDate(openingDateObj.openDate);
                 if (openDelta<0){
                     searchForm.openDateDisplay=openingDateObj.openDateDisplay;
                     setErrCode(38);

                 }
             }
        }
	}

}
function validateOpeningDate_ratelist(form){
    if (propertyArray && propertyArray.length>0){
        for (var i=0; i<propertyArray.length; i++){
            if (form.propertyID.value==propertyArray[i].pid){
                var openDateString=propertyArray[i].openDate;
                if (openDateString && openDateString.length>0){
                    var openDateArray = openDateString.split("/");
                    var openDate = new Date();
                    openDate.setFullYear(openDateArray[2],openDateArray[0]-1,openDateArray[1]);
                    var openDateObj = {openDate:openDate,openDateDisplay:propertyArray[i].openDateDisplay};
                    return openDateObj;
                }
            }
        }
    }
}
function outputOpenDateError_ratelist(){
    var checkinDateBeforeOpendate_bm_Container = document.getElementById("divMsgTop");
    var searchFormError = yuiDom.getElementsByClassName("searchFormErrorText","span",checkinDateBeforeOpendate_bm_Container)[0];
    var dateField =yuiDom.getElementsByClassName("openDateDisplay","span",searchFormError)[0];
    dateField.innerHTML=searchForm.openDateDisplay;
}
// this function takes Dates, not validDates()
function datesCheck(checkIn, checkOut) {
	checkIn.setField();
	var datesDelta = checkOut.d.getTime() - checkIn.d.getTime();
	var nowDelta = checkIn.diffDate(new Date());
	var datesEqual = (checkIn.d.getTime() == checkOut.d.getTime());

	// 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 = (checkOut.d.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*31) {
		// Error : stay too long (> 31 days or 1 month)
		setErrCode(12);
		setErrCode(1);
		setErrCode(2);
	}

    
}
