function calculateInterest()
{
    var floatPrincipal = document.formMain.floatPrincipal.value;
    var floatInterestRate = document.formMain.floatInterestRate.value;
    var intYear = document.formMain.intYear.value;
    var floatCalculatedInterest = floatPrincipal*(floatInterestRate/100)*intYear;
    strCalculatedInterest = currency(floatCalculatedInterest);
    document.getElementById('floatCalculatedInterest').innerHTML = strCalculatedInterest;
    
    var floatTotalPayment = num(floatCalculatedInterest) + num(floatPrincipal);
    strTotalPayment = currency(floatTotalPayment);
    document.getElementById('floatTotalPayment').innerHTML = strTotalPayment;
    
    var floatMonthlyInstalment = num(floatTotalPayment)/(12*num(intYear));
    strMonthlyInstalment = currency(floatMonthlyInstalment);
    document.getElementById('floatMonthlyInstalment').innerHTML = strMonthlyInstalment;
}

function round(number,X) 
{
    // rounds number to X decimal places, defaults to 2
    x = (!X ? 2 : X);
    return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}

function num(obj) 
{
    val = parseFloat(obj);
    if (isNaN(val)) 
    { 
        val = 0; 
    }
    return val;
}

function currency(anynum) 
{
   //-- Returns passed number as string in $xxx,xxx.xx format.
   anynum=eval(anynum)
   workNum=Math.abs((Math.round(anynum*100)/100));workStr=""+workNum
   if (workStr.indexOf(".")==-1){workStr+=".00"}
   dStr=workStr.substr(0,workStr.indexOf("."));dNum=dStr-0
   pStr=workStr.substr(workStr.indexOf("."))
   while (pStr.length<3){pStr+="0"}

   //--- Adds comma in thousands place.
   if (dNum>=1000) {
      dLen=dStr.length
      dStr=parseInt(""+(dNum/1000))+","+dStr.substring(dLen-3,dLen)
   }

   //-- Adds comma in millions place.
   if (dNum>=1000000) {
      dLen=dStr.length
      dStr=parseInt(""+(dNum/1000000))+","+dStr.substring(dLen-7,dLen)
   }
   
   //-- Adds comma in billions place.
   if (dNum>=1000000000) {
      dLen=dStr.length
      dStr=parseInt(""+(dNum/1000000000))+","+dStr.substring(dLen-11,dLen)
   }
   
   //-- Adds comma in trillions place.
   if (dNum>=1000000000000) {
      dLen=dStr.length
      dStr=parseInt(""+(dNum/1000000000000))+","+dStr.substring(dLen-15,dLen)
   }
   retval = dStr + pStr 
   //-- Put numbers in parentheses if negative.
   if (anynum<0) {retval="("+retval+")"}
   return ""+retval
   //return "$"+retval
}