﻿var notFoundMsg = "Not found...";

function locateFromPostcode(locationID, coordinatesID, buttonIDs, href) {

    // validate location input object and show a message
    if (locationID == null) return;
    var location = document.getElementById(locationID);
    if (location == null) return;
    var postcode = location.value;

    // validate post code variable
    if (postcode == null) return;
    postcode = postcode.trim();
    if (postcode == null || postcode == "" || postcode == "Anywhere" || postcode == notFoundMsg || postcode == notFoundMsg.toUpperCase()) return;
    location.value = "Working...";    

    // validate buttonIDs and disable then
    var buttons = null;
    if (buttonIDs != null & buttonIDs != '') {
        buttons = buttonIDs.split("|");
        for (var i = 0; i < buttons.length; i++) {
            var button = document.getElementById(buttons[i]);
            if (button != null) disableAnchor(button, true);
        }
    }    

    // validate coordinatesID variable
    var input = document.getElementById(coordinatesID);
    if (input == null) return;


    var localSearch = new GlocalSearch();
    localSearch.setSearchCompleteCallback(null,
            function() {

                if (localSearch.results[0]) {
                    var resultLat = localSearch.results[0].lat;
                    var resultLng = localSearch.results[0].lng;
                    var point = new GLatLng(resultLat, resultLng);

                    input.value = point.lat() + "," + point.lng();
                    location.value = postcode;
                    if (href != null) window.location = href;
                    
                    if (buttons != null) {
                        for (var i = 0; i < buttons.length; i++) {
                            var button = document.getElementById(buttons[i]);
                            if (button != null) disableAnchor(button, false);
                        }
                    }

                } else {
                    location.value = notFoundMsg;
                }
            });
     
     localSearch.execute(postcode + ", UK");
}

function disableAnchor(obj, disable) {

    // if disable is TRUE then move the href to a backup attribute
    if (disable) {
        var href = obj.getAttribute("href");
        if (href == null || href == "") return;
        var href_bak = obj.getAttribute("href_bak");
        if (href_bak != null && href_bak != "") return;

        obj.setAttribute('href_bak', href);
        obj.removeAttribute('href');
        obj.style.cursor = "Wait";
    }
    
    // if not disable then restore the href from the backup attribute
    else {
        var href_bak = obj.getAttribute("href_bak");
        if (href_bak == null || href_bak == "") return;
        var href = obj.getAttribute("href");
        if (href != null && href != "") return;
        
        obj.setAttribute('href', obj.attributes['href_bak'].nodeValue);
        obj.removeAttribute('href_bak');
        obj.style.cursor = "";
    }
}


function onEnterDoSearch(e, enterButtonID, locationID, coordinatesID, buttonIDs) {
    if (e.keyCode != 13) return;
    
    var button = document.getElementById(enterButtonID);
    if (button == null) return;

    var href = button.getAttribute("href");
    if (href == null || href == "") href = button.getAttribute("href_bak");
    if (href == null || href == "") return;
    
    locateFromPostcode(locationID, coordinatesID, buttonIDs, href);
}




