   function Validar_CNPJ( CNPJ ){ 
      var sCNPJ = "";
      var Numeros = "0123456789";
      
      for(i=0;i<CNPJ.length;i++){
         if(Numeros.indexOf( CNPJ.charAt( i ) )!=-1)
            sCNPJ = sCNPJ + CNPJ.charAt( i )
      }
      
      if( sCNPJ.length != 14 ){ 
         sim = false;
         return false;
      }
      else{
         sim=true
      }
      
      if( sim ){ 
         for (i=0;((i<=(sCNPJ.length-1))&& sim); i++){
            val = sCNPJ.charAt(i) 
            if( (val!="9")&&(val!="0")&&(val!="1")&&(val!="2")&&(val!="3")&&(val!="4") && (val!="5")&&(val!="6")&&(val!="7")&&(val!="8")){
               sim=false
            } 
         } 
         if(sim){ 
            m2 = 2 
            soma1 = 0 
            soma2 = 0 
            for (i=11;i>=0;i--){
               val = eval( sCNPJ.charAt( i ) ) 
               m1 = m2 
               if (m2<9){
                  m2 = m2+1
               }
               else{
                  m2 = 2
               }
               
               soma1 = soma1 + (val * m1) 
               soma2 = soma2 + (val * m2) 
            } // fim do for de soma 

            soma1 = soma1 % 11 
            
            if (soma1 < 2){
               d1 = 0
            }
            else{
               d1 = 11- soma1
            }

            soma2 = (soma2 + (2 * d1)) % 11 
            if (soma2 < 2){ 
               d2 = 0
            }
            else{
               d2 = 11- soma2
            }
              
            if ((d1==sCNPJ.charAt(12)) && (d2==sCNPJ.charAt(13))){
               return true
            }
            else return false;
         } 
      }
   }

function Validar_CPF( CPF ) {
   //int i, add, rev;
   
   //Verifica se o tamanho é 11 e é diferente de repetições
   if( ( CPF.length != 11 ) || ( CPF == "00000000000" ) || ( CPF == "11111111111" ) || ( CPF == "22222222222" ) || ( CPF == "33333333333" ) || ( CPF == "44444444444" ) || ( CPF == "55555555555" ) || ( CPF == "66666666666" ) || ( CPF == "77777777777" ) || ( CPF == "88888888888" ) || ( CPF == "99999999999" ) ){
      return false;
   }else{
      add = 0;

      for( i=0; i < 9; i++ )
         add += parseInt( CPF.charAt( i ) ) * ( 10 - i );

      rev = 11 - ( add % 11 );

      if( ( rev == 10 ) || 
          ( rev == 11 ) )
         rev = 0;

      if( rev != parseInt( CPF.charAt( 9 ) ) )
         return false;

      add = 0;
   
      for ( i=0; i < 10; i++ )
         add += parseInt( CPF.charAt( i ) ) * ( 11 - i );

      rev = 11 - ( add % 11 );
 
      if( ( rev == 10 ) ||
          ( rev == 11 ) )
         rev = 0;

      if( rev != parseInt( CPF.charAt( 10 ) ) )
         return false;
      else
         return true;
   }
}         

         function SomenteNumero(e){
            var tecla=(window.event)?event.keyCode:e.which;
    
            if( ( tecla > 47 ) && ( tecla < 58 ) ) 
               return true;
            else{
               if (tecla != 8) 
                  return false;
               else 
                  return true;
            }
         }

function mask( obj,e,mask ){
      if ((obj.value.length >= obj.maxLength) || (obj.value.length >= mask.length)) return false;

      var strCheck = '#ADMYHMS';
      var numbers = '0123456789';
      var whichCode = (window.Event) ? e.which : e.keyCode;
      var key = String.fromCharCode(whichCode);
      var cChar = mask.charAt(obj.value.length);

      while ((obj.value.length < obj.maxLength) && (obj.value.length < mask.length)){
         if (strCheck.indexOf(cChar) < 0){
            obj.value += cChar;
         }
         else
         {
            if (('#DMYHMS'.indexOf(cChar) >= 0) && (numbers.indexOf(key) < 0)) return false;
            break;
         }
         cChar = mask.charAt(obj.value.length);
      }
      return true;
   }

function Formatar_Moeda( vValor ){
   vValor = vValor.replace(".","");
   vValor = vValor.replace(",",".");
   return vValor;
}

function FormatarValor( objeto, teclapres, tammax, decimais ){
   var tecla   = teclapres.keyCode;
   var tamanhoObjeto = objeto.value.length;
   
   if((tecla==8)&&(tamanhoObjeto==tammax)){
      tamanhoObjeto = tamanhoObjeto-1;
   }
   
   if((tecla==8||tecla==88||tecla>=48&&tecla<=57||tecla>=96&&tecla<=105)&&((tamanhoObjeto+1)<=tammax)){
      vr = objeto.value;
      vr = vr.replace( "/", "" );
      vr = vr.replace( "/", "" );
      vr = vr.replace( ",", "" );
      vr = vr.replace( ".", "" );
      vr = vr.replace( ".", "" );
      vr = vr.replace( ".", "" );
      vr = vr.replace( ".", "" );
      tam = vr.length;
    
      if(tam < tammax && tecla != 8){
         tam = vr.length + 1 ;
      }

      if((tecla == 8) && (tam > 1)){
         tam = tam - 1 ;
         vr = objeto.value;
         vr = vr.replace( "/", "" );
         vr = vr.replace( "/", "" );
         vr = vr.replace( ",", "" );
         vr = vr.replace( ".", "" );
         vr = vr.replace( ".", "" );
         vr = vr.replace( ".", "" );
         vr = vr.replace( ".", "" );
      }
  
      if(tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
         if(decimais > 0){
            if((tam <= decimais)){ 
               objeto.value = ("0," + vr);
            }
        
            if((tam == (decimais + 1)) && (tecla == 8)){
               objeto.value = vr.substr( 0, (tam - decimais)) + ',' + vr.substr( tam - (decimais), tam );
            }
            
            if((tam > (decimais + 1)) && (tam <= (decimais + 3)) &&  ((vr.substr(0,1)) == "0")){
               objeto.value = vr.substr( 1, (tam - (decimais+1))) + ',' + vr.substr( tam - (decimais), tam ) ;
            }
            
            if ( (tam > (decimais + 1)) && (tam <= (decimais + 3)) &&  ((vr.substr(0,1)) != "0")){
               objeto.value = vr.substr( 0, tam - decimais ) + ',' + vr.substr( tam - decimais, tam ) ; 
            }
            
            if((tam >= (decimais + 4)) && (tam <= (decimais + 6))){
               objeto.value = vr.substr( 0, tam - (decimais + 3) ) + '.' + vr.substr( tam - (decimais + 3), 3 ) + ',' + vr.substr( tam - decimais, tam ) ;
            }
            
            if((tam >= (decimais + 7)) && (tam <= (decimais + 9))){
               objeto.value = vr.substr( 0, tam - (decimais + 6) ) + '.' + vr.substr( tam - (decimais + 6), 3 ) + '.' + vr.substr( tam - (decimais + 3), 3 ) + ',' + vr.substr( tam - decimais, tam ) ;
            }
    
            if((tam >= (decimais + 10)) && (tam <= (decimais + 12))){
               objeto.value = vr.substr( 0, tam - (decimais + 9) ) + '.' + vr.substr( tam - (decimais + 9), 3 ) + '.' + vr.substr( tam - (decimais + 6), 3 ) + '.' + vr.substr( tam - (decimais + 3), 3 ) + ',' + vr.substr( tam - decimais, tam ) ;
            }
            
            if((tam >= (decimais + 13)) && (tam <= (decimais + 15)) ){
               objeto.value = vr.substr( 0, tam - (decimais + 12) ) + '.' + vr.substr( tam - (decimais + 12), 3 ) + '.' + vr.substr( tam - (decimais + 9), 3 ) + '.' + vr.substr( tam - (decimais + 6), 3 ) + '.' + vr.substr( tam - (decimais + 3), 3 ) + ',' + vr.substr( tam - decimais, tam ) ;
            }
         }else if(decimais == 0){
            if(tam <= 3 ){ 
               objeto.value = vr;
            }
            
            if((tam >= 4) && (tam <= 6) ){
               if(tecla == 8){
                  objeto.value = vr.substr(0, tam);
                  window.event.cancelBubble = true;
                  window.event.returnValue = false;
               }
               
               objeto.value = vr.substr(0, tam - 3) + '.' + vr.substr( tam - 3, 3 ); 
            }
            
            if((tam >= 7) && (tam <= 9)){
               if(tecla == 8){
                  objeto.value = vr.substr(0, tam);
                  window.event.cancelBubble = true;
                  window.event.returnValue = false;
               }
               
               objeto.value = vr.substr( 0, tam - 6 ) + '.' + vr.substr( tam - 6, 3 ) + '.' + vr.substr( tam - 3, 3 ); 
            }
            
            if((tam >= 10) && (tam <= 12)){
               if(tecla == 8){
                  objeto.value = vr.substr(0, tam);
                  window.event.cancelBubble = true;
                  window.event.returnValue = false;
               }
     
               objeto.value = vr.substr( 0, tam - 9 ) + '.' + vr.substr( tam - 9, 3 ) + '.' + vr.substr( tam - 6, 3 ) + '.' + vr.substr( tam - 3, 3 ); 
            }

            if((tam>=13)&&(tam<=15)){
               if(tecla == 8){
                  objeto.value = vr.substr(0, tam);
                  window.event.cancelBubble = true;
                  window.event.returnValue = false;
               }
            
               objeto.value = vr.substr( 0, tam - 12 ) + '.' + vr.substr( tam - 12, 3 ) + '.' + vr.substr( tam - 9, 3 ) + '.' + vr.substr( tam - 6, 3 ) + '.' + vr.substr( tam - 3, 3 ) ;
            }   
         }
      }
   }else if((window.event.keyCode != 8) && (window.event.keyCode != 9) && (window.event.keyCode != 13) && (window.event.keyCode != 35) && (window.event.keyCode != 36) && (window.event.keyCode != 46)){
      window.event.cancelBubble = true;
      window.event.returnValue = false;
   }
}
