// JavaScript Document
function carrega_menu(acao)
{
	$.ajax({
		url: "ajax.php",
		type: 'post',
		cache: false,
		data: {acao: 'carrega_menu'},
		dataType: 'json',
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			//alert(textStatus);
		},
		success: function(retorno)
		{
			if (acao == 'home')
			{
				home_exibir();
			}
			else
			{
				$("#menu").html(decode(retorno.html));
			}
		}
	});	
}

/**
 * 
 */
function valida_login()
{
	$.ajax({
		url: "login.php",
		type: 'post',
		cache: false,
		data: {
				acao_site: 'valida_login'
		},
		dataType: 'json',
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			alert(textStatus);
		},
		success: function(retorno)
		{
			if (retorno.valido == 'OK')
			{
				regulamento_exibir();
			}
			else
			{
				login_exibir();
			}
		}
	});	
}

/**
 * 
 */
function login_exibir()
{
	$("#acao_site").val('login_exibir');
	$("#dados").action = 'login.php';
	$("#dados").submit();
}

/**
 * executa o login do usuário
 */
function login()
{
	campos = new Array();
	campos = put_element_MM(campos, 'login_email', 'Email', 'R');
	campos = put_element_MM(campos, 'login_senha', 'Senha', 'R');
	MM_validateForm(campos);
	
	if (document.MM_returnValue)
	{
		if ($("#redir"))
		{
			var redir = $("#redir").val();
		}
		else
		{
			var redir = '';
		}
		$('#validar').html('<img src="imagens/loadinfo.gif" border="0" align="absmiddle" /> <font class="nome_campo"><b>Aguarde... Processando</b></font>');
		$.ajax({
			url: "login.php",
			type: 'post',
			cache: false,
			data: {
				acao_site: 'login_exec', 
				login_email: $("#login_email").val(), 
				login_senha: $("#login_senha").val(), 
				redir: redir
			},
			dataType: 'json',
			error: function(XMLHttpRequest, textStatus, errorThrown)
			{
				alert(textStatus);
			},
			success: function(retorno)
			{				
				// login válido
				if (retorno.valido == 'OK')
				{
					// sem redirecionamento
					if (retorno.redir == '')
					{
						// campos de cadastro OK
						if (retorno.campos == "OK")
						{
							home_exibir();
						}
						// campos de cadastro incompletos
						else
						{
							// exibe dialog 
							exibe_dialog2(
								'Cadastro', 
								'Existem campos incompletos em seu cadastro.<br>Clique em OK para completá-los.', 
								'OK', 
								'cadastro_editar(\'\')', 
								'Cancelar', 
								'home_exibir();', 
								null, 
								null
							);
						}
					}
					// com redirecionamento
					else
					{
						// campos de cadastro OK
						if (retorno.campos == "OK")
						{
							window.location = retorno.redir;
						}
						// campos de cadastro incompletos
						else
						{
							// exibe dialog 
							exibe_dialog2(
								'Cadastro', 
								'Existem campos incompletos em seu cadastro.<br>Clique em OK para completá-los.', 
								'OK', 
								'cadastro_editar(\'\')', 
								'Cancelar', 
								'redirect(\''+retorno.redir+'\')', 
								null, 
								null
							);
						}
					}
				}
				// login inválido
				else
				{
					$('#validar').html('');
					alert(decode(retorno.msg));
				}
			}
		});	
	}
}

/**
 * 
 */
function esqueci_senha()
{
	campos = new Array();
	campos = put_element_MM(campos, 'login_usuario', 'Usuário', 'R');
	MM_validateForm(campos);
	
	if (document.MM_returnValue)
	{
		$.ajax({
			url: "login.php",
			type: 'post',
			cache: false,
			data: {
				acao_site: 'esqueci_senha', 
				login_email: $("#login_email").val()
			},
			dataType: 'json',
			success: function(retorno)
			{
				alert(decode(retorno.msg));
			},
			error: function(XMLHttpRequest, textStatus, errorThrown)
			{
				//alert(textStatus);
			}
		});	
	}
}

/**
 * 
 */
function logout()
{
	$.ajax({
		url: "login.php",
		type: 'post',
		cache: false,
		data: {
			acao_site: 'logout'
		},
		dataType: 'text',
		success: function(retorno)
		{
			carrega_menu('home');
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			//alert(textStatus);
		}
	});	
}

