var ajx = Object;

function _SE_ReceitasTentaLogin(){

	var campos;

	campos = "email=" + document.getElementById( "email" ).value + "&";
	campos = campos + "senha=" + document.getElementById( "senha" ).value;
	
	document.getElementById("loginLoad").style.display = "block";
	document.getElementById("btnAbrir").style.display = "none";
	
	
	criaRequisicao( ajx );
	
	mandaRequisicaoPOST( ajx, "../receitas/ajax-acoes.php?_SE_REC_ACT=LOGIN", campos, true, "_SE_ReceitasTrataLogin" );

}

function _SE_ReceitasTrataLogin(){

	if( ajx.req.readyState == 4 && ajx.req.status == 200 ){

		var msg = "";
		
		document.getElementById("senha").value = "";

		retorno = urldecode( ajx.req.responseText );

		switch( retorno ){
			
			case "0":
				document.getElementById("boxLogado").style.display = "block";
				document.getElementById("boxDeslogado").style.display = "none";
			break;
			
			case "-1":
				msg = "Preencha todos os campos corretamente!";
			break;
			
			case "-2":
				msg = "Login e/ou senha inválidos";
			break;
			
			case "-3":
				msg = "Login e/ou senha inválidos";				
			break;
			
		}
		document.getElementById("retorno").innerHTML = msg;
		document.getElementById("loginLoad").style.display = "none";
		document.getElementById("btnAbrir").style.display = "block";
		
		destroiRequisicao( ajx );
	
	}
	
}

function _SE_ReceitasTentaLogout(){

	criaRequisicao( ajx );
	
	mandaRequisicaoGET( ajx, "../receitas/ajax-acoes.php?_SE_REC_ACT=LOGOUT", true, "_SE_ReceitasTrataLogout" );

}

function _SE_ReceitasTrataLogout(){

	if( ajx.req.readyState == 4 && ajx.req.status == 200 ){

		var msg = "";
		
		retorno = urldecode( ajx.req.responseText );
		
		switch( retorno ){
			
			case "0":
				document.getElementById("boxLogado").style.display = "none";
				document.getElementById("boxDeslogado").style.display = "block";
			break;
			
		}
		
		destroiRequisicao( ajx );
	
	}
	
}

function _SE_ReceitasTentaAdicionar(codigo){
	
	var campos;

	campos = "codigo=" + codigo;	
	
	document.getElementById("menuLoad").style.display = "block";

	criaRequisicao( ajx );
	
	mandaRequisicaoPOST( ajx, "../receitas/ajax-acoes.php?_SE_REC_ACT=ADICIONAR", campos, true, "_SE_ReceitasTrataAdicionar" );

}

function _SE_ReceitasTrataAdicionar(){

	if( ajx.req.readyState == 4 && ajx.req.status == 200 ){

		var msg = "";
		
		retorno = urldecode( ajx.req.responseText );
		
		switch( retorno ){
			
			case "0":
				msg = "Receita adicionada!";				
			break;
			
			case "-1":
				msg = "Essa receita já se encontra no livro!";
			break;
			
		}
		document.getElementById("retorno").innerHTML = msg;
		document.getElementById("menuLoad").style.display = "none";
		
		destroiRequisicao( ajx );
		
		_SE_ReceitasMostraLista();
		_SE_ReceitasTentaListar();
	
	}
	
}

function _SE_ReceitasTentaRemover(codigo){
	
	var campos;

	campos = "codigo=" + codigo;	
	
	document.getElementById("menuLoad").style.display = "block";

	criaRequisicao( ajx );
	
	mandaRequisicaoPOST( ajx, "../receitas/ajax-acoes.php?_SE_REC_ACT=REMOVER", campos, true, "_SE_ReceitasTrataRemover" );

}

function _SE_ReceitasTrataRemover(){

	if( ajx.req.readyState == 4 && ajx.req.status == 200 ){

		var msg = "";
		
		retorno = urldecode( ajx.req.responseText );
		
		switch( retorno ){
			
			case "0":
				msg = "Receita removida!";
			break;
			
			case "-1":
				msg = "Essa receita não se encontra no livro!";
			break;
			
		}
		document.getElementById("retorno").innerHTML = msg;
		document.getElementById("menuLoad").style.display = "none";
		
		destroiRequisicao( ajx );
		
		_SE_ReceitasTentaListar();
	
	}
	
}

function _SE_ReceitasTentaListar(){
	
	document.getElementById("listaLoad").style.display = "block";		
	
	criaRequisicao( ajx );
	mandaRequisicaoGET( ajx, "../receitas/ajax-acoes.php?_SE_REC_ACT=LISTAR", true, "_SE_ReceitasTrataListar" );

}

function _SE_ReceitasTentaListarProximo(){
	
	document.getElementById("listaLoad").style.display = "block";
	
	criaRequisicao( ajx );
	mandaRequisicaoGET( ajx, "../receitas/ajax-acoes.php?_SE_REC_ACT=LISTAR&_D=DIR", true, "_SE_ReceitasTrataListar" );

}


function _SE_ReceitasTentaListarAnterior(){
	
	document.getElementById("listaLoad").style.display = "block";
	
	criaRequisicao( ajx );
	mandaRequisicaoGET( ajx, "../receitas/ajax-acoes.php?_SE_REC_ACT=LISTAR&_D=ESQ", true, "_SE_ReceitasTrataListar" );

}


function _SE_ReceitasTrataListar(){

	if( ajx.req.readyState == 4 && ajx.req.status == 200 ){
		
		retorno = urldecode( ajx.req.responseText );
		
		var arrInfo, pagEsq, pagDir;		
		var arrTemp = retorno.split( ";" );		
		var limite = arrTemp.length - 2;
		var conteudo = "";
		
		arrInfo = arrTemp[0].split( "#" );
		pagEsq  = arrInfo[0];
		pagDir  = arrInfo[1];
		
		for( x = 1; x <= limite; x++ ){
		
			arrInfo = arrTemp[x].split( "#" );
			
			conteudo = conteudo + "<div style='float:left; width:12px; margin-right:5px; margin-bottom:10px;'><img src='../imgs/bolinha-receita.png' style='margin-top:2px;'></div> ";
			conteudo = conteudo + "<div style='float:left; width:149px; margin-bottom:10px;'><a href='../receitas/index.php?codigo=" + arrInfo[0] + "' id='fonte'>" + arrInfo[1] + "</a></div>";			
			
		}
		
		document.getElementById( "area_receitas" ).innerHTML = conteudo;
		
		if(pagEsq == 0)
			document.getElementById( "btnEsquerdo" ).innerHTML = "<img src='../imgs/bot-seta-livro_esq_desativado.png' style='margin-top:1px;' />";
		else
			document.getElementById( "btnEsquerdo" ).innerHTML = "<a href='javascript:_SE_ReceitasTentaListarAnterior();' id='btnEsq'></a>";
			
		if(pagDir == 0)
			document.getElementById( "btnDireito" ).innerHTML = "<img src='../imgs/bot-seta-livro_dir_desativado.png' style='margin-top:1px;' />";
		else
			document.getElementById( "btnDireito" ).innerHTML = "<a href='javascript:_SE_ReceitasTentaListarProximo();' id='btnDir'></a>";	
			
		document.getElementById("listaLoad").style.display = "none";	
			
		destroiRequisicao( ajx );
	
	}
	
}

function _SE_ReceitasMostraPainel(){
	criaRequisicao( ajx );	
	mandaRequisicaoGET( ajx, "../receitas/ajax-acoes.php?_SE_REC_ACT=MUDAR&_P=painel", true, null );
	destroiRequisicao( ajx );
	
	document.getElementById("boxLogado").style.display = "block";
	document.getElementById("boxLista").style.display = "none";
}

function _SE_ReceitasMostraLista(){
	criaRequisicao( ajx );	
	mandaRequisicaoGET( ajx, "../receitas/ajax-acoes.php?_SE_REC_ACT=MUDAR&_P=lista", true, null );
	destroiRequisicao( ajx );
	
	document.getElementById("boxLogado").style.display = "none";
	document.getElementById("boxLista").style.display = "block";
}
