varUFAvanc="PR";
varCidAvanc="1199";

// ================================================================================

function changeCidade() {
	cidade=$('#txtCidade').val();
    
	if(area=='imob') {
		fBuscaImob('');
	} else {
		getTipoPorRegiao(cidade);	
	}
}

// ================================================================================

function changeUF() {
	var uf;
	var cidade;
	var id;
	
	uf = $('#txtUF').val();
	
	$('#txtCidade option').each(function() {	
		$(this).remove();
	});
	
	$('#txtCidade').append('<option value="0">Cidades</option>');
	
	$.ajax({
		type: 'GET',
		async: false,
		url: 'inc/xml/cidades.asp?uf='+uf,
		success: function(xml) {
			$('cidade', xml).each(function() {
				cidade = $(this).find('nome').text();
				id = $(this).find('id').text();
				
				$('#txtCidade').append('<option value="'+id+'">'+cidade+'</option>');
			});
		}
	});
}

// ================================================================================

function fBuscaImob(valor,opcao) {
   var cidade = $('#txtCidade').val();
	if (opcao=='alfabeto') {
		var url_inc = "inc/xml/getdados.asp?o=imob&cid="+cidade+"&pg=imob&qry_ini="+valor+"&rdn="+aleatorio();
	} else {
		var url_inc = "inc/xml/getdados.asp?o=imob&cid="+cidade+"&pg=imob&qry="+valor+"&rdn="+aleatorio();
	}
   var r = '';
	var tipos = '';
	var erro;
   
	$.ajax({
		type: 'GET',
		async: false,
		url: url_inc,
		success: function(xml) {
			$('erro', xml).each(function() {
				erro=$(this).text();
			});
			
			if(erro=="True") {
				r = '<p class="erro">Não encontrado</p>';
			} else {	
				$('imobiliarias', xml).each(function() {			
					id = $(this).find('id').text();
					descricao = $(this).find('nome').text();
					vendas = $(this).find('vendas').text();
					locacao = $(this).find('locacao').text();
					estrelas = $(this).find('estrelas').text();
					desc_estrela = $(this).find('desc_estrela').text();
					logo = $(this).find('logo').text();
					telefone = $(this).find('telefone').text();
					url = $(this).find('url').text();
					coordx = $(this).find('coordx').text();
					coordy = $(this).find('coordy').text();
					email = $(this).find('email').text();
					
					if(vendas!="null" || locacao!="null") {
						if(vendas!="null" && locacao!="null") {
							link="<a href=\"javascript:void(0);\" onClick=\"getOpcoesImob('ven_tipo=0&imob="+id+"&uf=&cid=&tit="+descricao+"','vl');\">"+descricao+"</a>";
						}
						else if (vendas!="null") {
							link="<a href=\"javascript:void(0);\" onClick=\"getOpcoesImob('ven_tipo=0&imob="+id+"&uf=&cid=&tit="+descricao+"','v');\">"+descricao+"</a>";
						}
						else {
							link="<a href=\"javascript:void(0);\" onClick=\"getOpcoesImob('ven_tipo=0&imob="+id+"&uf=&cid=&tit="+descricao+"','l');\">"+descricao+"</a>";
						}					
						if (estrelas > 0) {
							if (logo!=".") {
								logo="<img src=\""+logo+"\" alt=\"logo "+descricao+"\" border=\"0\" width=\"135\" height=\"90\" />";
								logo="<a href=\""+url+"\" target=\"_blank\">"+logo+"</a>";
							}
							else {
								logo="";
							}
							botoes="<table><tr>"
							if(vendas!="null" && locacao!="null") {
								botoes=botoes+"<td><a href=\"javascript:void(0);\" onClick=\"getOpcoesImob('ven_tipo=0&imob="+id+"&uf=&cid=&tit="+descricao+"','vl');\">ver imóveis</a></td><td class=\"dados_imob\"><a href=\"javascript:void(0);\" onClick=\"getOpcoesImob('ven_tipo=0&imob="+id+"&uf=&cid=&tit="+descricao+"','vl');\"><img src=\"images/busca_imob/casa.png\" class=\"icons_estrela\" /></a></td>";
							}
							else if (vendas!="null") {
								botoes=botoes+"<td><a href=\"javascript:void(0);\" onClick=\"getOpcoesImob('ven_tipo=0&imob="+id+"&uf=&cid=&tit="+descricao+"','v');\">ver imóveis</a></td><td class=\"dados_imob\"><a href=\"javascript:void(0);\" onClick=\"getOpcoesImob('ven_tipo=0&imob="+id+"&uf=&cid=&tit="+descricao+"','v');\"><img src=\"images/busca_imob/casa.png\" class=\"icons_estrela\" /></a></td>";
							}
							else {
								botoes=botoes+"<td><a href=\"javascript:void(0);\" onClick=\"getOpcoesImob('ven_tipo=0&imob="+id+"&uf=&cid=&tit="+descricao+"','l');\">ver imóveis</a></td><td class=\"dados_imob\"><a href=\"javascript:void(0);\" onClick=\"getOpcoesImob('ven_tipo=0&imob="+id+"&uf=&cid=&tit="+descricao+"','l');\"><img src=\"images/busca_imob/casa.png\" class=\"icons_estrela\" /></a></td>";
							}
							
							if (url!="-1") {
								botoes=botoes+"<td><a href=\""+url+"\" target=\"_blank\">www</a></td><td class=\"dados_imob\"><a href=\""+url+"\" target=\"_blank\"><img src=\"images/busca_imob/ie.png\" class=\"icons_estrela\" /></a></td>";
							}
							else {
								botoes=botoes+"<td><img src=\"images/busca_imob/ie.png\" class=\"icons_estrela\" /></td><td> Site</td> ";
							}
							if (email!="-1") {
								botoes=botoes+"<td><a href=\"mailto:"+email+"\">e-mail</a></td><td class=\"dados_imob\"><a href=\"mailto:"+email+"\"><img src=\"images/busca_imob/mail.png\" class=\"icons_estrela\" /></a></td>";
							}
							else {
								botoes=botoes+"<td><img src=\"images/busca_imob/mail.png\" class=\"icons_estrela\" /></td><td> Email</td>";
							}
							if (coordx!="-1") {
								botoes=botoes+"<td><a href=\"javascript:void(0);\" onClick=\"getMapaLink('"+coordx+"','"+coordy+"','site')\">localização</a></td><td class=\"dados_imob\"><a href=\"javascript:void(0);\" onClick=\"getMapaLink('"+coordx+"','"+coordy+"','site')\"><img src=\"images/busca_imob/localizacao.png\" class=\"icons_estrela\" /></a></td>";
							}
							else {
								botoes=botoes+"<td><img src=\"images/busca_imob/localizacao.png\" class=\"icons_estrela\" /></a></td><td> Localização</td>";
							}
							botoes=botoes+"<td><a href=\"javascript:void(0);\" onClick=\"mostrarTelefone('"+id+"','"+telefone+"')\">fone</a></td><td><a href=\"javascript:void(0);\" onClick=\"mostrarTelefone('"+id+"','"+telefone+"')\"><img src=\"images/busca_imob/fone.png\" class=\"icons_estrela\" /></a></td><td><div id=\"telefone_"+id+"\"></div></td>";
							botoes=botoes+"</tr></table>"
							tipos=tipos+"<div class=\"logoImob\">"+logo+"</div><div id=\"dvEstrelado\">"+link+"<div class=\"box_estrela\">"+desc_estrela+"</div>"+botoes+"</div>";
						} else {
							tipos=tipos+"<div id=\"dvTipos\">"+link+"</div>";
						}
					}
					else {
						if(vendas!="null") {
							pg="v";
						}
						else if(locacao!="null") {
							pg="l";
						}
						link="<a href=\"imovel_pesq.asp?ven_tipo=0&pg="+pg+"&imob="+id+"&uf=&cid=&tit="+descricao+"\">"+descricao+"</a>";
						tipos=tipos+"<div id=\"dvTipos\">"+link+"</div>";
					}
				});
			}
			
			if (r=='') {
				r = tipos;
			}			
			
			$('#dvTipoDeImoveis').html(r);
		}
	});
	
	getAlfabeto(cidade);
}

// ================================================================================

function fMapa(varUrl,vW,vH) {
	if(varUrl!='') {
		window.open(varUrl,'jMapa','width='+vW+',height='+vH);	   
	}
}

// ================================================================================

function getUF() {
	var url_inc = "inc/xml/getXML.asp?o=UF&pg="+area+"&dv=dvUF&rdn="+aleatorio();   
	
	$.ajax({
		type: 'GET',
		async: false,
		url: url_inc,
		success: function(xml) {
			$('erro', xml).each(function() {
				erro=$(this).text();
			});
			
			if(erro=="True") {
				$('#dvUF').html('<p class="erro">Não encontrado</p>');
			} else {
				select_uf = '<select id="txtUF" name="txtUF" onchange="changeUF();"><option value="0">UF</option></select>';
				$('#dvUF').html(select_uf);

				$('uf', xml).each(function() {
					uf=$(this).attr('nome');
					$('#txtUF').append('<option value="'+uf+'">'+uf+'</option>');
				});
				
				var objUF=document.getElementById("txtUF");				
				var b=false;
				//instrução usada para deixar por default o estado PR
				for (var i=0;i<objUF.options.length;i++) {
					if(objUF.options[i].value.toUpperCase()==varUFAvanc.toUpperCase()) {
						objUF.options[i].selected=true;
						b=true;
					}
				}
				
				if(b==false) {
					for (var i=0;i<objUF.options.length;i++) {
						if(objUF.options[i].value.toUpperCase()=="PR") {
							objUF.options[i].selected=true;
						}
					}
				}
			}
		}	
	});
	
	uf = $('#txtUF').val();	
	getCidade(uf);
}

// ================================================================================

function getAlfabeto(cidade) {
	var url_inc='inc/xml/getalfabeto.asp?cid='+cidade+'&rdn='+aleatorio();   
	$('#dvAlfabeto').load(url_inc);
}

// ================================================================================

function getCidade(uf) {	
   var url_inc="inc/xml/getXML.asp?o=CIDADES&uf="+uf+"&pg="+area+"&rdn="+aleatorio();
   
	$.ajax({
		type: 'GET',
		async: false,
		url: url_inc,
		success: function(xml) {
			$('erro', xml).each(function() {
				erro=$(this).text();
			});		
			
			if(erro=="True") {
				$('#dvCidade').html('<p class="erro">Não encontrado</p>');
			} else {
				select_cidade='<select id="txtCidade" name="txtCidade" onchange="changeCidade();"><option value="0">Cidades</option></select>';				
				$('#dvCidade').html(select_cidade);
				
				$('cidade', xml).each(function() {
					cidade = $(this).attr('nome');
					idcidade = $(this).attr('idcidade');
					$('#txtCidade').append('<option value="'+idcidade+'">'+cidade+'</option>');
				});
				
				var objCidade=document.getElementById("txtCidade");
				
				//instrução usada para deixar por default Curitiba
				for (var i=0;i<objCidade.options.length;i++)	{
					if(objCidade.options[i].value.toUpperCase()==varCidAvanc.toUpperCase()) {
						objCidade.options[i].selected=true;					
					}
				}
			}
		}
	});
	
	cidade = $('#txtCidade').val();
	
	if (area=='imob') {
		fBuscaImob('');
	} else {
		getTipoPorRegiao(cidade);
	}
}

// ================================================================================

function getMapaLink(cX,cY,area,vImob,vRef,vPG) {
	vW=800;
	vH=500;
	
	var vUrlAmpliar='';
	if(area=='site') {
		vUrlAmpliar="popAmpliarMapa.asp";
	} else if(area=='admin') {
		vUrlAmpliar="../popAmpliarMapa.asp";	
	}
	varUrlM=vUrlAmpliar+"?lat="+cX+"&lng="+cY+"&imob="+vImob+"&ref="+vRef+"&pg="+vPG;
	fMapa(varUrlM,vW,vH);
}

// ================================================================================

function getOpcoesImob(varParametroImob,varTipoTrabalho) {
	var varTipos="Imóveis para:";
	var varLink='';
	
	var ufimob = $('#txtUF').val();
	
	if (varTipoTrabalho=='vl' || varTipoTrabalho=='v') {
		varLink="<a href=\"imovel_pesq.asp?pg=v&"+varParametroImob+"&ufimob="+ufimob+"\">Vendas</a>";
		varTipos=varTipos+"<div id=\"dvTipos\">"+varLink+"</div>";
	}
	varLink="''";	
	if (varTipoTrabalho=='vl' || varTipoTrabalho=='l') {
		varLink="<a href=\"imovel_pesq.asp?pg=l&"+varParametroImob+"&ufimob="+ufimob+"\">Locação</a>";
		varTipos=varTipos+"<div id=\"dvTipos\">"+varLink+"</div>";
	}	
	$('#dvTipoDeImoveis').html(varTipos);
}

// ================================================================================

function mostrarTelefone(id_div,telefone) {
	$('#telefone_'+id_div).html(telefone);
}


