// JavaScript Document
<!--
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

    var map;
    var gdir;
    var geocoder = null;
    var addressMarker;
 
    function initialize() {
      if (GBrowserIsCompatible()) { 
        map = new GMap2(document.getElementById("map_canvas"));
        
		gdir = new GDirections(map, document.getElementById("directions"));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
		 
    	map.setCenter(new GLatLng(39.5548, -8.0419), 6); 
      }
    }
    
    function setDirections(fromAddress, toAddress, locale) {
	  GDraggableObject.setDraggableCursor('default');
	  GDraggableObject.setDraggingCursor('move');     
      gdir.load("from: " + fromAddress + " to: " + toAddress,
                { "locale": locale ,avoidHighways: true });
    }
 
    function handleErrors(){
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("Não foi encontrada nenhuma correspondência geográfica para um dos locais que especificou.\nVerifique se escreveu correctamente o nome das localidades.\nCódigo de erro: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	     alert("A geolocalização ou local pedida, não pode ser processado com sucesso, mas a razão para esta falha não é conhecida.\nCódigo de erro: " + gdir.getStatus().code);
	   
	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\nCódigo de erro: " + gdir.getStatus().code);
 
	//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
	//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
	     
	   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	     alert("The given key is either invalid or does not match the domain for which it was given.\nCódigo de erro: " + gdir.getStatus().code);
 
	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("Não é possivel analizar um dos locais indicados.\nCódigo de erro: " + gdir.getStatus().code);
	    
	   else alert("Ocorreu um erro desconhecido.");
	   
	}
 
function onGDirectionsLoad(){ 
      // Use this function to access information about the latest load()
      // results.
 
      // e.g.
      // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
	  var distancia = gdir.getDistance().meters/500;
	   document.form1.km.value = distancia.toFixed(1);
	   //document.form1.km.value = distancia*2;
       document.form1.km.select();
	  // and yada yada yada...
	}


function Tecla(e)
{
	if (document.all) // Internet Explorer
		var tecla = event.keyCode;
	else if(document.layers) // Nestcape
		var tecla = e.which;
	
	if (tecla > 47 && tecla < 58) // numeros de 0 a 9
			return true;
	else if (tecla == 46){ // ponto final .
		return true;
	}

	else
		{
			if (tecla != 8) // backspace
				event.keyCode = 0;
				//return false;
			else
				return true;
		}
}

function calcular(){

var tkm = document.form1.km.value * valorKm;
	document.getElementById('tkm').innerHTML = tkm.toFixed(2);
var ttempo = document.form1.espera.value * valorTempo;
	document.getElementById('ttempo').innerHTML = ttempo.toFixed(2);
var tbagagem
	if(document.form1.bagagem.checked){document.getElementById('tbagagem').innerHTML = valorBagagem.toFixed(2);tbagagem=valorBagagem;}
	else{document.getElementById('tbagagem').innerHTML="0.00";tbagagem=0;}
var ttelefone
	if(document.form1.telefone.checked){document.getElementById('ttelefone').innerHTML = valorTelefone.toFixed(2);ttelefone=valorTelefone}
	else{document.getElementById('ttelefone').innerHTML="0.00";ttelefone=0}
var tresultado = tkm+ttempo
var tnocturno = tkm*20/100
	if(document.form1.nocturno.checked){document.getElementById('tnocturno').innerHTML = tnocturno.toFixed(2);}
	else{document.getElementById('tnocturno').innerHTML="0.00";tnocturno=0}
		
var ttotal=tresultado+tbagagem+ttelefone+tnocturno;
	if(ttotal<valorMinimo){ttotal=valorMinimo;}
	document.getElementById('ttotal').innerHTML = ttotal.toFixed(2);

}

function limpart(){
	map.clearOverlays();
	  document.gDir.reset();
		document.getElementById('tkm').innerHTML="0.00";
		document.getElementById('ttempo').innerHTML="0.00";
		document.getElementById('tbagagem').innerHTML="0.00";
		document.getElementById('ttelefone').innerHTML="0.00";
		document.getElementById('tnocturno').innerHTML="0.00";
		document.getElementById('ttotal').innerHTML="0.00";
	initialize()
}
//#################################
//mostra sub-selects
function GetXmlHttpObject(){
var xmlHttp=null;
	try{
  // Firefox, Opera 8.0+, Safari
  		xmlHttp=new XMLHttpRequest();
  	}
	catch (e){
  // Internet Explorer
  		try{
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	}
  		catch (e){
    		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    	}
  	}
return xmlHttp;
}

function showCustomer(url,tagId){ 
	xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null){
  			alert ("Your browser does not support AJAX!");
  		return;
  		} 
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

var tagid;

function stateChanged() { 
	if (xmlHttp.readyState==4){ 
		document.getElementById(tagid).innerHTML=xmlHttp.responseText;
	}
	else{
		document.getElementById(tagid).innerHTML="a processar...";
	}
}

function showAlert(url){ 
	xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null){
 			 alert ("Your browser does not support AJAX!");
  		return;
  		} 
	xmlHttp.onreadystatechange=stateAlert;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateAlert(){ 
	if (xmlHttp.readyState==4){ 
		refCompare=xmlHttp.responseText;
	}
}

function local(id,nomelocal){
	if(id=="2"){
	document.gDir.to.value=nomelocal+", "+document.escolher.concelho2.value;
	}
	else{
	document.gDir.from.value=nomelocal+", "+document.escolher.concelho.value;
	}
}
// -->

