var url_addProductToBasket = website_url + 'winkelwagen/add';
var txt_totalPrice = '<br /><strong>Totaal: &euro; ';
var txt_totalDiscount = 'Voordeel: &euro; ';

var shopping_cart_div = false;


var ajaxObjects = new Array();

function showAjaxBasketContent(ajaxIndex)
{
	// Getting a reference to the shopping cart items table
	var itemBox = document.getElementById('shopping_cart_items');

	var productItems = ajaxObjects[ajaxIndex].response.split('|||');	// Breaking response from Ajax into tokens

	if(document.getElementById('shopping_cart_items_product' + productItems[0])){	// A product with this id is allready in the basket - just add number items
		var row = document.getElementById('shopping_cart_items_product' + productItems[0]);
		var items = row.cells[1].innerHTML /1;
		items = productItems[3];
		price = productItems[2];
		row.cells[1].innerHTML = '<p>'+items+'</p>';
		row.cells[3].innerHTML = '<p class=\"'+ productItems[6] +'">&#8364; ' + price +'</p>';
	}else{	// Product isn't allready in the basket - add a new row
		var tr = itemBox.insertRow(-1);
		tr.id = 'shopping_cart_items_product' + productItems[0]

		var td = tr.insertCell(-1);
	if(productItems[9] == 0){
		var a = document.createElement('A');
		td.appendChild(a);
		a.href = website_url + 'products/' + productItems[0] + '_' + productItems[1];
		a.onclick = function(){ delProduct(productItems[0]); };
		a.innerHTML = '<p>'+ productItems[1] + '</p>';
	}else{
	td.innerHTML = '<p>'+ productItems[1] + '</p>';
	
		
}
		var td = tr.insertCell(-1);
		td.innerHTML = '<p>'+ productItems[3] +'</p>'; 	// Number of items
		
		var td = tr.insertCell(-1);
		td.style.width = "1.5em";
		var a = document.createElement('A');
		td.appendChild(a);
		a.href = '#';
		if(productItems[13]/1 == '6 '){
		a.onclick = function(){ addMultiProduct(productItems[0]); };
		}else{
		a.onclick = function(){ addSingleProduct(productItems[0]); };
		}
		var img = document.createElement('IMG');
		img.src = website_url + 'images/plus.gif';
		img.alt = '+1';
		a.appendChild(img);
		var a = document.createElement('A');
		td.appendChild(a);
		a.href = '#';
				if(productItems[13]/1 == '6'){
		a.onclick = function(){ delMultiProduct(productItems[0]); };
		}else{
		a.onclick = function(){ delProduct(productItems[0]); };
		}
		var img = document.createElement('IMG');
		img.src = website_url + 'images/min.gif';
		img.alt = '-1'
		a.appendChild(img);
		//td.innerHTML = '<a href="#" onclick="removeProductFromBasket("' + productItems[0] + '");return false;"><img src="images/remove.gif"></a>';
		
		var td = tr.insertCell(-1);
		td.style.textAlign = 'right';
		var pricenew = productItems[2];
		td.innerHTML = '<p class=\"'+ productItems[6] +'">&#8364; ' + pricenew +'</p>'; 	// Price
	}
var total = productItems[4];
document.getElementById('shopping_cart_totalprice').innerHTML = txt_totalPrice + total + '</strong>';
document.getElementById('shopping_cart_view').style.display='';
//	updateTotalPrice(productItems[4]);

	ajaxObjects[ajaxIndex] = false;

}

function removeProductFromBasket(ajaxIndex){
var productItems = ajaxObjects[ajaxIndex].response.split('|||');	// Breaking response from Ajax into tokens
var row = document.getElementById('shopping_cart_items_product' + productItems[0]);
	if(productItems[3]/1 == '' || productItems[3]/1 == '0'){
		row.parentNode.removeChild(row);
	}else{
		price = productItems[2];
		row.cells[1].innerHTML = '<p>'+ productItems[3] +'</p>';
		row.cells[3].innerHTML = '<p class=\"'+ productItems[6] +'">&#8364; ' + price +'</p>';
	}

var total = productItems[4];
document.getElementById('shopping_cart_totalprice').innerHTML = txt_totalPrice + total;
}

function delProduct(productId)
{
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = url_addProductToBasket;	// Saving product in this file
	ajaxObjects[ajaxIndex].setVar('delproductId',productId);
	ajaxObjects[ajaxIndex].onCompletion = function(){ removeProductFromBasket(ajaxIndex); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function
}

function delMultiProduct(productId)
{
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = url_addProductToBasket;	// Saving product in this file
	ajaxObjects[ajaxIndex].setVar('delmultiproductId',productId);
	ajaxObjects[ajaxIndex].onCompletion = function(){ removeProductFromBasket(ajaxIndex); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function
}

function addSingleProduct(productId)
{
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = url_addProductToBasket;	// Saving product in this file
	ajaxObjects[ajaxIndex].setVar('addproductId',productId);
	ajaxObjects[ajaxIndex].onCompletion = function(){ showAjaxBasketContent(ajaxIndex); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function
}

function addMultiProduct(productId)
{
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = url_addProductToBasket;	// Saving product in this file
	ajaxObjects[ajaxIndex].setVar('addmultiproductId',productId);
	ajaxObjects[ajaxIndex].onCompletion = function(){ showAjaxBasketContent(ajaxIndex); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function
}