function isValid(entry, a, b) {

  if (isNaN(entry.value) || (entry.value==null) || (entry.value=="") || (entry.value < a) || (entry.value > b)) {

  alert("Invalid entry. Enter a number between $" + a + " and $" + b + ".")

	entry.focus()

    entry.select()

	return false

  }

	return true

}



function clearCalcs(form) {

	form.time_pay.value = ""

	form.total_payment.value = ""

	form.total_interest.value = ""

}



function calculate(form) {

	if (!isValid(form.cc_balance, 0, 100000)) {  

		return false

	} else if (!isValid(form.cc_interest, 0, 30)) {  

		return false

	} else {         

		var beg_bal = eval(form.cc_balance.value);

	}

	if (!isValid(form.cc_payment, beg_bal*.01, beg_bal)) {

		return false

  } else {

	var cur_bal = beg_bal;	

	var interest = eval(form.cc_interest.value/100);

	var mnth_pay = eval(form.cc_payment.value);

	var fin_chg = 0;	

	var num_mnths = 0;

	var tot_int = 0;

  }

	

  while (cur_bal > 0) {

    fin_chg = cur_bal*interest/12;

    cur_bal = cur_bal - mnth_pay + fin_chg;

    num_mnths++;

		if (num_mnths > 601) {

      	alert("The numbers shown here may result with a very high balance, high interest rate and low monthly payment.\n\nTry entering a higher monthly payment amount." )

				form.cc_payment.focus()

				form.cc_payment.select()

				return

      }

    tot_int += fin_chg;

  }

	form.time_pay.value = num_mnths;

	form.total_payment.value ="$" + round(beg_bal + tot_int);

	form.total_interest.value ="$" + round(tot_int);

   }


function round(x) {

	return Math.round(x*100)/100;

}