// Ref: http://devzone.skillfusion.com/ajaxArticle10.php
// This method (asynchronous xslt load) works in Firefox and Opera
// but not Safari

var xslloaded = false;
var xsldoc;

// Initialisation function - runs on page load
// When the page first loads, this function will run. This function will load
// the XSLT file in asynchronously. A callback function (loadComplete(), see
// below) will run as soon as the XSLT has been loaded in.
function init()
{
    // Load the XSLT in Firefox
    // Note that the loadComplete callback function will run when the XSLT
    // has been loaded.
    // We have to create an empty XML document and load the data into it.
    if(window.XSLTProcessor)
    {
        // Create an empty XML document
        xsldoc = document.implementation.createDocument("","",null);

        // Tell it to run the callback when it's completely loaded in.
        xsldoc.addEventListener("load",loadComplete,false);

        // Load the XSL into the empty document.
        // The callback will run as soon as the XSLT is loaded.
        xsldoc.load("flights3a.xsl");
    }
    // Do the same thing the Microsoft way
    else if (window.ActiveXObject)
    {
        xsldoc = new ActiveXObject("Microsoft.XMLDOM");
        xsldoc.ondataavailable = loadComplete;
        xsldoc.load("flights3a.xsl");
    }
    else
    {
        alert("Your browser can't handle this!");
    }
}

// This callback function runs when the XSLT has been loaded.
// We set a variable "xslloaded" to true, so that the main AJAX callback
// (below) can check that the XSLT has been loaded before trying to 
// apply it.
function loadComplete()
{
    xslloaded=true;
}

// Function which sends an AJAX request to retrieve all flights.
// Runs when the user clicks the button on the HTML page.
// The AJAX callback, below, will run when we get a response from the server.
function go()
{
    var request = new Ajax.Request
        ("http://www.free-map.org.uk/course/xsltproc/flights.php",
                {method:'get',
                parameters:'',
                onComplete:callback }
        );
}

// AJAX callback
// Runs when we get the XML data back from the server.
function callback(xmlHTTP)
{
    statusMsg('responseText='+xmlHTTP.responseText);

    // We need to check whether the XSL was loaded - the xsl was loaded
    // asynchronously (see above)
    if(xslloaded)
    {
        // Apply the XSLT, the same way as before.
        if (window.XSLTProcessor)
        {
            var processor = new XSLTProcessor();

            // Import the XSLT document loaded in above.
            processor.importStylesheet(xsldoc);
            var output = processor.transformToFragment
                    (xmlHTTP.responseXML, document);
            var result=document.getElementById("result");
            result.innerHTML="";
            result.appendChild(output);
        }
        else if (window.ActiveXObject)
        {
            var html = xmlHTTP.responseXML.transformNode(xsldoc);
            var result=document.getElementById("result");
            result.innerHTML = html;
        }    
        else
        {
            alert("your browser can't handle this!");
        }
    }
}


function statusMsg(msg)
{
    document.getElementById('status').innerHTML = msg;
}

