﻿var addressTimer = 0;

function cleanAddrLists(cleanStreets, cleanAddrs, cleanUnits)
{
    if (cleanStreets) dojo.byId("selStreet").options.length = 0;
    if (cleanAddrs) dojo.byId("selHouseNumbers").options.length = 0;
    if (cleanUnits) dojo.byId("selUnits").options.length = 0;
}

function getAddrStreets(street) {    
    //clear out the timer
    clearTimeout(addressTimer);

    if (street.length < 3) {
        cleanAddrLists(true,true,true);
        hideAddrBusy();
        return;
    }

    addressTimer = setTimeout(function() {
        //clear all street lists
        cleanAddrLists(true,true,true);

        //show address busy
        showAddrBusy('STREET');

        var params = {"prefixText":'"' + street + '"'}
        dojo.xhrGet(
        {
            url: "./wsSearches.asmx/GetStreetNames",
            handleAs: "json",
            contentType: "application/json; charset=utf-8",
            content: params,
            load: OnAddrStreetComplete,
            error: function(error,args){
                console.warn("error!",error);
            }
        });
    }, 800);    
}

function getHouseNumbers()
{
    //clear any existing numbers in the listbox
    cleanAddrLists(false,true,true)
    
    //show address busy
    showAddrBusy('ADDRESS');    
    
    //get the selected value from the list box and update the text box
    var street = dojo.byId("selStreet").value;
    dojo.byId("txtStreet").value = street;
    
    //build the parameter to pass to the web service
    var params = {"streetName":'"' + street + '"'}
    
    //call the webservice to get the data
    dojo.xhrGet(
    {
        url: "./wsSearches.asmx/GetHouseNumbers",
        handleAs: "json",
        contentType: "application/json; charset=utf-8",
        content: params,
        load: OnHouseNumberComplete,
        error: function(error,args){console.warn("error!",error);}
    });            
}

function getUnits()
{
    //clear any existing numbers in the listbox
    cleanAddrLists(false,false,true)
    
    //show address busy
    showAddrBusy('UNIT');

    //get the selected values from the list boxes
    var street = document.getElementById('selStreet').value;
    var houseParams = document.getElementById('selHouseNumbers').value.split("|");
    house = houseParams[1];
    
    //build the parameter to pass to the web service
    var params = {"streetName":'"' + street + '"', "houseNumber":'"' + house + '"'}
    //call the webservice to get the data
    dojo.xhrGet(
    {
        url: "./wsSearches.asmx/GetUnits",
        handleAs: "json",
        contentType: "application/json; charset=utf-8",
        content: params,
        load: OnUnitsComplete,
        error: function(error,args){console.warn("error!",error);}
    });            
    
}
        
function OnAddrStreetComplete(results, ioArgs) {
    var newResults = dojo.fromJson(results);
    var lBox = dojo.byId("selStreet");

    for (var i=0, il=newResults.length; i<il; i++) {
        var street = newResults[i];
        if (street != null) {
            var y=document.createElement('option');
            y.text=street;
            y.value=street;
            lBox.add(y);
        }
    }
    
    //hide the busy image
    hideAddrBusy();
}

function OnHouseNumberComplete(results, ioArgs) {
    //cleanAddrLists(false,true,true);
    
    var newResults = dojo.fromJson(results);
    var selBox = document.getElementById('selHouseNumbers')

    for (var i=0, il=newResults.length; i<il; i++) {
        var address = newResults[i];
        if (address != null) {
            var y=document.createElement('option');
            y.text=address.ADDRESS_NUMBER;
            y.value=address.ADDRESS_ID + "|" + address.ADDRESS_NUMBER + "|" + address.STREET_NAME + "|" + address.X_COORD + "|" + address.Y_COORD;
            selBox.add(y);
        }
    } 

    //hide the busy image
    hideAddrBusy();
}

function OnUnitsComplete(results, ioArgs) {
    //cleanAddrLists(false,false,true);
    //debugger;
    var newResults = dojo.fromJson(results);
    var selBox = document.getElementById('selUnits')
    
    for (var i=0, il=newResults.length; i<il; i++) {
        var unit = newResults[i];
        if (unit != null) {
            var y=document.createElement('option');
            y.text=unit.UNIT;
            y.value=unit.ADDRESS_ID + "|" + unit.ADDRESS_NUMBER + "|" + unit.STREET_NAME + "|" + unit.UNIT + "|" + unit.X_COORD + "|" + unit.Y_COORD;
            selBox.add(y);
        }
    }

    //hide the busy image
    hideAddrBusy();
}

function mapAddressPoint(){
    //debugger;
    var unitParams = dojo.byId("selUnits").value.split("|");
    var unit = unitParams[0];
    
    var houseParams = dojo.byId("selHouseNumbers").value.split("|");
    var house = houseParams[0];                        
    
    //create the address text to be displayed
    var addressText, x, y;
    if (unitParams != "") {
        addressText = unitParams[1] + " " + unitParams[2] + " #" + unitParams[3];
        x = parseInt(unitParams[4],10);
        y = parseInt(unitParams[5],10);
    } else if (houseParams != "") {
        addressText = houseParams[1] + " " + houseParams[2];
        x = parseInt(houseParams[3],10);
        y = parseInt(houseParams[4],10);
    }
    
    //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_DIAMOND);
    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, 8)));
        
    //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);
}

function checkAddressData() {
    if (document.getElementById('selStreet').selectedIndex < 0) return false;
    if (document.getElementById('selHouseNumbers').selectedIndex < 0) return false;
}

function showAddrBusy(searchingWhat) {
    var x = document.getElementById('divAddrBusy');
    switch (searchingWhat.toUpperCase()) {
        case 'STREET':
            x.style.left = "17px";
            x.style.top = "84px";
            x.style.visibility = "visible";
            x.style.display = "block";
            break;
        case 'ADDRESS':
            x.style.left = "238px";
            x.style.top = "60px";
            x.style.visibility = "visible";
            x.style.display = "block";
            break;
        case 'UNIT':
            x.style.left = "312px";
            x.style.top = "60px";
            x.style.visibility = "visible";
            x.style.display = "block";
            break;
    }
}        

function hideAddrBusy() {
    var x = document.getElementById('divAddrBusy');
    x.style.visibility = "hidden";
    x.style.display = "none";    
}

        

