 String.prototype.trim = function() {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
  }

//Função de validação de CPF
function isCPF(st) {
  if (st == "") return (false);
  l = st.length;

//aleterado para se usuário não digitar os zeros na frente do CPF, completar sozinho
  if ((l == 9) || (l == 8)) {
    for (i = l ; i < 10; i++){
      st = '0' + st
    }
  }

  l = st.length;
  st2 = "";

  for (i = 0; i < l; i++) {
    caracter = st.substring(i,i+1);

    if ((caracter >= '0') && (caracter <= '9'));
      st2 = st2 + caracter;
    }

    if ((st2.length > 11) || (st2.length < 10)) return (false);

    if (st2.length==10) st2 = '0' + st2;

    digito1 = st2.substring(9,10);
    digito2 = st2.substring(10,11);
    digito1 = parseInt(digito1,10);
    digito2 = parseInt(digito2,10);
    sum = 0; mul = 10;

    for (i = 0; i < 9 ; i++) {
        digit = st2.substring(i,i+1);
        tproduct = parseInt(digit ,10) * mul;
        sum += tproduct;
        mul--;
    }

    dig1 = ( sum % 11 );

    if ( dig1==0 || dig1==1 )
      dig1=0;
    else
      dig1 = 11 - dig1;

    if (dig1!=digito1) return (false);

    sum = 0;
    mul = 11;

    for (i = 0; i < 10 ; i++) {
        digit = st2.substring(i,i+1);
        tproduct = parseInt(digit ,10)*mul;
        sum += tproduct;
        mul--;
    }

    dig2 = (sum % 11);

    if ( dig2==0 || dig2==1 )
      dig2=0;
    else
      dig2 = 11 - dig2;

    if (dig2 != digito2) return (false);
    return (true);
}

//Verifica se os campos obrigatórios do formulário foram preenchidos
function verificaDados(f){
  var campos = f.getElementsByTagName("input");
  var select = f.getElementsByTagName("select");

  //campos de texto
  for(var i in campos){
    if(campos[i].id != "*") continue;

    if(campos[i].value.trim() == ""){
      alert("Preencha o campo " + campos[i].name + " corretamente");
      campos[i].select();
      return false;
    }
  }

  //Dropdown
  for(var i in select){
    if(select[i].value == "-1"){
      alert("Preencha o campo " + select[i].name);
      select[i].focus();
      return false;
    }
  }
  
  //procura algum campo de cpf para validação
  for(var i in campos){
    if(campos[i].name == "cpf"){
      //Valida o cpf
      if(!isCPF(campos[i].value)){
        alert("Digite um CPF valido");
        return false;
      }
    }
  }

  return true;
}

function checaNumero(e){
    var keynum;
    var keychar;
    var numcheck;

    if(window.event) { // IE
    	keynum = e.keyCode;
   	}else if(e.which) { // Netscape/Firefox/Opera
    	keynum = e.which;
   	}

    if(keynum == 8) return true;

    keychar = String.fromCharCode(keynum);
    numcheck = /\d/;
    return numcheck.test(keychar);
  }

  function formataData(campo, evento){
    var tecla = evento.keyCode;

    if(tecla == 8 || tecla == 46){
      return false;
    }

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

