<script language="JavaScript">
function calculate() {
// get the user's input from the form. Assume it is all valid.
// convert interest from a percentage to a decimal, and convert from
// an annual rate to monthly rate. Convert payment period in years
// to the number of monthly payments.
var principal = document.loandata.principal.value;
var interest = document.loandata.interest.value / 100 / 12;
var payments = document.loandata.years.value *12;
// now compute the montly payment figure, using esoteric math.
var x = Math.pow(1 + interest, payments);
var monthly = (principal*x*interest)/(x-1);

// check that the result is a finite number. If so, display the results.
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);
}
// otherwise, the user's input was probably invalid, so don't
// display anything.
else {
document.loandata.payment.value = "";
document.loandata.total.value = "";
document.loandata.totalinterest.value ="";

}
}
// this simple method rounds a number to two decimal places.
funtion round(x) {
return Math.round(x*100/100;
}
</script>