var getStates, getCities;
var selectedElement = "";
var loadingText = "";

// Function call to load state data
function onselect_Country(countryCode) {
    getStates = true;
    getCities = false;
    selectedElement = document.getElementById("stateSelect");
    handleLoading(selectedElement);
    buildDataArrays(countryCode);
}

// Function call to load city data
function onselect_State(countryCode, stateCode) {
    getStates = false;
    getCities = true;
    selectedElement = document.getElementById("citySelect");
    handleLoading(selectedElement);
    buildDataArrays(countryCode, stateCode);
}

// Function call to submit city data
function onselect_City(destinationId) {
    location.href = "/" + skin + "/destinations/city/overview.html?destinationID=" + destinationId;
}

// Function call to submit 10best data if destination service is unavailable
function onselect_10bestCity(url) {
    location.href = url;
}

// Function to handle drop down loading
function handleLoading(element) {
    var optionNode = document.createElement("option");
    element.disabled = (element.disabled == false)?true:false;
    if (element.disabled) {
        destroyDOMObjects(element, 0);
        optionNode.innerHTML = loadingText;
        element.appendChild(optionNode);
    } else {
        destroyDOMObjects(element, 0);
        if (element.id == 'stateSelect'){
            optionNode.innerHTML = stateOption;
        } else if (element.id == 'citySelect'){
            optionNode.innerHTML = cityOption;
        }
        element.appendChild(optionNode);
    }
}

function destroyDOMObjects(id,numToDestroy) {
    if (id != null && id != ""){
        var objectsToDestroy = id.getElementsByTagName("option");
        var numOfObjsToDestroy = objectsToDestroy.length;
        if (numToDestroy == 0) {
            for (i=0; i<numOfObjsToDestroy; i++) {
                id.removeChild(objectsToDestroy[0]);
            }
        } else {
            for (var i=1; i<numOfObjsToDestroy; i++) {
                if (objectsToDestroy[1].value != ''){
                    id.removeChild(objectsToDestroy[1]);
                }
            }
        }
    }
}

function buildDataArrays(countryCode, stateCode) {
    if (locations != null) {
        var stateArray = new Array();
        var cityArray = new Array();
        for (var i=0; i < locations.countries.length; i++){
            if (locations.countries[i].states != null && locations.countries[i].code == countryCode) {
                for (var j=0; j < locations.countries[i].states.length; j++) {
                    if (locations.countries[i].states[j] != null) {
                        stateArray[j] = locations.countries[i].states[j].name + ":" + locations.countries[i].states[j].name; //temporarily place state name in both locations until Sherlock returns stateCode
                        if (locations.countries[i].states[j].cities != null && (locations.countries[i].states[j].name == stateCode || stateCode == "")) {
                            for (var x=0; x < locations.countries[i].states[j].cities.length; x++) {
                                if (locations.countries[i].states[j].cities[x] != null) {
                                    cityArray[x] = locations.countries[i].states[j].cities[x].code + ":" + locations.countries[i].states[j].cities[x].name;
                                }
                            }
                        }
                    }
                }
                if (getStates){
                    buildDropdown(stateArray);
                } else {
                    buildDropdown(cityArray);
                }
            }
        }
    }
}

function buildDropdown(ddArray) {
    handleLoading(selectedElement); //remove the loading dialog and the dropdown disabling

    // build the DOM
    for (i=0; i<ddArray.length; i++) {
        var keyValueArray = new Array();
        keyValueArray = ddArray[i].split(":");

        var optionNode = document.createElement("option");
        optionNode.setAttribute("value", keyValueArray[0]);
        optionNode.innerHTML = keyValueArray[1];

        selectedElement.appendChild(optionNode);
    }

    if(getStates) {
        destroyDOMObjects(document.getElementById("citySelect"));
        if (keyValueArray[0] == "") { //the selection doesn't have any states, populate the city dropdown
            document.getElementById("stateSelect").style.display = "none";
            onselect_State(document.locationForm.country_select[document.locationForm.country_select.selectedIndex].value, "");
        } else {
            document.getElementById("stateSelect").style.display = "block";
        }
    }
}
