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

function advancedSearchForm_onSubmit() {
	searchForm.resetErrors();

	if (searchType=="city") {
		f.action = "results/city_advanced.html";
		//city_onBlur();
		checkDates();
		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" );
		}
	}
	if (searchType=="address") {
		f.action = "results/address_advanced.html";
		city_onBlur();
		checkDates();
		zipCodeStatus = getZipCodeStatus();
		locationStatus = getLocationSearchStatus();
		if (zipCodeStatus == "INVALID") {
			searchForm.addError("invalidZipCodeError", "zipCodeLabel");
		} else if (zipCodeStatus == "BLANK") { 
			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" );
			}
		}
	}
	if (searchType=="hotelName") {
		f.action = "results/hotel_name.html";
		document.searchForm.departureDate.value = dateFormatString;
		document.searchForm.arrivalDate.value = dateFormatString;
		if (f.hotelName.value.replace(/^\s+|\s+$/, '') == "") {
			searchForm.addError("hotelNameRequiredError");
		}
	}
	if (searchType=="airportCode") {
		f.action = "results/airport_advanced.html";
		checkDates();
		if (f.airportCode.value == "") {
			searchForm.addError("airportCodeRequiredError");
		}
	}
	
	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 getLocationSearchStatus() {
	cityEmpty = (f.city.value =="");
	stateProvinceEmpty = (f.state.value == "");
	countryEmpty = (f.country.value=="");
	countryUS = (f.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) && (!cityEmpty) ){ return "MISSING_STATE_US_SEARCH"; }
	if ((stateProvinceEmpty) && (countryUS) ){ return "INVALID_US_SEARCH"; }
	if ((!cityEmpty) && (stateProvinceEmpty) && (countryEmpty)) { return "CITY_ONLY"; }
	if ((cityEmpty) && (stateProvinceEmpty) && (countryEmpty)) { return "BLANK"; }
	return "INCOMPLETE";
}

function getAirportCode_onClick() {
	window.open('/dp/en_US/common/search/airportLookUp.jsp?element=searchForm.airportCode&brand=westin','mywindow','width=390,height=490,scrollbars=yes')
	return false;	
}

function tab_onClick(tabName) {
	searchForm.resetErrors();
	
	if ((tabName=="city") || (tabName=="address")) {
		displayBlock("citySearchFields")
	} else {
		displayNone("citySearchFields");
	}
	
	if (tabName=="city") {
		searchType="city";
		displayBlock("searchByLocation");
		setClass("cityTab", "selected")
	} else {
		displayNone("searchByLocation");
		setClass("cityTab", "")
	}
	
	if (tabName=="address") {
		searchType="address";
		displayBlock("searchByAddress");
		setClass("citySearchFields", "addressSearch");
		setClass("addressTab", "selected");
	} else {
		displayNone("searchByAddress");
		setClass("citySearchFields", "citySearch");
		setClass("addressTab", "");
	}
	if (tabName=="airport") {
		searchType="airportCode";
		displayBlock("airportSearchFields");
		displayBlock("searchByAirportCode");
		setClass("airportTab", "selected");
	} else {
		displayNone("airportSearchFields");
		displayNone("searchByAirportCode");
		setClass("airportTab", "")
	}
	if (tabName=="hotelName") {
		searchType="hotelName";
		displayBlock("hotelNameSearchFields");
		displayBlock("searchByHotelName");
		displayNone("commonSearchFields");
		displayNone("filterSearchFields");
		setClass("hotelNameTab", "selected")
	} else {
		displayNone("hotelNameSearchFields");
		displayNone("searchByHotelName");
		displayBlock("commonSearchFields");
		displayBlock("filterSearchFields");
		setClass("hotelNameTab", "")
	}
	return false;
}

function clear_onClick() {
    f.city.value="";
    if(locale != 'ja_JP' && locale != 'zh_CN'){
    f.streetAddress.value="";
    f.zipCode.value="";
    }
    f.stateProvince.selectedIndex = 0;
	f.country.selectedIndex = 0;
	f.airportCode.value="";
	f.airportRadius.selectedIndex = 2;
	f.hotelName.value="";
    f.numberOfRooms.selectedIndex =0;
    f.numberOfAdults.selectedIndex =0;
    f.arrivalDate.value=dateFormatString;
    f.departureDate.value=dateFormatString;
    if(f.airportRadiusUnit !=null) {
        f.airportRadiusUnit[0].checked = true;
    }
    if(f.units) {
        f.units[0].checked = true;
    }

    for (var i=0;i<f.elements.length;i++) {
		var e=f.elements[i];
		if (e.name=='hotelType') {
			e.checked=false;
		}
		if (e.name=='amenityType') {
			e.checked=false;
		}
        if(e.type=='checkbox') {
            e.checked=false;
        }
    }
}

function setClass(id, _class) {
	document.getElementById(id).className = _class
}
function displayBlock(id){
	document.getElementById(id).style.display="block";
}
function displayNone(id){
	document.getElementById(id).style.display="none";
}

function checkLocationFields() {
	locationStatus = getLocationStatus();
	addressEntered = (f.address.value != "")
	isAddressSearch = (f.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 ((f.city.value =="") && (f.state.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 = (f.city.value =="");
	stateProvinceEmpty = (f.state.value == "");
	countryEmpty = (f.country.value=="");
	countryUS = (f.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 city_onFocus() {
	f.city.value = "";
	if (stateProvinceAutoSet) { f.state.selectedIndex = 0; }
	if (countryAutoSet) { f.country.selectedIndex = 0; }
}

function city_onBlur() {
	a = isIn(f.city.value.toLowerCase(),topCities)
	if (a != false) {
		setDropDown(a[1],f.state);
		setDropDown(a[2],f.country);
		//country_onChange();
		stateProvinceAutoSet = true;
		countryAutoSet = true;
	}
}

function stateProvince_onChange() {
	c = false;
	if(isIn(f.state.value,us_states)) {
		c = "US";
	} else if(isIn(f.state.value,ca_prov)) {
		c = "CA";
	} else if(isIn(f.state.value,misc)) {
		c = "US";
	}
	if (c != false) {
		setDropDown(c,f.country);
	}
	country_onChange();
	stateProvinceAutoSet = false;
}
function country_onChange() {
	if ((f.country[f.country.selectedIndex].value != "US") && (f.country[f.country.selectedIndex].value != "CA")) {
		setDropDown("",f.state);
	}
	stateProvinceAutoSet = false;
	countryAutoSet = false;
}

function setDropDown(v,dd) {
	for (i = 0; i < dd.options.length ; 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 advancedSearch_onClick() {
	f.action='/meetings/search/index.html'
	f.submit();
	return false;
}

addLoadEvent(function () {
});

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function init() {
	setDateToToday(false,dateFormatString); // (default dates boolean, default value)
}
/* ********************************************************************** */
/* ***************        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=200,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;}
}
function getZipCodeStatus() {
	if (document.searchForm.postalCode) {
		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"
		}
	} else {
		return "BLANK";
	}
}
