var debugMode = false;

// Note: the function displayShoppingCart(data) is in grayScreen.js

function addToCart(itemId) {
    var parameters = { action: 'addItem', itemId: itemId };
    $.get("/pages/jsShoppingCart.php", parameters, function( data ) { 
			displayShoppingCart(data);
			$('#ecommercelinks').show();  // show the links to the cart
			$(".quantity input").keyup(function () {
				changeQuantityInCart($(this).parent().parent().attr('itemId'), $(this).val());
				updateCartPrices($(this).parent().parent().parent());
			});
    	}
	);
}

// I don't think this ever gets called
// function recalculateCart() {
// 	var parameters = $('#shoppingCartForm').formSerialize();//    $('#shoppingCartForm').submit();
// 	$.post('/pages/jsShoppingCart.php',parameters,function(data) { 
// 			displayShoppingCart(data);
// 		}
// 	);
// }

function displayCart() {
	// this is called by the browser when a user hits the "Shopping Cart" button
    var parameters = { ACTION: 'none' };
    $.post("/pages/jsShoppingCart.php", parameters, function( data ) { 
			displayShoppingCart(data);
			$(".quantity input").keyup(function () {
				changeQuantityInCart($(this).parent().parent().attr('itemId'), $(this).val());
				updateCartPrices($(this).parent().parent().parent());
			});
    	}
	);
}

function removeItemFromCart(id) {
	var tbody = $("tr[itemId='"+id+"']").parent();
	if($("tr[itemId]").size() == 1) {
		// it is the last one
		$("tr[itemId='"+id+"']").html("<td colspan='6' align='center' style='padding:12px;font-size: 14px'><b>There are no items in your shopping cart.</b></td>");
		$(".proceedToCheckout").attr('disabled', true);
		$('#ecommercelinks').hide();  // hide the links to the cart
	} else {
		$("tr[itemId='"+id+"']").remove();
	}
	updateCartPrices(tbody);

	// Tell the server what has been removed
	var parameters = { ACTION: 'updateQuantity', IID: id, QUANTITY: 0 };
	$.post("/pages/jsShoppingCart.php", parameters);
}

function changeQuantityInCart(id, value) {
	var parameters = { ACTION: 'updateQuantity', IID: id, QUANTITY: value };
	$.post("/pages/jsShoppingCart.php", parameters);
}

function addGiftCardToCart() {
	// alert($("select[name='GCAMOUNT']").val());
	addToCart($("select[name='GCAMOUNT']").val());
}

function updateCartPrices(tbody) {
	// tbody is a jQuery object
	//console.log("updateCartPrices");
	var subtotal = 0;
	tbody.find('.cartitem').each(function () {
		
		var price = $(this).attr('price');
		var qty = $(this).find(".quantity input").val();
		var total = Number(qty)*Number(price);
		
		//console.log("cart item:"+price+" "+qty);
		
		$(this).find(".total").html(formatCurrency(total));
		subtotal = Number(subtotal) + Number(total);
	});
	if($(".gssc1 .total").length) {
		// ecommerce cart
		$(".gssc1 .total").html(formatCurrency(subtotal));
	} else {
		// catering cart
		tbody.find('.subtotal .total').html(formatCurrency(subtotal));
	}
}

// Support
function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) {
		num = "0";
	}
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10) {
		cents = "0" + cents;
	}
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) {
		num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
	}
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

// Replaced by jQuery binding to clicks
// function addToCateringCart(itemId) {
// 	var quantity = $("div[itemId="+itemId+"] input").val();
//     var parameters = { action: 'addItem', itemId: itemId, quantity: quantity};
//     $.get("/pages/jsCateringCart.php", parameters, function( data ) { 
// 			displayShoppingCart(data);
// 			$(".quantity input").keyup(function () {
// 				changeQuantityInCateringCart($(this).parent().parent().attr('itemId'), $(this).val());
// 				updateCartPrices($(this).parent().parent().parent());
// 			});
//     	}
// 	);
// }

// I don't think this ever gets called
function recalculateCart() {
	var parameters = $('#shoppingCartForm').formSerialize();//    $('#shoppingCartForm').submit();
	$.post('/pages/jsShoppingCart.php',parameters,function(data) { 
			displayShoppingCart(data);
		}
	);
}

function displayCateringCart() {
	// this is called by the browser when a user hits the "Shopping Cart" button
    var parameters = { ACTION: 'none' };
    $.post("/pages/jsCateringCart.php", parameters, function( data ) { 
			displayShoppingCart(data);
			$(".quantity input").keyup(function () {
				changeQuantityInCateringCart($(this).parent().parent().attr('itemId'), $(this).val());
				updateCartPrices($(this).parent().parent().parent());
			});
    	}
	);
}

// Can this be combined with the other removeItemFromCart?
function removeItemFromCateringCart(id) {
	var tbody = $("tr[itemId='"+id+"']").parent();
	if($("tr[itemId]").size() == 1) {
		// it is the last one
		$("tr[itemId='"+id+"']").html("<td colspan='6' align='center' style='padding:12px;font-size: 14px'><b>There are no items in your catering cart.</b></td>");
		$(".proceedToCheckout").attr('disabled', true);
		$('#login').hide();  // hide the links to the catering cart
	} else {
		$("tr[itemId='"+id+"']").remove();
	}
	updateCartPrices(tbody);
	
	// Tell the server what has been removed	
	var parameters = { ACTION: 'updateQuantity', IID: id, QUANTITY: 0 };
	$.post("/pages/jsCateringCart.php", parameters);
}

function changeQuantityInCateringCart(id, value) {
	var parameters = { ACTION: 'updateQuantity', IID: id, QUANTITY: value };
	$.post("/pages/jsCateringCart.php", parameters);
}

function updateCateringCartPrices(tbody) {
	// tbody is a jQuery object
	var subtotal = 0;
	tbody.find('.cartitem2').each(function () {
		var price = $(this).attr('price');
		var qty = $(this).find(".quantity input").val();
		var total = Number(qty)*Number(price);
		$(this).find(".total").html(formatCurrency(total));
		subtotal = Number(subtotal) + Number(total);
	});
	$(".gssc1 .total").html(formatCurrency(subtotal));
}
