function doLoan(language)
{
var length = "";
var ammount = "";
var rate = "";
var monthly = 0.0;
var monthlyRate = 0.0;
if (1 == 1)
{
replaceComma(document.loanForm.ammount);
replaceComma(document.loanForm.rate);
ammount = document.loanForm.ammount.value;
rate = document.loanForm.rate.value;
length = document.loanForm.length.value;
if (ammount * 1 != ammount)
{
return true;
}
else if (rate * 1 != rate)
{
return true;
}
else if (length == 0)
{
return true;
}
else if (ammount > 0 && rate > 0)
{
length = length * 12; //transform to months
rate = rate / 100.0;
monthlyRate = rate / 12.0;
monthly = ammount * monthlyRate / (1.0 - calculate(monthlyRate, -length));
document.loanForm.monthly.value = formatNumber(monthly,2,true,false);
total = formatNumber(monthly,2,true,false) * length;
document.loanForm.total.value = formatNumber(total,2,true,false);
}
}
return (true);
}
function replaceComma(obj)
{
if (obj.value.indexOf(',') != -1)
{
obj.value = obj.value.substring(0,obj.value.indexOf(',') ) + '.' + obj.value.substring(obj.value.indexOf(',') + 1);
}
}
function calculate(a, n)
{
var i;
var sum, pow, term, cof;
if(n < 0)
return 1.0 / calculate(a, -n);
sum = 1.0;
pow = n;
term = 1;
cof = 1.0
for(i = 1; i < 10; i++) {
cof = cof * pow / i;
pow = pow - 1.0;
term = term * a;
sum = sum + cof * term;
}
return sum;
}
function formatNumber(num, decimalNum, bolLeadingZero, bolParens)
/* IN - num: the number to be formatted
decimalNum: the number of decimals after the digit
bolLeadingZero: true / false to use leading zero
bolParens: true / false to use parenthesis for - num
RETVAL - formatted number
*/
{
var tmpNum = num;
// Return the right number of decimal places
tmpNum *= Math.pow(10,decimalNum);
tmpNum = Math.floor(tmpNum);
tmpNum /= Math.pow(10,decimalNum);
var tmpStr = new String(tmpNum);
// See if we need to hack off a leading zero or not
if (!bolLeadingZero && num < 1 && num > -1 && num !=0)
if (num > 0)
tmpStr = tmpStr.substring(1,tmpStr.length);
else
// Take out the minus sign out (start at 2)
tmpStr = "-" + tmpStr.substring(2,tmpStr.length);
// See if we need to put parenthesis around the number
if (bolParens && num < 0)
tmpStr = "(" + tmpStr.substring(1,tmpStr.length) + ")";
return tmpStr;
}