
function refreshCartTotals(data)
{
	$('#cart_totals_div').html($('#cartTotalsTemplate').tmpl(data));
	var msg = data.enabledPromotionMessages;
	var size = msg == null ? 0 : msg.length;
	if (size == 0) {
		$('#checkout_promotion_div').html("");
	} else if (size > 0) {
		$('#checkout_promotion_div').html($('#cartPromotionTemplate').tmpl(data));
	}
}

function bindUpdateCartForm(contextPath)
{
	$('.update_cart_form').each(function () {
		var options = {
            cache: false,
			type: 'POST',
			dataType: 'json',
			success: function(data) {
				refreshCartTotals(data);
				$('#minicart_data').html($('#miniCartTemplate').tmpl(data));
				bindUpdateCartForm(contextPath);
				bindRemoveItemForm(contextPath);
			},
			error: function(xht, textStatus, ex) {
				alert("Failed to update cart. Error details [" + xht + ", " + textStatus + ", " + ex + "]");
			}
		};

		$(this).ajaxForm(options);
	});
}

function bindRemoveItemForm(contextPath)
{
	$('.remove_product_form').each(function () {
		var options = {
            cache: false,
			type: 'POST',
			dataType: 'json',
			success: function(data) {
				refreshCartTotals(data);
				$('#minicart_data').html($('#miniCartTemplate').tmpl(data));
				bindUpdateCartForm(contextPath);
				bindRemoveItemForm(contextPath);
			},
			error: function(xht, textStatus, ex) {
				alert("Failed to update cart. Error details [" + xht + ", " + textStatus + ", " + ex + "]");
			}
		};

		$(this).ajaxForm(options);
	});
}

