// JavaScript Document
//*********************************************************funçoes MÁSCARAS DE FORMATAÇÃO - inicio
// 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 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;
}

//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 ");
		return false;
	} 
	return true;
}

//*************************************************************FUNÇÕES DE VALIDAÇÃO - fim


function validaForm(nomeform) {
	if (!valSimples(nomeform, "cont_nome") ||
		!valSimples(nomeform, "cont_site") ||
		!valEmail(nomeform, "email") ||
		!valSimples(nomeform, "cont_cidade") ||
		!valSimples(nomeform, "cont_sistema") ||
		!valTelefone(nomeform, "cont_ddd", "cont_tel")){
		return false;
	}
	return true;
}


