

function el(id){
	return document.getElementById(id);
}
	
function init_devis(){
	
	el('depart').onchange = function(){
		calculer();
	}
	el('arrivee').onchange = function(){
		calculer();
	}
	el('allerRetour').onchange = function(){
		calculer();
	}
	for(var i=1;i<=3;i++){
		el('veh'+i).onclick = function(){
			calculer();
		}
		el('type'+i).onclick = function(){
			calculer();
		}
		
	}
	el('heure').onchange = function(){
		calculer();
	}
	// Initialisation des valeurs pour un calcul de démo par défaut
	el('heure').value=10;
	el('minute').value="00";
	el('veh1').checked=true;
	el('type1').checked=true;
	calculer();
}

function enable(){
	el('type1').disabled = false;
	el('type2').disabled = false;
	el('type3').disabled = false;
}

function calculer(){
	var valide1 = false;
	var valide2 = false;
	var retour = '';
	
	if(el('heure').value!='' && el('minute').value!=''){
		var h = parseInt(el('heure').value);
		var mn = parseInt(el('minute').value);
		if(h==8){
			el('type3').checked = true;
			el('type1').disabled = true;
			el('type2').disabled = true;
			el('type3').disabled = true;
		} else if(h>8 && h<17){
			el('type1').disabled = false;
			el('type2').disabled = false;
			el('type3').disabled = false;
		} else if(h==17 || (h==18 && mn<30)){
			el('type1').checked = false;
			el('type1').disabled = true;
			el('type2').disabled = false;
			el('type3').disabled = false;					
		} else if((h==18 && mn>=30) || (h>18 && h<20)){
			el('type3').checked = true;
			el('type1').disabled = true;
			el('type2').disabled = true;
			el('type3').disabled = true;				
		} else {
			retour = "Sur devis entre 20h et 8h";
		}
		
		if(retour==''){	  
			for(var i=1;i<=3;i++){
				if(el('veh'+i).checked){
					valide1=true;
					var transp = el('veh'+i).value;
				}
				if(el('type'+i).checked){
					valide2=true;
					var type = el('type'+i).value;
				}
			}
		}	
	}

	if (valide1 && valide2) {
		var tab_depart = el('depart').value.split('_');
		var bons1 = parseInt(tab_depart[1]);
		tab_arrivee = el('arrivee').value.split('_');
		var bons2 = parseInt(tab_arrivee[1]);
		if (tab_depart[1] == 0 && tab_arrivee[0] == 0) var paris = true;
		else var paris = false;
		var total = bons1+bons2;
		var temps = 0;
		// Calcul selon le vehicule, le trajet, et le type de demande
		
		// Cas de la course 2 roues ///////////////////////////////////////////////////////////////////////
		if (transp == 1) {
			switch (type) {
				// Tarif Normal ///////////////////////////////////
				case '1' :
					if (paris) {						
						temps = 150;
						total += 1;
					} else {
						temps = 180+10*total;
					}
					break;
				// Tarif Express ///////////////////////////////////
				case '2' :
					if (paris) {						
						temps = 75;
						total += 3;
					} else {					
						temps = 60+5*total;
						total += 4;
					}				
					break;
				// Tarif Exclusif ///////////////////////////////////
				case '3' :
					if (paris) {						
						temps = 37;
						total += 6;
					} else {			
						temps = 37+5*total;
						total += 6;
					}					
					break;
			}	
		}
		// Cas de la course voiture ////////////////////////////////////////////////////////////////////
		if (transp == 2) {
			switch (type) {
				// Tarif Normal ///////////////////////////////////
				case '1' :
					if (paris) {						
						temps = 150;
						total += 6;
					} else {
						temps = 180+10*total;
						total += 5;
					}
					break;
				// Tarif Express ///////////////////////////////////
				case '2' :
					if (paris) {						
						temps = 75;
						total += 10;
					} else {					
						temps = 120+5*total;
						total += 10;
					}				
					break;
				// Tarif Exclusif ///////////////////////////////////
				case '3' :
					if (paris) {						
						temps = 45;
						total += 16;
					} else {			
						temps = 60+5*total;
						total += 16;
					}					
					break;
			}	
		}		
		
		// Cas de la course camion ////////////////////////////////////////////////////////////////////
		if (transp==3) {
			switch (type) {
				// Tarif Normal ///////////////////////////////////
				case '1' :
					if (paris) {						
						temps = 150;
						total += 10;
					} else {
						temps = 180+10*total;
						total += 10;
					}
					break;
				// Tarif Exclusif ///////////////////////////////////
				case '2' :
					if (paris) {						
						temps = 75;
						total += 15;
					} else {					
						temps = 120+5*total;
						total += 15;
					}				
					break;
				// Tarif Exclusif ///////////////////////////////////
				case '3' :
					if (paris) {						
						temps = 60;
						total += 21;
					} else {			
						temps = 60+5*total;
						total += 21;
					}					
					break;
			}	
		}

		if (el('allerRetour').checked == true ) {
			temps = temps*2;
			var mn =  "" +(temps % 60);
			if(mn.length==1)mn='0'+mn;
			var PHT = (total*5);
			var PTT = Math.round((PHT*1.196)*100)/100;
			PHT = PHT*2;
			PTT = PTT*2;
			total = total*2;

		} else {
			var mn =  "" +(temps % 60);
			if(mn.length==1)mn='0'+mn;
			var PHT = (total*5);
			var PTT = Math.round((PHT*1.196)*100)/100;
		}
		
		retour = "<span>"+total+" bon(s)</span> soit un total de <span>"+PTT+" euros TTC</span> <br/>("+PHT+" euros hors taxes).<br /><br />Estimation du temps prévu : <span class='tps'>"+Math.floor(temps/60)+"h"+mn+"</span>";
	}
	el('resultats').innerHTML = retour;
	el('res').value = retour;
}
