var aAbas       = new Array();  // Lista de abas do documento atual
var sAbaAtiva   = ""                 // Define qual é a aba ativa no momento
var ABA_ID      = 1
var ABA_BLOCO   = 2
var ABA_CAMPOS  = 3
        
 function defineAba( sId, sBloco ){
    var aAba  = new Array( ABA_CAMPOS );
     aAba[ ABA_ID    ]  = sId;
     aAba[ ABA_BLOCO ]  = sBloco;
     aAbas.push( aAba );
 }

        function defineAbaAtiva( sId ){
           trataCliqueAba( sId );
        }

        function trataMouseAba( oAba ){
           oAba.style.cursor  = "pointer";
        }

        function trataCliqueAba( sId ){
           for ( var iAba  = 0; iAba < aAbas.length; iAba++ ){
              var aAba  = aAbas[ iAba ];

              if   ( aAba[ ABA_ID ] == sId ) ativaAba( aAba );

              else                           inativaAba( aAba );
           }
        }

        function ativaAba( aAba ){
           var sAba       = aAba[ ABA_ID ];
           var oAba       = document.getElementById( sAba );
           mudaClasse( oAba, "ativo" ); // Esse comando chama a classe  css para fazer a troca

           var sBlocoAba  = aAba[ ABA_BLOCO ];
           var oBlocoAba  = document.getElementById( sBlocoAba );
           oBlocoAba.style.display  = "block";
        }

        function inativaAba( aAba ){
           var sAba       = aAba[ ABA_ID ];
           var oAba       = document.getElementById( sAba );
           mudaClasse( oAba, "inativo" ); // Esse comando chama a classe css para fazer a troca

           var sBlocoAba  = aAba[ ABA_BLOCO ];
           var oBlocoAba  = document.getElementById( sBlocoAba );
           oBlocoAba.style.display  = "none";
        }
        
        function mudaClasse( oObjeto, sClasse ){
           oObjeto.className  = sClasse;
        }

function percorre(obj) {
	var string = "";

	var child = obj.firstChild;
	

	
	while(child) {
		if (child.nodeType == 1) 
			if (child.tagName == "INPUT" && (child.type == "text" || child.type == "hidden") || child.type == "password" || child.tagName == "TEXTAREA") 
				string += (child.name + "=" + encodeURIComponent(child.value) + "&");
			else if (child.tagName == "INPUT" && (child.type == "radio" || child.type == "checkbox") && child.checked ) 
				string += (child.name + "=" + encodeURIComponent(child.value) + "&");
			else if (child.tagName == "SELECT")
				string += (child.name + "=" + encodeURIComponent(child.options[child.selectedIndex].value) + "&");
	
		
	
		string += percorre(child);
		child = child.nextSibling;
		}
	return string;		
	}

function envia (formulario, url) {
	formulario = document.getElementById(formulario);
	formulario.action = url;
	formulario.submit();	
	}
	
function limpar (obj) {
	document.getElementById(obj).innerHTML = "";
	}

function formatar(myfield, e, mask)
{
	var key;
	var keychar;
	var i = myfield.value.length;
	var saida = '#';
	var texto = mask.substring(i);
	
	if (window.event)
	   key = window.event.keyCode;
	else if (e)
	   key = e.which;
	else
	   return true;
	keychar = String.fromCharCode(key);
	
	if ((key==null) || (key==0) || (key==8) || 
		(key==9) || (key==13) || (key==27) )
	   return true;
	
	else if ((("0123456789.").indexOf(keychar) > -1))
	{
		if (texto.substring(0,1) != saida )
			myfield.value += texto.substring(0,1);
	   return true;
	}
	else
	   return false;
}

function Clear(valor, validos)
{
	// retira caracteres invalidos da string
	var result = "";
	var aux;
	for (var i=0; i < valor.length; i++)
	{
		aux = validos.indexOf(valor.substring(i, i+1));
		if (aux>=0)
		{
			result += aux;
		}
	}
	return result;
}

//Formata número tipo moeda usando o evento onKeyDown

function FormatarValor(campo,tammax,teclapres,decimal)
{
	var tecla = teclapres.keyCode;
	vr = Clear(campo.value,"0123456789");
	tam = vr.length;
	dec=decimal
	
	if (tam < tammax && tecla != 8)
	{
		tam = vr.length + 1 ;
	}
	
	if (tecla == 8 )
	{
		tam = tam - 1 ;
	}
	
	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
	{
		if ( tam <= dec )
		{
			campo.value = vr ;
		}
		
		if ( (tam > dec) && (tam <= 5) )
		{
			campo.value = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam ) ;
		}
		if ( (tam >= 6) && (tam <= 8) )
		{
			campo.value = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; 
		}
		if ( (tam >= 9) && (tam <= 11) )
		{
			campo.value = vr.substr( 0, tam - 8 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ;
		}
		if ( (tam >= 12) && (tam <= 14) )
		{
			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 - dec, tam ) ;
		}
		if ( (tam >= 15) && (tam <= 17) )
		{
			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 toggleTree(iconEl, childBlockEl)
{
	if (childBlockEl.visible())
	{
		iconEl.update("[+]");
		iconEl.setStyle(
		{
			"color": "#080"
		});
		childBlockEl.hide();
	}
	else
	{
		iconEl.update("[-]");
		iconEl.setStyle(
		{
			"color": "#999"
		});
		childBlockEl.show();
	}
}

function  window_popup(url,nome,altura,largura)
{
  var minimo = screen.width/4; // esta será a largura e a altura mínima
  var maximo = screen.height - 100; // esta será a largura e a altura máxima
  var w = ( ( ( (largura>minimo)? largura:minimo )<maximo )?largura:maximo);
  var h = ( ( ( ( altura>minimo )? altura:minimo )<maximo )?altura:maximo);
  var l = (screen.width/2) - w/2;    // valor para a posição na horizontal
  var t = (screen.height/2) - h/2;    // valor para a posição na vertical
  var argumentos = 'resizable=yes,copyhistory=yes,width='+w+',height='+h+',left='+l+',top='+t+',screenX='+l+',screenY='+t;
  var novajan = window.open(url,nome, argumentos);
}

function ConfirmLogoff()
{
	if (confirm('Tem certeza que deseja efetuar Logoff?'))
	{
		location.href='/index/logoff';
	}	
}

