$(document).ready(function() {    
    $(".JCountry").change(function() {
        var regionsDropDownSelector = $(this).parentForm().find(".JRegion");
        var locationsDropDownSelector = $(this).parentForm().find(".JLocation");
        var regionCurrentValue = $(this).parentForm().find(".JCurrentRegion").val();
        
        var defaultRegionTitle = $(this).parentForm().find(".JRegionDefault").val();
        
        var countryId = $(this).val();
        if (countryId == "0") {
            regionsDropDownSelector.find("option").remove();
            regionsDropDownSelector.parent().hide();
            locationsDropDownSelector.find("option").remove();
            locationsDropDownSelector.parent().hide();
            return;
        }
        $.post("ajax",
               {
                   func: "loadRegionsListUnderProfiles",
                   country_id: countryId
               },
               function(data){
                   var len = data.length;
                   if (len > 0) {
                       var regionId = regionCurrentValue;
                       var options = '<option value="0">' + defaultRegionTitle + '</option>';
                       for (var i = 0; i < data.length; i++) {
                           if (data[i]['id'] == regionId) {
                               options += '<option value="' + data[i]['id']+ '" selected="yes">';
                           } else {
                               options += '<option value="' + data[i]['id']+ '">';
                           }
                           options += data[i]['title'] + '</option>';
                       }
                       regionsDropDownSelector.html(options);
                       regionsDropDownSelector.parent().show();
                   } else {
                       regionsDropDownSelector.find("option").remove();
                       regionsDropDownSelector.parent().hide();
                   }
                   regionsDropDownSelector.change();
               },
               "json"
        );
    });
    $(".JRegion").change(function() {
        var locationsDropDownSelector = $(this).parentForm().find(".JLocation");
        var locationCurrentValue = $(this).parentForm().find(".JCurrentLocation").val();
        
        var defaultLocationTitle = $(this).parentForm().find(".JLocationDefault").val();
        
        var regionId = $(this).val();
        if (regionId == "0") {
            locationsDropDownSelector.find("option").remove();
            locationsDropDownSelector.parent().hide();
            return;
        }
        $.post("ajax",
               {
                   func: "loadLocationsListUnderProfiles",
                   region_id: regionId
               },
               function(data){
                   var len = data.length;
                   if (len > 0) {
                       var locationId = locationCurrentValue;
                       var options = '<option value="0">' + defaultLocationTitle + '</option>';
                       for (var i = 0; i < data.length; i++) {
                           if (data[i]['id'] == locationId) {
                               options += '<option value="' + data[i]['id']+ '" selected="yes">';
                           } else {
                               options += '<option value="' + data[i]['id']+ '">';
                           }
                           options += data[i]['title'] + '</option>';
                       }
                       locationsDropDownSelector.html(options);
                       locationsDropDownSelector.parent().show();
                   } else {
                       locationsDropDownSelector.find("option").remove();
                       locationsDropDownSelector.parent().hide();
                   }
                   locationsDropDownSelector.change();
               },
               "json"
        );
    });
    $(".JCountry").change();
});
