


function adjustProductInCart(productID, delta)	{

	var field = findObj("quantity_" + productID);

	var quantity = eval(field.value);
	
	if(quantity<=0 && delta<0)	return;
	
	quantity+=delta;
	
	if(quantity<0)	quantity = 0;
	
	field.value = quantity;	
	
	saveProductInCart(productID, quantity);
}

function saveProductInCart(productID, quantity)	{

	// number check

	if((quantity+"").indexOf(".")==0)	quantity = "0" + quantity;
	findObj("quantity_" + productID).value = quantity;

	if(isNaN(quantity) || quantity<0)		{
	
		findObj("quantity_" + productID).value = "0";
		return;
	}
	
	// limit check
	
	var limit = eval("limit_" + productID);
	
	if(quantity>limit)	{
	
		quantity = limit;
	
		findObj("quantity_" + productID).value = quantity;
	}
	
	xmlconn.request("/shop/add_product/noheaders?productID=" + productID + "&quantity=" + escape(quantity));
}

xmlconn.setRespHandler('product_in_cart_saved', processProductInCart);

function processProductInCart(xmldoc) {

	try	{

		var item_count = loadOptionalXMLParam(xmldoc, "item_count");
		var item_sub_total = loadOptionalXMLParam(xmldoc, "item_sub_total");
		var productID = loadOptionalXMLParam(xmldoc, "productID");

		findObj("promo_text").innerHTML = loadOptionalXMLParam(xmldoc, "promo_text");
		
		findObj("quantity_" + productID).style.border="2px solid #007b09";
		
		updateCartDetails(item_count, item_sub_total);
		startFade("cart_details");


	} catch(e)	{

		displayError(e);
 	}
}

function updateCartDetails(item_count, item_sub_total) {

	var status;
	
	if(item_count==0)	status = "<i>Empty</i>";
	else				{
	
		status = item_count + " item" + (item_count==1?"":"s") + " ($" + item_sub_total + ")";
	}
	
	findObj("cart_details").innerHTML = status;
}