﻿//Station name autocomplete
//AJAX script 
//RRowson Sep 2008
//Version 1.3
//Version 1.3 addresses onkeyup behaviour in Safari and Chrome, and prevent submit


//change mainform to be the name of the form being used
// pass in the input field id and the "event" (the key being pressed)
function updatefield(fieldref, evt) {
    //this function called everytime a key is pressed in the main field
    
    // I really don't understand why the hell they are appending "field", "dropdown" etc.
    var mainfield = fieldref + "field";
    var selectfield = fieldref + "dropdown";
    var hiddenfield = fieldref + "hidden";
    var divfield = fieldref + "select";

    // associate some consts with events
    var RETURN = 13;
    var TAB = 9;
    var ESC = 27;
    var ARROWUP = 38;
    var ARROWDN = 40;
    var key;

    // Get the value of the field
    var letters = document.getElementById(mainfield).value;

    // If we have an event we get the key the pressed
    // they seem to be using window.event after due to browser compatability (IE)
    if (evt) {
        var key = evt.keyCode ? evt.keyCode : evt.charCode;
    }
    if (window.event) {
        var key = (window.event) ? window.event.keyCode : ev.keyCode;
    }

    // If they have enetered a letter which isn't RETURN or TAB then do a lookup on the value entered (letters)
    if ((key != RETURN) && (key != TAB)) {
        if (letters.length > 1) lookup(fieldref);
    }

    // If they have hit the down arrow key we will show the drop down
    if (key == ARROWDN) {
        document.getElementById(hiddenfield).value = "1";
        showdropdown(divfield);
        document.getElementById(selectfield).focus();
    }

    // If they have hit the ESC key we will hide the dropd own
    if (key == ESC) {
        cleardropdown(divfield);
        document.getElementById(hiddenfield).value = "0";
    }

    // If they hit TAB we check to see if there are two or less letters and remove the ddl. If not we do nothing
    if (key != TAB) {
        if (letters.length < 2) {
            cleardropdown(divfield);
            document.getElementById(hiddenfield).value = "0";
        }
    }
}

function updatedropdown(fieldref, evt) {
    //this function called everytime a key is pressed in the drop down list
    
    var mainfield = fieldref + "field";
    var selectfield = fieldref + "dropdown";
    var divfield = fieldref + "select";
    var hiddenfield = fieldref + "hidden";
    var RETURN = 13;
    var TAB = 9;
    var ESC = 27;
    var ARROWUP = 38;
    var ARROWDN = 40;

    if (evt) {
        var key = evt.keyCode ? evt.keyCode : evt.charCode;
    }
    if (window.event) {
        var key = (window.event) ? window.event.keyCode : ev.keyCode;
    }

    if (key == RETURN) {
        document.getElementById(mainfield).focus();
        bi = document.getElementById(selectfield).selectedIndex;
        if (bi > -1) document.getElementById(mainfield).value = document.getElementById(selectfield).options[bi].value;
        document.getElementById(hiddenfield).value = "0";
        cleardropdown(divfield);
        document.getElementById(mainfield).focus();

    }
    if (key == ESC) {
        document.getElementById(mainfield).focus();
    }
    if (key == TAB) {
        document.getElementById(mainfield).focus();
        bi = document.getElementById(selectfield).selectedIndex;
        if (bi > -1) document.getElementById(mainfield).value = document.getElementById(selectfield).options[bi].value;
        document.getElementById(hiddenfield).value = "0";
        cleardropdown(divfield);
        document.getElementById(mainfield).focus();
    }
}


function updatedropdownclick(fieldref) {
    //this function called everytime the mouse clicks on the drop down

    var mainfield = fieldref + "field";
    var selectfield = fieldref + "dropdown";
    var divfield = fieldref + "select";
    var hiddenfield = fieldref + "hidden";

    document.getElementById(mainfield).focus();
    b = document.getElementById(selectfield).selectedIndex;
    document.getElementById(mainfield).value = document.getElementById(selectfield).options[b].value;
    document.getElementById(hiddenfield).value = "0";
    cleardropdown(divfield);
}


function lookup(fieldref) {
    //this function called when needing to get XML data

    var hiddenfield = fieldref + "hidden";
    var mainfield = fieldref + "field";

    //get letters from the form
    var letters = document.getElementById(mainfield).value.toLowerCase();
    var firstletter = letters.substr(0, 1);

    //set the URL to check
    var url = "xml/Transform.aspx?file=" + firstletter + "_stations.xml";
    //var url = "xml/xmlfull/" + firstletter + "_stations.xml";

    //set-up the xml feed
    var httpRequest;
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        httpRequest = new XMLHttpRequest();
        if (httpRequest.overrideMimeType) {
            httpRequest.overrideMimeType('text/xml');
        }
    }
    else if (window.ActiveXObject) { // IE
        try {
            httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) { }
        }
    }

    if (!httpRequest) {
        //alert('Cannot connect to station data');
        return false;
    }

    // This is our callback function
    httpRequest.onreadystatechange = function() { dataReceived(httpRequest, fieldref); };

    // create the request
    httpRequest.open('GET', url, true);
    httpRequest.send(null);

}

function dataReceived(httpRequest, fieldref) {
    //this function called once data received ok

    var mainfield = fieldref + "field";
    var selectfield = fieldref + "dropdown";
    var divfield = fieldref + "select";
    var hiddenfield = fieldref + "hidden";
    if (httpRequest.readyState == 4) {
        // everything is good, the response is received
        if (httpRequest.status == 200) {
            // perfect
            //alert(httpRequest.responseText);
            var xmldoc = httpRequest.responseXML;
            var root_node = xmldoc.getElementsByTagName('NumberOfResults').item(0);
            j = root_node.firstChild.data;
            k = 0;
            var letters = document.getElementById(mainfield).value.toLowerCase();
            l = letters.length;
            for (i = 0; i < j; i++) {
                var root_node = xmldoc.getElementsByTagName('stationName').item(i);
                m = root_node.firstChild.data;
                minfo = root_node.getAttribute('info');
                crs = root_node.getAttribute('crs');
                if (minfo == "") minfo = m;
                if (crs < 999) crs = 1;
                if (crs != 1) minfo = minfo + " [" + crs + "]";
                n = m.substr(0, l);
                if (letters.toUpperCase() == n) {
                    newoption = new Option(minfo, m, false, false);
                    document.getElementById(selectfield).options[k] = newoption;
                    k = k + 1;
                    if (k > 25) i = j;
                }
                //next bits handles the London lookups
                if ("LONDON " + letters.toUpperCase() == m.substr(0, (l + 7))) {
                    newoption = new Option(minfo, m, false, false);
                    document.getElementById(selectfield).options[k] = newoption;
                    k = k + 1;
                    if (k > 25) i = j;
                }
                //next bits handles the CRS code lookups
                if (letters.toUpperCase() == crs) {
                    newoption = new Option(minfo, m, false, false);
                    document.getElementById(selectfield).options[k] = newoption;
                    k = k + 1;
                    if (k > 25) i = j;
                }
            }
            for (i = k; i < 26; i++) {
                newoption = new Option("", "", false, false);
                document.getElementById(selectfield).options[i] = newoption;
            }
            if (j == 1) {
                if (document.getElementById(selectfield).options[0].value.toLowerCase() == document.getElementById(mainfield).value.toLowerCase()) cleardropdown(divfield);
            }
            if (k > 0) {
                //make the dropdown visible
                showdropdown(divfield);
            }
        } else {
            // there was a problem with the request,
            // for example the response may be a 404 (Not Found)
            // or 500 (Internal Server Error) response codes
            return false;
        }
    } else {
        // still not ready
        return false;
    }


}


function lostfoc(fieldref) {
    //this function hides the drop down if the user clicks away
    //wait 200ms though incase they are clicking on the drop down itself
    var hiddenfield = fieldref + "hidden";
    if (document.getElementById(hiddenfield).value == "0") {
        if (fieldref == 'to') setTimeout("cleardropdownconditional('to')", 250);
        if (fieldref == 'from') setTimeout("cleardropdownconditional('from')", 250);
    }

}


function updatefocus(fieldref) {
    //this function hides the drop down if the user highlights on the dropdown - to prevent it going away
    var hiddenfield = fieldref + "hidden";
    document.getElementById(hiddenfield).value = "1";

}

function cleardropdownconditional(fieldref) {
    var hiddenfield = fieldref + "hidden";
    var divfield = fieldref + "select";
    if (document.getElementById(hiddenfield).value == "0") {
        document.getElementById(divfield).style.display = "none";
    }
}


//function cleardropdown(itemname) {
//    document.getElementById(itemname).style.display = "none";
//    setTimeout("document.getElementById('mainform').onsubmit = new Function('');", 250);
//}

//function showdropdown(itemname) {
//    document.getElementById(itemname).style.display = "block";
//    document.getElementById('mainform').onsubmit = new Function('return false;');
//}

function cleardropdown(itemname) {
    document.getElementById(itemname).style.display = "none";
    setTimeout("document.getElementById('ctl00_BuyYourTickets_imgbutFindTrains').onsubmit = new Function('');", 250);
}

function showdropdown(itemname) {
    document.getElementById(itemname).style.display = "block";
    document.getElementById('ctl00_BuyYourTickets_imgbutFindTrains').onsubmit = new Function('return false;');
}


 
