//=============================================================================
// Initialize
//============================== GIJS - OCCHIO ================================

// Get DOM Objects
var tablePrices = document.getElementById("priceTable");
var aAmountCells = getElementsByClassName(tablePrices, "td", "chooseNumber");
var aTotalCells = getElementsByClassName(tablePrices, "td", "totalPrice");
var tdPriceSum = document.getElementById("priceSum");
if(window.console) {
			window.console.log("Laden...");
		} 
// Cookie expiration
var expirationDays = 5;
var expire = new Date();
var today = new Date();
expire.setTime(today.getTime() + 3600000*24*expirationDays);

// Assign onclick and onchange events
for(var i=0; i < aAmountCells.length; i++) {
	
	var imgPlus = aAmountCells[i].getElementsByTagName("img")[0];
	imgPlus.onclick = function() {
		if(window.console) {
			window.console.log("Klikkerdeklik");
		} 
	
		var inputAmount = this.parentNode.parentNode.getElementsByTagName("input")[0];
		if(parseInt(inputAmount.value) < 99) {
			inputAmount.value = parseInt(inputAmount.value) + 1; 
			calculateSubTotal(this.parentNode.parentNode.getElementsByTagName("input")[0]);
		}
	}

	var imgMinus = aAmountCells[i].getElementsByTagName("img")[1];
	imgMinus.onclick = function() {
		var inputAmount = this.parentNode.parentNode.getElementsByTagName("input")[0];
		if(parseInt(inputAmount.value) > 0) {
			inputAmount.value = parseInt(inputAmount.value) - 1;
			calculateSubTotal(this.parentNode.parentNode.getElementsByTagName("input")[0]);
		} 
	}
	
	var inputAmount = aAmountCells[i].getElementsByTagName("input")[0];
	calculateSubTotal(inputAmount);
	inputAmount.onchange = function() {
		if(isNaN(this.value-0)) {
			this.value = 0;
		}

		// Calculate everything on load
		calculateSubTotal(this);
	}
}

//=============================================================================
// FUNCTION Calculate Subtotal
//============================== GIJS - OCCHIO ================================

function calculateSubTotal(oObject) {
	// Initialize
	var sPriceCell = getElementsByClassName(oObject.parentNode.parentNode, "td", "price")[0];
	var sTotalCell = getElementsByClassName(oObject.parentNode.parentNode, "td", "totalPrice")[0];
	var iAmount = parseInt(oObject.value);
	var rowIndex = oObject.parentNode.parentNode.id.substring(oObject.parentNode.parentNode.id.length - 1);
	var iPrice = aPrices[rowIndex];

	// Update total of row
	sTotalCell.innerHTML = addValuta(iAmount * iPrice);
	
	calculateTotal();

	document.cookie = 'zuiderzeemuseum-amount-' + rowIndex + '=' + oObject.value + ';expires=' + expire.toGMTString();
}

//=============================================================================
// FUNCTION Add Valuta
//============================== GIJS - OCCHIO ================================
function calculateTotal() {
	var iTotalPrice = parseInt(0);
	
	for(var i = 0; i < aAmountCells.length; i++) {
		var iAmount = aAmountCells[i].getElementsByTagName("input")[0].value;
		var iPrice = aPrices[i];
		iTotalPrice += iAmount * iPrice;
	}

	tdPriceSum.innerHTML = addValuta(iTotalPrice);
}
	
//=============================================================================
// FUNCTION Add Valuta
//============================== GIJS - OCCHIO ================================

function addValuta(iPrice) {
	iPrice = iPrice.toFixed(2)
	sPrice = iPrice + '';
	var sPrice = sPrice.replace(/\./g, ",");
	sPrice = "\&euro; " + sPrice;
	return(sPrice);
}

//=============================================================================
// FUNCTION Get Elements By Classname
//============================== GIJS - OCCHIO ================================
function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}
