cnome = ("a|á|â|ã|b|c|ç|d|e|é|ê|f|g|h|i|í|j|k|l|m|n|o|ó|ô|õ|p|q|r|s|t|u|ú|ü|v|w|x|y|z| |.|-");
cnome = cnome.split("|");

numeros = ("0|1|2|3|4|5|6|7|8|9");
numeros = numeros.split("|");

mail = ("a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|0|1|2|3|4|5|6|7|8|9|.|-|_|@");
mail = mail.split("|");


document.getElementById("DVD").onsubmit = function() {
	return valida_FormDVD();
}

function valida_FormDVD(){
	nome = document.getElementById("nome").value;
	end = document.getElementById("endereco").value;
	bairro = document.getElementById("bairro").value;
	cidade = document.getElementById("cidade").value;
	estado = document.getElementById("estado").value;
	cep = document.getElementById("cep").value;
	ddd = document.getElementById("ddd").value;
	tel = document.getElementById("telefone").value;
	email = document.getElementById("email").value;
//	comp = document.getElementById("comprovante").value;

	resp = "";

	if (nome.length < 3) {
		resp += "Preencha corretamente o campo \"Nome\"<br />";
	}
	else {
		valida_Nome(nome);
	}
	if (end.length < 5) {
		resp += "Preencha corretamente o campo \"Endere&ccedil;o\"<br />";
	}
	if (bairro.length < 2) {
		resp += "Preencha corretamente o campo \"Bairro\"<br />";
	}
	if (cidade.length < 2) {
		resp += "Preencha corretamente o campo \"Cidade\"<br />";
	}
	if (estado == "") {
		resp += "Selecione um item no campo \"Estado\"<br />";
	}
	if (cep.length != 8) {
		resp += "Preencha corretamente o campo \"CEP\"<br />";
	}
	else {
		valida_CEP(cep);
	}
	if (ddd.length < 2 || tel.length < 7) {
		resp += "Preencha corretamente o campo \"Telefone\"<br />";
	}
	else {
		valida_Telefone(ddd,tel);
	}
	if (email.length < 8) {
		resp += "Preencha corretamente o campo \"E-mail\"<br />";
	}
	else {
		valida_Email(email);
	}

	if (resp != "") {
		document.getElementById("adv").style.display = "block";
		document.getElementById("adv").innerHTML = resp;
//		alert(resp);
		return(false);
	}
	else if (comp == "") {
		if (!confirm("Você não anexou comprovante ao formulário. \nPara voltar e anexar um comprovante clique \"Cancelar\". \nPara continuar clique \"Ok\".")) {
			return(false);
		}
		else {
			return(true);
		}
	}
//	return(true);
}

function valida_Nome(nome) {
	nome = nome.toLowerCase();
	for (i=0;i<nome.length;i++) {
		y = 0;
		for (k=0;k<cnome.length;k++) {
			if (nome.charAt(i) != cnome[k]) {
				y = y + 1;
			}
		}
		if (y != cnome.length - 1) {
			resp += "Preencha corretamente o campo \"Nome\"<br />";
		}
	}
	return(true);
}

function valida_CEP(cep) {
	for (i=0;i<cep.length;i++) {
		y = 0;
		for (k=0;k<numeros.length;k++) {
			if (cep.charAt(i) != numeros[k]) {
				y = y + 1;
			}
		}
		if (y != numeros.length - 1) {
			resp += "Preencha corretamente o campo \"CEP\"<br />";
		}
	}
	return(true);
}

function valida_Telefone(ddd,telefone) {
	tel = ddd + telefone;
	if (ddd.length == 3) {
		if (ddd.charAt(0) != "0" || ddd.charAt(1) == "0" || ddd.charAt(2) == "0") {
			resp += "Preencha corretamente o campo \"Telefone\"<br />";
			return(false);
		}
	}
	if (telefone.length == 8) {
		if (telefone.charAt(0) == "0" || telefone.charAt(0) == "1") {
			resp += "Preencha corretamente o campo \"Telefone\"<br />";
			return(false);
		}
	}
	for (i=0;i<tel.length;i++) {
		y = 0;
		for (k=0;k<numeros.length;k++) {
			if (tel.charAt(i) != numeros[k]) {
				y = y + 1;
			}
		}
		if (y != numeros.length - 1) {
			resp += "Preencha corretamente o campo \"Telefone\"<br />";
		}
	}
	return(true);
}

function valida_Email(x){
	// Verifica se os caracteres são válidos.
	for (k=0;k<x.length;k++){
		y = 0;
		for (i=0;i<mail.length;i++){
			if (x.charAt(k) != mail[i]){
				y += 1;
			}
		}
		if (y >= mail.length){
			resp += "Preencha corretamente o campo \"E-mail\"<br />";
			return(false);
		}
	}
	// Verifica se o e-mail começa com os caracteres @ (arroba), . (ponto), - (traço ou hífen) e _ (underline).
	if (x.charAt(0) == '@' || x.charAt(0) == '.' || x.charAt(0) == '-' || x.charAt(0) == '_'){
		resp += "Preencha corretamente o campo \"E-mail\"<br />";
		return(false);
	}
	// Verifica se tem apenas um @ e a sua posição.
	if (x.indexOf('@') < 2 || x.indexOf('@') > (x.length - 6)){
		resp += "Preencha corretamente o campo \"E-mail\"<br />";
		return(false);
	}
	cont = 0;
	for (i=0;i<x.length;i++){
		if (x.charAt(i) == "@"){
			cont += 1;
		}
	}
	if (cont != 1){
		resp += "Preencha corretamente o campo \"E-mail\"<br />";
		return(false);
	}
	// Verifica se tem ao menos um ponto depois do @ e a sua posição.
	ponto = x.lastIndexOf('.');
	arroba = x.indexOf('@');
	if (x.length < ponto + 3 || x.length > ponto + 5 || ponto < arroba + 3){
		resp += "Preencha corretamente o campo \"E-mail\"<br />";
		return(false);
	}
	// Verifica se tem ponto antes ou depois do @.
	if (x.charAt(x.indexOf('@')-1) == '.' || x.charAt(x.indexOf('@')-1) == '-' || x.charAt(x.indexOf('@')-1) == '_' || x.charAt(x.indexOf('@')+1) == '.' || x.charAt(x.indexOf('@')+1) == '-' || x.charAt(x.indexOf('@')+1) == '_'){
		resp += "Preencha corretamente o campo \"E-mail\"<br />";
		return(false);
	}
	return(true);
}

/*	document.getElementById("changeimg").onclick = function(){
		document.getElementById("imagem").src = "captcha/imagem.asp?i=" + Math.random();
		return(false);
	}*/
