// FUNCAO PARA REDIRECIONAMENTO (MENU SUPERIOR)
// -> vai para um destino especificado - location
// v. 1.0 :: GabrielZ (gabrielz@gmail.com)
// 2007/03
//variavel global que e usada no banner dos imovel na home
var endeImagen = [];
var quantidade;
var slogan;
var frase;
var fraseAmostra;
var amostra ='';
var terminof1=true;
var terminof2=true;
var letreiroPara = false;
var letreiroMorto = true;
var id;
var gerenciadoLmorto = true;
var preCarreIma = false;
//------------------------------------------------------------------------------


//funãoo que cria ajax para a todas as versões de navegadores 
function criaAjax(){
      try{
        //Firefox, opera, 8.0, Safira
        ajax = new XMLHttpRequest();
      }catch(e){
        //Internet Explorer
        try{
          ajax = new ActiveXObject("Msxml2.XMLHTTP");
        }catch(e){
          try{
            ajax =  new ActiveXObject("Microsoft.XMLHTTP");
          }catch(e){
            alert("Seu browser n�o suporta AJAX");
            return false;
          }
        }
        
    }
  }  
//------------------------------------------------------------------------------

//função carrega as imagens.
function PreCarregamentoImagens(x){
      if(quantidade <= 0){
        return false;
      }
      if(x < quantidade){
        //alert("carregamento");
        criaAjax();
        ajax.open('GET',endeImagen[x],true);
        ajax.onreadystatechange=function(){
          
         if(ajax.readyState == 4){
           if(ajax.status == 200){
              x++;
              //alert(x);
              PreCarregamentoImagens(x);
              return true;
            }else{
              novoBanner();
            }
          }else{
            //carregando
           
          } 
        
        }
        
        ajax.send(null);
      }else{
         preCarreIma = true;
        return true;
      }
     
}
  
//------------------------------------------------------------------------------
/*
//fun��o que pedi ao servido um arquivo.
function novoBanner(){
      criaAjax();
      if(letreiroMorto == false){
        setTimeout("novoBanner()",100);
      }else{
        ajax.open('GET',"http://www.evolucaonegocios.com.br/test/pedidobanner.php",true);
        ajax.onreadystatechange=function(){
         if(ajax.readyState == 4){
         if(ajax.status == 200){
            if(ajax.responseXML){
              //alert("para");
             processaXML(ajax.responseXML);
            }else{
              novoBanner();
            }
          }else{
            novoBanner();
          }
          }else{
            //carregando
            document.getElementById("animacao").style.zIndex=0;
            document.getElementById("desc_area_atual").style.zIndex=0;
            document.getElementById("animacao2").style.zIndex=1; 
            document.getElementById("desc_area_atual2").style.zIndex=1;
          } 
        
        }
        
        ajax.send(null);
      }
      return false;
    }
//------------------------------------------------------------------------------

//fun��o que chamas as fun��es de imagen e letriro
function chamadaFuncao(){
  if( preCarreIma == true){
   // alert("chamando fun��es");
    letreiroPara = false;
    gerenciadoLmorto = false;
    fraseAmostra = slogan;
    letreiroMorto = false;
    letreiroInserindo(0);
    terminof1=false;
    //alert(letreiroPara);
    mudaImagen1(quantidade-1);
    terminof2=false;
    mudaImagen2(0,quantidade);
    document.getElementById("animacao").style.zIndex=1;
    document.getElementById("desc_area_atual").style.zIndex=1;
    document.getElementById("animacao2").style.zIndex=0; 
    document.getElementById("desc_area_atual2").style.zIndex=0;
  }else{
    setTimeout("chamadaFuncao()",500);
  }
}
//------------------------------------------------------------------------------
*/
//fun��o que recebe o dado em xml e trata ele.
function processaXML(xml){
  var imovel = xml.getElementsByTagName("imovel");
  id = imovel[0].getElementsByTagName("id")[0].firstChild.nodeValue;
  document.getElementById("linkB").href="http://www.evolucaonegocios.com.br/imoveis/imovel.php?p="+id;
  document.getElementById("linkB2").href="http://www.evolucaonegocios.com.br/imoveis/imovel.php?p="+id;
  quantidade = imovel[0].getElementsByTagName("quant")[0].firstChild.nodeValue;
  if(quantidade > 4){
    quantidade = 4;
  }
  slogan = imovel[0].getElementsByTagName("slogan")[0].firstChild.nodeValue;
  frase = imovel[0].getElementsByTagName("frase")[0].firstChild.nodeValue;
  //alert("id ="+id);
  //alert("quantidade ="+quant);
  //alert("slogan ="+slogan);
  //alert("frase ="+frase);
  imovel = xml.getElementsByTagName("imagen");
  if(imovel.length > 0){
    for(x=0;x<imovel.length;x++){
          var iten = imovel[x];
          endeImagen[x] = iten.getElementsByTagName("endere")[0].firstChild.nodeValue;
    }
    PreCarregamentoImagens(0);
    chamadaFuncao();
  }else{
   novoBanner();
  }
 
}
//------------------------------------------------------------------------------

//funcao 1 que vai muda imagen
function mudaImagen1(x){
  if(x>=0){
     document.getElementById("imagen1").src=endeImagen[x]; 
     x--;
     setTimeout("mudaImagen1("+x+")",7000);
  }else{
    terminof1 = true;
    clearTimeout(); 
  }
}
//------------------------------------------------------------------------------

function mudaImagen2(x,y){
  if(x < y){
    document.getElementById("imagen2").src=endeImagen[x];
    x++;
    setTimeout("mudaImagen2("+x+","+y+")",7000);
  }else{
    terminof2 = true;
    clearTimeout();
  }
}
//------------------------------------------------------------------------------

//funcao que vai gerenciar banner e letreiro
function gerenciaBanner(){
  if( (terminof1 == true) && ( terminof2 == true)){
    //alert("entro aqui");
    letreiroPara = true;
    novoBanner();
    setTimeout("gerenciaBanner()",20000);
  }else{
    setTimeout("gerenciaBanner()",20000);
  }
}
//------------------------------------------------------------------------------

//funcao que vai colocar frase ou slogan
function letreiroInserindo(x){
  if(letreiroPara == true ){
    document.getElementById("letrero").innerHTML ="";
    letreiroMorto = true;
    clearTimeout();
    return false;
  }else{
    
    if(fraseAmostra.length == amostra .length ){
      //alert("entro 2");
      var linkk = "<a href=http://www.evolucaonegocios.com.br/imoveis/imovel.php?p="+id+">"+amostra+"</a>";
      document.getElementById("letrero").innerHTML = linkk;
      if(x == 0){
        x = 1;
        amostra = '';
        fraseAmostra = frase;
      }else{
        x = 0;
        amostra = '';
        fraseAmostra = slogan;
      }
      setTimeout("letreiroInserindo("+x+")",3000);
    }else{
      var ta = parseFloat(amostra .length)+parseFloat(1);
      //alert(ta);
      amostra  = fraseAmostra.substring(0,ta);
      document.getElementById("letrero").innerHTML ="<strong>"+amostra+"</strong>";
      setTimeout("letreiroInserindo("+x+")",150);
    }
  }
}
//------------------------------------------------------------------------------

//funca que vai verifica se codigo de e valido
function verificaCodigo(){
      var cod = document.getElementById("codigo").value;
      if(cod == ""){
        alert("Codigo n&#259;o Informado");
        return false;
      }else{
        return true;
      }
      
}


function vai(destino){
	if(destino == 'imoveisvenda')
		document.location.href = base + '/imoveis/venda.php';
	else if(destino == 'imoveisaluguel')
		document.location.href = base + '/imoveis/aluguel.php';
	else
		document.location.href = base + '/' + destino;
}

// FUNCOES PARA EFEITOS DE 'HOVER' (MENU SUPERIOR)
// v. 1.0 :: GabrielZ (gabrielz@gmail.com)
// 2007/03

// -> variavel com as cores de 'over' e 'out'
var cores = new Array('','#ffffff');

// -> muda a cor do item atual e desmarca todos os outros
function foca(obj,link){
	var item = "document.getElementById('" + obj + "')";
  document.getElementById(obj).className="foca_class";
	
  eval(item).style.backgroundColor = cores[1];
	for(i=1;i<=9;i++){
		if('item' + i != obj)
			desfoca('item' + i);
	}
	
}
// -> reseta a cor do item passado para a padrao
function desfoca(obj){
	var item = "document.getElementById('" + obj + "')";
	eval(item).style.backgroundColor = cores[0];
	document.getElementById(obj).className="sem_focu";
}

// funcao de mascara para campos de moeda
// autor: Gabriel Fr�es (2005)
function mascaraMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, e){
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;
    if (whichCode == 13) return true;
    key = String.fromCharCode(whichCode); // Valor para o c�digo da Chave
    if (strCheck.indexOf(key) == -1) return false; // Chave inv�lida
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
    aux = '';
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) objTextBox.value = '';
    if (len == 1) objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
    if (len == 2) objTextBox.value = '0'+ SeparadorDecimal + aux;
    if (len > 2) {
        aux2 = '';
        for (j = 0, i = len - 3; i >= 0; i--) {
            if (j == 3) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);
            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;
        for (i = len2 - 1; i >= 0; i--)
        objTextBox.value += aux2.charAt(i);
        objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
    }
    return false;
}

// FUNCOES PARA FORMATACAO DE MOEDA
// v. 1.0 :: GabrielZ (gabrielz@gmail.com)
// 2007/05

// -> salta automaticamente, ao atingir o maxlength
function SaltaCampo(campo,prox,tammax,event){
	var tecla = event.which;
	vr = eval("document.form." + campo).value;
	tam = vr.length;
 	if (tecla != 0 && tecla != 9 && tecla != 16 ){
		if ( tam == tammax ){
			if ( eval("document.form." + prox) )
				eval("document.form." + prox).focus();
		}
	}
}
function FiltraCampo(campo){
	var s = "";
	var cp = "";
	vr = eval("document.form." + campo).value;
	tam = vr.length;
	for (i = 0; i < tam ; i++) {  
		if (vr.substring(i,i + 1) != "/" && vr.substring(i,i + 1) != "-" && vr.substring(i,i + 1) != "."  && vr.substring(i,i + 1) != "," ){
		 	s = s + vr.substring(i,i + 1);}
	}
	eval("document.form." + campo).value = s;
	return cp = eval("document.form." + campo).value
}
function FormataValor(campo) {
	eval("document.form." + campo).value = FiltraCampo(campo);
	vr = eval("document.form." + campo).value;
	tam = vr.length;
	if ( tam <= 2 )
 		eval("document.form." + campo).value = vr;
 	if ( (tam > 2) && (tam <= 5) )
 		eval("document.form." + campo).value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam );
 	if ( (tam >= 6) && (tam <= 8) )
 		eval("document.form." + campo).value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam );
 	if ( (tam >= 9) && (tam <= 11) )
 		eval("document.form." + campo).value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam );
 	if ( (tam >= 12) && (tam <= 14) )
 		eval("document.form." + campo).value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam );
 	if ( (tam >= 15) && (tam <= 17) )
 		eval("document.form." + campo).value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam );
}
function FormataValorSemCent(campo) {
	eval("document.form." + campo).value = FiltraCampo(campo);
	vr = eval("document.form." + campo).value;
	tam = vr.length;
	if ( tam <= 3 )
 		eval("document.form." + campo).value = vr;
 	if ( (tam > 3) && (tam <= 6) )
 		eval("document.form." + campo).value = vr.substr( 0, tam - 3 ) + '.' + vr.substr( tam - 3, tam );
 	if ( (tam >= 7) && (tam <= 9) )
 		eval("document.form." + campo).value = vr.substr( 0, tam - 6 ) + '.' + vr.substr( tam - 6, 3 ) + '.' + vr.substr( tam - 3, tam );
 	if ( (tam >= 10) && (tam <= 12) )
 		eval("document.form." + campo).value = vr.substr( 0, tam - 9 ) + '.' + vr.substr( tam - 9, 3 ) + '.' + vr.substr( tam - 6, 3 ) + '.' + vr.substr( tam - 3, tam );
 	if ( (tam >= 13) && (tam <= 15) )
 		eval("document.form." + campo).value = vr.substr( 0, tam - 12 ) + '.' + vr.substr( tam - 12, 3 ) + '.' + vr.substr( tam - 9, 3 ) + '.' + vr.substr( tam - 6, 3 ) + ',' + vr.substr( tam - 3, tam );
 	if ( (tam >= 17) && (tam <= 18) )
 		eval("document.form." + campo).value = vr.substr( 0, tam - 15 ) + '.' + vr.substr( tam - 15, 3 ) + '.' + vr.substr( tam - 12, 3 ) + '.' + vr.substr( tam - 9, 3 ) + '.' + vr.substr( tam - 6, 3 ) + ',' + vr.substr( tam - 3, tam );
}

