function busca_cidades(str){
	if (str==""){
		document.getElementById("cidades").innerHTML="";
		return;
	}
	if (window.XMLHttpRequest){// para IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	} else {// para IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}

	xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			//document.getElementById("cidades").innerHTML=xmlhttp.responseText;
			document.getElementById("cidade").innerHTML=xmlhttp.responseText;
		}
	}
	
	xmlhttp.open("GET","busca/cidade.php?q="+str,true);
	xmlhttp.send();
	
	if (str=="CO")ChamaComprar();  else   ChamaAlugar();
}

function busca_bairros(str1, str2){
	var str2 = document.getElementById("opcaoEscolhida").value;
	if (str1=="", str2==""){
		document.getElementById("bairros").innerHTML="";
		return;
	}
	
	if (window.XMLHttpRequest){// para IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	} else {// para IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}

	xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			document.getElementById("bairro").innerHTML=xmlhttp.responseText;
		}
	}
	xmlhttp.open("GET","busca/bairro.php?cidade="+str1+"&q1="+str2,true);
	xmlhttp.send();
	
	document.getElementById("bairro_fake").style.display = 'none';
}

function ChamaComprar(){
        var opcao="CO";
        var id="valorminimo";
        var valorMultiplo=50000;
        var primeirovalor=0;
        var ultimovalor=999999999999;  //12 digitos
        ConstroeSelect(id, valorMultiplo, primeirovalor, opcao);
		
        id="valormaximo";
        var valorMultiplo=170000;
     //  ConstroeTipos_imoveis(opcao);
		ConstroeSelect(id, valorMultiplo, ultimovalor, opcao);
		
		document.getElementById("cidade_fake").style.display = 'none';
}

function ChamaAlugar(){
        var opcao="AL";
        var id="valorminimo";
        var valorMultiplo=100;
        var primeirovalor=0;
        var ultimovalor=999999999999;  //12 digitos
        ConstroeSelect(id, valorMultiplo, primeirovalor, opcao);
        
        id="valormaximo";
        var valorMultiplo=300;
      //  ConstroeTipos_imoveis(opcao);
		ConstroeSelect(id, valorMultiplo, ultimovalor, opcao);
		
		document.getElementById("cidade_fake").style.display = 'none';
}

function ConstroeSelect(id, valorMultiplo, valor1, opcao){
        var select_de = document.getElementById(id);
                 select_de.innerHTML = "";

        var new_opcao = document.createElement("option");
        if (id=="valormaximo"){
        var texto = document.createTextNode("Valor Máximo");
        
        }else var texto = document.createTextNode("Valor Minimo");
                new_opcao.setAttribute("value",valor1);
                new_opcao.appendChild(texto);
                select_de.appendChild(new_opcao);
        
        if (opcao=="CO") var valor=10000;       else var valor=100;
        
        for(var i = 0; i < 10; i++) {
                
                var new_opcao = document.createElement("option");
                var texto = document.createTextNode("R$"+valor+",00");
                new_opcao.setAttribute("value",valor);
                new_opcao.appendChild(texto);
                select_de.appendChild(new_opcao);
                
                valor+=valorMultiplo;
        }
}
