/*
  Created by Sharon Paine of Dynamic Web Coding 11/20/99
  http://www.dyn-web.com
  See Terms of Use at http://www.dyn-web.com/bus/terms.html
  One of my early JavaScript efforts.
  Inspired by loan payment calculator in ch 1 of
  JavaScript Definitive Guide by David Flanagan
  Modified 3/31/2000 and July 2002
  Jan 2003, fix focus problem for ie (thanks to Danny Goodman, JS Bible Gold Edition)
  and a few minor changes to placate Opera
*/
 
// validation function
function isValid(fld,a,b) {
	var checkStr="", msg="";
	if (isValid.arguments.length==1) {
		checkStr = "isNaN(fld.value) || fld.value=='' || fld.value<0";
		msg = "Invalid entry. Enter a positive number please.";
	}	else if (isValid.arguments.length==3) {
		checkStr = "isNaN(fld.value) || fld.value=='' || fld.value<a || fld.value>b";
		msg = "Invalid entry. Enter a number between "+a+" and "+b+".";
	}
	if (eval(checkStr)) {
		alert(msg);
		// for ns4, in case form is in positioned layer
		if (document.forms[fld.form.name]) {
			// delay needed for some browsers (not ns4)
			setTimeout("setFocus(document.forms['"+fld.form.name+"'].elements['"+fld.name+"'])",100);
		} else {
			fld.focus();
		  fld.select();
		}
		return false;
	}
	return true;
}

function setFocus(fld) {
	fld.focus();
  fld.select();
}

// clear results fields when input values changed
function clearCalcs(frm) {
	frm.num_months.value = "";
	frm.total_pay.value = "";
	frm.total_int.value = "";
}

function calculate(frm) {
// send entries to validation function
// exit if not valid
	if (!isValid(frm.balance, 0, 1000000)) return false;
	if (!isValid(frm.interest, 0, 30)) return false;
	else var init_bal = eval(frm.balance.value);
	if (!isValid(frm.mnth_pay, 0, init_bal)) return false;
  else {
	// variables used in calculation
	var cur_bal = init_bal;		// used in loop
	var interest = eval(frm.interest.value/100);
	var mnth_pay = eval(frm.mnth_pay.value);
	var fin_chg = 0;			// finance charge
	var num_mnths = 0;
	var tot_int = 0;
  }
	
  while (cur_bal > 0) {
    fin_chg = cur_bal*(interest/12);
    cur_bal = cur_bal + fin_chg - mnth_pay;
    num_mnths++;
		if (num_mnths > 360) { // 30 years!
     	alert("You would want to pay it off in 30 years or less, right?\nAnd we don't want a system hang.\n\nTry entering a higher monthly payment amount." )
			if (document.forms[frm.name]) { // ns4 nested?
				setTimeout("setFocus(document.forms.cc_data.mnth_pay)",100);
			} else {
				frm.mnth_pay.focus();
				frm.mnth_pay.select();
			}
			return;
    }
    tot_int += fin_chg;
  }
	
// display result
	frm.num_months.value = num_mnths;
	frm.total_pay.value ="$" + commafy(formatDec(init_bal + tot_int,2));
	frm.total_int.value ="$" + commafy(formatDec(tot_int,2));
}

// generic positive number decimal formatting function
// from JS Bible by Danny Goodman
function formatDec(expr,decplaces) {
	var str = "" + Math.round (eval(expr) * Math.pow(10,decplaces));
	while (str.length <= decplaces) {
		str = "0" + str;
	}
	var decpoint = str.length - decplaces;
	return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);
}

// insert commas for display of large numbers
// adapted from JS Bible by Danny Goodman
function commafy(val) {
	if (1) { // or opera gives syntax error on re
		var re = /(\d+)(\d{3})/;
		while (re.test(val)) {
			val = val.replace(re,"$1,$2");
		}
	}
	return val;
}

