/* all functions for manipulating the cart */

function doAjaxBuy(pid, pageType)
{
	var theEl;
	var optionId;
	var choiceId;
	var optionCount = 0;
	var quantity, reguid, giftWrapping;
	
	var theUrl = 'ajaxPages/doCart.php?m=add&pid=' + pid;
	
	/* get the quantity */
	if ((theEl = document.getElementById('quantity')) && (isNumeric(theEl.value)))
		quantity = theEl.value;
	else if ((theEl = document.getElementById('quantity' + pid)) && (isNumeric(theEl.value)))
		quantity = theEl.value;
	else
	/* uh oh, we couldn't find anything, assume 1 */
		quantity = 1;

	theUrl += '&q=' + quantity;

	reguid = (document.getElementById('reguid')) ? document.getElementById('reguid').value : -1;
	theUrl += "&reguid=" + reguid;

	giftWrapping = ((el = document.getElementById('giftWrappingEnabled')) && (el.checked)) ? 1 : 0;
	theUrl += "&giftWrapping=" + giftWrapping;

	theUrl += "&pageType=" + pageType;

	while(theEl	= document.getElementById('optionId_' + pid + '_' + optionCount))
	{
		optionId = theEl.value;
		choiceId = document.getElementById('option_' + pid + '_' + optionId + '_' + optionCount).value;
		theUrl += '&options[' + optionId + ']=' + encodeURIComponent(choiceId);
		
		optionCount++;
	}
	
	tag = 'buyStatusTag' + ((document.getElementById('buyStatusTag' + pid)) ? pid : '');
	replacePanel(tag, theUrl);
}

function buyWishListItem(pid)						{	doAjaxBuy(pid, 'wishlist');		}
function buyProduct(pid) 								{	doAjaxBuy(pid, 'default');			}
function buyProdInCatItem(pid)					{	doAjaxBuy(pid, 'prodsbycat');	}
function buyItemCustomStr(pid, pageType)		{	doAjaxBuy(pid, pageType);		}

function updateQuantity(itemId)
{ 
	replacePanel('globalCartStatus', 'ajaxPages/doCart.php?m=chquant' 
										+ '&itemId=' + itemId 
										+ '&q=' + document.getElementById("quantity" + itemId).value); 
}

function removeFromCart(itemId)
{
	if (confirm("Are you sure you want to remove this item from your cart?") == true)
		replacePanel('globalCartStatus', 'ajaxPages/doCart.php?m=remove&itemId=' + itemId);
}


