/* productCompare.js
   Copyright (c) 2007, Decorative Product Source, Inc.
   $Id: productCompare.js,v 1.2 2009/07/10 19:39:05 dferruggia Exp $ */
	
function updateComparisonSession(jObject) //must be a jQuery object created by $(), not an ordinary DOM object
{
	var category = jObject.attr('category');
	var productID = jObject.attr('productID');
	var needUpdate = 0;
		
	jObject.attr('disabled', 'true');  //disable it for further user action and will unlock when ajax is completed
	
	switch (jObject.attr('checked'))
	{
		case true: //add the product
			if (category == compareCategory)
			{
				if (!($.grep(compareProductList, function(n, i){ return n == productID;})[0])) // if the productID is not included in list yet
				{
					if (compareProductList.length < 20) //we only accpet max of 20 products to be compared.
					{
						compareProductList.push(productID);
						needUpdate = 1;
					} else
					{
						alert('You have reached the maximum of 20 products to compare!');
						jObject.attr('disabled', '');
						jObject.attr('checked', '');
					}
				}
			} else //when category changes, actually this wont happen on the same result page with products sharing a common category.
			{
				compareCategory = category;
				compareProductList = [productID];
				needUpdate = 1;
			}
			break;
		default: //remove the product
			compareProductList = $.grep(compareProductList, function(n, i){ return n != productID;});
			needUpdate = 1;
			break;
	}
	
	if (typeof(compareUpdateSessionURL) != "undefined" && needUpdate)
	{
		$.get(compareUpdateSessionURL, { "COMPARE_CATEGORY": compareCategory, "COMPARE_PRODUCT_LIST": compareProductList.join(",") }, function(data){jObject.attr('disabled', '');});
		
	}
}
