// JavaScript Document

//*****************************************************funçoes AUXILIARES DO CNPJ - início
function isNUMB(c) { 
	if((cx=c.indexOf(","))!=-1) { 
		c = c.substring(0,cx)+"."+c.substring(cx+1); 
	} 
	if((parseFloat(c) / c != 1)) { 
		if(parseFloat(c) * c == 0) { 
			return(1); 
		} else { 
			return(0); 
		} 
	} else { 
		return(1); 
	} 
} 

function LIMP(c) { 
	while((cx=c.indexOf("-"))!=-1) { 
		c = c.substring(0,cx)+c.substring(cx+1); 
	} 
	while((cx=c.indexOf("/"))!=-1) { 
		c = c.substring(0,cx)+c.substring(cx+1); 
	} 
	while((cx=c.indexOf(","))!=-1) { 
		c = c.substring(0,cx)+c.substring(cx+1); 
	} 
	while((cx=c.indexOf("."))!=-1) { 
		c = c.substring(0,cx)+c.substring(cx+1); 
	} 
	while((cx=c.indexOf("("))!=-1) { 
		c = c.substring(0,cx)+c.substring(cx+1); 
	} 
	while((cx=c.indexOf(")"))!=-1) { 
		c = c.substring(0,cx)+c.substring(cx+1); 
	} 
	while((cx=c.indexOf(" "))!=-1) { 
		c = c.substring(0,cx)+c.substring(cx+1); 
	} 
	return(c); 
} 

function VerifyCNPJ(CNPJ) { 
	CNPJ = LIMP(CNPJ); 
	if(isNUMB(CNPJ) != 1) { 
		return(0); 
	} else { 
		if(CNPJ == 0) { 
			return(0); 
		} else { 
			g=CNPJ.length-2; 
			if(RealTestaCNPJ(CNPJ,g) == 1) { 
				g=CNPJ.length-1; 
				if(RealTestaCNPJ(CNPJ,g) == 1) { 
					return(1); 
				} else { 
					return(0); 
				} 
			} else { 
				return(0); 
			} 
		} 
	} 
} 

function RealTestaCNPJ(CNPJ,g) { 
	var VerCNPJ=0; 
	var ind=2; 
	var tam; 
	for(f=g;f>0;f--) { 
		VerCNPJ+=parseInt(CNPJ.charAt(f-1))*ind; 
		if(ind>8) { 
			ind=2; 
		} else { 
			ind++; 
		} 
	} 
	VerCNPJ%=11; 
	if(VerCNPJ==0 || VerCNPJ==1) { 
		VerCNPJ=0; 
	} else { 
		VerCNPJ=11-VerCNPJ; 
	} 
	if(VerCNPJ!=parseInt(CNPJ.charAt(g))) { 
		return(0); 
	} else { 
		return(1); 
	} 
}
//*********************************************************funçoes AUXILIARES DO CNPJ - fim

//*********************************************************funçoes MÁSCARAS DE FORMATAÇÃO - inicio
// Inicio da mascara p/ CNPJ
function FormataCGC(Formulario, Campo, TeclaPres) { 
	var tecla = TeclaPres.keyCode; 
	var strCampo; 
	var vr; 
	var tam; 
	var TamanhoMaximo = 14; 
	eval("strCampo = document." + Formulario + "." + Campo); 
	vr = strCampo.value; 
	while (vr.indexOf("/") != -1) {
		vr = vr.replace("/", ""); 
	}
	while (vr.indexOf(",") != -1) {
		vr = vr.replace(",", ""); 
	}
	while (vr.indexOf(".") != -1) {
		vr = vr.replace(".", ""); 
	}
	while (vr.indexOf("-") != -1) {
		vr = vr.replace("-", ""); 
	}
	tam = vr.length; 
	if (tam < TamanhoMaximo && tecla != 8) { 
		tam = vr.length + 1; 
	} 
	if (tecla == 8) { 
		tam = tam - 1; 
	} 
	if (tecla == 8 || tecla == 88 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105) { 
		if (tam <= 2) { 
			strCampo.value = vr; 
		} 
		if ((tam > 2) && (tam <= 6)) { 
			strCampo.value = vr.substr(0, tam - 2) + '-' + vr.substr(tam - 2, tam); 
		} 
		if ((tam >= 7) && (tam <= 9)) { 
			strCampo.value = vr.substr(0, tam - 6) + '/' + vr.substr(tam - 6, 4) + '-' + vr.substr(tam - 2, tam); 
		} 
		if ((tam >= 10) && (tam <= 12)) { 
			strCampo.value = vr.substr(0, tam - 9) + '.' + vr.substr(tam - 9, 3) + '/' + vr.substr(tam - 6, 4) + '-' + vr.substr(tam - 2, tam); 
		} 
		if ((tam >= 13) && (tam <= 14)) { 
			strCampo.value = vr.substr(0, tam - 12) + '.' + vr.substr(tam - 12, 3) + '.' + vr.substr(tam - 9, 3) + '/' + vr.substr(tam - 6, 4) + '-' + vr.substr(tam - 2, tam); 
		} 
		if ((tam >= 15) && (tam <= 17)) { 
			strCampo.value = vr.substr(0, tam - 14) + '.' + vr.substr(tam - 14, 3) + '.' + vr.substr(tam - 11, 3) + '.' + vr.substr(tam - 8, 3) + '.' + vr.substr(tam - 5, 3) + '-' + vr.substr(tam - 2, tam); 
		} 
	} 
} 


// Inicio da mascara p CPF
function FormataCPF(pForm,pCampo,pTeclaPres){
	var wTecla, wVr, wTam, pTamMax;
	pTamMax = 11;
	wTecla = pTeclaPres.keyCode;
	eval("strCampo = document." + pForm + "." + pCampo); 
	wVr = strCampo.value; 
	while (wVr.indexOf("-") != -1) {
		wVr = wVr.replace( "-", "" );
	}
	while (wVr.indexOf(",") != -1) {
		wVr = wVr.replace( ",", "" );
	}
	while (wVr.indexOf(".") != -1) {
		wVr = wVr.replace( ".", "" );
	}
	while (wVr.indexOf("/") != -1) {
		wVr = wVr.replace( "/", "" );
	}
	wTam = wVr.length ;
	if(wTam > pTamMax) { 
		wTam = wVr.length - 1; 
		wVr = wVr.substring(0, wTam);
	}
	if(wTecla == 8 ) { 
		wTam = wTam - 1; 
	}
	if( wTecla == 8 || wTecla == 88 || wTecla >= 48 && wTecla <= 57 || wTecla >= 96 && wTecla <= 105 ){
		if( wTam <= 2 ){
			strCampo.value = wVr;
		}
		if(wTam > 2 && wTam <= 5) {
			wVr = wVr.substring(0, 3) + '.' + wVr.substring(3);
		}
		if(wTam > 5 && wTam <= 8) {
			wVr = wVr.substring(0, 3) + '.' + wVr.substring(3,6) + '.' + wVr.substring(6);
		}
		if(wTam > 8) {
			wVr = wVr.substring(0, 3) + '.' + wVr.substring(3,6) + '.' + wVr.substring(6,9) + '-' + wVr.substring(9);
		}
		strCampo.value = wVr;
	}
}

// Inicio da mascara p RG
function FormataRG(pForm,pCampo,pTeclaPres){
	var wTecla, wVr, wTam, pTamMax;
	pTamMax = 9;
	wTecla = pTeclaPres.keyCode;
	eval("strCampo = document." + pForm + "." + pCampo); 
	wVr = strCampo.value; 
	while (wVr.indexOf("-") != -1) {
		wVr = wVr.replace( "-", "" );
	}
	while (wVr.indexOf(",") != -1) {
		wVr = wVr.replace( ",", "" );
	}
	while (wVr.indexOf(".") != -1) {
		wVr = wVr.replace( ".", "" );
	}
	while (wVr.indexOf("/") != -1) {
		wVr = wVr.replace( "/", "" );
	}
	wTam = wVr.length ;
	if(wTam > pTamMax) { 
		wTam = wVr.length - 1; 
		wVr = wVr.substring(0, wTam);
	}
//	if(wTecla == 8 ) { 
//		wTam = wTam - 1; 
//	}
	if( wTecla == 8 || wTecla == 88 || wTecla >= 48 && wTecla <= 57 || wTecla >= 96 && wTecla <= 105 ){
		if( wTam <= 1 ){
			strCampo.value = wVr;
		}
		if(wTam > 1 && wTam <= 4) {
			wVr = wVr.substring(0, wTam-1) + '-' + wVr.substring(wTam-1);
		}
		if(wTam > 4 && wTam <= 7) {
			wVr = wVr.substring(0, wTam-4) + '.' + wVr.substring(wTam-4,wTam-1) + '-' + wVr.substring(wTam-1);
		}
		if(wTam > 7) {
			wVr = wVr.substring(0, wTam-7) + '.' + wVr.substring(wTam-7,wTam-4) + '.' + wVr.substring(wTam-4,wTam-1) + '-' + wVr.substring(wTam-1);
		}
		strCampo.value = wVr;
	}
}

// Inicio da mascara p/ TELEFONE, CEP e DATAS
function FormataNum(formulario, campo, tipoCampo, tamMax, teclaPres) { 
	var tecla = teclaPres.keyCode; 
	var strCampo; 
	var vr; 
	var tam; 
	eval("strCampo = document." + formulario + "." + campo); 
	vr = strCampo.value; 
	while (vr.indexOf("-") != -1) {
		vr = vr.replace( "-", "" );
	}
	while (vr.indexOf(",") != -1) {
		vr = vr.replace( ",", "" );
	}
	while (vr.indexOf(".") != -1) {
		vr = vr.replace( ".", "" );
	}
	while (vr.indexOf("/") != -1) {
		vr = vr.replace( "/", "" );
	}
	tam = vr.length; 
	if(tam > tamMax) { 
		tam = vr.length - 1; 
		vr = vr.substring(0, tam);
	}
	if (tecla == 8) { 
		tam = tam - 1; 
	} 
	if (tecla == 8 || tecla == 88 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105) { 
		if (tipoCampo == "data") {
			if (tam <= 1) { 
				strCampo.value = vr; 
			} 
			if ((tam > 1) && (tam <= 3)) { 
				strCampo.value = vr.substring(0, 2) + '/' + vr.substring(2); 
			} 
			if (tam > 3) { 
				strCampo.value = vr.substring(0, 2) + '/' + vr.substring(2, 4) + '/' + vr.substring(4); 
			} 
		} else if (tipoCampo == "cep") {
			if (tam <= 4) { 
				strCampo.value = vr; 
			} 
			if (tam > 4) { 
				strCampo.value = vr.substring(0, 5) + '-' + vr.substring(5); 
			} 
		} else if (tipoCampo == "telefone") {
			if (tam <= 3) { 
				strCampo.value = vr; 
			} 
			if (tam > 3) { 
				strCampo.value = vr.substring(0, 4) + '-' + vr.substring(4); 
			} 
		}
	} 
} 
//*********************************************************funçoes MÁSCARAS DE FORMATAÇÃO - fim

//*************************************************************FUNÇÕES DE VALIDAÇÃO - inicio

// validação de qualquer tipo de campo texto: apenas se está preenchido
function valSimples(formulario, campo) {
	d = eval("document." + formulario + "." + campo);
	n = campo.split("_");
	nomecampo = n[1];

	if (d.value=="") {
		alert("O campo " + nomecampo.toUpperCase() + " deve ser preenchido");
		d.focus();
		return false;
	}
	return true;
}	

//validação da FORMA DE PAGAMENTO
function valFormapag(formulario, campo) {
	d = eval("document." + formulario + "." + campo);
	
	if (!d[0].checked && !d[1].checked && !d[2].checked && !d[3].checked) {
		alert("Escolha a FORMA DE PAGAMENTO");
		return false;
	}
	return true;
}



//validação da DATA DE NASCIMENTO
function valNasc(formulario, campo) {
	d = eval("document." + formulario + "." + campo);
	erro=0;
	hoje = new Date();
	anoAtual = hoje.getFullYear();
	barras = d.value.split("/");

	if (barras.length == 3 && d.value.length == 10){
		dia = barras[0];
		mes = barras[1];
		ano = barras[2];
		resultado = (!isNaN(dia) && (dia > 0) && (dia < 32)) && (!isNaN(mes) && (mes > 0) && (mes < 13)) && (!isNaN(ano) && (ano.length == 4) && (ano <= anoAtual && ano >= 1900));
		if (!resultado) {
			alert("DATA DE NASCIMENTO invalida");
			d.focus();
			d.select();
			return false;
		}
	} else {
		alert("DATA DE NASCIMENTO invalida - A data deve estar no formato dd/mm/aaaa");
		d.focus();
		d.select();
		return false;
	}
	return true;
}

//validação do CPF
function valCpf(formulario, campo) {
	d = eval("document." + formulario + "." + campo);
	cpf = d.value;
	while (cpf.indexOf("-") != -1) {
		cpf = cpf.replace( "-", "" );
	}
	while (cpf.indexOf(",") != -1) {
		cpf = cpf.replace( ",", "" );
	}
	while (cpf.indexOf(".") != -1) {
		cpf = cpf.replace( ".", "" );
	}
	while (cpf.indexOf("/") != -1) {
		cpf = cpf.replace( "/", "" );
	}
	valor = true;
	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 com problema!";
	}
	if (erro.length > 0){
		alert(erro);
		d.focus();
		d.select();
		return false;
	}
	return true;
}

//validação do RG
function valRg(formulario, campo) {
	d = eval("document." + formulario + "." + campo);
	n = campo.split("_");
	nomecampo = n[1];

	if (d.value=="") {
		alert("O campo " + nomecampo.toUpperCase() + " deve ser preenchido");
		d.focus();
		return false;
	}
	if (d.value.length <= 8){
		alert("O campo " + nomecampo.toUpperCase() + " é inválido");
		d.focus();
		return false;
	}
	return true;
}	

//validação do SEXO
function valSexo(formulario, campo) {
	d = eval("document." + formulario + "." + campo);
	
	if (!d[0].checked && !d[1].checked) {
		alert("Escolha o SEXO");
		return false;
	}
	return true;
}

// validação do DDD e do TELEFONE
function valTelefone(formulario, campoddd, campotel) {
	dddd = eval("document." + formulario + "." + campoddd);
	dtel = eval("document." + formulario + "." + campotel);

	num = dtel.value;
	while (num.indexOf("-") != -1) {
		num = num.replace( "-", "" );
	}

	if ((dddd.value.length != 2) || (isNaN(dddd.value))) {
		alert("O campo DDD deve ter 2 números");
		dddd.focus();
		dddd.select()
		return false;
	}
	if ((num.length != 8) || (isNaN(num))) {
		alert("O campo TELEFONE deve ter 8 números");
		dtel.focus();
		dtel.select();
		return false;
	}
	return true;
}

// validação do E-MAIL
function valEmail(formulario, campo) {
	d = eval("document." + formulario + "." + campo);
	parte1 = d.value.indexOf("@");
	parte2 = d.value.indexOf(".");
	parte3 = d.value.length;
	err=0;

	if (d.value == "") {
		alert("Informe seu e-mail");
		d.focus();
		return false;
	} else {
		if (parte1 > parte2){
			deparroba = d.value.substring((parte1),(parte3));
			parte2 = parte1 + deparroba.indexOf(".");
		}
		if (!(parte1 >= 2 && parte2 >= (parte1 +  3) && parte3 >= (parte2 + 3))) {
			err=1;
		}
		if((d.value.indexOf("@",parte1 + 1) != -1) ||
			(d.value.indexOf(".") < 1) ||
			(d.value.lastIndexOf(".") < parte1) ||
			(d.value.indexOf(" ") != -1) ||
			(d.value.indexOf("hotmail.com.br") > 0) ||
			(d.value.indexOf(".@") > 0) ||
			(d.value.indexOf("@.") > 0) ||
			(d.value.lastIndexOf(".") == (d.value.length - 1)) ||
			(d.value.indexOf("/") > 0) ||
			(d.value.indexOf("[") > 0) ||
			(d.value.indexOf("]") > 0) ||
			(d.value.indexOf("(") > 0) ||
			(d.value.indexOf(")") > 0) ||
			(d.value.indexOf("..") > 0)) {
			err=1;
		}
	}
	if(err==1) {
			alert("O e-mail informado parece não estar correto");
			d.focus();
			d.select();
			return false;
	}
	return true;
}

// validação do CEP
function valCep(formulario, campo) {
	d = eval("document." + formulario + "." + campo);
	num = d.value;
	while (num.indexOf("-") != -1) {
		num = num.replace( "-", "" );
	}

	if (num == "") {
		alert("O campo CEP deve ser preenchido");
		d.focus();
		return false;
	} else if (num.length != 8 || isNaN(num)) {
		alert("CEP invalido");
		d.focus();
		d.select();
		return false;
	}
	return true;
}

// validação do CNPJ
function valCnpj(formulario, campo) {
	d = eval("document." + formulario + "." + campo);

	if(VerifyCNPJ(d.value) != 1) { 
		alert("CNPJ inválido"); 
		d.focus();
		d.select();
		return false;
	} 
	return true;
}

//validar se concorda com envio de emails
function valAcordo(formulario, campo) {
	d = eval("document." + formulario + "." + campo);
	
	if (!d[0].checked && !d[1].checked) {
		alert("Você precisa deixar indicado se aceita ou não receber e-Mails do Jornal do Cristão");
		return false;
	} 
	return true;
}

//*************************************************************FUNÇÕES DE VALIDAÇÃO - fim


function validaForm(nomeform) {
		if (!valFormapag(nomeform, "ins_formapag") ||
			!valSimples(nomeform, "ins_nome") ||
			!valCpf(nomeform, "ins_cpf") ||
			!valSimples(nomeform, "ins_endereco") ||
			!valRg(nomeform, "ins_rg") ||
			!valSimples(nomeform, "ins_bairro") ||
			!valSexo(nomeform, "ins_sexo") ||
			!valSimples(nomeform, "ins_cidade") ||
			!valNasc(nomeform, "ins_nasc") ||
			!valSimples(nomeform, "ins_uf") ||
			!valEmail(nomeform, "email") ||
			!valCep(nomeform, "ins_cep") ||
			!valTelefone(nomeform, "ins_ddd", "ins_tel")) {
			return false;
		}
	return true;
}


