﻿// JScript File

function CalculateMonthlyPayment(frm) 
{

    var intInterestRate = frm.txtInterestRate.value;
    var intLoanAmount = frm.txtLoanAmount.value;
    var intLoanTerm = frm.txtLoanTerm.value;

    if (IsEmpty(intInterestRate))
    {
        alert('Please enter an Interest Rate');
        frm.txtInterestRate.focus();
        return false;
    }
    
    else if(IsEmpty(intLoanAmount))
    {
        alert('Please enter a Loan Amount');
        frm.txtLoanAmount.focus();
        return false;
    }
    
    else if(IsEmpty(intLoanTerm))
    {
        alert('Please enter a Loan Term in Years');
        frm.txtLoanTerm.focus();
        return false;
    }
    
    else
    {
        
        // Decide if Interest Only or Repayment Only
        
        if (frm.radInterestOnly.checked)
        {
        
            var intInterest = intInterestRate / 100;

            // Calculate monthly interest payment
            var vInterest = ((intLoanAmount * intInterest) * intLoanTerm);
            var vMonthlyRepayments = intLoanTerm * 12;
            var vMonthlyPayment = vInterest / vMonthlyRepayments;
            
            frm.txtDisplayText01.value = "Your Monthly payments for a interest"; 
            frm.txtDisplayText02.value = "only mortgage loan of " + formatCurrency(frm.txtLoanAmount.value) + " will be";
            frm.txtDisplayResult.value = "£" + round(vMonthlyPayment);
        
        }
        
        else
        {
            // Calculate Repayments Only
            var principal = intLoanAmount;
            var interest = intInterestRate / 100 / 12;
            var payments = intLoanTerm * 12;
        
            var x = Math.pow(1 + interest, payments);
            var monthly = (principal*x*interest)/(x-1);

            if (!isNaN(monthly) && 
                (monthly != Number.POSITIVE_INFINITY) &&
                (monthly != Number.NEGATIVE_INFINITY)) {

                frm.txtDisplayText01.value = "Your Monthly payments for a repayment"; 
                frm.txtDisplayText02.value = "mortgage loan of " + formatCurrency(frm.txtLoanAmount.value) + " will be";
                frm.txtDisplayResult.value = "£" + round(monthly);
        
                
        
            }

        }

    }
    
    return true;
    
}

function HandleRadioButtons (radioButtonOrGroup)
{
   //
}

function IsEmpty(aTextField) {
   if ((aTextField.length==0) ||
   (aTextField == null)) {
      return true;
   }
   else { return false; }
}

function round(x) {
  return Math.round(x*100)/100;
}

function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
    num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    pence = num%100;
    num = Math.floor(num/100).toString();
    if(pence<10)
    pence = "0" + pence;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
    num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + '£' + num + '.' + pence);
}