/************************************************************
Arquivo javascript para validação de texbox
v1.00 / Autor Moacir M Viana
*************************************************************/
function doDate(pStr, pFmt)
{
  var reDate1 = /^\d{1,2}\/\d{1,2}\/\d{1,4}$/;
  var reDate2 = /^[0-3]?\d\/[01]?\d\/(\d{2}|\d{4})$/;
  var reDate3 = /^(0?[1-9]|[12]\d|3[01])\/(0?[1-9]|1[0-2])\/(19|20)?\d{2}$/;
  var reDate4 = /^((0?[1-9]|[12]\d)\/(0?[1-9]|1[0-2])|30\/(0?[13-9]|1[0-2])|31\/(0?[13578]|1[02]))\/(19|20)?\d{2}$/;
  var reDate5 = /^((0[1-9]|[12]\d)\/(0[1-9]|1[0-2])|30\/(0[13-9]|1[0-2])|31\/(0[13578]|1[02]))\/\d{4}$/;
  var reDate = reDate4;

	eval("reDate = reDate" + pFmt);
	if (reDate.test(pStr)) {
			//alert(pStr + " é uma data válida.");
			return true;
	} else if (pStr != null && pStr != "") {
		alert(pStr + " NÃO é uma data válida.");
		return false;
	}
} 

function formatar(src, mask) 
{
  var i = src.value.length;
  var saida = mask.substring(0,1);
  var texto = mask.substring(i)
if (texto.substring(0,1) != saida) 
  {
	src.value += texto.substring(0,1);
  }
}

function validaCPF(vNum) {
      cpf = vNum;
      erro = new String;
      if (cpf.length < 11) erro += "Sao necessarios 11 digitos para verificacao do CPF! ";
          var nonNumbers = /\D/;
         if (nonNumbers.test(cpf)) erro += "A verificacao de CPF suporta apenas numeros! ";
            if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){
                         erro += "Numero de CPF invalido!"
               }
               var a = [];
               var b = new Number;
               var c = 11;
               for (i=0; i<11; i++){
                       a[i] = cpf.charAt(i);
                       if (i < 9) b += (a[i] * --c);
               }
               if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
               b = 0;
               c = 11;
               for (y=0; y<10; y++) b += (a[y] * c--);
               if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
               if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
                       erro +="Digito verificador com problema!";
               }
               if (erro.length > 0){
                       alert(erro);
                       return false;
               }
     return true;
  }


/************************************************************
Função Preenche zeros à esquerda
*************************************************************/
 
function preencheZeros(campo, tamanho){
var strText = campo.value;
if (strText == "" ) {
    if (confirm("Texto vazio - preencher com " + tamanho + " zeros? ")) {
  	   for (i=0; i<tamanho;i++)
	   campo.value += "0";
	  }
	else{
	     campo.focus();
	   }
 }else{ 
	var intTamStr = strText.length;
	if (intTamStr <= tamanho){//executa a adaptação do texto
		var intTam = parseInt(tamanho) - intTamStr;
		for (i=0; i<intTam; i++){
		   strText = "0" + strText;
		  }
	  campo.value = strText;
	 } else
	 {
	   alert("Este campo pode ter no máximo \n" + tamanho + " caracteres.");
	   campo.focus();
	 }
   }
 }
 
 function validaTIT(rcpf1) {
  if (rcpf1.length == 0) { 
    return true;
  }
  //if( !( isInteger(rcpf1)) ){
  //    alert("ATENÇÃO. titulo funao");
  // return alert(rcpf1 + "Número do título eleitoral deve ser numérico.");
  // }  

  if ( !(rcpf1.length == 13)  ) 
     preencheZeros(rcpf1, 13)
     
  rcpf2 = rcpf1.substr(11,2);
  j = rcpf1.substr(9,2);

  if ( (j<1) || (j>28) )
	return alert(rcpf1 + " Número do título eleitoral inválido.")

  d1 = 0;
  for (i=0;i<9;i++)  
    d1 += rcpf1.charAt(i)*(10-i);
  d1 = (d1 % 11);
  if (d1<=1){
     if (j<=2)
        d1 = 1 - d1;
      else
        d1 = 0;}
   else
     d1 = 11 - d1;

  if (rcpf2.charAt(0) != d1)
    return alert(rcpf1 + " Número do título eleitoral inválido.");

  d1 *= 2;
  for (i=9;i<11;i++)
    d1 += rcpf1.charAt(i)*(13-i);
  d1 = (d1 % 11);
  if (d1<=1){
     if (j<=2)
        d1 = 1 - d1;
      else
        d1 = 0;}
   else
     d1 = 11 - d1;

  if (rcpf2.charAt(1) != d1)
    return alert(rcpf1 + "Número do título eleitoral inválido.");

  return true;

}
  