Mascaras = {
IsIE: navigator.appName.toLowerCase().indexOf('microsoft')!=-1,
AZ: /[A-Z]/i,
Acentos: /[À-ÿ]/i,
Num: /[0-9]/,
carregar: function( parte ) {
  var Tags = ['input','textarea'];
   if ( typeof parte == "undefined" ) parte = document;
   for( var z=0; z < Tags.length; z++ ){
     Inputs = parte.getElementsByTagName( Tags[z] );
     for(var i = 0; i<Inputs.length; i++ )
        if( ('button,image,hidden,submit,reset').indexOf(Inputs[i].type.toLowerCase())==-1)
        this.aplicar(Inputs[i]);
     }
   },

   aplicar: function( campo ) {
      tipo = campo.getAttribute('tipo');
      if ( !tipo || campo.type == "select-one" ) return;
      orientacao = campo.getAttribute('orientacao');
      mascara = campo.getAttribute('mascara');
      if (tipo.toLowerCase() == "decimal"){
         orientacao = "esquerda";
         casasdecimais = campo.getAttribute('casasdecimais');
         tamanho = campo.getAttribute('maxLength');
         if (!tamanho || tamanho > 50) tamanho = 10;
         if (!casasdecimais) casasdecimais = 2;
         campo.setAttribute("mascara", this.geraMascaraDecimal(tamanho, casasdecimais));
         campo.setAttribute("tipo", "numerico");
         campo.setAttribute("orientacao", orientacao);
      }
      if (tipo.toLowerCase() == "cep"){
         tamanho = 9;
         campo.setAttribute("mascara", "#####-###" );
         campo.setAttribute("tipo", "numerico");
      }
      if (orientacao && orientacao.toLowerCase() == "esquerda") campo.style.textAlign = "right";
      if (mascara) campo.setAttribute("maxLength", mascara.length);
      if (tipo){
         campo.onkeypress = function(e){ return Mascaras.onkeypress(e?e:event); };
         campo.onkeyup = function(e){ Mascaras.onkeyup(e?e:event, campo) };
         campo.onfocus = function(e){ Mascaras.onfocus(e?e:event, campo) };
         campo.onblur = function(e){ Mascaras.onblur( e?e:event, campo) };
      }
      campo.setAttribute("snegativo", ((campo.value).substr(0,1) == "-" ? "s" : "n"));
   },

   onfocus : function( e, campo ) {
//       alert( document.getElementById(campo.id).style.background );
       document.getElementById(campo.id).style.background = "#FFFFcc";
   },

   onblur : function( e, campo ) {
      document.getElementById(campo.id).style.background = "#FFFFFF";
	  // valida o campo apos a saida do mesmo
      tipo = campo.getAttribute('tipo');
	  if (tipo.toLowerCase() == "validar") {
		 Dados();
      }

	  if (tipo.toLowerCase() == "cpfcnpj") {
         conteudo = this.chartodigits( campo.value );
	     if ( conteudo.length == 11 ) {
 	        if ( ! this.validacpf( conteudo ) ) {
               document.getElementById(campo.id).focus();
			   alert( "Número do CPF inválido !!!" );
			   return false;
			}
            campo.setAttribute( "mascara", "###.###.###-##" );
            this.aplicarMascara( campo, conteudo );
            campo.setAttribute( "mascara", "" );
			return true;
		 } 
		 if ( conteudo.length == 14 ) {
 	        if ( ! this.validacnpj( conteudo ) ) {
               document.getElementById(campo.id).focus();
//			   alert( "Número do CNPJ inválido !!!" );
			   return false;
			}
            campo.setAttribute( "mascara", "##.###.###/####-##" );
            this.aplicarMascara( campo, conteudo );
            campo.setAttribute( "mascara", "" );
			return true;
		}
  		   if ( conteudo.length >= 1 ) { document.getElementById(campo.id).focus(); }
//  	    alert( "Obrigatório informar o CPF ou CNPJ !!!" );
	  }
   },

   chartodigits : function( tchar ) {  
      //Remove todos os caracteres excetos 0-9  
	  s="";  
	  ts = new String( tchar );  
	  for ( x=0; x < ts.length; x++ ) {  
 		 ch = ts.charAt(x);  
	  	 if ( this.asc( ch ) >= 48 && this.asc( ch ) <= 57 ){  
			s = s + ch;  
		 }  
	  }  
	  return s;  
   },

   asc : function ( achar ) {  
	  var n=0;  
	  var ascstr = this.makecharsetstring();
	  for( i = 0; i < ascstr.length; i++ ) {  
		 if( achar==ascstr.substring( i, i+1 ) ) {  
			n = i;  
			break;  
		 }  
	  }  
	  return n + 32  
   },

   makecharsetstring : function () {  
      // Gera uma string com os caracteres básicos na sequência de códigos ASC  
   	  var astr ;
	  astr = ' !"#$%&\'()*+,-./0123456789:;<=>?@'  
	  astr+= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'  
	  astr+= '[\]^_`abcdefghijklmnopqrstuvwxyz'  
	  astr+= '{|}~'  
	  return astr  
   },

   validacpf : function( ncpf ) {
      // Calcula o digito verificador do cpf e compara se é válido
      if ( isNaN( ncpf ) ) {
         return false;
      }
      var i;
      var c = ncpf.substr( 0, 9 );
      var dv = ncpf.substr( 9, 2 );
      var d1 = 0;
      for (i = 0; i < 9; i++) {
          d1 += c.charAt(i)*(10-i);
      }
      if ( d1 == 0 ){
         return false;
      }        
      d1 = 11 - (d1 % 11);
      if (d1 > 9) d1 = 0;        
      if (dv.charAt(0) != d1) {
         return false;        
      }
      d1 *= 2;
      for (i = 0; i < 9; i++) {
         d1 += c.charAt(i)*(11-i);
      }
      d1 = 11 - (d1 % 11);
      if (d1 > 9) d1 = 0;
      if (dv.charAt(1) != d1) {
         return false;
      }
      return true;
   },

   validacnpj : function( ncnpj ) {
      // Calcula o digito verificador do cnpj e compara se é válido
      if ( isNaN( s ) )
         return false; 
      var i;
      var c = ncnpj.substr(0,12);
      var dv = ncnpj.substr(12,2);
      var d1 = 0;
      for ( i = 0; i < 12; i++ ) {
         d1 += c.charAt( 11-i ) * ( 2 + ( i % 8 ) );
      }
      if ( d1 == 0 )
         return false;
      d1 = 11 - (d1 % 11);
      if (d1 > 9) d1 = 0;
      if ( dv.charAt(0) != d1 )
         return false;
      d1 *= 2;
      for ( i = 0; i < 12; i++ ) {
         d1 += c.charAt(11-i)*(2+((i+1) % 8));
      }
      d1 = 11 - (d1 % 11);
      if (d1 > 9)
         d1 = 0;
      if ( dv.charAt(1) != d1 ){
         return false;
      }
      return true;
   },

   onkeypress: function( e ) {
      KeyCode = this.IsIE ? event.keyCode : e.which;
      campo =  this.IsIE ? event.srcElement : e.target;
      readonly = campo.getAttribute('readonly');
      //alert(event.keyCode);
      if (event.keyCode==13) {
  	     event.keyCode = 9;
	     return true
      }
      if ( readonly ) return;
      maxlength = campo.getAttribute('maxlength');
      pt = campo.getAttribute('pt');
      selecao = this.selecao(campo);
      if (selecao.length > 0 && KeyCode != 0){
         campo.value = ""; return true;
      }
      if (KeyCode == 0) return true;
      Char = String.fromCharCode(KeyCode);
      valor = campo.value;
      mascara = campo.getAttribute('mascara');
	  somar   = campo.getAttribute('somar');
      if ( KeyCode != 8 ){
         tipo = campo.getAttribute('tipo').toLowerCase();
         negativo = campo.getAttribute('negativo');
         if ( negativo && KeyCode == 45 ){
            snegativo = campo.getAttribute('snegativo');
            snegativo = (snegativo == "s" ? "n" : "s");
            campo.setAttribute( "snegativo", snegativo );
         } else {
            valor += Char
            if ( tipo == "numerico" && Char.search( this.Num ) == -1 ) return false;
            if ( KeyCode != 32 && tipo == "caracter" && Char.search(this.AZ) == -1 && Char.search(this.Acentos) == -1 ) return false;
         }
      }
      if ( mascara ) {
         this.aplicarMascara( campo, valor );
		 if ( somar ) soma();
         return false;
      }
      return true;
   },

   onkeyup: function(e, campo){
      KeyCode = this.IsIE ? event.keyCode : e.which;
      //alert(event.keyCode);
      if ( KeyCode != 9 && KeyCode != 16 && KeyCode != 109 ){
         valor = campo.value;
         somar = campo.getAttribute('somar');
         if ( KeyCode == 8 && !this.IsIE ) valor = valor.substr( 0,valor.length-1 );
         this.aplicarMascara( campo, valor );
         if ( somar ) soma();
      }
   },

   aplicarMascara: function( campo, valor ){
      mascara = campo.getAttribute( 'mascara' );
      if ( !mascara ) return;
      negativo = campo.getAttribute('negativo');
      snegativo = campo.getAttribute('snegativo');
      if ( negativo && valor.substr(0,1) == "-" ) 
         valor = valor.substr( 1, valor.length-1 );
      orientacao = campo.getAttribute('orientacao');
      var i = 0;
      for( i=0; i < mascara.length; i++ ) {
         caracter = mascara.substr(i,1);
         if (caracter != "#") valor = valor.replace(caracter, "");
      }
      retorno = "";
      if ( orientacao != "esquerda" ) {
         contador = 0;
         for( i=0; i < mascara.length; i++ ) {
            caracter = mascara.substr(i,1);
            if ( caracter == "#" ){
               retorno += valor.substr( contador, 1 );
               contador++;
            } else retorno += caracter;
            if( contador >= valor.length ) break;
         }
      } else {
        contador = valor.length-1;
        for( i = mascara.length - 1; i >= 0; i-- ) {
           if( contador < 0 ) break;
           caracter = mascara.substr( i, 1 );
           if (caracter == "#") {
              retorno = valor.substr( contador, 1 ) + retorno;
              contador--;
           	} else retorno = caracter + retorno;
        }
      }
      if ( negativo && snegativo == "s" )
         retorno = "-" + retorno;
      campo.value = retorno;
   },

   geraMascaraDecimal: function(tam, decimais){
      var retorno = ""; var contador = 0; var i = 0;
      decimais = parseInt(decimais);
      for (i=0;i<(tam-(decimais+1));i++) {
         retorno = "#" + retorno;
         contador++;
         if ( contador == 3 ) {
            retorno = "." + retorno;
            contador=0;
         }
      }
      if ( decimais > 0 ) {
         retorno = retorno + ",";
         for (i=0;i<decimais;i++) retorno += "#";
	  }
      return retorno;
   },

   selecao: function(campo){
      if (this.IsIE)
         return document.selection.createRange().text;
      else
         return (campo.value).substr(campo.selectionStart, (campo.selectionEnd - campo.selectionStart));
   },

   formataValor: function (valor, decimais){
      valor = valor.split('.');
      if (valor.length == 1) valor[1] = "";
      for(var i=valor[1].length;i<decimais;i++)
      valor[1] += "0"; 
      valor[1] = valor[1].substr(0,2);
      return (valor[0] + "." + valor[1]);
   }
};