/*************************************************************************
  This code is from Dynamic Web Coding at http://www.dyn-web.com/
  Copyright 1999-2004 by Sharon Paine 
  See Terms of Use at http://www.dyn-web.com/bus/terms.html
  regarding conditions under which you may use this code.
  This notice must be retained in the code as is!
*************************************************************************/
 
function isValid(fld,a,b) {
	var check, msg="";
	if (arguments.length==1) {
		check = isNaN(fld.value) || fld.value=='' || fld.value<0;
		msg = "Invalid entry. Enter a positive number please.";
	}	else if (arguments.length==3) {
		check = isNaN(fld.value) || fld.value=='' || fld.value<a || fld.value>b;
		msg = "Invalid entry. Enter a number between " + a + " and " + b + ".";
	}
	if (check) {
		alert(msg);
		if ( document.forms[fld.form.name] ) { // for ns4, in case form is in positioned layer
			// 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) {
	if ( !isValid(frm.balance, 0, 1000000) ) return false;  // limit balance to 1 million
	if ( !isValid(frm.interest, 0, 30) ) return false;  // limit interest to 30%
	var init_bal = parseFloat(frm.balance.value);
	if ( !isValid(frm.mnth_pay, 0, init_bal) ) return false;  // payment must be less than balance
	// variables used in calculation
	var cur_bal = init_bal;		// used in loop
	var interest = parseFloat(frm.interest.value)/100;
	var mnth_pay = parseFloat(frm.mnth_pay.value);
	var fin_chg = 0, num_mnths = 0, 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( formatDecimal(init_bal + tot_int,2) );
	frm.total_int.value ="$" + commafy( formatDecimal(tot_int,2) );
}

// format val to n number of decimal places
// modified version of Danny Goodman's (JS Bible)
function formatDecimal(val, n) {
  n = n || 2;
  var str = "" + Math.round ( parseFloat(val) * Math.pow(10, n) );
  while (str.length <= n) str = "0" + str;
  var pt = str.length - n;
  return str.slice(0,pt) + "." + str.slice(pt);
}


// insert commas for display of large numbers
// adapted from JS Bible by Danny Goodman
function commafy(val) {
	if (1) { // for opera 6, which gives syntax error on re without it 
		var re = /(\d+)(\d{3})/;
		while (re.test(val)) val = val.replace(re,"$1,$2");
	}
	return val;
}
