//---------------------------------------------------------------------------------
// Form Validator - By Rafael C.P. rcpinto@inf.ufrgs.br
//---------------------------------------------------------------------------------

function validacampo(campo) {
	var id = campo.getAttribute('id');
 	if (id == '' || id == null) id = campo.name;
 	var invalid = campo.form.getAttribute('invalid');
 	if (!invalid) invalid = 'Atenção! Você não preencheu o campo ';
 	var errormsg = invalid+'\''+id+'\'';
 	if (campo.getAttribute('required') != null && campo.value == '') {
  		alert(errormsg);campo.focus();return false;
 	}
 	if (campo.getAttribute('validation') != null && campo.value != '') {
  		var r = regexp(campo.getAttribute('validation'));
  		if (!r.test(campo.value)) {
  			alert(errormsg);
			campo.focus();
			return false
		}
 	}
 	return true;
}

function validaform(f) {
	var resultado = true;
 	for (i = 0; i < f.length; i++) {
 	    if(f.elements[i].getAttribute('validation') == 'cpf') {
                resultado = resultado && validaCPF(f.elements[i]);
        } else {
            resultado = resultado && validacampo(f.elements[i]);
        }
 	}
 	return resultado;
}

function regexp(tipo) {
	tipo = tipo.toLowerCase();
	var r = new RegExp(tipo);
	if (tipo == 'email' || tipo == 'e-mail') r =  /^[^@ :]{2,}@[^@ :]{2,}\.\w{2,4}(\.\w{2})?$/;
	if (tipo == 'ip') r = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
	if (tipo == 'float' || tipo == 'real' || tipo == 'fracionario') r = /^[-+]?([0-9]*[.,])?[0-9]+([eE][-+]?[0-9]+)?$/;
	if (tipo == 'moeda') r = /^[0-9]+(,[0-9]{2})?$/;
	if (tipo == 'currency') r = /^[0-9]+(\.[0-9]{2})?$/;
	if (tipo == 'mm/dd/yyyy' || tipo == 'date') r = /^(0[1-9]|1[012])\/(0[1-9]|[12][0-9]|3[01])\/\d{4}$/;
	if (tipo == 'dd/mm/yyyy' || tipo == 'data') r = /^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/\d{4}$/;
	if (tipo == 'zip' || tipo == 'cep') r = /^\d{5}(-?\d{3})?$/;
	if (tipo == 'phone' || tipo == 'fone') r = /^[\d\-() ]+$/;
	if (tipo == 'letters' || tipo == 'letras') r = /^[a-zA-Z]+$/;
	if (tipo == 'numbers' || tipo == 'numeros') r = /^[0-9]+$/;
	return r;
}

function validaCPF(inputBox) {
	cpf = inputBox.value;
	erro = new String;
	if (cpf.length < 11) 
		erro += "Sao necessarios 11 digitos para verificacao do CPF! \n\n"; 
	var nonNumbers = /\D/;
	if (nonNumbers.test(cpf))
		erro += "A verificacao de CPF suporta apenas numeros! \n\n"; 
	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 do CPF com problema!";
	}
	if (erro.length > 0){
		alert(erro);
		return false;
	}
	return true;
}

