var Cods = '';



document.onmouseup = function (e) {
e = e || event
var target = e.target || e.srcElement
var box = document.getElementById("cal20")

//if (box.style.display =='block') {
if (   document.forms[0].cal_flag.value == '1'){
   do {
      if ((box == target)){
// Click occured inside the box, do nothing.
         return;
	     }
      target = target.parentNode
   //alert(target);  
      }
   while (target)
//alert('fora'); 
// Click was outside the box, hide it.
   if (   document.forms[0].cal_flag.value == '1') {
      //box.style.display ='none';
      InsereData();
	  }
   }
}

//#########################################################################################################   
// FECHA TODAS AS INSTANCIAS DO CALENDARIO
//#########################################################################################################   
function HideAll(){
   document.getElementById('c_dia').style.display ='none';
   document.getElementById('c_sem').style.display ='none';
   document.getElementById('c_tri').style.display ='none';
   document.getElementById('c_mes').style.display ='none';
//   document.getElementById('cal20').style.visibility ='hidden';
   document.getElementById('cal20').style.display ='none';
   document.forms[0].cal_flag.value = '0';
   }
   
//#########################################################################################################   
// CLICOU EM UM CAMPO DE DATAS    DT=1 , DATA_INICIAL  DT=2 DATA_FINAL
//#########################################################################################################   
function ProcessaCalendario(dt) {
   document.forms[0].dt_ini_fim.value = dt;
   document.forms[0].datasn[1].checked = true;
   if(VerificaPeriodoCal()) {
//      if(document.getElementById('cal20').style.visibility =='hidden') {
//         document.getElementById('cal20').style.visibility ='visible';
      if(document.getElementById('cal20').style.display =='none') {
         document.getElementById('cal20').style.display ='block';
         if(document.forms[0].tab.value == '20') {   //diario 
            document.getElementById('c_dia').style.display ='block';
            document.getElementById('c_sem').style.display ='none';
            document.getElementById('c_tri').style.display ='none';
            document.getElementById('c_mes').style.display ='block';
            document.getElementById('td1a').style.display ='block';
            document.getElementById('td1b').style.display ='block';
            document.getElementById('td2a').style.display ='block';
            document.getElementById('td2b').style.display ='block';
            document.getElementById('mta').style.display ='none';
            document.getElementById('ds').style.display ='block';
            document.getElementById('campo1').innerHTML ='Dia';
            document.getElementById('campo2').innerHTML ='Mês';
            }
         else if(document.forms[0].tab.value == '30') {    // semanal
            document.getElementById('c_dia').style.display ='none';
            document.getElementById('c_sem').style.display ='block';
            document.getElementById('c_tri').style.display ='none';
            document.getElementById('c_mes').style.display ='block';
            document.getElementById('td1a').style.display ='block';
            document.getElementById('td1b').style.display ='block';
            document.getElementById('td2a').style.display ='block';
            document.getElementById('td2b').style.display ='block';
            document.getElementById('mta').style.display ='none';
            document.getElementById('ds').style.display ='block';
            document.getElementById('campo1').innerHTML ='Quadrissemana';
            document.getElementById('campo2').innerHTML ='Mês';
            }
         else if(document.forms[0].tab.value == '12') {    //mensal
            document.getElementById('c_dia').style.display ='none';
            document.getElementById('c_sem').style.display ='none';
            document.getElementById('c_tri').style.display ='none';
            document.getElementById('c_mes').style.display ='block';
            document.getElementById('td1a').style.display ='none';
            document.getElementById('td1b').style.display ='block';
            document.getElementById('td2a').style.display ='none';
            document.getElementById('td2b').style.display ='block';
            document.getElementById('mta').style.display ='block';
            document.getElementById('ds').style.display ='none';
            document.getElementById('campo1').innerHTML ='';
            document.getElementById('campo2').innerHTML ='Mês';
            }
         else if(document.forms[0].tab.value == '4') {    //trimestral
            document.getElementById('c_dia').style.display ='none';
            document.getElementById('c_sem').style.display ='none';
            document.getElementById('c_tri').style.display ='block';
            document.getElementById('c_mes').style.display ='none';
            document.getElementById('td1a').style.display ='none';
            document.getElementById('td1b').style.display ='block';
            document.getElementById('td2a').style.display ='none';
            document.getElementById('td2b').style.display ='block';
            document.getElementById('mta').style.display ='block';
            document.getElementById('ds').style.display ='none';
            document.getElementById('campo1').innerHTML ='';
            document.getElementById('campo2').innerHTML ='Trimestre';
            }
         else if(document.forms[0].tab.value == '1') {   //anual 
            document.getElementById('c_dia').style.display ='none';
            document.getElementById('c_sem').style.display ='none';
            document.getElementById('c_tri').style.display ='none';
            document.getElementById('c_mes').style.display ='none';
            document.getElementById('td1a').style.display ='none';
            document.getElementById('td1b').style.display ='none';
            document.getElementById('td2a').style.display ='none';
            document.getElementById('td2b').style.display ='none';
            document.getElementById('mta').style.display ='block';
            document.getElementById('ds').style.display ='none';
            document.getElementById('campo1').innerHTML ='';
            document.getElementById('campo2').innerHTML ='';
            }
         if (dt == '1') {
            document.getElementById('titulo_cal').innerHTML ='<font face="Arial, Helvetica, sans-serif" size="2" color="#FFFFFF"><b>&nbsp;Data Inicial:</b></font>';
            }
         else if (dt == '2'){
            document.getElementById('titulo_cal').innerHTML ='<font face="Arial, Helvetica, sans-serif" size="2" color="#FFFFFF"><b>&nbsp;Data Final:</b></font>';
            }
         document.forms[0].cal_flag.value = '1';
			}
      else {
         HideAll();
         }
      }
   }

function EscreveCal() {
var data = '';
   if(document.forms[0].tab.value == '20') {   //diario 
      data =  document.forms[0].dia.value + '/' + document.forms[0].mes.value + '/' + document.forms[0].ano_ds.value; 
      }
   else if(document.forms[0].tab.value == '30') {    // semanal   
      data =  document.forms[0].semana.value + '/' + document.forms[0].mes.value + '/' + document.forms[0].ano_ds.value; 
      }
   else if(document.forms[0].tab.value == '12') {    //mensal
      data =  document.forms[0].mes.value + '/' + document.forms[0].ano_mta.value; 
      }
   else if(document.forms[0].tab.value == '4') {    //trimestral
      data =  document.forms[0].mes.value + '/' + document.forms[0].ano_mta.value; 
      }
   else if(document.forms[0].tab.value == '1') {   //anual 	  
      data =  document.forms[0].ano_mta.value; 
      }
	  
   if (document.forms[0].dt_ini_fim.value == '1') {
      document.forms[0].dt_ini.value = data;
	  }
   else if (document.forms[0].dt_ini_fim.value == '2') {	 
      document.forms[0].dt_fim.value = data;
      }
   }   
   

// ########################################################################
//VERIFICA SE AS SERIES SELECIONADAS PARA VISUALIZACAO OU EXPORTACAO
// TEM A MESMA PERIODICIDADE. m = 0 VISUALIZACAO, m = 1 EXPORTACAO
// ########################################################################
function VerificaPeriodoCal() {
   var cont = 0;
   var max = parseInt(document.forms[0].cont.value, 10);
   var per_aux = '';
   var Periodo_ok = true;
   for (i=0; i <document.forms[0].cont.value; i++) {
      if(eval('document.forms[0].chk' + i + '.checked')) {
         cont ++;
         if (per_aux == '') {
            per_aux = eval('document.forms[0].per' + i + '.value');
            document.forms[0].tab.value = per_aux;
            }
         else if(per_aux != eval('document.forms[0].per' + i + '.value')) {
            alert("Por favor selecione séries com a mesma periodicidade.");
            HideAll();
            Periodo_ok = false;
            break;
            }
         }
      }
   if(cont==0) {
      Periodo_ok = false;
      }
   return Periodo_ok;
   }
//##############################################################################################################
//INSERE A DATA DIARIA NO CAMPO DT_INI OU DT_FIM   
//##############################################################################################################
function InsereData() {
   if(document.forms[0].tab.value == '20') {    
      var data = document.forms[0].dia.value + '/' + document.forms[0].mes.value + '/' + document.forms[0].ano_ds.value;
      }
   else if(document.forms[0].tab.value == '30') {    
      var data = document.forms[0].semana.value + '/' + document.forms[0].mes.value + '/' + document.forms[0].ano_ds.value;
      }
   else if(document.forms[0].tab.value == '12') {    
      var data = document.forms[0].mes.value + '/' + document.forms[0].ano_mta.value;
      }
   else if(document.forms[0].tab.value == '4') {    
      var data = document.forms[0].tri.value + '/' + document.forms[0].ano_mta.value;
      }
   else if(document.forms[0].tab.value == '1') {    
      var data = document.forms[0].ano_mta.value;
      }
      
   if(document.forms[0].dt_ini_fim.value == '1') {
      if(VerificaPeriodoDatas()) {
         document.forms[0].dt_ini.value = data;
         document.forms[0].per_dt_ini.value = document.forms[0].tab.value;
         
         if(document.forms[0].per_dt_fim.value != document.forms[0].tab.value) {
            document.forms[0].dt_fim.value = '';
            document.forms[0].per_dt_fim.value = '';
            }
         }
      else {
         document.forms[0].dt_ini.value = '';
         }      
      }
   else if(document.forms[0].dt_ini_fim.value == '2') {
      if(VerificaPeriodoDatas()){
         document.forms[0].dt_fim.value = data;
         document.forms[0].per_dt_fim.value = document.forms[0].tab.value;

         if(document.forms[0].per_dt_ini.value != document.forms[0].tab.value) {
            document.forms[0].dt_ini.value = '';
            document.forms[0].per_dt_ini.value = '';
            }
         }
      else {
         document.forms[0].dt_fim.value = '';
         }      
      }
   HideAll();
   }   

//#########################################################################################################   
// VERIFICA SE AS SERIES SELECIONADAS POSSUEM A MESMA PERIODICIDADE
//#########################################################################################################   
function VerificaPeriodoDatas() {
   var max = parseInt(document.forms[0].cont.value, 10);
   var Periodo_ok = true;
   for (i=0; i <document.forms[0].cont.value; i++) {
      if(eval('document.forms[0].chk' + i + '.checked')) {
         per_aux = eval('document.forms[0].per' + i + '.value');
         break;
         }
      }
   if(((document.forms[0].per_dt_ini.value !='') && (document.forms[0].per_dt_fim.value !='')) && (document.forms[0].per_dt_ini.value != document.forms[0].per_dt_fim.value)) {
      alert("A DataInicial e a Data Final devem ter a mesma periodicidade.\nRefaça sua seleção.");
      document.forms[0].per_dt_ini.value = '';
      document.forms[0].per_dt_fim.value = '';
      document.forms[0].dt_ini.value = '';
      document.forms[0].dt_fim.value = '';
      document.forms[0].tab.value = '';
      Periodo_ok = false;
      }
   return Periodo_ok;
   }
   
//##############################################################################################################
// CHECA A VALIDADE DOS CAMPOS DE LOGIN
//##############################################################################################################
function ChecaLogin() {
   if (CamposLoginVazios(document.forms[0].login.value) == 1) {
      alert('O campo Login está vazio.');
      }
   else if (CamposLoginVazios(document.forms[0].senha.value) == 1) {
      alert('O campo Senha está vazio.');
      }
   else if (CamposLoginVazios(document.forms[0].login.value) == 2) {
      alert('O campo Login deve ter entre 4 e 12 aracteres alfanuméricos.');
      }
   else if (CamposLoginVazios(document.forms[0].senha.value) == 2) {
      alert('O campo Senha deve ter entre 4 e 12 aracteres alfanuméricos.');
      }
   else if ((CamposLoginVazios(document.forms[0].login.value) == 0) || (CamposLoginVazios(document.forms[0].senha.value) == 0)) {
      document.getElementById('login_form').style.display ='none';
      document.getElementById('tab_series').style.display ='block';
      document.forms[0].target = 'frame_TopPanel';
      document.forms[0].action="login.dll/in";
      document.forms[0].submit();
      }
   }

//#########################################################################################################   
// VERIFICA SE OS CAMPOS DO FORM DE LOGIN ESTAO VAZIOS
//#########################################################################################################   
function CamposLoginVazios(campo) {
   var str = campo;
   if (str.length <1) {
      return 1;
      }
   else if ((str.length <4) || (str.length > 12)){
      return 2;
      }
   else {
      return 0;
      }
   }

//#########################################################################################################   
// FUNCIONALIDADES AUXILIARES NA MANIPULAÇAO DE LAYERS
//#########################################################################################################   
function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_showHideLayers() { //v3.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; }
}
// ########################################################################
//EXIBE OU ESCONDE O CAMPO NOME DA LISTA EM RENOMEAR LISTA
// ########################################################################
function toggleTable(pstrTable){
   if(document.getElementById(pstrTable).style.display =="none"){
      document.getElementById(pstrTable).style.display="block";
      }
   else {
      document.getElementById(pstrTable).style.display="none";
      }
   }
// ########################################################################
//MARCA OU DESMARCA TODOS OS CHECKBOXES DAS TABELAS DE SERIES 
// ########################################################################
function MarcaDesmarcaTodas() {
   var ini = 0;
   var fim = parseInt(eval('document.forms[0].cont.value;'),10);
   for (i = ini; i <fim; i++) {
      if (document.forms[0].marcar.checked) {
         document.forms[0].elements['chk' + i].checked = true;
         if(i == 29) {
            alert('O sistema só permite selecionar no máximo 30 séries.');
            break;
            }
         }
      else {
         document.forms[0].elements['chk' + i].checked  = false;
         }
      }
   }
   
// ########################################################################
// IMPLEMENTA O RANGE SELECTION DOS CHECKBOXES
// ########################################################################
var lastchecked=null; 
function checkrange(e,el){
   var cont = 0; 
   e = e || window.event; 
   var i, shift = e.shiftKey, index = +el.name.substr(3); 
   if( shift && typeof lastchecked=='number' ){ 
      var dir = lastchecked<index ? 1 : -1; 
      for( i = lastchecked; i*dir < index*dir; i+=dir ){ 
         if( el.form.elements['chk'+i] ){ 
            el.form.elements['chk'+i].checked=el.checked; 
            cont++;
            } 
         if(cont == 30) {
            el.form.elements['chk'+index*dir].checked=false; 
            alert('O sistema só permite selecionar no máximo 30 séries.');
            break;
            } 
        } 
    } 
 lastchecked=index; 
} 
   
// ########################################################################
//ABRE NOVA JANELA DE BROWSER CENTRALIZADA
// ########################################################################
var win= null; 
function MM_openBrWindow(mypage,myname,w,h){ 
   var winl = (screen.width-w)/2; 
   var wint = (screen.height-h)/2; 
   settings='height='+h+',width='+w+',top='+wint+',left='+winl; 
   win=window.open(mypage,myname,settings) 
   if(parseInt(navigator.appVersion) >= 4){win.window.focus();} 
} 

// ########################################################################
//PROCESSA AS OPCOES DA COMBO OPCOES DE LISTA
// ########################################################################
function ProcessaOpcoes(n) {
   var opcao = document.forms[0].opcoes.value;
// renomear lista
   if (opcao == 'ren') {
      toggleTable('nome_lista');
// renomear lista
      if (n == '1') {
         if((!CampoVazio(document.forms[0].nome_lista.value)) || (document.forms[0].nome_lista.value == 'Entre com o novo nome')) {
            alert('Você não digitou o nome da Lista');
            toggleTable('nome_lista');
            document.forms[0].nome_lista.focus();
            }
        else {   
           if(document.getElementById('nome_lista').style.display =="block"){
              toggleTable('nome_lista');
              }
           document.forms[0].opcoes.value = 'ren';
           document.forms[0].pg.value = '';
           document.forms[0].action = '../tabela.dll/in4';
           document.forms[0].submit();
           }
        }
     }
// apagar lista
   else if (opcao == 'del') {
      if(document.getElementById('nome_lista').style.display =="block"){
         toggleTable('nome_lista');
         }
      if(confirm('Tem certeza que deseja Apagar esta lista?')) {
          document.forms[0].pg.value = '';
          document.forms[0].action = '../tabela.dll/in4';
          document.forms[0].submit();
          }
      }
// excluir series selecionadas da lista
   else if (opcao == 'exc') {
      if(document.getElementById('nome_lista').style.display =="block"){
         toggleTable('nome_lista');
         }
      var serie_select = SerieSelecionada();
      fim = parseInt(eval('document.forms[0].cont.value;'),10);
      if (serie_select == 0) {
         alert('Não existe nenhuma Série selecionada.');
         }
      else if(serie_select < fim) {
         if(confirm("Tem certeza de que deseja remover as Séries selecionadas desta lista?")) {
            document.forms[0].cod.value = Cods;
            document.forms[0].action = '../tabela.dll/in4';
            document.forms[0].submit();
            }
         }
      else if (serie_select == fim) {
         if(confirm("Tem certeza de que deseja remover todas as Séries desta lista?\nA Lista também será removida.")) {
            document.forms[0].cod.value = Cods;
            document.forms[0].action = '../tabela.dll/in4';
            document.forms[0].submit();
            }
         }
      }
// abrir lista
   else if (opcao != '') {
      if(document.getElementById('nome_lista').style.display =="block"){
         toggleTable('nome_lista');
         }
      document.forms[0].opcoes[0].selected = true;
	  document.forms[0].cod_lista.value = opcao;
	  document.forms[0].pg.value = '3';
//      document.forms[0].action = '../tabela.dll/in?cod_lista=' + opcao + '&pg=3';
      document.forms[0].action = '../tabela.dll/in';
      document.forms[0].submit();
      }
   }
// ########################################################################
//ABRE UMA LISTA DO USUARIO
// ########################################################################
function AbrirLista() {
   var opcao = document.forms[0].opcoes.value;
   if(!CampoVazio(opcao)) {
      alert('Você não selecionou nenhuma Lista.');
      }
   else {
       document.forms[0].opcoes[0].selected = true;
       document.forms[0].cod_lista.value = opcao;
	   document.forms[0].pg.value = '3';
       document.forms[0].action = '../tabela.dll/in';
       document.forms[0].submit();
       }
   }
// ########################################################################
//ACHA TODAS AS SERIES A SEREM ADICIONADAS EM UMA LISTA
// ########################################################################
function AdicionaNaLista() {
   if(ChecaCookie()) {
      if (parseInt(SerieSelecionada(), 10) < '1') {
         alert('Não existe nenhuma Série selecionada.');
         }
      else {
         document.forms[0].cod.value = Cods;
         document.forms[0].target = 'center';
         document.forms[0].action = '../tabela.dll/in2';
         document.forms[0].submit();
         }
      }
   }


function ChecaCookie() {
   var TemCookie = false;

   thisCookie = document.cookie.split("; ");
   for (i=0; i < thisCookie.length; i++) {
      if ('loginuser' == thisCookie[i].split("=")[0]) {
         if(thisCookie[i].split("=")[1] != null) {
            return true;
            }
         }
      } 
   if(!TemCookie) {
      if(document.getElementById('login_form').style.display =="none"){
         document.getElementById('login_form').style.display = "block";
         document.getElementById('tab_series').style.display = "none";
         document.forms[0].login.focus();
         }
      return false;
      }
   }

   
// ########################################################################
//VERIFICA O FORMATO DA DATA DE ACORDO COM A PERIODICIDADE
// ########################################################################
function ChecaData() {
   var per = '';
   var p  = document.forms[0].tab.value;
   for (i=0; i <document.forms[0].datasn.length; i++) {
      if (document.forms[0].datasn[i].checked) {
         per = document.forms[0].datasn[i].value;
         document.forms[0].dt.value = per;
         }
      }
   if (per == '2') {
      var dti = document.forms[0].dt_ini.value;
      var dtf = document.forms[0].dt_fim.value;
      if (!CampoVazio(dti)) {
         alert('O campo Data Inicial é obrigatório.');
         document.forms[0].dt_ini.focus();
         return false;
         }
      else if (!CampoVazio(dtf)) {
         alert('O campo Data Final é obrigatório.');
         document.forms[0].dt_fim.focus();
         return false;
         }

      else if (p == '20' || p == '30') { //diario , semanal
         if(p == '30'){
            var re = /^(01|02|03|04){1}[\/](01|02|03|04|05|06|07|08|09|10|11|12){1}[\/][12]{1}\d{3}$/;
            if(!re.test(dti)) {
               alert("Para periodicidade Quadrissemanal o campo\nData Inicial deve estar no formato: ss\/mm\/yyyy.\n\nObs.: ss = 01, 02, 03 ou 04");
               document.forms[0].dt_ini.focus();
               return false;
               }
            else if(!re.test(dtf)) {
               alert("Para periodicidade Quadrissemanal o campo\nData Final deve estar no formato: ss\/mm\/yyyy.\n\nObs.: ss = 01, 02, 03 ou 04");
               document.forms[0].dt_fim.focus();
               return false;
               }
            else {
               re = /\//;
               Arrdti = dti.split(re);
               Arrdtf = dtf.split(re);
               if( parseInt(Arrdti[2]+Arrdti[1]+Arrdti[0],10) > parseInt(Arrdtf[2]+Arrdtf[1]+Arrdtf[0], 10)) {
                  alert("A Data Inicial deve ser menor ou igual a Data Final.");
                  document.forms[0].dt_ini.focus();
                  return false;
                  }
               }
            }
         else if(p == '20'){
            var re = /^(01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31){1}[\/](01|02|03|04|05|06|07|08|09|10|11|12){1}[\/][12]{1}\d{3}$/;
            if(!re.test(document.forms[0].dt_ini.value)) {
               alert("Para periodicidade Diária o campo\nData Inicial deve estar no formato: dd\/mm\/yyyy.");
               document.forms[0].dt_ini.focus();
               return false;
               }
            else if(!re.test(document.forms[0].dt_fim.value)) {
               alert("Para periodicidade Diária o campo\nData Final deve estar no formato: dd\/mm\/yyyy.");
               document.forms[0].dt_fim.focus();
               return false;
               }
            else {
               re = /\//;
               Arrdti = dti.split(re);
               Arrdtf = dtf.split(re);
               if( parseInt(Arrdti[2]+Arrdti[1]+Arrdti[0],10) > parseInt(Arrdtf[2]+Arrdtf[1]+Arrdtf[0], 10)) {
                  alert("A Data Inicial deve ser menor ou igual a Data Final.");
                  document.forms[0].dt_ini.focus();
                  return false;
                  }
               }   
            }   
         }
      else if (p == '12'){ //mensal 
         var re = /^(01|02|03|04|05|06|07|08|09|10|11|12){1}[\/][12]{1}\d{3}$/;
         if(!re.test(document.forms[0].dt_ini.value)) {
            alert("Para periodicidade Mensal o campo\nData Inicial deve estar no formato: mm\/yyyy.");
            document.forms[0].dt_ini.focus();
            return false;
            }
         else if(!re.test(document.forms[0].dt_fim.value)) {
            alert("Para periodicidade Mensal o campo\nData Final deve estar no formato: mm\/yyyy.");
            document.forms[0].dt_fim.focus();
            return false;
            }
         else {
            re = /\//;
            Arrdti = dti.split(re);
            Arrdtf = dtf.split(re);
            if( parseInt(Arrdti[1]+Arrdti[0],10) > parseInt(Arrdtf[1]+Arrdtf[0], 10)) {
               alert("A Data Inicial deve ser menor ou igual a Data Final.");
               document.forms[0].dt_ini.focus();
               return false;
               }
            }
         }
      else if (p == '4'){ //trimestral 
         var re = /^(01|02|03|04){1}[\/][12]{1}\d{3}$/;
         if(!re.test(document.forms[0].dt_ini.value)) {
            alert("Para periodicidade Trimestral o campo\nData Inicial deve estar no formato: tt\/yyyy.\n\nObs.: tt = 01, 02, 03 ou 04");
            document.forms[0].dt_ini.focus();
            return false;
            }
         else if(!re.test(document.forms[0].dt_fim.value)) {
            alert("Para periodicidade Trimestral o campo\nData Final deve estar no formato: tt\/yyyy.\n\nObs.: tt = 01, 02, 03 ou 04");
            document.forms[0].dt_fim.focus();
            return false;
            }
         else {
            re = /\//;
            Arrdti = dti.split(re);
            Arrdtf = dtf.split(re);
            if( parseInt(Arrdti[1]+Arrdti[0],10) > parseInt(Arrdtf[1]+Arrdtf[0], 10)) {
               alert("A Data Inicial deve ser menor ou igual a Data Final.");
               document.forms[0].dt_ini.focus();
               return false;
               }
            }
         }
      else if (p == '1'){ //anual
         var re = /^[12]{1}\d{3}$/;
         if(!re.test(document.forms[0].dt_ini.value)) {
            alert("Para periodicidade Anual o campo\nData Inicial deve estar no formato: yyyy.");
            document.forms[0].dt_ini.focus();
            return false;
            }
         else if(!re.test(document.forms[0].dt_fim.value)) {
            alert("Para periodicidade Anual o campo\nData Final deve estar no formato: yyyy.");
            document.forms[0].dt_fim.focus();
            return false;
            }
         else {
            if( parseInt(dti,10) > parseInt(dtf, 10)) {
               alert("A Data Inicial deve ser menor ou igual a Data Final.");
               document.forms[0].dt_ini.focus();
               return false;
               }
            }
         }
      }   
      
   return true;
   }
// ########################################################################
//CHECA SE UM CAMPO ESTÁ VAZIO
// ########################################################################
function CampoVazio(campo) {
   var str = campo;
	if (str.length <1) {
	   return false;
		}
	else {
	   return true;
		}
   }
// ########################################################################
//VERIFICA SE AS SERIES SELECIONADAS PARA VISUALIZACAO OU EXPORTACAO
// TEM A MESMA PERIODICIDADE. m = 0 VISUALIZACAO, m = 1 EXPORTACAO
// ########################################################################
function VerificaPeriodo(m) {
   Cods = '';
   var modo = m;
   var max = parseInt(document.forms[0].cont.value, 10);
   var per_aux = '';
   var Periodo_OK = true;
   for (i=0; i <document.forms[0].cont.value; i++) {
      if(eval('document.forms[0].chk' + i + '.checked')) {
         if (per_aux == '') {
            per_aux = eval('document.forms[0].per' + i + '.value');
            document.forms[0].tab.value = per_aux;
            }
         else {
            if(per_aux != eval('document.forms[0].per' + i + '.value')) {
               alert("Por favor selecione séries com a mesma periodicidade.");
               Periodo_OK = false;
               break;
               }
            }
         }
      }
   if (Periodo_OK) {
      ProcessaSeries(modo, per_aux);
      }
   }
// ########################################################################
//PROCESSA A EXPORTACAO OU VISUALIZACAO DE DADOS DAS SERIES SELECIONADAS
// m = 0 VISUALIZAR, m = 1 EXPORTAR, m = 2 GERAR GRAFICO
// ########################################################################
function ProcessaSeries(m, per) {
   t_calc = document.forms[0].tipo_calc.value;
   fim = parseInt(eval('document.forms[0].cont.value;'),10);
   dt_sn = '1';
   var x = SerieSelecionada();
   
   if (document.forms[0].datasn[0].checked) {
      dt_sn = '1';
      }
   else if (document.forms[0].datasn[1].checked) {
      dt_sn = '2';
   }

   if(document.forms[0].pg.value == '-1') {
      if (ChecaData()) {
         if (x == 0) {
            alert('Não existe nenhuma Série selecionada.');
            }
         else if(x > 30) {
            alert("O sistema só permite a Visualização ou Exportação\nde no máximo 30 séries simultaneamente.");
            }
         else if((t_calc == '1') && (t_calc == '2') && (per != '4') && (per != '12')) {
               alert("Este cálculo é válido apenas para séries mensais ou trimestrais.");
            }
         else {
//            document.forms[0].cod.value = Cods;
//            document.forms[0].modo.value = m;
//            document.forms[0].target = 'center';
//            document.forms[0].action = '../tabela.dll/in3';
//            document.forms[0].submit();
//              document.location = '../tabela.dll/in3?res=' + screen.width + '&cod=' + Cods + '&modo=' + m + '&tab=' + document.forms[0].tab.value + '&datasn=' + dt_sn + '&dt_ini=' + document.forms[0].dt_ini.value + '&dt_fim=' + document.forms[0].dt_fim.value + '&pc=&dec=&pg=-1&tipo_calc=' + t_calc + '&exp_opcoes=' + document.forms[0].exp_opcoes.value + '&grafico_sn=' + document.forms[0].grafico_sn;   
              document.location = '../tabela.dll/in3?res=' + screen.height + '&cod=' + Cods + '&modo=' + m + '&tab=' + document.forms[0].tab.value + '&datasn=' + dt_sn + '&dt_ini=' + document.forms[0].dt_ini.value + '&dt_fim=' + document.forms[0].dt_fim.value + '&pc=&dec=&pg=-1&tipo_calc=' + t_calc + '&exp_opcoes=' + document.forms[0].exp_opcoes.value + '&grafico_sn=' + document.forms[0].grafico_sn;   
            }
         }
      }
   else if (ChecaCookie()) {
      if (ChecaData()) {
         if (x == 0) {
            alert('Não existe nenhuma Série selecionada.');
            }
         else if(x > 30) {
            alert("O sistema só permite a Visualização ou Exportação\nde no máximo 30 séries simultaneamente.");
            }
         else if(((t_calc == '1') || (t_calc == '2')) && ((per != '4') && (per != '12'))) {
               alert("Este cálculo é válido apenas para séries mensais ou trimestrais.");
            }
         else {
//            document.forms[0].cod.value = Cods;
//            document.forms[0].modo.value = m;
//            document.forms[0].target = 'center';
//            document.forms[0].action = '../tabela.dll/in3';
//            document.forms[0].submit();

//              document.location = '../tabela.dll/in3?res=' + screen.width + '&cod=' + Cods + '&modo=' + m + '&tab=' + document.forms[0].tab.value + '&datasn=' + dt_sn + '&dt_ini=' + document.forms[0].dt_ini.value + '&dt_fim=' + document.forms[0].dt_fim.value + '&pc=&dec=&pg=&tipo_calc=' + t_calc + '&exp_opcoes=' + document.forms[0].exp_opcoes.value + '&grafico_sn=' + document.forms[0].grafico_sn;   
              document.location = '../tabela.dll/in3?res=' + screen.height + '&cod=' + Cods + '&modo=' + m + '&tab=' + document.forms[0].tab.value + '&datasn=' + dt_sn + '&dt_ini=' + document.forms[0].dt_ini.value + '&dt_fim=' + document.forms[0].dt_fim.value + '&pc=&dec=&pg=&tipo_calc=' + t_calc + '&exp_opcoes=' + document.forms[0].exp_opcoes.value + '&grafico_sn=' + document.forms[0].grafico_sn;   
            }
         }
      }
   }
   
function SerieSelecionada() {
   var i;
   var serie_select = 0;
   ini = 0;
   fim = parseInt(eval('document.forms[0].cont.value;'),10);
   for (i = ini; i <fim; i++) {
      if (eval('document.forms[0].chk' + i + '.checked')) {
         serie_select ++
         Cods = Cods + eval('document.forms[0].chk' + i + '.value') + '|';
         }
      }
   return serie_select;
   }
   
//########################################################################   
// PROCESSA AS LISTAS DE TESTDRIVE
//########################################################################   
function TestDrive() {
   if(document.forms[0].testdrive.checked) {
      document.forms[0].testdrive.value = '1';
      }
   else {
      document.forms[0].testdrive.value = '0';
      }
   document.forms[0].target = 'center';
   document.forms[0].action = '../tabela.dll/td';
   document.forms[0].submit();
   }
  
  
// ########################################################################
//BLOQUEIA A EXIBICAO DE LINKS NA BARRA DE STATUS
// DE ACORDO COM A RESOLUCAO DA TELA
// ########################################################################
function BlockStatus() { 
  status = ' ';
  document.returnValue = true;
}
      
// ########################################################################
//ALTERA O POSICIONAMENTO DO LAYER DE TABELAS DE SERIES
// DE ACORDO COM A RESOLUCAO DA TELA
// ########################################################################
function SetFrameCenter(){
   if(screen.height==600){
//dimensoes da tabela
//height = tamanho externo da janela superior
      document.writeln('<div id="topicos" style="position:absolute; left: 0px; top: 8px; width: 100%; height: 66%; z-index: 8; background-color:#ffffff; layer-background-color: #f2fffe;  border: 0px solid #000000; overflow:auto;">');
      document.writeln('<div>');
      }
   else if(screen.height==720){
      document.writeln('<div id="topicos" style="position:absolute; left: 0px; top: 5px; width: 100%; height: 77%; z-index: 8; background-color:#ffffff; layer-background-color: #f2fffe;  border: 0px solid #000000; overflow:auto;">');
      document.writeln('<div>');
      }
   else if(screen.height==760){
      document.writeln('<div id="topicos" style="position:absolute; left: 0px; top: 5px; width: 100%; height: 77%; z-index: 8; background-color:#ffffff; layer-background-color: #f2fffe;  border: 0px solid #000000; overflow:auto;">');
      document.writeln('<div>');
      }
   else if(screen.height==768){
      document.writeln('<div id="topicos" style="position:absolute; left: 0px; top: 5px; width: 100%; height: 79%; z-index: 8; background-color:#ffffff; layer-background-color: #f2fffe;  border: 0px solid #000000; overflow:auto;">');
      document.writeln('<div>');
      }
   else if(screen.height==800){
    //top = altura do layer sup em relação ao frame.
   //height = tamanho vertical da janela scroll do layer sup
      document.writeln('<div id="topicos" style="position:absolute; left: 0px; top: 5px; width: 100%; height: 79%; z-index: 8; background-color:#ffffff; layer-background-color: #f2fffe;  border: 0px solid #000000; overflow:auto;">');
   //posicao da tabela na pagina
      document.writeln('<div>');
      }
   else if(screen.height==864){
    //top = altura do layer sup em relação ao frame.
   //height = tamanho vertical da janela scroll do layer sup
      document.writeln('<div id="topicos" style="position:absolute; left: 0px; top: 5px; width: 100%; height: 82%; z-index: 8; background-color:#ffffff; layer-background-color: #f2fffe;  border: 0px solid #000000; overflow:auto;">');
   //posicao da tabela na pagina
      document.writeln('<div>');
      }
   else if(screen.height==900){
   //top = altura do layer sup em relação ao frame.
   //height = tamanho vertical da janela scroll do layer sup
      document.writeln('<div id="topicos" style="position:relative; left: 0px; top: 5px; width: 100%; height: 83%; z-index: 8; background-color:#ffffff; layer-background-color: #f2fffe;  border: 0px solid #000000; overflow:auto;">');
   //posicao da tabela na pagina
      document.writeln('<div>');
      }
   else if(screen.height==960){
   //top = altura do layer sup em relação ao frame.
   //height = tamanho vertical da janela scroll do layer sup
      document.writeln('<div id="topicos" style="position:relative; left: 0px; top: 5px; width: 100%; height: 84%; z-index: 8; background-color:#ffffff; layer-background-color: #f2fffe;  border: 0px solid #000000; overflow:auto;">');
   //posicao da tabela na pagina
      document.writeln('<div>');
      }
   else if(screen.height==1024){
   //top = altura do layer sup em relação ao frame.
   //height = tamanho vertical da janela scroll do layer sup
      document.writeln('<div id="topicos" style="position:relative; left: 0px; top: 5px; width: 100%; height: 85%; z-index: 8; background-color:#ffffff; layer-background-color: #f2fffe;  border: 0px solid #000000; overflow:auto;">');
   //posicao da tabela na pagina
      document.writeln('<div>');
      }
   else if(screen.height==1050){
   //top = altura do layer sup em relação ao frame.
   //height = tamanho vertical da janela scroll do layer sup
      document.writeln('<div id="topicos" style="position:relative; left: 0px; top: 5px; width: 100%; height: 86%; z-index: 8; background-color:#ffffff; layer-background-color: #f2fffe;  border: 0px solid #000000; overflow:auto;">');
   //posicao da tabela na pagina
      document.writeln('<div>');
      }
   else if(screen.height==1080){
   //top = altura do layer sup em relação ao frame.
   //height = tamanho vertical da janela scroll do layer sup
      document.writeln('<div id="topicos" style="position:relative; left: 0px; top: 5px; width: 100%; height: 87%; z-index: 8; background-color:#ffffff; layer-background-color: #f2fffe;  border: 0px solid #000000; overflow:auto;">');
   //posicao da tabela na pagina
      document.writeln('<div>');
      }
   else if(screen.height==1200){
   //top = altura do layer sup em relação ao frame.
   //height = tamanho vertical da janela scroll do layer sup
      document.writeln('<div id="topicos" style="position:relative; left: 0px; top: 5px; width: 100%; height: 88%; z-index: 8; background-color:#ffffff; layer-background-color: #f2fffe;  border: 0px solid #000000; overflow:auto;">');
   //posicao da tabela na pagina
      document.writeln('<div>');
      }
   else if(screen.height==1392){
   //top = altura do layer sup em relação ao frame.
   //height = tamanho vertical da janela scroll do layer sup
      document.writeln('<div id="topicos" style="position:relative; left: 0px; top: 5px; width: 100%; height: 90%; z-index: 8; background-color:#ffffff; layer-background-color: #f2fffe;  border: 0px solid #000000; overflow:auto;">');
   //posicao da tabela na pagina
      document.writeln('<div>');
      }
   else if(screen.height==1440){
   //top = altura do layer sup em relação ao frame.
   //height = tamanho vertical da janela scroll do layer sup
      document.writeln('<div id="topicos" style="position:relative; left: 0px; top: 5px; width: 100%; height: 90%; z-index: 8; background-color:#ffffff; layer-background-color: #f2fffe;  border: 0px solid #000000; overflow:auto;">');
   //posicao da tabela na pagina
      document.writeln('<div>');
      }
   else if(screen.height==1536){
   //top = altura do layer sup em relação ao frame.
   //height = tamanho vertical da janela scroll do layer sup
      document.writeln('<div id="topicos" style="position:relative; left: 0px; top: 5px; width: 100%; height: 91%; z-index: 8; background-color:#ffffff; layer-background-color: #f2fffe;  border: 0px solid #000000; overflow:auto;">');
   //posicao da tabela na pagina
      document.writeln('<div>');
      }
   }
// ########################################################################
//ALTERA O POSICIONAMENTO DO LAYER DE BOTOES DE SERIES
// DE ACORDO COM A RESOLUCAO DA TELA
// ########################################################################
function SetFrameBottom(){
   if(screen.height==600){
   //dimensoes da tabela
      document.writeln('<div id="botoes" style="position:absolute; left: 0px; top: 69%; width: 100%; height: 20%; z-index: 8; background-color:#ffffff; layer-background-color: #f2fffe;  border: 0px solid #000000; overflow:noscroll">');
   //posicao da tabela na pagina
      document.writeln('<div style="position:relative; left: 0px; top: 3px; width: 100%; height: 20%; z-index: 8;">');
      }
   else if(screen.height==720){
//dimensoes da tabela
// top= altura da janela inferior (externo) | encontro da divisão entre os layers sup e inf.
      document.writeln('<div id="botoes" style="position:absolute; left: 0px; top: 79%; width: 100%; height: 15%; z-index: 8; background-color:#ffffff; layer-background-color: #f2fffe;  border: 0px solid #000000; overflow:noscroll">');
   //posicao da tabela na pagina
  //top= altura da barra branca acima da linha preta
      document.writeln('<div style="position:relative; left: 0px; top: 4%; width: 100%; height: 10%; z-index: 8;">');
      }
   else if(screen.height==760){
//dimensoes da tabela
// top= altura da janela inferior (externo) | encontro da divisão entre os layers sup e inf.
      document.writeln('<div id="botoes" style="position:absolute; left: 0px; top: 79%; width: 100%; height: 15%; z-index: 8; background-color:#ffffff; layer-background-color: #f2fffe;  border: 0px solid #000000; overflow:noscroll">');
   //posicao da tabela na pagina
  //top= altura da barra branca acima da linha preta
      document.writeln('<div style="position:relative; left: 0px; top: 4%; width: 100%; height: 10%; z-index: 8;">');
      }
   else if(screen.height==768){
//dimensoes da tabela
// top= altura da janela inferior (externo) | encontro da divisão entre os layers sup e inf.
      document.writeln('<div id="botoes" style="position:absolute; left: 0px; top: 81%; width: 100%; height: 15%; z-index: 8; background-color:#ffffff; layer-background-color: #f2fffe;  border: 0px solid #000000; overflow:noscroll">');
   //posicao da tabela na pagina
  //top= altura da barra branca acima da linha preta
      document.writeln('<div style="position:relative; left: 0px; top: 4%; width: 100%; height: 10%; z-index: 8;">');
      }
   else if(screen.height==800){
   //top = 
   //height = altura do layer inf
      document.writeln('<div id="botoes" style="position:absolute; left: 0px; top: 82%; width: 100%; height: 12%; z-index: 8; background-color:#ffffff; layer-background-color: #f2fffe;  border: 0px solid #000000; overflow:noscroll">');
   //top = posição da borda superior do layer inf.
   //height = altura do layer inf
      document.writeln('<div style="position:relative; left: 0px; top: 0%; width: 100%; height: 10%; z-index: 8;">');
      }
   else if(screen.height==864){
   //top = 
   //height = altura do layer inf
      document.writeln('<div id="botoes" style="position:absolute; left: 0px; top: 84%; width: 100%; height: 11%; z-index: 8; background-color:#ffffff; layer-background-color: #f2fffe;  border: 0px solid #000000; overflow:noscroll">');
   //top = posição da borda superior do layer inf.
   //height = altura do layer inf
      document.writeln('<div style="position:relative; left: 0px; top: 0%; width: 100%; height: 10%; z-index: 8;">');
      }
   else if(screen.height==900){
   //top = 
   //height = altura do layer inf
      document.writeln('<div id="botoes" style="position:absolute; left: 0px; top: 84%; width: 100%; height: 10%; z-index: 8; background-color:#ffffff; layer-background-color: #f2fffe;  border: 0px solid #000000; overflow:noscroll">');
   //top = posição da borda superior do layer inf.
   //height = altura do layer inf
      document.writeln('<div style="position:relative; left: 0px; top: 5%; width: 100%; height: 8%; z-index: 8;">');
      }
   else if(screen.height==960){
   //top = 
   //height = altura do layer inf
      document.writeln('<div id="botoes" style="position:absolute; left: 0px; top: 85%; width: 100%; height: 10%; z-index: 8; background-color:#ffffff; layer-background-color: #f2fffe;  border: 0px solid #000000; overflow:noscroll">');
   //top = posição da borda superior do layer inf.
   //height = altura do layer inf
      document.writeln('<div style="position:relative; left: 0px; top: 5%; width: 100%; height: 8%; z-index: 8;">');
      }
   else if(screen.height==1024){
   //top = 
   //height = altura do layer inf
      document.writeln('<div id="botoes" style="position:absolute; left: 0px; top: 86%; width: 100%; height: 7%; z-index: 8; background-color:#ffffff; layer-background-color: #f2fffe;  border: 0px solid #000000; overflow:noscroll">');
   //top = posição da borda superior do layer inf.
   //height = altura do layer inf
      document.writeln('<div style="position:relative; left: 0px; top: 5%; width: 100%; height: 8%; z-index: 8;">');
      }
   else if(screen.height==1050){
   //top = 
   //height = altura do layer inf
      document.writeln('<div id="botoes" style="position:absolute; left: 0px; top: 87%; width: 100%; height: 7%; z-index: 8; background-color:#ffffff; layer-background-color: #f2fffe;  border: 0px solid #000000; overflow:noscroll">');
   //top = posição da borda superior do layer inf.
   //height = altura do layer inf
      document.writeln('<div style="position:relative; left: 0px; top: 5%; width: 100%; height: 8%; z-index: 8;">');
      }
   else if(screen.height==1080){
   //top = 
   //height = altura do layer inf
      document.writeln('<div id="botoes" style="position:absolute; left: 0px; top: 88%; width: 100%; height: 7%; z-index: 8; background-color:#ffffff; layer-background-color: #f2fffe;  border: 0px solid #000000; overflow:noscroll">');
   //top = posição da borda superior do layer inf.
   //height = altura do layer inf
      document.writeln('<div style="position:relative; left: 0px; top: 5%; width: 100%; height: 8%; z-index: 8;">');
      }
   else if(screen.height==1200){
   //top = 
   //height = altura do layer inf
      document.writeln('<div id="botoes" style="position:absolute; left: 0px; top: 89%; width: 100%; height: 7%; z-index: 8; background-color:#ffffff; layer-background-color: #f2fffe;  border: 0px solid #000000; overflow:noscroll">');
   //top = posição da borda superior do layer inf.
   //height = altura do layer inf
      document.writeln('<div style="position:relative; left: 0px; top: 5%; width: 100%; height: 8%; z-index: 8;">');
      }
   else if(screen.height==1392){
   //top = 
   //height = altura do layer inf
      document.writeln('<div id="botoes" style="position:absolute; left: 0px; top: 91%; width: 100%; height: 7%; z-index: 8; background-color:#ffffff; layer-background-color: #f2fffe;  border: 0px solid #000000; overflow:noscroll">');
   //top = posição da borda superior do layer inf.
   //height = altura do layer inf
      document.writeln('<div style="position:relative; left: 0px; top: 5%; width: 100%; height: 8%; z-index: 8;">');
      }
   else if(screen.height==1440){
   //top = 
   //height = altura do layer inf
      document.writeln('<div id="botoes" style="position:absolute; left: 0px; top: 91%; width: 100%; height: 7%; z-index: 8; background-color:#ffffff; layer-background-color: #f2fffe;  border: 0px solid #000000; overflow:noscroll">');
   //top = posição da borda superior do layer inf.
   //height = altura do layer inf
      document.writeln('<div style="position:relative; left: 0px; top: 5%; width: 100%; height: 8%; z-index: 8;">');
      }
   else if(screen.height==1536){
   //top = 
   //height = altura do layer inf
      document.writeln('<div id="botoes" style="position:absolute; left: 0px; top: 92%; width: 100%; height: 7%; z-index: 8; background-color:#ffffff; layer-background-color: #f2fffe;  border: 0px solid #000000; overflow:noscroll">');
   //top = posição da borda superior do layer inf.
   //height = altura do layer inf
      document.writeln('<div style="position:relative; left: 0px; top: 5%; width: 100%; height: 8%; z-index: 8;">');
      }
   }

