// This script contains functions required by various Funko-related modifications to phpBB3.
// Copyright 2009 Funatic Store

// This section contains functions for the Funatic FUN Club enrollment MOD

function getFee() {
	var userShip = document.getElementById('ship').value;
	var userFee = 24.95;
	var userTax = 0.00;
	var userShipping = 5.00;
	var userCard = 0;
	for (i=0; i < document.club.card.length; i++)
	{
		if (document.club.card[i].checked)
		{
			userCard = document.club.card[i].value;
		}
	}
	switch(userShip)
	{
	case "0":
		userFee = 0.00;
		break;
	case "1":
		userTax = 1.80;
		break;
	case "2":
		break;
	case "3":
		userShipping = 10.00;
		break;
	default:
		userShipping = 15.00;
	}
	userTotal = userFee + userTax + userShipping;
	if (userFee > 0)
	{
		displayFee = "$" + userFee.toFixed(2);
		if (userTax > 0)
		{
			displayFee = displayFee + " + $" + userTax.toFixed(2) + " tax";
		}
		if (userShipping > 0)
		{
			displayFee = displayFee + " + $" + userShipping.toFixed(2) + " shipping & handling";
		}
		if (userTotal > userFee)
		{
			displayFee = displayFee + " = <b>$" + userTotal.toFixed(2);
		}
		else
		{
			displayFee = "<b>" + displayFee;
		}
		displayFee = displayFee + " (US)</b>";
	}
	else
	{
		displayFee = "<b>not determined</b>";
	}
	document.getElementById('fee').innerHTML = displayFee;
	document.getElementById('hdnAmount').value = userFee;
	document.getElementById('hdnShipping').value = userShipping;
	document.getElementById('hdnTax').value = userTax;
	document.getElementById('hdnCard').value = userCard;
}

function confirmClub() {
	var msg = "";
	var errFound = false;
	if (document.getElementById('hdnCard').value == 0)
	{
		msg = "card design";
		errFound = true;
	}
	if (document.getElementById('ship').value == 0)
	{
		if (errFound)
		{
			msg = msg + " and a ";
		}
		msg = msg + "Ship-To location";
		errFound = true;
	}
	if (errFound)
	{
		msg = "You must select a " + msg + ".";
		alert(msg);
		return false;
	}
	else
	{
		return true;
	}
}

// This section contains functions for the Funkopedia MOD

function submitAction(fieldId, fieldValue) {
	if (fieldId > '')
	{
		document.getElementById(fieldId).value = fieldValue;
	}
	document.funkopedia.submit();
}

// This section contains functions for the Funatic Store MOD

function getTotalCost() {

	var userShip = document.getElementById('ship').value;
	var userItems = 0.00;
	var userItemCount = 0;
	var userTaxRate = 0.00;
	var userTax = 0.00;
	var userBaseShipping = 5.00;
	var userAddShipping = 2.00;
	var userShipping = 0.00;
	var userTotal = 0.00;
	var userCart = 0;
	var itemsBought = "";
	var count = 0;
	
	while(count < document.store.items_listed.value)
	{
		checkVar = "check_" + count;
		priceVar = "item_price_" + count;
		countVar = "item_pieces_" + count;
		flagsVar = "item_flags_" + count;
		descVar = "item_desc_" + count;
		if (document.getElementById(checkVar).checked)
		{
			userItems = userItems + Number(document.getElementById(priceVar).value) * 100;
			userItemCount = userItemCount + Number(document.getElementById(countVar).value);
			userCart = userCart + Number(document.getElementById(flagsVar).value);
			if (itemsBought > '')
			{
				itemsBought = itemsBought + "; ";
			}
			itemsBought = itemsBought + document.getElementById(descVar).value;
		}
		count = count + 1;
	}
	userItems = userItems / 100;

	// adjust shipping and tax based on zone
	switch(userShip)
	{
	case "0":
		userItems = 0.00;
		break;
	case "1":
		userTaxRate = 6.00;
		break;
	case "2":
		break;
	case "3":
		userBaseShipping = 10.00;
		break;
	default:
		userBaseShipping = 15.00;
	}

	// get total cost
	if (userItems > 0)
	{
		userShipping = userBaseShipping + (userItemCount - 1) * userAddShipping;
		userTax = Math.round((userItems + userShipping) * userTaxRate) / 100;
		userTotal = userItems + userShipping + userTax;
	}

	// format cost breakdown for display
	if (userTotal > 0)
	{
		displayTotalCost = "$" + userItems.toFixed(2);
		if (userShipping > 0)
		{
			displayTotalCost = displayTotalCost + " + $" + userShipping.toFixed(2) + " shipping & handling";
		}
		if (userTax > 0)
		{
			displayTotalCost = displayTotalCost + " + $" + userTax.toFixed(2) + " tax";
		}
		if (userTotal > userItems)
		{
			displayTotalCost = displayTotalCost + " = <b>$" + userTotal.toFixed(2);
		}
		else
		{
			displayTotalCost = "<b>" + displayTotalCost;
		}
		displayTotalCost = displayTotalCost + " (US)</b>";
	}
	else
	{
		displayTotalCost = "<b>not determined</b>";
	}

	// update the page
	document.getElementById('totalCost').innerHTML = displayTotalCost;
	document.getElementById('hdnAmount').value = userItems;
	document.getElementById('hdnShipping').value = userShipping;
	document.getElementById('hdnTax').value = userTax;
	document.getElementById('hdnCart').value = userCart;
	document.getElementById('hdnItems').value = itemsBought;
}

function getNothing() {
}

function confirmStore() {
	var msg = "";
	var errFound = false;
	if (document.getElementById('hdnCart').value == 0)
	{
		msg = "at least one item";
		errFound = true;
	}
	if (document.getElementById('ship').value == 0)
	{
		if (errFound)
		{
			msg = msg + " and ";
		}
		msg = msg + "a Ship-To location";
		errFound = true;
	}
	if (errFound)
	{
		msg = "You must select " + msg + ".";
		alert(msg);
		return false;
	}
	else
	{
		return true;
	}
}

function changeAuctions() {
	document.getElementById('startback').value = 0;
}

function getAuction(number) {
	document.getElementById('auction').value = number;
}

function bidit() {
	document.getElementById('makebid').value = "true";
}

function commitBid(type) {
	var bidtype;
	if (type == 1)
	{
		bidtype = "bidding";
	}
	else
	{
		bidtype = "grabbing";
	}
	document.getElementById('grabbing').value = bidtype;
	document.getElementById('commitment').value = "true";
}

function grabit(amount) {
	document.getElementById('bid').value = amount;
	document.getElementById('grabbing').value = "grabbing";
	document.getElementById('makebid').value = "true";
}

function cancelbid() {
	document.getElementById('auction').value = 0;
	if (document.getElementById('bid'))
	{
		document.getElementById('bid').value = 0;
	}
}

function editAction(editMode) {
	document.getElementById('revise').value = editMode;
	document.viewauction.action = "auctioncreation.php";
}

function createAction(action) {
	switch (action)
	{
	case 1: // cancel
		document.creation.action = "auctions.php";
		break;
	case 2: // save
		document.getElementById('dbAction').value = "save";
		break;
	case 3: // delete
		document.getElementById('dbAction').value = "delete";
		break;
	}
}

function setField(myField, myValue) {
	document.getElementById(myField).value = myValue;
}

// This section contains functions for the Funkopedia MOD

function showMap(myMap) {
	document.getElementById('selected_image').innerHTML = '<img src="' + document.getElementById('detail_image_' + myMap).value + '" />';
	document.getElementById('selected_map').innerHTML = document.getElementById('detail_map_' + myMap).value;
	document.getElementById('selected_funclub').innerHTML = document.getElementById('detail_funclub_' + myMap).value;
	document.getElementById('selected_found').innerHTML = document.getElementById('detail_found_' + myMap).value;
	document.getElementById('selected_hidden').innerHTML = document.getElementById('detail_hidden_' + myMap).value;
	document.getElementById('selected_value').innerHTML = document.getElementById('detail_value_' + myMap).value;
	action = document.getElementById('detail_action_' + myMap).value;
	if (action > '')
	{
		image = '<img src="' + document.getElementById('imagepath').value + action + '.png" />' ;
		myHTML = '<a href=\"#\" onclick=\"confirmMapAction(\'' + action + '\');\">' + image + '</a>';
	}
	else
	{
		myHTML = '';
	}
	document.getElementById('selected_action').innerHTML = myHTML;
	return false;
}

function confirmMapAction(myAction) {
	alert('Sorry, but we are not ready to play yet.');
	return false;
	/*
	var msg;
	msg = "Click OK to " + myAction + " Map #" + document.getElementById('selected_map').innerHTML + " or click Cancel to ignore.";
	if (confirm(msg))
	{
		document.getElementById('action').value = myAction;
		document.getElementById('map').value = document.getElementById('selected_map').innerHTML;
		document.getElementById('mapvalue').value = document.getElementById('selected_value').innerHTML;
		document.treasure.submit();
	}
	else
	{
		return false;
	}
	*/
}

function getTreasure() {
	document.treasurechest.submit();
}

