function carregaTransacao(valor){
	if(valor.toLowerCase() == "venda"){
		
		Cidades(valor);
		Bairros("", valor);
		Tipos(valor);
		Precos(valor);
		
	}else{
		
		Cidades(valor);
		Bairros("", 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);
			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
		case "txt_cidade":
			indice = document.getElementById(conteudo).selectedIndex;
			document.getElementById(id).innerHTML = document.getElementById(conteudo).options[indice].text;
			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 == "id_Tipo"){

		var Tipo_Imovel = document.getElementsByName("id_Tipo");
		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 marcado"; }
			else{ document.getElementById("txt_tipo_imovel").innerHTML = id_Tipo+" tipos marcados"; }
		}
	}
	
	if(campo == "id_Bairro"){
		
		var Bairros			= document.getElementsByName("id_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 Bairros(cidade, transacao){
	
	if(transacao == undefined){ transacao = "venda" } else { transacao = "aluguel" }
	
	xmlHttp=GetXmlHttpObject();
	url = "bairros_busca.asp";
	url = url + "?sid=" + Math.random();
	url = url + "&cidade="+ cidade;
	url = url + "&transacao="+ transacao;
	
	xmlHttp.onreadystatechange=AchouBairros;
	xmlHttp.open("GET",url,false);
	xmlHttp.send(null);

	if (xmlHttp.readyState == 1) { document.getElementById("txt_bairro").innerHTML = " ...Buscando dados... "; }


	function AchouBairros(){ 
		if (xmlHttp.readyState == 4){ 
			document.getElementById("txt_bairro").innerHTML = "Bairro";
			document.getElementById("Bairros").innerHTML = xmlHttp.responseText;
		}
	}
}

function Cidades(transacao){
	
	if(transacao == undefined){ transacao = "venda" } else { 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 valores = cidades[i].split(",");
					var novo = document.createElement("option");
					novo.value = valores[1];
					novo.text = valores[0];
					document.getElementById("cidade").options.add(novo);
					
				}
			}
			document.getElementById("Bairros").innerHTML = "";
			document.getElementById("txt_cidade").innerHTML = "Cidade";
		}
	}
}

function Tipos(transacao){
	
	if(transacao == undefined){ transacao = "venda" } 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 = "venda" } else { transacao = transacao }
	
	if(transacao.toLowerCase() == "venda"){
		
		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 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;  
}
