var stateProvinceAutoSet = true;
var countryAutoSet = true;
var searchForm = new WebForm();

function searchForm_onSubmit() {
	searchForm.resetErrors();
	
	if (searchType == "city") {
		//city_onBlur();
		dateStatus = checkDates();
		f.action = "/" + brandName + "/search/results/standard.html";
		locationStatus = getLocationSearchStatus();
		if ((locationStatus =="BLANK")) {
			searchForm.addError("searchCriteriaRequiredError" );
		}
		if ((locationStatus =="INVALID_US_SEARCH") ) {
			searchForm.addError("toGeneralUSSearchError" );
		}
		if ((locationStatus =="MISSING_STATE_US_SEARCH") ) {
			searchForm.addError("stateProvinceRequiredError" );
		}
		if ((locationStatus =="CITY_ONLY") ) {
			searchForm.addError("countryRequiredForCitySearchError" );
		}
	} else if (searchType == "hotelName") {
		f.action = "/" + brandName + "/search/results/hotel_name.html";
		document.searchForm.departureDate.value = dateFormatString;
		document.searchForm.arrivalDate.value = dateFormatString;
		if (f.hotelName.value == "") {
			searchForm.addError("hotelNameRequiredError");
		}
        // cater for simple search
    } else if(searchType == "simpleSearch"){
        f.action = "/" + brandName + "/search/results/results.html";
        if(f.complexSearchField.value == ""){
            searchForm.addError("searchCriteriaRequiredError" );
        }

    }
	
	if (searchForm.hasErrors()) {
		searchForm.showErrors();
		return false;
	} else {
		return true;
	}
}
function checkDates() {
	if ((document.searchForm.arrivalDate.value == dateFormatString) && (document.searchForm.departureDate.value == dateFormatString)) {
		return "DATES_NOT_SET";
	}

	var checkIn = new validDate(document.searchForm.arrivalDate);	
	var checkOut = new validDate(document.searchForm.departureDate);	
	
	if (!(checkIn.valid) && (!checkOut.valid) ) {
		searchForm.addError("~ciAnddepartureDatesInvalidError", "arrivalDateLabel","departureDateLabel")
	} else if (!checkIn.valid ) {
		searchForm.addError("~arrivalDateInvalidError","arrivalDateLabel")
	} else if (!checkOut.valid ) {
		searchForm.addError("~departureDateInvalidError","departureDateLabel")
	} else 	if ( (checkIn.valid) & (checkOut.valid)  ) {
		var datesDelta = checkIn.diffDate( checkOut.d);
		var nowDelta = checkIn.diffDate(new Date());
		if (nowDelta > 0 ) {
			searchForm.addError("~checkInEarlierThanTodayError","arrivalDateLabel");
		} else if (nowDelta < -561) {
			searchForm.addError("~bookToFarInFutureError","arrivalDateLabel");
			searchForm.addError()
		} else if (datesDelta < 0) {
			searchForm.addError("~departureBeforeArrivalError","departureDateLabel");
		} else if (datesDelta > 31) {
			searchForm.addError("~maximumLengthStayExceededError", "arrivalDateLabel","departureDateLabel");
		} else if (datesDelta < 1) {
			searchForm.addError("~arrivalEqualsDepartureError", "arrivalDateLabel","departureDateLabel");
		}
	}
	return "";
}

function tab_onClick(tabName) {
	searchForm.resetErrors();
	if (tabName=="city") {
		searchType="city";
		displayBlock("citySearchFields");
		setClass("cityTab", "selected");
	} else {
		displayNone("citySearchFields");
		setClass("cityTab", "");
	}
	if (tabName=="hotelName") {
		searchType="hotelName";
		displayBlock("hotelNameSearchFields");
		setClass("hotelNameTab", "selected");
	} else {
		displayNone("hotelNameSearchFields");
		setClass("hotelNameTab", "");
	}
	return false;
}

function init() {
}

/* ********************************************************************** */
/* ***************        BEGIN CALENDAR CODE       ********************* */
var myFloater = -1;
var thisDate;

var dMax = 550;
var dMin = 0;
var sameDate = false;
var aod = "";

var changedDate;
var fType = typeof f;

//-----------------------------------------------------------------
// this function is called from the calendar popup window
function setDate(mn,dy,yr) {
	var dateField;
	if (aod == "arr") {
		 dateField = new validDate(document.searchForm.arrivalDate);
	} else if (aod == "dep") {
		dateField = new validDate(document.searchForm.departureDate);
	}
    _d = new Date(yr,mn,dy);
	// _d.setTime( Date.parse( mn.toString() + "/" + dy.toString() + "/" + yr) );
	dateField.setD(_d);
	dateField.setField();
	arrivalDate_onBlur();
}

//-----------------------------------------------------------------
// this function is called from the calendar icon
function openCal(calType,ciDate,coDate) {
	oInput = ciDate;
	oInput2 = coDate;
	var dt1 = null;
	var dt2 = null;

	if (calType == "arrival") {
		aod = "arr";
		thisDate = oInput;
		var thisDate2 = oInput2;
		ciDate.value = thisDate.value;
		document.SearchForm.inputDate.value = thisDate.value;
		sameDate = true;
		changedDate = "dtArr";
	} else {
		aod = "dep";
		dt1 = new Date(oInput.value);
		dt2 = new Date(oInput2.value);
		thisDate = oInput;
		var thisDate2 = oInput2;

		if (dt1 < dt2) {
			coDate.value = thisDate2.value;
			sameDate = true;
		} else {
			ciDate.value = thisDate.value;
			sameDate = true;
		}
		
		document.SearchForm.inputDate.value = thisDate2.value;
		changedDate = "dtDep";
	}

	var mybrand = (fType != "undefined") ? ((f.theBrand) ? f.theBrand.value : "") : "";
	myFloater = window.open('','myWindow','scrollbars=no,status=no,width=220,height=240,top=450,left=250,resizable=no');

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

	myFloater.location.href = "/dp/en_US/common/search/calendar.jsp?brand=" + mybrand;
}
/* ***************         END CALENDAR CODE         ********************** */
/* *********************************************************************** */

function arrivalDate_onFocus() {
	var checkIn = new validDate(document.searchForm.arrivalDate);
	if (  !checkIn.valid ) { document.searchForm.arrivalDate.value="";}
}

function arrivalDate_onBlur() {
	var checkIn = new validDate(document.searchForm.arrivalDate);
	if ( checkIn.valid ) {
		checkIn.setField();
		var checkOut = validDate(document.searchForm.departureDate);
		if (checkOut.valid ) { before = (checkIn.diffDate(checkOut.d) <= 0);}
		if ( (! (checkOut.valid)) || before) {
			checkOut.setD(checkIn.d);
			checkOut.nextDay();
			checkOut.setField();
		} 
	} else {document.searchForm.arrivalDate.value=dateFormatString;}
}
function departureDate_onFocus() {
	var checkOut = new validDate(document.searchForm.departureDate);
	if (  !checkOut.valid ) { document.searchForm.departureDate.value="";}
}
function departureDate_onBlur() {
	var checkOut = new validDate(document.searchForm.departureDate);
	if ( checkOut.valid ) { checkOut.setField(); } else {document.searchForm.departureDate.value=dateFormatString;}
}
