
	ShoppingCart = {
	
		AddProduct: function( id ) {
			
			new Ajax.Request('/ajax.php', {
				
				method: 'post',
				postBody: 'action=AddProductToCart&id=' + id,
				
				onLoading: function() {
					$('cart_info_status').update( 'Updating cart..' );
				},
				
				onSuccess: function( bus ) {
					var response = JSON.PrepareResponse( bus.responseText );
					$('cart_info_status').update( response.content );
					if ($('cart_info_status2')) $('cart_info_status2').update( response.content );
					
					// Change the button
					if ($('add_to_cart_' + id)) $('add_to_cart_' + id).src = 'images/added.gif';
					
				}
				
					
			});

		},
		
		AddPackage: function( id ) {
			
			// Check that coop didnt remove the id from the select box
			if(! $('package_select')) alert('DEBUG: The id of the select box needs to be \'package_select\'');
			
			var package_id = $F('package_select');
			if(package_id == 0) return true;
			
			new Ajax.Request('/ajax.php', {
				
				method: 'post',
				postBody: 'action=AddPackageToCart&id=' + package_id,
				
				onLoading: function() {
					$('cart_info_status').update( 'Updating cart..' );
				},
				
				onSuccess: function( bus ) {
					var response = JSON.PrepareResponse( bus.responseText );
					$('cart_info_status').update( response.content );
					if ($('cart_info_status2')) $('cart_info_status2').update( response.content );
				}
				
					
			});
			
		},
		
		RemoveProduct: function( id, container_id ) {
			
			var container = $(container_id);
			if(! container) {
				alert('DEBUG: id ' + container_id + ' missing' );
				return false;
			} 
		
			this.RemoveItem( 'Product', id, container );
			
		},
		
		RemovePackage: function( id, container_id ) {
		
			var container = $(container_id);
			if(! container) {
				alert('DEBUG: id ' + container_id + ' missing' );
				return false;
			} 
		
			this.RemoveItem( 'Package', id, container );
		},
		
		RemoveItem: function( item_type, id, container ) {
			
			// Set up the item specific variables
			if(item_type == 'Package') {
				var sub_total = $('packages_total');
				var sub_container = $('cart_packages');
			}
			else if (item_type == 'Product') {
				var sub_total = $('products_total');
				var sub_container = $('cart_products');
			}
			else {
				return false;
			}
			
			var post_body = 'action=Remove' + item_type + 'FromCart&id=' + id;
			
				
			// Send the request
			new Ajax.Request('/ajax.php', {
				
				method: 'post',
				postBody: post_body,
				
				onLoading: function() {
					$('cart_info_status').update( 'Updating cart..' );
				},
				
				onSuccess: function( bus ) {
					
					var response = JSON.PrepareResponse( bus.responseText );
					
					$('cart_info_status').update( response.cart_info ); // Check all these divs
					sub_total.update( response.sub_total );
					$('grand_total').update( response.grand_total );
					$('paypal_container').update( response.paypal_button );
					
					if (response.status == 'true') 
						container.remove();
						
					if(response.no_more) 
						sub_container.update( response.no_more );
						
					if(response.cart_empty) {
						$('cart_container').update( response.cart_empty );
						$('paypal_container').update();
					}

				}
					
			});
			
		},
		
		TosCheck: function() {
			if (! $('tos').checked) {
				alert('You must accept the Terms of Service before purchasing');
				return false;
			}
			else
				return true;
		}
	
	};
	
	
	
	
	JSON = {
	
		PrepareResponse: function( responseText ) {
		
			var response = eval('(' + responseText + ')') || { content: 'No response from server' };
			
			if (response.error) { 
				alert( response.error );
			}
			
			if (response.notify) { 
				alert( response.notify );
			}
			
			return response;
			
		}
		
	};