function validaCPF(cpf) 
{
  erro = new String;

	if (cpf.value.length == 11)
	{	
			cpf.value = cpf.value.replace('.', '');
			cpf.value = cpf.value.replace('.', '');
			cpf.value = cpf.value.replace('-', '');

			var nonNumbers = /\D/;
	
			if (nonNumbers.test(cpf.value)) 
			{
					erro = "A verificacao de CPF suporta apenas números!"; 
			}
			else
			{
					if (cpf.value == "00000000000" || 
							cpf.value == "11111111111" || 
							cpf.value == "22222222222" || 
							cpf.value == "33333333333" || 
							cpf.value == "44444444444" || 
							cpf.value == "55555555555" || 
							cpf.value == "66666666666" || 
							cpf.value == "77777777777" || 
							cpf.value == "88888888888" || 
							cpf.value == "99999999999") {
							
							erro = "Número de CPF inválido!"
					}
	
					var a = [];
					var b = new Number;
					var c = 11;

					for (i=0; i<11; i++){
							a[i] = cpf.value.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.value.charAt(9) != a[9]) || (cpf.value.charAt(10) != a[10])) {
						erro = "Número de CPF inválido.";
					}
			}
	}
	else
	{
		if(cpf.value.length == 0)
			return false
		else
			erro = "Número de CPF inválido.";
	}
	if (erro.length > 0) {
			alert(erro);
			cpf.focus();
			return false;
	} 	
	return true;	
}

//envento onkeyup
function maskCPF(CPF) {
	var evt = window.event;
	kcode=evt.keyCode;
	if (kcode == 8) return;
	if (CPF.value.length == 3) { CPF.value = CPF.value + '.'; }
	if (CPF.value.length == 7) { CPF.value = CPF.value + '.'; }
	if (CPF.value.length == 11) { CPF.value = CPF.value + '-'; }
}

// evento onBlur
function formataCPF(CPF)
{
	with (CPF)
	{
		value = value.substr(0, 3) + '.' + 
				value.substr(3, 3) + '.' + 
				value.substr(6, 3) + '-' +
				value.substr(9, 2);
	}
}
function retiraFormatacao(CPF)
{
	with (CPF)
	{
		value = value.replace (".","");
		value = value.replace (".","");
		value = value.replace ("-","");
		value = value.replace ("/","");
	}
}


function valida(form) {
if (form.cliente_nome.value=="") {
alert("Preencha o nome corretamente.");
form.cliente_nome.focus();
return false;
}



if (form.cliente_compl.value=="" || !IsNum(form.cliente_compl.value)) {
alert("Preencha o CPF corretamente.");
form.cliente_compl.focus();
return false;
}


	
var filtro_mail = /^.+@.+\..{2,3}$/
if (!filtro_mail.test(form.cliente_email.value) || form.cliente_email.value=="") {
alert("Preencha o e-mail corretamente.");
form.cliente_email.focus();
return false;
}


if (form.endereco.value=="" || form.endereco.value.length < 8) {
alert("Preencha o endereço corretamente.");
form.endereco.focus();
return false;
}



if (form.data_nascimento.value=="" || form.data_nascimento.value.length != 10) {
alert("Preencha a data de nascimento corretamente.");
form.data_nascimento.focus();
return false;
}

if (form.senha.value=="" || form.senha.value.length < 6) {
alert("Preencha a senha corretamente.");
form.senha.focus();
return false;
}

if (form.conf_senha.value=="" || form.conf_senha.value.length < 6) {
alert("Preencha a confirmação de senha corretamente.");
form.conf_senha.focus();
return false;
}

if (form.senha.value!=form.conf_senha.value) {
alert("A senha e a confirmação tem de ser iguais.");
form.conf_senha.focus();
return false;
}

if (form.sexo[0].checked==false && form.sexo[1].checked==false) {
alert("Selecione o sexo.");
return false;
}
}


