
// Busqueda por nombre de Ubigeo


// Constantes
var NIVEL_PERU = "peru";
var NIVEL_DPTO = "dpto";
var NIVEL_PROV = "prov";
var NIVEL_DIST = "dist";


function buscarUbigeo(){
	texto = Trim(getElemento("txtBuscar").value);	
	if(texto.length==0){
		alert("Ingrese texto a buscar.");
		getElemento("tdBusqueda").innerHTML="";
	}
	else if(texto.length<3){
		alert("Ingrese como minimo 3 caracteres");
		getElemento("tdBusqueda").innerHTML="";
	}
	else{
		getElemento("tdBusqueda").innerHTML="Buscando ...";		
		getElemento("tdBusqueda").innerHTML=getHtmlListaUbigeo(texto);
	}
}

function getHtmlListaUbigeo(psNombre) {	
	
	var html = "";
	var intFila = 1;
	var fila = 0;
	var indiceUbigeo = 0;
	var indiceInicial = 0;
	var indiceFinal = 0;
	aData = new Array ();
		
	// nivel, CC, NombreDD, NombrePP, NombreDI
	if(psNombre.toUpperCase()=='PERU'){
		aData[fila] = new Array ('', '', 'PERU', '', '');
		fila++;
	}	
	
	for (var ind = 0; ind < aDatoDD.length; ind++) {
		//alert(aDatoDD[ind][1] + " " + psNombre.toUpperCase());
		//alert(aDatoDD[ind][1].indexOf(psNombre.toUpperCase()).toString());
		if (aDatoDD[ind][1].indexOf(psNombre.toUpperCase()) >= 0) {
			aData[fila] = new Array ('dpto', aDatoDD[ind][0], aDatoDD[ind][1], '', '');
			fila++;
		}
	}
	
	for (var ind = 0; ind < aDatoPP.length; ind++) {
		if (aDatoPP[ind][1].indexOf(psNombre.toUpperCase()) >= 0) {
			//indiceUbigeo = parseInt(aDatoPP[ind][0].substring(0, 2)) - 1;
			indiceUbigeo = aDatoPP[ind][0].substring(0, 2) - 1;
			aData[fila] = new Array ('prov', aDatoPP[ind][0], aDatoDD[indiceUbigeo][1], aDatoPP[ind][1], '');
			fila++;
		}
	}
	
	for (var ind = 0; ind < aDatoDI.length; ind++) {
		if (aDatoDI[ind][1].indexOf(psNombre.toUpperCase()) >= 0) {
			//codigo = aDatoDI[ind][0].substring(0, 2);
			indiceUbigeo = aDatoDI[ind][0].substring(0, 2) - 1;
			//alert(indiceUbigeo.toString());
			NombreDD = aDatoDD[indiceUbigeo][1];
			indiceInicial = parseInt(aDatoDD[indiceUbigeo][2]);
			indiceFinal = parseInt(aDatoDD[indiceUbigeo][3]);
			
			for (var cont = indiceInicial; cont <= indiceFinal; cont++) {
				if (aDatoDI[ind][0].substring(0, 4) == aDatoPP[cont][0]) NombrePP = aDatoPP[cont][1];
			}
			
			aData[fila] = new Array ('dist', aDatoDI[ind][0], NombreDD, NombrePP, aDatoDI[ind][1]);
			fila++;
		}
	}
	
	html = "<TABLE valign='top' BORDER='0' CELLSPACING='0' CELLPADDING='0' class='tabla'>";
	//html += "<TR><TD width='10%'></TD><TD width='15%'></TD><TD width='5%'></TD><TD width='70%'></TD></TR>";
	html += "<TR><TD></TD><TD></TD><TD></TD><TD></TD></TR>";	
	html += "<TR><TD colspan='4'>" + aData.length.toString() + " coincidencias con <B>" + psNombre.toUpperCase() + "</B></TD></TR>";
	html += "<TR><TD colspan='4'>&nbsp;</TD></TR>";
    estilo = "class='texto'";
	estilo="";
	for (var ind = 0; ind < aData.length; ind++) {
		if (aData[ind][0] == '') {			
			html += "<TR "+estilo+">";
			html += "<TD "+estilo+">" + intFila.toString() + ".&nbsp;</TD>";
			html += "<TD "+estilo+">Pa�s</TD><TD>:&nbsp;</TD>";
			html += "<TD "+estilo+"><a href=\"#\" onclick=\"javascript:clickMapaNav('" + aData[ind][1] + "')\">" + aData[ind][1] + "&nbsp;" + aData[ind][2] + "</a></TD></TR>";			
			html += "<TR "+estilo+"><TD colspan='4'>&nbsp;</TD></TR>";
		}
		if (aData[ind][0] == NIVEL_DPTO) {			
			html += "<TR "+estilo+">";
			html += "<TD "+estilo+">" + intFila.toString() + ".&nbsp;</TD>";
			html += "<TD "+estilo+">Dpto.</TD><TD>:&nbsp;</TD>";
			html += "<TD "+estilo+"><a href=\"#\" onclick=\"javascript:clickMapaNav('" + aData[ind][1] + "')\">" + aData[ind][1] + "&nbsp;" + aData[ind][2] + "</a></TD></TR>";			
			html += "<TR "+estilo+"><TD colspan='4'>&nbsp;</TD></TR>";
		}
		if (aData[ind][0] == NIVEL_PROV) {			
			html += "<TR "+estilo+">";
			html += "<TD "+estilo+">" + intFila.toString() + ".&nbsp;</TD>";
			html += "<TD "+estilo+">Dpto.</TD><TD>:&nbsp;</TD>";
			html += "<TD "+estilo+">" + aData[ind][1].substring(0,2) + "&nbsp;" + aData[ind][2] + "</TD></TR>";
			html += "<TR "+estilo+"><TD>&nbsp;</TD>";
			html += "<TD "+estilo+">Prov.</TD><TD>:&nbsp;</TD>";			
			html += "<TD "+estilo+"><a href=\"#\" onclick=\"javascript:clickMapaNav('" + aData[ind][1] + "')\" >" + aData[ind][1].substring(2,4) + "&nbsp;" + aData[ind][3] + "</a></TD></TR>";			
			html += "<TR "+estilo+"><TD colspan='4'>&nbsp;</TD></TR>";
		}
		if (aData[ind][0] == NIVEL_DIST) {      						
			html += "<TR "+estilo+">";
			html += "<TD "+estilo+">" + intFila.toString() + ".&nbsp;</TD>";
			html += "<TD "+estilo+">Dpto.</TD><TD>:&nbsp;</TD>";
			html += "<TD "+estilo+">" + aData[ind][1].substring(0,2) + "&nbsp;" + aData[ind][2] + "</TD></TR>";			
			html += "<TR "+estilo+"><TD>&nbsp;</TD>";
			html += "<TD "+estilo+">Prov.</TD><TD>:&nbsp;</TD>";
			html += "<TD "+estilo+">" + aData[ind][1].substring(2,4) + "&nbsp;" + aData[ind][3] + "</TD></TR>";
			html += "<TR "+estilo+"><TD>&nbsp;</TD>";
			html += "<TD "+estilo+">Dist.</TD><TD>:&nbsp;</TD>";
			html += "<TD "+estilo+"><a href=\"#\" onclick=\"javascript:clickMapaNav('" + aData[ind][1] + "')\">" + aData[ind][1].substring(4,6) + "&nbsp;" + aData[ind][4] + "</a></TD></TR>";
			html += "<TR "+estilo+"><TD colspan='4'>&nbsp;</TD></TR>";
		}
		intFila++;
 	}
	html += "</TABLE>";
	return html;
}; 


// ver/oculta buscar

function verBuscar(num){
	//verfrmBuscar();
	
	if(num=="1"){
		// ver		
		//getElemento("aBuscar").innerHTML="Buscar (Ocultar)";
		getElemento("aBuscar").href="javascript:verBuscar('0')";
		getElemento("divBuscar").style.display="";
		getElemento("txtBuscar").focus();
	}else{
		//ocultar
		//getElemento("aBuscar").innerHTML="Buscar (Ver)";
		getElemento("aBuscar").href="javascript:verBuscar('1')";
		getElemento("divBuscar").style.display="none";
	}
	
}
