<!-- This example is from JavaScript: The Definitive Guide, 3rd Edition.   -->
<!-- That book and this example were Written by David Flanagan.            -->
<!-- They are Copyright (c) 1996, 1997, 1998 O'Reilly & Associates.        -->
<!-- This example is provided WITHOUT WARRANTY either expressed or implied.-->
<!-- You may study, use, modify, and distribute it for any purpose,        -->
<!-- as long as this notice is retained.                                   -->

function calculate() {
    var principal = document.loandata.principal.value;
    var isinterest = document.loandata.interest.value;
    var interest = document.loandata.interest.value / 100 / 12;
    var payments = document.loandata.years.value * 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)) {

        document.loandata.payment.value = round(monthly);
        document.loandata.total.value = round(monthly * payments);
        document.loandata.totalinterest.value = 
            round((monthly * payments) - principal);
        var ispercent= round((monthly * payments) - principal);
        ispercent = round((ispercent/principal)*100);
        document.loandata.percent.value = ispercent;
    }

	else {
        document.loandata.payment.value = "";
        document.loandata.total.value = "";
        document.loandata.totalinterest.value = "";
        document.loandata.percent.value = "";
    }
}

function round(x) {
  return Math.round(x*100)/100;
}
