////////////////////////////////////////////////////////////
//  Documento Java Script                                 //
//  Francisco C Paulino - Tofinha - fcptofinha@globo.com  //
//  06/11/2002                                            //
////////////////////////////////////////////////////////////
// Início das Funções Eventos
// TRATAMENTO DO EVENTO ONCONTEXTMENU
document.oncontextmenu = _oncontextmenu;

function _oncontextmenu()
	{
		//event.returnValue = false;	   
	}

//  TRATAMENTO DO EVENTO ONFOCUS DE INPUT/SELECT/TEXTAREA 
function control_onfocus(campo){
	campo.style.backgroundColor='White';
}

//  TRATAMENTO DO EVENTO ONBLUR DE INPUT/SELECT/TEXTAREA *************
function control_onblur(campo){
	campo.style.backgroundColor='#efefef'
}

//  TRATAMENTO DO EVENTO ONKEYDOWN DE INPUT/SELECT/TEXTAREA *************
function control_onkeydown(campo){		
	var tecla = event.keyCode;
	var prox = FindNextControl(campo);	
	if (tecla == 13){
		while (prox.disabled){
			prox = FindNextControl(prox);
		}		
		prox.focus();
		event.returnValue = 0;
	}
}

// EVENTOS DO TOOLBAR
function toolBarOver(obj){	
	obj.className = 'ToolBarAtivo';
}
function toolBarOut(obj){
	obj.className = 'ToolBar';
}
function toolBarClick(td,url){
}
function toolBarClickVolatr(td1){
}
function tdOver(td){
}
function tdOut(td){
}
function tdClick(td, tipo, modulo){
}
// Fim das Funções Eventos
//////////////////////////////////////////////////////
//Início da Função para Máscara e Validação de Datas
//Chamada= onblur="check_date(this)"
   function check_date(field) 
   {
      var checkstr = "0123456789"; 
      var DateField = field; 
      var Datevalue = ""; 
      var DateTemp = ""; 
      var seperator = "/"; 
      var day; 
      var month; 
      var year; 
      var leap = 0; 
      var err = 0; 
      var i; 
      err = 0; 
      DateValue = DateField.value; 
      /* Deletando todos os caracteres exceto o 0..9 */ 
      for (i = 0; i < DateValue.length; i++) 
      { 
         if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) 
            { 
                DateTemp = DateTemp + DateValue.substr(i,1); 
            } 
      } 
      DateValue = DateTemp; 
      /* Exectutando a data para 8 digitos - string*/ 
      /* if entrada do ano com 2-digitos / exemplo 20xx */ 
      if (DateValue.length == 6) 
         { 
            if (DateValue.substr(4,2) < 50)
			{
				DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); 
			}else{
				DateValue = DateValue.substr(0,4) + '19' + DateValue.substr(4,2); 
			}
         } 
      if (DateValue.length != 8) 
         { 
            err = 19; 
         } 
      /* Se o ano for errado = 0000 */ 
      year = DateValue.substr(4,4); 
      if (year == 0) 
         { 
            err = 20; 
         } 
      /* Validando o mês*/ 
      month = DateValue.substr(2,2); 
      if ((month < 1) || (month > 12)) 
         { 
            err = 21; 
         } 
      /* Validando o dia*/ 
      day = DateValue.substr(0,2); 
      if (day < 1) 
         { 
            err = 22; 
         } 
      /* Validando ano Bissexto / fevereiro / dia */ 
      if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) 
         { 
            leap = 1; 
         } 
      if ((month == 2) && (leap == 1) && (day > 29)) 
         { 
            err = 23; 
         } 
      if ((month == 2) && (leap != 1) && (day > 28)) 
         { 
            err = 24; 
         } 
      /* Validando o mês */ 
      if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) 
         { 
            err = 25; 
         } 
      if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) 
         { 
            err = 26; 
         } 
      /* if 00 houvendo entrada, sem erros */ 
      if ((day == 0) && (month == 0) && (year == 00)) 
         { 
            err = 0; day = ""; month = ""; year = ""; seperator = ""; 
         } 
      /* if sem erros, escrevo a data completa no Input-Field (e.x. 13/12/2001) */ 
      if (err == 0) 
         { 
            DateField.value = day + seperator + month + seperator + year; 
         } 
      /* Mensagem de erro if err != 0 */ 
      else 
         { 
            alert("Atenção, sua Data está Incorreta!"); 
            DateField.select(); 
            DateField.focus(); 
         } 
}
//Fim da Função para Máscara e Validação de Datas
///////////////////////////////////////////////////////////
//Início da Função Formata Valores em Reais
//Chamada: onKeyPress="return(FormataReais(this,'.',',',event))"
function FormataReais(fld, milSep, decSep, 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 = fld.value.length;
for(i = 0; i < len; i++)
if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
aux = '';
for(; i < len; i++)
if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
aux += key;
len = aux.length;
if (len == 0) fld.value = '';
if (len == 1) fld.value = '0'+ decSep + '0' + aux;
if (len == 2) fld.value = '0'+ decSep + aux;
if (len > 2) {
aux2 = '';
for (j = 0, i = len - 3; i >= 0; i--) {
if (j == 3) {
aux2 += milSep;
j = 0;
}
aux2 += aux.charAt(i);
j++;
}
fld.value = '';
len2 = aux2.length;
for (i = len2 - 1; i >= 0; i--)
fld.value += aux2.charAt(i);
fld.value += decSep + aux.substr(len - 2, len);
}
return false;
}
//Fim da Função Formata Valores em Reais
///////////////////////////////////////////////////////////
//Início da Função Máscaras Gerais
//Chamadas:<input type="text" name="nomeCampo" maxlength="20" size="20"
             //onkeypress="return txtBoxFormat(document.Form, 'nomeCampo', '99.999999999/9999-99', event);">
function txtBoxFormat(objForm, strField, sMask, evtKeyPress) {
      var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

      if(document.all) { // Internet Explorer
        nTecla = evtKeyPress.keyCode; }
      else if(document.layers) { // Nestcape
        nTecla = evtKeyPress.which;
      }

      sValue = objForm[strField].value;

      // Limpa todos os caracteres de formatação que
      // já estiverem no campo.
      sValue = sValue.toString().replace( "-", "" );
      sValue = sValue.toString().replace( "-", "" );
      sValue = sValue.toString().replace( ".", "" );
      sValue = sValue.toString().replace( ".", "" );
      sValue = sValue.toString().replace( "/", "" );
      sValue = sValue.toString().replace( "/", "" );
      sValue = sValue.toString().replace( "(", "" );
      sValue = sValue.toString().replace( "(", "" );
      sValue = sValue.toString().replace( ")", "" );
      sValue = sValue.toString().replace( ")", "" );
      sValue = sValue.toString().replace( " ", "" );
      sValue = sValue.toString().replace( " ", "" );
      fldLen = sValue.length;
      mskLen = sMask.length;

      i = 0;
      nCount = 0;
      sCod = "";
      mskLen = fldLen;

      while (i <= mskLen) {
        bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/"))
        bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

        if (bolMask) {
          sCod += sMask.charAt(i);
          mskLen++; }
        else {
          sCod += sValue.charAt(nCount);
          nCount++;
        }

        i++;
      }

      objForm[strField].value = sCod;

      if (nTecla != 8) { // backspace
        if (sMask.charAt(i-1) == "9") { // apenas números...
          return ((nTecla > 47) && (nTecla < 58)); } // números de 0 a 9
        else { // qualquer caracter...
          return true;
        } }
      else {
        return true;
      }
    }
//Fim da Função Máscaras Gerais
//////////////////////////////////////////////////////////

function sonumeros(ConteudoCampo)
{  //alert ("Key Press: " + event.keyCode);
	//alert(ConteudoCampo.name);
   if (((event.keyCode) > 47) && ((event.keyCode) < 58)) return(true)
   else return(false)
}
function so_numeros_virgula(ConteudoCampo)
{  //alert ("Key Press: " + event.keyCode);
	//alert(ConteudoCampo.name);
   if ( ((event.keyCode > 47) && (event.keyCode < 58)) || (event.keyCode == 44) ) return(true)
   else return(false)
}

function filtradata(ConteudoCampo)
{  //alert ("Key Press: " + event.keyCode);
	//alert(ConteudoCampo.name);
   if (((event.keyCode) > 46) && ((event.keyCode) < 58)) return(true)
   else return(false)
}

function filtrahorario(ConteudoCampo)
{  //alert ("Key Press: " + event.keyCode);
	//alert(ConteudoCampo.name);
   if (((event.keyCode) > 47) && ((event.keyCode) < 58)) return(true)
   else return(false)
}

function MascaraData (keypress, objeto)
{
	campo = eval (objeto);
	caracteres = '01234567890';
	separacoes = 2;
	separacao1 = '/';
	conjuntos = 3;
	conjunto1 = 2;
	conjunto2 = 2;
	conjunto3 = 4;
	if ((caracteres.search(String.fromCharCode (keypress))!=-1) && campo.value.length < 
		(conjunto1 + conjunto2 + conjunto3 + 3))
	{
		if (campo.value.length == conjunto1) 
		   campo.value = campo.value + separacao1;
		else if (campo.value.length == conjunto1 + conjunto2 + 1) 
		   campo.value = campo.value + separacao1;
	}
	else event.returnValue = false;
}
function MascaraHorario (keypress, objeto)
{
	campo = eval (objeto);
	caracteres = '01234567890';
	separacoes = 1;
	separacao1 = ':';
	conjuntos = 2;
	conjunto1 = 2;
	conjunto2 = 2;
	if ((caracteres.search(String.fromCharCode (keypress))!=-1) && campo.value.length < 
		(conjunto1 + conjunto2 + 2))
	{
		if (campo.value.length == conjunto1) campo.value = campo.value + separacao1;
	}
	else event.returnValue = false;
}

function MascaraCNPJ (keypress, objeto)
{
campo = eval (objeto);
	caracteres = '01234567890';
	separacoes = 4;
	separacao1 = '.';
	separacao3 = '/';
	separacao4 = '-';
	conjuntos = 5;
	conjunto1 = 2;
	conjunto2 = 3;
	conjunto3 = 3;
	conjunto4 = 4;
	conjunto5 = 2;
	if ((caracteres.search(String.fromCharCode (keypress))!=-1) && campo.value.length < 
	(conjunto1 + conjunto2 + conjunto3 + conjunto4 + conjunto5 + 5))
	{
		if (campo.value.length == conjunto1) 
		   campo.value = campo.value + separacao1;
		else if (campo.value.length == conjunto1 + conjunto2 + 1) 
		   campo.value = campo.value + separacao1;
		else if (campo.value.length == conjunto1 + conjunto2 + conjunto3 + 2) 
		   campo.value = campo.value + separacao3;
		else if (campo.value.length == conjunto1 + conjunto2 + conjunto3 + conjunto4 + 3) 
		   campo.value = campo.value + separacao4;
	}
	else 
		event.returnValue = false;
}

function MascaraCPF (keypress, objeto)
{
campo = eval (objeto);
	caracteres = '01234567890';
	separacoes = 3;
	separacao1 = '.';
	separacao3 = '-';
	conjuntos = 4;
	conjunto1 = 3;
	conjunto2 = 3;
	conjunto3 = 3;
	conjunto4 = 2;
	if ((caracteres.search(String.fromCharCode (keypress))!=-1) && campo.value.length < 
	(conjunto1 + conjunto2 + conjunto3 + conjunto4 + 4))
	{
		if (campo.value.length == conjunto1) 
		   campo.value = campo.value + separacao1;
		else if (campo.value.length == conjunto1 + conjunto2 + 1) 
		   campo.value = campo.value + separacao1;
		else if (campo.value.length == conjunto1 + conjunto2 + conjunto3 + 2) 
		   campo.value = campo.value + separacao3;
	}
	else 
		event.returnValue = false;
}


function limita_tamanho (campo, limite)
{
	if (campo.value.length > limite -1) return false;
	else return true;
}

function FtrataBackSpace(dado)
{
   NumDig = dado.value;
   TamDig = NumDig.length;
   TamDig--;
   Contador = 0;

   if ((TamDig >= 0) && (event.keyCode == 8))
    { numer = "";
      for (i = TamDig; (i >= 0); i--){
          if ((parseInt(NumDig.substr(i,1))>=0) && (parseInt(NumDig.substr(i, 1))<=9))
            {
             Contador++;
			 /*alert(NumDig.substr(i, 1) + " Contador " + Contador+ " numer "+ numer+ " TamDig " +TamDig + " i " + i );*/
             if ((Contador == 4) && ((TamDig -i) < 5))
              {numer = ","+numer;
               Contador = 0;
               }
             else if ((Contador == 3) && ((numer.length) > 4))  
              {numer = "."+numer;
               Contador = 0;
              }
			  
             numer = NumDig.substr(i, 1)+numer;
			
            }
			}
			if (numer == "001" || numer == "000" || numer == "002" || numer == "003" || numer == "004" || numer == "005" || numer == "006" || numer == "007" || numer == "008" || numer == "009") 
			    numer="";		
			if ((numer.length) == 3 )
			    numer= "0," + numer;

		dado.value = numer;
      };

}


function FmascTempoReal(ConteudoCampo)
{  //alert ("Key Press: " + event.keyCode);
//alert(ConteudoCampo.name);
if (ConteudoCampo.name == "PERCENTUAL") TamMax = 6;
else TamMax = 14;
if (ConteudoCampo.value.length < TamMax) {
   if (((event.keyCode) > 47) && ((event.keyCode) < 58)) //abre if #1
   {
   NumDig = ConteudoCampo.value;
   //alert("NumDig = " + NumDig);
   TamDig = NumDig.length;
   //alert("TamDig = " + TamDig);
   Contador = 0;
   if (TamDig > 1) { //abre if #2
      numer = "";
      for (i = TamDig; (i >= 0); i--){
		  //alert("i = " + i);
		  //alert("NumDig.substr(i,1) = " + NumDig.substr(i,1));
		  //alert("parseInt(NumDig.substr(i,1)) = " + parseInt(NumDig.substr(i,1)));
          if ((parseInt(NumDig.substr(i,1))>=0) && (parseInt(NumDig.substr(i, 1))<=9)) //abre if #3
            {
             Contador++;
             //alert("Contador = " + Contador);
             if ((Contador == 2) && ((TamDig -i) < 4))
              {numer = ","+numer;
               Contador = 0;
               }
             else if (Contador == 3)
              {numer = "."+numer;
               Contador = 0;
              }
             numer = NumDig.substr(i, 1)+numer;
             //alert("numer = " + numer);
            } //fecha if #3
           } //fecha for
      ConteudoCampo.value = numer;
      //alert(ConteudoCampo.value);
      }; //fecha if #2
   return(true)
   } //fecha if #1
   else return(false)
}
}
function FtrataBackSpaceVPN(e) {
   NumDig = document.form1.VALORPRINC.value;
   TamDig = NumDig.length;
   TamDig--;
   Contador = 0;
   //alert(NumDig);
   if ((TamDig >= 0) && (e.which == 8))
    { numer = "";
      for (i = TamDig; (i >= 0); i--){
          if ((parseInt(NumDig.substr(i,1))>=0) && (parseInt(NumDig.substr(i, 1))<=9))
            {
             Contador++;
			 /*alert(NumDig.substr(i, 1) + " Contador " + Contador+ " numer "+ numer+ " TamDig " +TamDig + " i " + i );*/
             if ((Contador == 4) && ((TamDig -i) < 5))
              {numer = ","+numer;
               Contador = 0;
               }
             else if ((Contador == 3) && ((numer.length) > 4))  
              {numer = "."+numer;
               Contador = 0;
              }
			  
             numer = NumDig.substr(i, 1)+numer;
			 
            }
			}
			if (numer == "001" || numer == "000" || numer == "002" || numer == "003" || numer == "004" || numer == "005" || numer == "006" || numer == "007" || numer == "008" || numer == "009") 
			    numer="";		
			if ((numer.length) == 3 )
			    numer= "0," + numer;
		//alert(numer);
		document.form1.VALORPRINC.value = numer.slice(0, numer.length-1);
		document.form1.VALORPRINC.select();	
      };
}
