function getCondition(){
    if (document.getElementsByName('mss3_inv_search').length>0){
    var cond =0;
    switch(true) {
        case ($('.mss3_inv_search')[0].checked): // new
            cond=1;
            break;
        case ($('.mss3-check-certified')[0].checked):  // certified
            cond=3;
            break;
        case ($('.mss3_inv_search')[1].checked):  // pre-owned
            cond=2;
            break;
        }} 
    return cond;
}

function search_simple_ajax3(container)
{	
	advanced_search({
		'yearfrom': $.trim(container.find('.mss3-year').val()),
		'make': $.trim(container.find('.mss3-make').val()),
		'model':$.trim((container.find('.mss3-make').val()?container.find('.mss3-model').val():'')),
		'condition': getCondition(),
		'keyword' :$('#mss3_input_keyword').val()
	}, simple3_status);
}

$(document).ready(function(){
    
    $('#simpf_used, #simpf_all').click(function(){
        var _href = website;
        var _key  = $('#mss3_input_keyword').val();
        _key =  escape(_key.replace('_','+').replace('/',' ')); 
        
        if(version == '9')
            var u = simple3_status == 'sold'?'/sold_':'/cars-for-sale_';
        else 
            var u = simple3_status == 'sold'?'/vehicle/sold_':'/vehicle_';
        
        if($(this).attr('id') == 'simpf_used'){
            if(version == '9')
               _href += u+'condition_2';
            else
                _href += u+'filter_condition_USED';
        } else {
            if(version == '9')
                _href += u+"/condition_3";
            else
                _href += u+"filter_condition_CERTIFIED+PRE-OWNED";
        }
        
       if(_key){
           if(version == '9')
               _href += "_keyword_"+_key;
           else
               _href += "_filter_keywords_"+_key;
       } else{
           _href += ".html";
       }
       
       $(this).attr('href', _href); 
        
    }); 
   
    if (document.getElementsByName('mss3_inv_search').length>0){
			document.getElementsByName('mss3_inv_search')[0].onclick = function(){
                selectChainCondition(getCondition());  
            }
			document.getElementsByName('mss3-check-certified')[0].onclick = function (){
                selectChainCondition(getCondition());
            }

			document.getElementsByName('mss3_inv_search')[1].onclick = function(){
                selectChainCondition(getCondition());
            }
    }

    $('.mss3_inv_search:radio:first').attr('checked', true);
    $('.mss3-check-certified:checkbox:first').attr('checked', false);
    $('select#mss3-model, select#mss3-make').val('').attr('disabled', true);

$('#mss3_input_keyword').keyup(function(a,b,c){
		if (a.keyCode==13)
			search_simple_ajax3($('.modul-simple3'));
	});
	
    function selectChainCondition(cond){
        $.ajax({
                url:		'ajax?'+simple3_params,
                dataType:	'json',
                type:		'POST',
                data:		{oper:'selectchain_vehicle',selectchain:'condition',condition:cond, target:'year'},
                success:	function(result){
                    $('.mss3-make').html('<option value="">Select Make</option>');
		            $('.mss3-model').html('<option value="">Select Model</option>');
                    $('#mss3-year').html('<option value="">Select Year</option>');
                    for (id in result)
                    {
                        $('#mss3-year').append('<option value="'+result[id].name+'">'+result[id].name+' or newer ('+result[id].count+')</option>')
                    }
                }
            });
    }

	$('.modul-simple3').each(function(){
		var container = $(this);
		var sYear = container.find('.mss3-year');
		var sMake = container.find('.mss3-make');
		var sModel = container.find('.mss3-model');
		if (sYear.length && sMake.length && sModel.length)
		{
			sYear.change(function(){
				var val = $(this).val();
				$.ajax({
					url:		'ajax?'+simple3_params,
					dataType:	'json',
					type:		'POST',
					data:		{oper:'selectchain_vehicle',selectchain:'year',year:sYear.val(), condition: getCondition()},
					success:	function(result){
						sMake.find("option[value!='']").remove();
						sMake.find("option[value=='']").attr('selected','selected');
						sModel.find("option[value!='']").remove();
						sModel.find("option[value=='']").attr('selected','selected');
						for (id in result)
						{
							sMake.append('<option value="'+id+'">'+result[id].name+' ('+result[id].count+')</option>')
							sMake.removeAttr('disabled');
						}
						sModel.attr('disabled','disabled');
					}
				});
			});
			sMake.change(function(){
				var val = $(this).val();
				$.ajax({
					url:		'ajax?'+simple3_params,
					dataType:	'json',
					type:		'POST',
					data:		{oper:'selectchain_vehicle',selectchain:'make',make:val,year:sYear.val(), condition: getCondition()},
					success:	function(result){
						sModel.find("option[value!='']").remove();
						sModel.find("option[value=='']").attr('selected','selected');
						if (result.length !== 0)
						{
							for (id in result)
							{
								sModel.append('<option value="'+id+'__'+$.trim(result[id].name)+'">'+result[id].name+' ('+result[id].count+')</option>')
								sModel.removeAttr('disabled');
							}
						}
						else
						{
							sModel.attr('disabled','disabled');
						}
					}
				});
			});
		}
	});
	
	$('.mss3_inv_search').click(function(){
	// no alternative layout
		if($(".simpf-selHeader-label3").length ==0){
			if( $('#mss3-radio-new').attr('checked')){$('.simpf-selHeader-label1').show();$('.simpf-selHeader-label2').hide(); $("#mss3_inv_search").attr('checked',false);}
			if( $('#mss3-radio-preowned').attr('checked')){$('.simpf-selHeader-label2').show();$('.simpf-selHeader-label1').hide();$("#mss3_inv_search").attr('checked',false);}
		}else{ //use alternative layout
			if( $('#mss3-radio-new').attr('checked')){$('.simpf-selHeader-label1').show();$('.simpf-selHeader-label3').hide(); $("#mss3_inv_search").attr('checked',false);$(".simpf3-contdiv").show();$('.simpf-selHeader-label2').hide();$('.simpf3-sel_A').show();$('.simpf3-sel_A','.simpf-selHeader-label3').hide();$('.simpf3-submit').show();}
			if( $('#mss3-radio-preowned').attr('checked')){
				var w = $('.simpf3-midd').height();
				$('.simpf-selHeader-label3').show();$('.simpf-selHeader-label1').hide();$('.simpf-selHeader-label2').hide();$("#mss3_inv_search").attr('checked',false);$(".simpf3-contdiv").hide();$('.simpf3-sel_A').hide();$('.simpf3-sel_A','.simpf-selHeader-label3').show();$('.simpf3-submit').hide(); $('.simpf3-midd').height(w);}
		}
	});
	
	switch(ssimmple3_layout) {
	  case 'used': 
	     $('.simpf-selHeader-label1').hide();$('.simpf-selHeader-label2').show();
	    $('#mss3-radio-preowned').parent('label').hide();
	      $('#mss3-radio-new').parent('label').hide();
	       $('#mss3-radio-preowned').attr('checked',true);
	    break;
	  case 'new':
	   
	     $('#mss3-radio-preowned').parent('label').hide();
	       $('#mss3-radio-new').parent('label').hide();
	     $('#mss3-radio-new').attr('checked',true);
	    break;
	}
});

