// JavaScript Document
$(document).ready(function(){
	
});


/* VALIDAÇÃO DO FORUMÁRIO DE LOGIN PARA O ADMIN */
function enviarFormLogin()
{
	if (document.form_login.nome_usuario.value == "")
	{
		$("#espaco_alerta").html("<p class=\"alerta\">Informe seu nome de usuário, por favor.</p>");
		document.form_login.nome_usuario.focus();
		return false;
	}
	if (document.form_login.senha_usuario.value == "")
	{
		$("#espaco_alerta").html("<p class=\"alerta\">Informe sua senha, por favor.</p>");
		document.form_login.senha_usuario.focus();
		return false;
	}
	return true;
}



function enviarExercicios()
{
	mostrarCarregando();
	document.formExerciciosUnidade.submit();	
}


/* VALIDAÇÃO DO FORUMÁRIO DE RECUPERAÇÃO DE DADOS */
function enviarRecuperaDados()
{
	if (document.form_recuperar.nome_usuario.value == "" && document.form_recuperar.email_usuario.value == "" )
	{
		$("#espaco_alerta").html("<p class=\"alerta\">Informe pelo menos um dos campos abaixo.</p>");
		document.form_recuperar.nome_usuario.focus();
		return false;
	}
	if ( document.form_recuperar.email_usuario.value != "" && !checkMail(document.form_recuperar.email_usuario.value) )
	{
		$("#espaco_alerta").html("<p class=\"alerta\">Informe um email válido.</p>");
		document.form_recuperar.nome_usuario.focus();
		return false;
	}
	
	$("#espaco_alerta").html("<p class=\"alerta\">PROCESSANDO...</p>");
	return true;
}














/* ********** FUNÇÕES ********** **/
function checkMail(mail)
{
	var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
	if(typeof(mail) == "string")
	{
		if(er.test(mail))
			return true; 
		else
			return false;
	}
	else 
		if(typeof(mail) == "object")
		{
			if(er.test(mail.value))
				return true;
			else
				return false
		}
		else
		{
			return false;
		}
}


function Verifica_CPF(cpf) {
	if (cpf.length != 11 || 
		cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || 
		cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || 
		cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || 
		cpf == "99999999999")
		return false;
	
	add = 0;
	for (i=0; i < 9; i ++)
		add += parseInt(cpf.charAt(i)) * (10 - i);
	rev = 11 - (add % 11);
	if (rev == 10 || rev == 11)
		rev = 0;
	if (rev != parseInt(cpf.charAt(9)))
		return false;
	add = 0;
	for (i = 0; i < 10; i ++)
		add += parseInt(cpf.charAt(i)) * (11 - i);
	rev = 11 - (add % 11);
	if (rev == 10 || rev == 11)
		rev = 0;
	if (rev != parseInt(cpf.charAt(10)))
	return false;
	
	return true;
}
