
//===========================================================================================//
//                                                                                           //
//     Descrição........................: Funções JavaScript (AJAX).                         //
//                                                                                           //
//===========================================================================================//

//##################################################################################
//Script > 


function addbookmark(title, url){
	if (document.all) window.external.AddFavorite(url, title);
	else 
	if (window.sidebar) window.sidebar.addPanel(title, url, "") 
}

function addUrlBookmark(){
	
	document.getElementById('link_imo').value=document.URL;
	
}


function carregaJs(){
	
	//Carrega form busca
	defineBusca('a1', 'form_busca','');

}
function defineBusca(tipo, destino,campo) {
	
	var doc = document.getElementById(destino);
	document.getElementById('tipoBusca').value = tipo ;

	//Form busca, com os campos select
	if (tipo == 'a1' || tipo == 'b2' || tipo == 'c3' || tipo == 'd4') {
		
		//Executa a função que prrenche os combo box.
		formBusca(campo);
		
		return true;
	}
	
	//Form busca com o campo codigo do imovel
	if (tipo == 'e5') {
		
		return true;
		
	}
	
	return false;
}

function buscaRapida() {
	if ((document.getElementById('tipo').value == '') && 
		(document.getElementById('operacao').value == '') &&
		(document.getElementById('cidade').value == '') &&
		(document.getElementById('bairro').value == '')) {
		alert("- Selecione pelo menos um campo para buscar!");
		document.getElementById('tipo').focus();
	}
	else {
		BuscaRapida = new lsBuscaRapida(
			document.getElementById('idCliente'),
			document.getElementById('tipoBusca'),
			document.getElementById('tipo'),
			document.getElementById('operacao'),
			document.getElementById('cidade'),
			document.getElementById('bairro'),
			'http://www.imobrasil.com.br/templates/library/ajax/buscaRapida.php',
			'http://www.imobrasil.com.br/templates/xsl/tBuscaRapida.xsl',
			{});
	}
}

function buscaRapidaCod() {
	if ((document.getElementById('codigo').value == '')) {
		alert("- Digite o codigo do imóvel!");
		document.getElementById('codigo').focus();
	}
	else {
		BuscaRapidaCod = new lsBuscaRapidaCod(
			document.getElementById('idCliente'),
			document.getElementById('codigo'),
			'http://www.imobrasil.com.br/templates/library/ajax/buscaRapidaCod.php',
			'http://www.imobrasil.com.br/templates/xsl/tBuscaRapidaCod.xsl',
			{});
	}
}

//CARREGA IMG
function carregaImg(img, desc){

	document.getElementById('foto_grande_a').src=img;
	document.getElementById('descricao-foto').innerHTML=desc;

}
//FIM

//CARREGA IMG
function abas(div){
	switch (div) {
		
		case 'detalhes':
		document.getElementById('detalhes').className='selected';
		document.getElementById('aba_detalhes').style.display='block';
		document.getElementById('fotos').className='no_selected';
		document.getElementById('aba_fotos').style.display='none';
		document.getElementById('aba_localiza').style.display='block';
		document.getElementById('video').className='no_selected';
		document.getElementById('aba_video').style.display='none';
		break;
		
		case 'fotos':
		document.getElementById('detalhes').className='no_selected';
		document.getElementById('aba_detalhes').style.display='none';
		document.getElementById('fotos').className='selected';
		document.getElementById('aba_fotos').style.display='block';
		document.getElementById('aba_localiza').style.display='none';
		document.getElementById('video').className='no_selected';
		document.getElementById('aba_video').style.display='none';
		break;
		
		case 'localiza':
		document.getElementById('detalhes').className='no_selected';
		document.getElementById('aba_detalhes').style.display='none';
		document.getElementById('fotos').className='no_selected';
		document.getElementById('aba_fotos').style.display='none';
		document.getElementById('aba_localiza').style.display='block';
		document.getElementById('video').className='no_selected';
		document.getElementById('aba_video').style.display='none';
		break;
		
		case 'video':
		document.getElementById('detalhes').className='no_selected';
		document.getElementById('aba_detalhes').style.display='none';
		document.getElementById('fotos').className='no_selected';
		document.getElementById('aba_fotos').style.display='none';
		document.getElementById('aba_localiza').style.display='none';
		document.getElementById('video').className='selected';
		document.getElementById('aba_video').style.display='block';
		break;
	
	}

}
//FIM

//FUNÇÃO DE CHECAGEM DE EMAIL
function echeck(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   alert("E-Mail inválido. Por favor digite um e-mail válido. \n Ex.: nome@provedor.com.br")
	   return false
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert("E-Mail inválido. Por favor digite um e-mail válido. \n Ex.: nome@provedor.com.br")
	   return false
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		alert("E-Mail inválido. Por favor digite um e-mail válido. \n Ex.: nome@provedor.com.br")
		return false
	}
	 if (str.indexOf(at,(lat+1))!=-1){
		alert("E-Mail inválido. Por favor digite um e-mail válido. \n Ex.: nome@provedor.com.br")
		return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		alert("E-Mail inválido. Por favor digite um e-mail válido. \n Ex.: nome@provedor.com.br")
		return false
	 }
	 if (str.indexOf(dot,(lat+2))==-1){
		alert("E-Mail inválido. Por favor digite um e-mail válido. \n Ex.: nome@provedor.com.br")
		return false
	 }
	 if (str.indexOf(" ")!=-1){
		alert("E-Mail inválido. Por favor digite um e-mail válido. \n Ex.: nome@provedor.com.br")
		return false
	 }
	 return true					
}

//VALIDAÇÃO FALE CONOSNO
function vFale()
{
  if (document.fale.nome.value == "Seu nome") {
  		alert("- Digite o seu Nome!");
  		document.fale.nome.style.background = "#E1E1E1";
  		document.fale.nome.focus()
      return false;
  }
  var emailID=document.fale.email
	
	if ((emailID.value==null)||(emailID.value=="E-mail para contato")){
		alert("- Digite o seu E-Mail!")
		emailID.style.background = "#E1E1E1";
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value="nome@provedor.com.br"
		emailID.style.background = "#E1E1E1";
		emailID.focus()
		return false
	}
   if (document.fale.assunto.value == "Assunto da mensagem"){
      alert("- Digite o Assunto!");
	  document.fale.assunto.style.background = "#E1E1E1";
	  document.fale.assunto.focus()
      return false;
  }
  if (document.fale.msg.value == "Digite aqui sua mensagem."){
      alert("- Digite a mensagem!");
	  document.fale.msg.style.background = "#E1E1E1";
	  document.fale.msg.focus()
      return false;
  }
  return true;
}

//link externo
function init() {
	createExternalLinks();
}

function createExternalLinks() {
    if(document.getElementsByTagName) {
        var anchors = document.getElementsByTagName('a');
        for(var i=0; i<anchors.length; i++) {
            var anchor = anchors[i];
            if(anchor.getAttribute("href") && anchor.getAttribute('rel')=='externo') {
                anchor.target = '_blank';
                var title = anchor.title + ' (Este link abre uma nova janela)';
                anchor.title = title;
            }
        }
    }
}


//Fim


//##################################################################################
//Script > ComboBox tipo, operação, cidade e bairro

//INICIO
var HttpReq = null;

function formBusca(campo){
	
		//Url para pesquisa
	var url = 'templates/library/ajax/cboxBuscaImo.php';
	
	//Campos
	var id_clt = document.getElementById('idCliente').value;
	var tBusca = document.getElementById('tipoBusca').value;
	var lingua = document.getElementById('tipolingua').value;
	var tipo = document.getElementById('tipo').value;
	var op1 = document.getElementById('operacao');
	var operacao = op1.options[op1.selectedIndex].value;
	var operacao2 = op1.options[op1.selectedIndex].text;

		if(tipo==1){
	dest_combo1 = 'tipo';
	 dest_combo2 = 'operacao';
	dest_combo3 = 'cidade';
	 dest_combo4 = 'bairro';	
		}
		if(tipo>1){
	dest_combo1 = '';
	 dest_combo2 = 'operacao';
	dest_combo3 = 'cidade';
	 dest_combo4 = 'bairro';	
		}
		if(operacao2!='Operação' && operacao!=''){
	dest_combo1 = '';
	dest_combo2 = '';
	dest_combo3 = 'cidade';
	 dest_combo4 = 'bairro';	
		}
	
	//Monta caminho para busca.
	var randomnumber=Math.floor(Math.random()*9999);
    url = url + '?tbusca=' + tBusca + '&' + 'clt=' + id_clt+'&rdn='+randomnumber+'&lang='+lingua+'&tipo='+tipo+'&operacao='+operacao;
    
    
    //Verifica se o Browser suporta AJAX.
    if (document.getElementById) { 
        
    	if (window.XMLHttpRequest) {
    		
            HttpReq = new XMLHttpRequest();
            HttpReq.onreadystatechange = XMLHttpRequestImoBusca;
            HttpReq.open("GET", url, true);
            HttpReq.send(null);
            
        } else 
        if (window.ActiveXObject) {
        	
            HttpReq = new ActiveXObject("Microsoft.XMLHTTP");
            
            if (HttpReq) {
            	
                HttpReq.onreadystatechange = XMLHttpRequestImoBusca;
                HttpReq.open("GET", url, true);
                HttpReq.send();
            }
        }
    }
}

function XMLHttpRequestImoBusca() {
	
    if (HttpReq.readyState == 4 && HttpReq.status == 200){  //Verifica se o arquivo foi carregado com sucesso.
        
    	var result = HttpReq.responseXML;
    	
        var tipo = result.getElementsByTagName("tipo");
		var operacao = result.getElementsByTagName("operacao");
		var cidade = result.getElementsByTagName("cidade");
		var bairro = result.getElementsByTagName("bairro");
		
		if(dest_combo1!=''){
        document.getElementById(dest_combo1).innerHTML = ""; 
        for (var i = 0; i < tipo.length; i++) {
            new_opcao = optionImoBusca(tipo[i]);
            document.getElementById(dest_combo1).appendChild(new_opcao);
        }
		}
        
		if(dest_combo2!=''){
		document.getElementById(dest_combo2).innerHTML = "";
        for (var i = 0; i < operacao.length; i++) {
            new_opcao = optionImoBusca(operacao[i]);
            document.getElementById(dest_combo2).appendChild(new_opcao);
        }
		}
        if(dest_combo3!=''){
		document.getElementById(dest_combo3).innerHTML = "";
        for (var i = 0; i < cidade.length; i++) {
            new_opcao = optionImoBusca(cidade[i]);
            document.getElementById(dest_combo3).appendChild(new_opcao);
        }
		}
		if(dest_combo4!=''){
        document.getElementById(dest_combo4).innerHTML = "";
        for (var i = 0; i < bairro.length; i++) {
            new_opcao = optionImoBusca(bairro[i]);
            document.getElementById(dest_combo4).appendChild(new_opcao);
        }
		}
    }
}

function optionImoBusca(optBusca) { //Cria um novo elemento OPTION.
    
    var new_opcao = document.createElement("option"); //Cria um OPTION.
    var texto = document.createTextNode(optBusca.childNodes[0].data); //Cria um texto.
    new_opcao.setAttribute("value",optBusca.getAttribute("id")); //Adiciona o atributo de valor a nova opção.
    new_opcao.appendChild(texto); //Adiciona o texto a OPTION.
    return new_opcao; // Retorna a nova OPTION.
}
//FIM

//##################################################################################
//Script > ComboBox Estado e cidade para Busca Imo

//INICIO
var HttpReq = null;
var dest_combo = null;

function buscaBairro(){
	
	url = 'templates/library/ajax/cboxBuscaBairro.php';
	
	id_clt = 'idCliente';
	tbusca = 'tipoBusca';
	origem_combo = 'cidade'
    dest_combo = 'bairro';
    
    var tbusca = document.getElementById('tipoBusca').value;
    var id_clt = document.getElementById('idCliente').value;
	var tipo = document.getElementById('tipo').value;
	var operacao = document.getElementById('operacao').value;
    var indice = document.getElementById(origem_combo).selectedIndex;
    var sigla = document.getElementById(origem_combo).options[indice].getAttribute('value');
    url = url + '?id=' + sigla + '&tbusca=' + tbusca + '&id_clt=' + id_clt;
	if(tipo !=''){
	url = url +'&tipo='+tipo;
	}
	
	if(operacao !=''){
	url = url +'&operacao='+operacao;
	}
	
    if (document.getElementById) { //Verifica se o Browser suporta DHTML.
        
    	if (window.XMLHttpRequest) {
    		
            HttpReq = new XMLHttpRequest();
            HttpReq.onreadystatechange = XMLHttpRequestImo3;
            HttpReq.open("GET", url, true);
            HttpReq.send(null);
            
        } else 
        if (window.ActiveXObject) {
        	
            HttpReq = new ActiveXObject("Microsoft.XMLHTTP");
            
            if (HttpReq) {
            	
                HttpReq.onreadystatechange = XMLHttpRequestImo3;
                HttpReq.open("GET", url, true);
                HttpReq.send();
            }
        }
    }
}

function XMLHttpRequestImo3() {
	
    if (HttpReq.readyState == 4 && HttpReq.status == 200){  //Verifica se o arquivo foi carregado com sucesso.
        
    	var result = HttpReq.responseXML;
        var bairro = result.getElementsByTagName("bairro");
        document.getElementById(dest_combo).innerHTML = "";
        
        for (var i = 0; i < bairro.length; i++) {
            new_opcao = optionImo3(bairro[i]);
            document.getElementById(dest_combo).appendChild(new_opcao);
        }
    }
}

function optionImo3(opt3) { //Cria um novo elemento OPTION.
    
    var new_opcao = document.createElement("option"); //Cria um OPTION.
    var texto = document.createTextNode(opt3.childNodes[0].data); //Cria um texto.
    new_opcao.setAttribute("value",opt3.getAttribute("id")); //Adiciona o atributo de valor a nova opção.
    new_opcao.appendChild(texto); //Adiciona o texto a OPTION.
    return new_opcao; // Retorna a nova OPTION.
}
//FIM
