function swapI(obj,focus){
	if(focus){
		if(obj.value == obj.attributes["label"].value){
			obj.value = "";
		}
	} else {
		if(obj.value.trim() == ""){
			obj.value = obj.attributes["label"].value;
		}
	}
}
function swapP(obj,focus){
	if(focus){
		var pass = document.getElementById(obj.attributes["parent"].value);
		if(pass){
			obj.innerHTML = "";
			pass.focus();
		}
	} else {
		if(obj.value == ""){
			var div = document.getElementById("div"+obj.id);
			if(div){
				div.innerHTML = obj.attributes["label"].value;
			}
		}
	}
}
function swapM(menu,show){
	var m = $('#div_'+menu);
	if(m){
		if(show){
			tmp_width = m.parent().width();
			if(menu == 'contato'){
				tmp_width += 30;
				m.css('margin-left','-15px');
			}
			tmp_width = tmp_width+'px';
			m.find('table.submenu').each(function (){
				$(this).css('width',tmp_width);
			});
			m.css('display','');
		} else {
			m.css('display','none');
		}
	}
}

function resetSelect(tmp_idcmapo){
	var campo = document.getElementById(tmp_idcmapo);
	if(campo){
		var dis = campo.disabled;
		campo.disabled = '';
		for(var x=campo.length-1;x>=2;x--){
			campo.remove(x);
		}
		campo.disabled = dis;
	}
}


// limpando a div antes de um novo envio
function buscaTopo() {

	var ok = false;
	var msg = 'Selecione uma cidade para continuar.';
	
	//if ($('#cod_cidade').val().length > 1){
	var cod_cidade = $('#cod_cidade').val();

	if (cod_cidade.length < 1){
	ok = false;
	alert(msg);
	}
	else {
	ok = true
	}
	var bairro_total = $('#Bairro_CMB0_total').val();
	bairros = $('#Bairro_CMB0_inner').find('input').filter(':checked');
	var i=0
	var selecionadas = ''
        for (var i = 0; i < bairros.length; i++) {
            alert(bairros[i].id + " - " + $("#" + bairros[i].id).is(':checked') );
        // Retorna true em todos os checkboxes
            if ($("#" + bairros[i].id).is(':checked')) {
      // Sempre entra aqui, mesmo estando desmarcado
               selecionadas += $("#"+ bairros[i].id).val() + ",";
           }
        } 

	var valor_imovel = $('#valor_imovel').val();
	var tipo_operacao = $('#tipo_operacao').val();
	var cod_tipo_imovel = $('#cod_tipo_imovel').val();
	if(ok){
  		alert("form pode ser enviado")
	
		$("#frm_buscaTopo").attr("action","procura.asp");
		$("#frm_buscaTopo").submit();
	}
}


function apagadiv(obj_id) {

 $('#'+obj_id).css('display','none');
	
}


//verifica se já não está atualizando
var addingSelecao = false;
function addSelecao_5(tmp_codigo){
	//$$('addSelecao').checked = true;
	//$$('delSelecao').checked = true;
	if(!(addingSelecao)){
		addingSelecao = true;
		var a = new Ajax();
		a.onLoad = function() {
			with(parent){
				addingSelecao = false;
				$$('span_addSelecao').style.display = 'none';
				$$('span_delSelecao').style.display = 'block';
				alert("Item adicionado com sucesso.\nPara visualizar sua lista clique em minha seleção.");
			}
		}
		a.get('rotinas.asp?action=addSelecao2&tmp_codigo='+tmp_codigo);
	} else {
		alert('Por favor aguarde a conclusão do processo...');
	}
}

var dellingSelecao = false;
function delSelecao_5(tmp_codigo){
	$$('addSelecao').checked = false;
	$$('delSelecao').checked = false;
	if(!(dellingSelecao)){
		dellingSelecao = true;
		var a = new Ajax();
		a.onLoad = function() {
			with(parent){
				dellingSelecao = false;
				$$('span_addSelecao').style.display = 'block';
				$$('span_delSelecao').style.display = 'none';
			}
		}
		a.get('../../inc/routines/routines.php?action=delSelecao&tmp_codigo='+tmp_codigo);
	} else {
		alert('Por favor aguarde a conclusão do processo...');
	}
}



var pegando_conteudo = false;
function pega_conteudo(url){
	document.getElementById("loading").innerHTML = "<img src='imagens/loading.gif'>";
	if(!(pegando_conteudo)){
		pegando_conteudo = true;
		var a = new Ajax();
		a.onLoad = function() {
			with(parent){
				pegando_conteudo = false;
				document.getElementById("detalhesRecebe").innerHTML = this.html;
				document.getElementById("loading").innerHTML = ""
			}
		}
		a.get(url);
	} else {
		alert('Por favor aguarde a conclusão do processo...');
	}
}


function lightup(id)
{
elm = document.getElementById(id);
if(document.delr.id.checked == true)
{
elm.bgColor = "red";
}
else
{
elm.bgColor = "yellow";
}
}




var vinculando_imovel = false;
function gerenciar_imoveis_vinculados(obj_id,tmp_codigo,id_evento){
	document.getElementById("loading").innerHTML = "<img src='../imagens/loading.gif'>";

	if(document.check_imoveis[obj_id].checked == true)	{

	if(!(vinculando_imovel)){
		vinculando_imovel = true;
		var a = new Ajax();
		a.onLoad = function() {
		
			atualizar_lista_imoveis_vinculados(id_evento)			

			with(parent){
				
				vinculando_imovel = false;
				window.opener.document.getElementById("vincula_imoveis_sempre_mostra").style.display = 'none';
				window.opener.document.getElementById("vincula_imoveis").style.display = 'block';

				window.opener.document.getElementById("vincula_imoveis").innerHTML = this.html;
				document.getElementById("loading").innerHTML = ""
				//alert(this.html);
			}
		}
		a.get('../rotinas.asp?action=addimoveisatendimento&tmp_codigo='+tmp_codigo+'&id_evento='+id_evento+'&sidjs='+Math.random());
	} else {
		alert('Por favor aguarde a conclusão do processo...');
	}

	}

	else

	{

	if(!(vinculando_imovel)){
		vinculando_imovel = true;
		var a = new Ajax();
		a.onLoad = function() {

			atualizar_lista_imoveis_vinculados(id_evento)

			with(parent){
				vinculando_imovel = false;
				window.opener.document.getElementById("vincula_imoveis_sempre_mostra").style.display = 'none';
				window.opener.document.getElementById("vincula_imoveis").style.display = 'block';

				window.opener.document.getElementById("vincula_imoveis").innerHTML = this.html;
				document.getElementById("loading").innerHTML = ""
				//alert(this.html);
			}
		}
		a.get('../rotinas.asp?action=delimoveisatendimento&tmp_codigo='+tmp_codigo+'&id_evento='+id_evento+'&sidjs='+Math.random());
	} else {
		alert('Por favor aguarde a conclusão do processo...');
	}


	}




}


function atualizar_lista_imoveis_vinculados(id_evento) {
	var a = new Ajax();
	a.onLoad = function() {

		//alert(this.html);	
		window.opener.document.getElementById("vincula_imoveis").innerHTML = this.html;
	}
	a.get('../rotinas.asp?action=atualizar_lista_imoveis_vinculados&id_evento='+id_evento+'&sidjs='+Math.random());

}


function contaImoveisSelecao(obj_id){
	
	var a = new Ajax();
	a.onLoad = function() {

		//alert(this.html);	
		$$('num_imoveis_selecao').innerHTML = this.html;
	}
	a.get('rotinas.asp?action=contarImoveis&sidjs='+Math.random());
}

function addSelecao(obj_id, tmp_codigo){
	$$(obj_id).innerHTML = 'Add...';
		
	var a = new Ajax();
	a.onLoad = function() {

		//alert(this.html);
		contaImoveisSelecao()
		with(parent){
			$$(obj_id).innerHTML = '<a href="javascript:delSelecao(\'add'+tmp_codigo+'\',\''+tmp_codigo+'\');"><img src="imagens/ico_remover_favoritos_index.png" width="30" height="30" border="0" onmouseover="Tip(\'REMOVER IMÓVEL A SUA SELEÇÃO DE FAVORITOS\')" onmouseout="UnTip()"></a>';
		}
	}
	a.get('rotinas.asp?action=addSelecao&tmp_codigo='+tmp_codigo+'&sidjs='+Math.random());
}



function delSelecao(obj_id, tmp_codigo){
	$$(obj_id).innerHTML = 'Exc...';
	var a = new Ajax();
	a.onLoad = function() {

		//alert(this.html);	
		contaImoveisSelecao()
		with(parent){
			$$(obj_id).innerHTML = '<a href="javascript:addSelecao(\'add'+tmp_codigo+'\',\''+tmp_codigo+'\');"><img src="imagens/ico_add_favoritos_index.png" width="30" height="30" border="0" onmouseover="Tip(\'ADICIONAR IMÓVEL A SUA SELEÇÃO DE FAVORITOS\')" onmouseout="UnTip()"></a>';
		}
	}
	a.get('rotinas.asp?action=delSelecao&tmp_codigo='+tmp_codigo+'&sidjs='+Math.random());
}

function addSelecao2(obj_id, tmp_codigo){
	$$(obj_id).innerHTML = 'Adicionando...';
	var a = new Ajax();
	a.onLoad = function() {

		//alert(this.html);		
		contaImoveisSelecao()
		with(parent){
			$$(obj_id).innerHTML = '<a href="javascript:delSelecao2(\'add'+tmp_codigo+'\', \''+tmp_codigo+'\');">Excluir da minha seleção</a>';
		}
	}
	a.get('rotinas.asp?action=addSelecao2&tmp_codigo='+tmp_codigo+'&sidjs='+Math.random());
}

function delSelecao2(obj_id, tmp_codigo){
	$$(obj_id).innerHTML = 'Excluindo...';
	var a = new Ajax();
	a.onLoad = function() {
		//alert(this.html);	
		contaImoveisSelecao()
		with(parent){
			$$(obj_id).innerHTML = '<a href="javascript:addSelecao2(\'add'+tmp_codigo+'\', \''+tmp_codigo+'\');">Adicionar a minha seleção</a>';
		}
	}
	a.get('rotinas.asp?action=delSelecao2&tmp_codigo='+tmp_codigo+'&sidjs='+Math.random());
}


//============================================
// SCRIPT INTEGRACAO PORTAL WEBCASAS
//============================================


function add_webcasas(img_id,obj_id,tmp_codigo){
	$$(obj_id).innerHTML = 'Inte...';
	var a = new Ajax();
	a.onLoad = function() {

		var str = this.html;
		
		//alert(str)

		with(parent){
	
			if (str == "msg_1") {

				confirm_personalizado('Esse imóvel não pode ser integrado:<br><br>Você deve editar o imóvel primeiro', {tipo:'erro'}, function(){});
				$$(obj_id).innerHTML = '<a href="javascript:add_webcasas(\'img'+tmp_codigo+'\',\'add'+tmp_codigo+'\', \''+tmp_codigo+'\');">Integrar</a>';
				$$(img_id).innerHTML = '<img src="imagens/admin_integracao_no.png" width="20" height="20" border="0">';
	
			}
			else if (str == "msg_2") {
				
				confirm_personalizado('Sucesso:<br><br>Imóvel <b>INTEGRADO</b> com sucesso!', {titulo:'Transação Efetuada',tipo:'info'}, function(){});
				$$(img_id).innerHTML = '<img src="imagens/admin_integracao_ok.png" width="20" height="20" border="0">';
				$$(obj_id).innerHTML = '<a href="javascript:del_webcasas(\'img'+tmp_codigo+'\',\'add'+tmp_codigo+'\', \''+tmp_codigo+'\');">Excluir</a>';
	
			}
			else if (str == "msg_3") {

				confirm_personalizado('Esse imóvel não pode ser integrado:<br><br>Tente Novamente', {tipo:'erro'}, function(){});
				$$(obj_id).innerHTML = '<a href="javascript:add_webcasas(\'img'+tmp_codigo+'\',\'add'+tmp_codigo+'\', \''+tmp_codigo+'\');">Integrar</a>';
				$$(img_id).innerHTML = '<img src="imagens/admin_integracao_no.png" width="20" height="20" border="0">';
			}	

		}

		
	}
	a.get('rotinas.asp?action=add_webcasas&tmp_codigo='+tmp_codigo+'&sidjs='+Math.random());
}

function del_webcasas(img_id,obj_id, tmp_codigo){
	$$(obj_id).innerHTML = 'Excl...';
	var a = new Ajax();
	a.onLoad = function() {
		
		//alert(this.html);	
	
		var str = this.html;	

		with(parent){
	
			if (str == "msg_2") {
				
				confirm_personalizado('Sucesso:<br><br>Imóvel <b>EXCLUIDO</b> com sucesso!', {titulo:'Transação Efetuada',tipo:'info'}, function(){});
				$$(img_id).innerHTML = '<img src="imagens/admin_integracao_no.png" width="20" height="20" border="0">';
				$$(obj_id).innerHTML = '<a href="javascript:add_webcasas(\'img'+tmp_codigo+'\',\'add'+tmp_codigo+'\', \''+tmp_codigo+'\');">Integrar</a>';
	
			}
			else if (str == "msg_3") {

				confirm_personalizado('IMOVEL NAO EXCLUIDO:<br><br>Tente Novamente', {tipo:'erro'}, function(){});
				$$(obj_id).innerHTML = '<a href="javascript:del_webcasas(\'img'+tmp_codigo+'\',\'add'+tmp_codigo+'\', \''+tmp_codigo+'\');">Excluir</a>';
				$$(img_id).innerHTML = '<img src="imagens/admin_integracao_ok.png" width="20" height="20" border="0">';
			}	

		}
	
	}
	a.get('rotinas.asp?action=del_webcasas&tmp_codigo='+tmp_codigo+'&sidjs='+Math.random());
}


//============================================
// SCRIPT INTEGRACAO PORTAL WEBCASAS
//============================================


function pop(tmp_src,tmp_w,tmp_h){
	var p = new Popup(tmp_w,tmp_h);
	p.template = 1;
	p.overflow = 'hidden';
	p.position = 'fixed';
	p.open(tmp_src);
}


function openMe(obj){
	if(obj.className.indexOf('disable') == -1){
		//abre, não está desabilitado
		var inner = $$(obj.id+'_inner');
		if(inner.style.display == 'none'){
			inner.style.display = 'block';
		}
		else {
			 inner.style.display = 'none';
		}
	}
}

var total_bairros = 0

function loadBairro(tmp_idSelecionando,tmp_idcampo){
	// $('#'+tmp_idcampo).css('display','none');
	// var h = $('#'+tmp_idcampo).height();
	// $('#'+tmp_idcampo).height(0);
	// $('#'+tmp_idcampo).css('display','block');
	// $('#'+tmp_idcampo).animate({
								// height: h
							  // }, 'slow', function() {
	total_bairros = 0							// // Animation complete.
							  // });
	var campo = $$(tmp_idcampo);
	campo.className = 'select disable';

	var inner = $$(tmp_idcampo+'_inner');
	inner.innerHTML = '';
	inner.style.display = 'none';

	var total = $$(tmp_idcampo+'_total');
	total.value = 0;

	var a = new Ajax();
	a.onLoad = function(){
		//alert(this.html);
		var str = this.html;
		var arr = str.split("###");//separa por registro
		var arraux = new Array();

		if (str != '') {
			for(var x = 0; x < arr.length; x++){
				arraux = arr[x].split("...");

				addOptionBairro(tmp_idcampo,arraux[1],arraux[0]);
			}
			campo.className = 'select';
		} else {
			//addOptionBairro(tmp_idcampo,'Nenhum item encontrado','');
			$$("Bairro_CMB0_title").innerHTML = 'Encontrados 0 Bairros';
			$$("Bairro_CMB0_inner").hide();
			campo.className = 'select disable';
			
		}

		var total = $$(tmp_idcampo+'_total').value;

		$$("Bairro_CMB0_title").innerHTML = 'Buscar em ' +total+ ' Bairros';

		if(total < 7){
			inner.style.height = 'auto';
			} else {
			inner.style.width = '550px';
			inner.style.height = '300px';
		}
		inner.style.display = 'block';
	}

		a.get('rotinas.asp?action=carrega_bairros&cod_cidade='+tmp_idSelecionando);
}


function pesquisaSelecionaCombo(sItem) {
	if (sItem.className == "pesquisaPaginasComboItemOn") {
		sItem.className = "pesquisaPaginasComboItem";
		total_bairros = total_bairros - 1
		if (total_bairros < 2) {
		$$("Bairro_CMB0_title").innerHTML = 'Buscar em ' + total_bairros + ' Bairro';
		}else {
		$$("Bairro_CMB0_title").innerHTML = 'Buscar em ' + total_bairros + ' Bairros';
		}
		
	}else{
		sItem.className = "pesquisaPaginasComboItemOn";
		total_bairros = total_bairros + 1
		if (total_bairros < 2) {
		$$("Bairro_CMB0_title").innerHTML = 'Buscar em ' + total_bairros + ' Bairro';
		}else {
		$$("Bairro_CMB0_title").innerHTML = 'Buscar em ' + total_bairros + ' Bairros';
		}
		
	}
}


function addOptionBairro(id_select,tmp_text,tmp_value){
	var select = $$(id_select);
	var total = $$(id_select+'_total').value;
	total++;total--;

	var span = document.createElement('span');
	span.className = 'pesquisaPaginasComboItem';
	var id_span = 'id_option'+total
	span.id = 'id_option'+total

	span.innerHTML = '<input type="checkbox" id="'+id_select+total+'" name="'+select.attributes['name'].value+'" value="'+tmp_value+'"  onclick=\"javascript:pesquisaSelecionaCombo(document.getElementById(\''+id_span+'\'))\" /><label for="'+id_select+total+'" style="">'+tmp_text+'</label>';
	total++;
	$$(id_select+'_total').value = total;
	$$(id_select+'_inner').appendChild(span);
	
}




