﻿function incChange(tbInc, tbOut, sIncCurrLabel, sOutCurrLabel, bForce)
{
	if (tbInc == null || tbOut == null)
		return;

	var nIncSum = Number(tbInc.value.replace(",", "."));

	tbOut.value = inc2Out(nIncSum, sIncCurrLabel, sOutCurrLabel);
	if (bForce)
		outChange(tbInc, tbOut, sIncCurrLabel, sOutCurrLabel, false)
}

function outChange(tbInc, tbOut, sIncCurrLabel, sOutCurrLabel, bForce)
{
	if (tbInc == null || tbOut == null)
		return;

	var nOutSum = Number(tbOut.value.replace(",", "."));

	tbInc.value = out2Inc(nOutSum, sIncCurrLabel, sOutCurrLabel)
	if (bForce)
		incChange(tbInc, tbOut, sIncCurrLabel, sOutCurrLabel, false)
}

function inc2Out(nIncSum, sIncCurrLabel, sOutCurrLabel)
{
	if (isNaN(nIncSum))
		nIncSum = 0.0;
	if (isNaN(nOutAddon[sIncCurrLabel][sOutCurrLabel]))
		nOutAddon[sIncCurrLabel][sOutCurrLabel] = 0.0;
	if (isNaN(nOutsPer1Inc[sIncCurrLabel][sOutCurrLabel]))
		return '';

	var nIncPwr = nPower[sIncCurrLabel];
	var nOutPwr = nPower[sOutCurrLabel];
	var nAddon = nOutAddon[sIncCurrLabel][sOutCurrLabel];
	var nRate = nOutsPer1Inc[sIncCurrLabel][sOutCurrLabel];

	nIncSum = Math.round(nIncSum * nIncPwr) / nIncPwr;
	var nBase = new Number(nIncSum * nRate * nOutPwr).toFixed(6);
	var nOutSum = Math.round((Math.floor(nBase) / nOutPwr - nAddon) * nOutPwr) / nOutPwr;
	if (nOutSum < 0)
		nOutSum = 0;

	return nOutSum;
}

function out2Inc(nOutSum, sIncCurrLabel, sOutCurrLabel)
{
	if (isNaN(nOutSum))
		nOutSum = 0.0;
	if (isNaN(nOutAddon[sIncCurrLabel][sOutCurrLabel]))
		nOutAddon[sIncCurrLabel][sOutCurrLabel] = 0.0;
	if (isNaN(nOutsPer1Inc[sIncCurrLabel][sOutCurrLabel]))
		return '';

	var nIncPwr = nPower[sIncCurrLabel];
	var nOutPwr = nPower[sOutCurrLabel];
	var nAddon = nOutAddon[sIncCurrLabel][sOutCurrLabel];
	var nRate = nOutsPer1Inc[sIncCurrLabel][sOutCurrLabel];

	nOutSum = Math.round(nOutSum * nOutPwr) / nOutPwr + nAddon;
	var nBase = new Number(nOutSum / nRate * nIncPwr).toFixed(6);
	return Math.round((Math.ceil(nBase) / nIncPwr) * nIncPwr) / nIncPwr;
}

