var httpo = null;
var httpd = null;
var sMode = 'r';
var tx_cpde, tx_cpor;
var lato;
var latd;
var lono;
var lond;
var Loca, Prov, NomVia, NumVia, TpVia;
var	provor, provde;
var ready = false;
var tx_busq_origen,tx_busq_destino
//NUEVA FUNCIÓN PARA ENVIAR LOS DATOS DE LA BÚSQUEDA DE DIRECCIÓN

function envio_planos_ruta(tipo, m_origen, m_lato, m_lono, m_destino, m_latd, m_lond)
{	
	var origen;
	var destino;
	
	if(((m_lato)&&(m_lono))&&((m_latd)&&(m_lond))) {
		var finalpage = '/vemaps/mapa.asp?lator=';
		finalpage += m_lato + '&lonor=' + m_lono + '&l1or=' + m_origen + '&latde=' + m_latd + '&londe=' + m_lond + '&l1de=' + m_destino +'&mode=r';
		top.document.location.href = finalpage;
	}
	else {
		if (m_origen) {
			origen=m_origen;
			destino=m_destino;
		}
		else {
			if (tipo == 1) {
				origen=document.busqueda_rutas.direccion_origen.value+ ' ' +document.busqueda_rutas.local_origen.value;
				destino=document.busqueda_rutas.direccion_destino.value+ ' ' +document.busqueda_rutas.local_destino.value;
			}
			else {
				origen=document.busqueda_rutas_unica.ruta_unica_origen.value;
				destino=document.busqueda_rutas_unica.ruta_unica_destino.value;
			}
		}
		origen = origen.replace(/^(\s|\&nbsp;)*|(\s|\&nbsp;)*$/g,"");
		destino = destino.replace(/^(\s|\&nbsp;)*|(\s|\&nbsp;)*$/g,"");
		
		//var url='/proxy.asp?mode=ws&cu=' + direcor + ' ' + localor + '&culture=' + document.form_culture.culture.value;;
		//var url2='/proxy.asp?mode=ws&cu=' + direcde + ' ' + localde + '&culture=' + document.form_culture.culture.value;;
		var url;
		if (m_lato) {
		//	url='/proxy.asp?mode=ws&service=ruta&tipo=1&l1o=' + escape(origen) + '&lator=' + m_lato + '&lonor=' + m_lono + '&l1d=' + escape(destino) + '&latde=' + m_latd + '&londe=' + m_lond;
			url='/routeproxy.aspx?mode=ws&service=ruta&tipo=1&l1o=' + escape(origen) + '&lator=' + m_lato + '&lonor=' + m_lono + '&l1d=' + escape(destino) + '&latde=' + m_latd + '&londe=' + m_lond;
		}
		else {
			//url='/proxy.asp?mode=ws&service=ruta&tipo=1&cuo=' + escape(origen) + '&cud=' + escape(destino);
			url='/routeproxy.aspx?mode=ws&service=ruta&tipo=1&cuo=' + escape(origen) + '&cud=' + escape(destino);
		}
		//alert( url);
	    if ((origen=='') || (destino==''))
		{
			alert("<!-- ##Malerta_falta_prov_loc_nombre -->Los campos Provincia, Ciudad y Nombre de la Via son Obligatorios.<!-- ##E -->");
			//url='/proxy.asp?mode=ws&nomvia=' + '&loca=' + local + '&prov=' + prov;
			//var url='/proxy.asp?mode=ws&nomvia=' + nombrevia + '&tpvia=' + tipovia + '&numvia=' + numerovia + '&loca=' + local;
		}
		else
		{
			if (tipo==1){
				var sParam = "origen=" + origen;
				sParam += "&destino=" + destino;
				sParam += '&mode=r&site=callejero'
				//pintaMosca(window.document, sParam);
				httpo = new peticion_http(g_iTimeOutAjax, url, "GET", cargaRuta, fallo, 'o');
				httpo.start();
			}else{
				tx_busq_origen = "origen=" + origen;
				tx_busq_destino = "&destino=" + destino;
				var sParam = 'mode=r&site=callejero'
				//pintaMosca(window.document, sParam);
				httpo = new peticion_http(g_iTimeOutAjax, url, "GET", cargaRutaUnica, fallo, 'o');
				httpo.start();
			
			}
	    }
		return;
	}
}

function fallo() 
{
	boton_reloj(0);
	if (g_sMostrarAlert == "true")
	{
		
		//alert("Se ha producido un error en la comunicación con el servidor. Inténtalo un poco más adelante.");
	}
}
	
	function cargaRuta()
	{
		try
		{		
			var mplocao
			var  mplocad
			var documento_xml;
			mplocao = document.getElementById('local_origen').value;
			mplocad = document.getElementById('local_destino').value;
			
			documento_xml = httpo.getXML();	
			
			var root = documento_xml.getElementsByTagName("direcciones")[0];
			
			if (root){
				if (documento_xml.getElementsByTagName("item")[0]){
					if (documento_xml.getElementsByTagName("error")[0])
					{
						alert("<!-- ##Malerta_sin_resultado -->No se encontraron resultados<!-- ##E -->. " + documento_xml.getElementsByTagName("error")[0].childNodes[0].nodeValue);
						location.reload();
						boton_reloj(0);
					}
					else
					{
				
					if(documento_xml.getElementsByTagName("item").length>1)
					{
						var sCadena;
						if (documento_xml.getElementsByTagName("multio")[0]) { 
							sCadena = "multiple=-1";
						}
						if (documento_xml.getElementsByTagName("multid")[0]) { 
							sCadena = "multiple=-2"; 
						}
						if (documento_xml.getElementsByTagName("multiod")[0]) { 
							sCadena = "multiple=-3"; 
						}
						var tx_tipovia = "";
						var tx_calle = "";
						var tx_loca = "";
						var tx_cp = "";
						var tx_lat = "";
						var tx_lon = "";					
						var tx_prov = "";
						if (documento_xml.getElementsByTagName("cuo")[0]) { sCadena = sCadena + "&cuo=" + documento_xml.getElementsByTagName("cuo")[0].firstChild.nodeValue; }
						if (documento_xml.getElementsByTagName("cud")[0]) { sCadena = sCadena + "&cud=" + documento_xml.getElementsByTagName("cud")[0].firstChild.nodeValue; }
						if (documento_xml.getElementsByTagName("l1o")[0]) { sCadena = sCadena + "&l1o=" + documento_xml.getElementsByTagName("l1o")[0].firstChild.nodeValue; }
						if (documento_xml.getElementsByTagName("l1d")[0]) { sCadena = sCadena + "&l1d=" + documento_xml.getElementsByTagName("l1d")[0].firstChild.nodeValue; }
						if (documento_xml.getElementsByTagName("lato")[0]) { sCadena = sCadena + "&lato=" + documento_xml.getElementsByTagName("lato")[0].firstChild.nodeValue; }
						if (documento_xml.getElementsByTagName("lono")[0]) { sCadena = sCadena + "&lono=" + documento_xml.getElementsByTagName("lono")[0].firstChild.nodeValue; }
						if (documento_xml.getElementsByTagName("latd")[0]) { sCadena = sCadena + "&latd=" + documento_xml.getElementsByTagName("latd")[0].firstChild.nodeValue; }
						if (documento_xml.getElementsByTagName("lond")[0]) { sCadena = sCadena + "&lond=" + documento_xml.getElementsByTagName("lond")[0].firstChild.nodeValue; }
						var items = documento_xml.getElementsByTagName("item");
						for (i = 0;i<=items.length-1;i++)
						{
							var item = items.item(i);
							tx_tipovia = ""
													
							if(item.getElementsByTagName("tx_calle")[0])
							{
								tx_calle = item.getElementsByTagName("tx_calle")[0].childNodes[0].nodeValue;
									
								if(item.getElementsByTagName("tx_loca")[0])
								{
									tx_loca = item.getElementsByTagName("tx_loca")[0].childNodes[0].nodeValue;
								}
								if(item.getElementsByTagName("tx_tipovia")[0])
								{
									tx_tipovia = item.getElementsByTagName("tx_tipovia")[0].childNodes[0].nodeValue;
								}
								if(item.getElementsByTagName("tx_prov")[0])
								{								
									tx_prov = item.getElementsByTagName("tx_prov")[0].childNodes[0].nodeValue;
								}
								if(item.getElementsByTagName("tx_cp")[0])
								{
									tx_cp = item.getElementsByTagName("tx_cp")[0].childNodes[0].nodeValue;
								}								
								else
								{
									tx_cp = "00000"
								}
								if(item.getElementsByTagName("lat")[0])
								{
									tx_lat = item.getElementsByTagName("lat")[0].childNodes[0].nodeValue;
								}
								else
								{
									tx_lat = "00000"
								}
								if(item.getElementsByTagName("long")[0])
								{
									tx_lon = item.getElementsByTagName("long")[0].childNodes[0].nodeValue;
								}
								else
								{
									tx_lon = "00000"
								}
								
								if(tx_tipovia == "")
								{
									if(tx_calle.indexOf("%20") != -1) 
									{
										tx_tipovia = tx_calle.substring(0, tx_calle.indexOf("%20"))
										tx_calle   = tx_calle.substring(tx_calle.indexOf("%20")+3, tx_calle.length)		
									}
								}
							}	
													
							if (documento_xml.getElementsByTagName("multid")[0]) {					
								sCadena = sCadena + "&Td" + i + "=" + tx_tipovia + "&Cd" + i + "=" +  tx_calle + "&Pd" + i + "=" + tx_cp + "&latd" + i + "=" + tx_lat + "&lond" + i + "=" + tx_lon + "&provd" + i + "=" + tx_prov + "&locad" + i + "=" + tx_loca;
							}
							else {
								sCadena = sCadena + "&To" + i + "=" + tx_tipovia + "&Co" + i + "=" +  tx_calle + "&Po" + i + "=" + tx_cp + "&lato" + i + "=" + tx_lat + "&lono" + i + "=" + tx_lon + "&provo" + i + "=" + tx_prov + "&locao" + i + "=" + tx_loca;
							}
							//ready = false;
							
						}					
						items = documento_xml.getElementsByTagName("item2");
						for (i = 0;i<=items.length-1;i++)
						{
							var item = items.item(i);
							tx_tipovia = ""
													
							if(item.getElementsByTagName("tx_calle")[0])
							{
								tx_calle = item.getElementsByTagName("tx_calle")[0].childNodes[0].nodeValue;
									
								if(item.getElementsByTagName("tx_loca")[0])
								{
									tx_loca = item.getElementsByTagName("tx_loca")[0].childNodes[0].nodeValue;
								}
								if(item.getElementsByTagName("tx_tipovia")[0])
								{
									tx_tipovia = item.getElementsByTagName("tx_tipovia")[0].childNodes[0].nodeValue;
								}
								if(item.getElementsByTagName("tx_prov")[0])
								{								
									tx_prov = item.getElementsByTagName("tx_prov")[0].childNodes[0].nodeValue;
								}
								if(item.getElementsByTagName("tx_cp")[0])
								{
									tx_cp = item.getElementsByTagName("tx_cp")[0].childNodes[0].nodeValue;
								}								
								else
								{
									tx_cp = "00000"
								}
								if(item.getElementsByTagName("lat")[0])
								{
									tx_lat = item.getElementsByTagName("lat")[0].childNodes[0].nodeValue;
								}
								else
								{
									tx_lat = "00000"
								}
								if(item.getElementsByTagName("long")[0])
								{
									tx_lon = item.getElementsByTagName("long")[0].childNodes[0].nodeValue;
								}
								else
								{
									tx_lon = "00000"
								}
								
								if(tx_tipovia == "")
								{
									if(tx_calle.indexOf("%20") != -1) 
									{
										tx_tipovia = tx_calle.substring(0, tx_calle.indexOf("%20"))
										tx_calle   = tx_calle.substring(tx_calle.indexOf("%20")+3, tx_calle.length)		
									}
								}
							}														
														
							sCadena = sCadena + "&Td" + i + "=" + tx_tipovia + "&Cd" + i + "=" +  tx_calle + "&Pd" + i + "=" + tx_cp + "&latd" + i + "=" + tx_lat + "&lond" + i + "=" + tx_lon + "&provd" + i + "=" + tx_prov + "&locad" + i + "=" + tx_loca;
							ready = false;
							
						}
						boton_reloj(0);
						tb_show('M&uacute;ltiples Resultados', '/iframes/multiples.asp?'+sCadena+'&keepThis=true&TB_iframe=true&height=400&width=400', '')
					}
					else
					{
						lato = documento_xml.getElementsByTagName("lato")[0].firstChild.nodeValue;
						lono = documento_xml.getElementsByTagName("lono")[0].firstChild.nodeValue;
						l1o = documento_xml.getElementsByTagName("l1o")[0].firstChild.nodeValue;
						latd = documento_xml.getElementsByTagName("latd")[0].firstChild.nodeValue;
						lond = documento_xml.getElementsByTagName("lond")[0].firstChild.nodeValue;
						l1d = documento_xml.getElementsByTagName("l1d")[0].firstChild.nodeValue;
						var finalpage = '/vemaps/mapa.asp?lator=';
						finalpage += lato + '&lonor=' + lono + '&l1or=' + l1o + '&latde=' + latd + '&londe=' + lond + '&l1de=' + l1d +'&mode=r';
						top.document.location.href = finalpage;
						}
					}
				} else {
					if (g_sMostrarAlert == "true")
					{
						alert('<!-- ##Malerta_errordatos -->error en datos<!-- ##E -->');
					}
					boton_reloj(0);
					ready = false;
					
				}
			}else{
				if (g_sMostrarAlert == "true")
				{
					alert('<!-- ##Malerta_errordatos2 -->error en datos<!-- ##E -->');
				}
				boton_reloj(0);
				ready = false;
			}
		}
		catch(e)
		{
			boton_reloj(0);
			if (g_sMostrarAlert == "true")
			{
				for (atributo in e) alert("origen[" + atributo + "] = " + e[atributo]);
			}
		}
	}
	
	function cargaRutaUnica()
	{
		try
		{		
			var mplocao
			var  mplocad
			var documento_xml;
			mplocao = document.getElementById('local_origen').value;
			mplocad = document.getElementById('local_destino').value;
			
			documento_xml = httpo.getXML();	
			
			var root = documento_xml.getElementsByTagName("direcciones")[0];
			
			if (root){
				if (documento_xml.getElementsByTagName("item")[0]){
					if (documento_xml.getElementsByTagName("error")[0])
					{
						alert("<!-- ##Malerta_sin_resultado -->No se encontraron resultados<!-- ##E -->. " + documento_xml.getElementsByTagName("error")[0].childNodes[0].nodeValue);
						location.reload();
						boton_reloj(0);
					}
					else
					{
				
					if(documento_xml.getElementsByTagName("item").length>1)
					{
						var sCadena;
						if (documento_xml.getElementsByTagName("multio")[0]) { 
							sCadena = "multiple=-1";
						}
						if (documento_xml.getElementsByTagName("multid")[0]) { 
							sCadena = "multiple=-2"; 
						}
						if (documento_xml.getElementsByTagName("multiod")[0]) { 
							sCadena = "multiple=-3"; 
						}
						var tx_tipovia = "";
						var tx_calle = "";
						var tx_loca = "";
						var tx_cp = "";
						var tx_lat = "";
						var tx_lon = "";					
						var tx_prov = "";
						if (documento_xml.getElementsByTagName("cuo")[0]) { sCadena = sCadena + "&cuo=" + documento_xml.getElementsByTagName("cuo")[0].firstChild.nodeValue; }
						if (documento_xml.getElementsByTagName("cud")[0]) { sCadena = sCadena + "&cud=" + documento_xml.getElementsByTagName("cud")[0].firstChild.nodeValue; }
						if (documento_xml.getElementsByTagName("l1o")[0]) { sCadena = sCadena + "&l1o=" + documento_xml.getElementsByTagName("l1o")[0].firstChild.nodeValue; }
						if (documento_xml.getElementsByTagName("l1d")[0]) { sCadena = sCadena + "&l1d=" + documento_xml.getElementsByTagName("l1d")[0].firstChild.nodeValue; }
						if (documento_xml.getElementsByTagName("lato")[0]) { sCadena = sCadena + "&lato=" + documento_xml.getElementsByTagName("lato")[0].firstChild.nodeValue; }
						if (documento_xml.getElementsByTagName("lono")[0]) { sCadena = sCadena + "&lono=" + documento_xml.getElementsByTagName("lono")[0].firstChild.nodeValue; }
						if (documento_xml.getElementsByTagName("latd")[0]) { sCadena = sCadena + "&latd=" + documento_xml.getElementsByTagName("latd")[0].firstChild.nodeValue; }
						if (documento_xml.getElementsByTagName("lond")[0]) { sCadena = sCadena + "&lond=" + documento_xml.getElementsByTagName("lond")[0].firstChild.nodeValue; }
						var items = documento_xml.getElementsByTagName("item");
						for (i = 0;i<=items.length-1;i++)
						{
							var item = items.item(i);
							tx_tipovia = ""
													
							if(item.getElementsByTagName("tx_calle")[0])
							{
								tx_calle = item.getElementsByTagName("tx_calle")[0].childNodes[0].nodeValue;
									
								if(item.getElementsByTagName("tx_loca")[0])
								{
									tx_loca = item.getElementsByTagName("tx_loca")[0].childNodes[0].nodeValue;
								}
								if(item.getElementsByTagName("tx_tipovia")[0])
								{
									tx_tipovia = item.getElementsByTagName("tx_tipovia")[0].childNodes[0].nodeValue;
								}
								if(item.getElementsByTagName("tx_prov")[0])
								{								
									tx_prov = item.getElementsByTagName("tx_prov")[0].childNodes[0].nodeValue;
								}
								if(item.getElementsByTagName("tx_cp")[0])
								{
									tx_cp = item.getElementsByTagName("tx_cp")[0].childNodes[0].nodeValue;
								}								
								else
								{
									tx_cp = "00000"
								}
								if(item.getElementsByTagName("lat")[0])
								{
									tx_lat = item.getElementsByTagName("lat")[0].childNodes[0].nodeValue;
								}
								else
								{
									tx_lat = "00000"
								}
								if(item.getElementsByTagName("long")[0])
								{
									tx_lon = item.getElementsByTagName("long")[0].childNodes[0].nodeValue;
								}
								else
								{
									tx_lon = "00000"
								}
								
								if(tx_tipovia == "")
								{
									if(tx_calle.indexOf("%20") != -1) 
									{
										tx_tipovia = tx_calle.substring(0, tx_calle.indexOf("%20"))
										tx_calle   = tx_calle.substring(tx_calle.indexOf("%20")+3, tx_calle.length)		
									}
								}
							}	
													
							if (documento_xml.getElementsByTagName("multid")[0]) {					
								sCadena = sCadena + "&Td" + i + "=" + tx_tipovia + "&Cd" + i + "=" +  tx_calle + "&Pd" + i + "=" + tx_cp + "&latd" + i + "=" + tx_lat + "&lond" + i + "=" + tx_lon + "&provd" + i + "=" + tx_prov + "&locad" + i + "=" + tx_loca;
							}
							else {
								sCadena = sCadena + "&To" + i + "=" + tx_tipovia + "&Co" + i + "=" +  tx_calle + "&Po" + i + "=" + tx_cp + "&lato" + i + "=" + tx_lat + "&lono" + i + "=" + tx_lon + "&provo" + i + "=" + tx_prov + "&locao" + i + "=" + tx_loca;
							}
							//ready = false;
							
						}					
						items = documento_xml.getElementsByTagName("item2");
						for (i = 0;i<=items.length-1;i++)
						{
							var item = items.item(i);
							tx_tipovia = ""
													
							if(item.getElementsByTagName("tx_calle")[0])
							{
								tx_calle = item.getElementsByTagName("tx_calle")[0].childNodes[0].nodeValue;
									
								if(item.getElementsByTagName("tx_loca")[0])
								{
									tx_loca = item.getElementsByTagName("tx_loca")[0].childNodes[0].nodeValue;
								}
								if(item.getElementsByTagName("tx_tipovia")[0])
								{
									tx_tipovia = item.getElementsByTagName("tx_tipovia")[0].childNodes[0].nodeValue;
								}
								if(item.getElementsByTagName("tx_prov")[0])
								{								
									tx_prov = item.getElementsByTagName("tx_prov")[0].childNodes[0].nodeValue;
								}
								if(item.getElementsByTagName("tx_cp")[0])
								{
									tx_cp = item.getElementsByTagName("tx_cp")[0].childNodes[0].nodeValue;
								}								
								else
								{
									tx_cp = "00000"
								}
								if(item.getElementsByTagName("lat")[0])
								{
									tx_lat = item.getElementsByTagName("lat")[0].childNodes[0].nodeValue;
								}
								else
								{
									tx_lat = "00000"
								}
								if(item.getElementsByTagName("long")[0])
								{
									tx_lon = item.getElementsByTagName("long")[0].childNodes[0].nodeValue;
								}
								else
								{
									tx_lon = "00000"
								}
								
								if(tx_tipovia == "")
								{
									if(tx_calle.indexOf("%20") != -1) 
									{
										tx_tipovia = tx_calle.substring(0, tx_calle.indexOf("%20"))
										tx_calle   = tx_calle.substring(tx_calle.indexOf("%20")+3, tx_calle.length)		
									}
								}
							}														
														
							sCadena = sCadena + "&Td" + i + "=" + tx_tipovia + "&Cd" + i + "=" +  tx_calle + "&Pd" + i + "=" + tx_cp + "&latd" + i + "=" + tx_lat + "&lond" + i + "=" + tx_lon + "&provd" + i + "=" + tx_prov + "&locad" + i + "=" + tx_loca;
							ready = false;
							
						}
						boton_reloj(0);
						tb_show('M&uacute;ltiples Resultados', '/iframes/multiples.asp?'+sCadena+'&keepThis=true&TB_iframe=true&height=400&width=400', '')
					}
					else
					{
						lato = documento_xml.getElementsByTagName("lato")[0].firstChild.nodeValue;
						lono = documento_xml.getElementsByTagName("lono")[0].firstChild.nodeValue;
						l1o = documento_xml.getElementsByTagName("l1o")[0].firstChild.nodeValue;
						latd = documento_xml.getElementsByTagName("latd")[0].firstChild.nodeValue;
						lond = documento_xml.getElementsByTagName("lond")[0].firstChild.nodeValue;
						l1d = documento_xml.getElementsByTagName("l1d")[0].firstChild.nodeValue;
						
						
						var finalpage = '/vemaps/mapa.asp?lator=';
						finalpage += lato + '&lonor=' + lono + '&l1or=' + l1o + '&latde=' + latd + '&londe=' + lond + '&l1de=' + l1d +'&mode=r&tx_busq_origen=' + escape(tx_busq_origen) + '&tx_busq_destino=' + escape(tx_busq_destino);
						top.document.location.href = finalpage;
						}
					}
				} else {
					if (g_sMostrarAlert == "true")
					{
						alert('<!-- ##Malerta_errordatos -->error en datos<!-- ##E -->');
					}
					boton_reloj(0);
					ready = false;
					
				}
			}else{
				if (g_sMostrarAlert == "true")
				{
					alert('<!-- ##Malerta_errordatos2 -->error en datos<!-- ##E -->');
				}
				boton_reloj(0);
				ready = false;
			}
		}
		catch(e)
		{
			boton_reloj(0);
			if (g_sMostrarAlert == "true")
			{
				for (atributo in e) alert("origen[" + atributo + "] = " + e[atributo]);
			}
		}
	}

	