$(function(){
  $("select#category_id").change(function(){

    $("#brands_div").html("");
    document.getElementById('brands_div').style.height= '0px';
    document.getElementById('brands_div').style.border= 'none';
    document.getElementById('brands_image').style.visibility= 'hidden';
    document.getElementById('brand').style.width= '167px';
    document.getElementById('brand').readOnly = false;
    document.getElementById('brand').value = "";
    document.getElementById('brand').style.cursor = "text";
      if($(this).val() > 0){
      $("select#subcategory_id").html('<option value="0" >'+tr_First_Select_Category+'</option>');
      $("select#type_id").html('<option value="0" >'+tr_First_Select_Subcategory+'</option>');
      $.getJSON(getSubcategoriesURL+"/"+$(this).val(),{}, function(j){
      var options = '';
      for (var i = 0; i < j.length; i++) {
        options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
      }
      $("select#subcategory_id").html(options);
    })
    }else{
        document.getElementById('subcategory_id').selectedIndex=0;
        document.getElementById('type_id').selectedIndex=0;
        $("select#subcategory_id").html('<option value="0" >'+tr_First_Select_Category+'</option>');
        document.getElementById('brands_div').style.height= '0px';
        document.getElementById('brands_div').style.border= 'none';
        document.getElementById('brands_image').style.visibility= 'hidden';
        document.getElementById('brand').style.width= '167px';
        document.getElementById('brand').readOnly = false;
        document.getElementById('brand').value = "";
        document.getElementById('brand').style.cursor = "text";

        document.getElementById('model').value = "";
        document.getElementById('year_from').value = "";
        document.getElementById('year_to').value = "";
        document.getElementById('hours_from').value = "";
        document.getElementById('hours_to').value = "";
        document.getElementById('price_from').value = "";
        document.getElementById('price_to').value = "";
        razListe(document.getElementById('country_clear'),document.getElementById('country'),'Countries','country');
    }
  })
  $("select#subcategory_id").change(function(){

    $("#brands_div").html("");
    document.getElementById('brands_div').style.height= '0px';
    document.getElementById('brands_div').style.border= 'none';
    document.getElementById('brands_image').style.visibility= 'hidden';
    document.getElementById('brand').style.width= '167px';
    document.getElementById('brand').readOnly = false;
    document.getElementById('brand').value = "";
    document.getElementById('brand').style.cursor = "text";
      if($(this).val() > 0){
      $("select#type_id").html('<option value="0" >'+tr_First_Select_Subcategory+'</option>');
      $.getJSON(getTypesURL+"/"+$(this).val(),{}, function(j){
      var options = '';
      for (var i = 0; i < j.length; i++) {
        options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
      }
      $("select#type_id").html(options);
    })
    }
  })

  $("select#type_id").change(function(){
  if($(this).val() > 0){

    $.getJSON(getBrandsURL+"/"+$(this).val(),{}, function(j){
      $("#brand").val(tr_Brands);
      var str = "<p style='width:146px' onclick="+'"razListe(document.getElementById('+"'brand_clear'),document.getElementById('brand'),'"+tr_Brands+"','brand');"+'">';
      str += "<input type='checkbox'  id='brand_clear' name='brand_clear' value='1' style='border:none;' readonly='readonly'  onclick='this.checked = !this.checked;' /><span>"+tr_Clear_selected+"</span>";
      str += "</p>";
      var k = 0;

      for (var i = 0; i < j.length; i++) {
          if(j[i].optionValue>0){
            if(k%2 == 0){
                class_name = 'class="item1"';
            }else{
                class_name = '';
            }
            k++;
            str += "<p style='width:146px' "+class_name+" onclick="+'"majListe(document.getElementById('+"'brand_"+ j[i].optionValue +"'),document.getElementById('brand'),'"+tr_Brands+"','brand');"+'">';
            str +="<input id='hidden_brand_"+j[i].optionValue+"' type='hidden'  value='"+j[i].optionDisplay+"' />";
            str +="<input type='checkbox'  id='brand_"+j[i].optionValue+"' name='brand_id[]' value='"+j[i].optionValue+"' style='border:none;' readonly='readonly'  onclick='this.checked = !this.checked;' /><span>"+j[i].optionDisplay+"</span>";
            str +="</p>";
          }
      }

      $("#brands_div").html(str);
        document.getElementById('brands_div').style.height= '166px';
        document.getElementById('brands_div').style.border= '1px solid #000000';
        document.getElementById('brands_image').style.visibility= 'visible';
        document.getElementById('brand').style.width= '150px';
        document.getElementById('brand').readOnly = true;
        document.getElementById('brand').style.cursor = "pointer";

    })
    }else{
    document.getElementById('brands_div').style.height= '0px';
    document.getElementById('brands_div').style.border= 'none';
    document.getElementById('brands_image').style.visibility= 'hidden';
    document.getElementById('brand').style.width= '167px';
    document.getElementById('brand').readOnly = false;
    document.getElementById('brand').value = "";
    document.getElementById('brand').style.cursor = "text";
    }
  })

})
