function handleCart(obj)
{
	var purchasetype = 0;
	var purchaseformat = 0;
	
	
	$(obj)
	.attr('href','javascript:void(0)');
	
	$('.get-price-section')
	.each(function()
	{
		var radio = $(this)
		.find('input:checked');
		
		var which = radio
			.parent()
			.parent()
			.find('label:first')
			.find('span:first')
			.html();
			
		if( radio.val() != null && radio.val() != '' )
		{
			$('.get-price-section-list')
			.find('select option:selected')
				.each(function()
				{	
					var opt = parseInt($(this).attr('value'));
					
					if( opt != null && opt != 'none' && !isNaN(opt) )
					{
						purchasetype = priceObject[parseInt(radio.attr('rel'))][opt].typeid;
						purchaseformat = priceObject[parseInt(radio.attr('rel'))][opt].id;
						//alert(purchaseformat);
					}
				});
		}
	});
	
	var cartDataSrc = 'ajax/cart.php';
	var title = $('.item-title').find('span:first').html();
	var vendor = $('#info-image-id').html();
	var item_action = '';
	var cart_count = 0;
	
	if( $(obj).hasClass('remove') )
	{
		// Remove item from cart
		item_action = 'remove_from_cart';
		var item_id = parseInt($('.add-to-cart').find('a').attr('title'));
	}
	else
	{
		if( $(obj).hasClass('add') )
		{
			//Add item to the cart
			item_action = 'add_to_cart';
			var item_id = parseInt($('.add-to-cart').find('a').attr('title'));
		}
		else
		{
			item_action = 'is_cart_member';
		}
	}
	
	function handleData(data, status)
	{
		var objResponse = eval('('+ data +')');
		if(objResponse.status == 'success')
		{
			cart_count = objResponse.count;
			
			if( $(obj).hasClass('remove') )
			{	
				cart_count--;
				(cart_count == 1)? i = 'There is now '+ cart_count + ' item' : i = 'There are now ' + cart_count + ' items';
				
				showDialog('', 'The Item <span class="bold">"' + title + '"</span> with Item ID <span class="bold">' + vendor + '</span> has been <span class="bold plus-one">removed</span> your cart.<br>' + i +' in your cart.<br><br>To view your cart click on the cart link at the top-right corner of the page.');
				
				//window.location.href = 'my-cart?ref=' + window.location.href;

				$(obj)
				.removeClass('remove')
				.addClass('add')
				.find('span:first')
				.html('Add to cart');
				
				$('#cart-count').html(cart_count);
			}
			else
			{
				if( item_action == 'is_cart_member' )
				{
					if( objResponse.is_member )
					{
						$(obj)
						.addClass('remove')
						.find('span:first')
						.html('Remove from cart');
					}
					$('#cart-count').html(cart_count);
				}
				else
				{	
					cart_count++;
					(cart_count == 1)? i = 'There is now '+ cart_count + ' item' : i = 'There are now ' + cart_count + ' items';
					
					//showDialog('', 'The Item <span class="bold">"' + title + '"</span> with ItemID <span class="bold">' + vendor + '</span> has been <span class="bold plus-one">added</span> to your cart.<br>' + i + ' in your cart.<br><br>To view your cart click on the cart link at the top-right corner of the page.');
					
					$(obj)
					.removeClass('add')
					.addClass('remove')
					.find('span:first')
					.html('Remove from cart');
					
					$('#cart-count').html(cart_count);
					action.enable();
					
					if (objResponse.redirect != null) 
						window.location.href = objResponse.redirect;
					else
						window.location.href = 'my-cart';

					
				}
			}			
		}
	}
	
	function handleError(XHR, error, except)
	{
		if( error == 'timeout' )
		{
			if( item_action == 'add_to_cart' )
			{
				alert('Oops, there was an error while\nadding this item to your cart.\nPlease reload this page to try again.');
			}
			else
			{
				alert('Oops, there was an error while\nverifying that this item is in your cart.\nPlease reload this page to try again.');
			}
		}
		else
		{
			alert(error + ' : ' + except);
		}
	}
	
	function handleComplete(XHR, status)
	{
		//alert('Completed: ' + status);
	}
	
	function handleFilter(data, type)
	{
		return data;
	}
	
	$.ajax(
	{
		type		:	'POST',
		url			:	cartDataSrc,
		dataType	: 	'html',
		data		:	({itemid: item_id, action: item_action, purchase_type: purchasetype, purchase_format: purchaseformat}),
		success		:	handleData,
		cache		:	true,
		async		:	true,
		error		:	handleError,
		complete	:	handleComplete,
		dataFilter	:	handleFilter,
		timeout		:	60000
	});
}
