jQuery(document).ready(function() { 
    var options = { 
        target:        '#price', 
		beforeSubmit:  checkRequest,
        timeout:   3000 
    }; 
 
    jQuery('#price-form').submit(function() { 
        jQuery(this).ajaxSubmit(options); 
		return false; 
    }); 
}); 
function checkRequest(formData, jqForm, options) { 
	if(jQuery("#mark").val().length && jQuery("#model").val().length) {
		return true
	}
	else {
		alert("Заполните необходимые поля");
		return false;
	}
} 

function getOptions(elem) {
	jQuery("#price").html('');
	var mark = jQuery("#mark").val();
	var model=jQuery("#model").val();
	var year=jQuery("#year").val();
	var body_type=jQuery("#body_type").val();
	var engine_type=jQuery("#engine_type").val();
	var steering_wheel=jQuery("#steering_wheel").val();
	var transmission=jQuery("#transmission").val();
	var km_age_to=jQuery("#km_age_to").val();
	if(model.length) {
		if(jQuery(elem).attr("id")=="mark"){
			jQuery.ajax({url:"/price/index.php?mark="+mark,type:"GET",data:{},error:function(){
					alert("Произошла ошибка. Перезагрузите страницу или попробуйте позже.")
				},success:function(result){
					jQuery("#model").replaceWith(jQuery(result).find("#model-cont").html());
					jQuery(".price-form select[id != 'mark'][id != 'model']").attr("disabled","disabled");
				}
			})
		}
		if(jQuery(elem).attr("id")=="model"){
			jQuery.ajax({url:"/price/index.php?mark="+mark+"&model="+model,type:"GET",data:{},
				error:function(){
					alert("Произошла ошибка. Перезагрузите страницу или попробуйте позже.")
				},success:function(result){
					jQuery(".price-form select[id != 'mark']").each(function(l){
						var m=jQuery(this).attr("id")+"-cont";
						if(jQuery(result).find("#"+m).length){
							jQuery("#"+jQuery(this).attr("id")).replaceWith(jQuery(result).find("#"+m).html())
						}
					});
				}
			})
		}
		else {
			if(jQuery("body.index").length){}
			else {
				jQuery.ajax({url:"/price/index.php?mark="+mark+"&model="+model+"&year="+year+"&body_type="+body_type+"&engine_type="+engine_type+"&steering_wheel="+steering_wheel+"&transmission="+transmission+"&km_age_to="+km_age_to,type:"GET",data:{},
					error:function() {
						alert("Произошла ошибка. Перезагрузите страницу или попробуйте позже.")
					},success:function(result){
						jQuery(".price-form select[id != 'mark']").each(
							function(l){
								var m=jQuery(this).attr("id")+"-cont";
								if(jQuery(result).find("#"+m).length) {
									jQuery("#"+jQuery(this).attr("id")).replaceWith(jQuery(result).find("#"+m).html())
								}
							}
						);
					}
				})
			}
		}
	}
	else {
		jQuery.ajax({url:"/price/index.php?mark="+mark,type:"GET",data:{},error:function() {
				alert("Произошла ошибка. Перезагрузите страницу или попробуйте позже.")
			},success:function(result) {
				jQuery("#model").replaceWith(jQuery(result).find("#model-cont").html());
			}
		});
	}

}


