﻿<!--
    // --------------------------------------------------------
    // postback scripts
    // --------------------------------------------------------

    function Process_Street_ReceiveServerData(g_Street_ReceiveServerData_Arg, context)
    {
        var aResults = g_Street_ReceiveServerData_Arg.split("|");
        
        // were any search results returned ???
        if( aResults.length == 1 )
        {
            //alert("The suburb / postcode could not be located. Please try again.");
            return;
        }
        else
        {
            
            // create array of geographical result objects
            var aGeoResults = ParseStreetSearchArray(aResults);
            
            /*if( aGeoResults.length == 1 )
            {
                var oSearchText = document.getElementById("txtGeographicalSearchField");
                //DisplayGeoSearchresult(oSearchText, aGeoResults[0]);
                oSearchText.value = FormatSearchResults(aGeoResults[0]);
                AttemptAgencyOptionsLookup2();
            }*/
            if( aGeoResults.length >= 1 )
            {
                DisplayStreetSearchResults(aGeoResults, context);
            }
        }
    }

    function Street_ReceiveServerData(arg, context) {
        window.setTimeout(function() { Process_Street_ReceiveServerData(arg, context); }, 5);
    }
    
    function SetStreetAutoSuggestControlCallback(autoSuggestControl) {
        // get rid of the result drop down in case its currently beeing displayed
        HideSearchResults(autoSuggestControl);

        SetStreetSearchCallback(autoSuggestControl.textbox.id, autoSuggestControl);
    }
    
    var iTimerId = 0;
    function SetStreetSearchCallback(elementId, context) {
   
        // clear any pending callbacks that haven't fired yet
        if(iTimerId != 0)
        {
            window.clearTimeout(iTimerId);
            iTimerId = 0;
        }
        
        // get rid of the result drop down in case its currently beeing displayed
        var state = getState();
        var element = document.getElementById(elementId);
        
        
        if( (element.value.length > 1) && (isNaN(element.value) == false) )
        {
            iTimerId = window.setTimeout(function() { PerformStreetSearch(state, oSuburbText.value, element.value, context); }, 250);
            return true;
        }
        else if( element.value.length < 3 )
        {
            // don't bother searching
            return false;
        }
        else
        {
            // if its all numbers, then assume its a postcode and do nothing
            if( isNaN(element.value) == false )
            {
                // it must be a postcode.... just return
                return false;
            }
            
            iTimerId = window.setTimeout(function() { PerformStreetSearch(state, oSuburbText.value, element.value, context); }, 250);
            return true;
        }
    }
    
   
    function PerformStreetSearch(state, suburb, street, context)
    {
        if( street.length > 0 && suburb.length > 0 )
        {
            Street_CallServer("?State=" + state + "&SuburbPostcode=" + suburb + "&Street=" + street, context);
        }
    
        // clear timer id as the timer has already fired
        iTimerId = 0;
    }
    
    function Street_CallServer(queryString, context)
    {
        jQuery.get("/AJAX/StreetSuggest.aspx" + queryString, "", function(data) { Street_ReceiveServerData(data, context) });
    }

    function StreetSelectCallback(context)
    {
        AttemptSetStreetType(context.itemValue);
    }
    
    function swallowEnter(e)
    {
        if(e.keyCode==13)
        {
            return false;
        }
        
        return true;
    }
    
    // this is a dodgey hack thats been thrown in
    // we need to update the agency's for this area... provided the agency options are actually there
    function AttemptSetStreetType(streetType)
    {
    
        // test to see if the set region function exists
        var funcexists = (this.StreetType_SetStreetType == null) ? false : true;
    
        if( funcexists == true )
            StreetType_SetStreetType(streetType);
    }
    
//-->
