﻿jQuery(document).ready(function() {
    // do stuff when DOM is ready
    var regionDropdown = jQuery('#officeLocationsFilters .region');
    var countriesDropdown = jQuery('#officeLocationsFilters .country');
    jQuery('#officeLocationsFilters .button').click(function() {
        window.location.replace('?catl1=' + regionDropdown.val() + '&catl2=' + countriesDropdown.val());
        return false;
    });

    regionDropdown.change(function() {

        var onError = function(xhr, textStatus, errorThrown) {};
        var onSuccess = function(data, textStatus) {
            jQuery('>option:not(:eq(0))', countriesDropdown).remove();
            jQuery.each(data.Data, function(i, item) {
                countriesDropdown.append(jQuery('<option></option>').val(item.Id).html(item.Name));
            });
            countriesDropdown.attr('disabled', false);
            countriesDropdown.focus();
        };

        var countryId = jQuery(this).val();
        if (countryId && countryId.length > 0) {
            jQuery.ajax({
                type: 'GET',
                url: '/api/v1_0/categories/officeLocationRegions/' + countryId,
                dataType: 'json',
                error: onError,
                success: onSuccess
            });
        } else {
            jQuery('>option:not(:eq(0))', countriesDropdown).remove();
            countriesDropdown.attr('disabled', true);
        }
    });

});