var __commissions = null;
var __thresholds = null;
var __authorized_base = 0;
var __authorized = 0;

//function Comm_Init(commissions, thresholds, authorized) {
function Comm_Init(thresholds, authorized) {
//	__commissions      = $A(commissions.evalJSON());
	__thresholds       = $A(thresholds.evalJSON());
	__authorized_base  = parseFloat(authorized);
	__authorized       = __authorized_base;
}

/**
 * Gets item commission from merchant commissions table.
 */
function Comm_getItemCommission(product_id, category_id, merchant_id) {
	var _commission = null;
	
	// product_id
	__commissions.each(
		function(c) {
			if(c.get('product_id') == product_id) _commission = c;
		}
	)
	if(_commission) return _commission;
	
	// category_id
	__commissions.each(
		function(c) {
			if(c.get('product_id') == 0 && c.get('category_id') == category_id) _commission = c;
		}
	)
	if(_commission) return _commission;
	
	// merchant_id
	if(__commissions.length > 0) return 
	__commissions.each(
		function(c) {
			if(!c.get('product_id') && !c.get('category_id') && c.get('merchant_id') == merchant_id) _commission = c;
		}
	)
	return _commission;
}

/**
 * Gets item threshold commission.
 */
function Comm_getItemThreshold(trns_id, item_id, _item_price) {
	
	var value = __authorized + _item_price;
	var thold = false;
	for(var t=0 ; t<__thresholds.length ; t++) {
		var thold_val = parseFloat(__thresholds[t].threshold);
		//alert(__authorized + ' - ' + value + ' - ' + thold_val)
		if((value < thold_val) || (value > thold_val && __authorized < thold_val)) {
			if(t > 0) thold = __thresholds[t - 1];
			break;
		} else thold = __thresholds[t];
	}
	
	return thold;
}

function Comm_checkItem(trns_id, item_id, checked) {
	if(!checked) {
		// Item commission
		var comm_value_obj = $('item_' + item_id + '-commission_value');
		if(comm_value_obj) comm_value_obj.update('0,00');
		var comm_amount_obj = $('item_' + item_id + '-commission_amount')
		if(comm_amount_obj) comm_amount_obj.update(money_format(0.00));
		Comm_updateTransactionCommission(trns_id, 0);
	}
	
	Comm_getCommissions();
}

/**
 * Gets commission to Pointer.
 */
function Comm_authorizeItem(trns_id, item_id, checked) {
	if(checked) {
		var comm_price_obj = $('item_' + item_id +'_' + trns_id + '-price');
		var item_price = comm_price_obj ? parseFloat(comm_price_obj.value.replace(/\,/, '.')) : 0;
/*		var threshold  = Comm_getItemThreshold(trns_id, item_id, item_price);
        comm_thr = '0.00';
        comm_thr_label = '';
		if(threshold) {
			switch(threshold.commission_type) {
				case 'percentage':
					comm_thr = threshold.commission;
					comm_thr_label += " + " + money_format(parseFloat(threshold.commission)) + '%';
					break;
				case 'ammount'   :
					comm_thr = threshold.commission;
					comm_thr_label += " + " + money_format(parseFloat(threshold.commission)) + '&euro;';
					break;
				default          : break;
			}
			comm_thr_label += '<br /><span style="color:#' + threshold.name_color + ';font-weight:bold;font-style:italic;">' + threshold.name + '</span>';
		}
*/
		var commission = $('item_' + item_id + '-commission_json').value.evalJSON();
		switch(commission.commission_type) {
			case 'percentage':
//                alert(item_price +'*'+ commission.commission +'+'+ comm_thr +'='+ item_price * ((parseFloat(commission.commission) + parseFloat(comm_thr)) / 100));
				var comm_value = item_price * (parseFloat(commission.commission) / 100);
				var comm_label = money_format(parseFloat(commission.commission)) + '%';
				break;
			case 'ammount'   :
				var comm_value = parseFloat(commission.commission);
				var comm_label = money_format(parseFloat(commission.commission)) + '&euro;';
				break;
			default          :
				var comm_value = 0.00;
				var comm_label = '0,00';
				break;
		}

/*	    var comm_value_obj = $('item_' + item_id + '-commission_value');
	    if(comm_value_obj) comm_value_obj.update(comm_label); */
	    var comm_amount_obj = $('item_' + item_id + '-commission_amount')
	    if(comm_amount_obj) comm_amount_obj.update(money_format(comm_value));
	    Comm_updateTransactionCommission(trns_id, comm_value);
//	    Comm_updateTotalCommission(comm_value);

	    __authorized += item_price;

	}
}

function Comm_updateTransactionCommission(trns_id, comm_value) {
	var comm_amount_trns = $('transaction_' + trns_id + '-commission_amount');
	if(comm_amount_trns) {
		var pre_amount = parseFloat(comm_amount_trns.innerHTML.replace(/,/, '.'));
		var new_amount = pre_amount + comm_value;
		comm_amount_trns.update(money_format(new_amount));
	}
}
/*
function Comm_updateTotalCommission(comm_value) {
	var comm_total_commission = $('total_commission');
	if(comm_total_commission) {
		var pre_amount = parseFloat(comm_total_commission.innerHTML.replace(/,/, '.'));
		var new_amount = pre_amount + comm_value;
		comm_total_commission.update(money_format(new_amount));
	}
}
*/
function Comm_getCommissions(resetAll) {
	if(arguments.length == 0) var resetAll = false;
	
	__authorized = __authorized_base;
	
	// Set total commission-span to '0'
	var comm_total_commission = document.getElementById('total_commission');
	//var comm_total_commission = $('total_commission');
	if(comm_total_commission) comm_total_commission.update(money_format(0));
	
	// Set transaction commission-span to '0'
	/*var spans = $$('.commission');
	spans.each(
		function(s) {
			s.update(money_format(0));
		}
	)
	*/
	// Calculates commissions
	var checkboxes = document.getElementsByTagName("input");
	for (var cb in checkboxes)
		{
			if((cb.type == 'checkbox') && (toks = cb.id.match(/item_(\d+)_(\d+)-checkbox/))) {
				if(!cb.checked && resetAll) Comm_checkItem(toks[2], toks[1], false);
                else if(cb.checked) Comm_authorizeItem(toks[2], toks[1], true);
			}
		}
	
}

/* =============== */
function money_format(value) {
	return value.toFixed(2).replace(/\./, ',');
}


