
function change_tipo(){
	if(document.getElementById("tipo")!=null){
		document.getElementById("load1").style.display="block";
		document.getElementById("load2").style.display="block";
		
		document.getElementById("gruppo").disabled=true;
		document.getElementById("modello").disabled=true;
		id_tipo = document.getElementById("tipo").value;
		gruppo = document.getElementById("gruppo");
		url=site_url+"/vetture/flotta_ajax/"+id_tipo;
		 $.ajax({
		   type: "GET",
		   url:url,
		  
		   success: function(msg){
			gruppo.options.length=0;
			for(i=0;i<gruppo.options.length+1;i++){
				gruppo.options[0]=null;
			}
			furgoni = msg.split(";");
			for(i=0;i<furgoni.length;++i){
				f=furgoni[i].split("|");
				//alert(f[0]+"  "+f[1]);
				gruppo.options[gruppo.options.length]=new Option(f[0],f[1]);
			}
			document.getElementById("load1").style.display="none";
			document.getElementById("gruppo").disabled=false;
			if(furgoni!=""){
				
				change_gruppo();
			}else{
				modello = document.getElementById("modello");
				for(i=0;i<modello.options.length+1;i++){
					modello.options[0]=null;
				}
				document.getElementById("load2").style.display="none";
			}
		   }
		 });
	 }
	 
}


function change_gruppo(){
	document.getElementById("load2").style.display="block";
	document.getElementById("load2").style.display="block";
	id_gruppo = document.getElementById("gruppo").value;
	modello = document.getElementById("modello");
	url=site_url+"/vetture/flotta_ajax_modelli/"+id_gruppo;
	 $.ajax({
	   type: "GET",
	   url:url,
	  
	   success: function(msg){
		modello.options.length=0;
		for(i=0;i<modello.options.length+1;i++){
			modello.options[0]=null;
		}
		furgoni = msg.split(";");
		for(i=0;i<furgoni.length;++i){
			f=furgoni[i].split("|");
			//alert(f[0]+"  "+f[1]);
			modello.options[modello.options.length]=new Option(f[0],f[1]);
		}
		document.getElementById("load2").style.display="none";
		document.getElementById("modello").disabled=false;
	   }
	 });
	
}

$(document).ready(function(){
	change_tipo();
});

function show_img(img){
	if(document.getElementById("img_1")!=null){
	document.getElementById("img_1").style.display="none";
	}
	if(document.getElementById("img_2")!=null){
		document.getElementById("img_2").style.display="none";
	}
	if(document.getElementById("img_3")!=null){
		document.getElementById("img_3").style.display="none";
	}
	if(document.getElementById("img_a_1")!=null){
		document.getElementById("img_a_1").className="";
	}
	if(document.getElementById("img_a_2")!=null){
		document.getElementById("img_a_2").className="";
	}
	if(document.getElementById("img_a_3")!=null){
		document.getElementById("img_a_3").className="";
	}
	if(document.getElementById("img_a_"+img)!=null){
		document.getElementById("img_a_"+img).className="attuale";
	}
	if(document.getElementById("img_"+img)!=null){
		document.getElementById("img_"+img).style.display="block";
	}
	
	
}

function change_assicurazione(id,costo,sottrai){
	if(sottrai==null){
		sottrai=true;
	}
	var t = $("#totale_valore").html();
	t = t.split(" ")[0];
	
	var  totale = parseFloat(t);
	
	if(document.getElementById("ass_"+id).checked){
		
		totale=parseFloat(totale)+parseFloat(costo);
	}else if(sottrai){
		totale=parseFloat(totale)-parseFloat(costo);
	}
	if(parseInt(totale)==totale){
		totale = totale+".00 &euro;";
	}else if (parseInt(totale*10)==totale*10){
		totale = totale+"0 &euro;"
	}else{
		totale = totale+" &euro;";
	}
	document.getElementById("totale_valore").innerHTML=totale;
	
}


function change_chilometri(km){
	call_ricalcolo();
	url=site_url+"/vetture/get_costi_ajax/"+km;
	 $.ajax({
	   type: "GET",
	   url:url,
	  
	   success: function(msg){
		
		var costi = msg.split("_")[0];
		
		var sconti = msg.split("_")[1];
		var giornaliero = costi.split("|")[0];
		var vettura = costi.split("|")[1];
		var costo = costi.split("|")[2];
		document.getElementById("giornaliero").innerHTML=giornaliero+" &euro;"
		document.getElementById("valore_totale_tariffa").innerHTML=vettura+" &euro;"
		document.getElementById("totale_valore").innerHTML=costo+" &euro;"
		as = $(".assicurazioni");
		
		for(i=0;i<as.length;i++){
			id  = $(as[i]).attr("id");
			id=id.split("_")[1];
			change_assicurazione(id,$(as[i]).attr("ref"),false);
			
		}
		text="";
		if(sconti!="NO SCONTI"){
			sconto = sconti.split("#");
			for(i=0;i<sconto.length;i++){
				descrizione = sconto[i].split("|")[0];
				sc = sconto[i].split("|")[1];
				text += "<div class='testo_totale_tariffa'>Sconto: "+descrizione+"</div>";
				text += "<div class='valore_totale_tariffa'>-"+sc+" &euro;</div>";
				text += "<div style='clear:both'></div>";
			}
		}
		document.getElementById("lista_sconti").innerHTML=text;
		close_ricalcola();
	   }
	 });
	
}


function call_ricalcolo(){
	$("#ricalcola").css({
		"position": "absolute",
		"display":"block"
	});
}

function close_ricalcola(){
	$("#ricalcola").css({
			"display":"none"
		});
}

