﻿// This java script file can only be used with the PropertySearch.ascx.
// If you want to use the similar functionality in other pages please copy.

$(document).ready(function() {

    // Hide the start date and end date input boxes as for now we will not use them.
    $("#divDates").hide();

    // Add changing handlers on city and country input box to refresh data.
    $("#city").change(updateAreas);

    $("#country").change(function() {
        updateCities();
    });

    // Update the area visibility based on selected city.
    updateAreas();

});

function updateCities(selectedIndex) {
    $.getJSON(
                "/Property/GetCitiesByCountry",
                { country: $("#country").val() },
                function(data) {
                    $("#city").fillSelect(data);
                    $("#city")[0].selectedIndex = selectedIndex;
                    updateAreas();
                });
}

function updateAreas() {
    if ($("#city").val() == 1) {
        $("#divArea").show();
        $("#divEmptyArea").hide();
    }
    else {
        $("#divArea").hide();
        $("#divEmptyArea").show();
    }
}

$.fn.clearSelect = function() {
    return this.each(function() {
        if (this.tagName == 'SELECT') {
            this.options.length = 0;
        }
    });
}

$.fn.fillSelect = function(data) {
    return this.clearSelect().each(function() {
        if (this.tagName == 'SELECT') {
            var dropdownList = this;
            $.each(data, function(index, optionData) {
                var option = new Option(optionData.Text, optionData.Value);

                if ($.browser.msie) {
                    dropdownList.add(option);
                }
                else {
                    dropdownList.add(option, null);
                }
            });
        }
    });
}
