/**
 * change product quantity
 * @param object self quantity input being changed
 * @param float price cost of 1 item
 */
function shoppingCartUpdate(self, price, currencyCode)
{
	var q = (q = self.value) < 0 ? 0 : q, id = self.id;
	var p = document.getElementById(id+'_price');
	if (p && !isNaN(q)) {
		p.innerHTML = (q * price).formatMoney(this,currencyCode);
		shoppingCartUpdateTotal(self, currencyCode);
	}
	return false;
}
/**
 * remove an item from the cart
 * @param object self button pressed to remove item
 */
function shoppingCartRemove(self, currencyCode)
{
	var id = (id = self.id).substring(0, id.length - 6) + 'row';
	var r = document.getElementById(id);
	if (r && r.parentNode) {
		var top = shoppingCartGetTop(self);
		// check to see if there are no items anymore... if there aren't then
		// all the request to go through and reload the page.
		// can't do this as an empty check after the removal as aparently a
		// true/false needs to be returned before any DOM manipulation occurs.
		if (1 >= top.innerHTML.match(/products_\d+_row/g).length)
			return true;

		// remove and update total
		r.parentNode.removeChild(r);
		shoppingCartUpdateTotal(top, currencyCode);
	}
	return false;
}
/**
 * @param object self either the quantity input changed of the remove button
 *    pressed that is causing this update to occur.
 */
function shoppingCartGetTop(self)
{
	var pref = (pref = self.id.match(/^(.*)products_\d+(?:_.*)?$/))
		&& 1 < pref.length ? pref[1] : '';
	return document.getElementById(pref + 'top');
}
/**
 * Update the total price displayed at the bottom of the cart
 * @param object self is the block that the cart is contained in
 */
function shoppingCartUpdateTotal(self, currencyCode)
{
	// check if self is the top element
	if (self.id && 'top' != self.id.substring(self.id.length - 3)) {
		self = shoppingCartGetTop(self);
	}
	if (!self.id ||'top' != self.id.substring(self.id.length - 3))
		return;
	var pref = (pref = self.id).substring(0, self.id.length - 3);
	var t = document.getElementById(pref + 'total');
	if (t && t.innerHTML) {
		var ids = self.innerHTML.match(/products_(\d+)/g);
		var d = {}, total = 0, q, p;
		for (i in ids) {
			if (!d[ids[i]]) {
				p = document.getElementById(pref + ids[i] + '_price')
				if (p && p.innerHTML) {
					p = parseFloat(p.innerHTML.replace(/[^\d.]/g, ''));
					if (!isNaN(p)) {
						d[ids[i]] = p;
						total += p;
					}
				}
			}
		}
		t.innerHTML = total.formatMoney(this, currencyCode);
	}
}
