/************************************* FUNÇÕES *******************************************/

/* Função de login */
function enviar() {
var usuario = document.getElementById("login").value;
var senha = document.getElementById("pass").value;
abre('result_login', 'login.php?usuario='+usuario+'&senha='+senha, '1')
}

/* Carrega produtos por categoria e paginação */
function carrega_produtos (categoria_cd,paginacao) {
abre('produtos', 'lista_produtos.php?categoria_cd='+categoria_cd+'&paginacao='+paginacao, '2')
}

/* Carrega receitas por codigo */
function carrega_receita (receita_cd) {
abre('receitas', 'lista_receita.php?receita_cd='+receita_cd,2);
}

/* Carrega ficha dos pedidos finalizados */
function carrega_ficha(pedido_cd) {
abre('minhaconta', 'pedidosfinalizados_ficha.php?pedido_cd='+pedido_cd,2);
}

/* Carrega onde encontrar por cidade */
function carrega_ondeencontrar (cidade_cd) {
abre('onde_encontrar', 'lista_onde_encontrar.php?cidade_cd='+cidade_cd,2);
}

/* Pagina minha conta */
function minha_conta(pagina) {
abre('minhaconta', 'lista_minhaconta.php?pagina='+pagina,2);
}

function minhaconta_alterarsenha(obj) {
  if (obj.checked) {
    document.getElementById("senha").disabled = false;
    document.getElementById("repita").disabled = false;
  } else {
    document.getElementById("senha").disabled = true;
    document.getElementById("repita").disabled = true;
  }
}

/* Habilida campo quantidade */
function habilita (obj) {
var codigo = obj.value;

	var temp = codigo.split("x");
	var valor_cd = temp[0];
	var produto_cd = temp[1];

	if (obj.checked == true) {
	eval("document.getElementById('quant_"+obj.value+"').disabled = false;");
	eval("document.getElementById('quant_"+obj.value+"').value = 1;");
	eval("document.getElementById('quant_"+obj.value+"').focus();");
	} else {
	eval("document.getElementById('quant_"+obj.value+"').disabled = true;");
	eval("document.getElementById('quant_"+obj.value+"').value = '';");
	}
}

function enviar_cesta (produto_cd) {

		var elementos = eval("document.form_" +produto_cd+ ".elements.length");
		var checados = 0;
		var embalagem = "";
		
		for (var i=0;i<elementos;i++) {
		var x = eval("document.form_" +produto_cd+ ".elements["+i+"]");

			if ((x.type == "checkbox") && (x.checked == true) && (x.disabled == false)) {
			
			var temp = x.value.split("x");
			var valor_cd = temp[1];
			
			checados = 1;
				
			    if (eval("document.form_" +produto_cd+".quant_"+x.value+".value") == "") {
						alert("Informe a Quantidade!");
						eval("document.form_" +produto_cd+".quant_"+x.value+".focus();");
						return false;
			    	} else {
					  if (x.value.indexOf("x") == -1)
					  embalagem = embalagem +"0"+"x"+eval("document.form_" +produto_cd+".quant_"+x.value+".value")+",";
					  else
					  embalagem = embalagem +valor_cd+"x"+eval("document.form_" +produto_cd+".quant_"+x.value+".value")+",";
					  
					  eval("document.getElementById('img_"+x.value+"').style.display = '';");
					  eval("document.getElementById('quant_"+x.value+"').disabled = true;");
					  eval("document.getElementById('"+x.value+"').disabled = true;");
					}
			}
		}
		
		if (checados == 0) {
		alert('Selecione a embalagem que deseja comprar!');
		return false;
		}
		
		embalagem = embalagem.substr(0,embalagem.length - 1);
		abre('loga', 'cesta.php?produto_cd='+produto_cd+'&embalagens='+embalagem, '1');
		
}

function atualiza_carrinho(acao,cesta_cd,cesta_quantidade) {
if (cesta_quantidade != undefined)
var quant = cesta_quantidade.value;
else
var quant = "";

	if (acao == "excluir") {
		if (window.confirm (' Deseja realmente excluir este item do carrinho? ')) {
		abre('carrinho', 'calcula_carrinho.php?acao='+acao+'&cesta_cd='+cesta_cd, '2');
		abre('loga', 'cesta.php', '1');
		return true;
		}
	}

	if (quant != "") {
			abre('carrinho', 'calcula_carrinho.php?acao='+acao+'&cesta_cd='+cesta_cd+'&cesta_quantidade='+quant, '2');
			abre('loga', 'cesta.php', '1');
	} else {
		alert ("Preencha o campo quantidade!");
		cesta_quantidade.focus();
		return false;		
	}
	
}

function atualiza_carrinho_enter(acao,cesta_cd,cesta_quantidade,event) {
var quant = cesta_quantidade.value;
var keycode;

	keycode = event.keyCode;
	if (keycode == "13") {
		if (quant != "") {
			abre('carrinho', 'calcula_carrinho.php?acao='+acao+'&cesta_cd='+cesta_cd+'&cesta_quantidade='+quant, '2');
			abre('loga', 'cesta.php', '1');
		} else {
			cesta_quantidade.focus();
			return false;		
		}
	}
}

function esvaziar_carrinho(pedido_cd,cliente_cd,acao) {

	if (window.confirm (' Deseja realmente esvaziar o carrinho de compras? ')) {
		abre('carrinho', 'calcula_carrinho.php?acao=esvaziar&cliente_cd='+cliente_cd+'&pedido_cd='+pedido_cd, '2');
		abre('loga', 'cesta.php', '1');
		return true;
	}
		
}

function inteiro(int) {
	var er = /[^0-9]/;

	if (er.test (int.value)) {
		//alert ("Informe valores inteiros!");
		int.value = "";
		int.focus();
	}

	if (int.value == 0) {
		int.value = "";
		int.focus();  
  }
  
}

function valida_receita() {

  if (document.formulario.receita_nome.value == "") {
    alert('Campo nome da receita é de preenchimento obrigatório!');
    document.formulario.receita_nome.focus();
    return false;
  }

  if (document.formulario.receita_ingredientes.value == "") {
    alert('Campo ingredientes é de preenchimento obrigatório!');
    document.formulario.receita_ingredientes.focus();
    return false;
  }

  if (document.formulario.receita_preparo.value == "") {
    alert('Campo modo de preparo é de preenchimento obrigatório!');
    document.formulario.receita_preparo.focus();
    return false;
  }

  if (document.formulario.receita_foto.value == "") {
    alert('Campo foto é de preenchimento obrigatório!');
    document.formulario.receita_foto.focus();
    return false;
  }

return true;
}
