﻿var mgeo=null,sgeo=null,mk=null,tbl=null,evt=null,ot=null,hlat=null,hlng=null;
function l365_sLoad(la, ln, map){if (GBrowserIsCompatible){sgeo = new GClientGeocoder();hlat=document.getElementById(la);hlng=document.getElementById(ln);if(map){mgeo = new GMap2(document.getElementById(map)); mgeo.setCenter(new GLatLng(0, 0), 1);}}}
function l365_sUnload(){GUnload();}

// look up locations to a list
function l365_sOnKeyUp(e)
{
    var t, tb, s, x, y, cr, cc, ci;

    if (!e) var e = window.event;
    if (e.srcElement) t = e.srcElement;
    else if (e.target) t = e.target;

    // check that key resulted in text changing
    if (t.old != null && t.old == t.value)
        return;
    else
        t.old = t.value;

    // check at least 3 chars exist
    if (t.value.length > 3)
    {
        // reset the lat/lng fields
        hlat.value = "";
        hlng.value = "";
        
        if (tbl == null)
        {
            tb = document.getElementById("Table_LocationList");
            tbl = document.createElement("tbody");
            tb.appendChild(tbl);
            tb.style.height = "auto";
        }
    
        if (evt != null)
            window.clearTimeout(evt);
        evt = window.setTimeout("sgeo.getLocations('" + t.value + "', l365_sPopulateList);", 500);
        tbl.populateElement = t.id;

        // delete existing rows (if any) and add searching row if not already present
        if (tbl.rows.length == 0 || (tbl.rows.length > 0 && tbl.rows[0].cells.length > 0 && tbl.rows[0].cells[0].className != "searching"))
        {
            while (tbl.rows.length) tbl.deleteRow(0);
        
            // [re]position table and display it
            s = l365_common_getElementDimensions(t);
            x = l365_common_getElementLeft(t);
            y = l365_common_getElementTop(t);
            tbl.parentNode.style.left = x + "px";
            tbl.parentNode.style.top = y + "px";
            tbl.parentNode.style.width = s.width + "px";
            tbl.parentNode.style.display = "block"
            
            // add "searching..." row
            cr = tbl.appendChild(document.createElement("tr"));
            cc = cr.appendChild(document.createElement("td"));
            cc.className = "searching";
            ci = cc.appendChild(document.createElement("img"));
            ci.src = "images/loading_globe.gif";
            ci.height = "16";
            ci.width = "16";
            ci.align = "absmiddle";
            cc.appendChild(document.createTextNode(" Searching..."));
            cr.appendChild(cc);
        }
    }
    else if (tbl != null)
    {
        while (tbl.rows.length) tbl.deleteRow(0);
        tbl.parentNode.style.display = "block"
    }
}

function l365_sValidate(eLat,eLng)
{
    var lat, lng;
    lat = document.getElementById(eLat);
    lng = document.getElementById(eLng);
    
    if(lat.value != null && lat.value.length > 0 && lat.value != null && lat.value.length > 0)
        return true;
    else
    {
        alert("Please select a location to start searching");
        return false;
    }
}

function l365_sOnBlur(e)
{
    if (tbl.populateElement != null)
        tbl.parentNode.style.display = "none";
}

function l365_sOnRowClick(e)
{
    var t;
    
    if (!e) var e = window.event;
    if (e.srcElement) t = e.srcElement;
    else if (e.target) t = e.target;
    
    if (tbl.populateElement == null || t.className == "popupInfoRow" || t.className == "searching")
        return;

    // set textbox to contents of clicked <td>
    e = document.getElementById(tbl.populateElement);
    e.value = t.childNodes[0].data;
    
    // set the hidden fields for latitude and longitude
    //hlat.value = t.style.latitude;
    //hlng.value = t.style.longitude;
    hlat.value = (t.attributes.getNamedItem("lat").value);
    hlng.value = (t.attributes.getNamedItem("lng").value);
    
    // hide table
    tbl.parentNode.style.display = "none";
}

function l365_sOnRowClickWithMapping(e)
{
    var t, pt;
    
    if (!e) var e = window.event;
    if (e.srcElement) t = e.srcElement;
    else if (e.target) t = e.target;
    
    if (tbl.populateElement == null || t.className == "popupInfoRow" || t.className == "searching")
        return;

    // set textbox to contents of clicked <td>
    e = document.getElementById(tbl.populateElement);
    e.value = t.childNodes[0].data;
    
    // set the hidden fields for latitude and longitude
    //hlat.value = t.style.latitude;
    //hlng.value = t.style.longitude;
    hlat.value = (t.attributes.getNamedItem("lat").value);
    hlng.value = (t.attributes.getNamedItem("lng").value);
    
    // hide table
    tbl.parentNode.style.display = "none";
    
    // do a mapping lookup into 'map' box
    if (mgeo != null)
    {
        // convert to lat/long
        pt = new GLatLng(hlat.value, hlng.value);

        // centre the map on the new point, zoom to '13' (??)
        mgeo.setCenter(pt, 13);

        // create a marker and add to overlay
        mgeo.clearOverlays();
        mk = new GMarker(pt,{draggable:true});
        mgeo.addOverlay(mk);

        // add the mini map controls
        mgeo.addControl(new GSmallMapControl());

        GEvent.addListener(mk, "dragstart", l365_sDragStart);
        GEvent.addListener(mk, "dragend", l365_sDragEnd);

        // display address and "drag for accuracy" message
        mk.openInfoWindowHtml("<span style=\"font-family: Verdana, sans-serif; font-size: 7pt; line-height: 8pt;\">" + strLocationFound.replace("{0}", e.value) + "</span>");
    }
}

function l365_sDragStart(point)
{
    mk.closeInfoWindow();
}


function l365_sDragEnd(point)
{
    var e = document.getElementById(tbl.populateElement);
    mgeo.panTo(point);
    mk.openInfoWindowHtml("<span style=\"font-family: Verdana, sans-serif; font-size: 7pt;\">" + strLocationFound.replace("{0}", e.value) + "</span>");
    hlat.value = point.lat();
    hlng.value = point.lng();
}

function l365_sPopulateList(loc)
{
    var i;
    var cr, cc, at;
    
    if (loc == null || loc.Placemark == null || loc.Placemark[0] == null)
    {
        // delete existing rows
        while (tbl.rows.length) tbl.deleteRow(0);
    
        // add not found item
        cr = tbl.appendChild(document.createElement("tr"));
        cc = cr.appendChild(document.createElement("td"));
        cc.className = "popupInfoRow";
        cc.appendChild(document.createTextNode("Location not found"));
        return;
    }
    
    // confirm list control is configured
    if (tbl != null)
    {
        // delete existing rows
        while (tbl.rows.length) tbl.deleteRow(0);

        // add top info row
        cr = tbl.appendChild(document.createElement("tr"));
        cr.className = "popupInfoRow";
        cc = cr.appendChild(document.createElement("td"));
        cc.appendChild(document.createTextNode("Found these closest matches. If your location is not listed, continue typing to keep searching."));
        cc.className = "popupInfoRow";
    
        // add list items
        for (i = 0; i < loc.Placemark.length && i < 5; i++)
        {
            // add table row and and cells
            cr = tbl.appendChild(document.createElement("tr"));
            cc = cr.appendChild(document.createElement("td"));
            cc.className = "item";
            cc.appendChild(document.createTextNode(loc.Placemark[i].address));

            // add lat/lng attributes            
            at = document.createAttribute("lat");
            at.value = loc.Placemark[i].Point.coordinates[1];
            cc.attributes.setNamedItem(at);
            at = document.createAttribute("lng");
            at.value = loc.Placemark[i].Point.coordinates[0];
            cc.attributes.setNamedItem(at);

            //cc.style.latitude = loc.Placemark[i].Point.coordinates[1];
            //cc.style.longitude = loc.Placemark[i].Point.coordinates[0];
        }
        
        // add "...more..." row at bottom
        if (loc.Placemark.length > 5)
        {
            // add table row and and cells
            cr = tbl.appendChild(document.createElement("tr"));
            cr.className = "popupInfoRow";
            cc = cr.appendChild(document.createElement("td"));
            cc.appendChild(document.createTextNode("More locations found - continue typing to help narrow down the search."));
            cc.className = "popupInfoRow";
        }
    }    
}