function validaCpf(campo) {
 strcpf = campo.value;
 str_aux = "";
 for (i = 0; i <= strcpf.length - 1; i++)
   if ((strcpf.charAt(i)).match(/\d/)) 
     str_aux += strcpf.charAt(i);
   else if (!(strcpf.charAt(i)).match(/[\.\-]/)) {
     alert ("O campo CPF apresenta caracteres inválidos !!!");
     //campo.focus();
     return false;
   }
 if (str_aux.length != 11) {
   alert ("O campo CPF deve conter 11 dígitos !!!");
   //campo.focus();
   return false;
 }
 soma1 = soma2 = 0;
 for (i = 0; i <= 8; i++) {
   soma1 += str_aux.charAt(i) * (10-i);
   soma2 += str_aux.charAt(i) * (11-i);
 }
 d1 = ((soma1 * 10) % 11) % 10;
 d2 = (((soma2 + (d1 * 2)) * 10) % 11) % 10;
 if ((d1 != str_aux.charAt(9)) || (d2 != str_aux.charAt(10))) {
   alert ("O CPF digitado é inválido !!!");
   //campo.focus();
   return false;
 }
 campo.value = str_aux;
 return true;
}

/***
 *
 *
 ***/
 
function validaCnpj(CNPJ) {
  //CNPJ = campo.value;
  erro = new String;
  if (CNPJ.length < 18) erro += "É necessario preencher corretamente o número do CNPJ! \n\n"; 
  if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
    if (erro.length == 0) erro += "É necessário preencher corretamente o número do CNPJ! \n\n";
  }
  //substituir os caracteres que não são números
   if(document.layers && parseInt(navigator.appVersion) == 4){
		   x = CNPJ.substring(0,2);
		   x += CNPJ. substring (3,6);
		   x += CNPJ. substring (7,10);
		   x += CNPJ. substring (11,15);
		   x += CNPJ. substring (16,18);
		   CNPJ = x; 
   } else {
		   CNPJ = CNPJ. replace (".","");
		   CNPJ = CNPJ. replace (".","");
		   CNPJ = CNPJ. replace ("-","");
		   CNPJ = CNPJ. replace ("/","");
   }
   var nonNumbers = /\D/;
   if (nonNumbers.test(CNPJ)) erro += "A verificação de CNPJ suporta apenas números! \n\n"; 
   var a = [];
   var b = new Number;
   var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
   for (i=0; i<12; i++){
		   a[i] = CNPJ.charAt(i);
		   b += a[i] * c[i+1];
   }
   if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
   b = 0;
   for (y=0; y<13; y++) {
		   b += (a[y] * c[y]); 
   }
   if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }
   if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
		   erro +="Dígito verificador com problema!";
   }
   if (erro.length > 0){
		   alert(erro);
		   return false;
   } 
   return true;
}
/***
 *
 *
 ***/

function validaUsuario(campo){
	tamanho = campo.value.length;
	original = campo.value;
	for(i=0;i<=tamanho;i++){
		campo.value = campo.value.replace(' ', '');
	}
	if (original != campo.value) {
		alert("O campo 'usuário' não pode conter espaços.\n Por isso seu nome de usuário foi alterado para '"+document.frmLoginCadastra.usuario.value+"'.");
	}
	if (campo.value != '' && campo.value != null) {
		return true;
	} else {
		alert("O nome de usuário não pode ser deixado em branco!");
		return false;
	}
}

//var reEmail1 = /^[\w!#$%&'*+\/=?^`{|}~-]+(\.[\w!#$%&'*+\/=?^`{|}~-]+)*@(([\w-]+\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
//var reEmail2 = /^[\w-]+(\.[\w-]+)*@(([\w-]{2,63}\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
var reEmail3 = /^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
var reEmail = reEmail3;

/***
 *
 *
 ***/

function validaEmail(email) {
	//eval("reEmail = reEmail" + pFmt);
	if (reEmail.test(email)) {
		return true;
		//alert(email + "  um endereo de e-mail vlido.");
	} else if (email != null && email != "") {
		alert(email + " não é um endereço de e-mail válido.");
		return false;
	} else {
		alert ("O endereço de e-mail deve ser informado");
		return false;
	}
} // validaEmail

/***
 *
 *
 ***/

function confEmail(email1, email2) {
	if (email1 != email2) {
		alert("E-mails digitados no conferem ("+email1+" e "+email2+")");
		return false;
	} else { 
		return true;
	}
} // confEmail

/***
 *
 *
 ***/

function validaSenha(senha) {
	tamanho = senha.value.length;
	if (tamanho < 5) {
		alert("Sua senha precisa ter ao menos 5 dígitos.");	
		//document.frmLoginCadastra.senha.focus();
		return false;
	}
	return true;
} // validaSenha

/***
 *
 *
 ***/

function confSenha(senha, senhaC) {
	if (senha != senhaC) {
		alert("Senhas digitadas no conferem.");
		document.frmLoginCadastra.senhaC.value = '';
		return false;
	} else { 
		return true;
	}
} // confEmail

