function inicialitzaPagina(){
	if (googleMapsActiu){
		initialize();
	}
	else {
		document.getElementById("map_canvas").innerHTML="<br><br>API Google Maps no present";
	}
	formulari=document.forms['formTaxi'];
	kmPosiblerutaLenta=0;
}

function calcularPreu() {
	
	if (formulari.tarifa[0].checked)
	{
		horaDesperaCalcul=horaDespera;
		preuKmCalcul=preuKm;
	}
	else
	{
		horaDesperaCalcul=horaDesperaNocturn;
		preuKmCalcul=preuKmNocturn;
	}
	
	document.getElementById("pTotalValor").innerHTML=horaDesperaCalcul+"€";
	
	var tmpKm=Number(formulari.km.value);
	if (kmPosiblerutaLenta<200 && (tmpKm-kmPosiblerutaLenta)>5){
		document.getElementById("rutaLenta").innerHTML="<div style='color:#FFFFFF; padding:5px'>"+jsRutaLenta+"<br>  ("+kmPosiblerutaLenta+"km) <strong>"+(Math.floor(2*kmPosiblerutaLenta*preuKmCalcul*10+.5)/10)+"€</strong></div>";
	}
	if (tmpKm>0){
		tmpKm=Math.floor(2*tmpKm*preuKmCalcul*10+.5)/10;document.getElementById("pViatgeValor").innerHTML=tmpKm+"€";
	}
	else {
		document.getElementById("pViatgeValor").innerHTML="";
	}
	var tmpEspera=Number(formulari.espera.value);
	if (tmpEspera>0){
		tmpEspera=Math.floor(tmpEspera*horaDesperaCalcul*10+.5)/10;
		document.getElementById("pEsperaValor").innerHTML=tmpEspera+"€";
	}
	else {
		document.getElementById("pEsperaValor").innerHTML="";
	}
	var tmpTotal=Math.round((tmpEspera+tmpKm)*100)/100;
	if (tmpTotal>0){
		document.getElementById("pTotalValor").innerHTML=tmpTotal+"€";
	}
	else{
		document.getElementById("pTotalValor").innerHTML="0€";
	}
	
}
	
function calcularRuta(){
	
	document.getElementById("tempsViatge").innerHTML="";
	document.getElementById("distanciaKm").innerHTML="";
	document.getElementById("costAutopista").innerHTML="";
	document.getElementById("rutaLenta").innerHTML="";
	eval(formulari.desti.value);
	formulari.km.value=s_km;
	kmPosiblerutaLenta=s_km;
	document.getElementById("costAutopista").innerHTML=(s_peatge)?"<div style='color:#FFFFFF; padding:5px'>"+jsPeargeAutopista+"</div>":"";
	if (!googleMapsActiu){
		
		calcularPreu();
		//return;
	}
	if (s_local==''){
		initialize();
		calcularPreu();
		//return;
	}
	
	calcRoute();
}

googleMapsActiu=false;
if (typeof google != "undefined"){
	var directionDisplay;
	var directionsService = new google.maps.DirectionsService();
	var map;
	googleMapsActiu = true;
}

function initialize() {    
	directionsDisplay = new google.maps.DirectionsRenderer();
    var Banyoles = new google.maps.LatLng(42.121145,2.764778);
    var myOptions = { zoom:14,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		center: Banyoles
	};    
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    directionsDisplay.setMap(map);
} 
	
function calcRoute() {
    var start = "Banyoles";
    var end = s_local;
	
    var request = {
	origin:start,
	destination:end,
	travelMode: google.maps.DirectionsTravelMode.DRIVING,unitSystem: google.maps.DirectionsUnitSystem.METRIC
	}
	request.avoidTolls= !s_peatge; 
	directionsService.route(request, function(response, status) {
	if (status == google.maps.DirectionsStatus.OK) {
	directionsDisplay.setDirections(response);
	formulari.km.value=response.routes[0].legs[0].distance.value/1000;
	tmpDistance=response.routes[0].legs[0].distance.text;
	tmpDuration=response.routes[0].legs[0].duration.text;
	setTimeout('document.getElementById("distanciaKm").innerHTML=tmpDistance;document.getElementById("tempsViatge").innerHTML=tmpDuration',250);
	calcularPreu();
	}
    });
}

