/************************************************************************************************************
(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 = 'http://'+ url +'/website/contactlenzen-jeuris/site/addProduct.php';
var url_removeProductFromBasket = 'http://' + url + '/website/contactlenzen-jeuris/site/removeProduct.php';
var txt_totalPrice = 'Û ';

var ajaxObjects = new Array();

function move() {
	window.location = 'http://' + url + '/popup/be-nl/0/offerte/show/';
}

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[0].innerHTML /1;
		//items = items + 1;
		//row.cells[0].innerHTML = items;
		alert("This product is already in your basket.");
	}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.className = 'number_of_items';
		td.innerHTML = '1'; 	// Number of items
		*/
		
		var td = tr.insertCell(-1);
		var a = document.createElement('A');
		td.appendChild(a);
		td.className = 'trashcan';
		a.href = '#';
		a.onclick = function(){ removeProductFromBasket(productItems[0]); };
		var img = document.createElement('IMG');
		img.src = 'http://flexscripts.browsbox.be/templates/flexscripts/images/trashcan.gif';
		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.className = 'productName';
		td.innerHTML = productItems[1]; 	// Description
		
		var td = tr.insertCell(-1);
		td.style.textAlign = 'right';
		td.className = 'productPrice';
		td.innerHTML = productItems[2]; 	// Price	
		
			} 


	updateTotalPrice();
	
	ajaxObjects[ajaxIndex] = false;		
	
}
/*
function updateTotalPrice()
{
	var itemBox = document.getElementById('shopping_cart_items');
	// Calculating total price and showing it below the table with basket items
	var totalPrice = 0;
	if(document.getElementById('basket-total-price')){
		for(var no=0;no<itemBox.rows.length;no++){
			totalPrice = totalPrice + parseFloat(itemBox.rows[no].cells[2].innerHTML);
			
		}		
		document.getElementById('basket-total-price').innerHTML = "&euro; " + totalPrice.toFixed(2);
		if(itemBox.rows.length == 0){
			document.getElementById('basket_empty').style.visibility = 'visible';
			document.getElementById('basket_empty').style.height = '20px';
		} else {
			document.getElementById('basket_empty').style.visibility = 'hidden';
			document.getElementById('basket_empty').style.height = '0px';
		}
	}	
	
}
*/

function checkIfEmpty(link)
{
	var itemBox = document.getElementById('shopping_cart_items');
	if(document.getElementById('basket-total-price')){
		if(itemBox.rows.length == 0){
			alert("Your shopping basket is empty.");
			return false;
		} else {
			//alert(link);
			window.location = link;
		}
	}	
	
}

function removeProductFromBasket(productId)
{
	var productRow = document.getElementById('shopping_cart_items_product' + productId);
	
	//var numberOfItemCell = productRow.cells[0];
	//if(numberOfItemCell.innerHTML == '1'){
		productRow.parentNode.removeChild(productRow);	
	//}else{
	//	numberOfItemCell.innerHTML = numberOfItemCell.innerHTML/1 - 1;
	//}
	updateTotalPrice();
	ajaxRemoveProduct(productId);	
}

function ajaxValidateRemovedProduct(ajaxIndex,tablerow)
{
	//alert(ajaxObjects[ajaxIndex].response);
	//alert(ajaxObjects[ajaxIndex].response);
	//var returnBasket = new Array();
	//returnBasket = ajaxObjects[ajaxIndex].response;
	//alert(returnVar);
	/*for (i=0;i<returnBasket.length;i++)
	{
		alert(returnBasket[i] + "<br >");
	} */
	//alert(returnBasket[0]);
	
	deleteRow(tablerow);
	
	//alert(ajaxObjects[ajaxIndex].response);
	
	var DeleteRespons = ajaxObjects[ajaxIndex].response.split('|||');
	//alert(DeleteRespons[0]);
	if(DeleteRespons[0] == '0'){
		document.getElementById('product-detail-content').innerHTML = '<p>&nbsp;</p><p>&nbsp;</p>Er bevinden zich geen producten in uw winkelmandje.<br /><br /><a href="/index/be-nl/2098/">Klik hier</a> om naar de lenshop te gaan.';
	} else { //update price
		//update total price;
		//alert(DeleteRespons[3]);
		
		document.getElementById('span-totalprice').innerHTML = DeleteRespons[3];
		
		//update delivery costs
		var price_noedit = DeleteRespons[4];
		var deliveryfee = 0;
		//alert(price_noedit);
		//if(price_noedit >= 30 && price_noedit < 50) {
		if(price_noedit < 50) {
			deliveryfee = '7,00';
			document.getElementById('deliveryfee').innerHTML = deliveryfee;
		} else if(price_noedit >= 50) {
			deliveryfee = '4,00';	
			document.getElementById('deliveryfee').innerHTML = deliveryfee;
		} /*else {
			checkboxes = document.getElementsByName('verzending');
			var i = 0;
			while(checkbox = checkboxes[i]){
				if(i == 1) {
					checkbox.disabled = true;
					document.getElementById('radio_levering').innerHTML = '(Levering onder de 30&euro; niet mogelijk)';
				}
				i++;
			}
		}*/
		//alert(DeleteRespons[1]);
		//alert(DeleteRespons[2]);
		var rowID = DeleteRespons[1];
		var rowPrice = DeleteRespons[2];
		var cellID = 10;
		changeContent(rowID,cellID,rowPrice);
		
		
		
		
	}
}

function ajaxRemoveProduct(productId,eye,tablerow)
{
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = url_removeProductFromBasket;	// Saving product in this file
	ajaxObjects[ajaxIndex].setVar('productIdToRemove',productId);
	//ajaxObjects[ajaxIndex].setVar('packToRemove',pack);
	ajaxObjects[ajaxIndex].setVar('eyeToRemove',eye);
	ajaxObjects[ajaxIndex].onCompletion = function(){ 
		ajaxValidateRemovedProduct(ajaxIndex,tablerow); 
	};	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function		
}

function ajaxAddProduct(productId,links_ok,rechts_ok)
{
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = url_addProductToBasket;	// Saving product in this file
	ajaxObjects[ajaxIndex].setVar('productId',productId);
	//alert('rechts ok = '+links_ok);
	if(links_ok == true) {
		var aantal_links = trim(document.getElementById("select" + "aantal_links").childNodes[0].nodeValue);
		
		//if(aantal_links == 0) alert('links is 0');
		
		var verpakking_links = trim(document.getElementById("select" + "verpakking_links").childNodes[0].nodeValue);
		ajaxObjects[ajaxIndex].setVar('aantal_links',aantal_links);
		ajaxObjects[ajaxIndex].setVar('verpakking_links',verpakking_links);
		//bc
		if (document.getElementById("select" + "bc_links") != null) {
			var bc_links = trim(document.getElementById("select" + "bc_links").childNodes[0].nodeValue);
			ajaxObjects[ajaxIndex].setVar('bc_links',bc_links);
		}
		//pwr
		if (document.getElementById("select" + "pwr_links") != null) {
			var pwr_links = trim(document.getElementById("select" + "pwr_links").childNodes[0].nodeValue);
			ajaxObjects[ajaxIndex].setVar('pwr_links',pwr_links);
		}
		//as
		if (document.getElementById("select" + "as_links") != null) {
			var as_links = trim(document.getElementById("select" + "as_links").childNodes[0].nodeValue);
			ajaxObjects[ajaxIndex].setVar('as_links',as_links);
		}
		//additie
		if (document.getElementById("select" + "additie_links") != null) {
			var additie_links = trim(document.getElementById("select" + "additie_links").childNodes[0].nodeValue);
			ajaxObjects[ajaxIndex].setVar('additie_links',additie_links);
		}
		//cyl
		if (document.getElementById("select" + "cyl_links") != null) {
			var cyl_links = trim(document.getElementById("select" + "cyl_links").childNodes[0].nodeValue);
			ajaxObjects[ajaxIndex].setVar('cyl_links',cyl_links);
		}
	}
	//alert('rechts ok = '+rechts_ok);
	if(rechts_ok == true) {
		var aantal_rechts = 0;
		if(document.getElementById("select" + "aantal_rechts")) aantal_rechts = trim(document.getElementById("select" + "aantal_rechts").childNodes[0].nodeValue);
		var verpakking_rechts = 0;
		if(document.getElementById("select" + "verpakking_rechts")) verpakking_rechts = trim(document.getElementById("select" + "verpakking_rechts").childNodes[0].nodeValue);
		ajaxObjects[ajaxIndex].setVar('aantal_rechts',aantal_rechts);
		ajaxObjects[ajaxIndex].setVar('verpakking_rechts',verpakking_rechts);
		//bc
		if (document.getElementById("select" + "bc_rechts") != null) {
			var bc_rechts = trim(document.getElementById("select" + "bc_rechts").childNodes[0].nodeValue);
			ajaxObjects[ajaxIndex].setVar('bc_rechts',bc_rechts);
		}
		//pwr
		if (document.getElementById("select" + "pwr_rechts") != null) {
			var pwr_rechts = trim(document.getElementById("select" + "pwr_rechts").childNodes[0].nodeValue);
			ajaxObjects[ajaxIndex].setVar('pwr_rechts',pwr_rechts);
		}
		//as
		if (document.getElementById("select" + "as_rechts") != null) {
			var as_rechts = trim(document.getElementById("select" + "as_rechts").childNodes[0].nodeValue);
			ajaxObjects[ajaxIndex].setVar('as_rechts',as_rechts);
		}	
		//additie
		if (document.getElementById("select" + "additie_rechts") != null) {
			var additie_rechts = trim(document.getElementById("select" + "additie_rechts").childNodes[0].nodeValue);
			ajaxObjects[ajaxIndex].setVar('additie_rechts',additie_rechts);
		}
		//cyl
		if (document.getElementById("select" + "cyl_rechts") != null) {
			var cyl_rechts = trim(document.getElementById("select" + "cyl_rechts").childNodes[0].nodeValue);
			ajaxObjects[ajaxIndex].setVar('cyl_rechts',cyl_rechts);
		}
	}
	//alert('aantal links = ' + aantal_links + ' & aantal rechts = ' + aantal_rechts + ' & verpakking_links = ' + verpakking_links + ' & verpakking_rechts = ' + verpakking_rechts);
	//alert(productId);
	
	ajaxObjects[ajaxIndex].onCompletion = function(){ 
		//update the items in the basket
		//alert(ajaxObjects[ajaxIndex].response);
		document.getElementById('shoppingbasket-text').innerHTML = 'winkelmandje (' + ajaxObjects[ajaxIndex].response + ')';
		
		//show text that the product has been added
		if(document.getElementById('basketimage').src == 'http://contactlenzen-jeuris-nl.browsbox.be/templates/contactlenzen-jeuris/images/btn_pasaan.gif') {
			document.getElementById('basket-actions').innerHTML = 'Het product werd aangepast in het winkelmandje';
			
		} else {
			document.getElementById('basket-actions').innerHTML = 'Het product werd toegevoegd aan het winkelmandje';
			document.getElementById('basketimage').src = 'http://contactlenzen-jeuris-nl.browsbox.be/templates/contactlenzen-jeuris/images/btn_pasaan.gif';
			
		}
		
		// redirect to basket
		setTimeout('move()',1250);
		
		//replace the button
		
	};	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function	

}

function ajaxAddProductReorder(purchaseID)
{
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = 'http://'+ url +'/website/contactlenzen-jeuris/site/addProductReorder.php';	// Saving product in this file
	ajaxObjects[ajaxIndex].setVar('purchaseID',purchaseID);
	
	
	ajaxObjects[ajaxIndex].onCompletion = function(){ 
		
		if(ajaxObjects[ajaxIndex].response == 'all'){
			alert('De producten van deze bestelling werden toegevoegd aan het winkelmandje');
		} else {
			alert('Sommige producten van deze bestelling zijn niet meer leverbaar en kunnen niet worden toegevoegd aan het winkelmandje.');
		}// redirect to basket
		setTimeout('move()',1000);
		
	};	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function	

}
