﻿var xLinkAjaxObj = null;
var currentLocDDL;
var backButtonCheck = false;
var requestedWebsiteLocationNodeId = 0

function populateHotelXLinkDropDown(websiteLocationNodeId) {
    if (websiteLocationNodeId > 0) {        
        requestedWebsiteLocationNodeId = websiteLocationNodeId;
        sendAjaxChildRequest(websiteLocationNodeId);
    }    
}

function sendAjaxChildRequest(locationNodeId) {
    xLinkAjaxObj = createxLinkAjaxObj();

    currentLocDDL = document.getElementById('xlinkform').xlinklocation;
    
    xLinkAjaxObj.onreadystatechange = processXLinkRequest;
    xLinkAjaxObj.open('GET', '/utility/xlinkhotels.aspx?locationid=' + locationNodeId + '&cachekiller=' + cacheKiller(), true);
    xLinkAjaxObj.send('');
}

function processXLinkRequest() {
    if (xLinkAjaxObj.readyState === 4) {
        if (xLinkAjaxObj.status === 200) {            
            if (xLinkAjaxObj.responseXML.getElementsByTagName('Location')) {
                locations = xLinkAjaxObj.responseXML.getElementsByTagName('Location');                
                if (getElement('parentLocationNodeId')) {
                    var parentLocationId = getElement('parentLocationNodeId').value;
                }               
                if (locations.length > 0 && parentLocationId != requestedWebsiteLocationNodeId) {
                    if (!getElement('level2locationddl')) {
                        var subLocationDDL = addDDL('xlinkform', 'level2locationddl', true);
                        subLocationDDL.onchange = function() { populateHotelXLinkDropDown(getElement('xlinkform').level2locationddl.value); };
                    } else {
                        var subLocationDDL = getElement('level2locationddl')
                        clearDDL(subLocationDDL);
                    }           
                    for (var i = 0; i < locations.length; i++) {
                        if (i === 0) {
                            addOption(subLocationDDL, '< Select Location >', '');
                        }
                        var locationName = locations[i].firstChild.lastChild.nodeValue;
                        var locationId = locations[i].getAttribute('LocationId');
                        addOption(subLocationDDL, locationName, locationId);
                    }
                    subLocationDDL.disabled = false;
                    backButtonCheck = false;
                } else {
                    if (backButtonCheck) {
                        currentLocDDL.selectedIndex = 0;
                    } else if (xLinkAjaxObj.responseXML.firstChild.getAttribute('LocationUrl')) {
                        window.location.href = '/hotels/' + xLinkAjaxObj.responseXML.firstChild.getAttribute('LocationUrl');                        
                    }
                    backButtonCheck = false;
                }
            }
        }
        xLinkAjaxObj = null;
    }
}

function addDDL(parentid, id, initStatus) {
    var ddl = document.createElement('select');
    ddl.setAttribute('id', id);
    getElement(parentid).appendChild(ddl);
    ddl.disabled = initStatus;
    return ddl;
}

function addOption(ddl, text, value) {
    var optn = document.createElement('OPTION');
    optn.text = text;
    optn.value = value;
    ddl.options.add(optn);
}

function clearDDL(ddl) {
    ddl.innerHTML = '';
}

function createxLinkAjaxObj() {
    if (window.XMLHttpRequest) {
        // W3C method
        return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        // IE method
        return new ActiveXObject('Microsoft.XMLHTTP');
    } else {
        return;
    }
}

function getElement(elementId){
	if (document.getElementById){
		return document.getElementById(elementId);
	}else{
		return document.all[elementId];
	}
}

function AjaxUnload() {
    xLinkAjaxObj = null;
}

function xLinkBackButtonCheck() {
    if (window.name === 'HotelSearchResults') {
        backButtonCheck = true;
        window.name = '';
        populateHotelXLinkDropDown(getElement('xlinklocation').value);
    }
}

function cacheKiller() {
    var now = new Date();
    return now.getMilliseconds();
}

if (window.addEventListener) {														//EXECUTED ON WINDOW LOAD/UNLOAD EVENT. ATTEMPTS FF/MOZ ETC THEN IE THEN OVERIDES window.onload/unload EVENTS
    window.addEventListener('load', function() { xLinkBackButtonCheck(); }, false);
    window.addEventListener('unload', function() { AjaxUnload(); }, false);
} else if (window.attachEvent) {
    window.attachEvent('onload', xLinkBackButtonCheck);
    window.attachEvent('onunload', AjaxUnload);
} else {
    window.onload = function() { xLinkBackButtonCheck(); };
    window.onunload = function() { AjaxUnload(); };
}