//Permite que sejam digitados apenas numeros
function numero(e) {
  if(typeof(e)=='undefined')var e=window.event
  campo=e.target?e.target:e.srcElement
  if(campo.nodeType == 3)campo = campo.parentNode

  tecla = e.keyCode;
  if (!	(tecla > 47 && tecla < 58)) // numeros de 0 a 9
    //backspace
    if (!(tecla == 8)) {
      campo.value = campo.value.substring(0, campo.value.length -1)
    }
}

function mascara_data(e){
  if(typeof(e)=='undefined')var e=window.event
  campo=e.target?e.target:e.srcElement
  if(campo.nodeType == 3)campo = campo.parentNode

  var mydata = '';
  mydata = mydata + campo.value;
  if (mydata.length == 2){
    mydata = mydata + '/';
    campo.value = mydata;
  }
  if (mydata.length == 5){
    mydata = mydata + '/';
    campo.value = mydata;
  }
}

function valida_data(e) {
  if(typeof(e)=='undefined')var e=window.event
  campo=e.target?e.target:e.srcElement
  if(campo.nodeType == 3)campo = campo.parentNode

  dia = (campo.value.substring(0,2));
  mes = (campo.value.substring(3,5));
  ano = (campo.value.substring(6,10));
  ok = true;
  // verifica o dia valido para cada mes
  if ((dia < 01)||(dia < 01 || dia > 30) && (  mes == 04 || mes == 06 || mes == 09 || mes == 11 ) || dia > 31) {
    ok = false;
  }
  // verifica se o mes e valido
  if (mes < 01 || mes > 12 ) {
    ok = false;
  }
  // verifica se e ano bissexto
  if (mes == 2 && ( dia < 01 || dia > 29 || ( dia > 28 && (parseInt(ano / 4) != ano / 4)))) {
    ok = false;
  }
  // verifica se o ano e valido
  if (ano < 1800 || ano > 3000 ) {
    ok = false;
  }
  if (ok == "true") {
    return true;
  } else {
    return false;
  }
}

function valida(formulario){
  campos = formulario.getElementsByTagName("input");
  erros = '';
  numero_erros = 0;

  for (var aux = 0; aux < campos.length; aux++) {
    nome = campos[aux].name;
    //Verifica se o campo deve ser validado
    if (nome.substring(0, nome.indexOf('_')) == 'validar') {
      nome = nome.substring(nome.indexOf('_') + 1, nome.length);

      //Descobre o tipo do campo para validação
      tipo = nome.substring(0, nome.indexOf('_'));

      //Se o tipo do campo for indefinido
      if (tipo == 'naonulo') {
        nome = nome.substring(nome.indexOf('_') + 1, nome.length);
        if (campos[aux].value == '') {
          erros = erros + nome + ';\n';
          erro = nome;
          numero_erros++;
        }
      }
    }
  }
  if (numero_erros > 0) {
    if (numero_erros == 1) {
      erros = 'O campo '+erro+' é obritatório!';
    }
    if (numero_erros > 1) {
      erros = 'Os campos abaixo são obrigatórios e você deve preenche-los:\n'+erros;
    }
    alert(erros);
    return false;
  } else {
    return true;
  }


}

function FormataValor(campo,tammax,teclapres) {
  var tecla = teclapres.keyCode;
  var vr = campo.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 = tam - 1 ; }

  if ( tecla != 8 || (tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105) ){
    if ( tam <= 2 ){
      campo.value = vr ; }
    tam = tam - 1;
    if ( (tam > 2) && (tam <= 5) ){
      campo.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
    if ( (tam >= 6) && (tam <= 8) ){
      campo.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
    if ( (tam >= 9) && (tam <= 11) ){
      campo.value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
    if ( (tam >= 12) && (tam <= 14) ){
      campo.value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
    if ( (tam >= 15) && (tam <= 17) ){
      campo.value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ;}
  }
}


function init(){
  campos = document.getElementsByTagName("input");

  for (var aux = 0; aux < campos.length; aux++) {
    nome = campos[aux].name;
    //Verifica se o campo deve ser validado
    if (nome.substring(0, nome.indexOf('_')) == 'validar') {
      nome = nome.substring(nome.indexOf('_') + 1, nome.length);

      //Descobre o tipo do campo para validação
      tipo = '';
      if (nome.substring(0, nome.indexOf('_')) == 'data') {
        tipo = 'data';
      }
      if (nome.substring(0, nome.indexOf('_')) == 'numero') {
        tipo = 'numero';
      }

      if (tipo == 'data') {
        campos[aux].onkeyup = mascara_data;
        campos[aux].maxlength = 10;
      }
      if (tipo == 'numero') {
        campos[aux].onkeyup = numero;
      }
    }
  }
}
