﻿// JScript File

function setAddressLocator() {
    //set the locator service
    locator = new esri.tasks.Locator("http://new.kgis.net/ArcGIS/rest/services/Locators/KGIS.KnoxCo_AddrPts/GeocodeServer");
    dojo.connect(locator, "onAddressToLocationsComplete", showAddressResults);
}

function geocodeAddress() {
    map.graphics.clear();
    
    //force the display in the results panel
    var resultsPane = dojo.byId("cpResults");    
    resultsPane.innerHTML = "";


    var add = dojo.byId("txtAddress").value;           
    
    if (add == "") {
        alert("Please eneter an address.");
        return;
    }
    
    
    
    var address = {Street : add};
    /*            
    var address = {
        Address : add,
        City: "knoxville",
        State: "tn",
        Zip: "37918"                
    };
    */            
    clearDetails();
    showBusy();
    locator.addressToLocations(address);
}

function showAddressResults(candidates) {
    //debugger;
    var candidate;
    var content = "";
    var rowClass;
    
    for (var i=0, il=candidates.length; i<il; i++) {
        candidate = candidates[i];
        if (candidate.score == 100) {
            //100% match drive to this address
            zoomToAddressPoint(candidate.location.x, candidate.location.y, candidate.address);
            break;
        } else {
            //show the results in the search results window and let the user drive to each address
            var attributes = {address: candidate.address, score:candidate.score, locatorName:candidate.attributes.address };
    
            if ((i % 2) == 0) {
                rowClass = "row1";
            } else {
                rowClass = "row2";
            }

            //on the first record write the initial table tag
            if (i==0) {
                content += "<table width='260' cellpadding='3' cellspacing='2'>";
                content += "<tr class='rowHeader'><th align='left' width='50'>Score</th><th align='left' width='210'>Address</th></tr>";    //<th align='center' width='60'>Zoom</th></tr>";
            }
                   
            content += "  <tr class='" + rowClass + "'>";
            content += "    <td align='left'>" + candidate.score + "</td>";
            content += "    <td align='left'><span onclick='zoomToAddressPoint(" + candidate.location.x + "," + candidate.location.y + ",\"" + candidate.address + "\")' onmouseover='this.className=\"addressLinkHover\"' onmouseout='this.className=\"addressLink\"' class='addressLink'>" + candidate.address + "</span></td>";
            //content += "    <td align='center'><img src='./images/ZoomToRecord_12.png' onclick='zoomToAddressPoint(" + candidate.location.x + "," + candidate.location.y + ",\"" + candidate.address + "\")' /></td>";
            content += "  </tr>";

            //on the last row write the ending table tag
            if (i == il-1) {
                content += "</table>";                
            }

            //force the display in the results panel
            var resultsPane = dojo.byId("cpResults");    
            resultsPane.innerHTML = content;
            
            resultsPane.style.visibility = "visible";
            resultsPane.style.display = "block";
            dijit.byId("mainTabContainer").selectChild("tabResults");
            dijit.byId("acSearchResults").selectChild("apResults");
        }
    }
    hideBusy();
}

function zoomToAddressPoint(x,y,addressText) {
    map.graphics.clear();
    
    //create the graphics for the map
    var point =  new esri.geometry.Point(x, y, map.spatialReference);    
    var pointSymbol = new esri.symbol.SimpleMarkerSymbol();
    pointSymbol.setStyle(esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE);
    pointSymbol.setColor(new dojo.Color([255,0,0,0.75]));

    var graphic = new esri.Graphic(point, pointSymbol);
    map.graphics.add(graphic);
    map.graphics.add(new esri.Graphic(point, new esri.symbol.TextSymbol(addressText).setOffset(0, 10)));
        
    //create an extent to be used for zooming around the address point
    var x1, x2, y1, y2;
    x1 = x - 300;
    x2 = x + 300;
    y1 = y - 300;
    y2 = y + 300;
    var env = new esri.geometry.Extent(x1, y1, x2, y2, map.spatialReference);
    map.setExtent(env);
}
