/************************************************************************************************************
(C) www.dhtmlgoodies.com, March 2006

This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.

Terms of use:
You are free to use this script as long as the copyright message is kept intact. However, you may not
redistribute, sell or repost it without our permission.

Version:
	1.0	Released	March. 3rd 2006

Thank you!

www.dhtmlgoodies.com
Alf Magne Kalleland

************************************************************************************************************/

var url_addProductToBasket = website_url + 'shoppingcart/add';

var shopping_cart_div = false;

var ajaxObjects = new Array();

function showAjaxBasketContent(ajaxIndex,big)
{
	// 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[0].innerHTML /1;
		items = productItems[3];
		price = productItems[2];
		row.cells[0].innerHTML = '<p>'+items+'</p>';
		row.cells[2].innerHTML = '<p class=\"'+ productItems[6] +'">' + 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);
		td.innerHTML = '<p>'+ productItems[3] +'</p>'; 	// Number of items

		var td = tr.insertCell(-1);

		var a = document.createElement('A');
		td.appendChild(a);
		a.href = website_url + productItems[9];
		a.onclick = function(){ delProduct(productItems[0]); };
		a.innerHTML = '<p>'+ productItems[1] + '</p>';


		var td = tr.insertCell(-1);
		td.style.textAlign = 'right';
		var pricenew = productItems[2];
		td.innerHTML = '<p class=\"'+ productItems[6] +'">' + pricenew +'</p>'; 	// Price

	}
	var total = productItems[4];
	document.getElementById('shopping_cart_totalprice').innerHTML = total;
	//document.getElementById('shopping_cart_totaldiscount').innerHTML = txt_totalDiscount + productItems[7];
	//	updateTotalPrice(productItems[4]);

	if(big > 0){
		var row = document.getElementById('shopping_cart_big_items_product' + productItems[0]);
		var items = row.cells[2].innerHTML /1;
		items = productItems[3];
		price = productItems[2];
		row.cells[2].innerHTML = items;
		row.cells[4].innerHTML = '<p class=\"'+ productItems[6] +'">&euro; ' + price + '</p>';
		document.getElementById('shopping_cart_big_totalprice').innerHTML = '' + total + '';
	}

	ajaxObjects[ajaxIndex] = false;

}



function removeProductFromBasket(ajaxIndex,all){

	var productItems = ajaxObjects[ajaxIndex].response.split('|||');	// Breaking response from Ajax into tokens
	var row = document.getElementById('shopping_cart_items_product' + productItems[0]);
	var row2 = document.getElementById('shopping_cart_big_items_product' + productItems[0]);

	if(productItems[3]/1 == '' || productItems[3]/1 == '0' || all == '1'){
		row.parentNode.removeChild(row);
		row2.parentNode.removeChild(row2);
	}else{
		price = productItems[2];
		row.cells[0].innerHTML = '<p>'+ productItems[3] +'</p>';
		row.cells[2].innerHTML = '<p class=\"'+ productItems[6] +'">' + price +'</p>';
		row2.cells[2].innerHTML = productItems[3];
		row2.cells[4].innerHTML = '<p class=\"'+ productItems[6] +'">&euro; ' + price + '</p>';
	}

	var total = productItems[4];
	document.getElementById('shopping_cart_totalprice').innerHTML = total;
	//document.getElementById('shopping_cart_totaldiscount').innerHTML = txt_totalDiscount + productItems[7];

	var total = productItems[4];
	document.getElementById('shopping_cart_big_totalprice').innerHTML = '' + total + '';
	//document.getElementById('shopping_cart_big_totalitems').innerHTML = '<p>' + productItems[8] + '</p>';
}



function delProduct(productId,all)
{
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = url_addProductToBasket;	// Saving product in this file
	if(all > 0){
		ajaxObjects[ajaxIndex].setVar('delallId',productId);
	}else{
		ajaxObjects[ajaxIndex].setVar('delproductId',productId);
	}
	ajaxObjects[ajaxIndex].onCompletion = function(){ removeProductFromBasket(ajaxIndex,all); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function
}
function removeProduct(productId)
{
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = url_addProductToBasket;	// Saving product in this file
	ajaxObjects[ajaxIndex].setVar('remproductId',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 addProductToCart(productId,option)
{
	if(option){
		var optionid = document.getElementById('productoption').value;
	}
	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].setVar('productoption',optionid);
	ajaxObjects[ajaxIndex].onCompletion = function(){ showAjaxBasketContent(ajaxIndex); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function
}
function updateProduct(productId)
{
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = url_addProductToBasket;	// Saving product in this file
	ajaxObjects[ajaxIndex].setVar('updateproductId',productId);
	ajaxObjects[ajaxIndex].onCompletion = function(){ showAjaxBasketContent(ajaxIndex,'1'); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function
}

