
function initClassier()
{
	if( $(".classier").length > 0)
 	$.ajax({
	   type: "POST",
	   url: "/classier/getMarque",
	   async: true,
	   dataType: 'text',
	   success: function(msg){
	    
	       $(".classier").html(msg) ;
	   }
	 });
}

function getCity(id)
{
	var cp = document.getElementById(id).value;
	if(cp.length == 5)
		$.ajax({
		   type: "POST",
		   url: "/index.php/zonier/getCity",
		   data: "codePostal=" + cp,
		   async: true,
		   dataType: 'text',
		   success: function(msg){
		        $("#" + id).parent().find(".city").html(msg);	  
		        if(msg == "")
		        {
		        	 if($("#" + id).parent().find(".errcp").length > 0)
		        	 {
		        	 	$("#" + id).parent().find(".city").hide();
		        		$("#" + id).parent().find(".errcp").fadeIn();
		        		
		        		$("#" + id).parent().find(".validator").removeClass("good");
		        		$("#" + id).parent().find(".validator").addClass("bad");
		        	}
		        	$("#" + id).parent().find(".city").hide();
		        }
		        else
		        {
		        	 if($("#" + id).parent().find(".errcp").length > 0)
		        	 {
		        	 	$("#" + id).parent().find(".errcp").hide();
		        	 	$("#badCp").hide();
		        	 	$("#" + id).parent().find(".city").fadeIn();
		        	 	$("#" + id).parent().find(".validator").removeClass("bad");
		        		$("#" + id).parent().find(".validator").addClass("good");
		        	  }
		  		}
		   }
		 });
		else  $("#" + id).parent().find(".city").html("");
	
}

function chargeCylindree()
{
	$(".cylindree").html("<option value=''><i>Chargement...</i></option");

	var marque;
	var buf;
	
	if (buf = $(".classier").val().match(/^(.*)(_doublon)$/))
		marque = buf[1];
	else marque = $(".classier").val();
	
	$.ajax({
	   type: "POST",
	   url: "/classier/getCylindree",
	   data: "marque=" + marque,
	   async: true,
	   dataType: 'text',
	   success: function(msg){
	       $(".cylindree").html(msg) ;
	   }
	 });
}

function chargeModele()
{
	$(".model").html("<option value=''><i>Chargement...</i></option");

	$.ajax({
	   type: "POST",
	   url: "/classier/getModel",
	   data: "marque=" + $(".classier").val(),
	   async: true,
	   dataType: 'text',
	   success: function(msg){
	   
	       $(".model").html(msg) ;

	   }
	 });
}

function chargeVersion()
{

	$(".model").html("<option>Chargement...</option");

	$.getJSON(
	   "/classier/getVersionAMT",
	   {  marque: $(".classier").val(),
		  cylindree: $(".cylindree").val()
		  },
	   function(json){
	       $(".model").html(json.version);
	   });
	   
}

function filtreMoto()
{
	$.getJSON(
	   "/classier/getVersionAMT",
	   {  marque: $(".classier").val(),
		  modele: $(".model").val(),
		  cylindree: $(".cylindree").val()
		  },
	   function(json){
	       $(".versionMoto").val(json.version);
	   });
}

function chargeAnnee()
{
	$("#annee-Moto").html("<option>Chargement...</option");
	
	$.ajax({
	   type: "POST",
	   url: "/classier/getAnnee",
	   data: "marque=" + $(".classier").val() + "&modele=" + $(".classier + select").val(),
	   async: true,
	   dataType: 'text',
	   success: function(msg){
	    
	       $("#annee-Moto").html(msg) ;

	   }
	 });

}

function showRightImage(classM, group)
{
	if (classM == "" && group == "" && $(".model").val() != "" && getRadioValue("devis[M5]") != "1")
	{
		var detail = $(".model").val().split("|");
		$(".groupCondition").hide();
		group = detail[2];
		classM = detail[3];
	}
	
	if (classM != "" && getRadioValue("devis[M5]") != "1")
	{
		$("#conditions").show();

		if (group <= 8)
			$("#groupe_4_8").slideDown();
		else if (group <= 10)
			$("#groupe_9_10").slideDown();
		else if (group <= 12)
			$("#groupe_11_12").slideDown();
		else if (group <= 14)
			$("#groupe_13_14").slideDown();
		else if (group <= 12)
			$("#groupe_11_12").slideDown();
		else if (group == 15 && classM <= "D")
			$("#groupe_15_inf_D").slideDown();
		else if (group == 15 && classM > "D")
			$("#groupe_15_sup_D").slideDown();
		else if (group <= 17)
			$("#groupe_16_17").slideDown();
		else $("#groupe_18").slideDown();
	}
}

function initZonier()
{
	if($(".zonier").length>0) getCity($(".zonier").attr("id"));
	
	$(".zonier").keyup(function()
	{
		var cp = $(this).val();
		
		getCity($(this).attr("id"));
	
	});
	
	$(".zonier").change(function()
	{
		var cp = $(this).val();
		
		getCity($(this).attr("id"));
	
	});
	

	
	
	$(".versionVeh").change(function()
	{
		
		chargeVeh();
	
	});

}

$(document).ready(function() {

	$(".classier").change(function()
	{
		$(".model").html("<option value=''>- - - - - - - - - - - - - - - - - - -</option>");
		$(".cylindree").html("<option value=''>- - - - - - - - - - - - - - - - - - -</option>");

		chargeCylindree();
	});
	
	$(".cylindree").change(function()
	{
		chargeVersion();
	});
	
	$(".model").change(function()
	{
		showRightImage("","");
	});
	
	initZonier();

});