var ref = "";
var parent = "";
var tr = document.createElement("tr");
var td = document.createElement("td");

function insereLinha(pos, msg) {
	var cria_p;
	ref = document.getElementsByTagName("tr")[pos];
	parent = ref.parentNode;
	cria_p = "<p class=\"alerta\" style=\"width:520px;margin:0px\">";
	cria_p += msg;
	cria_p += "</p>";
	td.innerHTML = cria_p;	
	td.setAttribute('colSpan', '4');
	tr.appendChild(td);	
	parent.insertBefore(tr, ref.nextSibling);		
}
function removeLinha() {
	if (td.parentNode != null) {
		var indice = td.parentNode.parentNode.rowIndex;
		td.parentNode.parentNode.parentNode.deleteRow(indice);
		
	}
}
function valida() {
	$("exalunos").onsubmit = function() {
		var indice = 0;
		if ($("nome").value == "") {
			indice = $("nome").parentNode.parentNode.rowIndex;
			insereLinha(indice, "Preencha o campo Nome completo");
			$("nome").style.backgroundColor = "#fdf9e5";
			$("nome").focus();
			return false;
		} else {
			removeLinha();
			$("nome").style.backgroundColor = "#fff";
		}
		if ($("inicio_graduacao").value == "") {
			indice = $("inicio_graduacao").parentNode.parentNode.rowIndex;
			insereLinha(indice, "Preencha o campo Ano de início da graduação");
			$("inicio_graduacao").style.backgroundColor = "#fdf9e5";
			$("inicio_graduacao").focus();
			return false;
		} else {
			removeLinha();
			$("inicio_graduacao").style.backgroundColor = "#fff";
		}
		if ($("conclusao").value == "") {
			indice = $("conclusao").parentNode.parentNode.rowIndex;
			insereLinha(indice, "Preencha o campo conclusão");
			$("conclusao").style.backgroundColor = "#fdf9e5";
			$("conclusao").focus();
			return false;
		} else {
			removeLinha();
			$("conclusao").style.backgroundColor = "#fff";
		}
		if ($("endereco").value == "") {
			indice = $("endereco").parentNode.parentNode.rowIndex;
			insereLinha(indice, "Preencha o campo Endereço");
			$("endereco").style.backgroundColor = "#fdf9e5";
			$("endereco").focus();
			return false;
		} else {
			removeLinha();
			$("endereco").style.backgroundColor = "#fff";
		}
		if ($("numero").value == "") {
			indice = $("numero").parentNode.parentNode.rowIndex;
			insereLinha(indice, "Preencha o campo Número");
			$("numero").style.backgroundColor = "#fdf9e5";
			$("numero").focus();
			return false;
		} else {
			removeLinha();
			$("numero").style.backgroundColor = "#fff";
		}
		if ($("bairro").value == "") {
			indice = $("bairro").parentNode.parentNode.rowIndex;
			insereLinha(indice, "Preencha o campo Bairro");
			$("bairro").style.backgroundColor = "#fdf9e5";
			$("bairro").focus();
			return false;
		} else {
			removeLinha();
			$("bairro").style.backgroundColor = "#fff";
		}
		if ($("cidade").value == "") {
			indice = $("cidade").parentNode.parentNode.rowIndex;
			insereLinha(indice, "Preencha o campo Cidade");
			$("cidade").style.backgroundColor = "#fdf9e5";
			$("cidade").focus();
			return false;
		} else {
			removeLinha();
			$("cidade").style.backgroundColor = "#fff";
		}
		if ($("cep").value == "") {
			indice = $("cep").parentNode.parentNode.rowIndex;
			insereLinha(indice, "Preencha o campo CEP");
			$("cep").style.backgroundColor = "#fdf9e5";
			$("cep").focus();
			return false;
		} else {
			removeLinha();
			$("cep").style.backgroundColor = "#fff";
		}
		if ($("tel_res").value == "") {
			indice = $("tel_res").parentNode.parentNode.rowIndex;
			insereLinha(indice, "Preencha o campo Telefone residencial");
			$("tel_res").style.backgroundColor = "#fdf9e5";
			$("tel_res").focus();
			return false;
		} else {
			removeLinha();
			$("tel_res").style.backgroundColor = "#fff";
		}
		if ($("email").value == "") {
			indice = $("email").parentNode.parentNode.rowIndex;
			insereLinha(indice, "Preencha o campo E-mail corretamente");
			$("email").style.backgroundColor = "#fdf9e5";
			$("email").focus();
			return false;
		} else {
			removeLinha();
			$("email").style.backgroundColor = "#fff";
		}
		if ($("informacoes_sim").checked == false && $("informacoes_nao").checked == false) {
			indice = $("informacoes_sim").parentNode.parentNode.rowIndex;
			insereLinha(indice, "Informe se você gostaria de receber informações da USJT");			
			$("informacoes_sim").focus();
			return false;
		} else {
			removeLinha();
		} if ($("informacoes_sim").checked == true && $("debates").checked == false && $("cursos_aperfeicoamento").checked == false && $("cursos_pos").checked == false && $("atividades_extensionais").checked == false) {
			indice = $("atividades_extensionais").parentNode.parentNode.rowIndex;
			insereLinha(indice, "Selecione pelo menos uma das opções acima");
			$("informacoes_sim").focus();
			return false;
		} else {
			removeLinha();
		} 
		if ($("faculdade").value == "") {
			indice = $("faculdade").parentNode.parentNode.rowIndex;
			insereLinha(indice, "Selecione a qual faculdade pertence o seu curso de graduação") 
			$("faculdade").focus();
			$("faculdade").style.backgroundColor = "#fdf9e5";
			return false;
		} else {
			removeLinha();
			$("faculdade").style.backgroundColor = "#fff";
		}
		if ($("empregado").checked == false && $("desempregado").checked == false) {
			indice = $("empregado").parentNode.parentNode.rowIndex;
			insereLinha(indice, "Informe se você está Empregado(a) ou Desempregado(a)");
			$("faculdade").focus();			
			return false;
		} else {
			removeLinha();
		}
		if ($("empregado").checked == true && $("empregador").checked == false && $("outra").checked == false) {
			indice = $("empregador").parentNode.parentNode.rowIndex;
			insereLinha(indice, "Informe se você é Empregador");
			$("faculdade").focus();			
			return false;
		} else {
			removeLinha();
		}
		if ($("empregado").checked == true && $("depende").checked == false && $("independe").checked == false) {
			indice = $("depende").parentNode.parentNode.rowIndex;
			insereLinha(indice, "Informe se a função exercida por você é resultante da sua graduação na USJT");
			$("faculdade").focus();			
			return false;
		} else {
			removeLinha();
		}
		if ($("empregado").checked == true && $("tipo_empresa").value == "") {
			indice = $("tipo_empresa").parentNode.parentNode.rowIndex;
			insereLinha(indice, "Informe se a empresa onde você exerce sua função é Privada, Pública ou Particular");
			$("tipo_empresa").focus();
			$("tipo_empresa").style.backgroundColor = "#fdf9e5";
			return false;
		} else {
			removeLinha();
			$("tipo_empresa").style.backgroundColor = "#fff";
		}
		if ($("empregado").checked == true && $("renda").value == "") {
			indice = $("renda").parentNode.parentNode.rowIndex;
			insereLinha(indice, "Preencha o campo Renda mensal");
			$("renda").focus();
			$("renda").style.backgroundColor = "#fdf9e5";
			return false;
		} else {
			removeLinha();
			$("renda").style.backgroundColor = "#fff";
		}		
		if ($("empregado").checked == true && ($("atividade_01").value == "")) {
			indice = $("tempo_03").parentNode.parentNode.rowIndex;
			insereLinha(indice, "Informe quais as atividades mais importantes exercidas por você");
			$("atividade_01").focus();
			$("atividade_01").style.backgroundColor = "#fdf9e5";
			return false;
		} else {
			removeLinha();
			$("atividade_01").style.backgroundColor = "#fff";
		}
		if ($("empregado").checked == true && ($("tempo_01").value == "")) {
			indice = $("tempo_03").parentNode.parentNode.rowIndex;
			insereLinha(indice, "Informe há quanto tempo você exerce esta função em meses");
			$("tempo_01").focus();
			$("tempo_01").style.backgroundColor = "#fdf9e5";
			return false;
		} else {
			removeLinha();
			$("tempo_01").style.backgroundColor = "#fff";
		}
		if ($("empregado").checked == true && ($("atividade_02").value == "")) {
			indice = $("tempo_03").parentNode.parentNode.rowIndex;
			insereLinha(indice, "Informe quais as atividades mais importantes exercidas por você");
			$("atividade_02").focus();
			$("atividade_02").style.backgroundColor = "#fdf9e5";
			return false;
		} else {
			removeLinha();
			$("atividade_02").style.backgroundColor = "#fff";
		}
		if ($("empregado").checked == true && ($("tempo_02").value == "")) {
			indice = $("tempo_03").parentNode.parentNode.rowIndex;
			insereLinha(indice, "Informe há quanto tempo você exerce esta função em meses");
			$("tempo_02").focus();
			$("tempo_02").style.backgroundColor = "#fdf9e5";
			return false;
		} else {
			removeLinha();
			$("tempo_02").style.backgroundColor = "#fff";
		}
		if ($("empregado").checked == true && ($("atividade_03").value == "")) {
			indice = $("tempo_03").parentNode.parentNode.rowIndex;
			insereLinha(indice, "Informe quais as atividades mais importantes exercidas por você");
			$("atividade_03").focus();
			$("atividade_03").style.backgroundColor = "#fdf9e5";
			return false;
		} else {
			removeLinha();
			$("atividade_03").style.backgroundColor = "#fff";
		}
		if ($("empregado").checked == true && ($("tempo_03").value == "")) {
			indice = $("tempo_03").parentNode.parentNode.rowIndex;
			insereLinha(indice, "Informe há quanto tempo você exerce esta função em meses");
			$("tempo_03").focus();
			$("tempo_03").style.backgroundColor = "#fdf9e5";
			return false;
		} else {
			removeLinha();
			$("tempo_03").style.backgroundColor = "#fff";
		}
		if ($("empregado").checked == true && ($("articulacao_sim").checked == false && $("articulacao_nao").checked == false)) {
			indice = $("conhecimentos_gerais_sim").parentNode.parentNode.rowIndex;
			insereLinha(indice, "Escolha Sim ou Não para os campos acima");
			$("articulacao_sim").focus();
			return false;
		} else {
			removeLinha();
		}
		if ($("empregado").checked == true && ($("facilidade_sim").checked == false && $("facilidade_nao").checked == false)) {
			indice = $("conhecimentos_gerais_sim").parentNode.parentNode.rowIndex;
			insereLinha(indice, "Escolha Sim ou Não para os campos acima");
			$("articulacao_sim").focus();
			return false;			
		} else {
			removeLinha();
		}
		if ($("empregado").checked == true && ($("conhecimentos_necessarios_sim").checked == false && $("conhecimentos_necessarios_nao").checked == false)) {
			indice = $("conhecimentos_gerais_sim").parentNode.parentNode.rowIndex;
			insereLinha(indice, "Escolha Sim ou Não para os campos acima");
			$("conhecimentos_necessarios_sim").focus();
			return false;			
		} else {
			removeLinha();
		}
		if ($("empregado").checked == true && ($("conhecimentos_gerais_sim").checked == false && $("conhecimentos_gerais_nao").checked == false)) {
			indice = $("conhecimentos_gerais_sim").parentNode.parentNode.rowIndex;
			insereLinha(indice, "Escolha Sim ou Não para os campos acima");
			$("conhecimentos_gerais_sim").focus();
			return false;			
		} else {
			removeLinha();
		}
		if ($("conjunto_disciplinas").checked == false && $("bibliografia").checked == false && $("grupo_professores").checked == false && $("conteudo_conhecimentos").checked == false) {
			indice = $("conteudo_conhecimentos").parentNode.parentNode.rowIndex;
			insereLinha(indice, "Selecione pelo menos uma das opções acima");
			$("conjunto_disciplinas").focus();
			return false;
		} else {
			removeLinha();
		}
		if ($("dificuldades_sim").checked == false && $("dificuldades_nao").checked == false) {
			indice = $("dificuldades_sim").parentNode.parentNode.rowIndex;
			insereLinha(indice, "Informe se você encontrou dificuldades no exercício da função que o curso deveria ter sanado");
			$("dificuldades_sim").focus();
			return false;
		} else {
			removeLinha();
		}
		if ($("quais").value == "") {
			indice = $("quais").parentNode.parentNode.rowIndex;
			insereLinha(indice, "Informe quais dificuldades você encontrou");
			$("quais").focus();
			$("quais").style.backgroundColor = "#fdf9e5";
			return false;
		} else {
			removeLinha();
			$("quais").style.backgroundColor = "#fff";
		}
	}
}
window.onload = function() {
	valida();
}
