

function el(id){
	return document.getElementById(id);
}
	
function init_devis(){
	el('depart').onchange = function(){
		calculer();
	}
	el('arrivee').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 = 135;
						total+=1;
					} else {
						temps = 180+10*total;
					}
					break;
				// Tarif Exclusif ///////////////////////////////////
				case '2' :
					if(paris) {						
						temps = 60;
						total+=3;
					} else {					
						temps = 60+5*total;
						total+=4;
					}				
					break;
				// Tarif Super Express ///////////////////////////////////
				case '3' :
					if(paris) {						
						temps = 30;
						total+=6;
					} else {			
						temps = 30+5*total;
						total+=6;
					}					
					break;
			}	
		}
		// Cas de la course voiture ////////////////////////////////////////////////////////////////////
		if(transp==2){
			switch(type){
				// Tarif Normal ///////////////////////////////////
				case '1' :
					if(paris) {						
						temps = 165;
						total+=6;
					} else {
						temps = 180+10*total;
						total+=5;
					}
					break;
				// Tarif Exclusif ///////////////////////////////////
				case '2' :
					if(paris) {						
						temps = 90;
						total+=10;
					} else {					
						temps = 120+5*total;
						total+=10;
					}				
					break;
				// Tarif Super Express ///////////////////////////////////
				case '3' :
					if(paris) {						
						temps = 60;
						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 = 180;
						total+=10;
					} else {
						temps = 180+10*total;
						total+=10;
					}
					break;
				// Tarif Exclusif ///////////////////////////////////
				case '2' :
					if(paris) {						
						temps = 90;
						total+=15;
					} else {					
						temps = 120+5*total;
						total+=15;
					}				
					break;
				// Tarif Super Express ///////////////////////////////////
				case '3' :
					if(paris) {						
						temps = 60;
						total+=21;
					} else {			
						temps = 60+5*total;
						total+=21;
					}					
					break;
			}	
		}
		var mn =  "" +(temps % 60);
		if(mn.length==1)mn='0'+mn;
		var PHT = (total*10);
		var PTT = Math.round((PHT*1.196)*100)/100;
		(total*10)/1.196
		retour = total+" bon(s) soit un total de "+PTT+" euros TTC <br/>("+PHT+" euros hors taxes).<br/>Temps prévu : "+Math.floor(temps/60)+"h"+mn;
	}
	el('resultats').innerHTML = retour;
	el('res').value = retour;
}