﻿//=================== Browse-Location dropdown functionality====================//

$(document).ready(function () {

    if ($('div#modify-results').length) {
        //return all the autocomplete based on their parent and the selected type
        SearchResultsJS.BrowseAllLocation = function (method, autoCompleteType, autoCompleteId, callback, error) {
            SearchResultsJS.proxy.invokeAjaxJsonGet(method,
                { 'randRequest': Math.random(),
                    'autoCompleteTypeId': autoCompleteType,
                    'autoCompleteId': autoCompleteId
                },
                callback, error
            );
        };

        //return N (max-min) records of the autocomplete data based on their parent and the selected type
        SearchResultsJS.BrowseLocation = function (method, autoCompleteType, autoCompleteId, minRecord, maxRecord, callback, error) {
            SearchResultsJS.proxy.invokeAjaxJsonGet(method,
                { 'randRequest': Math.random(),
                    'autoCompleteTypeId': autoCompleteType,
                    'autoCompleteId': autoCompleteId,
                    'minRecord': minRecord,
                    'maxRecord': maxRecord
                },
                callback, error
            );
        };

        //Applied only for city
        SearchResultsJS.RefreshCities = function (data, ddlCity, isMoreEvent) {
            var strOptions = '';
            var optionArray = [];
            len = data.length;
            if (isMoreEvent) {
                $(ddlCity).find('option[value="more"]').remove();
                for (i = 0; i < len; i++) {
                    optionArray[i] = '<option value="' + data[i].Id + '">' + data[i].Desc + '</option>';
                    //strOptions += '<option value="' + data[i].Id + '">' + data[i].Desc + '</option>';
                }
                //strOptions+='<option value="more">... more cities ...</option>';
                strOptions = optionArray.join("") + '<option value="more">... more cities ...</option>';
            }
            $(ddlCity).append(strOptions);
            $(ddlCity).multiselect("refresh");
        };

        //add the option list into the selected dropdown (ddlObject)
        SearchResultsJS.RefreshDropDown = function (data, ddlObject, defaultValue, defaultText, moreText) {
            len = data.length;
            $(ddlObject).find("option").remove();
            var strOptions = '<option value="' + defaultValue + '">' + defaultText + '</option>';
            var hasRegion = false;
            var locationType = "";
            for (i = 0; i < len; i++) {
                switch (data[i].GroupTypeId) {
                    case SearchResultsJS.AutoCompleteTypeRegionCountry:
                    case SearchResultsJS.AutoCompleteTypeRegionState:
                        strOptions += '<option disabled="disabled">Regions</option>';
                        hasRegion = true;
                        break;
                    case SearchResultsJS.AutoCompleteTypeState:
                        if (hasRegion)
                            strOptions += '<option disabled="disabled">States</option>';
                        break;
                    case SearchResultsJS.AutoCompleteTypeCity:
                        if (hasRegion)
                            strOptions += '<option disabled="disabled">Cities</option>';
                        break;
                }
                locationType = data[i].GroupTypeId;
                items = data[i].AutoCompleteItems;
                lenItems = items.length;
                for (j = 0; j < lenItems; j++) {
                    strOptions += '<option value="' + items[j].Id + '" typeid="' + locationType + '">' + items[j].Desc + '</option>';
                }

                if (data[i].MoreRecord) {
                    strOptions += '<option value="more">... ' + moreText + ' ...</option>';
                }
            }
            $(ddlObject).append(strOptions);
            $(ddlObject).multiselect("refresh");

            //(Re)enable the dropdown object
            if ($('div#advanced-search').length) {
                $(ddlObject).next("a.ui-multiselect").removeClass("ui-state-disabled").css("background", "").find("span").css("opacity", "");
            }
            if ($('div#modify-results').length) {
                var id = $(ddlObject).attr("id").substring($(ddlObject).attr("id").length - 1);
                if (id == 0)
                    $(ddlObject).next("a.ui-multiselect").removeClass("ui-state-disabled").css("background", "").find("span").css("opacity", "");
                else
                    $(ddlObject).next("a.ui-multiselect").css("display", "block");
            }
            return true;
        };

        SearchResultsJS.ResetDropDown = function (ddlObject, defaultValue, defaultText) {
            $(ddlObject).find("option").remove();
            var strOptions = '<option value="' + defaultValue + '" typeid="">' + defaultText + '</option>';
            $(ddlObject).append(strOptions);
            $(ddlObject).multiselect("refresh");

            //Disable the dropdown element
            if ($('div#advanced-search').length) {
                $(ddlObject).next("a.ui-multiselect").addClass("ui-state-disabled").css({ "margin-top": "0", "background": "#ddd" }).find("span").css("opacity", "0.5");
            }
            if ($('div#modify-results').length) {
                var id = $(ddlObject).attr("id").substring($(ddlObject).attr("id").length - 1);
                if (id == 0)
                    $(ddlObject).next("a.ui-multiselect").addClass("ui-state-disabled").css({ "margin-top": "0", "background": "#ddd" }).find("span").css("opacity", "0.5");
                else
                    $(ddlObject).next("a.ui-multiselect").css("display", "none");
            }
        }

        SearchResultsJS.HasState = function (data) {
            len = data.length;
            for (i = 0; i < len; i++) {
                if (data[i].GroupTypeId == SearchResultsJS.AutoCompleteTypeState)
                    return true;
            }
            return false;
        };

        SearchResultsJS.HasCity = function (data) {
            len = data.length;
            for (i = 0; i < len; i++) {
                if (data[i].GroupTypeId == SearchResultsJS.AutoCompleteTypeCity)
                    return true;
            }
            return false;
        };

        SearchResultsJS.MoreCities = function (selectedState, ddlCity) {
            minRecord = $(ddlCity).find("option").length - 2; // minus 2, because we have select city & ... more cities ...
            maxRecord = minRecord + SearchResultsJS.NbCityRecords;
            SearchResultsJS.BrowseLocation('BrowseLocationWithoutRegionByAutoCompleteType',
                SearchResultsJS.AutoCompleteTypeCity,
                selectedState,
                minRecord + 1,
                maxRecord,
                function (data) {
                    SearchResultsJS.RefreshCities(data, ddlCity, true);
                },
                SearchResultsJS.error
            );
        };

        SearchResultsJS.HasNeighborhood = function (data) {
            len = data.length;
            for (i = 0; i < len; i++) {
                if (data[i].GroupTypeId == SearchResultsJS.AutoCompleteTypeNeighborhood)
                    return true;
            }
            return false;
        };

        //--- Populates the State list given the chosen country ---//
        //Params: [int selectedCountry => autocompleteID of chosen country] , [int id => index of the location. 1,2, or 3]
        SearchResultsJS.ShowStates = function (selectedCountry, id) {
            SearchResultsJS.BrowseAllLocation('BrowseAllLocationByAutoCompleteType',
                SearchResultsJS.AutoCompleteTypeState,
                selectedCountry,
                function (data) {
                    if (SearchResultsJS.HasState(data)) {
                        if ($('#country' + id + ' option:selected').text().toLowerCase() != 'united states')
                            SearchResultsJS.RefreshDropDown(data, $("#state" + id), "0", "Any Province");
                        else
                            SearchResultsJS.RefreshDropDown(data, $("#state" + id), "0", "Any State");

                        SearchResultsJS.ResetDropDown($("#city" + id), "0", "Select City");
                        SearchResultsJS.ResetDropDown($("#neighborhood" + id), "0", "Select Neighborhood");
                    }
                    else {
                        SearchResultsJS.ResetDropDown($("#state" + id), "0", "All States");
                        if (SearchResultsJS.HasCity(data)) {
                            SearchResultsJS.RefreshDropDown(data, $("#city" + id), "0", "Any City");
                            SearchResultsJS.ResetDropDown($("#neighborhood" + id), "0", "Select Neighborhood");
                        }
                        else {
                            SearchResultsJS.ResetDropDown($("#city" + id), "0", "All Cities");
                            if (SearchResultsJS.HasNeighborhood(data)) {
                                SearchResultsJS.RefreshDropDown(data, $("#neighborhood" + id), "0", "Any Neighborhood");
                            }
                            else {
                                SearchResultsJS.ResetDropDown($("#neighborhood" + id), "0", "All Neighborhoods");
                            }
                        }
                    }

                    if (SearchResultsJS.Switching) {
                        //Loaded the states for the given country_id, shown as selectedCountry
                        var index = SwitchDataArray[current].locationIndex;
                        itemArray = SwitchDataArray[current].itemArray;
                        //Now look for the autocompleteID of the state matching the next string in the autocompleteItem array
                        stateID = $("#state" + index + " option").filter(function () { return $(this).text() == itemArray[1]; }).val();
                        if (isNaN(stateID) || stateID == 0) {
                            if (!SearchResultsJS.HasState(data)) { //if no-state because there are no states
                                if (itemArray.length == 1) { //Then we're done with this location, on to the next
                                    //$("div#searched-location" + index + " div.locked a.edit").css('display', 'block');
                                    if (SwitchDataArray[current + 1] != undefined) {
                                        current++;
                                        SearchResultsJS.OnClickCountry();
                                    }
                                }
                                else {
                                    SearchResultsJS.ShowCities(SwitchDataArray[current].country_id, $("#city" + index));
                                }
                            }
                            else {  //if no-state because it wasn't found in the state list, end it and move on
                                //$("div#searched-location" + index + " div.locked a.edit").css('display', 'block');
                                if (SwitchDataArray[current + 1] != undefined) {
                                    current++;
                                    SearchResultsJS.OnClickCountry();
                                }
                            }
                        }
                        //The state was found in the dropdown, so go to the function that simulates a click on it.
                        else {  //For case country+state+...
                            $("#state" + index).val(stateID).next(".ui-multiselect").find("span:eq(1)").text(itemArray[1]);
                            SwitchDataArray[current].state_id = stateID;
                            SearchResultsJS.OnClickState();
                        }
                    }
                },
                SearchResultsJS.error
            );
            return true;
        };

        //--- Populates the City list given the chosen state (or chosen country) ---//
        //Params: [int selectedState => autocompleteID of chosen state] , [object ddlCity => which city dropdown. #city1, 2, or 3]
        SearchResultsJS.ShowCities = function (selectedState, ddlCity) {
            SearchResultsJS.BrowseAllLocation('BrowseAllLocationByAutoCompleteType',
                SearchResultsJS.AutoCompleteTypeCity,
                selectedState,
                function (data) {
                    id = $(ddlCity).attr("id").substring($(ddlCity).attr("id").length - 1);
                    if (SearchResultsJS.HasCity(data)) {
                        SearchResultsJS.RefreshDropDown(data, ddlCity, "0", "Any City");
                        SearchResultsJS.ResetDropDown($("#neighborhood" + id), "0", "Select Neighborhood");
                    }
                    else {
                        SearchResultsJS.ResetDropDown(ddlCity, "0", "All Cities");
                        if (SearchResultsJS.HasNeighborhood(data)) {
                            SearchResultsJS.RefreshDropDown(data, $("#neighborhood" + id), "0", "Any Neighborhood");
                        }
                        else {
                            SearchResultsJS.ResetDropDown($("#neighborhood" + id), "0", "All Neighborhoods");
                        }
                    }

                    if (SearchResultsJS.Switching) {
                        //Loaded the cities for chosen state_id - selectedState - or for chosen country_id, for countries without states.
                        var index = SwitchDataArray[current].locationIndex;
                        itemArray = SwitchDataArray[current].itemArray;
                        idx = (itemArray.length == 2) ? 1 : 2;
                        //Now look for the autocompleteID of the city matching the next string in the autocompleteItem array
                        cityID = $("#city" + index + " option").filter(function () { return $(this).text() == itemArray[idx]; }).val();
                        if (isNaN(cityID) || cityID == 0) {
                            if (!SearchResultsJS.HasCity(data)) {  //if no-city because there are no cities
                                if (SwitchDataArray[current].state_id == 0) {   //for case country+[not-state]+[not-city]+[neighborhood]
                                    SearchResultsJS.ShowNeighborhoods(SwitchDataArray[current].country_id, $("#neighborhood" + index));
                                }
                                else {  //for case country+state+[not-city]+[neighborhood]
                                    SearchResultsJS.ShowNeighborhoods(SwitchDataArray[current].state_id, $("#neighborhood" + index));
                                }
                            }
                            else {  //if no-city because it wasn't found in the city list, end it and move on
                                //$("div#searched-location" + index + " div.locked a.edit").css('display', 'block');
                                if (SwitchDataArray[current + 1] != undefined) {
                                    current++;
                                    SearchResultsJS.OnClickCountry();
                                }
                            }
                        }
                        else {
                            $("#city" + index).val(cityID).next(".ui-multiselect").find("span:eq(1)").text(itemArray[idx]);
                            SwitchDataArray[current].city_id = cityID;
                            SearchResultsJS.OnClickCity();
                        }
                    }
                },
                SearchResultsJS.error
            );
            return true;
        };

        //--- Populates the Neigborhood list given the chosen city ---//
        //Params: [int selectedCity => autocompleteID of chosen city] , [object ddlNeighborhood => which neighborhood dropdown. #neighborhood1, 2, or 3]
        SearchResultsJS.ShowNeighborhoods = function (selectedCity, ddlNeighborhood) {
            SearchResultsJS.BrowseAllLocation('BrowseAllLocationByAutoCompleteType',
                SearchResultsJS.AutoCompleteTypeNeighborhood,
                selectedCity,
                function (data) {
                    id = $(ddlNeighborhood).attr("id").substring($(ddlNeighborhood).attr("id").length - 1);
                    if (SearchResultsJS.HasNeighborhood(data)) {
                        SearchResultsJS.RefreshDropDown(data, ddlNeighborhood, "0", "Any Neighborhood");
                    }
                    else {
                        SearchResultsJS.ResetDropDown(ddlNeighborhood, "0", "All Neighborhoods");
                    }

                    if (SearchResultsJS.Switching) {
                        //Loaded the neighborhoods for chosen city_id - selectedCity.
                        var index = SwitchDataArray[current].locationIndex;
                        itemArray = SwitchDataArray[current].itemArray;
                        //Now look for the autocompleteID of the neighborhood matching the next string in the autocompleteItem array
                        neighborhoodID = $("#neighborhood" + index + " option").filter(function () { return $(this).text() == itemArray[3] }).val();
                        if (isNaN(neighborhoodID) || neighborhoodID == 0) {
                            //If no neighborhood found, do nothing.
                        }
                        else {
                            $("#neighborhood" + index).val(neighborhoodID).next(".ui-multiselect").find("span:eq(1)").text(itemArray[3]);
                            SwitchDataArray[current].neighborhood_id = neighborhoodID;
                        }
                        //$("div#searched-location" + index + " div.locked a.edit").css('display', 'block');

                        if (SwitchDataArray[current + 1] != undefined) {
                            current++;
                            SearchResultsJS.OnClickCountry();
                        }
                    }
                },
                SearchResultsJS.error
            );
            return true;
        };

        SearchResultsJS.OnClickCountry = function (event, ui) {
            var index = (SearchResultsJS.Switching) ? SwitchDataArray[current].locationIndex : event.locationIndex;
            var country_autoCompleteID = (SearchResultsJS.Switching) ? SwitchDataArray[current].country_id : ui.value;
            SearchResultsJS.changedCountry = (country_autoCompleteID != $("#country" + index).val()) ? true : false;

            if (country_autoCompleteID > 0) { //If this country has a valid autocompleteID, load its states
                if (!SearchResultsJS.Switching) {
                    var locationType = $("#country" + index + " option[value=" + country_autoCompleteID + "]").attr("typeid");
                    $("#country" + index + " option[value=" + country_autoCompleteID + "]").attr("ischosen", "yes");
                    $("input#temp").val(country_autoCompleteID).attr("typeid", locationType);
                }
                SearchResultsJS.ShowStates(country_autoCompleteID, index);
            }
            else {
                SearchResultsJS.ResetDropDown($("#state" + index), "0", "Select State");
                SearchResultsJS.ResetDropDown($("#city" + index), "0", "Select City");
                SearchResultsJS.ResetDropDown($("#neighborhood" + index), "0", "Select Neighborhood");
                $("#country" + index + " option").removeAttr("ischosen");
                if (SearchResultsJS.changedCountry) {
                    $("input#temp").val("").attr({ typeid: "", location: "", chosen_city: "" });
                    $("input#location-postal-webid1").val("").attr({ autocpid: "", autocptypeid: "", countryid: "" });
                }
            }

            //if (!SearchResultsJS.Switching) $("#autoComplete"+index).attr("source","browse");
            //SearchResultsJS.GetPropertiesCount();
            return true;
        }

        $(".country-select").multiselect({
            click: function (event, ui) {
                SearchResultsJS.Switching = false;
                event.locationIndex = $(this).attr("id").substring($(this).attr("id").length - 1);
                SearchResultsJS.OnClickCountry(event, ui);
            },
            close: function () {
                //var index  = $(this).attr("id").substring($(this).attr("id").length - 1);
                //setThisLockedDiv(index);
            }
        });

        SearchResultsJS.OnClickState = function (event, ui) {
            var index = (SearchResultsJS.Switching) ? SwitchDataArray[current].locationIndex : event.locationIndex;
            state_autoCompleteID = (SearchResultsJS.Switching) ? SwitchDataArray[current].state_id : ui.value;
            SearchResultsJS.changedState = (state_autoCompleteID != $("#state" + index).val()) ? true : false;

            if (index > 0) {
                $("div#include-surrounds-inputPair" + index).css("display", "none");
                /*var locationName = $("div#location-city-selected" + index).find("div.selection span").text();
                locationID = $('input.autoCompletes').filter(function () { return $(this).attr("location") == locationName; }).attr("id");
                var locationPos = locationID.charAt(locationID.length - 1);*/
            }

            if (state_autoCompleteID > 0) { //If this state has a valid autocomplete id, load its cities
                if (!SearchResultsJS.Switching) {
                    var locationType = $("#state" + index + " option[value=" + state_autoCompleteID + "]").attr("typeid");
                    if (index == 0) $("input#temp").val(state_autoCompleteID).attr("typeid", locationType);
                    if (index > 0) {
                        $("input#autoComplete" + index).val(state_autoCompleteID).attr("typeid", locationType);
                        SearchResultsJS.HasFilterChanged = true;
                        SearchResultsJS.UpdateSearchData();
                    }
                    $("#state" + index + " option[value=" + state_autoCompleteID + "]").attr("ischosen", "yes");
                }
                SearchResultsJS.ShowCities(state_autoCompleteID, $("#city" + index));
            }
            else {
                SearchResultsJS.ResetDropDown($("#city" + index), "0", "Select City");
                SearchResultsJS.ResetDropDown($("#neighborhood" + index), "0", "Select Neighborhood");
                if (!SearchResultsJS.Switching && SearchResultsJS.changedState) {
                    $("#state" + index + " option").removeAttr("ischosen");
                    //$("#autoComplete"+index).val($("#country"+index).val());
                    var locationType = $("#country" + index + " option[value=" + $("#country" + index).val() + "]").attr("typeid");
                    if (index == 0) $("input#temp").val($("#country" + index).val()).attr("typeid", locationType);
                    if (index > 0) {
                        var country_autocompleteID = $("input#autoComplete" + index).attr("original-autocpid");
                        var country_autocompleteTypeID = $("input#autoComplete" + index).attr("original-typeid");
                        $("input#autoComplete" + index).val(country_autocompleteID).attr("typeid", country_autocompleteTypeID);
                        SearchResultsJS.HasFilterChanged = true;
                        SearchResultsJS.UpdateSearchData();
                    }
                }
            }

            //if (!SearchResultsJS.Switching) $("#autoComplete"+index).attr("source","browse");
            //SearchResultsJS.GetPropertiesCount();
            return true;
        };

        $(".state-select").multiselect({
            click: function (event, ui) {
                SearchResultsJS.Switching = false;
                if ($(this).find("option").length > 1) {  //ignore the selection if there is no option
                    event.locationIndex = $(this).attr("id").substring($(this).attr("id").length - 1);
                    SearchResultsJS.OnClickState(event, ui);
                }
            },
            close: function () {
                //var index  = $(this).attr("id").substring($(this).attr("id").length - 1);
                //setThisLockedDiv(index);
            }
        });

        SearchResultsJS.OnClickCity = function (event, ui, task) {
            var index = (SearchResultsJS.Switching) ? SwitchDataArray[current].locationIndex : event.locationIndex;
            city_autoCompleteID = (SearchResultsJS.Switching) ? SwitchDataArray[current].city_id : ui.value;
            SearchResultsJS.changedCity = (city_autoCompleteID != $("#city" + index).val()) ? true : false;

            if (city_autoCompleteID == "more") { //there is no more link
                //Handle 'more' events here
                SearchResultsJS.MoreCities($("#state" + index).val(), $("select#city" + index));
                return false;
            }
            else {
                /*if (index > 0) {
                var locationName = $("div#location-city-selected" + index).find("div.selection span").text();
                locationID = $('input.autoCompletes').filter(function () { return $(this).attr("location") == locationName; }).attr("id");
                var locationPos = locationID.charAt(locationID.length - 1);
                }*/

                var val = parseInt(city_autoCompleteID);
                if (val > 0) {  //If this city has a valid autocompleteID, show its neighborhoods
                    var chosenOption = $("#city" + index + " option[value=" + city_autoCompleteID + "]");
                    if (!SearchResultsJS.Switching) {
                        if (index == 0) $("input#temp").val(city_autoCompleteID).attr({ "typeid": $(chosenOption).attr("typeid"), "chosen_city": $(chosenOption).text() });
                        if (index > 0) {
                            $("input#autoComplete" + index).val(city_autoCompleteID).attr("typeid", $(chosenOption).attr("typeid"));
                            SearchResultsJS.HasFilterChanged = true;
                            SearchResultsJS.UpdateSearchData();
                        }
                        $("#city" + index + " option[value=" + city_autoCompleteID + "]").attr("ischosen", "yes");
                    }
                    if (index > 0) {
                        var expression = new RegExp('((and vicinity))');
                        if (!expression.test($(chosenOption).text())) {
                            $("div#location-city-selected" + index).find(".inputPair label").text("Also search area surrounding " + $(chosenOption).text());
                            $("div#include-surrounds-inputPair" + index).css("display", "block");
                        }
                        else {
                            $("div#include-surrounds-inputPair" + index).css("display", "none");
                        }
                    }
                    SearchResultsJS.ShowNeighborhoods(city_autoCompleteID, $("#neighborhood" + index));
                }
                else {
                    if (index > 0) $("div#include-surrounds-inputPair" + index).css("display", "none");
                    SearchResultsJS.ResetDropDown($("#neighborhood" + index), "0", "Select Neighborhood");

                    if (!SearchResultsJS.Switching && SearchResultsJS.changedCity) {
                        $("#city" + index + " option").removeAttr("ischosen");
                        var lastValidSelect = $("#city" + index).parent().find("select").has("option[ischosen]").last();
                        var lastValidAutocpID = $(lastValidSelect).val();
                        var lastValidTypeID = $(lastValidSelect).find("option[value=" + lastValidAutocpID + "]").attr("typeid");
                        if (index == 0) $("input#temp").val(lastValidAutocpID).attr("typeid", lastValidTypeID);
                        if (index > 0) {
                            if ($(lastValidSelect).length == 0) {
                                var original_autocompleteID = $("input#autoComplete" + index).attr("original-autocpid");
                                var original_autocompletetypeID = $("input#autoComplete" + index).attr("original-typeid");
                                $("input#autoComplete" + index).val(original_autocompleteID).attr("typeid", original_autocompletetypeID);
                            }
                            else {
                                $("input#autoComplete" + index).val(lastValidAutocpID).attr("typeid", lastValidTypeID);
                            }
                            SearchResultsJS.HasFilterChanged = true;
                            SearchResultsJS.UpdateSearchData();
                        }
                    }
                }

                //if (!SearchResultsJS.Switching) $("#autoComplete"+index).attr("source","browse");
                //SearchResultsJS.GetPropertiesCount();
                return true;
            }
        };

        $(".city-select").multiselect({
            click: function (event, ui) {
                SearchResultsJS.Switching = false;
                event.locationIndex = $(this).attr("id").substring($(this).attr("id").length - 1);
                SearchResultsJS.OnClickCity(event, ui);
            },
            close: function () {
                //var index = $(this).attr("id").substring($(this).attr("id").length - 1);
                //setThisLockedDiv(index);
            }
        });

        SearchResultsJS.OnClickNeighborhood = function (event, ui) {
            var index = (SearchResultsJS.Switching) ? SwitchDataArray[current].locationIndex : event.locationIndex;
            neighborhood_autoCompleteID = (SearchResultsJS.Switching) ? SwitchDataArray[current].neighborhood_id : ui.value;
            SearchResultsJS.changedNeighborhood = (neighborhood_autoCompleteID != $("#neighborhood" + index).val()) ? true : false;

            /*if (index > 0) {
            var locationName = $("div#location-city-selected" + index).find("div.selection span").text();
            locationID = $('input.autoCompletes').filter(function () { return $(this).attr("location") == locationName; }).attr("id");
            var locationPos = locationID.charAt(locationID.length - 1);
            }*/

            if (neighborhood_autoCompleteID > 0) { //If this neighborhood has a valid autocompleteID
                if (!SearchResultsJS.Switching) {
                    var locationType = $("#neighborhood" + index + " option[value=" + neighborhood_autoCompleteID + "]").attr("typeid");
                    if (index == 0) $("input#temp").val(neighborhood_autoCompleteID).attr("typeid", locationType);
                    if (index > 0) {
                        $("input#include-surrounds" + index).attr('checked', false);
                        $("input#autoComplete" + index).val(neighborhood_autoCompleteID).attr("typeid", locationType);
                        $("input#autoComplete" + index).attr("surroundings", 0);
                        SearchResultsJS.HasFilterChanged = true;
                        SearchResultsJS.UpdateSearchData();
                    }
                    $("#neighborhood" + index + " option[value=" + neighborhood_autoCompleteID + "]").attr("ischosen", "yes");
                }
            }
            else {
                if (!SearchResultsJS.Switching && SearchResultsJS.changedNeighborhood) {
                    $("#neighborhood" + index + " option").removeAttr("ischosen");
                    var lastValidSelect = $("#neighborhood" + index).parent().find("select").has("option[ischosen]").last();
                    var lastValidAutocpID = $(lastValidSelect).val();
                    var lastValidTypeID = $(lastValidSelect).find("option[value=" + lastValidAutocpID + "]").attr("typeid");
                    if (index == 0) $("input#temp").val(lastValidAutocpID).attr("typeid", lastValidTypeID);
                    if (index > 0) {
                        if ($(lastValidSelect).length == 0) {
                            var original_autocompleteID = $("input#autoComplete" + index).attr("original-autocpid");
                            var original_autocompletetypeID = $("input#autoComplete" + index).attr("original-typeid");
                            $("input#autoComplete" + index).val(original_autocompleteID).attr("typeid", original_autocompletetypeID);
                        }
                        else {
                            $("input#autoComplete" + index).val(lastValidAutocpID).attr("typeid", lastValidTypeID);
                        }
                        SearchResultsJS.HasFilterChanged = true;
                        SearchResultsJS.UpdateSearchData();
                    }
                }
            }
            //$("#autoComplete"+index).attr("source","browse");
            //SearchResultsJS.GetPropertiesCount();
        };

        $(".neighborhood-select").multiselect({
            click: function (event, ui) {
                SearchResultsJS.Switching = false;
                event.locationIndex = $(this).attr("id").substring($(this).attr("id").length - 1);
                SearchResultsJS.OnClickNeighborhood(event, ui);
            },
            close: function () {
                //var index = $(this).attr("id").substring($(this).attr("id").length - 1);
                //setThisLockedDiv(index);
            }
        });

    }   //end if ($('div#modify-results').length)

});                 //end document-ready
