// JavaScript Document
function checa(boletins, lingua) {
	var linguas = document.forms[0].elements['linguas[]'];
	
	if(!boletins.checked) {
		for ( x=0; x< linguas.length; x++){
			var el = linguas[x];
			el.checked = false;
			el.disabled= true;
		}
	} else {
	
		for ( x=0; x< linguas.length; x++){
			var el = linguas[x];
			if( el.value == lingua) {
				el.checked = true;
			} else {
				el.checked=false;
			}
			el.disabled= false;
		}
		
	}
	
}
function valida() {
	/*if(!validaBoletins()){
		return false;
	}*/
	if(!validaUsuario()) { 
		return false;
	} 
		
	if(!validaEmail()) {
		return false;
	}
	document.forms[0].submit();
}


function validaCurriculo() {
	
	if(!validaUsuario()) { 
		return false;
	} 
		
	if(!validaEmail()) {
		return false;
	}
	document.forms[0].submit();
}

function validaBoletins() {
	b=0;
	var boletins = document.forms[0].boletins;
	var linguas = document.forms[0].elements['linguas[]'];
	if(boletins.checked){
		for(x=0; x < linguas.length; x++){
			var a = linguas[x];
			
			if (a.checked){
				b++;
			}
		}
		if(b==0) {
			alert("escolha um idioma para receber o boletim");
			return false;
		}
		else return true;	
			
	} else {
		return true;
	}
	
	/*
	if (!boletins.checked && portugues.disabled == true) {
		return true;		
	} else	if (portugues.checked || ingles.checked || frances.checked || holandes.checked || espanhol.checked || alemao.checked) { 
			return true;
	} else if (boletins.checked && (!portugues.checked || !ingles.checked || !frances.checked || !holandes.checked || !espanhol.checked || !alemao.checked)){
			alert("Selecione a Lingua");
			return false;
	}
	*/
}

function validaUsuario() {
	var nome = document.forms[0].nome;
	var palavra = nome.value;
	if ( !isNome(palavra)) {
		nome.value = '';
		nome.focus();
		return false;
	} else {
		return true;
	}
}

function isNome(palavra) {
	if (palavra.length < 3) {
		alert ("Preencha o Nome corretamente" );
		return false;		
	}
	
	for (i = 0; i < palavra.lenght; i++) {
		var c = palavra.charAt(i);
			if (isDigit(c))  {
				alert ("O campo Nome não aceita numeros");
				return false;
			}
	}
	
	return true;
}

function validaEmail() {
	var email = document.forms[0].email;
	var palavra = email.value;
	if ( !isEmail(palavra)) {
		email.value = '';
		email.focus();
		return false;
	} else {
		return true;
	}
}

function isEmail(email) {
	if (email.indexOf("@") == -1) {
		alert("Preencha o Email corretamente");
		return false;
	}
	email_back = email.substring(email.indexOf("@")+1);
	
	if (email_back.indexOf(".") == -1){
		alert("Preencha o Email corretamente");
		return false;
	}
	email_end = email_back.substring(email_back.lastIndexOf(".")+1);
	
	if(email_end.length != 2 && email_end.length != 3) {
    	alert("Preencha o Email corretamente");			
		return false;
	}
    
	return true;	
}
