// ------- Submenu -------- //
var lastActiveSubMenu = 'submenu-institucional';
function OpenCloseSubMenu(subMenuId, show)
{
	if (document.getElementById(subMenuId))
	{
		document.getElementById(lastActiveSubMenu).style.display = 'none';
		document.getElementById(subMenuId).style.display = '';
		lastActiveSubMenu = subMenuId;
	}
}

// Calendário
function OnChangeCalendar(periodo)
{
	if (periodo == 'matutino')
	{
		document.getElementById('div-calendario-noturno').style.display = 'none';
		document.getElementById('div-calendario-matutino').style.display = '';
		
		document.getElementById('div-calendario-noturno-legenda').style.display = 'none';
		document.getElementById('div-calendario-matutino-legenda').style.display = '';
	}
	else if (periodo == 'noturno')
	{
		document.getElementById('div-calendario-noturno').style.display = '';
		document.getElementById('div-calendario-matutino').style.display = 'none';
		
		document.getElementById('div-calendario-noturno-legenda').style.display = '';
		document.getElementById('div-calendario-matutino-legenda').style.display = 'none';
	}
}

// -------- Validação -------//
function verifica_form (form)
{
	var campos = "" ;

	for (i=0;i<form.elements.length;i++)
	{

		if(form.elements[i].id.search("ob_") != -1)
		{

			//Resseta a classe do campo
			form.elements[i].className = 'input-text' ;
			
			//Valida Preenchimento
			if(form.elements[i].value.length == 0 || form.elements[i].value == "0")
			{
				campos = campos + "- " + form.elements[i].title + "\n" ;
				form.elements[i].className = 'input-text-erro' ;
			}

			//Valida Campo de E-mail
			if(form.elements[i].value.length != 0 && form.elements[i].id.search("email") != -1)
			{
				campos += valida_email( form.elements[i].value , form.elements[i].title , form.elements[i] ) ;
			}
			
			//Valida Campo de Data
			//if(form.elements[i].value.length != 0 && form.elements[i].id.search("data_") != -1)
			//{
			//	campos += jValidaData( form.elements[i].value , form.elements[i].title , form.elements[i] ) ;
			//}
			
			//Valida Campo de C.P.F
			if(form.elements[i].value.length != 0 && form.elements[i].id.search("cpf") != -1)
			{
				campos += jValidaCPF( form.elements[i].value , form.elements[i].title , form.elements[i] ) ;
			}

			//Valida Campo de C.N.P.J.
			if(form.elements[i].value.length != 0 && form.elements[i].id.search("cnpj") != -1)
			{
				campos += jValidaCNPJ( form.elements[i].value , form.elements[i].title , form.elements[i] ) ;
			}

		} // Fim Campo Obrigatório

	} // For
	
	if (campos.length > 0)
	{
		alert("O preenchimento dos seguintes campos é obrigatório:\n" + campos);
		return false;
	}
	else
	{
		return true;
	}
}

function valida_email ( email , titulo , objeto )
{
	var mensagem = "";

	if (email.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) == -1)
	{
		objeto.className = 'input-text-erro' ;
		mensagem = "- " + titulo + " foi preenchido incorretamente\n";
	}

	return (mensagem);
}

function viewVideo(file,titulo)
{
	videoHTML = '';
	videoHTML += '<object id="MediaPlayer1" CLASSID="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701" standby="Carregando..." type="application/x-oleobject" width="320" height="290">';
	videoHTML += ' <param name="AnimationAtStart" value="true">';
	videoHTML += ' <param name="AutoSize" value="false">';
	videoHTML += ' <param name="AutoStart" value="true">';
	videoHTML += ' <param name="Loop" value="true">';
	videoHTML += ' <param name="FileName" value="'+ file +'">';
	videoHTML += ' <param name="ShowControls" value="true">';
	videoHTML += ' <param name="ShowStatusBar" value="true">';
	videoHTML += ' <param name="TransparentAtStart" value="false">';
	videoHTML += ' <param name="Volume" value="-230">';
	videoHTML += ' <embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" src="'+ file +'" name="MediaPlayer" width="320" height="290" ShowStatusBar="1" autostart="1" showcontrols="1" volume="-230">';
	videoHTML += '</object>'
	
	document.getElementById('divVideo').innerHTML = videoHTML;
	document.getElementById('titleVideo').innerHTML = titulo;
}

/** valida os dados no formulario de acesso ao portal do aluno **/
function studentAcessValidate()
{
	type = document.getElementById("AccessMode");
	
	if(type.value=="A")
	{
		tx_empresa = document.getElementById("form_aluno_empresa");
		tx_ra = document.getElementById("form_aluno_ra");
		tx_senha = document.getElementById("form_aluno_senha");
		
		if(tx_empresa.value!=-1)
		{
			if((tx_ra.value!="RA")&&(tx_ra.value!=""))
			{
				if((tx_senha.value!="Senha")&&(tx_senha.value!=""))
				{
					tx_ra.value = tx_ra.value.toUpperCase();
					return true;
				}
				else
				{
					alert("Informe sua senha!");
					return false;
				}
			}
			else
			{
				alert("Infome seu RA!");
				return false;
			}
		}
		else
		{
			alert("Escolha sua unidade!");
			return false;
		}
	}
	else
	{
		tx_empresa = document.getElementById("form_professor_empresa");
		tx_matricula = document.getElementById("form_professor_matricula");
		tx_senha = document.getElementById("form_professor_senha");
		
		if(tx_empresa.value!=-1)
		{
			if((tx_matricula.value!="MATRICULA")&&(tx_matricula.value!=""))
			{
				if((tx_senha.value!="Senha")&&(tx_senha.value!=""))
				{
					tx_matricula.value = tx_matricula.value.toUpperCase();
					return true;
				}
				else
				{
					alert("Informe sua senha!");
					return false;
				}
			}
			else
			{
				alert("Infome seu número de matrícula!");
				return false;
			}
		}
		else
		{
			alert("Escolha sua unidade!");
			return false;
		}
	}
}

/* altera o modo de acesso de aluno para professor (ou ao contrario) */
function alterAccessMode()
{
	type = document.getElementById("AccessMode");
	
	if(type.value=="A")
	{
		document.images["topo-banner-aluno-titulo"].src = "Images/top_professor_titulo.gif";
		document.images["topo-banner-aluno-subtitulo"].src = "Images/top_aluno_subtitulo.gif";
		document.getElementById("login").style.display = "none";
		document.getElementById("login_professor").style.display = "";
		document.getElementById("link-lembra-senha-aluno").style.display = "none";
		document.getElementById("link-lembra-senha-professor").style.display = "";
		type.value = "P";
	}
	else
	{
		document.images["topo-banner-aluno-titulo"].src = "Images/top_aluno_titulo.gif";
		document.images["topo-banner-aluno-subtitulo"].src = "Images/top_professor_subtitulo.gif";
		document.getElementById("login_professor").style.display = "none";
		document.getElementById("login").style.display = "";
		document.getElementById("link-lembra-senha-professor").style.display = "none";
		document.getElementById("link-lembra-senha-aluno").style.display = "";
		type.value = "A";
	}
}

/* Funcções utilizadas no formulário de inscrição */

var oldDom = null;
			
function swapForm(objectID){
	dom = findDOM('formStack',objectID,1);
	if (oldDom) oldDom.visibility = 'hidden';
	dom.visibility = 'visible';
	oldDom = dom;
	oldObjectID = objectID;
}

//Valida se todos os campos estão preenchidos
function validaForm()
{
	if(document.all.nome.value == false)
	{
		alert("Preencha o campo Nome!");
		document.all.nome.focus();
		return false;
	}
	
	if(document.all.tipo_cpf.value == "")
	{
		alert("Preencha o Tipo de CPF!");
		document.all.tipo_cpf.focus();
		return false;
	}
	
	if(document.getElementById("sexo").value == "")
	{
		window.alert("Preencha o Sexo!");
		document.getElementById("sexo").focus();
		return false;
	}
	
	if(document.getElementById("est_civil").value == "")
	{
		window.alert("Preencha o Estado Civil!");
		document.getElementById("est_civil").focus();
		return false;
	}
	
	if(document.getElementById("dt_nasc").value == "")
	{
		window.alert("Preencha a Data de Nascimento!");
		document.getElementById("dt_nasc").focus();
		return false;
	}
	
	if(document.getElementById("nome_mae").value == "")
	{
		window.alert("Preencha o Nome da Mãe");
		document.getElementById("nome_mae").focus();
		return false;
	}
	
	if(document.getElementById("nome_pai").value == "")
	{
		window.alert("Preencha o Nome do Pai");
		document.getElementById("nome_pai").focus();
		return false;
	}
	
	if(document.getElementById("nacio").value == "")
	{
		window.alert("Preencha a Nacionalidade!");
		document.getElementById("nacio").focus();
		return false;
	}
	
	if(document.getElementById("natural").value == "")
	{
		window.alert("Preencha a Naturalidade!");
		document.getElementById("natural").focus();
		return false;
	}
	
	if(document.all.email.value == "")
	{
		alert("Preencha o campo E-mail!");
		document.all.email.focus();
		return false;
	}
	
	if(document.all.email.value != "")
	{
		//validar email(verificao de endereco eletronico)
		parte1 = document.all.email.value.indexOf("@");
		//parte2 = document.all.email.value.indexOf(".");
		parte3 = document.all.email.value.length;
		//  if (!(parte1 >= 3 && parte2 >= 6 && parte3 >= 9)) {
		if (!(parte1 >= 3 && parte3 >= 9))
		{
			alert("O campo " + document.all.email.name + " deve ser  um endereco eletrônico!");
			document.all.email.focus();
			return false;
		}
	}
	
	if(document.getElementById("rg").value == "")
	{
		window.alert("Preencha o RG!");
		document.getElementById("rg").focus();
		return false;
	}
	
	//ano_concl_emiss
	if(document.getElementById("ano_concl_emiss").value == "")
	{
		window.alert("Preencha o Ano de Conclusão!");
		document.getElementById("ano_concl_emiss").focus();
		return false;
	}
	
	///cep
	if(document.getElementById("cep").value == "")
	{
		window.alert("Preencha o Cep!");
		document.getElementById("cep").focus();
		return false;
	}
	
	if(document.getElementById("endereco").value == "")
	{
		window.alert("Preencha o Endereço!");
		document.getElementById("endereco").focus();
		return false;
	}
	
	if(document.getElementById("numero").value == "")
	{
		window.alert("Preencha o Número do Endereço!");
		document.getElementById("numero").focus();
		return false;
	}
	
	if(document.getElementById("bairro").value == "")
	{
		window.alert("Preencha o Bairro!");
		document.getElementById("bairro").focus();
		return false;
	}
	
	if(document.getElementById("uf").value == "")
	{
		window.alert("Preencha o UF da Cidade!");
		document.getElementById("uf").focus();
		return false;
	}
	
	if(document.getElementById("cidade").value == "")
	{
		window.alert("Preencha a Cidade!");
		document.getElementById("cidade").focus();
		return false;
	}
	
	if (document.all.fone_res.value == "")
	{
		alert("Preencha o campo Telefone!");
		document.all.fone_res.focus();
		return false;
	}
	
	if(document.all.conh_ipep.value == "")
	{
		window.alert("Preencha o campo de como conheceu o IPEP!");
		document.all.conh_ipep.focus();
		return false;
	}
	
	//validar data de nascimento
	erro=0;
	hoje = new Date();
	anoAtual = hoje.getFullYear();
	barras = document.all.dt_nasc.value.split("/");
	if (barras.length == 3)
	{
		dia = barras[0];
		mes = barras[1];
		ano = barras[2];
		resultado = (!isNaN(dia) && (dia > 0) && (dia < 32)) && (!isNaN(mes) && (mes > 0) && (mes < 13)) && (!isNaN(ano) && (ano.length == 4) && (ano <= anoAtual && ano >= 1900));
		if (!resultado)
		{
			alert("Formato de data invalido!");
			document.all.dt_nasc.focus();
			return false;
		}
	}
	else
	{
		alert("Formato de data invalido!");
		document.all.dt_nasc.focus();
		return false;
	}
	
	marcado = -1
	for (i=0; i<form1.rd1op.length; i++) 
	{
		if (form1.rd1op[i].checked)
		{
			marcado = i
			resposta = nform.resp[i].value
		}
	}
	
	if (marcado == -1)
	{
		alert("Selecione o Curso!");
		form1.rd1op[0].focus();
		return false;
	}
	else
	{
		//esse else so foi colocado para evitar que o form desse o submit
		alert("Você selecionou o Curso " + resposta);
		return false;
	}
	
	return true;
}

//adiciona mascara de cep
function MascaraCep(cep)
{
	if(mascaraInteiro(cep)==false)
	{
		event.returnValue = false;
	}
	
	return formataCampo(cep, '00000-000', event);
}

//adiciona mascara de data
function MascaraData(data)
{
	if(mascaraInteiro(data)==false)
	{
		event.returnValue = false;
	}
	
	return formataCampo(data, '00/00/0000', event);
}

//adiciona mascara ao telefone
function MascaraTelefone(tel)
{
	if(mascaraInteiro(tel)==false)
	{
		event.returnValue = false;
	}
	
	return formataCampo(tel, '(00) 0000-0000', event);
}

//adiciona mascara ao CPF
function MascaraCPF(cpf)
{
	if(mascaraInteiro(cpf)==false)
	{
		event.returnValue = false;
	}
	
	return formataCampo(cpf, '000.000.000-00', event);
}

//valida telefone
function ValidaTelefone(tel)
{
	exp = /\(\d{2}\)\ \d{4}\-\d{4}/
	if(!exp.test(tel.value))
		alert('Numero de Telefone Invalido!');
}

//valida CEP
function ValidaCep(cep)
{
	exp = /\d{2}\d{3}\-\d{3}/
	if(!exp.test(cep.value))
		alert('Numero de Cep Invalido!');        
}

//valida data
function ValidaData(data)
{
	exp = /\d{2}\/\d{2}\/\d{4}/
	if(!exp.test(data.value))
		alert('Data Invalida!');            
}

//valida numero inteiro com mascara
function mascaraInteiro()
{
	if (event.keyCode < 48 || event.keyCode > 57)
	{
		event.returnValue = false;
		return false;
	}
	return true;
}

//formata de forma generica os campos
function formataCampo(campo, Mascara, evento)
{
	var boleanoMascara; 
	var Digitato = evento.keyCode;
	exp = /\-|\.|\/|\(|\)| /g
	campoSoNumeros = campo.value.toString().replace( exp, "" ); 

	var posicaoCampo = 0;     
	var NovoValorCampo="";
	var TamanhoMascara = campoSoNumeros.length;; 

	if (Digitato != 8)
	{ // backspace 
		for(i=0; i<= TamanhoMascara; i++)
		{ 
			boleanoMascara  = ((Mascara.charAt(i) == "-") || (Mascara.charAt(i) == ".")
							|| (Mascara.charAt(i) == "/")) 
			boleanoMascara  = boleanoMascara || ((Mascara.charAt(i) == "(") 
							|| (Mascara.charAt(i) == ")") || (Mascara.charAt(i) == " ")) 
			if (boleanoMascara)
			{ 
				NovoValorCampo += Mascara.charAt(i); 
				TamanhoMascara++;
			}
			else
			{
				NovoValorCampo += campoSoNumeros.charAt(posicaoCampo); 
				posicaoCampo++; 
			}
		}
		
		campo.value = NovoValorCampo;
		return true; 
	}
	else
	{
		return true; 
	}
}

function AmpliarFoto(title, url, width, height)
{
	newwindow=window.open('','','width=' + width + ',height=' + height + ',toolbar=no, locationno, directories=no, status=no, menubar=no, scrollbars=no, copyhistory=no, resizable=yes');
	newwindow.document.writeln('<html> <head> <title>' + title + '<\/title> <\/head> <body style=\"margin:0;padding:0\"> <center>');
	newwindow.document.writeln('<img src=' + url + ' title=\"' + title + '\" alt=\"' + title + '\" >');
	newwindow.document.writeln('<\/center> <\/body> <\/html>');
	newwindow.document.close();
}