﻿var xmlhttp;
// Call our AJAX handler for tour xml data
function loadXMLDoc(city) {
    xmlhttp = null;
    if (window.XMLHttpRequest) {// code for all new browsers
        xmlhttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {// code for IE5 and IE6
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (xmlhttp != null) {
        xmlhttp.onreadystatechange = state_Change;
        xmlhttp.open("GET", "/AJAXDBHandler.ashx?city=" + city, true); // Change to point to the correct location of file
        xmlhttp.send(null);
    }
    else {
        alert("Your browser does not support XMLHTTP.");
    }
}

// Handle our AJAX request
function state_Change() {
    var xmldoc;
    var tourTxt, tourNodes, tourNode;
    var actTxt, actNodes, actNode;
    var attTxt, attNodes, attNode;
    var pakTxt, pakNodes, pakNode;
    var text;
    if (xmlhttp.readyState == 4) {
        if (xmlhttp.status == 200) {
            xmldoc = xmlhttp.responseXML;
            
            // Set up the tour information
            tourTxt = "<table class=tours>";
            tourNodes = xmldoc.getElementsByTagName("tours")[0];
            for (var iNode = 0; iNode < tourNodes.childNodes.length; iNode++) {
                tourNode = tourNodes.childNodes.item(iNode);
                tourTxt += "<tr><td><a href=\"tour.aspx?ID=" + normaliseResult(tourNode.getElementsByTagName("tourid")[0]) + "\">"
                            + normaliseResult(tourNode.getElementsByTagName("name")[0]) + "</a></td></tr>";
            }
            tourTxt += "</table>";
            
            // Set up the activity information
            actTxt = "<table class=activities>";
            actNodes = xmldoc.getElementsByTagName("activities")[0];
            for (var iNode = 0; iNode < actNodes.childNodes.length; iNode++) {
                actNode = actNodes.childNodes.item(iNode);
                actTxt += "<tr><td><a href=\"tour.aspx?ID=" + normaliseResult(actNode.getElementsByTagName("tourid")[0]) + "\">"
                            + normaliseResult(actNode.getElementsByTagName("name")[0]) + "</a></td></tr>";
            }
            actTxt += "</table>";
            
            // Set up the attraction information
            attTxt = "<table class=attractions>";
            attNodes = xmldoc.getElementsByTagName("attractions")[0];
            for (var iNode = 0; iNode < attNodes.childNodes.length; iNode++) {
                attNode = attNodes.childNodes.item(iNode);
                attTxt += "<tr><td><a href=\"tour.aspx?ID=" + normaliseResult(attNode.getElementsByTagName("tourid")[0]) + "\">"
                            + normaliseResult(attNode.getElementsByTagName("name")[0]) + "</a></td></tr>";
            }
            attTxt += "</table>";
            
            // Set up the Packages information
            pakTxt = "<table class=packages>";
            pakNodes = xmldoc.getElementsByTagName("packages")[0];
            for (var iNode = 0; iNode < pakNodes.childNodes.length; iNode++) {
                pakNode = pakNodes.childNodes.item(iNode);
                pakTxt += "<tr><td><a href=\"tour.aspx?ID=" + normaliseResult(pakNode.getElementsByTagName("tourid")[0]) + "\">"
                            + normaliseResult(pakNode.getElementsByTagName("name")[0]) + "</a></td></tr>";
            }
            pakTxt += "</table>";
            
            
            // Update the right hand fields with the new information
            document.getElementById('tour_info').innerHTML = tourTxt;
            document.getElementById('act_info').innerHTML = actTxt;
            document.getElementById('att_info').innerHTML = attTxt;
            document.getElementById('pak_info').innerHTML = pakTxt;            
        }
        else {
            alert("Problem retrieving XML data:" + xmlhttp.statusText);
        }
	} else if (xmlhttp.readyState != 0) {
        document.getElementById('tour_info').innerHTML = "<img src='/Google/images/ajax-loader.gif'> Loading";
        document.getElementById('act_info').innerHTML = "<img src='/Google/images/ajax-loader.gif'> Loading";
        document.getElementById('att_info').innerHTML = "<img src='/Google/images/ajax-loader.gif'> Loading";
        document.getElementById('pak_info').innerHTML = "<img src='/Google/images/ajax-loader.gif'> Loading";
    }
}

// Return the correct node result for browser compatibility
function normaliseResult(node)
{
    if (typeof node.textContent != 'undefined') {
        return(node.textContent);
    }
    else if (typeof node.text != 'undefined') {
        return(node.text);
    }
    else {
        return("Error gathering XML data, please notify site admin.");
    }
}
