
// 28/11/2002 validação de campos de entrada
//   /  /     
function oMask( pThis, pType, pSpecial, pMin, pMax )
{
  var sPasteCopy = '';   
  var sNull      = '';   

  var sMaskNumber     = '0123456789';
  var sMaskLetter     = 'abcdefghijklmnopqrstuvxywz\xE0\xE1\xE2\xE3\xE7\xE9\xEA\xED\xF3\xF4\xF5\xDA';
  var sMaskLetterCaps = 'ABCDEFGHIJKLMNOPQRSTUVXYWZ\xC0\xC1\xC2\xC3\xC7\xC9\xCA\xCD\xD3\xD4\xD5\xFA';
  var sMaskSpecial    = '!@#$%&()<>[]{}_=\/*-+|;:,.';
  var sMaskSpace      = ' ';
  var sMessage = '';
  sList = pSpecial;

  if (pType.indexOf('A')      != -1) 
    pThis.value = pThis.value.toUpperCase();

  if (pType.indexOf('9')      != -1)   { sList+=sMaskNumber;       }  
  if (pType.indexOf('m')      != -1)   { sList+=sMaskNumber+',-+'; } 
  if (pType.indexOf('d')      != -1)   { sList+=sMaskNumber+'/';   }
  if (pType.indexOf('h')      != -1)   { sList+=sMaskNumber+':';   }
  if (pType.indexOf('a')      != -1)   { sList+=sMaskLetter+sMaskLetterCaps;  }
  if (pType.indexOf('A')      != -1)   { sList+=sMaskLetterCaps;  }
  if (pType.indexOf('s')      != -1)   { sList+=sMaskSpecial;      }
  if (pType.indexOf('p')      != -1)   { sList+='\´';      }


  if ( pType.indexOf('a') != -1 && pType.indexOf('b') == -1 ) sList+=sMaskSpace; 
  if ( pType.indexOf('A') != -1 && pType.indexOf('b') == -1 ) sList+=sMaskSpace; 
 

  if ( pType.indexOf('e') == -1 )  //email
  {
    for (i=0;i<=pThis.value.length;i++)
    {
      cChar = pThis.value.charAt(i); 
      if (sList.indexOf(cChar) == -1)
      {
        if( cChar == ' ' ) 
        {
          sMessage = 'ADVERTÊNCIA: Espaço em branco não é permitido neste campo.';
        }
        else
        { 
          sMessage = 'ADVERTÊNCIA: Caracter "'+cChar+'" não é permitido.';
        }
      }
    }
  }

  if (pType.indexOf('m')  != -1) 
  { 
    if ( pThis.value.indexOf('-') >= 0 )
    {
      if ( pThis.value.substr(0,1) != '-' )
        sMessage = 'Valor incorreto.';
    }
    if ( pThis.value.indexOf('+') >= 0 )
    {
      if ( pThis.value.substr(0,1) != '+' )
        sMessage = 'Valor incorreto.';
    }

    iV = 0;
    iP = 0;
    iM = 0;
    for ( i=0; i<pThis.value.length; i++ ) 
    {  
      if ( pThis.value.substr(i,1) == ',' ) iV++; 
      if ( pThis.value.substr(i,1) == '+' ) iP++; 
      if ( pThis.value.substr(i	,1) == '-' ) iM++; 
    }
    if ( iV>1 || iP>1 || iM>1 ) 
      sMessage = 'Valor incorreto.';
 
  }
 
  if( pMin != pMax && pThis.value != '' ) 
  { 
    if( pThis.value < pMin || pThis.value > pMax )
    {
      sMessage = 'ADVERTÊNCIA: O conteúdo deste campo não pode ser menor que "'+pMin+'" e nem maior que "'+pMax+'".'; 
    }
  } 
 
  if (pType.indexOf('e')  != -1 && pThis.value != '' )  //email
  { 
    if( pThis.value.search(/^\w+((-\w+)|(\.\w+))*\@[a-z0-9]+((\.|-)[a-z0-9]+)*\.[a-z0-9]+$/) == -1 )
    { 
      sMessage ='ADVERTÊNCIA: Digite um e-mail válido.';
    } 
  }

  if (pType.indexOf('fullName')  != -1 && pThis.value != '' )
  { 
    if (pThis.value.indexOf(' ') == -1)
    { 
      sMessage ='ADVERTÊNCIA: Digite o nome completo.';
    } 
  }

 
  if (pType.indexOf('d')  != -1 && pThis.value != '' )
  { 
    if ( pThis.value.substr(2,1).indexOf('/') == -1 )
      pThis.value = pThis.value.substr(0,2) + '/' + pThis.value.substr(2,8);
    if ( pThis.value.substr(5,1).indexOf('/') == -1 )
      pThis.value = pThis.value.substr(0,5) + '/' + pThis.value.substr(5,8);

    //var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
    var matchArray = pThis.value.match(datePat); 
    if (matchArray == null || pThis.value.length != 10 ) 
    { 
      sMessage ='Formato inválido. Entre DD/MM/AAAA.'; 
    } 
    else 
    { 
      day = matchArray[1];   month = matchArray[3];   year = matchArray[4]; 
      if (month < 1 || month > 12) 
      { 
        sMessage ='Mês inválido.';  
      } 
      if (day < 1 || day > 31)
      {
        sMessage ='Dia inválido.'; 
      }
      if ((month==4 || month==6 || month==9 || month==11) && day==31)
      {
        sMessage ='O mês '+month+' não tem 31 dias.';
      }
      if (month == 2)
      {
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day>29 || (day==29 && !isleap))
        {
          sMessage ='Fevereiro de ' + year + ' não tem ' + day + ' dias.';
        }
      }
    }
    if ( year < 1700 || year > 2100 )
      sMessage = 'Ano inválido, aceitos somente entre 1700 e 2100';
  }



  if (pType.indexOf('h')  != -1 && pThis.value != '' )
  { 
    if ( pThis.value.indexOf(':') == -1 )
      pThis.value = pThis.value.substr(0,2) + ':' + pThis.value.substr(2,5);
    var hour = pThis.value.substr(0,2);
    var min  = pThis.value.substr(3,5);
    if ( hour >= 24 || min >= 60 || pThis.value.length != 5 )
    {
      sMessage = 'Hora inválida. Entre com HH:MI';
      pThis.value = '';
    }
  }

 
  if( sMessage != '' ) 
  { 
    alert( sMessage );
    pThis.focus();
    event.returnValue = false;
  } 
} 







// 28/11/2002 Alimenta array para verificação na fIUD
//   /  / 
aNotNullInput = new Array;

function addNotNullInput (obj)
{
  aNotNullInput[aNotNullInput.length] = obj;
}




// 03/12/2002 Chama página de seleção
//   /  / 
function fSelCall( pGUIName, pparm, pwidth, pheight, pleft, ptop, pscroll )
{
  if ( pscroll=='scroll')  pscroll='yes';
  window.open( pGUIName+'?p_O_Cookie='+pparm , pGUIName.replace('.php',''), 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars='+pscroll+',resizable=yes,menubar=no,width='+pwidth+',height='+pheight+',left='+pleft+',top='+ptop);
}





// 03/12/2002 Atualiza input da página chamadora com valor da página de seleção.
// 25/09/2006 Atualização para o FireFox
//   /  / 
function fSetSelection( pInput, pIdVal, pRecVal, pIndexVal, pClose )
{
  var cmd = 'parent.window.opener.document.f1.'+pInput+'.value = '+pIdVal+';';  

  eval(cmd);

  if ( pRecVal )
  {
      if (document.all)
          cmd = 'parent.window.opener.document.getElementById(\'link_' + pInput + '\').innerText = pRecVal;';
      else
          cmd = 'parent.window.opener.document.getElementById(\'link_' + pInput + '\').textContent = pRecVal;';

      eval (cmd);
  }
  else
      parent.window.opener.document.getElementById('link_'+pInput).innerText = 'Selecionar';

  setCookie('last_'+pInput+'_Id',pIdVal,35 );
  setCookie('last_'+pInput+'_recognize',pRecVal,35 );
  setCookie('last_'+pInput+'_index',pIndexVal,35 );  

  if( pClose=='close' ) { window.close(); }

}



// 04/12/2002 Utilizado para mostrar "Selecionar" nos inputs tipo link
//   /  / 
function fSetText( p )
{
  if ( p == "" ) 
    p = 'Selecionar';
  document.write( p );
}




function fGetSelection ( pInput, pInputType )
{
  if ( pInputType == 'list' ) 
  {
    var cmd = 'parent.window.opener.document.f1.'+pInput+'_Id.selectedIndex = '+pIndexVal+';';  
    eval(cmd);
  }
  if ( pInputType == 'link' ) 
  {
    var cmd = 'parent.window.opener.document.f1.'+pInput+'.value = '+pIdVal+';';  
    eval(cmd);
    if ( pRecVal )
        parent.window.opener.document.all['link_'+pInput].innerText = pRecVal;
    else
        parent.window.opener.document.all['link_'+pInput].innerText = 'Selecionar';     
  }  
}














function fSetSelection__OLD( pInput, pIdVal, pRecVal, pIndexVal, pClose )
{
  setCookie(pInput+'_Id',pIdVal,1 );
  setCookie(pInput+'_recognize',pRecVal,1 );
  setCookie(pInput+'_index',pIndexVal,1 );  

  setCookie('last_'+pInput+'_Id',pIdVal,35 );
  setCookie('last_'+pInput+'_recognize',pRecVal,35 );
  setCookie('last_'+pInput+'_index',pIndexVal,35 );  

  if( pClose=='close' ) { window.close(); }
}





function openGUI( pGUIURL, pGUIname, pwidth, pheight, pleft, ptop )
{  
   //var wObj = pGUIname.substr(0,pGUIname.indexOf('_'));
   // if( !pwidth ) pwidth=800;
   // window.open( pGUIURL+'?pN'+wObj+'_Id='+oCookie('pN'+wObj+'_Id'), pGUIname, 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,menubar=no,width='+pwidth+',height='+pheight+',left='+pleft+',top='+ptop);

  if ( pGUIname.toUpperCase().indexOf('_IR') > 0 )
  {
    pheight = pheight - 40;
    window.open( pGUIURL, pGUIname, 'toolbar=no,location=no,directories=no,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width='+pwidth+',height='+pheight+',left='+pleft+',top='+ptop);
  }
  else
  {
    window.open( pGUIURL, pGUIname, 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width='+pwidth+',height='+pheight+',left='+pleft+',top='+ptop);
  }
}




function oCookie( name )
{
  if (document.cookie.length > 0) 
  {              
    begin = document.cookie.indexOf(name+"=");       
    if (begin != -1) 
    {           
      begin += name.length+1;       
      end    = document.cookie.indexOf(";", begin);
      if (end == -1) end = document.cookie.length;
      return unescape(document.cookie.substring(begin, end));
    } 
  }
  return '';
}



	
//optimizer
function getCookie( name )
{
  if (document.cookie.length > 0) 
  {              
    begin = document.cookie.indexOf(name+"=");       
    if (begin != -1) 
    {           
      begin += name.length+1;       
      end    = document.cookie.indexOf(";", begin);
      if (end == -1) end = document.cookie.length;
      return unescape(document.cookie.substring(begin, end));
    } 
  }
  return '';
}






//optimizer
function setCookie(name, value, expiredays) {
    var ExpireDate = new Date ();

ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));

   document.cookie = name + "=" + escape(value) + ((expiredays == null) ? "; expires=" : "; expires=" + ExpireDate.toGMTString());
}



function fSubmit( i, pOption )
{
  if( pOption.substr(0,6) == 'delete' ) 
  { 
    if( confirm('Deseja realmente excluí=lo ?' ) ) 
    { 
      document.forms[i].pVOPTION.value = pOption; 
      document.forms[i].submit(); 
    }
  }
  else 
  { 
    document.forms[i].pVOPTION.value = pOption; 
    document.forms[i].submit(); 
  } 
} 
  
function fSubmitSearchId( pId )
{
  document.pNId.value    = pId; 
  document.formIUD.pOption.value = 'Search'; 
  document.formIUD.submit(); 
} 

function fCallSelection( pObj )
{
  wSelection = pObj;
  openGUI( pObj+'_iselection', pObj+'_iselection', 748, 450, 24, 80 );
}


//optimizer

//optimizer 2002
function fAcento( p )
{
  p = p.replace('&Agrave;','\xC0');
  p = p.replace('&agrave;','\xE0');  
  p = p.replace('&Aacute;','\xC1');  
  p = p.replace('&aacute;','\xE1');
  p = p.replace('&Acirc;','\xC2');
  p = p.replace('&acirc;','\xE2');
  p = p.replace('&Atilde;','\xC3');
  p = p.replace('&atilde;','\xE3');
  p = p.replace('&Ccedil;','\xC7');
  p = p.replace('&ccedil;','\xE7');
  p = p.replace('&Eacute;','\xC9');
  p = p.replace('&eacute;','\xE9');
  p = p.replace('&Ecirc;','\xCA');
  p = p.replace('&ecirc;','\xEA');


  p = p.replace('&Iacute;','\xCD');
  p = p.replace('&iacute;','\xED');
  p = p.replace('&Oacute;','\xD3');
  p = p.replace('&oacute;','\xF3');
  p = p.replace('&Ocirc;','\xD4');
  p = p.replace('&ocirc;','\xF4');
  p = p.replace('&Otilde;','\xD5');
  p = p.replace('&otilde;','\xF5');
  p = p.replace('&Uacute;','\xDA');
  p = p.replace('&uacute;','\xFA');
  p = p.replace('&Uuml;','\xDC');
  p = p.replace('&uuml;','\xFC');
  return p;
}

//optimizer
function fSetSelectInput( p )
{
  var cmd = 'fSetSelection(\''+p+'\',document.'+p+'_Id.options[document.'+p+'_Id.selectedIndex].value,document.'+p+'_Id.options[document.'+p+'_Id.selectedIndex].text,document.'+p+'_Id.selectedIndex,0)';
  eval(cmd);
}


//optimizer 2002
function fGetSelection____ ( pInput, pInputType ) {
  strFuncParm = "fGetSelectionNoTimer('" + pInput + "','" + pInputType + "');";
  setTimeout (strFuncParm, 100);
}

function fGetSelection___OLD ( pInput, pInputType )
{
  if ( pInputType == 'list' ) 
  {
    var cmd = 'document.f1.'+pInput+'_Id.selectedIndex = getCookie(\''+pInput+'_index\');';  
    eval(cmd);
  }
  if ( pInputType == 'link' ) 
  {
    var cmd = 'document.f1.'+pInput+'.value = getCookie(\''+pInput+'_Id\');';  
    eval(cmd);
    if ( getCookie(pInput+'_recognize')!='' )
//      cmd = 'document.all.link_'+pInput+'.innerText = fAcento(getCookie(\''+pInput+'_recognize\'));';  
        document.all['link_'+pInput].innerText = fAcento(getCookie(pInput+'_recognize'));

    else
//      cmd = 'document.all.link_'+pInput+'.innerText = \'Selecionar\';';     
        document.all['link_'+pInput].innerText = 'Selecionar';     

//  eval(cmd);
  }  
}




//optimizer
function fGUICallCookie( pGUIName, pParams, pwidth, pheight, pleft, ptop, pscroll )
{
  var sParamsValue = '?';
  var sWord        = '';
  if( pParams.length>0 )
    pParams         += ' ';

  while ( pParams.indexOf(' ')>=0 )
  {
    sWord   = pParams.substring(0,pParams.indexOf(' ')); 
    pParams = pParams.substring(pParams.indexOf(' ')+1,50);
    sParamsValue+=sWord+'='+getCookie(sWord)+'&';
  }  
  sParamsValue = sParamsValue.substring(0,(sParamsValue.length)-1);
  if( sParamsValue=='?=' ) sParamsValue='';

  window.open( pGUIName+sParamsValue, pGUIName, 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars='+pscroll+',resizable=yes,menubar=no,width='+pwidth+',height='+pheight+',left='+pleft+',top='+ptop);
}




//
function fGUICall( pGUIName, pwidth, pheight, pleft, ptop, pscroll )
{
  var sName;
  
  if ( pGUIName.indexOf('?')>0 )
  {
    sName = pGUIName.substring(0,pGUIName.indexOf('?'));
    sName = sName.substring(0,pGUIName.indexOf('.'));
  }
  else
  {
    sName = pGUIName; 
    sName = pGUIName.substring(0,pGUIName.indexOf('.'));
  }

  if ( pscroll=='scroll')  pscroll='yes';
 
  if ( pGUIName.toUpperCase().indexOf('_IR') > 0 )
  {
    pheight = pheight - 40;
    window.open( pGUIName, sName, 'toolbar=no,location=no,directories=no,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width='+pwidth+',height='+pheight+',left='+pleft+',top='+ptop);
  }
  else
  {
    window.open( pGUIName, sName, 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width='+pwidth+',height='+pheight+',left='+pleft+',top='+ptop);
  }

}



function fSEStartWith( pLetter )
{
  document.f1.pSOSYS_search.value = 'startWith='+pLetter;
  fSubmit(0,'submit');
}




// Utilizada para colocar o foco automaticamente entre os inputs/selects subsequentes, 
// após o refresh da página.
// Para seu funcionamento é necessário colocar em cada input -> 
// onfocus="setCookie(\'AutoFocus\',this.sourceIndex,1)"
// Chamar essa função no foco da página.
function fAutoFocus()
{
  var p_Num = parseInt ( getCookie('AutoFocus') ) + 1; 

  while ( p_Num<document.all.length )
  {
    if ( document.all(p_Num).tagName == 'INPUT' || document.all(p_Num).tagName == 'SELECT' )
      break;
    p_Num++; 
  }
  if ( p_Num<document.all.length )
    eval ( 'document.all(p_Num).focus();' )  
}



// Usado como maxsize para TEXTAREA
// Uso: 
// oC ( printn('<textarea name=p_X rows=3 cols=70 onkeyup="fTextAreaLimit(this,255)">'.$X_Y_qId[Z].'</textarea><br><i id=p_X_Y_CounterView style="font-size:9pt"></i>'); )
function fTextAreaLimit ( p_TextArea, p_Limit )
{
  var val = p_Limit - p_TextArea.value.length;
  if ( val <=0 ) 
  {
    p_TextArea.value = p_TextArea.value.substr(0,p_Limit);
    val = p_Limit - p_TextArea.value.length;
  }
  eval("document.all."+p_TextArea.name+"_CounterView.innerText = val + ' caracteres restantes'");

}




function fPagina(pNome)
{ 
  if ( pNome )
  {
    openGUI('/'+document.f1.p_O_Search.value+'private/'+pNome+'.php',pNome,780,532,5,5);
  }
}


function fPage(pURL,pName)
{
  openGUI(pURL,pName,489,260,5,5);
}


function UpperCase()
{
  key = window.event.keyCode;
  if ((key > 0x60) && (key < 0x7B))
  window.event.keyCode = key-0x20;
}



function O_gnCPF ( obj_input ) 
{
  cpf = obj_input.value;

  var a = [];
  var b = new Number;
  var c = 11;

  erro = new String;
  if (cpf.length != 11 && cpf.length != 0 ) 
    erro = "CPF inválido"; 


  var nonNumbers = /\D/;

  if (nonNumbers.test(cpf)) 
    erro = "Digite apenas números"; 

  if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999" )
  {
    erro = "CPF inválido"
  }

  for (i=0; i<11; i++)
  {
    a[i] = cpf.charAt(i);

    if (i < 9) 
      b += (a[i] * --c);
  }

  if ((x = b % 11) < 2) 
  { 
    a[9] = 0; 
  } 
  else
  { 
    a[9] = 11-x; 
  }
   b = 0;
  c = 11;
   for ( y=0; y<10; y++ ) 
    b += (a[y] * c--); 
   if ( (x = b % 11) < 2) 
  { 
    a[10] = 0; 
  } 
  else 
  { 
    a[10] = 11-x;
  }
 
  if ( ( (cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10]) ) && cpf.lenght != 0)
  {
    erro ="CPF inválido";
  }
   if (erro.length > 0)
  {
    alert(erro);
    obj_input.value = "";
    ret = false;
  }
  else 
  {
    ret = true;
  }

   return ret;

}


var req = new Array();
function retrieveURL( p_url, p_exec, p_num ) 
{
  var bn = navigator.appName;
  var bv = navigator.appVersion;
  var p_num = typeof(p_num) != 'undefined' ? p_num : 0;
  if ( bn == "Netscape" ) 
  { 
    req[p_num] = new XMLHttpRequest();
    req[p_num].overrideMimeType ( 'text/xml' );
    req[p_num].onreadystatechange = p_exec; 
    try 
    {
      req[p_num].open ( "GET", p_url, true ); 
      //wait_show ( true );
    } 
    catch (e) 
    {
      alert(e);
    }
    req[p_num].send(null);
  } 
  else 
  {
    if ( bn == "Microsoft Internet Explorer" ) 
    { 
      req[p_num] = new ActiveXObject ( "Microsoft.XMLHTTP" );
      if ( req[p_num] ) 
      {
        req[p_num].onreadystatechange = p_exec; //processStateChange;
        req[p_num].open ( "GET", p_url, true );
        req[p_num].send();
        //wait_show ( true );
      }
    }
  }
}


function trim ( str_text ) 
{ 

  while ( str_text.substring ( 0, 1 ) == ' ' || str_text.substring ( 0, 1 ) == '\n' || str_text.substring ( 0, 1 ) == '\r' ) 
    str_text = str_text.substring ( 1, str_text.length );

  while ( str_text.substring ( str_text.length-1, str_text.length ) == ' ' || str_text.substring ( str_text.length-1, str_text.length ) == '\n' || str_text.substring ( str_text.length-1, str_text.length ) == '\r' )
    str_text = str_text.substring ( 0, str_text.length-1 );

  return str_text;

} 



