﻿// JScript File

// global flag
var isIE = false;

// global request and XML document objects
var HttpRequest;

var defaultAirportEntry = "Select airport";

function clearOptions(id)
{
   	var selectObj = document.getElementById(id);
    selectObj.options.length=0;
}

// invoked by "originAirports" select element change;
// loads chosen XML document, clears "destinationAirports" select
// element, loads new items into "destinationAirports" select element
function originAirportChangeHandler(originairportselect) {

    try {
        if (originairportselect.selectedIndex > 0) {      
            getXMLDocument("charterflights/CharterFlightsHandler.ashx?depid=" + originairportselect.options[originairportselect.selectedIndex].value, "destinationAirports");
        }	
    } catch(e) {
        var msg = (typeof e == "string") ? e : ((e.message) ? e.message : "Unknown Error");
        alert("Unable to get XML data:\n" + msg);
        return;
    }
}


function getXMLDocument(url, select) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        HttpRequest = new XMLHttpRequest();
        HttpRequest.onreadystatechange = function(){onReadyStateChangeHandler(select)};
        HttpRequest.open("GET", url, true);
        HttpRequest.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        HttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
        if (HttpRequest) {
            HttpRequest.onreadystatechange = function(){onReadyStateChangeHandler(select)};
            HttpRequest.open("GET", url, true);
            HttpRequest.send();
        }
    }
}



// handle onreadystatechange event of HttpRequest object
function onReadyStateChangeHandler(select) {
    // only if HttpRequest shows "loaded"
    if (HttpRequest.readyState == 4) {
        // only if "OK"
        if (HttpRequest.status == 200) {
            clearOptions(select);
            buildAirportList(select);
         } else {
            alert("There was a problem retrieving the XML data:\n" + HttpRequest.statusText);
         }
    }
}


function switchToCharterFlights() {
   
   var destinationAirportsdll = document.getElementById("destinationAirports");
   var originAirportsdll = document.getElementById("originAirports");

   if (document.getElementById("chkCharterFlights").checked) {
        
        clearOptions("originAirports");
        clearOptions("destinationAirports");
        
        originAirportsdll.options[0] = new Option(defaultAirportEntry, "", true, true);
        destinationAirportsdll.options[0] = new Option(defaultAirportEntry, "", true, true);

        getXMLDocument("charterflights/CharterFlightsHandler.ashx", "originAirports");
        
        originAirportsdll.onchange = function(){originAirportChangeHandler(originAirportsdll)}; 

   } else {
   
        clearOptions("originAirports");
        clearOptions("destinationAirports");
        onLoadFunctions();
        destinationAirportsdll.options[0] = new Option(defaultAirportEntry, "", true, true);
   
   }

}

// fill Topics select list with items from
// the current XML document
function buildAirportList(select) {
    var select = document.getElementById(select);
    var items = HttpRequest.responseXML.getElementsByTagName("item");
    // loop through <item> elements, and add each nested
    // <title> element to Topics select element
    select.options[0] = new Option(defaultAirportEntry, "", true, true);
    for (var i = 0; i < items.length; i++) {
        appendToSelect(select, document.createTextNode(getElementText("", "value", items[i], 0)).nodeValue, document.createTextNode(getElementText("", "text", items[i], 0)));
    }

}


// retrieve text of an XML document element, including
// elements using namespaces
function getElementText(prefix, local, parentElem, index) {
    var result = "";
    if (prefix && isIE) {
        // IE/Windows way of handling namespaces
        result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
    } else {
        // the namespace versions of this method 
        // (getElementsByTagNameNS()) operate
        // differently in Safari and Mozilla, but both
        // return value with just local name, provided 
        // there aren't conflicts with non-namespace element
        // names
        result = parentElem.getElementsByTagName(local)[index];
    }
    if (result) {
        // get text, accounting for possible
        // whitespace (carriage return) text nodes 
        if (result.childNodes.length > 1) {
            return result.childNodes[1].nodeValue;
        } else {
            return result.firstChild.nodeValue;    		
        }
    } else {
        return "n/a";
    }
}

// add item to select element the less
// elegant, but compatible way.
function appendToSelect(select, value, content) {
    var opt;
    opt = document.createElement("option");
    opt.value = value;
    opt.appendChild(content);
    select.appendChild(opt);
}

