﻿function processLocationData(responseText) {
	eval(responseText);
	return words;
}

function getLocationValue(item) {
	document.searchfrm.keyword.value = item.lastChild.innerHTML;
	return item.firstChild.innerHTML;
}

function getLocationText(item) {
	return item.firstChild.innerHTML
}

function createLocationItem(item) {
	var wd = document.createElement("DIV");

	var city = document.createElement("SPAN");
	city.className = 'city';
	
	if (item[1] != "") {
		city.innerHTML = item[0] + ", " + item[1];
	}else {
		city.innerHTML = item[0];
	};
	
	wd.appendChild(city);
	
	var url = document.createElement("SPAN");
	url.className = 'url';
	url.innerHTML = item[2];
	wd.appendChild(url);

	return wd;
}

var config = new suggestion.Config();
config.scriptURL = '/remote/getlocation.aspx?word=';
config.className = 'suggestDiv';
config.itemHoverClass = 'bAutocomplete';
config.itemNormalClass = 'aAutocomplete';
config.processResponse = processLocationData;
config.createItem = createLocationItem;
config.getItemValue = getLocationValue;
config.getItemText = getLocationText;
config.maxItem = 10;
config.mode = suggestion.PartialWord;
config.minWordLength = 2;

var locationSuggest;

function OnLoad() {
	locationSuggest = new suggestion('location',document.searchfrm.txtSearch,config);
}

//suggestion.addEvent(window,"load",OnLoad);

function ValidateSearch(frm) {
    var intError = 0;
    var accommodationType = 'All';
    //alert(frm.txtSearch.value + "  " + frm.cmbMonthYear.value + "/" + frm.cmbDay.value);
    
    if (!isValidDate(frm.cmbDay.options[frm.cmbDay.selectedIndex].value + '/' + frm.cmbMonthYear.options[frm.cmbMonthYear.selectedIndex].value)) {
        frm.cmbDay.parentNode.className = "error";
        alert('invalid date');
        intError = 1;
    }else {
        frm.cmbDay.parentNode.className = "";
    }
    
    if (isEmpty(frm.txtSearch.value)) {
        intError = 1;
        alert('Please enter a keyword to search');
    }
    
    if (!isValidHTMLInput(frm.txtSearch.value)) {
        intError = 1;
        alert('Keyword is invalid');
    }
    
    if ((!(frm.chkHotel.checked || frm.chkApartment.checked))&& (intError != 1)) {
        alert('Please specify type of accommodation to search');
        intError = 1;
    }
    else {
        var result = 0;
        if (frm.chkHotel.checked) result += parseInt(frm.chkHotel.value);
        if (frm.chkApartment.checked) result += parseInt(frm.chkApartment.value);
        switch (result) {
            case 1:
                accommodationType = 'Hotel';
                break;
            case 2:
                accommodationType = 'Apartment';
                break;
        }
    }
    
    if (intError)
        return false;
    else {
        //AdvantagePlus warning
        if (gSite && gSite.advMember) gSite.advPlusWarning();

        if (!isEmpty(frm.keyword.value))
            window.location.href = frm.action + '?' + frm.keyword.value + '&date=' + frm.cmbDay.options[frm.cmbDay.selectedIndex].value + '/' + frm.cmbMonthYear.options[frm.cmbMonthYear.selectedIndex].value + '&mm=' + frm.mm.value + '&booking=' + frm.booking.value + '&accommodation=' + accommodationType;
        else {
            window.location.href = frm.action + '?date=' + frm.cmbDay.options[frm.cmbDay.selectedIndex].value + '/' + frm.cmbMonthYear.options[frm.cmbMonthYear.selectedIndex].value + '&keyword=' + frm.txtSearch.value + '&mm=' + frm.mm.value + '&booking=' + frm.booking.value + '&accommodation=' + accommodationType;
        }
        return false;
    }
}

function ValidateSearch4Location(frm, prefix) {
    var intError = 0;
    var accommodationType = 'All';
    var country, countryText;
    var city, cityText;
    
    if (eval("frm." + prefix + "$selCountries.selectedIndex") == 0) {
        alert('Please select a country to search');
        intError = 1;
    }
    
    if (!isValidDate(frm.cmbDay4Location.options[frm.cmbDay4Location.selectedIndex].value + '/' + frm.cmbMonthYear4Location.options[frm.cmbMonthYear4Location.selectedIndex].value)) {
        frm.cmbDay4Location.parentNode.className = "error";
        alert('invalid date');
        intError = 1;
    }else {
        frm.cmbDay4Location.parentNode.className = "";
    }
            
    if((!(frm.chkHotel.checked || frm.chkApartment.checked)) && (intError != 1)) {
        alert('Please specify type of accommodation to search');
        intError = 1;
    }
    else {
        var result = 0;
        if (frm.chkHotel.checked) result += parseInt(frm.chkHotel.value);
        if (frm.chkApartment.checked) result += parseInt(frm.chkApartment.value);
        switch (result) {
            case 1:
                accommodationType = 'Hotel';
                break;
            case 2:
                accommodationType = 'Apartment';
                break;
        }
    }
    
    if (intError)
        return false;
    else {        
        //AdvantagePlus warning
        if (gSite && gSite.advMember) gSite.advPlusWarning();    
    
        country = eval("frm." + prefix + "$selCountries").value;
        if (frm.cmbSearchLocation.value.lastIndexOf("_all") == -1) {
            city = frm.cmbSearchLocation.value;
        } else {
            city = "";
        }

        if (country == "OTHER") {
            window.location.href = "http://www.accorhotels.com";
        } else {   
            //Original               
            window.location.href = frm.action + '?date=' + frm.cmbDay4Location.options[frm.cmbDay4Location.selectedIndex].value + '/' + frm.cmbMonthYear4Location.options[frm.cmbMonthYear4Location.selectedIndex].value + '&country=' + country + '&city=' + city + '&mm=' + frm.mm.value + '&booking=' + frm.booking.value + '&accommodation=' + accommodationType;
        } 
        
        return false;
    }
}