<!--
/**
 * Shopping cart functionality
 * @version 09/25/2008 - SC
 */

/**
* Update the shopping cart
* @param	string	act
*/
function updateCart(act)
{
	if (isLoading) { return; }

	clearRowSelect();
	Form.disable('buttons');
	Form.disable('cartForm');

	$('act').value = act;

	var url = '/cart/cartUpdate.php';
	var params = Form.serialize('cartForm');
	var ajax = new Ajax.Request(
		url,
		{
			method:'post',
			parameters:params,
			onLoading:showLoadCart,
			onComplete:checkCartResult,
			onFailure:checkCartResult
		}
	);

	return false;
}
function showLoadCart() { showLoadIcon('Updating Cart'); }

/**
* Cart response handler
*/
function checkCartResult(req)
{
	var response = req.responseText;
	var res = boom(response);
	if (res.length)
	{
		if (res[0] == 1)
		{
			// success
			if (res[1] == 'delete')
			{
				if (res[2].length)
				{
					var itemIDs = res[2].split(',');
					for (var i=0; i<itemIDs.length; i++)
					{
						Element.remove('row' + itemIDs[i]);
					}
					fixRowColors('cartTable',true,1,true,1);
					checkNone();
				}
			}
			else if (res[1] == 'update' || res[1] == 'check_out')
			{
				var items = res[2].split(',');
				for (var i=0; i<items.length; i+=3)
				{
					itemID = items[i];
					qty = (items[i + 1] * 1);
					extend = items[i + 2];

					if (qty <= 0)
					{
						// the qty is zero or less; remove the item
						Element.remove('row' + itemID);
					}
					else
					{
						// update the item
						$('qty' + itemID).value = qty;
						$('extend' + itemID).innerHTML = ('$' + formatCurrency(extend));
					}
				}

				var totals = res[3].split(',');
				$('total_qty').innerHTML = totals[0];
				$('total_price').innerHTML = ('<font color="red">*</font> $' + formatCurrency(totals[1]));

				fixRowColors('cartTable',true,1,true,1);
				var empty = checkNone();

				if (res[1] == 'check_out')
				{
					if (empty)
					{
						alert('Unable to check out - there are no items in your shopping cart!');
					}
					else
					{
						// items in cart - go to checkout
						Form.disable('buttons');
						Form.disable('cartForm');
						go('/checkout.php');
						return;
					}
				}
			}
		}
		else
		{
			//alert('Unable to update your shopping cart:\n' + response);
			alert('Unable to update your shopping cart. Please try again. [1]');
		}
	}
	else
	{
		alert('Unable to update your shopping cart. Please try again. [2]');
	}

	Form.enable('buttons');
	Form.enable('cartForm');
	hideLoadIcon();
}

/**
* Make sure they want to remove items and then do so
*/
function removeItemsRefresh()
{
	if (!Form.anyChecked('cartForm'))
	{
		alert('Please select at least one item to remove from your shopping cart.');
	}
	else if (confirm('Are you sure you would like to remove the selected items from your shopping cart?'))
	{
		document.cartForm.act.value="delete_item";
		document.cartForm.submit();
	}
}
/**
* Make sure they want to remove items and then do so
*/
function removeItems()
{
	if (!Form.anyChecked('cartForm'))
	{
		alert('Please select at least one item to remove from your shopping cart.');
	}
	else if (confirm('Are you sure you would like to remove the selected items from your shopping cart?'))
	{
		updateCart('delete');
	}
}

/**
* Make sure they want to remove items and then do so
*/
function removeSingleItem()
{
	if (confirm('Are you sure you would like to remove the selected items from your shopping cart?'))
	{
		showLoadIcon('Loading...');
		document.removeForm.submit();
	}
}

function checkSingleUpdate(val)
{
	showLoadIcon('Loading...');
	document.cartForm.act.value = "update_item";
	document.cartForm.submit();

	if (val=='check_out')
	{
		Form.disable('buttons');
		Form.disable('cartForm');
		go('/checkout.php');
		return;
	}
}
/**
* Open the wishlist select form
* @param	object	e	event object
*/
function showWishlist(e)
{
	if (!Form.anyChecked('cartForm'))
	{
		alert('Please select at least one item to move to a wishlist.');
	}
	else
	{
		showVis('wishDiv');
		positionDiv('wishDiv',e,null,null,false);
		Field.focus('sel_wishlistID');
	}
}
/**
* Hide the wishlist select form
*/
function hideWishlist()
{
	Form.blurAll('wishForm');
	hideVis('wishDiv');
}

/**
* Check if there are no items in the shopping cart
* If there aren't, show the message
*/
function checkNone()
{
	if ($('cartTable').rows.length == 2)
	{
		hideDisp('cartContainer');
		showDisp('noneMsg');
		return true;
	}
	else
	{
		return false;
	}
}

/**
* Check that they can update the cart and update it if applicable
*/
function checkUpdate()
{
	var has_zero = false;
	var objs = Form.getInputs('cartForm','text');
	for (var i=0; i<objs.length; i++)
	{
		val = parseInt($F(objs[i]));
		if (!isNaN(val) && val <= 0)
		{
			has_zero = true;
			break;
		}
	}

	if (!has_zero || confirm('One or more of your shopping cart items has a quantity that is less than one.\nThese items will be removed from your cart.\nAre you sure this is what you would like to do?'))
	{
		Form.checkAll('cartForm',false);
		updateCart('update');
	}
}

// -->