/**
 * updatePrijs
 *
 * Gives totals and grandtotal in the uitvoeringen table
 *
 * @since Fri May 23 2008
 * @return integer
 **/
function updatePrijs(aantal, prijs, id, currencySymbol) {
	aantal = parseInt(aantal);

	if (isNaN(aantal) || aantal < 0) {
		return updatePrijs(0, prijs, id, currencySymbol);
	}
	
	var total = calculateTotalPrice(aantal, prijs);
	total = total.toFixed(2);
	total = (total + "").replace(".", ",");
	
	var totalcell = document.getElementById("total_" + id);
	var links = totalcell.getElementsByTagName("a");

	// Keep cms
	var div = document.createElement("div");
	if (links.length > 0) {
		for (i = 0; i < links.length; i++) {
			div.appendChild(links[i]);
		}
	}
	
	totalcell.innerHTML = currencySymbol + " " + total + div.innerHTML;
	
	return ( (aantal == 0) ? "" : aantal);
}

function calculateTotalPrice(aantal, prijs) {
	// The prijs should be ordered high numbers to low numbers
	if (aantal < 1) {
		return 0;
	}
	var prijs = $H(prijs);
	foundTop = false;
	
	var result = prijs.find(function(aantal, prijsHash, aantalTopPrijs, index) {
		if (foundTop || (index == 0 && aantalTopPrijs[0] <= aantal) ) {
			return true;
		}
		if (aantal < aantalTopPrijs[0] ) {
			var next = $A(prijsHash)[index + 1][0];
			if ( (!isNaN(next) && aantal >= next) || isNaN(next) ) {
				foundTop = true;
			}
		}
		return false;
	}.bind(window, aantal, prijs) );
	
	return parseFloat(result[1]) * aantal;
}

function updateWinkelwagen(formid, original, count) {
	if (original != count) {
		var form = document.forms["winkelwagenform_" + formid];
		form.count.value = count;
		form.submit();
	}
}

