﻿window.onload = initAll;
var xhr = false;
var usUrl = "http://www.adb-airfield.com";
var belgiumUrl = "http://www.adb-air.com";

function initAll() {
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    }
    else {
        if (window.ActiveXObject) {
            try {
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) { }
        }
    }

    if (xhr) {
        xhr.onreadystatechange = function() {
            formatDXML(xhr);
        }
        xhr.open("GET", "xml/map.xml", true);
        xhr.send(null);
    }
    else {
        alert("Sorry, but I couldn't create an XMLHttpRequest");
    }
    // Now lets check to see if the user has been here before.
    getCookie();
}

function formatDXML(info) {

        var data = info.responseXML;
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                var allInfo = data.getElementsByTagName("INFO");
                for (var i = 0; i < allInfo.length; i++) {          
                    var country = allInfo[i].getElementsByTagName("COUNTRY")[0].childNodes[0].nodeValue;
                    var territory = allInfo[i].getElementsByTagName("TERRITORY")[0].childNodes[0].nodeValue;
                    // Update the URL
                    var dURL = "";
                    if (territory == "US") {
                        dURL = usUrl;
                    } else {
                        dURL = belgiumUrl;
                    }
                    try {
                        // Now add to the drop down
                        populateTheDrop(document.drop_list.Country_List, country, dURL)
                    }
                    catch (e) {
                    }
                }
            }else {
                alert("There was a problem with the request " + xhr.status);
            }
        }
}

function populateTheDrop(selectbox, coun, terr) {
    var optn = document.createElement("OPTION");
    optn.text = coun;
    optn.value = terr;
    selectbox.options.add(optn);
}

function getDropText() {
    return dropStr;
}

/*
NON-FLASH DROP DOWN
This function is called when the user makes a selection
from the non-flash based drop down menu.
*/
function loadDsite(whichURL) {
    // Write the cookie
    setCookie(whichURL);
    // Load site
    window.open(whichURL, "_self");
}


/*
COOKIE INIT
This function will check if the user has been to the 
global home page before.  If so then it will read from
the cookie which country (territory) they had chossen
and will automatically forward the user on.
*/
function getCookie() {
    // Check to see if the user has come to the site from one of the "global" buttons on the US and BE sites.
    var fromGlobal = false;
    try {

        var isIt = getValue("page");
        if (isIt == "true") {
            fromGlobal = true;
        }
    }
    catch (e) { }
    if (fromGlobal == false) {
        // Init var
        var choosenTerritory = "";
        // Get cookie value
        choosenTerritory = cookieVal("choosenTerritory");
        // Check if the user should be directed to a site
        if (choosenTerritory != "") {
            window.open(choosenTerritory, "_self");
        }
    }
}	

/*
SET THE COOKIE
This will take what territory the user selected and write
this into the cookie which will be saved for 6 months.
TODO: Get this working for the flash map as well.
*/
function setCookie(durl) {
    var expireDate = new Date();
    expireDate.setMonth(expireDate.getMonth() + 6);
    document.cookie = "choosenTerritory=" + durl + ";expires=" + expireDate;
}

/*
GET THE COOKIE VALUE
Loops through all the cookies and finds the one we need.
*/
function cookieVal(cookieName) {
    var thisCookie = document.cookie.split(";");
    for (var i = 0; i < thisCookie.length; i++) {
        if (cookieName == thisCookie[i].split("=")[0]) {
            return thisCookie[i].split("=")[1];
        }
    }
    return 0;
}


function getValue(varname) {
    // First, we load the URL into a variable
    var url = window.location.href;

    // Next, split the url by the ?
    var qparts = url.split("?");

    // Check that there is a querystring, return "" if not
    if (qparts.length == 0) {
        return "";
    }

    // Then find the querystring, everything after the ?
    var query = qparts[1];

    // Split the query string into variables (separates by &s)
    var vars = query.split("&");

    // Initialize the value with "" as default
    var value = "";

    // Iterate through vars, checking each one for varname
    for (i = 0; i < vars.length; i++) {
        // Split the variable by =, which splits name and value
        var parts = vars[i].split("=");

        // Check if the correct variable
        if (parts[0] == varname) {
            // Load value into variable
            value = parts[1];

            // End the loop
            break;
        }
    }

    // Convert escape code
    value = unescape(value);

    // Convert "+"s to " "s
    value.replace(/\+/g, " ");
    //alert("value: " + value);
    // Return the value
    return value;
}