﻿    // --------------------------------------------------------
    // postback scripts
    // --------------------------------------------------------
        
    function Process_Suburb_ReceiveServerData(g_Suburb_ReceiveServerData_Arg, context)
    {
        var aResults = g_Suburb_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 = ParseSuburbSearchArray(aResults);
            
            /*if( aGeoResults.length == 1 )
            {
                var oSearchText = document.getElementById("txtGeographicalSearchField");
                //DisplayGeoSearchresult(oSearchText, aGeoResults[0]);
                oSearchText.value = FormatSearchResults(aGeoResults[0]);
                AttemptAgencyOptionsLookup2();
            }*/
            if( aGeoResults.length >= 1 )
            {
                DisplaySuburbSearchResults(aGeoResults, context);
            }
        }
    }
    
    function Suburb_ReceiveServerData(arg, context)
    {
        window.setTimeout(function () { Process_Suburb_ReceiveServerData(arg, context); }, 5);
    }

    function SetSuburbAutoSuggestControlCallback(autoSuggestControl) {
        // get rid of the result drop down in case its currently beeing displayed
        HideSearchResults(autoSuggestControl);

        SetSuburbSearchCallback(autoSuggestControl.textbox.id, autoSuggestControl);
    }

    var iTimerId = 0;
    function SetSuburbSearchCallback(elementId, context) {
    
        // clear any pending callbacks that haven't fired yet
        if(iTimerId != 0)
        {
            window.clearTimeout(iTimerId);
            iTimerId = 0;
        }

        //a function that returns the state is required by this control
        var state = getState();
        var element = document.getElementById(elementId);
        
        if ((element.value.length > 1) && (isNaN(element.value) == false))
        {
            iTimerId = window.setTimeout(function() { PerformSuburbSearch(state, 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() { PerformSuburbSearch(state, element.value, context); }, 250);
            return true;
        }
    }

    function PerformSuburbSearch(state, suburb, context)
    {
        if (state.length > 0 && suburb.length > 0) {
            var queryString = "?State=" + state + "&SearchTerm=" + suburb;
            Suburb_CallServer(queryString, context);
        }
        
        // clear timer id as the timer has already fired
        iTimerId = 0;
    }

    function Suburb_CallServer(queryString, context) {
        jQuery.get("/AJAX/Suggest.aspx" + queryString, "", function(data) { Suburb_ReceiveServerData(data, context) });
    }

    function SuburbSelectCallback(context)
    {
        AttemptAgencyOptionsLookup2();
    }
    
    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 AttemptAgencyOptionsLookup2()
    {
        // test to see if the agency lookup function exists
        var funcexists = (this.AgencyOptions_PerformLookup == null) ? false : true;
    
        if( funcexists == true )
            AgencyOptions_PerformLookup();
    }
