
function getCountry() {
  return document.searchForm['country'].value;
}

function getRegionId() {
  return document.searchForm['region'].value;
}

/* If the country is not specified in the querystring, deselect it from the country drop-down. */
function updateCountryDropdown() {
    var url = window.location.href;
    if (url.indexOf('?') != -1) {
        document.searchForm['country'].value = url.substring(url.indexOf('=')+1, url.length);
    } else {
        var countryDropDown = document.searchForm['country'];
        // deselect country for IE and then for FireFox
        //countryDropDown.options[countryDropDown.selectedIndex].selected = '';
        //countryDropDown.value = null;
        countryDropDown.selectedIndex = -1;
        countrySelected = false;
    }
}

var countrySelected = true;

function country_onChange() {
    document.searchForm['region'].selectedIndex = -1;
    var url = window.location.href;
    if (url.indexOf('?') != -1) {
        currentCountryId = url.substring(url.indexOf('=')+1, url.length);
    }
    var countryIsoCode = getCountry();
    if (countryIsoCode != currentCountryId)
    {
        location="?country=" + countryIsoCode;
    } else {
        if (countrySelected) {
            document.searchForm['country'].selectedIndex = -1;
            countrySelected = false;
        } else {
            countrySelected = true;
        }
    }
}

function region_onChange() {
    if (document.searchForm['country'].value == '')
    {
        document.searchForm['country'].value = currentCountryId;
        countrySelected = true;
    }
}
        
function doDestinationSearch() {
    if (checkForDestination()) {
        document.searchForm.action = "search/Advanced";
        document.searchForm.submit();
    }
}

function doTextSearch() {
    if (validateTextSearch()) {
        document.searchForm.action = "search/TextSearch";
        document.searchForm.submit();
    }
}

function validateTextSearch() {
    var searchTerms = trim(document.searchForm['searchTerms'].value);
    if (searchTerms.length == 0) {
        alert('Please specify a search term');
        return false;
    } else if (searchTerms.length < 2) {
        alert('Please specify at least two characters');
        return false;
    }
    return true;
}

function trim(theString) {
    theString = theString.replace( /^\s+/g, "" );// strip leading
    return theString.replace( /\s+$/g, "" );// strip trailing
}

function setCheckboxParameters(field, checked, value)
{
    field.checked = checked;
    if (checked) field.value = value;
    else field.value = "";
}

function doSearch() {
    if (document.searchForm["searchTerms"] &&
        document.searchForm["searchTerms"].value != "") {
        doTextSearch();
    } else {
        doDestinationSearch();
    }
}

function checkForDestination() {
    if (document.searchForm["region"].value == "" ||
            document.searchForm["region"].value == "null" ||
            document.searchForm["region"].selectedIndex < 0 ) {
        alert("Please select a destination.");
    	document.searchForm["region"].focus();
        return false;
    }
    return true;
}

function selectSuggestion (text, li) {
    document.searchForm["selectedSuggestionId"].value = li.id;
}