// <![CDATA[
function itemAdded(product_id, response) {

	if($('message_itemAdded')) {
		$('message_itemAdded').injectBefore($('product_container_'+product_id));
		$('message_itemAdded').setStyle('display','');
	}
	
	Cart.get_items();
}

function gotItems(response) {
	
	items = eval('(' + response + ')');
	
	if(items['cart'] != -1) {
		
		var total = 0;
		var table_cart = "<table border='0' cellpadding='2' cellspacing='0' width='190px'>";
		
			for(i=0;i<items['cart'].length;i++) {
				table_cart += "<tr>";
					table_cart += "<td width='15px'>";
						table_cart += "<a href='/' rel='nofollow' onclick='Cart.del_item(" + items['cart'][i].user_cart_item_id + ");return false;'><img src='/images/basket_delete.png' alt='' width='16px' height='16px' /></a>";
					table_cart += "</td>";
					
					table_cart += "<td width='35px' align='right'>";
						table_cart += "<em>" + items['cart'][i].quantity + "x</em>";
					table_cart += "</td>";
					
					table_cart += "<td width='85px' align='right'>";
						table_cart += items['cart'][i].name;
						if (items['cart'][i].extra != 'undefined') { table_cart += " (" + items['cart'][i].extra + ")"; }
					table_cart += "</td>";
					
					table_cart += "<td width='55px' style='font-size: 12px;' align='right'>";
						table_cart += "&euro; " + ((parseInt(items['cart'][i].price) * parseInt(items['cart'][i].quantity)) / 100).toFixed(2);
					table_cart += "</td>";
					
				table_cart += "</tr>";
				
				total = total + parseInt(items['cart'][i].price * parseInt(items['cart'][i].quantity));
			}
			
			table_cart += "<tr>";
				table_cart += "<td style='border-top: 1px solid #702A00;'>";
					table_cart += "&nbsp;";
				table_cart += "</td>";
				table_cart += "<td style='border-top: 1px solid #702A00;'>";
					table_cart += "&nbsp;";
				table_cart += "</td>";
				table_cart += "<td align='right' style='border-top: 1px solid #702A00;'>";
					table_cart += "<strong style='font-size: 12px;'>TOTAAL</strong>";
				table_cart += "</td>";
				table_cart += "<td align='right' style='border-top: 1px solid #702A00;'>";
					table_cart += "<strong style='font-size: 12px;'>&euro; " + (total / 100).toFixed(2) + "</strong>";
				table_cart += "</td>";
			table_cart += "</tr>";
			
		table_cart += "</table>";
		
		$('shoppingcart').innerHTML = table_cart;
	}
	else {
		$('shoppingcart').innerHTML = "De winkelwagen is leeg.";
	}
}

function itemDeleted(response) {
	
	if($('message_itemAdded')) {
		$('message_itemAdded').setStyle('display','none');
	}
	
	Cart.get_items();
}

var ShoppingCart = new Class({
	newCart: function(cart_id) {
		this.cart = cart_id;
	},
	
	add_item: function(product_id, quantity, extra) {
		//alert(quantity);
		var data = "action=add_item&cart_id=" + this.cart + "&product_id=" + product_id + "&quantity=" + quantity + "&extra=" + extra;
		new Ajax('/ajax_server/shoppingcart.php',{method: 'post', postBody: data, onComplete: function(response){itemAdded(product_id,response);}}).request();
	},
	
	get_items: function() {
		var data = "action=get_items&cart_id=" + this.cart;
		new Ajax('/ajax_server/shoppingcart.php',{method: 'post', postBody: data, onComplete: gotItems}).request();
	},
	
	del_item: function(product_id) {
		var data = "action=del_item&cart_id=" + this.cart + "&product_id=" + product_id;
		new Ajax('/ajax_server/shoppingcart.php',{method: 'post', postBody: data, onComplete: itemDeleted}).request();
	},
	
	del_items: function() {
		
		var confirmdelete = confirm("Weet u zeker dat u de winkelwagen leeg wilt maken?");
		
		if(confirmdelete) {
			var data = "action=del_cart&cart_id=" + this.cart;
			new Ajax('/ajax_server/shoppingcart.php',{method: 'post', postBody: data, onComplete: itemDeleted}).request();
		}
	},
	
	duplicate_cart: function() {
		var data = $('frm_quickselect').toQueryString();
		data += "&cart_id=" + this.cart;
		
		new Ajax('/ajax_server/shoppingcart.php',{method: 'post', postBody: data, onComplete: itemAdded}).request();
	}
	
	
});
// ]]>