var resposta;
function gardaLinea(id_linea,idaVolta) {
	try{
		var params = 'tipo=linea&idaVolta=' + idaVolta + '&id=' +id_linea+ '&coordeadas='+LineaFinal;
	}catch(e){ return false;	}
	var res = CreaXmlHttp("gardaPuntos.php",params);
	return false;
}

function gardaLineas(id_linea,idaVolta) {
	txtArea = document.getElementById('coordenadas_lina');
	try{
		poly = gdir.getPolyline();
		for(var i=0;i<eval(poly.getVertexCount()-1);i++){
			txtArea.value += '('+showCoord(poly.getVertex(i))+')\n';
		}
		txtArea.value += '('+showCoord(poly.getVertex(i))+')\n';
		LineaFin = txtArea.value.split("\n");
	}catch(e){
		alert("Para gardar tes que xenerar unha nova Linea");
		txtArea.value ="";
		return false;
	}

	try{
		var params = 'tipo=linea&idaVolta=' + idaVolta + '&id=' +id_linea+ '&coordeadas='+LineaFin;
	}catch(e){ return false;	}
	//alert(params);
	var res = CreaXmlHttp("gardaPuntos.php",params);
	alert('Gardado');
	return false;
}

function _gardaCoordenada(id,coordeada){
	var params = 'tipo=parada&id=' +id+ '&coordeadas='+coordeada;
	//alert('Vaise gardar a coordenada ('+coordeada+') no id ="'+id+'"');

	console.info(params);
	return CreaXmlHttp("gardaPuntos.php",params);
}

function _leeLineaFich(id_linea,idaVolta){
	var params = 'tipo=leelinea&idaVolta=' + idaVolta + '&id=' +id_linea;
	//alert('Vaise gardar a coordenada ('+coordeada+') no id ="'+id+'"');
	CreaXmlHttp("gardaPuntos.php",params);
	return resposta;
}

function _cargaPuntos(id_linea,idaVolta){
	var params = 'tipo=leepuntos&idaVolta=' + idaVolta + '&id=' +id_linea;
	CreaXmlHttp("gardaPuntos.php",params);
	return resposta;
}

function copiaCircular(id_linea){
	var params = 'tipo=circular&id=' +id_linea;
	CreaXmlHttp("gardaPuntos.php",params);
	alert('Linea de volta creada.');
	return false;
}


function CreaXmlHttp(url,params) {
  var oXmlHttp = zXmlHttp.createRequest();
   oXmlHttp.open("post", url, false);
   oXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   
   oXmlHttp.onreadystatechange = function () {
       if (oXmlHttp.readyState == 4) {
           if (oXmlHttp.status != 200) {
              saveResultDiv("Ocurrio un error: " + oXmlHttp.statusText);
           }
       }            
   };
	
   oXmlHttp.send(params); 
   saveResult(oXmlHttp.responseText);
 	
   var opcions = oXmlHttp.responseText.split(":");
   if (opcions[0] == "E" ) {
      saveResultDiv("Ocurrio un error: "+oXmlHttp.responseText);
      return false;
   }
   return true;
}




function saveResult(sMessage) {
	 resposta = sMessage; 
}

function saveResultDiv(sMessage) {
 alert(sMessage);  
}

