function sub_RecalcPage()
{
	var el, i, j, k, l; // small reusable variables
	var ao = window.sub_AvailableOptions; // So that it's shorter
	var Currency, CurrencyCode; // Selected currency object
	var TimeK, Recurring; // Time coefficient (1 for recurring) and whether recurring or not.
	var OptionSum, OptionBetterSum, OptionCount, ComboOptionCount; // Price sum of all options and count of selected options
	var Time; // Currently selected Time object
	// First - get the currency
	
	CurrencyCode = document.getElementById('currency').value;
	Currency = ao.Currencies[CurrencyCode];

	// Second - get the time coefficient. If recurring, use 1.
	el = document.getElementById('prd_time');
	Time = el.options[el.selectedIndex].value; 

	if (Time == -1)
	{
		TimeK = 1;
		Recurring = true;
	}
	else
	{
		TimeK = ao.Times[Time];
		Recurring = false;
	}

	// Third - sum up the option prices, and count selections. Assume first choice is "not selected".

	OptionSum = 0;
	OptionCount = 0;
	ComboOptionCount = 0;
	for (i in ao.Sections)
		for (j in ao.Sections[i] )
		{
			if (document.getElementById('opt_' + i + '_' + j).checked && ao.Sections[i][j].BasePrice != 0)
			{
				OptionSum += ao.Sections[i][j].BasePrice;
				OptionCount++;
				if (ao.Sections[i][j].Comboable)
					ComboOptionCount++;
			}

		}

	// Fourth - calculate combo discounts and multiply by time coefficient
	
	if (ComboOptionCount > 1)
		OptionBetterSum = OptionSum - ComboOptionCount + 1; // 1$ for every item starting at second.
	else
		OptionBetterSum = OptionSum;
	OptionSum *= TimeK;
	OptionBetterSum *= TimeK;
	

	// Fifth - apply curency rate and rounding
	var RoundFun = Currency.RoundType == 'D' ? Math.floor : (Currency.RoundType == 'U' ? Math.ceil : Math.round);
	OptionSum = RoundFun(OptionSum * Currency.Rate / Currency.RoundValue) * Currency.RoundValue;
	OptionBetterSum = RoundFun(OptionBetterSum * Currency.Rate / Currency.RoundValue) * Currency.RoundValue;

	// Sixth - display!

	document.getElementById('sub_price').innerHTML = OptionBetterSum.toFixed(2);// + ' ' + Currency.Code + (Recurring?' monthly':'');
	document.getElementById('sub_save_amount').innerHTML = (OptionSum - OptionBetterSum).toFixed(2);
	document.getElementById('sub_save_currency').innerHTML = CurrencyCode;
	document.getElementById('sub_save_period').innerHTML = (Recurring?' monthly':'');

	if (document.getElementById('sub_target').value == -1 || OptionCount < 1)
	{
		document.getElementById('sub_Confirm_PayPal').style.display='none';
		document.getElementById('sub_Confirm_Mail').style.display='none';
		document.getElementById('sub_Confirm_PayPal_gray').style.display='inline';
		document.getElementById('sub_Confirm_Mail_gray').style.display='inline';
	}
	else
	{
		document.getElementById('sub_Confirm_PayPal').style.display='inline';
		document.getElementById('sub_Confirm_PayPal_gray').style.display='none';
		if ( Recurring )
		{
			document.getElementById('sub_Confirm_Mail').style.display='none';
			document.getElementById('sub_Confirm_Mail_gray').style.display='inline';
		}
		else
		{
			document.getElementById('sub_Confirm_Mail').style.display='inline';
			document.getElementById('sub_Confirm_Mail_gray').style.display='none';
		}
	}
}
function sub_ActivateTarget(targetID)
{
	var ao = window.sub_AvailableOptions; // So that it's shorter
	var i;
	for ( i = 0; i < ao.Targets; i++ )
	{
		document.getElementById('sub_target_'+i).className = (i == targetID? 'sub_selectedtarget':'');
		document.getElementById('sub_targetfields_'+i).style.display = (i == targetID? '':'none');
	}
	document.getElementById('sub_target').value = targetID;
	sub_RecalcPage();
}

// Same as RecalcPage, just for admin section.
function sub_RecalcPage2()
{
	var el, i, j, k, l; // small reusable variables
	var ao = window.sub_AvailableOptions; // So that it's shorter
	var Currency, CurrencyCode; // Selected currency object
	var TimeK, Recurring; // Time coefficient (1 for recurring) and whether recurring or not.
	var OptionSum, OptionBetterSum, OptionCount, ComboOptionCount; // Price sum of all options and count of selected options
	var Time; // Currently selected Time object
	// First - get the currency
	
	CurrencyCode = document.getElementById('currency').value;
	Currency = ao.Currencies[CurrencyCode];

	// Second - get the time coefficient. If recurring, use 1.
	el = document.getElementById('prd_time');
	Time = el.options[el.selectedIndex].value; 

	if (Time == -1)
	{
		TimeK = 1;
		Recurring = true;
	}
	else
	{
		TimeK = ao.Times[Time];
		Recurring = false;
	}

	// Third - sum up the option prices, and count selections. Assume first choice is "not selected".

	OptionSum = 0;
	OptionCount = 0;
	ComboOptionCount = 0;
	for (i in ao.Sections)
	{
		el = document.getElementById('opt_' + i);
		j = el.value;
		if (ao.Sections[i][j].BasePrice != 0)
		{
			OptionSum += ao.Sections[i][j].BasePrice;
			OptionCount++;
			if (ao.Sections[i][j].Comboable)
				ComboOptionCount++;
		}
	}

	// Fourth - calculate combo discounts and multiply by time coefficient
	
	if (ComboOptionCount > 1)
		OptionBetterSum = OptionSum - ComboOptionCount + 1; // 1$ for every item starting at second.
	else
		OptionBetterSum = OptionSum;
	OptionSum *= TimeK;
	OptionBetterSum *= TimeK;
	

	// Fifth - apply curency rate and rounding
	var RoundFun = Currency.RoundType == 'D' ? Math.floor : (Currency.RoundType == 'U' ? Math.ceil : Math.round);
	OptionSum = RoundFun(OptionSum * Currency.Rate / Currency.RoundValue) * Currency.RoundValue;
	OptionBetterSum = RoundFun(OptionBetterSum * Currency.Rate / Currency.RoundValue) * Currency.RoundValue;

	// Sixth - display!

	document.getElementById('sub_price').innerHTML =
		OptionBetterSum.toFixed(2) + ' ' + CurrencyCode;
	document.getElementById('sub_savings').innerHTML =
		(OptionSum - OptionBetterSum).toFixed(2) + ' ' + CurrencyCode + (Recurring?' monthly':''); 
}
