
function currency(x) { return parseFloat(x).toFixed(2); }


if (JsHttpRequest) // JsHttpRequest class must be accessible!
{
	
	function Cart()
	{
		var me = this;
		
		me.add = function (category, id, qty)
		{
			qty = qty || 1; // по умолчанию количество = 1 шт.
			
			JsHttpRequest.query
			(
				'/ajax.cart.php',
				{
					action: "cart.add",
					params:
					{
						category: category,
						id: id,
						qty: qty
					}
				},
				function (r, e) { if (e) me.onerrors(e); if (r) me.onadd(r); },
				true
			);
		}
		
		me.remove = function (item_id)
		{
			JsHttpRequest.query
			(
				'/ajax.cart.php',
				{
					action: 'cart.remove',
					item_id: item_id
				},
				function (r, e) { if (e) me.onerrors(e); if (r) me.onremove(r); },
				true
			);
		}
		
		me.change = function (item_id, qty)
		{
			JsHttpRequest.query
			(
				'/ajax.cart.php',
				{
					action: 'cart.change',
					params:
					{
						item_id: item_id,
						new_qty: qty
					}
				},
				function (r, e) { me.onchange(r, e); },
				true
			);
		}
		me.clear = function ()
		{
			JsHttpRequest.query
			(
				'/ajax.cart.php',
				{
					action: 'cart.clear'
				},
				function (r, e) { if (e) me.onerrors(e); else me.onclear(r); },
				true
			);
		}
		me.total = function (node)
		{
			JsHttpRequest.query (
				'/ajax.cart.php',
				{ action: 'cart.total' },
				function (r, e) { if (r.total) node.innerHTML = currency(r.total); },
				true
			);
		}
		
		me.onadd     = function (result) {}
		me.onremove  = function (result) {}
		me.onchange  = function (result) {}
		me.onclear   = function (result) {}
		
		me.onerrors  = function (errors) {}
	}
}