var cart = [];
var cartLoaded  = false;
function showCart() {
	loadCart();
	txt = "<table cellpadding='0' cellspacing='0' border='0' style='background-color: #f8f8f9; border: 4px solid #ccc'>";
	txt += "<tr><td style='padding: 0px 0 0 0px; vertical-align: middle;'><img src='images/title-shopping-list.png' style='margin-left: 20px; '></td><td width='51' height='61' align='center' valign='middle'><img src='images/bt-close2.png' onmouseover='imover(this)' onmouseout='imout(this)' style='cursor: pointer;' onclick='closeWait();'></td></tr>";
	txt += "<tr><td colspan='2'><div style='width: 430px; padding: 0px 0px; overflow: auto; height: 429px; margin-bottom: 40px; margin-left: 20px; color: #48124a;'>";
	txt += "<table class='cart' cellpadding='0' cellspacing='0' border='0'>";
	txt += '<tr><th width="250"></th><th colspan="3" width="75" align="right">Quantity</th><th width="70" align="right">Price</th></tr>';
	totalvalue = 0;
	for (i in cart)
	{
		if (parseInt(cart[i].qty) <= 0)
		{
			continue;
		}
		totalvalue += parseInt(cart[i].qty) * parseFloat(cart[i].price);
		txt += '<tr id="cart-row-' + cart[i].id + '"><td width="250">'+cart[i].name+'</td><td width="41" align="right"><img src="images/bt-plus.png" onclick="changeQty('+cart[i].id+',1)"></td><td width="20"><img src="images/bt-minus.png" onclick="changeQty('+cart[i].id+',-1)"></td><td width="14" align="right">'+cart[i].qty+'</td><td width="70" align="right">'+cart[i].price+'</td></tr>';
	}
			txt += '<tr><td height="40" colspan="5" style="border: none;">&nbsp;</td></tr>';
			txt += '<tr><td colspan="3" style="border: none;">&nbsp;</td><td colspan="3" style="border-bottom: none;" align="right">'+numberFormat(Math.round(totalvalue*100)/100)+'</td></tr>';
	txt += '</table>';
	txt += '<div class="cart-buttons">';
	txt += '<img src="images/bt-place-order.png" onclick="placeOrder();">';
	txt += '<p>or</p>';
	txt += '<img src="images/bt-continue.png" onclick="closeWait();">';
	txt += '</div>';
	
	txt += "</div>";
	txt += '<div style="text-align: center;"><img src="images/indulge3.png"></div>';
	txt += '<div style="text-align: left;color:#ccc; font-size: 9px; width: 420px; margin-left: 20px;">While we do everything we can to ensure the complete accuracy of this website, prices are subject to change without notice and errors and omissions can occure. When we cann you to review our order, we will confirm pricing, product information and inventory status at this time.</div>';
	txt += "</td></tr>";
	txt += "</table>";

	showOverlay();
	box(txt);

}

function addToCart(prodID, prodName, prodPrice) {
	loadCart();
	for (i in cart)
	{
		if (cart[i].id == prodID)
		{
			cart[i].qty = parseInt(cart[i].qty) + 1;
			saveCart();
			showCart();
			return true;
		}
	}

	cart[cart.length] = {id:prodID, name: prodName, price: prodPrice, qty: 1};
	saveCart();
	showCart();
	return true;
}

function saveCart() {
	txt = "";
	for (i in cart)
	{
		if (parseInt(cart[i].qty) <= 0)
		{
			continue;
		}
		if (txt) txt += "_____";
		txt += cart[i].id + "***" + cart[i].name + "***" + cart[i].price + "***" + cart[i].qty
	}
	Set_Cookie("plShoppingCart", txt);

}

function loadCart() {
	if (cartLoaded) return true;
	var tmp = "";
	var tmp2 = "";
	txt2 = Get_Cookie("plShoppingCart");
	if (txt2)
	{
		tmp = txt2.split("_____");
		cart = [];
		for (i in tmp)
		{
			tmp2 = tmp[i].split("***");
			if (parseInt(tmp2[3]) <= 0)
			{
				continue;
			}

			if (tmp2.length == 4) cart[cart.length] = {id:parseInt(tmp2[0]), name:tmp2[1], price:parseFloat(tmp2[2]), qty:parseInt(tmp2[3])};
		}
	}

	cartLoaded = true;
}

function resetCart() {
	Set_Cookie("plShoppingCart", "", -1000);
	Delete_Cookie("plShoppingCart");
	cart = [];
}


function changeQty(prodID, prodQty) {
	for (i in cart)
	{
		if (cart[i].id == prodID)
		{
			cart[i].qty = parseInt(cart[i].qty) + prodQty;
			saveCart();
			showCart();
			return true;
		}
	}
}

function placeOrder() {
	window.location = "home-delivery.php";
}

function validateOrder() {
	if (!validateField("FirstName", "First Name*")) { alert("First Name is required"); return false;};
	if (!validateField("LastName", "Last Name*")) { alert("Last Name is required"); return false;};
	if (!validateField("Email", "Email*")) { alert("Email is required"); return false;};
	if (!validateField("PostalCode", "Postal Code*")) { alert("Postal Code is required"); return false;};
	if (!validateField("Address", "Address*")) { alert("Address is required"); return false;};
	if (!validateField("Phone", "Phone*")) { alert("Phone Number is required"); return false;};

	return true;
}


