var arrTabs = new Array('tab1_tp1','tab2_tp2','tab3_tp3','tab4_tp4');
var menuAberto = null;
var menuFecha = null;
var n3Aberto = null;
var n3Fecha = null;

document.observe("dom:loaded", function() {
  if($('tp2')){
    $('tp2','tp3','tp4').invoke('hide');
  }
  if($('loading'))$('loading').hide();
});
function trataTab(obj, pn){
	arrTabs.each(function(item) {
		var sp = item.split('_');
		//alert(sp[0] + ' - ' + sp[1]);
		if(sp[0]==obj.id){
			$(sp[1]).appear();
			//$(sp[1]).style.display='';
			$(sp[0]).addClassName('boton');
			$(sp[0]).removeClassName('botoff');
		} else {
			$(sp[1]).fade({ duration: 0.1, from: 1, to: 0 });
			//$(sp[1]).style.display='none';
			$(sp[0]).addClassName('botoff');
			$(sp[0]).removeClassName('boton');
		}
	});
}	
function showMeuFilho(codPai, qtdF){
  if(qtdF==0){
    if(menuAberto){
      hideMe(menuAberto)
    }
  } else {
    idFilho = 'n1-' + codPai;
    if(menuAberto != null && menuFecha!=menuAberto){
      menuFecha = menuAberto;
    }
    menuAberto = $(idFilho); 
    menuAberto.addClassName('c-mnu-n1-on');
    menuAberto.removeClassName('c-mnu-n1-off');
    //menuAberto.appear();
    //fazSombra(menuAberto,5);
    if(menuFecha && menuFecha != menuAberto){
      hideMe(menuFecha)
    }
  }
}
// trabalha o terceiro nivel
function showNivel3(codPai, qtdF){
  if(qtdF==0){
    if(n3Aberto){
      hideN3(n3Aberto)
    }
  } else {
    //alert('sobre ' + codPai + ' e com ' + qtdF + ' filhos');
    var idNeto = 'n3-' + codPai;
    
    if(n3Aberto != null && n3Fecha!=n3Aberto){
      n3Fecha = n3Aberto;
    }
    n3Aberto = $(idNeto);
   /*
   
       if($(idNeto)){
	 $(idNeto).addClassName('c-mnu-n2-on');
	 $(idNeto).removeClassName('c-mnu-n2-off');
       }
           
   */ 
    if(n3Aberto){
      n3Aberto.addClassName('c-mnu-n2-on');
      n3Aberto.removeClassName('c-mnu-n2-off');
    }
    
    
    if(n3Aberto && n3Fecha != n3Aberto){
      hideN3(n3Fecha)
    }
  } 
}

function showItemMenu(eu, s){
  if(s==1){
    eu.addClassName('item-menu-on');
    eu.removeClassName('item-menu-off');
  } else {
    eu.addClassName('item-menu-off');
    eu.removeClassName('item-menu-on');
  }
}

function hideMe(eu){
  idFilho = eu.id;
  //alert('Filho que eu vou esconder: ' + idFilho);
  $(idFilho).addClassName('c-mnu-n1-off');
  $(idFilho).removeClassName('c-mnu-n1-on');
}
function hideN3(eu){
  //alert(eu);
  if(eu==null) return;
  $(eu).addClassName('c-mnu-n2-off');
  $(eu).removeClassName('c-mnu-n2-on'); 
}
function gotoURL(loc){
  document.location = loc;
}
function checaIsNaN(oId){
	if(isNaN($(oId).value)){
		$(oId).value='';
		alert("Por favor, use apenas caracteres numéricos");
	}
}

function hideTit(idDiv, campo){
  if($(idDiv).style.display == 'none'){
    //alert('entrei' + ' - ' + $(idDiv) + ' - ' +$(campo).value);
    if($(campo).value==''){
      $(idDiv).style.display = 'block';
    }
  } else {
    $(idDiv).style.display = 'none';
  }
}
function fazSombra(dequem,els){
	// uma função que permite fazer sombra para qualquer DIV desde que passado o ID deste container
	// verifica se a página está montada
	document.observe("dom:loaded", function(){
		var prefixId = '-sombra'; // cria um sufixo para os ids
		var fundo = new Element('div',{'id':'div-fundo-el','class':'fundo'});// cria um fundo para encaixar exatamento sobre o elemento original
		if(!els) {var numElements = 4;} else {var numElements = els;} // permite definir numero de divs para compor a sombra
		var widthOrigem = $(dequem).getWidth();// pega a largura do elemento original
		var heightOrigem = $(dequem).getHeight(); // pega a altura do elemento original
		var ZI = 5; // z-index
		var zIndex_inc = 5; // incremento z-index
		var width_inc = 15; // incremento largura
		var height_inc = 15; // incremento altura
		var top_inc = height_inc; // incremento posição top
		var left_inc = width_inc; // incremento posição left
		var opac = .01; // opacidade moz e chrome
		var opac_IE = 2; // opacidade IE
		 // trata o fundo para encaixar certinho no container original
		fundo.style.position='absolute';
		fundo.style.width = $(dequem).getWidth() -2 + 'px';
		fundo.style.height = $(dequem).getHeight() -2 + 'px';
		fundo.style.top = 0 + 'px';
		fundo.style.left = 0 + 'px';
		fundo.style.backgroundColor = 'white';
		fundo.style.zIndex = 100;
		//itera para a montagem do número de divs a serem criados.
		for(var i=1;i<numElements;i++){
			//alert(width_inc);
			var sombra = new Element('div',{'id': 'sombra' + i});
			//alert(sombra.id);
			sombra.style.position='absolute';
			sombra.style.width = widthOrigem + width_inc + 'px';
			sombra.style.height = heightOrigem + height_inc + 'px';
			sombra.style.top = top_inc/2*-1 + 'px';
			sombra.style.left = left_inc/2*-1 + 'px';
			sombra.style.backgroundColor = '#000';
			sombra.style.opacity = opac;
			sombra.style.filter = 'alpha(opacity=' + opac_IE + ')';
			sombra.style.zIndex = ZI + zIndex_inc
			$(dequem).appendChild(sombra);
			// incrementa os incrementos...
			// primeiro será o original, o segundo sera original mais o inc
			width_inc = width_inc + 5;
			height_inc = height_inc + 5;
			top_inc = top_inc + 5;
			left_inc = left_inc + 5;
			zIndex_inc = zIndex_inc + 5;
		}
		$(dequem).appendChild(fundo);
	});
}
/*
* Máscara de CPF e RG na tela de resposta
*
*/
function mascara(valor, elemento){
	valor=valor.replace(/\D/g,"");
	if(elemento=='cpf'){
		valor=valor.replace(/(\d{3})(\d)/,"$1.$2");
		valor=valor.replace(/(\d{3})(\d)/,"$1.$2");
		valor=valor.replace(/(\d{3})(\d{1,2})$/,"$1-$2");
	}
	if(elemento=='rg'){
		valor=valor.replace(/(\d{1})(\d{1})$/,"$1-$2");
	}
	$(elemento).update(valor);
	return;
}
/**
 *	Função para formatar moeda. Replicada de finan.js
 */
function formataMoedaFinan(elementId){
	var elemento = $(elementId).value;
		
	elemento = elemento.replace(/\D/g,"");			//permite digitar apenas números
	elemento = elemento.replace(/(\d{2})$/,",$1"); 		//Coloca a virgula
	elemento = elemento.replace(/(\d+)(\d{3},\d{2})$/g,"$1.$2"); //Coloca o primeiro ponto
	var qtdLoop = (elemento.length-3)/3;
	var count = 0;
	while (qtdLoop > count)
	{
		count++;
		elemento=elemento.replace(/(\d+)(\d{3}.*)/,"$1.$2"); //Coloca o resto dos pontos
	}
	
	$(elementId).value = elemento;
}
function logoff(){
  var f=document.form_logoff
  if (f) f.submit();
}

function verifEmail(c){
	new Ajax.Request(arq,{
		method:'post',
		parameters:'format=js&class=user&method=vermail&l='+(logado?logado:'false')+'&acai='+acai+'&c='+c,
		onFailure: function(){alert('Não conseguimos validar seu e-mail neste momento. Tente novamente mais tarde, por favor.');}
		,onSuccess: function(r){}
	})
}
function shResize_deprecado(){
	var s=$('sombra');
	s.style.width=document.viewport.getWidth()+'px';
	s.style.height=document.viewport.getHeight()+'px';
}
function abreModal(conteudo){
	panelModal=$('janelaModal');
	panelModal.parentNode.removeChild(panelModal);
	var b=document.body;
	b.style.overflow='hidden';
	b.appendChild(panelModal);
	var s=$('sombra');
	//shResize();
	panelModal.show();
	//Event.observe(window, 'resize', shResize);
	if(conteudo){
	  $('contModal').update(conteudo);
	} else {
	  //alert("não veio");
	}
}
function fechaModal(conteudo){
	$('janelaModal').hide();
	document.body.style.overflow='auto';
	//Event.stopObserving(window, 'resize', shResize);
	$('contModal').update();
}
/* * Função de login no modal * */
function getFormLogin(cb){
	abreModal();
	var waiting=cb;
	Event.observe(window, 'fin:quitlogin',function(ev){var action=waiting;afterLogin(ev.memo.complete, action);});
    new Ajax.Updater('contModal',arq,{
	method:'post',
	parameters:'format=html&method=getFormLogin&class=simu',
	onFailure: function(){alert('Desculpe-nos! Não foi possível cadastrar seu usuário.');},
	onSuccess: function(){
	}
    });
}

