function SoNumeros(_valor){

  var valor = "";
  for(var I=0; I != _valor.value.length; I++){
    if(_valor.value.charAt(I).search(/[0123456789]/)!=-1)
      valor += _valor.value.substr(I,1);
  }
  return valor;
}


function FormataMoeda(_valor,_casasDecimais)
{
	var valorFloat 	= 0.00;
	var achou 		= false;
	var i			= 0;
	var svalor		= "";
    var valor 		= 0.00;
	var m			= 0.00
	
	_valor.value = _valor.value.replace( ".", "" );
    _valor.value = _valor.value.replace( ",", "." );
	
    for(i=0; i != _valor.value.length; i++){
    if(_valor.value.charAt(i).search(/[0123456789.]/)!=-1)
	  valorFloat += _valor.value.substr(i,1);
	}

	if (valorFloat != 0)
	{
	  m = Math.pow(10,_casasDecimais);
	  valor =  parseInt(valorFloat * m, 10) / m;
	  svalor = valor.toString().replace( ".", "," );

      for(i=0; i != svalor.length; i++){
	    if(svalor.charAt(i).search(/[.,]/)!=-1)
		  {
	        achou = true;
		  }
	  }
	  
	}
	else
	{
	  achou  = false;
	  svalor = "0";
	}
	

    if (!achou)
	{
	  if (_casasDecimais > 0)
	    for (i = 0; i <= _casasDecimais;  i++)
	    {
	      if (i==0) svalor +=",";
	      else svalor +="0";
	    }
	}
    return svalor;
}


function FormataData(_valor){
  var data = "";
  for(var I=0; I != _valor.value.length; I++){
    if(_valor.value.charAt(I).search(/[0123456789]/)!=-1)
      data += _valor.value.substr(I,1);
  }
  
  if (data.length < 8)
  {
	  count =  8 - data.length;
	  for(i=0; i < count; i++){
	    data += "0";
	  }
  }
  
  if(data.length >= 8)
  {
    return Formatar(data, "99/99/9999");
  }
  else
  {
  	alert ("Formato de data inválido");
    return "";
  }
}

function FormataHora(_valor){
  var hora = "";
  for(var i=0; i != _valor.value.length; i++){
    if(_valor.value.charAt(i).search(/[0123456789]/)!=-1)
      hora += _valor.value.substr(i,1);
  }
	
  if (hora.length < 4)
  {
	  count =  4 - hora.length;
	  for(i=0; i < count; i++){
	    hora += "0";
	  }
  }

  return Formatar(hora,"99:99");
 }


function FormataCpf(_valor){

  var cpf = "";
  for(var I=0; I != _valor.value.length; I++){
    if(_valor.value.charAt(I).search(/[0123456789]/)!=-1)
      cpf += _valor.value.substr(I,1);
  }

  if (cpf.length < 11)
  {
	  count =  11 - cpf.length;
	  for(i=0; i < count; i++){
	    cpf += "0";
	  }
  }

  return Formatar(cpf, '999.999.999-99');
}


function FormataCnpj(_valor){
  if (_valor.value != "")  
  {
    var cnpj = "";
    for(var I=0; I != _valor.value.length; I++){
      if(_valor.value.charAt(I).search(/[0123456789]/)!=-1)
        cnpj += _valor.value.substr(I,1);
    }

    if (cnpj.length < 14)
    {
	  count =  14 - cnpj.length;
	  for(i=0; i < count; i++){
	    cnpj += "0";
	  }
    }

    return Formatar(cnpj, '99.999.999/9999-99');
  }
  else
    return "";
}

function VerificaTecla(_campo, _evento, _tipo){

  var tecla = navigator.appName=="Netscape" ? _evento.which : _evento.keyCode;
  var _CpfCnpj = "";
  var temVirgula = false;

  if(_tipo == "moeda"){  
	if(tecla == 0 || tecla == 8 || tecla == 44 || tecla == 188 || tecla == 37 || tecla == 39 || tecla == 9 )
      if (tecla == 188)
	  { 
	    for(var i=0; i != _campo.value.length; i++){
          if(_campo.value.charAt(i).search(/[,]/)!=-1)
            temVirgula = true;
  	    }
		if (! temVirgula) return true;
		else
		{ 
		  return false;
		}
	  }
	  else 
	  {
	   return true;
	  }
  }    
  else if(_tipo == "data"){
	   if (tecla == 0 || tecla == 8  || tecla == 37    || tecla == 39 || tecla == 9 || tecla == 46 || tecla == 36 || tecla == 35 || (_evento.ctrlKey && tecla == 86) || (_evento.ctrlKey && tecla == 90) || (_evento.ctrlKey && tecla == 67) || (_evento.ctrlKey && tecla == 88) || (_evento.ctrlKey && tecla == 65) || (_evento.ctrlKey && tecla == 89))
	//                BackupSpace    Seta esquerda     Seta direita   Tab           Delete         Home           End               CTRL + V                            CTRL + Z                            CTRL + C                            CTRL + X                            CTRL + A                             CTRL + Y 
      return true;
  }    
  else if(_tipo == "hora"){
    if(tecla == 0 || tecla == 8 || tecla == 58  || tecla == 37 || tecla == 39 || tecla == 9)
      return true;
  }    
  else if(_tipo == "numerico"){
    if (tecla == 0 || tecla == 8  || tecla == 37    || tecla == 39 || tecla == 9)
	//                BackupSpace    Seta esquerda     Seta direita   Tab       
      return true;
  }    
  else if (_tipo == "cnpj")
  {
	   if (tecla == 0 || tecla == 8  || tecla == 37    || tecla == 39 || tecla == 9 || tecla == 46 || tecla == 36 || tecla == 35 || (_evento.ctrlKey && tecla == 86) || (_evento.ctrlKey && tecla == 90) || (_evento.ctrlKey && tecla == 67) || (_evento.ctrlKey && tecla == 88) || (_evento.ctrlKey && tecla == 65) || (_evento.ctrlKey && tecla == 89))
	//                BackupSpace    Seta esquerda     Seta direita   Tab           Delete         Home           End               CTRL + V                            CTRL + Z                            CTRL + C                            CTRL + X                            CTRL + A                             CTRL + Y 
      return true;
  }
  if( tecla < 48 || tecla > 57 )
 	 if( tecla < 96 || tecla > 105 )	
	 {
    	return false;
	 }
		 
  return true;  
}

function Formatar(_valor, _mascara){
  var S, Z, M, D, TM, DEC, V;

  DEC=0;
  if(_valor.length == 0){
    return "";
  }
  
  V=_valor.indexOf(',');
  if(_mascara.indexOf('#') != -1 && _mascara.indexOf('.') != -1){
    DEC=_mascara.indexOf('.');
    DEC=_mascara.length-DEC;
    if( V > 0 ){
      if(_valor.length-V < DEC)
        _valor+=repetir(DEC-(_valor.length-V),'0');
    }
    else
      _valor+=repetir(DEC,'0');
  }
  if(_mascara.length > 0 && _mascara.indexOf('X') == -1){
    if(_mascara.indexOf('#') != -1){
      TM=0;
      for(M=0; M != _mascara.length; M++){
        if(_mascara.charAt(M).search(/[0123456789#-]/)!=-1)
          TM++;
      }
      Z="";
      for(M=0; M != _valor.length; M++){
        if(_valor.charAt(M).search(/[0123456789-]/)!=-1)
          Z+=_valor.charAt(M);
      }
      if(Z.length < TM)
        Z=repetir(TM-Z.length,'0') + Z;
      S="";
      D=1;
      for(M=0; M!=_mascara.length; M++){
        if(_mascara.charAt(M).search(/[0123456789#-]/)!=-1){
          S+=Z.charAt(D);
          D++;
        }
        else{
          if(_mascara.charAt(M) == '.' && DEC > 0)
            S+=',';
          else if(_mascara.charAt(M) == ',' && DEC > 0)
            S+='.';
          else
            S+=_mascara.charAt(M);
        }
        if(DEC > 0)
		  {
          S=FormatFloat(parseFloat(Z)/Math.exp(DEC*Math.log(10)));
		  }
        else
		 {
          S=FormatFloat(parseFloat(Z));
		 }
        S=space(_mascara.length - S.length) + S;
      }
    }
    else{
      TM=0;
      for(M=0; M!=_mascara.length; M++){
        if(_mascara.charAt(M).search(/[0123456789]/)!=-1)
          TM++;
      }
      Z="";
      for(M=0; M!=_valor.length; M++){
        if(_valor.charAt(M).search(/[0123456789]/)!=-1){
          Z+=_valor.charAt(M);
        }
      }
      if(Z.length < TM)
        Z=repetir(TM-Z.length,'0') + Z;
      S="";
      D=0;
      for(M=0; M!=_mascara.length; M++){
        if(_mascara.charAt(M).search(/[0123456789]/)!=-1){
          S+=Z.charAt(D);
          D++;
        }
        else
          S+=_mascara.charAt(M);
      }
    }
  }
  else
    S = _valor;
  return S;  
 
}