
//Credit Card Debt Calculator

function calculateForm(form) {

/* Dont do this, breaks rest of function! document.write(interest_rate); */
var interest_rate = form.interest_rate.value;
// If user sets interest rate as a number - convert to decimal - else leave as a decimal

    if (interest_rate  > 1.0) {

        interest_rate  = interest_rate  / 100.0;

        form.interest_rate.value = interest_rate;

    }


// convert interest rate to monthly rate
     interest_rate = (interest_rate/12);

// assign variables from form values
   var principal = form.principal.value;
   var monthly_payment = eval(form.monthly_payment.value);
   var monthly_interest = 0;
   var acc_interest = 0;
   var month = 0;
   var forever = 0;


  // form.monthlypmt.value should b monthly_payment
  if(eval(form.monthly_payment.value) < (principal * interest_rate)) {
       // If  monthly payment is less than interest charged for the month then loan will go on forever
    forever =1;
    form.interest_paid.value = "NA";
    form.number_months.value = "Forever";
    form.number_years.value = "Forever";
  }


if(forever == 0){ 
    while( principal > 0) 
    {
          monthly_interest = principal * interest_rate;
          acc_interest = acc_interest + monthly_interest;
          principal = eval(principal) + monthly_interest - monthly_payment;
          month = month + 1;
 
          // Test principal
          if ( parseInt(principal*100) == 0 )
          {
             principal = 0
          }
    }// End While
	
	  form.interest_paid.value = acc_interest;
      form.number_months.value = month;
      form.number_years.value = month / 12;
				}

  

      

} // End Function calculateForm



function clearForm(form)
//function to reset the form
{
    form.principal.value = "";
    form.interest_rate.value = "";
     form.monthly_payment.value = "";
     form.interest_paid.value = "";
     form.number_months.value ="";
     form.number_years.value = "";

} // End Function clearForm

