try {
    xmlhttp = new XMLHttpRequest();
} catch(ee) {
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch(E) {
            xmlhttp = false;
        }
    }
}

fila = new Array();
ifila=0
to = '';
function ajaxHTML(id,url,destino)
{
    //Carregando...
	//alert(id);
	to = destino;
	
	if(id)
		document.getElementById(id).innerHTML='<div id="carregadorBusca" style="float:left; background:#006699; color:#FFFFFF; font-family: Verdana; font-size: 11px; font-weight:bold; padding:2px;">Carregando ... </div>';
    //Adiciona à fila
    fila[fila.length]=[id,url]
    //Se não há conexões pendentes, executa
	
    if((ifila+1)==fila.length)
		ajaxRun();
	
}

function ajaxRun()
{
    //Abre a conexão
	
    xmlhttp.open("GET",fila[ifila][1],true);
	//alert(fila[ifila][1]);
	
    //Função para tratamento do retorno
    xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4){
            //Mostra o HTML recebido
			
			texto = unescape(xmlhttp.responseText.replace(/\+/g," "))
			
			//if(fila[ifila][0])
				//document.getElementById(fila[ifila][0]).innerHTML=texto
			
			//alert(texto);
			
			
			if(texto.indexOf("table") == -1)
			{
				
				
				if(texto.indexOf("|") != -1)
				{
					//cid = document.getElementById("cidade");
					//ch = '|';
				}
				else if(texto.indexOf("%") != -1)
				{
					cid = document.getElementById("bairro");
					ch = '%';
				}
				
				
				
				//return;
				
				qtdCidade = i = cid.options.length;
				for(i = i-1; i > 0; i--)
				{
					//cidade.removeChild(cidade.options[i]);
					//alert(qtdCidade+" "+i+" "+cidade.options[i].text);
					cid.options[i] = null;
				}
				
				
				texto = texto.split(ch);
				
				i = 1;
				
				for(cont=0;cont<texto.length;cont++)
				{
					
					//conteudo.innerHTML += texto[cont];
					if(texto[cont])
					{
						//alert(texto[cont]);
							
						newText = texto[cont].split("*");
						//alert(newText[0]);return;
						cid.options[i] = new Option(newText[0]);
						cid.options[i].value = newText[1];
						i++;
					}
				}
				
				document.getElementById(fila[ifila][0]).innerHTML = '';
			}
			else
			{
				if(fila[ifila][0])
					document.getElementById(fila[ifila][0]).innerHTML=texto;
			}
				//alert(texto);
				//window.open('debug.php?'+xmlhttp.responseText);
				//if(xmlhttp.responseText.indexOf("consultarTarifasSaida") != -1)
					//document.getElementById("TxtResult").value = xmlhttp.responseText;return;
					
			//Roda o próximo
            ifila++
            if(ifila<fila.length)
				setTimeout("ajaxRun()",2)
			
        }
			
    }
	
	//Executa
    xmlhttp.send(null)
}

function showHideDiv(id)
{

	if($(id).style.display == 'block')
	{
		$(id).style.display = 'none';
		
	}
	else
	{
		$(id).style.display = 'block';
		location.href = '#top';
	}
}

function Detalhes(id, codAg)
{
	showHideDiv(id);
	showHideDiv("Detalhes");
	url = 'Detalhes.php?id='+codAg;
	ajaxHTML('Detalhes', url);
}

function DetalhesFoto(id, idAlbum, foto)
{
	showHideDiv(id);
	showHideDiv("Detalhes");
	url = 'DetalhesFoto.php?IdAlbum='+idAlbum+'&Foto='+foto;
	ajaxHTML('Detalhes', url);
}

function buscaCidade(id)
{
	url = "Cidades.php?uf="+id.value;
	alert(url);
	//ajaxHTML('', url);
}

function buscaBairro(id)
{
	url = "Cidades.php?cidade="+id;
	//alert(url);
	ajaxHTML('carregadorBusca', url);
}