// calculate function etc for loan payment calculator

// 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 a monthly rate. Convert payment period in months
    
// to the number of years.
    

function Calculate_Compound_Interest() {


// First remove any comma from principal amount entered
   var principalI = document.loandata.principal.value;

   principalI = principalI.replace(/,/g, "");

   var principal = parseFloat(principalI);

    
 
   var interest = document.loandata.interest.value;
   interest = (interest / 100) + 1;


   var payments = document.loandata.months.value;
   var years = payments / 12;



// Now compute the monthly payment figure, using esoteric math.
   var v = Math.pow(interest, years);


var monthly = 0;

// Check that the result is a finite number. If so, display the results
    
   if (!isNaN(v) && (v != Number.POSITIVE_INFINITY) &&
  (v != Number.NEGATIVE_INFINITY)) {
	document.loandata.payment.value = round((principal * v) / payments);
	document.loandata.total.value = round(principal * v);
	document.loandata.totalinterest.value = round((principal * v) - 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 = "";
    
   }

}



//
//Used to calculate simple interest
//
function Calculate_Simple_Interest() {
 

// First remove any comma from principal amount entered
   var principalI = document.loandata.principal.value;

   principalI = principalI.replace(/,/g, "");

   var principal = parseFloat(principalI);



   var interest = parseFloat(document.loandata.interest.value);


   var payments = parseInt(document.loandata.months.value);
   var years = parseFloat(payments / 12);



// Now compute total interest amount
   var v = (principal * interest * years) / 100;

// Check that the result is a finite number. If so, display the results
    
   if (!isNaN(v) && (v != Number.POSITIVE_INFINITY) &&
  (v != Number.NEGATIVE_INFINITY)) {
	document.loandata.payment.value = round((principal + v) / payments);
	document.loandata.total.value = round(principal + v);;
	document.loandata.totalinterest.value = round(v);
   }


// 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.

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