window.onload = initForm;

function initForm() {
	document.getElementById("eusou").selectedIndex = 0;
	
	populaAssunto();
	
	document.getElementById("eusou").onchange = populaAssunto;
	document.getElementById("assuntoemail").onchange = selecionaMensagem;
	
		for (var i=0; i< document.forms.length; i++) {
		document.forms[i].onsubmit = function() {return validForm();}
	}
}

function populaAssunto() {
	apagaMensagem();
	document.getElementById("assuntoemail").options.length = 0;

	if (document.getElementById("eusou").selectedIndex == 0) {
		var destinosEmail = new Array("Críticas, sugestões ou elogios","Dúvidas para cadastro, vagas ou problemas de inscrição","Informações Gerais","ABRIL - Programa Trainee 2011","AES BRASIL - Programa de Trainees 2011","ALE - Programa Trainee 2010","ALLIANZ - Programa de Estágio 2010","BANCO VOTORANTIM - Programa de Formação 2010","BOSCH - Programa de Estágio 2011","BRADESCO SAÚDE - Programa Jovens Profissionais 2010","BRMALLS - Programa Trainee 2010","CEMAR - Programa de Trainee 2011","CYRELA - Formação Executiva de Analista de Negócios","CYRELA - Programa de Estágio 2010","DHL - Programa Trainee 2010","EATON - PROGRAMA DE ESTÁGIO 2010 (MOGI MIRIM/SP)","EATON - PROGRAMA DE ESTÁGIO 2010 (VALINHOS/SP)","ESTÁCIO - Programa Trainee 2010","ERICSSON - Programa de Trainee 2010","FURUKAWA - Programa de Trainee 2011","GRUPO COMOLATTI - Programa Novos Talentos Comolatti 2010","GRUPO LIBRA - Programa Trainee 2010","GVT - Programa Educar - Gerente de Negócios Corporativos 2010","HENEL 2010 - Programa de estágio Julho/2010","HSBC - Programa de Estágio 2010","ICTS - Programa Novos Talentos 2010","KPMG - Programa de Trainees 2010","KRAFT - Joining Talents Estagiários 2011","LANXESS - Programa de Estágio 2010","LOUIS DREYFUS - Programa Trainee 2011","MONSANTO - Programa de Estágio 2010","MONSANTO - Programa Semeando Futuro","MRV ENGENHARIA - Programa Caça Talentos 2010","MUDAR - Programa de Estágio","MULTDIA - Programa Trainee 2010","NOVARTIS - Programa Jovens Talentos 2º semestre 2010","OXITENO - Programa de Estágio 2011","PHILIPS - GENERATION Philips 2010","PHILIPS - Processo Seletivo de Estágio 2011","POSITIVO INFORMÁTICA - Programa Jovens Profissionais - Estágio","POSITIVO INFORMÁTICA - Programa Jovens Profissionais - Trainee","PROMON ENGENHARIA - Programa de Novos Talentos 2011","PROMON LOGICALIS - Programa de Estágio 2011","RECKITT BENCKISER - Posições de Estágio","RHODIA - Programa de Estágio 2011","ROMI - Programa de Trainee 2011","SANOFI-AVENTIS - Programa Jovens Talentos 2010","SIEMENS - Programa de Desenvolvimento de Talentos 2011","SYNGENTA - Programa de Estágio 2010","TEMPO - Programa Trainee 2010","TIGRE - Programa Trainee 2010","TIM - Programa Talentos sem Fronteiras","VEYANCE TECHNOLOGIES - Programa de Estágio 2010","XP INVESTIMENTOS - Programa XP Investimentos");
		var destinosValue = new Array("ombudsperson@grupofoco.com.br","grupofoco@grupofoco.com.br","grupofoco@grupofoco.com.br","traineeabril2011@focotalentos.com.br","aesbrasil2011@focotalentos.com.br","ale2010@focotalentos.com.br","allianz2010@focotalentos.com.br","bv2010@focotalentos.com.br","bosch2011@focotalentos.com.br","bradescosaude2010@focotalentos.com.br","brmalls2010@grupofoco.com.br","cemar2011@focotalentos.com.br","cyrela2010@focotalentos.com.br","cyrelaestagio2010@focotalentos.com.br","dhl@grupofoco.com.br","eaton2010@focotalentos.com.br","eatonvalinhos@focotalentos.com.br","estacio2010@focotalentos.com.br","ericssontrainee2010@grupofoco.com.br","traineefurukawa2011@focotalentos.com.br","grupocomolatti2010@grupofoco.com.br","traineelibra2010@focotalentos.com.br","programaeducargvt@grupofoco.com.br","henkelestagio2010@focotalentos.com.br","hsbc2010@grupofoco.com.br","icts2010@focotalentos.com.br","kpmg2010@focotalentos.com.br","kraft2011@focotalentos.com.br","lanxess2010@focotalentos.com.br","talentos@grupofoco.com.br","monsanto2010@grupofoco.com.br","semeandofuturo@focotalentos.com.br","mrv2010@focotalentos.com.br","estagiomudar@focotalentos.com.br","multdia2010@focotalentos.com.br","talentos@grupofoco.com.br","oxiteno2011@focotalentos.com.br","generationphilips2010@focotalentos.com.br","estagiophilips2011@focotalentos.com.br","positivoinformaticaestagio@grupofoco.com.br","positivoinformaticatrainee@grupofoco.com.br","promonengenharia2011@focotalentos.com.br","promonlogicalis2011@focotalentos.com.br","reckittbenckiser@focotalentos.com.br","rhodia2011@focotalentos.com.br","traineeromi2011@focotalentos.com.br","sanofiaventis2010@focotalentos.com.br","siemenspdt2011@focotalentos.com.br","syngenta2010@grupofoco.com.br","tempo2010@focotalentos.com.br","tigre2010@grupofoco.com.br","talentostim@focotalentos.com.br","veyance2010@grupofoco.com.br","xpi@grupofoco.com.br");
		if(document.getElementById("Empresa")) { document.getElementById("formMailO").removeChild(document.getElementById("Empresa")); }
	} else 
	{
		var destinosEmail = new Array("Seleção de Executivos","Seleção de Middle Management","Seleção de Trainees e Estagiários","Mão-de-Obra Temporária","Integração empresa-escola","Informações, críticas, sugestões ou elogios");
		var destinosValue = new Array("grupofoco@grupofoco.com.br","grupofoco@grupofoco.com.br","grupofoco@grupofoco.com.br","grupofoco@grupofoco.com.br","grupofoco@grupofoco.com.br","ombudsperson@grupofoco.com.br");
  	mostraNovoCampo("Empresa");
		}
	
	for (var i=0; i < destinosEmail.length;i++){
		addOption(document.form.assuntoemail, destinosEmail[i],destinosValue[i]);
		}
}

function addOption(selectbox,text,value )
{
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
selectbox.options.add(optn);
}

function selecionaMensagem() {
	if (document.getElementById("eusou").selectedIndex == 0) {
		if(document.getElementById("Empresa")) { document.getElementById("formMailO").removeChild(document.getElementById("Empresa")); }
	} else {
		mostraNovoCampo("Empresa");
	}
}
	
function apagaMensagem() {
	if (document.getElementById("mensagemEmail")) {
	document.getElementById("conteudoInterna").removeChild(document.getElementById("mensagemEmail"));
	}
}

function mostraMensagem(mensagem) {
	var divInserido = document.createElement('div');
		divInserido.id = "mensagemEmail";
		document.getElementById("conteudoInterna").insertBefore(divInserido, document.getElementById("formularioEmail"));
		document.getElementById("mensagemEmail").innerHTML = mensagem;
}

function mostraNovoCampo(rotulo) {
	var pInserido = document.createElement('p');
	  pInserido.id = "formMailO";
	var inputInserido = document.createElement('input');
		inputInserido.id = rotulo;
		document.getElementById("formularioEmail").insertBefore(pInserido, document.getElementById("formMail2"));
		document.getElementById("formMailO").innerHTML = "<span></span>";
		document.getElementById("formMailO").appendChild(inputInserido);
		inputInserido.setAttribute('class','reqd');
		inputInserido.setAttribute('maxlength',50);
		inputInserido.type = "text";
		inputInserido.setAttribute('name',rotulo);
		inputInserido.setAttribute('value','Insira aqui o nome da empresa...');
}

function validForm() {
	var allGood = true;
	var allTags = document.getElementsByTagName("*");

	for (var i=0; i<allTags.length; i++) {
		if (!validTag(allTags[i])) {
			allGood = false;
		}
	}
	return allGood;

	function validTag(thisTag) {
		var outClass = "";
		var allClasses = thisTag.className.split(" ");
	
		for (var j=0; j<allClasses.length; j++) {
			outClass += validBasedOnClass(allClasses[j]) + " ";
		}
	
		thisTag.className = outClass;
	
		if (outClass.indexOf("invalid") > -1) {
			invalidLabel(thisTag.parentNode);
			thisTag.focus();
			if (thisTag.nodeName == "INPUT") {
				thisTag.select();
			}
			return false;
		}
		return true;
		
		function validBasedOnClass(thisClass) {
			var classBack = "";
		
			switch(thisClass) {
				case "":
				case "invalid":
					break;
				case "reqd":
					if (allGood && thisTag.value == "") {
						classBack = "invalid ";
					}
					classBack += thisClass;
					break;
				case "radio":
					if (allGood && !radioPicked(thisTag.name)) {
						classBack = "invalid ";
					}
					classBack += thisClass;
					break;
				case "email":
					if (allGood && !validEmail(thisTag.value)) {
						classBack = "invalid ";
					}
					classBack += thisClass;
					break;
				default:
					/*if (allGood && !crossCheck(thisTag,thisClass)) {
						classBack = "invalid ";
					}*/
					classBack += thisClass;
			}
			return classBack;
		}
				
		function crossCheck(inTag,otherFieldID) {
			if (!document.getElementById(otherFieldID)) {
				return false;
			}
			return (inTag.value == document.getElementById(otherFieldID).value);
		}
		
		function radioPicked(radioName) {
			var radioSet = "";

			for (var k=0; k<document.forms.length; k++) {
				if (!radioSet) {
					radioSet = document.forms[k][radioName];
				}
			}
			if (!radioSet) return false;
			for (k=0; k<radioSet.length; k++) {
				if (radioSet[k].checked) {
					return true;
				}
			}
			return false;
		}
				
		function validEmail(email) {
			var invalidChars = "çãõáéíóúäëïöü'!#$%¨&*()+=[{]}/?;:>,<\| ";
		
			if (email == "") {
				return false;
			}
			for (var k=0; k<invalidChars.length; k++) {
				var badChar = invalidChars.charAt(k);
				if (email.indexOf(badChar) > -1) {
					return false;
				}
			}
			var atPos = email.indexOf("@",1);
			if (atPos == -1) {
				return false;
			}
			if (email.indexOf("@",atPos+1) != -1) {
				return false;
			}
			var periodPos = email.indexOf(".",atPos);
			if (periodPos == -1) {	
				return false;
			}
			if (periodPos+3 > email.length)	{
				return false;
			}
			return true;
		}
		
		function invalidLabel(parentTag) {
			if (parentTag.nodeName == "LABEL") {
				parentTag.className += " invalid";
			}
		}
	}
}

