// $Id: Cart.js,v 1.2 2009/07/03 21:02:15 cmanley Exp $
// Copyright (c) 2009, Craig Manley (craigmanley.com)
//
// Requires:
// domTT
// /js/I18N.js
// /js/RpcClient.js


var Cart = new (function() {

	// Updates the visible values in the cart summary panel.
	this.updatePanel = function(count, value) {
		var e = document.getElementById('panelCartCount');
		if (e) {
			e.innerHTML = count;
		}
		e = document.getElementById('panelCartValue');
		if (e) {
			e.innerHTML = value.toCurrency();
		}
	}

	// Adds an item to the cart.
	this.addItem = function(caller, event, code, amt) {
		if (!amt) {
			amt = 1;
		}
		var result = RpcClient.cartAddItem(code, amt);
		if (result) {
			if (window.location.pathname == '/cart.php') {
				window.location.reload(true);
			}
			else {
				this.updatePanel(result.count, result.value);
				var content = 'Product ' + code + ' is aan uw winkelmandje toegevoegd.';
				domTT_activate(caller, event, 'content', content, 'styleClass', 'domTTClassic', 'lifetime', 2000);
			}
		}
	}

	// Adds items to the cart from existing order.
	this.addItemsFromOrder = function(caller, event, order_id) {
		var result = RpcClient.cartAddItemsFromOrder(order_id);
		if (result) {
			alert('De product(en) van bestelling nr. ' + order_id + ' zijn aan uw winkelmandje toegevoegd.');
			if (window.location.pathname == '/cart.php') {
				window.location.reload(true);
			}
			else {
				this.updatePanel(result.count, result.value);				
			}
		}
		else {
			alert('Mislukt!');
		}
	}

})();

