function NumberFormat(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function ToNative (szStr)
{
	var szRet = '';
	
	for (var i=0; i<szStr.length; i++)
	{
		if (szStr.charAt(i)!=',' && szStr.charAt(i)!='.')
		{
			szRet += szStr.charAt(i);
		}
	}
	
	return szRet;	
}

function FilterDigits(str, bDots)
{
	var szRet = '';
	var y=0;
	
	for (var i=0; i<str.length; i++)
	{
		if (str.charCodeAt(i)>=48 && str.charCodeAt(i) <=57 || (bDots && str.charCodeAt(i)==46))
		{
			szRet += str.charAt(i);
		}
	}
	
	return szRet;
}

function EmptyString(str)
{
	for (var i=0; i<str.length; i++)
	{
		if (str.charCodeAt(i) > 32)
		{
			return false;
		}
	}
	return true;
}

function CalculatePayments ()
{
	var iRate = ($('bcFinalInterestRate').innerHTML * 0.01) / 12;
	var iDepositValue = ($('bcDeposit').nativeValue)?$('bcDeposit').nativeValue:ToNative($('bcDeposit').value);

	$('bcMonthly').innerHTML = NumberFormat(Math.round(((g_iMandatePrice -iDepositValue) * iRate) / (1 - Math.pow(iRate+1.0, - ($('bcYears').value*12)))));
	$('bcErrors').innerHTML = '';
}

function BondCalc_AdjustInterest (obj, e)
{
	// Prevent invalid keys
	var cKey = (e)?e.keyCode:window.event.keyCode;
	
	if (cKey >= 96) cKey -= 48;
	
	if ((cKey >= 48 && cKey <= 57) || cKey==142 || cKey==62)
	{
		if ((cKey==142 || cKey==62) && obj.value.lastIndexOf('.')!=obj.value.indexOf('.'))
		{
			$('bcErrors').innerHTML = '<strong>Error: </strong>Only one decimal point (.) is allowed in a percentage';
			obj.value = obj.value.substr(0, obj.value.length-1);
		}
		else if ($('bcRate').innerHTML - obj.value <= 0)
		{
			$('bcErrors').innerHTML = '<strong>Error: </strong>You cannot subtract more interest than total prime.';
		}
		else
		{
			$('bcFinalInterestRate').innerHTML = Math.round(($('bcRate').innerHTML - obj.value)*100)/100;
			CalculatePayments();
		}
	}
	else if (cKey > 40)
	{
		obj.value = FilterDigits(obj.value, true);
		$('bcErrors').innerHTML = "<strong>Error:</strong> Please use only the digits 0-9, or a normal decimal point (.)";
	}
	
	
	
}

function BondCalc_AdjustYears (obj, e)
{
	// Prevent invalid keys
	var cKey = (e)?e.keyCode:window.event.keyCode;
	
	if (cKey >= 96) cKey -= 48;
	
	if (EmptyString(obj.value)) return;

	if ((cKey >= 48 && cKey <= 57) || cKey == 8)
	{
		if (obj.value > 50)
		{
			obj.value = "50";
			$('bcErrors').innerHTML = "<strong>Error:</strong> Maximum payment period is 50 years.";
		}
		else if (obj.value > 0)
		{
			CalculatePayments();
		}
	}
	else if (cKey > 40)
	{
		obj.value = FilterDigits(obj.value, false);
		$('bcErrors').innerHTML = "<strong>Error:</strong> Please use only the digits 0-9, decimal points are not required.";
	}

}

function BondCalc_AdjustDeposit (obj, e)
{
	// Prevent invalid keys
	var cKey = (e)?e.keyCode:window.event.keyCode;
	
	
	if (cKey >= 96) cKey -= 48;
	
	if (EmptyString(obj.value)) return;

	if ((cKey >= 48 && cKey <= 57) || cKey == 8)
	{
	/*	if (obj.value < g_iMandatePrice*0.1)
		{
			$('bcErrors').innerHTML = "<strong>Note:</strong> Amount for deposit must be at least R" + Math.round(g_iMandatePrice*0.1) + ".";
		}*/
		obj.nativeValue = ToNative(obj.value);
		obj.value = NumberFormat(obj.nativeValue);
				
		if (obj.nativeValue >= g_iMandatePrice)
		{
			$('bcErrors').innerHTML = "<strong>Error: </strong>Your deposit amount is too high.";
		}
		else if (obj.nativeValue > 0)
		{
			CalculatePayments();
		}
	}
	else if (cKey > 40)
	{
		obj.value = NumberFormat(FilterDigits(obj.value, false));
		$('bcErrors').innerHTML = "<strong>Error:</strong> Please use only the digits 0-9, decimal points are not required.";
	}

}

