function GetXMLHttp() {
if(navigator.appName == "Microsoft Internet Explorer") {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else {
xmlHttp = new XMLHttpRequest();
}
return xmlHttp;
}

var xmlRequest = GetXMLHttp();


function localizaCidade(idTipoImovel){

	if(idTipoImovel != '0'){

	document.getElementById("dormitoriosBusca").style.display="none";
	document.getElementById("garagemBusca").style.display="none";

	if(idTipoImovel == 1){
		document.getElementById("dormitoriosBusca").style.display="block";
		document.getElementById("garagemBusca").style.display="block";
	}

	else if(idTipoImovel == 2){
		document.getElementById("dormitoriosBusca").style.display = "block";
		document.getElementById("garagemBusca").style.display = "block";

	}else if(idTipoImovel == 11){
		document.getElementById("dormitoriosBusca").style.display = "block";
		document.getElementById("garagemBusca").style.display = "block";

	}else if(idTipoImovel == 12){
		document.getElementById("dormitoriosBusca").style.display = "block";
		document.getElementById("garagemBusca").style.display = "block";

	}else if(idTipoImovel == '0'){
		document.getElementById("dormitoriosBusca").style.display = "none";
		document.getElementById("garagemBusca").style.display = "none";

	}else{

		document.getElementById("dormitoriosBusca").style.display="none";
		document.getElementById("garagemBusca").style.display="none";

	}

	xmlRequest.open("GET","index.php?m=Index&A=Index&a=showBuscaPrincipalCidades&idTipoImovel="+idTipoImovel,true);
    xmlRequest.onreadystatechange = mudEst;
    xmlRequest.send(null);

	if (xmlRequest.readyState == 1) {
document.getElementById("cidade").options[0]=new Option(" -- Aguarde ... -- "," -- Aguarde ... -- ");

	document.getElementById("loadingBuscaNormal").style.display="Block";

	document.getElementById("botaoBuscaNormal").disabled=true;

}

	}

}

function mudEst(){
        if (xmlRequest.readyState==4){

            //limpa o select
            var cidade=document.getElementById("cidade");

            var bairro=document.getElementById("bairro");
            while(bairro.options.length>0){
				bairro.options[0]=null;
			}

            while(cidade.options.length>0){
				cidade.options[0]=null;
			}
            //Transforma a lista de cidades JSON em Javascript
            var aCidades=eval('('+xmlRequest.responseText+')');

				cidade.options[cidade.options.length]=new Option('Selecione a Cidade',0);

				var bairro=document.getElementById("bairro");
				bairro.options[0]=new Option('Selecione a Cidade',0);

            //popula o select com a lista de cidades obtida
            for(var i=0;i < aCidades.Cidades.length;i++){

                aCidades.Cidades[i].cidade=unescape(aCidades.Cidades[i].cidade);

                cidade.options[cidade.options.length]=new Option(aCidades.Cidades[i].cidade,aCidades.Cidades[i].idCidade);

            }

			document.getElementById("loadingBuscaNormal").style.display="none";

			document.getElementById("botaoBuscaNormal").disabled=false;


        }


}

function localizaBairro(idCidade){

	if(idCidade != '0'){

	var idTipo = document.getElementById("tipoImovel").value;

	xmlRequest.open("GET","index.php?m=Index&A=Index&a=showBuscaPrincipalBairros&idCidade="+idCidade+"&idTipo="+idTipo,true);
    xmlRequest.onreadystatechange = mudEstBairro;
    xmlRequest.send(null);

	if (xmlRequest.readyState == 1) {
document.getElementById("bairro").options[0]=new Option(" -- Aguarde ... -- "," -- Aguarde ... -- ");

document.getElementById("loadingBuscaNormal").style.display="Block";

document.getElementById("botaoBuscaNormal").disabled=true;

}

	}

}

function mudEstBairro(){
        if (xmlRequest.readyState==4){
            //limpa o select
            var bairro=document.getElementById("bairro");
            while(bairro.options.length>0){
				bairro.options[0]=null;
			}

            bairro.options[bairro.options.length]=new Option('Selecione o Bairro',0);

            //Transforma a lista de cidades JSON em Javascript
            var aBairro=eval('('+xmlRequest.responseText+')');

				//popula o select com a lista de cidades obtida
				for(var i=0;i < aBairro.Bairros.length;i++){

					aBairro.Bairros[i].bairro=unescape(aBairro.Bairros[i].bairro);
					bairro.options[bairro.options.length]=new Option(aBairro.Bairros[i].bairro);

		       }

			    document.getElementById("loadingBuscaNormal").style.display="none";

			    document.getElementById("botaoBuscaNormal").disabled=false;


		}
        }	
