/* ========================================================================= */
/* ****	Website Builder - Copyright (C)2007-2009 by EasyStar Software
/* ========================================================================= */

/************************************************************************************************************
This script is based on the original script from www.dhtmlgoodies.com.
Original Copyright by (C) www.dhtmlgoodies.com, March 2006

Terms of use from www.dhtmlgoodies.com:
-----------------------------------------------------------------------------------------------------
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

www.dhtmlgoodies.com
Alf Magne Kalleland
-----------------------------------------------------------------------------------------------------
New version: 1.1 by EasyStar Software - www.easystar.nl - Richard Nijmeijer - November 2007
Several modifications and improvements to adapt script in Easy WebSite Builder(TM) by EasyStar Software

Terms of use from EasyStar Software:
-----------------------------------------------------------------------------------------------------
You are free to use this script with updates as long as this complete copyright message is kept intact.
************************************************************************************************************/

var shopping_cart_div = false;
var flyingDiv = false;
var currentProductDiv = false;
var shopping_cart_x = false;
var shopping_cart_y = false;
var slide_xFactor = false;
var slide_yFactor = false;
var diffX = false;
var diffY = false;
var currentXPos = false;
var currentYPos = false;
var ajaxObjects = new Array();

function shoppingCart_getTopPos(inputObj) {		
  var returnValue = inputObj.offsetTop;
  while ((inputObj = inputObj.offsetParent) != null) {
  	if (inputObj.tagName!='HTML') returnValue += inputObj.offsetTop;
  }
  return returnValue;
}

function shoppingCart_getLeftPos(inputObj) {
  var returnValue = inputObj.offsetLeft;
  while ((inputObj = inputObj.offsetParent) != null) {
  	if (inputObj.tagName!='HTML') returnValue += inputObj.offsetLeft;
  }
  return returnValue;
}
	
function addToBasket(productId) {
	openRightSlidePanel();
	soundManager.play('addItem');
	document.getElementById('cartLoadingDiv').style.visibility = "visible";
	if (!shopping_cart_div) shopping_cart_div = document.getElementById('shoppingCartContent');
	if (!flyingDiv) {
		flyingDiv = document.createElement('DIV');
		flyingDiv.style.position = 'absolute';
		document.body.appendChild(flyingDiv);
	}
	shopping_cart_x = shoppingCart_getLeftPos(shopping_cart_div);
	shopping_cart_y = shoppingCart_getTopPos(shopping_cart_div);
	currentProductDiv = document.getElementById('slidingProduct' + productId);
	currentXPos = shoppingCart_getLeftPos(currentProductDiv);
	currentYPos = shoppingCart_getTopPos(currentProductDiv);
	diffX = shopping_cart_x - currentXPos;
	diffY = shopping_cart_y - currentYPos;
	
	var shoppingContentCopy = currentProductDiv.cloneNode(true);
	shoppingContentCopy.id = '';
	flyingDiv.innerHTML = '';
	flyingDiv.style.left = currentXPos + 'px';
	flyingDiv.style.top = currentYPos + 'px';
	flyingDiv.appendChild(shoppingContentCopy);
	flyingDiv.style.display = 'block';
	flyingDiv.style.width = currentProductDiv.offsetWidth + 'px';
	flyToBasket(productId);
}

function flyToBasket(productId) {
	var maxDiff = Math.max(Math.abs(diffX),Math.abs(diffY));
	var moveX = (diffX / maxDiff) * flyingSpeed;;
	var moveY = (diffY / maxDiff) * flyingSpeed;	
	currentXPos = currentXPos + moveX;
	currentYPos = currentYPos + moveY;
	flyingDiv.style.left = Math.round(currentXPos) + 'px';
	flyingDiv.style.top = Math.round(currentYPos) + 'px';	
	if (moveX > 0 && currentXPos > shopping_cart_x) {
		flyingDiv.style.display = 'none';		
	}
	if (moveX < 0 && currentXPos < shopping_cart_x) {
		flyingDiv.style.display = 'none';		
	}
	if (flyingDiv.style.display == 'block') setTimeout('flyToBasket("' + productId + '")', 10); else ajaxAddProduct(productId);	
}

function getAjaxBasketContent(ajaxIndex) {
	if (ajaxObjects[ajaxIndex].response != "NULL") {
		var cartLoadDetails = ajaxObjects[ajaxIndex].response.split('###');
		var cartLoadAmount = cartLoadDetails[0];
        if (cartLoadDetails[1]) {
		    var cartLoadProductIDs = cartLoadDetails[1];
		    var productLoadDetails = cartLoadProductIDs.split('|||');
		    for (var p = 0; p < cartLoadAmount; p++) {
			    ajaxGetProduct(productLoadDetails[p]);
		    }
        }
	}
	updateTotalPrice();
	ajaxObjects[ajaxIndex] = false;		
}

function fillAjaxBasketContent(ajaxIndex) {
	var itemBoxRow;
	var itemBoxRowIndex = 0;
	var itemBox = document.getElementById('shoppingCartItems');
	var productItems = ajaxObjects[ajaxIndex].response.split('|||');

	if (document.getElementById('shoppingCartItemsProduct' + productItems[0])) {
		itemBoxRow = document.getElementById('shoppingCartItemsProduct' + productItems[0]);
		itemBoxRowIndex = itemBoxRow.rowIndex;
	}
	if (itemBoxRowIndex > 0) {
		var numberOfItemCell = itemBoxRow.cells[0];
		numberOfItemCell.innerHTML = productItems[1];
	} else {
		var tr = itemBox.insertRow(-1);
		tr.id = 'shoppingCartItemsProduct' + productItems[0];
		
		var td = tr.insertCell(-1);
		td.style.textAlign = 'center';
		td.innerHTML = productItems[1];
		
		var td = tr.insertCell(-1);
		td.style.textAlign = 'left';
		td.innerHTML = productItems[2];
		
		var td = tr.insertCell(-1);
		td.style.textAlign = 'right';
		td.innerHTML = productItems[3];
		
		var td = tr.insertCell(-1);
		var a = document.createElement('A');
		td.appendChild(a);
		a.onclick = function(){ removeProductFromBasket(productItems[0]); };
		var img = document.createElement('IMG');
		img.src = ''+HTML_PATH+'webfiles/webshop/shop/images/remove.gif';
		a.appendChild(img);
	}
	updateTotalPrice();
	document.getElementById('cartLoadingDiv').style.visibility = "hidden";
	ajaxObjects[ajaxIndex] = false;		
}

function updateTotalPrice() {
	var itemBox = document.getElementById('shoppingCartItems');
	var totalPrice = 0;
    var witems = 0;
	if (document.getElementById('shoppingCartTotals')) {
		for (var no = 1; no < itemBox.rows.length; no++) {
			totalPrice = totalPrice + (itemBox.rows[no].cells[0].innerHTML.replace(/[^0-9]/g) * itemBox.rows[no].cells[2].innerHTML);
            witems = witems + (itemBox.rows[no].cells[0].innerHTML.replace(/[^0-9]/) * 1);
		}		
		document.getElementById('shoppingCartTotals').innerHTML = txt_totalPrice + txt_valueSymbol + totalPrice.toFixed(2);
        if (document.getElementById('wcount')) {
            document.getElementById('wcount').innerHTML = (itemBox.rows.length - 1);
        }
        if (document.getElementById('wsum')) {
            document.getElementById('wsum').innerHTML = totalPrice.toFixed(2);
        }
        if (document.getElementById('witems')) {
            document.getElementById('witems').innerHTML = witems;
        }
	}	
}

function removeProductFromBasket(productId) {
	soundManager.play('delItem');
	var productRow = document.getElementById('shoppingCartItemsProduct' + 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 ajaxValidateAddedProduct(ajaxIndex) {
	if (ajaxObjects[ajaxIndex].response != 'OK') {
        if (ajaxObjects[ajaxIndex].response == 'ExpX') {
            alert('(X) Een fout is ontstaan tijdens het toevoegen van het product in de database!');
        } else if (ajaxObjects[ajaxIndex].response == 'Exp2') {
            alert('(2) Een fout is ontstaan tijdens het toevoegen van het product in de database!');
        } else if (ajaxObjects[ajaxIndex].response == 'Exp3') {
            alert('(3) Een fout is ontstaan tijdens het toevoegen van het product in de database!');
        } else if (ajaxObjects[ajaxIndex].response == 'Exp4') {
            alert('(4) Een fout is ontstaan tijdens het toevoegen van het product in de database!');
        } else {
            alert('(?) Een fout is ontstaan tijdens het toevoegen van het product in de database!');
        }
	} else {
		getCartContent();
	}
}

function ajaxValidateRemovedProduct(ajaxIndex) {
	if (ajaxObjects[ajaxIndex].response != 'OK') {
        if (ajaxObjects[ajaxIndex].response == 'ExpX') {
            alert('(X) Een fout is ontstaan tijdens het verwijderen van het product uit de database!');
        } else if (ajaxObjects[ajaxIndex].response == 'Exp6') {
            alert('(6) Een fout is ontstaan tijdens het verwijderen van het product uit de database!');
        } else if (ajaxObjects[ajaxIndex].response == 'Exp7') {
            alert('(7) Een fout is ontstaan tijdens het verwijderen van het product uit de database!');
        } else {
            alert('(-) Een fout is ontstaan tijdens het verwijderen van het product uit de database!');
        }
	} else {
		getCartContent();
	}
}

function getCartContent() {
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = url_getCartContent;
	ajaxObjects[ajaxIndex].onCompletion = function(){ getAjaxBasketContent(ajaxIndex); };
	ajaxObjects[ajaxIndex].runAJAX();
}

function ajaxGetProduct(productId) {
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = url_getProductDetails;
	ajaxObjects[ajaxIndex].setVar('productId',productId);
	ajaxObjects[ajaxIndex].onCompletion = function(){ fillAjaxBasketContent(ajaxIndex); };
	ajaxObjects[ajaxIndex].runAJAX();
}

function ajaxAddProduct(productId) {
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = url_addProductToBasket;
	ajaxObjects[ajaxIndex].setVar('productId',productId);
    if (document.getElementById('choice_'+productId+'_yn')) {
        ajaxObjects[ajaxIndex].setVar('choiceYN',document.getElementById('choice_'+productId+'_yn').value);
    } else if (document.getElementById('choice_'+productId+'_int')) {
        ajaxObjects[ajaxIndex].setVar('choiceINT',document.getElementById('choice_'+productId+'_int').value);
    } else if (document.getElementById('choice_'+productId+'_dec')) {
        ajaxObjects[ajaxIndex].setVar('choiceDEC',document.getElementById('choice_'+productId+'_dec').value);
    } else if (document.getElementById('choice_'+productId+'_txt')) {
        ajaxObjects[ajaxIndex].setVar('choiceTXT',document.getElementById('choice_'+productId+'_txt').value);
    } else {
        if (document.getElementById('choice_'+productId+'_sA')) {
            ajaxObjects[ajaxIndex].setVar('choiceSA',document.getElementById('choice_'+productId+'_sA').value);
        }
        if (document.getElementById('choice_'+productId+'_sB')) {
            ajaxObjects[ajaxIndex].setVar('choiceSB',document.getElementById('choice_'+productId+'_sB').value);
        }
    }
    if (document.getElementById('choice_'+productId+'_ID')) {
        ajaxObjects[ajaxIndex].setVar('choiceID',document.getElementById('choice_'+productId+'_ID').value);
    }
	ajaxObjects[ajaxIndex].onCompletion = function(){ ajaxValidateAddedProduct(ajaxIndex); };
	ajaxObjects[ajaxIndex].runAJAX();
}

function ajaxRemoveProduct(productId) {
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = url_removeProductFromBasket;
	ajaxObjects[ajaxIndex].setVar('productIdToRemove',productId);
	ajaxObjects[ajaxIndex].onCompletion = function(){ ajaxValidateRemovedProduct(ajaxIndex); };
	ajaxObjects[ajaxIndex].runAJAX();
}
