var cartCount = 0;

function getCartCount(area)
{
	new Ajax.Updater(area, 'partials/cart.php?action=count', { method: 'post' });
}

function submitOrder()
{
	new Ajax.Request('/partials/cart.php?action=submitOrder', {
		onSuccess: function(transport) {
		}
	});
}

function emptyCart()
{
	new Ajax.Request('/partials/cart.php?action=destroy', {
		onSuccess: function(transport) {
		}
	});
}

function getTotalItems()
{
	return $('itemsCount').innerHTML;
}

function addToCart(id)
{
	new Ajax.Updater('itemsCount', 'partials/cart.php?action=add&id='+id, { method: 'post' });
	
}

function getCartTotal(whereto)
{
	new Ajax.Updater(whereto, '../partials/cart.php?action=total', { method: 'post' });
}

function removeFromCart(id)
{
	new Ajax.Updater('itemsCount', 'partials/cart.php?action=remove&id='+id, {method: 'post' });
	
}

function removeCartItem($id)
{
	var item = $('row-'+$id);
	Effect.Puff(item);
	removeFromCart($id);
	getCartCount('itemsCount');
	getCartCount('itemsCount2');
	getCartTotal('itemsTotal');
	new Ajax.Updater('grand_total', 'partials/cart.php?action=grandTotal', {method: 'post' });
}

function addCoupon(code)
{
	new Ajax.Updater('coupon_code_notice', 'partials/cart.php?action=addCoupon&coupon_code='+code, {method: 'post' });
	new Ajax.Updater('grand_total', 'partials/cart.php?action=grandTotal', {method: 'post' });
	new Ajax.Updater('coupons_discount', 'partials/cart.php?action=getCouponDiscount', {method: 'post' });
}

function removeCoupon(code)
{
	new Ajax.Updater('coupon_code_notice', 'partials/cart.php?action=removeCoupon', {method: 'post' });
	new Ajax.Updater('grand_total', 'partials/cart.php?action=grandTotal', {method: 'post' });
	new Ajax.Updater('coupons_discount', 'partials/cart.php?action=getCouponDiscount', {method: 'post' });
}

function showCart()
{
	$('menu').style.display = "block";
}


function closeMenu(event) {
	$('menu').style.display = "none";
}

function checkout()
{
	var cnt = getTotalItems();
	if(cnt >0)
	{
		window.location = "/submit_order.php";
	} else {
		alert('There are no items in your cart.');
	}
}