function validaBusca(){
	
	var referencia 		= document.getElementById("Referência").value;
	var negociacao 		= document.getElementById("Negociacao").value;
	var tipo_imovel 	= document.getElementsByName("tipo_imovel");
	var tipos_imovel 	= document.getElementsByName("tipos_imovel");
	var indice = 0;
	var tipo = 0;
	
	if(referencia == "" || referencia == "Referência"){
	
		if(negociacao == ""){
			alert("Selecione o tipo de Negociação !");
			document.getElementById("Negociacao").focus();
			return false;
		}
		
		while (indice < tipos_imovel.length){
			if(tipos_imovel[indice].checked == true){ tipo++; }
			indice++;
		}
		
		indice = 0;
		
		while (indice < tipo_imovel.length){
			if(tipo_imovel[indice].checked == true){ tipo++; }
			indice++;
		}
		
		if(tipo == 0){
			alert("Selecione um tipo de Imóvel !");
			document.getElementById("txt_tipo_imovel").focus();
			return false;
		}
	}
	
	document.busca.submit()

}

function carregaTransacao(valor){
	if(valor.toLowerCase() == "vendas"){
		
		Cidades(valor);
		BairrosBusca("", valor);
		Tipos(valor);
		Precos(valor);
		
	}else{
		
		Cidades(valor);
		BairrosBusca("", valor);
		Tipos(valor);
		Precos(valor);
	}
}

function seleciona(conteudo, id){
	
	switch (id) {
		case "txt_preco_de":
			if(conteudo == "valorDe"){
				document.getElementById(id).innerHTML = "Valor De";
				break 
			}
			total = parseInt(conteudo.length) - 3;
			valor = conteudo.substring(total);
			final = conteudo.substring(total, conteudo.length);
			
			if(final == 0){
				conteudo = conteudo.replace(valor, "");
				conteudo = conteudo + " mil"
			}
			
			document.getElementById(id).innerHTML = conteudo;
			
			break
		case "txt_preco_ate":
			if(conteudo == "valorAte"){
				document.getElementById(id).innerHTML = "Valor Até";
				break 
			}
			
			if(conteudo.length > 6){
				valor = conteudo.substring(1);
				conteudo = conteudo.replace(valor, "");
				conteudo = conteudo + " milhão"
			}else{
				total = parseInt(conteudo.length) - 3;
				valor = conteudo.substring(total);
				conteudo = conteudo.replace(valor, "");
				conteudo = conteudo + " mil"
			}
			
			document.getElementById(id).innerHTML = conteudo;
			
			break
		default:
			document.getElementById(id).innerHTML = conteudo;
	}
}

function Some(id){
	if(id == undefined){
		document.getElementById("Tipos").style.display = "none";
		document.getElementById("Bairros").style.display = "none";
	}else{
		if(id == "imovelTipo"){
			document.getElementById("Bairros").style.display = "none";
		}else{
			document.getElementById("Tipos").style.display = "none";
		}
	}
}

function showCampos(campoID){
	if(campoID == "imovelTipo"){
		
		validaID = "validaTipo";
		valida = document.getElementById(validaID).value;
		
		if (valida == "0"){ 
			document.getElementById(validaID).value = "1";
			document.getElementById("Tipos").style.display = "block";
			document.getElementById("Tipos").className = "campoTiposBlock";
		}
		else{
			document.getElementById(validaID).value = "0";
			document.getElementById("Tipos").style.display = "none";
		}
		
	}else if(campoID == "imovelBairro"){
		
		validaID = "validaBairro";
		valida = document.getElementById(validaID).value;
		
		if (valida == "0"){ 
			document.getElementById(validaID).value = "1";
			document.getElementById("Bairros").style.display = "block";
			document.getElementById("Bairros").className = "campoTiposBlock";
		}
		else{
			document.getElementById(validaID).value = "0";
			document.getElementById("Bairros").style.display = "none";
		}
	}
}
	
function contagem(campo){
	
	if (campo == "tipo_imovel"){

		var Tipo_Imovel = document.getElementsByName("tipo_imovel");
		var indice 		= 0;
		var id_Tipo 	= 0;
		
			while (indice < Tipo_Imovel.length){
				
				 if(Tipo_Imovel[indice].checked == true){ id_Tipo++; }
				indice++;
			}
			
			if(id_Tipo < 1){ document.getElementById("txt_tipo_imovel").innerHTML="Tipos de Im&oacute;vel"; }
			else{
				if (id_Tipo == 1){ document.getElementById("txt_tipo_imovel").innerHTML=id_Tipo+" tipo selecionado"; }
				else{ document.getElementById("txt_tipo_imovel").innerHTML=id_Tipo+" tipos selecionados"; }
			}
	}
	
	if(campo == "bairro"){
		
		var Bairros			= document.getElementsByName("bairro");
		var indice 			= 0;
		var Vetor_Bairro 	= 0;
		
		while (indice < Bairros.length){
			
			 if(Bairros[indice].checked == true){ Vetor_Bairro++; }
			indice++;
			
		}
		
		if(Vetor_Bairro < 1){ document.getElementById("txt_bairro").innerHTML="Bairro"; }
		else{
			if (Vetor_Bairro == 1){ document.getElementById("txt_bairro").innerHTML=Vetor_Bairro+" bairro selecionado"; }
			else{ document.getElementById("txt_bairro").innerHTML=Vetor_Bairro+" bairros selecionados"; }
		}
	}
}

function BairrosBusca(cidade, transacao){
	
	if(transacao == undefined){ transacao = "VENDAS" } else { transacao = "ALUGUEL" }
	
	xmlHttp=GetXmlHttpObject();
	url="bairros_busca.asp";
	url = url + "?sid=" + Math.random();
	url = url + "&cidade="+ cidade;
	url = url + "&transacao="+ transacao;
	
	xmlHttp.onreadystatechange=AchouBairrosBusca;
	xmlHttp.open("GET",url,false);
	xmlHttp.send(null);

	if (xmlHttp.readyState == 1) { document.getElementById("txt_bairro").innerHTML = " ...Buscando dados... "; }


	function AchouBairrosBusca(){ 
		if (xmlHttp.readyState == 4){ 
			document.getElementById("txt_bairro").innerHTML = "Bairro";
			document.getElementById("Bairros").innerHTML = xmlHttp.responseText;
		}
	}
}

function Cidades(transacao){
		
	if(transacao == undefined){ var transacao = "VENDAS" } else { var transacao = transacao }
	
	xmlHttp=GetXmlHttpObject();
	url="cidades_busca.asp";
	url = url + "?sid=" + Math.random();
	url = url + "&transacao="+ transacao;
	
	xmlHttp.onreadystatechange=AchouCidades;
	xmlHttp.open("GET",url,false);
	xmlHttp.send(null);

	if (xmlHttp.readyState == 1) { document.getElementById("txt_cidade").innerHTML = " ...Buscando dados... "; }


	function AchouCidades(){ 
		if (xmlHttp.readyState == 4){
			
			document.getElementById("cidade").innerHTML = "";
			
			if(xmlHttp.responseText != "" && xmlHttp.responseText != undefined){
				var cidades = xmlHttp.responseText.split("#");
				var i = 0;
				
				var option = document.createElement("option");
				option.value = "Cidade";
				option.text = "Cidade";
				document.getElementById("cidade").options.add(option);
				
				for (i == 0; i < cidades.length; i++){
					
					var novo = document.createElement("option");
					novo.value = cidades[i];
					novo.text = cidades[i];
					document.getElementById("cidade").options.add(novo);
					
				}
			}
			document.getElementById("Bairros").innerHTML = "";
			document.getElementById("txt_cidade").innerHTML = "Cidade";
		}
	}
}

function Tipos(transacao){
	
	if(transacao == undefined){ transacao = "VENDAS" } else { transacao = transacao }
	
	xmlHttp=GetXmlHttpObject();
	url="tipos_busca.asp";
	url = url + "?sid=" + Math.random();
	url = url + "&transacao="+ transacao;
	
	xmlHttp.onreadystatechange=AchouTipos;
	xmlHttp.open("GET",url,false);
	xmlHttp.send(null);

	if (xmlHttp.readyState == 1) { document.getElementById("txt_tipo_imovel").innerHTML = " ...Buscando dados... "; }


	function AchouTipos(){ 
		if (xmlHttp.readyState == 4){ 
			document.getElementById("txt_tipo_imovel").innerHTML = "Tipos de Im&oacute;vel";
			document.getElementById("Tipos").innerHTML = xmlHttp.responseText;
		}
	}
}

function Precos(transacao){
	
	if(transacao == undefined){ transacao = "vendas" } else { transacao = transacao }
	
	if(transacao.toLowerCase() == "vendas"){
		
		document.getElementById("valorDe").innerHTML = "";
		
		var option 		= document.createElement("option");
		option.value 	= "valorDe";
		option.text 	= "valor De";
		document.getElementById("valorDe").options.add(option);	
                        
		var valorIncremento = 50000;
		var limite 			= 10;
		var descricao 		= "";
		var valor			= 0;
		
		for (i = 1; i <= limite; i++){
			
			valor 			= valorIncremento * i;
			valorFormat		= valor + ",00";
			valorFinal		= valorFormat.replace("000,00", ".000,00");
			descricao 		= "R$ " + valorFinal;
			optionFor		= document.createElement("option");
			optionFor.value = valor;
			optionFor.text 	= descricao;
			
			document.getElementById("valorDe").options.add(optionFor);			
		}
		
		document.getElementById("valorAte").innerHTML = "";
		
		
		var option2 	= document.createElement("option");
		option2.value 	= "valorAte";
		option2.text	= "Valor Até"
		document.getElementById("valorAte").options.add(option2);	
		
		valorIncremento = 100000;
		limite 			= 10;
		descricao 		= "";
		valor			= 0;
		
		for (i = 1; i <= limite; i++){
			
			valor 				= valorIncremento * i;
			valorFormat			= valor + ",00";
			valorFinal			= valorFormat.replace("000,00", ".000,00");
			valorFinal			= valorFinal.replace("000.000,00", ".000.000,00")
			descricao 			= "R$ " + valorFinal;
			optionFor2			= document.createElement("option");
			optionFor2.value 	= valor;
			optionFor2.text 	= descricao;
			
			document.getElementById("valorAte").options.add(optionFor2);			
		}
		
		var optionFinal 	= document.createElement("option");
		optionFinal.value 	= "1000000+";
		optionFinal.text 	= "+ de 1 milhão"
		document.getElementById("valorAte").options.add(optionFinal);
		
	}else{
		
		document.getElementById("valorDe").innerHTML = "";
		
		var option 		= document.createElement("option");
		option.value 	= "valorDe";
		option.text 	= "valor De";
		document.getElementById("valorDe").options.add(option);	
                        
		var valorIncremento = 500;
		var limite 			= 10;
		var descricao 		= "";
		var valor			= 0;
		
		for (i = 1; i <= limite; i++){
			
			valor 			= valorIncremento * i;
			valorFormat		= valor + ",00";
			valorFinal		= valorFormat.replace("000,00", ".000,00");
			if(i > 1){
				valorFinal		= valorFinal.replace("500,00", ".500,00");
			}
			descricao 		= "R$ " + valorFinal;
			optionFor		= document.createElement("option");
			optionFor.value = valor;
			optionFor.text 	= descricao;
			
			document.getElementById("valorDe").options.add(optionFor);			
		}
		
		document.getElementById("valorAte").innerHTML = "";
		
		
		var option2 	= document.createElement("option");
		option2.value 	= "valorAte";
		option2.text	= "Valor Até"
		document.getElementById("valorAte").options.add(option2);	
		
		valorIncremento = 1000;
		limite 			= 10;
		descricao 		= "";
		valor			= 0;
		
		for (i = 1; i <= limite; i++){
			
			valor 				= valorIncremento * i;
			valorFormat			= valor + ",00";
			valorFinal			= valorFormat.replace("000,00", ".000,00");
			descricao 			= "R$ " + valorFinal;
			optionFor2			= document.createElement("option");
			optionFor2.value 	= valor;
			optionFor2.text 	= descricao;
			
			document.getElementById("valorAte").options.add(optionFor2);			
		}
		
	}
}

function Clean(campoID){
	
	valor = document.getElementById(campoID).value;
	
	if(valor == ""){
		document.getElementById(campoID).value = "Referência" ;
	}else{
		if(campoID == valor){
			document.getElementById(campoID).value = "" ;
		}
	}
}

function desabilitaCampo(id){
	
	limite 		= document.getElementById("limite_" + id).value;
	validaTipo 	= document.getElementById("validaTipo").value;
	
	if(validaTipo == "0"){
		
		document.getElementById("validaTipo").value = "1";
		
		for(i=1; i < limite; i++){
			document.getElementById(id + "_" + i).disabled = 0;
		}
		
	}else{
		
		document.getElementById("validaTipo").value = "0";
		
		for(i=1; i < limite; i++){
			document.getElementById(id + "_" + i).disabled = 1;
		}
		
	}
	
}

function GetXmlHttpObject(){
	
	 if (window.XMLHttpRequest) {   
			   a=new XMLHttpRequest(); } //Objeto nativo (FF/Safari/Opera7.6+)  
		   else {  
			 try {   
				a=new ActiveXObject("Msxml2.XMLHTTP");  //activeX (IE5.5+/MSXML2+)  
			 }	 
			 catch(e) {  
			   try {   
				  a=new ActiveXObject("Microsoft.XMLHTTP"); //activeX (IE5+/MSXML1)  
			   }	 
			   catch(e) { /* O navegador não tem suporte */   
				  a=false;   
			   }  
			 }  
		   }   
		   return a;  
}
