/*
    Copyright (c) 2006, SpatialPoint, LLC.
    
    All rights reserved.
    
    http://www.spatialpoint.com
*/

var foundLocations = [];
function setLocation(foundLocationIndex)
{
    var location = foundLocations[foundLocationIndex];

    var icon = new Object();
    
    icon.coordinates = location.coordinates;
    icon.source = "MapPoint.Icons";
    icon.name = "21";
    icon.label = encodeURIComponent(location.name);
    
    spMap.icon = icon;
    spMap.centroid = icon.coordinates;
    spMap.setZoomLevel(location.zoomLevel);
    
}

function setGeocodingTab(tab)
{
    
    toggleDisplay(currentTab + "Table");
    
    currentTab = tab.options[tab.selectedIndex].text;
    
    toggleDisplay(currentTab + "Table");
    
   
    document.getElementById(firstControls[currentTab]).focus();
}

function clearLocations()
{
    spMap.icon = null;
    spMap.refresh();
    
    var LOCATIONS
        = [
            "addrStreet", "addrCity", "addrState", "addrZIP", 
            "intStreet1", "intStreet2", "intCity", "intState", "intZIP",
            "placeName"
          ];
          
    for (var i = 0; i < LOCATIONS.length; i++)
    {
        if (document.getElementById(LOCATIONS[i]) != null)
        {
            document.getElementById(LOCATIONS[i]).value = "";
        }
    }
    
    document.getElementById('addrState').value = "OH";
    document.getElementById('intState').value = "OH";
    
    hideControl("ambiguousResults");
}

function findLocation()
{
    spMap.onGeocodeResponse = geocodeResponse;
    hideControl("ambiguousResults");
    
    var location = new Object();
    location.street = location.city = location.state = location.zip = location.country = "";
    
    switch (currentTab)
    {
        case "Address":
            location.street = trim(document.getElementById("addrStreet").value);
            location.city = trim(document.getElementById("addrCity").value);
            location.state = trim(document.getElementById("addrState").value);
            location.zip = trim(document.getElementById("addrZIP").value);
            
            if (location.street + location.city + location.state + location.zip == "")
            {
                alert("Please type-in a location.");
                document.getElementById(firstControls[currentTab]).focus();

                return;
            }
            
            break;
            
        case "Intersection":
            location.street 
                = trim(document.getElementById("intStreet1").value)
                + " & "
                + trim(document.getElementById("intStreet2").value);
            location.city = trim(document.getElementById("intCity").value);
            location.state = trim(document.getElementById("intState").value);
            location.zip = trim(document.getElementById("intZIP").value);

            if (  trim(document.getElementById("intStreet1").value) 
                + trim(document.getElementById("intStreet2").value) 
                + location.city 
                + location.state 
                + location.zip == "")
            {
                alert("Please type-in a location.");
                document.getElementById(firstControls[currentTab]).focus();

                return;
            }
            break;

        case "Landmark":
            location.street = trim(document.getElementById("placeName").value);

            if (location.street == "")
            {
                alert("Please type-in a place or landmark name.");
                document.getElementById(firstControls[currentTab]).focus();

                return;
            }
            
            break;
    }
    
    spMap.geocode(location);
}

function geocodeResponse(response)
{
    foundLocations = response;
    
    switch (response.length)
    {
        case 0: // Couldn't geocode.
            alert("Unable to find the location, please try again.");
            break;
            
        case 1: // Single match.
            setLocation(0);
            break;
            
        default: // Ambiguous results.
            var control = document.getElementById("ambiguousResults").options;
            
            control.length = 0;
            for (var i = 0; i < response.length; i++)
            {
                control[i] = new Option(response[i].name);
            }
            
            toggleDisplay("ambiguousResults");
        
            break;
    }
}
