$(function() {
   // handeling the search form
	$('#search_form select').change( function() {
		// switch keywords availibility
      /*if ($('#manufacturer')[0].value!='' || $('#body')[0].value!='' || $('#start_price')[0].value!='0' || $('#end_price')[0].value!='0')
         $('#keywords_input')[0].disabled = true;
      else $('#keywords_input')[0].disabled = false; */

      //if (this.name=='manufacturer') $('#model').attr('disabled', false);

      // if manufacturer switch - reset model select
  		//if ($(this).name='manufacturer') $('#model_id').attr('value','');

      var changed_el = this;
      //var load_complete = true;
      $('#search_form select').not('#change_currency_select').not('#'+changed_el.name).each(function (el) {      	   this.disabled=true;
      	   $('#search_but').css('opacity',0.5).attr('disabled',true);
      	   var dealer;
      	   if ($('#dealer:hidden').size()==0) dealer=0;
      	   else dealer = $('#dealer')[0].value;
      	   $('#'+this.name).html('<option value="'+this.value+'">Loading...</option>').load('/inc/get_search_list.php', {      	   	manufacturer: $('#manufacturer').attr('value'),
      	   	body: $('#body').attr('value'),
      	   	start_price: $('#start_price').attr('value'),
      	   	end_price: $('#end_price').attr('value'),
      	   	price_range: $('#price_range').attr('value'),
      	   	start_year: $('#start_year').attr('value'),
      	   	end_year: $('#end_year').attr('value'),
      	   	get_type: this.name,
      	   	type: $('#type').attr('value'),
      	   	region: $('#region').attr('value'),
      	   	model_id: $('#model_id').attr('value'),
      	   	dealer: dealer},
      	   	function() {      	   		this.disabled = false;
      	   		//console.log('!!!!!!!!');
      	   		//console.log($('#search_form [disabled]'));
      	   		if ($('#search_form [disabled]').length==1) $('#search_but').css('opacity',1).attr('disabled',false);
      	   	}
      	   );
		});
	});

	// first time load models list
	if ($('#manufacturer').length>0)
		if ($('#manufacturer')[0].value!='') $('#model_id').load('/inc/get_search_list.php?get_type=model_id', {'manufacturer':$('#manufacturer')[0].value}).attr('disabled',false);
	//alert($('#manufacturer')[0].value);

	//if ($('#manufacturer')[0].value!='' || $('#body')[0].value!='') $('#search_form select').trigger('change');


});
