// JavaScript Document

function loadMenu() {
	createItemsList();
	checkQuantity();
	updateCart();
	//writeMenu(category);
}

function updateCat(cat) {
	writeMenu(cat);
}


function writeMenu(cat){
	innerHeader= "<h3>"+ cat +"</h3>";
	innerBody="";
	for(var a = 0; a < itemsDB.length; a++){
	
		//checks type and category
		if(itemsDB[a].category == cat && itemsDB[a].type == "i"){
			
			innerBody = innerBody + 
				"<div class='item-module'>" +
				itemsDB[a].num + " " +
				"<a onclick='writeItem(" + itemsDB[a].id + ");'>" + itemsDB[a].name + 
				" <small>" + itemsDB[a].name2 + "</small></a>" +
				"</div>";
				
		};
	};
	getDIV("menu-list-header").innerHTML = innerHeader;
	getDIV("menu-list-body").innerHTML = innerBody;	
}

function writeItem(num){
	itemHeader = "<h3>" + itemsDB[num].name + "<br/><em>" + itemsDB[num].name2 +"</em></h3>";
	itemBody = "";
/*
	itemBody =	"<h4>Description</h4> <div class='item-img'>" +
		"<img width='"+ (itemsDB[num].imageW /5) +"' height='"+ (itemsDB[num].imageH/5) +"' alt='" + itemsDB[num].name + " " + itemsDB[num].name2 + 
			"' src='images/gallery/thumbnails/" + itemsDB[num].image + "' onclick='popUp( \"images/gallery/images/" + 
			itemsDB[num].image + " \" , \" " + itemsDB[num].name+ " \" , \" " + itemsDB[num].imageW + 
			" \" , \" " + itemsDB[num].imageH + " \")' />" +
		"</div>";
*/		
	//only show options after there is one in the cart
	if(itemsDB[num].q >0)
		itemBody = getOptions(itemsDB[num].num);
	
	itemBody = itemBody + "<br />" +
		"<a onclick='addItem(" + itemsDB[num].id + ");updateCart(); writeItem(" + num + ");'>Add to Cart</a>" +
		" $" + fixFloat(itemsDB[num].price, 2);

	itemFooter = "<div><br />" + itemsDB[num].desc + "</div>"; //Over writes the image temporatily, until there are enough pictures

	getDIV("menu-item-header").innerHTML = itemHeader;
	getDIV("menu-item-body").innerHTML = itemBody;
	getDIV("menu-item-footer").innerHTML = itemFooter;

	
}

function updateCart(){
	subtotal = 0;
	GST = 0;
	total = 0;
	innerBody = "";
	innerHeader = "<h3>Order Summary</h3>";

	for(var b = 0; b < itemsDB.length; b++)
		{
		if (itemsDB[b].q > 0)
			{

			innerBody = innerBody + "<div class='item-module'>" +
				"<span><a title='Increase Quantity' onclick='addItem(" + itemsDB[b].id + ")'>[+]</a> " +	
				"<a title='Reduce Quantity' onclick='removeItem(" + itemsDB[b].id + ")'>[-]</a> </span> " +				
				"<a onclick='writeItem("+itemsDB[b].id+");'>" + itemsDB[b].name + "</a>" +
				"<small> $" + fixFloat(itemsDB[b].price, 2) + "<br />" +
				"Quantity: " + itemsDB[b].q + "</small>" +				
				"</div>";
				subtotal += itemsDB[b].q * itemsDB[b].price;
			};
		}
	
	GST = subtotal * 0.05;
	total = subtotal + GST;
	innerCheckout = "<span><strong>Subtotal:</strong> $" + fixFloat(subtotal, 2) + "</span>" +
		"<span><strong>GST:</strong> $" + fixFloat(GST, 2) + "</span>" +
		"<span><strong>Total</strong> $" + fixFloat(total, 2) + "</span>" + 
		"<a onclick='deleteAll();'>Clear</a>";
	
	innerCheckout = innerCheckout + "&nbsp;&nbsp;<button onclick='checkOut();'>Check Out</button>";

	getDIV("menu-cart-header").innerHTML = innerHeader;
	getDIV("menu-cart-body").innerHTML = innerBody;
	getDIV("menu-checkout").innerHTML = innerCheckout;

}

function orderFormOutput(){
	//loadMenu();
	
	subtotal = 0;
	GST = 0;
	total = 0;
	innerBody = "";
	
	innerHeader = "<h3>Order Summary</h3>";
	for(var b = 0; b < itemsDB.length; b++)
		{
		if (itemsDB[b].q > 0)
			{
				innerBody = innerBody + "<div><a onclick='writeItem("+itemsDB[b].id+");'>" + 
					itemsDB[b].name + "</a><span><a onclick='removeItem(" + itemsDB[b].id + ")'>[-]</a></span>" +				
					"<br />" + itemsDB[b].q + " @  $" + fixFloat(itemsDB[b].price, 2) + "</div>";
				
				subtotal += itemsDB[b].q * itemsDB[b].price;
			};
		}
	
	GST = subtotal * 0.05;
	total = subtotal + GST;
		innerFooter = "<strong>Subtotal:</strong> $" + fixFloat(subtotal, 2) + "<br />" +
		"<strong>GST:</strong> $" + fixFloat(GST, 2) + "<br />" +
		"<strong>Total</strong> $" + fixFloat(total, 2);
	
	innerFooter = innerFooter + "<br /><br /><button onclick='deleteAll();'>Clear</button><button onclick='checkOut();'>Back</button>";

	getDIV("menu-cart-header").innerHTML = innerHeader;
	getDIV("menu-cart-body").innerHTML = innerBody;
	getDIV("menu-cart-footer").innerHTML = innerFooter;
}

function writeOrder(){
	document.write("<input type='hidden' value='"+ document.cookie +"' name='cookie' />");	
	}