var ab_none='<option value=0>Не выбрано</option>';

	function ab_models(fromcoo){
		if(fromcoo) {
			if(getCookie('ab_mark')!=''&& getCookie('ab_mark')!=null) $('#_model').show(); else $('#_model').hide();
			return;
		}
		delCookie('ab_mark');
		delCookie('ab_model');
		delCookie('ab_modif');
		delCookie('ab_year');
		$('#model').val('');
		$('#year').val('');
		$('#modif').val('');
		$('#year,#modif,#model').html(ab_none);		
		$('#_year,#_modif').hide('fast');
		if($('#mark').val()=='') {$('#_model').hide('fast'); return;}
		$('#model').attr('disabled','disabled').after(loading);
		$('#_model').show('fast');
		setCookie('ab_mark',$('#mark').val());
		$.ajax({ 
			url: '/ajx/avto_podbor.html', data: { act:'get_models', mark:$('#mark').val()},
			complete: function(){
				$('#model + #loading').remove();
				$('#model').attr('disabled','');
			},
			success: function(res){
				if(res.fres) {
					$('#model').html(res.s);
				} else _alert('ОШИБКА! '+res.err_msg);
			}
		});
	};

	function ab_years(fromcoo){
		if(fromcoo) {
			if(getCookie('ab_model')!='' && getCookie('ab_model')!=null) $('#_year').show(); else $('#_year').hide();
			return; 
		}
		delCookie('ab_model');
		delCookie('ab_modif');
		delCookie('ab_year');
		$('#year').val('');
		$('#modif').val('');
		$('#modif').html(ab_none);
		$('#_modif').hide('fast');
		if($('#model').val()=='') {$('#_year').hide('fast'); return;}
		$('#year').attr('disabled','disabled').after(loading);
		$('#_year').show('fast');
		setCookie('ab_model',$('#model').val());
		$.ajax({ 
			url: '/ajx/avto_podbor.html', data: { act:'get_years', mark:$('#mark').val(), model:$('#model').val()},
			complete: function(){
				$('#year + #loading').remove();
				$('#year').attr('disabled','');
			},
			success: function(res){
				if(res.fres) {
					$('#year').html(res.s);
				} else _alert('ОШИБКА! '+res.err_msg);
			}
		});
	}

	function ab_modif(fromcoo){
		if(fromcoo) {
			if(getCookie('ab_year')!=''&& getCookie('ab_year')!=null) $('#_modif').show(); else $('#_modif').hide();
			return; 
		}
		delCookie('ab_year');
		delCookie('ab_modif');
		$('#modif').val('');
		if($('#year').val()=='') {$('#_modif').hide('fast'); return;}
		$('#modif').attr('disabled','disabled').after(loading);
		$('#_modif').show('fast');
		setCookie('ab_year',$('#year').val());
		$.ajax({ 
			url: '/ajx/avto_podbor.html', data: { act:'get_modif', mark:$('#mark').val(), model:$('#model').val(), year:$('#year').val()},
			complete: function(){
				$('#modif + #loading').remove();
				$('#modif').attr('disabled','');
			},
			success: function(res){
				if(res.fres) {
					$('#modif').html(res.s);
				} else _alert('ОШИБКА! '+res.err_msg);
			}
		});
	}

function apGo(){
	var v=$('#mark').val()!='' && $('#mark').val()!='0'?('/'+$('#mark').val()):'';
	var m=$('#model').val()!='' && $('#model').val()!='0'?('/'+$('#model').val()):'';
	var y=$('#year').val()!='' && $('#year').val()!='0'?('/'+$('#year').val()):'';
	var md=$('#modif').val()!='' && $('#modif').val()!='0'?('/'+$('#modif').val()):'';
	var s=v+m+y+md+'.html';
	location.href='/avtoPodbor'+s;
}

$(document).ready(function(){

	$('#mark').change(function(res){
		ab_models(false);
	});

	$('#model').change(function(res){
		ab_years(false);
	});

	$('#year').change(function(res){
		ab_modif(false);
	});

	$('#modif').change(function(res){
		setCookie('ab_modif',$('#modif').val());
	});
	
	$('.uab_vendors').click(function(){
		setCookie('ab_mark',$(this).attr('sname'));
		delCookie('ab_model');
		delCookie('ab_year');
		delCookie('ab_modif');
	});
	$('.uab_models').click(function(){
		setCookie('ab_model',$(this).attr('sname'));
		delCookie('ab_year');
		delCookie('ab_modif');
	});
	$('.uab_years').click(function(){
		setCookie('ab_year',$(this).attr('sname'));
		delCookie('ab_modif');
	});
	$('.uab_modifs').click(function(){
		setCookie('ab_modif',$(this).attr('sname'));
	});

	ab_models(true);
	ab_years(true);
	ab_modif(true);

});
