﻿// JScript File
function calculate()
{
    var ddlMethod = document.getElementById("ddlMethod");
    var txtMonthly = document.getElementById("txtMonthlyPayment");
           
    /*	- get user's input from FORM
    - convert the annual rate to a monthly rate
    - convert interest from a % to a decimal
    - convert payment period from years to months
    - compute the monthly payments */
    if(ddlMethod.options[ddlMethod.selectedIndex].value == '1')
    {
        var principal = document.getElementById("txtAmount").value;
        var interest = document.getElementById("txtRate").value /100 / 12;
        var payment = document.getElementById("txtTerm").value * 12;
        var x = Math.pow(1 + interest,payment);
        var months = (principal*x*interest) / (x-1);
        
        /*	check that the result is a finite number.
	        If so display results */
        if (!isNaN(months) && (months != Number.POSITIVE_INFINITY) && (months != Number.NEGATIVE_INFINITY) )
            txtMonthly.value = rounding (months);		         		        
        else
            txtMonthly.value = "";        
    }
    else if(ddlMethod.options[ddlMethod.selectedIndex].value == '2')
    {
        var principal = document.getElementById("txtAmount").value;
        txtMonthly.value =  rounding(((document.getElementById("txtAmount").value)*(document.getElementById("txtRate").value /100)) /12);
    }
}	// End Fn Calculate
        
// round to 2 decimal places function
function rounding(x)
{
    return Math.round(x*100)/100;
}	
//End Fn rounding        


