$(document).ready(function() {
  $('select.with_subtypes').each(function() {
    if (!$(this).attr('multiple')) {
    
      $(this).change(function(e) {
        $(this).blur();
        var id = $(this).val();
        if (!id) {
          if ($(this).data("submarker")) {
            var subsel =  $(this).parents('form').find('select[name="' + $(this).data("submarker") + '"], #'+ $(this).data("submarker")).get(0);
            var firstoption = $(subsel).find('option:first').clone();
            $(subsel).html(firstoption);
          }
          return;
        }
        var thissel = $(this);
                
        var ads = 0;
        var name = $(this).attr('name');
        if ((name.search('county') != -1 || name.search('parish') != -1) && name.search('old_') == -1)
          ads = 1;
                
        $.loadingIndicator.init(true, e);
        $.getJSON("json.php?type=classificator_subtypes", {
          id : id,
          ads : ads
        },
        function(j){
          $.loadingIndicator.stop();
          if (j.status == 'ok') {
            var prefix = name
            .replace(/\[/g, "")
            .replace(/\]/g, "")
            .replace(new RegExp(j.marker), "");
            var subsel =  $(thissel).parents('form').find('select[name="' + prefix + j.submarker + '"], #'+ prefix + j.submarker).get(0);
            if (!subsel) return alert('no_subselect');
            $(thissel).data("submarker", prefix + j.submarker);
            var firstoption = $(subsel).find('option:first').clone();
            $(subsel).html(firstoption);
            $.each(j.results, function(i,item){
              $("<option></option>").val(item.id).text(item.description).appendTo(subsel);
            });
          } else {
            alert(j.message);
          }
                    
        });
        
      });

        
    } else {

      $(this).change(function(e) {
        $(this).blur();
        var ids = new String($(this).val());
               
        if (ids == 'null') {
          if ($(this).data("submarker")) {
            var subsel =  $(this).parents('form').find('select[name="' + $(this).data("submarker") + '"], #'+ $(this).data("submarker")).html('');
          }
          return;
        }
        var thissel = $(this);
        var ads = 0;
        var name = $(this).attr('name');
        var prefix = '';
        if ((name.search('county') != -1 || name.search('parish') != -1) && name.search('old_') == -1)
          ads = 1;
                
        $.loadingIndicator.init(true, e);
        $.getJSON("json.php?type=classificator_subtypes", {
          ids : ids,
          ads : ads
        },
        function(j){
          $.loadingIndicator.stop();
          if (j.status == 'ok') {
            var prefix = name
            .replace(/\[/g, "")
            .replace(/\]/g, "")
            .replace(new RegExp(j.marker), "");
            var subsel =  $(thissel).parents('form').find('select[name="' + prefix + j.submarker + '\[\]"], #'+ prefix + j.submarker).get(0);
            if (!subsel) return alert('no_subselect');
            $(thissel).data("submarker", prefix + j.submarker);
            var existst = new Array();
            $(subsel).children().each(function(i){
              if ($.inArray($(this).val(), j.ids) != -1) {
                existst[i] = $(this).val();
              } else {
                $(this).remove();
              }
            })
                          
            $.each(j.results, function(i,item){
              if ($.inArray(item.id, existst) == -1) {
                $("<option></option>").val(item.id).text(item.description).appendTo(subsel);
              }
            });
          } else {
            alert(j.message);
          }
        });
        
      })
        
    }
        
    
  })
});
