//This script drives the client-side portion of the ordering system.
//It dynamically creates table entries from cookies.

var ordertotal = 0.00;

var cartitems = parseInt(readCookie("CartItems"));
var OrderBody = document.getElementById("OrderTableBody");
for (var cartitem = 0; cartitem < cartitems; cartitem++) {

	var itemid = parseInt(readCookie("ItemID" + cartitem));
	var cartrow = document.createElement("tr");
	cartrow.id = "Item" + itemid;
	OrderBody.appendChild(cartrow);
	
	var ProductName = document.createElement("td");
	var NameNode = document.createTextNode(readCookie("ItemName" + itemid));
	cartrow.appendChild(ProductName);
	ProductName.appendChild(NameNode);

	//You cannot fool this on the client side because it is verified with the server upon checkout.
	//All you succeed in doing by tampering with the script is altering the total that you SEE.
	var ProductPrice = document.createElement("td");
	var PriceFloat = parseFloat(readCookie("ItemPrice" + itemid)).toFixed(2);
	var PriceNode = document.createTextNode('$' + PriceFloat);
	cartrow.appendChild(ProductPrice);
	ProductPrice.appendChild(PriceNode);
	
	var ProductQuant = document.createElement("td");
	cartrow.appendChild(ProductQuant);
	
	var QuantText = document.createElement("input");
	QuantText.type="text";
	QuantText.size="3";
	QuantText.name="qty" + itemid;
	QuantText.onchange=modifyQuantityNoID;
	
	var QuantCookie = readCookie("ItemQuant" + itemid);
	QuantText.value=(QuantCookie != null) ? parseInt(QuantCookie) : "1";
	
	ProductQuant.appendChild(QuantText);
	
	var QuantTotal = document.createElement("td");
	QuantTotal.id="Total" + itemid;

	var QuantNode = document.createTextNode('$' + (PriceFloat * parseInt(QuantText.value)).toFixed(2));

	ordertotal += (PriceFloat * parseInt(QuantText.value));
	cartrow.appendChild(QuantTotal);
	QuantTotal.appendChild(QuantNode);

	var RemoveCol = document.createElement("td");
	cartrow.appendChild(RemoveCol);
	
	var RemoveButton = document.createElement("input");
	RemoveButton.type="button";
	RemoveButton.value="Remove";
	RemoveButton.id="Remove" + itemid;
	RemoveCol.appendChild(RemoveButton);
	RemoveButton.onclick=removeItemNoID;
}

document.getElementById("OrderTotal").childNodes[0].nodeValue = '$' + ordertotal.toFixed(2);
document.getElementById("SalesTax").childNodes[0].nodeValue = 'NJ residents must add 7% sales tax ($' + (ordertotal * .07).toFixed(2) + ')';

function modifyQuantity(itemid, newquant) {
	//Modifies the quantity being purchased of an item.
	itemid = parseInt(itemid);
	newquant = parseInt(newquant);

	if (isNaN(newquant) || newquant < 1 || newquant > 16777215) {
//		alert("Please choose a numeric quantity greater than zero.");
		return;
	}
	
	createCookie("ItemQuant" + itemid, newquant, 365);

	var TotalPrice = parseFloat(document.getElementById("OrderTotal").childNodes[0].nodeValue.substring(1));

	var OldItemTotal = parseFloat(document.getElementById("Total" + itemid).childNodes[0].nodeValue.substring(1));
	var PriceFloat = parseFloat(readCookie("ItemPrice" + itemid)).toFixed(2);
	var ItemTotal = PriceFloat * newquant;
	
	document.getElementById("Total" + itemid).childNodes[0].nodeValue = '$' + ItemTotal.toFixed(2);
	
	TotalPrice += ItemTotal - OldItemTotal;
	document.getElementById("OrderTotal").childNodes[0].nodeValue = '$' + TotalPrice.toFixed(2);
	document.getElementById("SalesTax").childNodes[0].nodeValue = 'NJ residents must add 7% sales tax ($' + (TotalPrice * .07).toFixed(2) + ')';
}

function removeItem(itemid) {
	
	itemid = parseInt(itemid);
	var cartitems = parseInt(readCookie("CartItems"));

	//Find the item in the cart.
	var founditem = -1;
	for (var cartitem = 0; cartitem < cartitems; cartitem++) {
			if (parseInt(readCookie("ItemID" + cartitem)) == itemid) {
					//Swap with the last and decrement the count.
					createCookie("ItemID" + cartitem, parseInt(readCookie("ItemID" + (cartitems-1))), 365);
					eraseCookie("ItemID" + (cartitems-1));
					
					createCookie("CartItems", --cartitems, 365);
					founditem = cartitem;
					
					break;
			}
	}

	if (founditem == -1) {
			alert("The item you are trying to remove was not found in your basket.");
			return;
	}

	var OrderBody = document.getElementById("OrderTableBody");
	var TargetRow = document.getElementById("Item" + itemid);
	OrderBody.removeChild(TargetRow);

	var TotalPrice = parseFloat(document.getElementById("OrderTotal").childNodes[0].nodeValue.substring(1));
	var ItemPrice = parseFloat(readCookie("ItemPrice" + itemid));
	var ItemQuant = parseInt(readCookie("ItemQuant" + itemid));
	var ItemTotal = ItemPrice * ItemQuant;

	TotalPrice -= ItemTotal;
	document.getElementById("OrderTotal").childNodes[0].nodeValue = '$' + TotalPrice.toFixed(2);
	document.getElementById("SalesTax").childNodes[0].nodeValue = 'NJ residents must add 7% sales tax ($' + (TotalPrice * .07).toFixed(2) + ')';
	
	var remitemname = readCookie("ItemName" + itemid);
	
	eraseCookie("ItemName" + itemid);
	eraseCookie("ItemPrice" + itemid);
	eraseCookie("ItemQuant" + itemid);
	
	alert(remitemname + " was removed from your basket.");
}

function removeItemNoID() {
	//This is because of that stupid limitation that you can't pass parameters to programmatically created events.
	var itemid = this.id.substring(6);
	removeItem(itemid);
}

function modifyQuantityNoID() {
	var itemid = this.name.substring(3);
	var quantity = this.value;
	
	modifyQuantity(itemid, quantity);
}
