function number_format (number, decimals, dec_point, thousands_sep)
{
  var exponent = "";
  var numberstr = number.toString ();
  var eindex = numberstr.indexOf ("e");
  if (eindex > -1)
  {
    exponent = numberstr.substring (eindex);
    number = parseFloat (numberstr.substring (0, eindex));
  }
  
  if (decimals != null)
  {
    var temp = Math.pow (10, decimals);
    number = Math.round (number * temp) / temp;
  }
  var sign = number < 0 ? "-" : "";
  var integer = (number > 0 ? 
      Math.floor (number) : Math.abs (Math.ceil (number))).toString ();
  
  var fractional = number.toString ().substring (integer.length + sign.length);
  dec_point = dec_point != null ? dec_point : ".";
  fractional = decimals != null && decimals > 0 || fractional.length > 1 ? 
               (dec_point + fractional.substring (1)) : "";
  if (decimals != null && decimals > 0)
  {
    for (i = fractional.length - 1, z = decimals; i < z; ++i)
      fractional += "0";
  }
  
  thousands_sep = (thousands_sep != dec_point || fractional.length == 0) ? 
                  thousands_sep : null;
  if (thousands_sep != null && thousands_sep != "")
  {
  for (i = integer.length - 3; i > 0; i -= 3)
      integer = integer.substring (0 , i) + thousands_sep + integer.substring (i);
  }
  
  return sign + integer + fractional + exponent;
}

/*update the price*/

var stackList     = new Array();
var stackListCount     = new Array();


var keyList       = new Array();
var stackTypes    = new Array();

var groupOrder = '';
var aGroupOrder = new Array();

/*start prototype helper*/
// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};
 
/*return the first index for a value*/
Array.prototype.firstIndexOf = function (sSearch,bStrict){
	for(var i = 0;i<this.length;i++){
		if(bStrict && bStrict!='undefined' ){
			if(this[i]===sSearch) return i;
		} else {
			if(this[i]==sSearch) return i;
		}
	}	
	return -1;
}
 
/*return the last valid not empty value*/
Array.prototype.getLastValue = function (index) {
 	if(!this[index]) {
		if(index>-1) return this.getLastValue(index-1);
		return -1;
	} else {
		return this[index];
	} 
}
/*end prototype*/

function setStackData(stackKey,stackPrices){
	stackList[stackKey] = stackPrices;
	stackListCount[stackKey] = 0;
}

function getStackPrice(stackKey, nr){
	
	var ret = 0;
	
	var takeKey = nr;
	var lStack = stackList[stackKey];
	if(lStack.length < nr) {
		if(lStack.length > 1){
			//we have stack prices but the prices for current item is not defined so auto-calc it
			var priceDiff = lStack[lStack.length-1] - lStack[lStack.length-2];
			ret = priceDiff;
		} else {
			//there is only one prices
			ret = lStack[0];
		} 
	} else {
		//price found
		if(nr > 1){
			ret = lStack[nr-1] - lStack[nr-2];
		} else {
			ret = lStack[nr-1];
		}
		
		
	}
	
	//alert('return: ' + ret + ' for ' + stackKey + '|' + nr);
	return ret;
}

function setData(index,key,stackType){
	keyList[index] = key;
	stackTypes[index] = stackType;
}


function setOrder(sOrder) {
	var aSetOrder = new Array();
	var index = 0;
	var key = 0;
	aOrder = sOrder.split(','); //here we got a list of ids/keys (! not the index)
	for(var i = 0;i<aOrder.length;i++){
		key = aOrder[i];
		index = keyList.firstIndexOf(key);
		if(index >= 0) {
			aSetOrder[aSetOrder.length] = index;
		}		
	}
	aGroupOrder = aSetOrder;
}


function calcPrice(){
	orderList = groupOrder.split(',');
	
	
	
	var priceSum   = 0;
	var orderIndex = 0;
	var orderKey   = 0;
	var stackData  = new Array();
	var aIDOrder = new Array();
	
	for(var i=0;i<aGroupOrder.length;i++){
		orderIndex = aGroupOrder[i];
		orderKey   = keyList[orderIndex];
		stackType  = stackTypes[orderKey];
		stackListCount[stackType] = 0;
	}
		
	
	for(var i=0;i<aGroupOrder.length;i++){
		orderIndex = aGroupOrder[i];
		orderKey   = keyList[orderIndex];

		
		stackType  = stackTypes[orderKey];
		stackListCount[stackType]++;
		thisPrice = getStackPrice(stackType,stackListCount[stackType]);
		
		priceSum = priceSum*100 + thisPrice*100;
		priceSum = Math.round(priceSum)/100;
		
		aIDOrder[aIDOrder.length] = orderKey;
	}
	
	document.getElementById('tx_dscdenaaboregister[groupOrder]').value = aIDOrder.join(',');
	return priceSum;
}
 
function showPrice(sPrice){
	document.getElementById('tx_dscdenaaboregister[fullprice]').innerHTML = number_format (sPrice, 2, ",") + '&nbsp;&euro;';
}
 
function group_onclick(obj,index){
  var remove = obj.checked == false;
  if(remove) {
	remIndex = aGroupOrder.firstIndexOf(index);
	if(remIndex > -1) aGroupOrder.remove(remIndex,remIndex);
  } else {
  	aGroupOrder[aGroupOrder.length] = index;
  }
  groupOrder = aGroupOrder.join(',');
  var price = calcPrice();
  showPrice(price);
}