/**
 * @author Adriano Pereira Machado
 */

// Variavel Global para armazenar informa��es sobre os layers utilizados
var aLayers = new Array();

/**
 * Função que declara os Layers(divs) que serão usados pelo script
 */
function setLayers(){
	
	aLayers.push(document.getElementById("dAcONLINE"));			// ID: 0 - Div Online
	aLayers.push(document.getElementById("dAcBIBLIOTECA"));		// ID: 1 - Div Biblioteca
	aLayers.push(document.getElementById("dAcWEBMAIL"));		// ID: 2 - Div Webmail
	aLayers.push(document.getElementById("dAcERRO"));			// ID: 3 - Div Erro
	aLayers.push(document.getElementById("dAcDICAS"));			// ID: 4 - Div Dica
			
}

/**
 * Função para ocultar todos os Layers
 */
function hideAllLayers(){
	
	var iQtdeLayers = aLayers.length;
	
	for (x=0;x<iQtdeLayers;x++)
		aLayers[x].style.display = "none";
	
}

/**
 * Função para revelar um Layer (pelo seu id) e esconder os demais
 * 
 * @param {Integer} iIdLayer
 */
function showLayer(iIdLayer){
	
	var iQtdeLayers = aLayers.length;
	
	for (x = 0; x < iQtdeLayers; x++) {
		
		if(iIdLayer == x)
			aLayers[x].style.display = "block";
		else		
			aLayers[x].style.display = "none";
		
	}
	
}

/**
 * Função de load a ser usado na p�gina
 */
function loadOpAcesso(){
	
	setLayers();
	hideAllLayers();
	
	// Seta a opção DICA (INDEX = 0) como padrão no load da página
	document.getElementById("lSelOpcao").selectedIndex = 0;
	setOpcao(document.getElementById("lSelOpcao"));

}

/**
 * Função que seta os forms de acordo com a op��o escolhida
 * 
 * @param {Object} oListBox
 */
function setOpcao(oListBox){
	
	var opcao = oListBox.value.toUpperCase();
	var acao = "#"; var form; var oForm;
	
	switch(opcao){
		case "AADGPG":
		
			document.location.href = 'http://aadgpg.alfa.br/';
		
//			acao = "http://aadgpg.alfa.br/logar.php?origem=sitealfa";
//			form = "fAcONLINE";
//			showLayer(0);
					
		break;	
		case "AONLINE":
		
			acao = "http://lyceum.alfa.br/lyceump/aonline/middle_logon.asp";
			form = "fAcONLINE";
			showLayer(0);
					
		break;	
		case "CONLINE":

			acao = "http://lyceum.alfa.br/lyceump/conline/middle_logon.asp";		
			form = "fAcONLINE";
			showLayer(0);
												
		break;
		case "DONLINE":
			
			acao = "http://lyceum.alfa.br/lyceump/donline/LogonBody.asp";		
			form = "fAcONLINE";
			showLayer(0);
			
		break;
		case "RONLINE":
			
			acao = "http://lyceum.alfa.br/lyceump/ronline/middle_logon.asp";		
			form = "fAcONLINE";
			showLayer(0);
			
		break;
		case "BIBINTERNET":
		
			acao = "http://www.alfa.br/acervo/biblioteca_s/php/login_usu.php";		
			form = "fAcBIBLIOTECA";
			showLayer(1);

		break;			
		case "WEBMAIL":
		
			acao = "http://www.alfa.br/webmail.php";		
			form = "fAcWEBMAIL";
			showLayer(2);
					
		break;
		default:
			showLayer(4);
			return false;	
	}

	oForm = document.getElementById(form);	
	oForm.action = acao;
	oForm.method = "post";
	oForm.reset();
	
	cHiddenInput(oForm,'opAcao', opcao);
	
}

/**
 * Função para criar campos ocultos em um form
 * 
 * @param {Object} form
 * @param {String} name
 * @param {String} value
 */
function cHiddenInput(form, name, value){
	
	var oOldInput = document.getElementById(name);
	var oInput = document.createElement("input");
			
	oInput.setAttribute('id', name);
	oInput.setAttribute('name', name);
	oInput.setAttribute('value', value);
	oInput.setAttribute('type', 'hidden');
	
	form.appendChild(oInput);
		
	if (oOldInput != null){
		var oOldForm = oOldInput.parentNode;
		oOldForm.removeChild(oOldInput);
	}
	
}

/**
 * Função para validar os formul�rios
 */
function validaForm(){
	
	var opAcao = document.getElementById('opAcao');
	var opcao = opAcao.value;
	
	var oForm = opAcao.parentNode;
	
	if (opAcao == null) {
		showMsgErro("Ocorreu um erro de processamento.\nOpção de serviço não escolhida!");
		window.location.reload();	
	}
	else {
		
		switch(opcao){
			case "AADGPG":
			
				var oMatricula = document.getElementById("ONLINE_Matricula");
				var oSenha = document.getElementById("ONLINE_txtSenha");
					
				cHiddenInput(oForm, "txtMatricula", oMatricula.value);
				cHiddenInput(oForm, "txtSenha", oSenha.value);
				
				if ( !evalMatricula(oMatricula.value, "aluno") ){
					showMsgErro("A matricula informada ("+ oMatricula.value +") &eacute; inv&aacute;lida!");
					oMatricula.select(); oMatricula.focus(); return false;
				}
				
				if ( oSenha.value.length < 1 ){
					showMsgErro("A senha deve ser informada!");
					oSenha.focus(); return false;
				}
					
			break;			
			case "AONLINE":
			

				var oMatricula = document.getElementById("ONLINE_Matricula");
				var oSenha = document.getElementById("ONLINE_txtSenha");
					
				cHiddenInput(oForm, "txtnumero_matricula", oMatricula.value);
				cHiddenInput(oForm, "txtsenha_tac", oSenha.value);
/*
				
				if ( !evalMatricula(oMatricula.value, "aluno") ){
					showMsgErro("A matricula informada ("+ oMatricula.value +") &eacute; inv&aacute;lida!");
					oMatricula.select(); oMatricula.focus(); return false;
				}
				
				if ( oSenha.value.length < 1 ){
					showMsgErro("A senha deve ser informada!");
					oSenha.focus(); return false;
				}

*/
					
			break;	
			case "CONLINE":
			case "DONLINE":
			case "RONLINE":
	
				var oMatricula = document.getElementById("ONLINE_Matricula");
				var oSenha = document.getElementById("ONLINE_txtSenha");
				
				cHiddenInput(oForm, "txtnumero_matricula", oMatricula.value);
				cHiddenInput(oForm, "txtsenha", oSenha.value);
				
				if (opcao == "RONLINE" && !evalMatricula(oMatricula.value, "reitor")){
					showMsgErro("A matricula ("+ oMatricula.value +") &eacute; inv&aacute;lida!");
					oMatricula.select(); oMatricula.focus(); return false;					
				}
							
				if (opcao != "RONLINE" && !evalMatricula(oMatricula.value, "outros") ){
					showMsgErro("A matricula ("+ oMatricula.value +") &eacute; inv&aacute;lida!");
					oMatricula.select(); oMatricula.focus(); return false;
				}
				
				if ( oSenha.value.length < 1 ){
					showMsgErro("A senha deve ser informada!");
					oSenha.focus(); return false;
				}				
										
			break;
			case "BIBINTERNET":
			
				var oMatricula = document.getElementById("BIB_txtMatricula");
				var oSenha = document.getElementById("BIB_txtSenha");				
	
				cHiddenInput(oForm,"flag", "index.php");
				cHiddenInput(oForm,"login", oMatricula.value);
				cHiddenInput(oForm,"password", oSenha.value);
				
				if ( !evalMatricula(oMatricula.value, "outros") ){
					showMsgErro("A matricula ("+ oMatricula.value +") &eacute; inv&aacute;lida!");
					oMatricula.select(); oMatricula.focus(); return false;
				}
				
				if ( oSenha.value.length < 1 ){
					showMsgErro("A senha deve ser informada!");
					oSenha.focus(); return false;
				}					
			
			break;			
			case "WEBMAIL":
			
				var oEmail = document.getElementById("WEB_txtEmail");
				var oSenha = document.getElementById("WEB_txtSenha");
								
				var aEmail = oEmail.value.split("@");
				
				var sUsuario = aEmail[0];
				var sDominio = aEmail[1];
					
				cHiddenInput(oForm,"select_email", sDominio);
				cHiddenInput(oForm,"usuario", sUsuario);
				cHiddenInput(oForm,"senha", oSenha.value);
	
				if ( oEmail.value.length <= 1){
					showMsgErro("O e-mail deve ser informado!");
					oEmail.select(); oEmail.focus(); return false;
				}
	
				if ( !evalEmail(oEmail.value) ){
					showMsgErro("O e-mail informado ("+ oEmail.value +") &eacute; inv&aacute;lido!");
					oEmail.select(); oEmail.focus(); return false;
				}
				
				if ( oSenha.value.length < 1 ){
					showMsgErro("A senha deve ser informada!");
					oSenha.focus(); return false;
				}	
						
			break;
			default:
				showMsgErro("Acao inv&aacute;lida: ("+ opcao +")");
				return false;
		}
		return true;
	}
	return false;
}

/**
 * Função para validação da Matricula
 * 
 * @param {Integer} mat
 * @param {String} tipo
 */
function evalMatricula(mat, tipo){
	
	tipo = tipo.toUpperCase();
	
	switch(tipo){
		case "ALUNO":

/*
			if (mat.length > 11 || mat.length < 10) 
				return false;
			else {
			
				if (mat.length == 10) 
					var pattern = /20([0-1]{1})([0-9]{1})(1|2)([0-9]{2})([0-9]{3})/;
				else 
					var pattern = /20([0-1]{1})([0-9]{1})(1|2)([0-9]{2})(1|3|6)([0-9]{3})/;
				
				if (!pattern.test(mat)) 
					return false;
				else 
					return true;
				
			}
*/
		break;
		case "REITOR":
		
			var pattern = /^([A-Za-z0-9])+$/;

			if (mat.length < 2 || !pattern.test(mat))
				return false;
			else
				return true;
		
		break;
		default:
		
			var pattern = /^([0-9])+$/;
		
			if (isNaN(mat) || mat.length < 2 || !pattern.test(mat))
				return false;
			else
				return true;
	}
	
	return false;
	
}

/**
 * Função para validação do e-mail
 * 
 * @param {String} email
 */
function evalEmail(email){
	
	if (email.length < 8)
		return false;
		
	var aPartEmail = email.split("@");
	
	if (aPartEmail.length != 2)
		return false;
				
	var aDomALFA = new Array("alfa.br", "graduacao.alfa.br", "posgraduacao.alfa.br", "alfajr.alfa.br");
	var iDomCrtl = 0;
	
	for (x = 0; x < aDomALFA.length; x++) {
		if (aDomALFA[x].indexOf(aPartEmail[1]) != -1) 
			iDomCrtl++;
	}
			
	if (iDomCrtl == 0)
		return false;
	
	var pattern = /^([a-z]{1})([a-z0-9\-\_]+)$/;
	
	if (!pattern.test(aPartEmail[0]))
		return false;

	return true;		
}


function showMsgErro(msg){
	
	if (msg.length < 1)
		return false;
	
	aLayers[3].style.display = "block";
	aLayers[3].innerHTML = "<p>"+ msg +"</p>";
	
	return true;
}
