var localAtivo = "";

function clicarServicoPainel(id) {
	localAtivo = "painel";

	var servicoID = eval("document.formServico.servico_" + id + ".value");

	if (servicoID == 0) {
		document.getElementById("BOTAO_contratar_" + id).value = "Contratar Serviço";
		document.getElementById("BOTAO_contratar_" + id).acao = "contratar";

		if (eval("document.formServico.quantidade_" + id + ".value > 0")) {
			quantidade = eval("document.formServico.quantidade_" + id + ".value");
			eval("document.formServico.quantidadeContratando_" + id + ".value = " + quantidade);
			eval("document.formServico.quantidade_" + id + ".value = " + quantidade);

		}

	} else if (servicoID == 1) {
		eval("document.formServico.quantidadeContratando_" + id + ".value = document.formServico.quantidade_" + id + ".value");
		document.getElementById("BOTAO_contratar_" + id).value = "Cancelar Serviço";
		document.getElementById("BOTAO_contratar_" + id).acao = "cancelar";

	}

	if (eval("document.formServico.servicoAbriuDescricao_" + id + ".value == 1")) {
		eval("document.formServico.servicoAbriuDescricao_" + id + ".value = 0");
		document.getElementById("TABLE_descricaoServico_" + id).style.display = "none";

	} else if (eval("document.formServico.servicoAbriuDescricao_" + id + ".value == 0")) {
		eval("document.formServico.servicoAbriuDescricao_" + id + ".value = 1");
		document.getElementById("TABLE_descricaoServico_" + id).style.display = "block";

	}

}

function clicarServicoSite(id) { // É a mesma Função acima, mas essa funciona no site, onde não tem o <form>
	localAtivo = "site";

	var servicoID = document.getElementById("servico_" + id).value;

	if (servicoID == 0) {
		document.getElementById("BOTAO_contratar_" + id).value = "Contratar Serviço";
		document.getElementById("BOTAO_contratar_" + id).acao = "contratar";

		if (document.getElementById("quantidade_" + id).value > 0) {
			quantidade = document.getElementById("quantidade_" + id).value;
			document.getElementById("quantidadeContratando_" + id).value = quantidade;
			document.getElementById("quantidade_" + id).value = quantidade;

		}

	} else if (servicoID == 1) {
		document.getElementById("quantidadeContratando_" + id).value = document.getElementById("quantidade_" + id).value;
		document.getElementById("BOTAO_contratar_" + id).value = "Cancelar Serviço";
		document.getElementById("BOTAO_contratar_" + id).acao = "cancelar";

	}

	if (document.getElementById("servicoAbriuDescricao_" + id).value == 1) {
		document.getElementById("servicoAbriuDescricao_" + id).value = 0;
		document.getElementById("TABLE_descricaoServico_" + id).style.display = "none";

	} else if (document.getElementById("servicoAbriuDescricao_" + id).value == 0) {
		document.getElementById("servicoAbriuDescricao_" + id).value = 1;
		document.getElementById("TABLE_descricaoServico_" + id).style.display = "block";

	}

}

function verificarServico(id, acao) {
	if (acao == "contratar") {
		if (localAtivo == "painel") {
			contratarServicoPainel(id);

		} else if (localAtivo == "site") {
			contratarServicoSite(id);

		}

	} else if (acao == "cancelar") {
		if (localAtivo == "painel") {
			cancelarServicoPainel(id);

		} else if (localAtivo == "site") {
			cancelarServicoSite(id);

		}

	}

}

function cancelarServicoPainel(id) {
	document.getElementById("servicoLabel_" + id).style.color = document.getElementById("servicoLabel_" + id).corInativa;
	document.getElementById("servicoTD_" + id).style.backgroundColor = document.getElementById("servicoLabel_" + id).backInativo;

	eval("document.formServico.servico_" + id + ".value = '0'");

	document.getElementById("servicoImagem_" + id).style.filter = document.getElementById("servicoImagem_" + id).servicoInativo;

	eval("document.formServico.quantidadeContratando_" + id + ".value = 0");
	eval("document.formServico.quantidade_" + id + ".value = 0");

	clicarServicoPainel(id);
	contarServicosAtivos(0);

}

function cancelarServicoSite(id) {
	document.getElementById("servicoLabel_" + id).style.color = document.getElementById("servicoLabel_" + id).corInativa;
	document.getElementById("servicoTD_" + id).style.backgroundColor = document.getElementById("servicoLabel_" + id).backInativo;

	document.getElementById("servico_" + id).value = 0;

	document.getElementById("servicoImagem_" + id).style.filter = document.getElementById("servicoImagem_" + id).servicoInativo;

	document.getElementById("quantidadeContratando_" + id).value = 0;
	document.getElementById("quantidade_" + id).value = 0;

	clicarServicoSite(id);
	contarServicosAtivos(0);

}

function contratarServicoPainel(id) {
	document.getElementById("servicoLabel_" + id).style.color = document.getElementById("servicoLabel_" + id).corAtiva;
	document.getElementById("servicoTD_" + id).style.backgroundColor = document.getElementById("servicoLabel_" + id).backAtivo;

	eval("document.formServico.servico_" + id + ".value = '1'");

	document.getElementById("servicoImagem_" + id).style.filter = document.getElementById("servicoImagem_" + id).servicoAtivo;


	quantidade = eval("document.formServico.quantidadeContratando_" + id + ".value");
	if (quantidade == 0) {
		quantidade = 1;

	} else if (quantidade > 0) {
		quantidade = eval("document.formServico.quantidadeContratando_" + id + ".value");

	} else {
		alert("A quantidade deve ser preenchida com um número real!!");

	}

	if (quantidade > 0) {
		eval("document.formServico.quantidadeContratando_" + id + ".value = " + quantidade);
		eval("document.formServico.quantidade_" + id + ".value = " + quantidade);

	}

	clicarServicoPainel(id);
	contarServicosAtivos(0);

}

function contratarServicoSite(id) { // É a mesma Função acima, mas essa funciona no site, onde não tem o <form>
	document.getElementById("servicoLabel_" + id).style.color = document.getElementById("servicoLabel_" + id).corAtiva;
	document.getElementById("servicoTD_" + id).style.backgroundColor = document.getElementById("servicoLabel_" + id).backAtivo;

	document.getElementById("servico_" + id).value = 1;

	document.getElementById("servicoImagem_" + id).style.filter = document.getElementById("servicoImagem_" + id).servicoAtivo;


	quantidade = document.getElementById("quantidadeContratando_" + id).value;
	if (quantidade == 0) {
		quantidade = 1;

	} else if (quantidade > 0) {
		quantidade = document.getElementById("quantidadeContratando_" + id).value;

	} else {
		alert("A quantidade deve ser preenchida com um número real!!");

	}

	if (quantidade > 0) {
		document.getElementById("quantidadeContratando_" + id).value = quantidade;
		document.getElementById("quantidade_" + id).value = quantidade;

	}

	clicarServicoSite(id);
	contarServicosAtivos(0);

}

function contarServicosAtivosPainel() {
	localAtivo = "painel";
	contarServicosAtivos(1);

}

function contarServicosAtivos(load) {
	var servicos;
	var servicosLength;

	if (localAtivo == "painel") {
		servicos = document.formServico.servicosAtivos;
		servicosLength = servicos.length;

	} else if (localAtivo == "site") {
		servicosLength = document.getElementById("servicosAtivosTotal").value;

	}

	var valorTotal = 0;
	var contColor = 0;
	var servicoAddData = "";
	var quantidadeAdd = 0;

	for (i = 0; i < servicosLength; i++) {
		if (localAtivo == "painel") {
			id = servicos[i].value;
			valor = servicos[i].servicoValor;

		} else if (localAtivo == "site") {
			id = document.getElementById("servicosAtivos_" + i).value;
			valor = document.getElementById("servicosAtivos_" + i).servicoValor;

		}


		try {
			if (localAtivo == "painel") {
				servicoAddData = eval("document.formServico.servicoAdd_" + id);

			} else if (localAtivo == "site") {
				servicoAddData = document.getElementById("servicoAdd_" + id);

			}

		} catch(e) {
			servicoAddData = "";

		}

		if (localAtivo == "painel") {
			servicoAtivo = eval("document.formServico.servico_" + id + ".value");

		} else if (localAtivo == "site") {
			servicoAtivo = document.getElementById("servico_" + id).value;

		}

		if (servicoAtivo == 1) {
			if (load == 1) {
				try {
					quantidade = servicoAddData.length;
					if (localAtivo == "painel") {
						quantidade = servicoAddData.length;

					} else if (localAtivo == "site") {
						try {
							quantidade = servicoAddData;
							quantidade = 1;

						} catch(e) {
						}

					}

				} catch(e) {
					quantidade = 0;

				}

				if (typeof quantidade == "undefined") {
					try {
						quantidade = servicoAddData.value;
						quantidade = 1;

					} catch(e) {
						quantidade = 0;

					}

				}

				if (localAtivo == "painel") {
					eval("document.formServico.quantidadeContratando_" + id + ".value = " + quantidade);
					eval("document.formServico.quantidade_" + id + ".value = " + quantidade);

				} else if (localAtivo == "site") {
					document.getElementById("quantidadeContratando_" + id).value = quantidade;
					document.getElementById("quantidade_" + id).value = quantidade;

				}

			}

			if (localAtivo == "painel") {
				eval("quantidadeAdd = document.formServico.quantidade_" + id + ".value");

			} else if (localAtivo == "site") {
				eval("quantidadeAdd = document.getElementById('quantidade_" + id + "').value");

			}

			quantidadeAdd = parseInt(quantidadeAdd);

			valor = parseInt(valor);
			valor = valor * quantidadeAdd;

			valorTotal += valor;
			if (contColor % 2) { 
				bgcolor = "#FFFFFF";

			} else {
				bgcolor = "#EEEEFF";

			}

			document.getElementById("TR_servico_" + id).style.display = "block";
			document.getElementById("TR_servico_" + id).style.backgroundColor = bgcolor;

			contColor++;

		} else {
			document.getElementById("TR_servico_" + id).style.display = "none";

		}

	}

	if (valorTotal == 0) {
		document.getElementById("TR_servico_none").style.display = "block";

	} else {
		document.getElementById("TR_servico_none").style.display = "none";

	}

	document.getElementById("B_servico_total").innerText = valorTotal;

}

function contratar() {
	var formServico = document.formServico;

	var servicoSelecionado = 0;

	for (i = 0; i < formServico.length; i++) {
		if (formServico[i].name.substring(0, 8) == "servico_" && formServico[i].value == 1) {
			servicoSelecionado++;

		}

	}

	if (servicoSelecionado == 0) {
		alert("Você deve selecionar pelo menos um serviço para gerenciar sua conta!");

	} else if (servicoSelecionado > 0) {
		formServico.submit();

	} else {
		alert("Houve um erro ao processar o pedido.\r\nPor favor, entre em contato com o Atendimento LojaCom e nos informe deste erro!");

	}

}

function confirmar() {
	formServico.submit();

}

function voltarIndex() {
	formServico.action = "index.php";
	formServico.submit();

}