//var request;
var contenedor;
var iniciarEn = 1;
var titulo1 = 'Ciudad';
var titulo2 = 'Comuna';
var titulo3 = 'Local';	

function CreateXmlHttpObj()
{
	var xmlhttp=false;
//	try {
//		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
//	} catch (e) {
//		try {
//			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
//		} catch (E) {
//			xmlhttp = false;
//		}
//	}

//	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
//	xmlhttp = new XMLHttpRequest();
//	}
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		xmlhttp = new XMLHttpRequest();
		if (xmlhttp.overrideMimeType) {
			xmlhttp.overrideMimeType('text/html');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
			   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	return xmlhttp;
}

function PoblarDatos(modo,valor1,valor2,valor3)
{	
	var request = CreateXmlHttpObj();

	if(!request)
	{
		alert ('No se puede crear consulta ajax');
		return false;
	}
	else
	{
		//request.setRequestHeader('Content-type: text/javascript; charset=iso-8859-1');		
		//alert (modo);		

		if (!(document.getElementsByTagName || document.all))
		{		
			return;
		}

		request.open('GET', 'archivos/datos_tmp.txt', true);
		request.onreadystatechange = function (){		
			if ((request.readyState == 4) && (request.status == 200))
			{
				urlArray = request.responseText.split("\n");
				imprimir(urlArray,modo,valor1,valor2,valor3);
			}
			else if ((request.readyState == 4) && (request.status != 200))
			{
				alert('404 Document not found');
			}
		}
		request.send(null);
	}
}

function imprimir(lineas,modo,valor1,valor2,valor3)
{
	var arreglo = new Array();

	if (modo != 2)
	{
		for (i = iniciarEn; i < lineas.length; i++)
		{		
			campos = lineas[i].split("\t");
			if (modo == 0) arreglo[i] = campos[0];
			else if (modo == 1 && valor1 == campos[0])	arreglo[i] = campos[1];
			else if (modo == 3 && valor1 == campos[0] && valor2 == campos[1])
			{
				arreglo[i] = campos[2] + '---' + campos[3] + '---' + campos[4];				
			}
		}
	}
	else if (modo == 2) 
	{
		arreglo[0] = "Todas";
	}
	

	var arreglo2 = unique(arreglo);	
	
	if (modo != 3 || (modo == 3 && valor3 == '')) document.getElementById('celInfo').innerHTML = '';

	salida = '';
	if (modo == 0)
	{
		contenedor = document.getElementById('cboE');
		salida = salida + "<select name='cboE' class='yellowBg'     onchange='PoblarDatos(1,this.value,\"\",\"\");'>" + "\n";
		salida = salida + "<option selected value=''>Seleccionar " + titulo1 + "....</option>" + "\n";
	}
	else if (modo == 1) 
	{
		contenedor = document.getElementById('cboC');
		salida = salida +  "<select name='cboC' class='yellowBg' onchange='PoblarDatos(2,document.fBsqTda.cboE.value,this.value,\"\");'>" + "\n";
		salida = salida +  "<option selected value=''>Seleccionar " + titulo2 + "....</option>" + "\n";
	}
	else if (modo == 2) 
	{
		contenedor = document.getElementById('cboT');
		salida = salida +  "<select name='cboT' class='yellowBg' onchange='PoblarDatos(3,document.fBsqTda.cboE.value,document.fBsqTda.cboC.value,this.value);'>" + "\n";
		salida = salida +  "<option selected value=''>Seleccionar " + titulo3 + "....</option>" + "\n";
	}
	else if (modo == 3 && valor3 == 'Todas')
	{
		contenedor = document.getElementById('celInfo');
		salida = salida +  '<table width="515" class="small" cellpadding="4">' + "\n";
		salida = salida +  '<tr valign="top"><td width="130"><b>Nombre</b></td><td width="285"><b>Direcci&oacute;n</b></td><td width="75"><b>Tel&eacute;fono</b></td></tr>' + "\n";
		salida = salida +  '<tr height="1"><td width="515" colspan="3" bgcolor="666666"></td></tr>' + "\n";
	}

	if (modo == 0 || modo == 1 || modo == 2)
	{
		for (z = 0; z <= arreglo2.length; z++)
		{
			if (arreglo2[z] != '' && arreglo2[z] != undefined) salida = salida + '<option value="' + arreglo2[z]  + '">' + Limpiar(arreglo2[z])  + '</option>' + "\n";
		}		
		salida = salida + '</select>' + "\n";
	}
	else if (modo == 3 && valor3 == 'Todas')
	{
		if (-1 == 0)
		{
			salida = salida +   '<tr>' + "\n";
			salida = salida +   '<td colspan="3">No se encontraron locales</td>' + "\n";
			salida = salida +   '</tr>' + "\n";
		}
		else
		{
			for (z = 0; z < arreglo2.length; z++)
			{
				if (arreglo2[z] != '' && arreglo2[z] != undefined)
				{
					items2 = arreglo2[z].split("---");
					casos = 0;
					salida = salida + '<tr class="small">' + "\n";
					for (y = 0; y < items2.length; y++)
					{
						salida = salida + '<td>' + Limpiar(items2[y]) + '</td>' + "\n";								
						casos = casos + 1;
					}
					salida = salida + '</tr>' + "\n";
				}
			}		
		}
		salida = salida +   '</table>' + "\n";		
	}
		
	contenedor.innerHTML = salida;
}

function Limpiar(texto)
{	
	texto = texto.replace(/u00C1/g,'&Aacute;');
	texto = texto.replace(/u00E1/g,'&aacute;');
	texto = texto.replace(/u00C9/g,'&Eacute;');
	texto = texto.replace(/u00E9/g,'&eacute;');
	texto = texto.replace(/u00CD/g,'&Iacute;');
	texto = texto.replace(/u00ED/g,'&iacute;');
	texto = texto.replace(/u00D3/g,'&Oacute;');
	texto = texto.replace(/u00F3/g,'&oacute;');
	texto = texto.replace(/u00DA/g,'&Uacute;');
	texto = texto.replace(/u00FA/g,'&uacute;');
	texto = texto.replace(/u00D1/g,'&Ntilde;');
	texto = texto.replace(/u00F1/g,'&ntilde;');
	texto = texto.replace(/u00DC/g,'&Uuml;');
	texto = texto.replace(/u00FC/g,'&uuml;');
	texto = texto.replace(/u0022/g,'&amp;');
	texto = texto.replace(/u003C/g,'&lt;');
	texto = texto.replace(/u003E/g,'&gt;');
	texto = texto.replace(/u0022/g,'&quot;');
	texto = texto.replace(/u00BA/g,'&deg;');
	return texto;
}

function unique(a) {
	tmp = new Array(0);
	for(i=0;i<a.length;i++){
		if(!contains(tmp, a[i])){
			tmp.length+=1;
			tmp[tmp.length-1]=a[i];
		}
	}
	return tmp;
}

function contains(a, e) {
	for(j=0;j<a.length;j++)if(a[j]==e)return true;
	return false;
}
