
var stateProvinceAutoSet = true;
var countryAutoSet = true;
var searchForm = new WebForm();

function searchForm_onSubmit() {
	searchForm.resetErrors();
	dateStatus = checkDates();
	
	if (document.searchForm.city != null){
		locationStatus = checkLocationFields();
	}	
	if (document.searchForm.searchType.value != "favorite") {
		//checkLocationFields();
		searchForm.collapseErrors("multipleError");
	} else if (document.searchForm.searchType.value == "favorite") {
		checkFavoriteSearch(dateStatus);
		searchForm.collapseErrors("datesAndPropertyRquiredError");
	}
	
	if (searchForm.hasErrors()) {
		searchForm.showErrors();
		return false;
	} else {
		return true;
	}
}

function searchFormSOP_onSubmit() {
    searchForm.resetErrors();
    dateStatus = checkDates();

    if (searchForm.hasErrors()) {
        searchForm.showErrors();
        return false;
    } else {
        getLengthOfStay(document.searchForm.arrivalDate.value, document.searchForm.departureDate.value);
        setURLDateFormat();
        return true;
    }
}

function setURLDateFormat() {
    var aryarrivalDate;
    var arydepartureDate;

    switch (dateFormatString) {
        case 'MM/DD/YYYY':
            aryarrivalDate = document.searchForm.arrivalDate.value.split("/");
            arydepartureDate = document.searchForm.departureDate.value.split("/");
            document.searchForm.arrivalDate.value = aryarrivalDate[2] + "-" + aryarrivalDate[0] + "-" + aryarrivalDate[1];
            document.searchForm.departureDate.value = arydepartureDate[2] + "-" + arydepartureDate[0] + "-" + arydepartureDate[1];
            break;
        case 'DD/MM/YYYY':
            aryarrivalDate = document.searchForm.arrivalDate.value.split("/");
            arydepartureDate = document.searchForm.departureDate.value.split("/");
            document.searchForm.arrivalDate.value = aryarrivalDate[2] + "-" + aryarrivalDate[1] + "-" + aryarrivalDate[0];
            document.searchForm.departureDate.value = arydepartureDate[2] + "-" + arydepartureDate[1] + "-" + arydepartureDate[0];
            break;
        case 'DD.MM.YYYY':
            aryarrivalDate = document.searchForm.arrivalDate.value.split(".");
            arydepartureDate = document.searchForm.departureDate.value.split(".");
            document.searchForm.arrivalDate.value = aryarrivalDate[2] + "-" + aryarrivalDate[1] + "-" + aryarrivalDate[0];
            document.searchForm.departureDate.value = arydepartureDate[2] + "-" + arydepartureDate[1] + "-" + arydepartureDate[0];
            break;
        case 'YYYY/MM/DD':
            aryarrivalDate = document.searchForm.arrivalDate.value.split("/");
            arydepartureDate = document.searchForm.departureDate.value.split("/");
            document.searchForm.arrivalDate.value = aryarrivalDate[0] + "-" + aryarrivalDate[1] + "-" + aryarrivalDate[2];
            document.searchForm.departureDate.value = arydepartureDate[0] + "-" + arydepartureDate[1] + "-" + arydepartureDate[2];
            break;
        case 'YYYY/DD/MM':
            aryarrivalDate = document.searchForm.arrivalDate.value.split("/");
            arydepartureDate = document.searchForm.departureDate.value.split("/");
            document.searchForm.arrivalDate.value = aryarrivalDate[0] + "-" + aryarrivalDate[2] + "-" + aryarrivalDate[1];
            document.searchForm.departureDate.value = arydepartureDate[0] + "-" + arydepartureDate[2] + "-" + arydepartureDate[1];
            break;
        default:  // Default to MM/DD/YYYY
            aryarrivalDate = document.searchForm.arrivalDate.value.split("/");
            arydepartureDate = document.searchForm.departureDate.value.split("/");
            document.searchForm.arrivalDate.value = aryarrivalDate[2] + "-" + aryarrivalDate[0] + "-" + aryarrivalDate[1];
            document.searchForm.departureDate.value = arydepartureDate[2] + "-" + arydepartureDate[0] + "-" + arydepartureDate[1];
            break;
    }
}

function checkFavoriteSearch(dateStatus) {
	if (document.searchForm.favoritePropertyID.value < 1) {
		searchForm.addError("~favoritePropertyNotSelectedError", "favoriteHotelLabel");
	}
	if (dateStatus == "DATES_NOT_SET") {
		searchForm.addError("~datesAreRequiredError","arrivalDateLabel","departureDateLabel");
	}
}

function checkLocationFields() {
	locationStatus = getLocationStatus();
	zipCodeStatus = getZipCodeStatus();
	addressEntered = (document.searchForm.address.value != "")
	isAddressSearch = (document.searchForm.searchType.value == "address")
	if (!isAddressSearch) {
		if (locationStatus =="BLANK") {
			searchForm.addError("~searchCriteriaRequiredError","cityLabel", "stateProvinceError", "countryError" );
			return;
		}
		if (locationStatus == "CITY_ONLY") {
			searchForm.addError("~countryNeededForCitySearchError", "countryError" );
			return;
		}
		if (locationStatus == "INVALID_US_SEARCH") {
			searchForm.addError("~toGeneralUSSearchError","stateProvinceError" );
			return;
		}
	} else {
		if (zipCodeStatus == "INVALID") {
			searchForm.addError("~invalidZipCodeError","zipLabel");
			return;
		}
		if (zipCodeStatus == "VALID") {
			return;
		}
		if ((locationStatus =="INVALID_US_SEARCH") && (!addressEntered)) {
			searchForm.addError("~searchCriteriaRequiredError","cityLabel", "stateProvinceError", "zipLabel", "addressLabel" );
			return;
		}
		if (addressEntered) {
			if ((document.searchForm.city.value =="") && (document.searchForm.stateProvince.value == "")) {
				searchForm.addError("~cityAndStateRequiredError","cityLabel", "stateProvinceError");
				return;
			} else if(locationStatus=="STATE_SEARCH") {
				searchForm.addError("~cityRequiredError","cityLabel");
				return;
			} else if(locationStatus=="INVALID_US_SEARCH") {
				searchForm.addError("~stateRequiredError","stateProvinceError");
				return;
			}
		}
		
	}
}
function getLocationStatus() {
	cityEmpty = (document.searchForm.city.value =="");
	stateProvinceEmpty = (document.searchForm.stateProvince.value == "");
	countryEmpty = (document.searchForm.country.value=="");
	countryUS = (document.searchForm.country.value=="US");
	
	if ((!cityEmpty) && (!stateProvinceEmpty) && (!countryEmpty)) { return "COMPLETE_SEARCH"; }
	if ((!stateProvinceEmpty) && (!countryEmpty)) { return "STATE_SEARCH"; }
	if ((!cityEmpty) && (!countryUS) && (!countryEmpty)) { return "NON_US_CITY_SEARCH"; }
	if ((!countryEmpty) && (!countryUS) ) { return "NON_US_COUNTRY_SEARCH"; }
	if ((stateProvinceEmpty) && (countryUS) ){ return "INVALID_US_SEARCH"; }
	if ((!cityEmpty) && (stateProvinceEmpty) && (countryEmpty)) { return "CITY_ONLY"; }
	if ((cityEmpty) && (stateProvinceEmpty) && (countryEmpty)) { return "BLANK"; }
	return "INCOMPLETE";
}
function getZipCodeStatus() {
	var objRegExp  = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
	if(document.searchForm.postalCode.value == ''){
		return "BLANK";
	} else if(!objRegExp.test(document.searchForm.postalCode.value)){
		return "INVALID";
	} else {
		return "VALID"
	}
}

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 > 30) {
//			searchForm.addError("~maximumLengthStayExceededError", "arrivalDateLabel","departureDateLabel");
		} else if (datesDelta < 1) {
			searchForm.addError("~arrivalEqualsDepartureError", "arrivalDateLabel","departureDateLabel");
		}
	}
	return "";
}



function city_onFocus() {
	document.searchForm.city.value = "";
	if (stateProvinceAutoSet) { f.stateProvince.selectedIndex = 0; }
	if (countryAutoSet) { f.country.selectedIndex = 0; }
}

function city_onBlur() {
	a = isIn(document.searchForm.city.value.toLowerCase(),topCities)
	if (a != false) {
		setDropDown(a[1],document.searchForm.stateProvince);
		setDropDown(a[2],document.searchForm.country);
		country_onChange();
		stateProvinceAutoSet = true;
		countryAutoSet = true;
	}
}

function stateProvince_onChange() {
	c = false;
	if(isIn(document.searchForm.stateProvince.value,us_states)) {
		c = "US";
	} else if(isIn(document.searchForm.stateProvince.value,ca_prov)) {
		c = "CA";
	} else if(isIn(document.searchForm.stateProvince.value,misc)) {
		c = "US";
	}
	if (c != false) {
		setDropDown(c,document.searchForm.country);
	}
	country_onChange();
	stateProvinceAutoSet = false;
}
function country_onChange() {
	var f = document.searchForm;
	if (f.searchType.value=="address") {
		setDropDown("US",document.searchForm.country);
	}
	if ((f.country[f.country.selectedIndex].value != "US") && (f.country[f.country.selectedIndex].value != "CA")) {
		setDropDown("",document.searchForm.stateProvince);
	}
	if (f.country[f.country.selectedIndex].value == "US") {
		f.rateRange.disabled = false;
	} else {
		f.rateRange.disabled = true;
		f.rateRange.selectedIndex = 0;
	}
	stateProvinceAutoSet = false;
	countryAutoSet = false;
}
function rateRange_onChange() {
	var f = document.searchForm;
	if (f.country[f.country.selectedIndex].value == "US") {
		f.rateRange.disabled = false;
	} else {
		f.rateRange.disabled = true;
		f.rateRange.selectedIndex = 0;
	}
}

function setDropDown(v,dd) {
	for (i = 0; i < dd.options.length - 1; i++ ) {
		if ( dd.options[i].value == v ) {
			dd.selectedIndex = i;
			return true
		}
	}
	return false;
}

function isIn(elt, list){
	for(var i=0; i < list.length; i++){
		var tempArray = list[i].split(":")
		if( tempArray[0] == elt ){
			return tempArray;
		}
	}
	return false;
}

function searchTab_onClick(searchAction, isLoggedIn) {
	searchForm.resetErrors();
	document.searchForm.searchAction.value = searchAction;
	var t = (searchAction == 'findRooms')
	document.getElementById('rightSearchTab').className = t ? "faded" : "selected";
	document.getElementById('leftSearchTab').className = t ? "selected" : "faded";
	document.getElementById('roomInfo').style.display= t ? "" : "none";

	document.getElementById('redeemLoggedOut').style.display = (!isLoggedIn && (!t)) ? "block" : "none";
	document.getElementById('standardForm').style.display = (! (!isLoggedIn && (!t))) ? "block" : "none";
}

function arrivalDate_onFocus() {
	var checkIn = new validDate(document.searchForm.arrivalDate);
	if (  !checkIn.valid ) { document.searchForm.arrivalDate.value="";}
}

function arrivalDate_onBlur() {
	var month, day, year;
	var tempDate = new Date();
	tempDate.setDate(today.getDate()+ 1);
	month = tempDate.getMonth()+1;
	day = tempDate.getDate();
	year = tempDate.getFullYear();

	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;
		//if (dateFormatString == 'MM/DD/YYYY') {document.searchForm.arrivalDate.value = lZero(month) + "/" +  lZero(day + 1) + "/200" + year.charAt(year.length-1);}
		//else if (dateFormatString == 'DD/MM/YYYY') {document.searchForm.arrivalDate.value = lZero(day + 1) + "/" + lZero(month) + "/200" + year.charAt(year.length-1);}
		//else if (dateFormatString == 'DD.MM.YYYY') {document.searchForm.arrivalDate.value = lZero(day + 1) + "." + lZero(month) + ".200" + year.charAt(year.length-1);}
		//else if (dateFormatString == 'YYYY/MM/DD') {document.searchForm.arrivalDate.value = "200" + year.charAt(year.length-1) + "/" + lZero(month) + "/" +  lZero(day + 1);}
		if (dateFormatString == 'MM/DD/YYYY') {document.searchForm.arrivalDate.value = lZero(month) + "/" +  lZero(day + 1) + "/" + year;}
		else if (dateFormatString == 'DD/MM/YYYY') {document.searchForm.arrivalDate.value = lZero(day + 1) + "/" + lZero(month) + "/" + year;}
		else if (dateFormatString == 'DD.MM.YYYY') {document.searchForm.arrivalDate.value = lZero(day + 1) + "." + lZero(month) + "." + year;}
		else if (dateFormatString == 'YYYY/MM/DD') {document.searchForm.arrivalDate.value = year + "/" + lZero(month) + "/" +  lZero(day + 1);}
	}
}
function departureDate_onFocus() {
	var checkOut = new validDate(document.searchForm.departureDate);
	if (  !checkOut.valid ) { document.searchForm.departureDate.value="";}
}
function departureDate_onBlur() {
	var month, day, year;
	var tempDate = new Date();
	tempDate.setDate(today.getDate()+ 1);
	month = tempDate.getMonth()+1;
	day = tempDate.getDate();
	year = tempDate.getFullYear();

	var checkOut = new validDate(document.searchForm.departureDate);
	if ( checkOut.valid ) 
	{ 
		checkOut.setField(); 
	} else {
		//document.searchForm.departureDate.value=dateFormatString;
		//if (dateFormatString == 'MM/DD/YYYY') {document.searchForm.departureDate.value = lZero(month) + "/" +  lZero(day + 2) + "/200" + year.charAt(year.length-1);}
		//else if (dateFormatString == 'DD/MM/YYYY') {document.searchForm.departureDate.value = lZero(day + 2) + "/" + lZero(month) + "/200" + year.charAt(year.length-1);}
		//else if (dateFormatString == 'DD.MM.YYYY') {document.searchForm.departureDate.value = lZero(day + 2) + "." + lZero(month) + ".200" + year.charAt(year.length-1);}
		//else if (dateFormatString == 'YYYY/MM/DD') {document.searchForm.departureDate.value = "200" + year.charAt(year.length-1) + "/" + lZero(month) + "/" +  lZero(day + 2);}
		if (dateFormatString == 'MM/DD/YYYY') {document.searchForm.departureDate.value = lZero(month) + "/" +  lZero(day + 2) + "/" + year;}
		else if (dateFormatString == 'DD/MM/YYYY') {document.searchForm.departureDate.value = lZero(day + 2) + "/" + lZero(month) + "/" + year;}
		else if (dateFormatString == 'DD.MM.YYYY') {document.searchForm.departureDate.value = lZero(day + 2) + "." + lZero(month) + "." + year;}
		else if (dateFormatString == 'YYYY/MM/DD') {document.searchForm.departureDate.value = year + "/" + lZero(month) + "/" +  lZero(day + 2);}
	}
}

function favorites_onBlur() {
}

function favorites_onClick(id, t) {
	document.searchForm.favoritePropertyID.value = id;
	var nodes = document.getElementById('favoriteHotels').getElementsByTagName('A');
	for( var x = 0; x < nodes.length ; x++ ) {
		nodes[x].className="";
	}
	t.className="selectedFavorite";
	document.searchForm.arrivalDate.focus();
}

function searchType_onClick(searchType) {
	searchForm.resetErrors();
	document.searchForm.searchType.value = searchType;
	document.getElementById('cityToggle').className = (searchType == "city") ? "toggleHREFSelected" : "toggleHREF";
	document.getElementById('addressToggle').className = (searchType == "address") ? "toggleHREFSelected" : "toggleHREF";
	document.getElementById('favoritesToggle').className = (searchType == "favorite") ? "toggleHREFSelected" : "toggleHREF";
	var byF = (searchType == "favorite")
	document.getElementById('byLocationBlock').style.display = byF ? "none" : "block";
	document.getElementById('byFavoriteBlock').style.display = byF ? "block" : "none";
	if (!byF) {
		var t = (searchType == "city");
		document.getElementById('byLocationBlock').style.display = "none";
		document.getElementById('addressBlock').style.display= t ? "none" : "block";
		document.getElementById('city').style.width= t ? "200px" : "60px";
		document.getElementById('zipCode').style.display= t ? "none" : "block";
		if (!t) {document.searchForm.country.selectedIndex = 1;}
		document.getElementById('byLocationBlock').style.display = "block";
		rateRange_onChange();
	} else {
		f.rateRange.disabled = true;
		f.rateRange.selectedIndex = 0;
	}
}
function advancedSearch_onClick() {
	document.searchForm.searchAction.value = "advanced";
	if (document.searchForm.sendRedirect.value != null){
		document.searchForm.sendRedirect.value = "true";
	}
	f.submit();
}

