// JavaScript Document

/**
 * 
 * @return
 */
function inscricoes_validar()
{
	// aguarde
	$('#validar2').html('<img src="imagens/loadinfo.gif" border="0" align="absmiddle" /> <font class="nome_campo"><b>Aguarde... Processando</b></font>');
	
	// valida inscrições
	$.ajax({
		url: "inscricoes.php",
		type: 'post',
		cache: false,
		data: {
			acao_site: 'inscricoes_validar'
		},
		dataType: 'json',
		success: function(retorno)
		{
			if (retorno.valido == 'OK')
			{
				if (retorno.tipo == 'I')
				{
					perguntas_listar();
				}
				else
				{
					inscricao_equipe_revezamento();
				}
			}
			else
			{
				$('#validar2').html('<button type="button" class="botao_voltar" onclick="regulamento_exibir()">Voltar</button><button type="button" class="botao_avancar" onclick="inscricoes_validar()">Avan&ccedil;ar</button>');
				alert(decode(retorno.msg));
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			$('#validar2').html('<button type="button" class="botao_voltar" onclick="regulamento_exibir()">Voltar</button><button type="button" class="botao_avancar" onclick="inscricoes_validar()">Avan&ccedil;ar</button>');
			//alert(textStatus);
		}
	});	
}

/**
 * 
 * @return
 */
function inscricao_equipe_revezamento()
{
	// salva nome da equipe de revezamento
	$.ajax({
		url: "ajax.php",
		type: 'post',
		cache: false,
		data: {
			acao: 'inscricao_equipe_revezamento'
		},
		dataType: 'html',
		success: function(retorno)
		{
			exibe_dialog2('Equipe', retorno, 'OK', 'inscricao_equipe_revezamento_salvar()', 'Cancelar', 'perguntas_listar()', 300, 200);
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			//alert(textStatus);
		}
	});	
}

/**
 * 
 * @return
 */
function inscricao_equipe_revezamento_salvar()
{
	// salva nome da equipe de revezamento
	$.ajax({
		url: "ajax.php",
		type: 'post',
		cache: false,
		data: {
			acao: 'inscricao_equipe_revezamento_salvar',
			equipe_revezamento: $("#equipe_revezamento").val()
		},
		dataType: 'html',
		success: function(retorno)
		{
			perguntas_listar();
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			//alert(textStatus);
		}
	});	
}

/**
 * 
 * @return
 */
function inscricoes_listar()
{
	$("#acao_site").val('inscricoes_listar');
	$("#dados").attr('action', 'inscricoes.php');
	$("#dados").submit();
}

/**
 * 
 * @return
 */
function inscrever_usuario()
{
	// desabilitar botões
	$('#botavanc').attr('disabled', true);
	$('#botvoltar').attr('disabled', true);
	$('#bot_adicionar_amigos').attr('disabled', true);
	
	// aguarde
	$('#validar').html('<img src="imagens/loadinfo.gif" border="0" align="absmiddle" /> <font class="nome_campo"><b>Aguarde... Processando</b></font>');
	
	pos = $('#pos').val();
	
	$.ajax({
		url: "inscricoes.php",
		type: 'post',
		cache: false,
		data: {
			acao_site: 'inscrever_usuario', 
			pos: pos
		},
		dataType: 'json',
		success: function(retorno)
		{
			if (retorno.valido == 'OK')
			{
				$('#pos').val(retorno.pos);
				if (retorno.msg != '')
				{
					//alert(decode(retorno.msg));
					exibe_dialog('Inscrição', decode(retorno.msg), 'inscricoes_listar()', 'inscricoes_listar()');
				}
				else
				{
					inscricoes_listar();
				}
			}	
			else
			{
				// habilitar botões
				$('#botvoltar').removeAttr('disabled');
				$('#botavanc').removeAttr('disabled');
				$('#bot_adicionar_amigos').removeAttr('disabled');
				
				$('#validar').html('');
				//alert(decode(retorno.msg));
				exibe_dialog('Inscrição', decode(retorno.msg), 'fecha_dialog()', 'fecha_dialog()');
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			$('#validar').html('');
			alert(textStatus);
		}
	});	
}

/**
 * 
 * @param doc
 * @return
 */
function inscricao_excluir(doc)
{
	$('#validar').html('<img src="imagens/loadinfo.gif" border="0" align="absmiddle" /> <font class="nome_campo"><b>Aguarde... Processando</b></font>');
	$('#botavanc').attr('disabled', true);
	$('#botvoltar').attr('disabled', true);
	$('#bot_adicionar_amigos').attr('disabled', true);
	$('#botao_inscrever_usuario').attr('disabled', true);
	
	$.ajax({
		url: "ajax.php",
		type: 'post',
		cache: false,
		data: {acao: 'inscricao_excluir', doc: doc},
		dataType: 'json',
		success: function(retorno)
		{
			$('#pos').val(retorno.pos);
			inscricoes_listar();
			
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			$('#botvoltar').removeAttr('disabled');
			$('#botavanc').removeAttr('disabled');
			$('#bot_adicionar_amigos').removeAttr('disabled');
			$('#botao_inscrever_usuario').removeAttr('disabled');
			$('#validar').html('');
			alert(textStatus);
			
		}
	});	
	
}

/**
 * 
 * @return
 */
function inscricoes_salvar()
{
	// valida forma de pgto
	var forma_pgto = $("input[name=forma_pgto]:checked");
	if (forma_pgto.length == 0)
	{
		var forma_pgto = $('#forma_pgto').val();
	}
	else
	{
		var forma_pgto = forma_pgto.val();
	}
	
	$.ajax({
		url: "inscricoes.php",
		type: 'post',
		cache: false,
		data: {
			acao_site: 'inscricoes_salvar', 
			forma_pgto: forma_pgto, 
			modo: 'site'
		},
		dataType: 'json',
		success: function(retorno)
		{
			$("#id_pedido").val(retorno.id_pedido);
			pgto_efetivar();
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			//alert(textStatus);
		}
	});	
}

/**
 * 
 * @param id_pedido
 * @return
 */
function abre_inscricoes(id_pedido)
{
	var elem = $("#tr_inscricoes"+id_pedido)[0];
	if(elem.style.display == 'none')
	     $("#tr_inscricoes"+id_pedido).show();
	else
	{
	     $("#tr_inscricoes"+id_pedido).hide();                       
	}

}

/**
 * 
 * @param doc
 * @return
 */
function inscricao_codigo_net_dialog(doc)
{
	$.ajax({
		url: "inscricoes.php",
		type: 'post',
		cache: false,
		data: {
			acao_site: 'inscricao_codigo_net_dialog'
		},
		dataType: 'html',
		success: function(retorno)
		{
			exibe_dialog2("Código NET", decode(retorno), "OK", "inscricao_codigo_net_validar("+doc+")", "Cancelar", "fecha_dialog()");
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			//alert(textStatus);
		}
	});	
}

/**
 * 
 * @param doc
 * @return
 */
function inscricao_codigo_net_validar(doc)
{
	$.ajax({
		url: "inscricoes.php",
		type: 'post',
		cache: false,
		data: {
			acao_site: 'inscricao_codigo_net_validar',
			codigo_net: $("#codigo_net").val(),
			doc: doc
		},
		dataType: 'json',
		success: function(retorno)
		{
			if (retorno.valido == '')
			{
				exibe_dialog_erro("Código NET", decode(retorno.msg), "OK", "fecha_dialog_erro()", null, null);
			}
			else
			{
				inscrever_usuario();
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			//alert(textStatus);
		}
	});	
}

/**
 * 
 */
function inscricao_categoria_selecionar()
{
	var id_categoria = $('input[@name=id_categoria]:checked').val();
	if (!id_categoria)
	{
		alert('Selecione uma categoria');
		return;
	}
	$('#validar').html('<img src="imagens/loadinfo.gif" border="0" align="absmiddle" /> <font class="nome_campo"><b>Aguarde... Processando</b></font>');
	$.ajax({
		url: "inscricoes.php",
		type: 'post',
		cache: false,
		data: {
			acao_site: 'inscricao_categoria_selecionar', 
			id_categoria: id_categoria
		},
		dataType: 'json',
		success: function(retorno)
		{
			if (retorno.valido == 'OK')
			{
				redirect('index.php?acao_site=regulamento_exibir');
			}
			else
			{
				alert(decode(retorno.msg));
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{
			alert(textStatus);
		}
	});	
}

/**
 * 
 * @param id_evento
 */
function continuar_comprando(id_evento)
{
	$("#acao_site").val("modalidades_listar");
	$("#id_evento").val(id_evento);
	$("#dados").submit();
}
