function digita_valor(campo) {
 var valor = "";
 var tecla = 0;
 var componente = campo;
 bloqueia_alfa();
 if (event.keyCode > 0) {
  valor = campo.value;
  componente.value = valor + String.fromCharCode(event.keyCode);
//  alert(componente.value);
  mascara_valor(componente);
  event.keyCode = 0;
 }
}

function mascara_valor(parametro)
{
        var r                = 0;
        var e                = 0;
        var i                = 0;
        var x                = 0;
        var valor            = "";
        var valor1           = "";
        var valor2           = "";
        var str              = "";
        var str1             = "";
        var achou            = false;

        valor = parametro.value;

        for (x=0;(x<valor.length);x++) {
         str = valor.substr(x,1);
         if ((str != "0" &&
             str != "." &&
             str != ",") ||
             achou) {
          str1 = str1+str;
          achou = true;
         }
        }
        valor = str1;

        if (valor.length == 1) {
         valor = "00"+valor;
        }
        else if (valor.length == 2) {
         valor = "0"+valor;
        }

        for (i=0;(i<valor.length);i++)
        {
                valor = valor.replace(".","");
                valor = valor.replace(",","");
        }

        r                = valor.length;

        if (r > 0)
        {
                valor1        = valor.substr(0,(r-2));
                valor2        = valor.substr((r-2),2);
                i                = (valor.length/3);
                i                = parseInt(i);
                e                = 3;

                while(i > 0)
                {
                        valor1 = valor1.substr(0,(valor1.length-e)) + "" + valor1.substr((valor1.length-e),e);
                        i = i - 1
                        e = e + 4
                }
                if (valor1.substr(0,1) == ".")
                {
                        valor1 = valor1.substr(1);
                }
                if (valor1 != "")
                {
                        parametro.value = valor1 + "," + valor2;
                }
        }
        else
        {
                parametro.value = "";
        }
}

function bloqueia_alfa_valor() {
 //função que permite a digitação de números 0-9
 // 44 ==> ,
 // 46 ==> .
 if ((event.keyCode < 44 || event.keyCode >57))
 {
  event.keyCode = 0;
  return false;
 }
 else
 {
  if (event.keyCode == 47)
  {
   event.keyCode = 0;
   return false;
  }
 }
}

function upcase(campo) {
 var texto = campo.value;
 campo.value = texto.toUpperCase();
}

function strzero(campo,qtde){
 var tam;
 var qtde_zeros;
 var str;

 str = campo.value;
 tam = str.length;
 qtde_zeros = qtde - tam;

 for (i=1; i <= qtde_zeros; i++) {
  str = '0'+str;
 }
 campo.value = str;
}

function valida_data(str_data) {
 if (str_data.value != "") {
  if (str_data.value.length < 10) {
   alert('Data inválida!');
   str_data.focus();
  }
  else {
   array_data = str_data.value.split('/');
   var dia;
   var mes;
   var ano;

   dia = array_data[0];
   mes = array_data[1];
   ano = array_data[2];

   if (!valida_data1(dia, mes, ano)) {
    alert('Data inválida!');
    str_data.focus();
   }
  }
 }
}

function valida_data1(dia,mes,ano) {
        //Funcionalidade:        Valida a Data retornando True se for uma Data
        //                                        válida e False se não for.
        //                                        Antes de se usar esta função deve-se garantir que os parâmetros
        //                                        passados sejam numéricos e inteiros.
        // PARÂMETROS:
        //                Dia = Dia da Data(caracteres numericos),
        //                Mes = Mes da Data(caracteres numericos),
        //                Ano = Ano da Data(caracteres numericos)

        var v_dia;
        var v_mes;
        var v_ano;

        if (!valida_inteiro(dia)){
                return (false);
        }
        if (!valida_inteiro(mes)){
                return (false);
        }
        if (!valida_inteiro(ano)){
                return (false);
        }

        v_dia = dia;
        v_mes = mes;
        v_ano = ano;

        if (v_dia.length < 1)
        {
                return(false);
        }

        if (v_mes.length < 1)
        {
                return(false);
        }

        if (v_ano.length < 4)
        {
                return(false);
        }

        if (((v_ano < 1900) || (v_ano > 2079)) && (v_ano.length != 0))
        {
                return(false);
        }

        if (v_dia > 31 || v_dia < 1)
        {
                return(false);
        }

        if (v_mes > 12 || v_mes < 1)
        {
                return(false);
        }

        if (v_dia == "31")
        {
                if ((v_mes == "04") || (v_mes == "06") || (v_mes == "09") || (v_mes == "11"))
                {
                        return(false);
                }
        }

        //Validação de Ano Bissexto
        if (v_mes == "02")
        {
                if (!(v_ano%4))
                {
                        if (v_dia > 29)
                        {
                                return(false);
                        }
                }
                else if (v_dia > 28)
                {
                        return(false);
                }
        }

        //o -if- abaixo testa se algum campo foi preenchido e outro deixado em branco deixando a data incompleta

        if (((v_dia != "") || (v_mes != "") || (v_ano != "")) && ((v_dia == "") || (v_mes == "") || (v_ano == "")))
        {
                return(false);
        }

        return(true);
}

function valida_inteiro(parametro)  //FUNCAO PARA VALIDACAO DE NÚMEROS INTEIROS, E ESPAÇOS EM BRANCO
{
        if (parametro.length != 0)
        {
                if (!verifica_branco(parametro))
                {
                        return true;
                }

                teste_ponto = "false";
                tamanho_parametro = parametro.length;

                if (isNaN(parametro)) //valor digitado não é numérico
                {
                        return false;
                }
                else //valor digitado é um numérico válido
                {

                        for (k = 0; k < tamanho_parametro; k++)
                        {if ((parametro.charAt(k) == '.') || (parametro.charAt(k) == '-') || (parametro.charAt(k) == '+'))
                                {
                                        teste_ponto = "true"; /*existe caracter ponto*/
                                }
                        }

                        if (teste_ponto == "true") //encontrou caracter ponto(numero real)
                        {
                                return false;
                        }
                        else
                        {
                                return true;
                        }
                }
        }
        else
        {
                return true;
        }

}
function verifica_branco(parametro)   // FUNCAO PARA VERIFICAÇÃO DE CAMPOS NÃO PREENCHIDOS
                                                                        // OU PREENCHIDOS APENAS COM ESPAÇOS EM BRANCO
{

        var tamanho_parametro = 0;
        teste_parametro = "false"; //variavel para teste de espacos em branco
        tamanho_parametro = parametro.length;
        if (tamanho_parametro != 0)
        {
                for (i = 0; i < tamanho_parametro; i++)
                        {if (parametro.charAt(i) != " ")
                                {
                                        teste_parametro = "true"; /*existe caracter diferente de branco*/
                                }
                        }
                if (teste_parametro == "false")  //todos os caracteres digitados são brancos
                {
                        return false;
                }
                else
                {
                        return true;
                }
        }
        else
        {
                return false;
        }

}
function mascara_data(param)
{
        bloqueia_alfa();
        if (param.value.length==2)
        {
                param.value = param.value + "/";
                return;
        }
        if (param.value.length==5)
        {
                param.value = param.value + "/";
                return;
        }
}

function valida_email(valor) {
 if (valor.value != "") {
  if (valor.value.indexOf("@") == -1 ||
      valor.value.indexOf(".") == -1) {
   return false;
  }
  else {
   return true;
  }
 }
 else {
  return true
 }
}

function cgc_cpf(valor) {
 if (valor.value.length == 11) {
  return valida_cpf(valor.value);
 }
 else if (valor.value.length == 14) {
  return valida_cnpj(valor.value);
 }
}

function valida_cpf(cpf) {
 rcpf1 = cpf.substr(0,9);
 rcpf2 = cpf.substr(9,2);

  d1 = 0;
  for (i=0;i<9;i++)
    d1 += rcpf1.charAt(i)*(10-i);
        d1 = 11 - (d1 % 11);
  if (d1>9) d1 = 0;

  if (rcpf2.charAt(0) != d1)
    return false;

  d1 *= 2;
  for (i=0;i<9;i++)
    d1 += rcpf1.charAt(i)*(11-i);
        d1 = 11 - (d1 % 11);

  if (d1>9) d1 = 0;

  if (rcpf2.charAt(1) != d1) return false;

  return true;
}

function valida_cnpj(strCNPJ) {
        if (strCNPJ == "")
        {
                return false;
        }

        var
                strDV = strCNPJ.substr(12,2),
                intSoma,
                intDigito = 0,
                strControle = "",
                strMultiplicador = "543298765432";

        strCNPJ = strCNPJ.substr(0, 12);
        for(var j = 1; j <= 2; j++)
        {
                intSoma = 0;
                for(var i = 0; i <= 11; i++)
                {
                        intSoma += (parseInt(strCNPJ.substr(i, 1), 10) * parseInt(strMultiplicador.substr(i, 1), 10))
                }
                if(j == 2){intSoma += (2 * intDigito)}
                intDigito = (intSoma * 10) % 11;
                if(intDigito == 10){intDigito = 0}
                strControle += intDigito.toString();
                strMultiplicador = "654329876543";
        }
        return(strControle == strDV);
}

function bloqueia_alfa() {
 //função que permite a digitação de números 0-9
 if ((event.keyCode < 47 || event.keyCode >57))
 {
  event.keyCode = 0;
  return false;
 }
 else
 {
  if (event.keyCode == 47)
  {
   event.keyCode = 0;
   return false;
  }
 }
}