// Shopping Cart AJAX Functions

	function Trim(str)
	{  
		str = str.replace(/[\r\n]+/g, '');
		str = str.replace(/^\s*/g,'');
		str = str.replace(/\s*$/g,'');
		return str;
	}

	function add_to_cart(product_id, charity_id) {
		var date_now = new Date();
		$.ajax({
			url: 'add_to_cart.cfm?product=' + product_id + '&charity=' + charity_id + '&ts=' + date_now.getTime(),
			type: 'GET',
			dataType: 'html',
			timeout: 1000,
			error: function() {
				alert('Unable to add item to cart.');
			},
			success: function(text) {
				text = Trim(text);
				alert(text + ' was successfully added to your shopping cart!');
			}
		});
	}

	function readd_to_cart(product_id, charity_id) {
		var date_now = new Date();
		$.ajax({
			url: 'add_to_cart.cfm?product=' + product_id + '&charity=' + charity_id + '&ts=' + date_now.getTime(),
			type: 'GET',
			dataType: 'html',
			timeout: 1000,
			error: function() {
				alert('Unable to add item to cart.');
			},
			success: function(text) {
				var product_price = parseFloat($('#prod_' + product_id + '_price').text());
				var in_cart = $('#prod_'+product_id+' > li > ul > li > span.in_cart').text();
				var cart_total = parseFloat($("#cart_total").text());
				in_cart = parseInt(in_cart) + 1;
			  $('#prod_'+product_id+' > li > ul > li > span.in_cart').html(in_cart);
				cart_total = cart_total + product_price;
				$("#cart_total").html(cart_total.toFixed(2));				
			}
		});
	}

	function remove_from_cart(product_id) {
		var date_now = new Date();
		$.ajax({
    	url: 'remove_from_cart.cfm?product='+product_id + '&ts=' + date_now.getTime(),
	    type: 'GET',
	    dataType: 'text',
	    timeout: 1000,
	    error: function(){
	        alert('Unable to remove item from cart.');
	    },
	    success: function(text){
				var product_price = parseFloat(text);
				var in_cart = $('#prod_'+product_id+' > li > ul > li > span.in_cart').text();
				var cart_total = parseFloat($("#cart_total").text());
				in_cart = parseInt(in_cart) - 1;
				if (in_cart < 1) {
					$('#prod_'+product_id+' > li > ul > li > span.in_cart').html("0");
				} else {
				  $('#prod_'+product_id+' > li > ul > li > span.in_cart').html(in_cart);
				}
				$("#cart_total").html((cart_total - product_price).toFixed(2));				
	    }
		});
	}
