﻿(function($){
  // очищаем select
  $.fn.clearSelect = function() {
    return this.each(function(){
      if(this.tagName=='SELECT') {
        this.options.length = 0;
        $(this).attr('disabled','disabled');
      }
    });
  }
  // заполняем select
  $.fn.fillSelect = function(dataArray) {
    return this.clearSelect().each(function(){
      if(this.tagName=='SELECT') {
        var currentSelect = this;
        $.each(dataArray,function(index,data){
          var option = new Option(data.text,data.value);
          if($.support.cssFloat) {
            currentSelect.add(option,null);
          } else {
            currentSelect.add(option);
          }
        });
      }
    });
  }

})(jQuery);

// Автозаполнение марки
function adjustBrand() {
  var tmpSelect = $('#brand');
    $.getJSON(
      '/index.php?id=170',
      {},
      function(data) {
        tmpSelect.fillSelect(data).attr('disabled','');
    })
};  

// Выбор марки
function adjustModel(){
  var brandValue = $('#brand').val();
  var tmpSelect = $('#model');
  if(brandValue.length == 0) {
    tmpSelect.attr('disabled','disabled');
    tmpSelect.clearSelect();
  } else {
    $.getJSON(
      '/index.php?id=170',
      {brand:brandValue},
      function(data) {
        tmpSelect.fillSelect(data).attr('disabled','');
    });
  }
};
function loadTyres(){
  var modelValue = $('#model').val();
  if(modelValue.length == 0) {
    //$('#select_result').attr('style','display:none;');
  } else {
    //$('#select_result').attr('style','display:block;');  
	$('#select_result').load(
      '/index.php?id=170',
      {model:modelValue});
  }
};
