/******************************************************************

				OPERAÇÕES COM FORMULÁRIOS
				-------------------------
				Triares Informática e Tecnologia
				http://www.triares.com.br	

*******************************************************************/

function permitirNumero(e){
	var tecla=(window.event)?event.keyCode:e.which;	
    if((tecla > 47 && tecla < 58)){	
		return true;
	}else{
    	return (tecla != 8 && tecla !=13 && tecla != 0) ? false : true; 
	} 
}

function maxLength(obj, limit) {
	if (obj.value.length >= limit) {
		obj.value = obj.value.substring(0, limit-1);
	}
}

function trim(campo){
	var i = 0;
	while (campo.charCodeAt(0) == '32'){
		campo = campo.substring(i,campo.length);
		i++;
	}
	while(campo.charCodeAt(campo.length-1) == "32"){
		campo = campo.substring(0,campo.length-1);
	}	
	return campo;
}

function validarEmail(email){
	var bolReturn = false;
	var oRegEmail = /^[a-z0-9\._\-]+\@[a-z0-9\._\-]+\.[a-z]{2,3}$/i;
	bolReturn = oRegEmail.test(email);
	
	if(!bolReturn){
		return false;
	}
	return true;
}

/*
function validarData(campo){
	var expReg = /^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[1-2][0-9]\d{2})$/;
	if ((campo.match(expReg)) && (campo!='')){
	var dia = campo.substring(0,2);
	var mes = campo.substring(3,5);
	var ano = campo.substring(6,10);
	if(mes==4 || mes==6 || mes==9 || mes==11 && dia > 30){
	return false;
	} else{
	if(ano%4!=0 && mes==2 && dia>28){
	return false;
	} else{
	if(ano%4==0 && mes==2 && dia>29){
	return false;
	} else{
	return true;
	}}}} else {
	campo.focus();
	return false;
}}*/

function validarCPF( cpf ) {	
	if(cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" ||
	  cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" ||
	  cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" ||
	  cpf == "88888888888" || cpf == "99999999999"){
	  return false;
   }

   soma = 0;
   for(i = 0; i < 9; i++)
   	 soma += parseInt(cpf.charAt(i)) * (10 - i);
   resto = 11 - (soma % 11);
   if(resto == 10 || resto == 11)
	 resto = 0;
   if(resto != parseInt(cpf.charAt(9))){
	 return false;
   }
   soma = 0;
   for(i = 0; i < 10; i ++)
	 soma += parseInt(cpf.charAt(i)) * (11 - i);
   resto = 11 - (soma % 11);
   if(resto == 10 || resto == 11)
	 resto = 0;
   if(resto != parseInt(cpf.charAt(10))){
	 return false;
   }
   return true;
}

function validarCNPJ(CNPJ){
	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])){
		return false;
	}	
	return true;
}

function isCpfCnpj(pCpfCnpj) {
	var numero = pCpfCnpj.replace(/\D/g, "");
	if (numero.length > 11)
		return validarCNPJ(pCpfCnpj);
	else
		return validarCPF(pCpfCnpj);
}


/* ====================================================================================== */

function validarForm( form ) {
	for(i=0; i < form.elements.length; i++) {	
		if(typeof(form.elements[i].name) != "undefined") {
			var id = form.elements[i].getAttribute("id");
			var value = trim(form.elements[i].value);
			if(form.elements[i].getAttribute("class") == "requerido" && value == "") {
				alert("Preencha todos os campos obrigatórios");
				form.elements[i].focus();
				return false;
			}
			if(id.indexOf("email") != -1 && value != "") {	
				if(!validarEmail(value)) {
					alert("E-mail Inválido");
					form.elements[i].focus();
					return false;
				}
			}
			if(id.indexOf("cd_cpf_cnpj") != -1 && value != "") {
				if(!isCpfCnpj(value)) {
					alert("CPF ou CNPJ Inválido");
					return false;
				}
			}
			if(id.indexOf("cd_cnpj") != -1 && value != "") {
				if( !validarCNPJ(value) ) {
					alert('CNPJ Inválido');
					return false;
				}
			}			
			/*			if(id.indexOf("dt_") != -1 && value != "") {
				if(!validarData(value)) {
					alert("Data Inválida");
					form.elements[i].focus();
					return false;
				}
			}
			*/				
		}
	}
	return true;
	
}
