function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function format(expr,decplaces) {
	var str = "" + Math.round(eval(expr) * Math.pow(10,decplaces));
	while (str.length <= decplaces) {
		str = "0" + str
	}
	var comma = str.length + 3
	var decpoint = str.length - decplaces

	return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);
}

function dollarize(expr) {
 	return "$" + format(expr,2)
}

function validate_form1()
{
    valid = true;
    if ( document.form_1.amt.value == 0) {document.form_1.months.value="Error: Dollar amount field cannot = 0"; document.form_1.interest.value=valid = ''; valid = false;}
    if ( document.form_1.bal.value == "" || document.form_1.rate.value == "" || document.form_1.pay.value == "" || document.form_1.amt.value == "") {
	document.form_1.months.value="Please complete the form!"; document.form_1.intrest.value=valid = ''; valid = false;}
    return valid;
}

function payoff(form,valid) 
{
if (valid == true){

var balance = form.bal.value;
var APR = form.rate.value;
var minPay = form.pay.value;
var minAmount = form.amt.value; 
var minPayment = 0;
var intPaid = 0;
var intr = 0;
var totalIntPaid = 0;
var month = 0;
var minPer = 0;

    while (balance > 0) 
    {
          minPayment = balance * (minPay / 100);
		  
          if (minPayment < minAmount) {minPayment = minAmount;}
          intPaid = balance * (1+(APR / 100)/12);
          intr = intPaid - balance;
          var x = balance;
          balance = intPaid - minPayment;
          if (balance > x) {
               month = -1;
               break;
          }
          else {
          if (balance < minPayment) {minPayment = balance;}
          if (balance < 0) {balance = 0; minPayment = 0;}
          totalIntPaid = totalIntPaid + intr;
          month++}
    }

if (month <= 1) {
	if (month == -1) {
		month = "Warning! Payoff not possible because the payment is to low!"
		totalIntPaid = "Not calculated";
	}
	else {
		if (month > 24) {
			year = month / 12;
			month = "It will take approx " + format(year,2) + " year to pay off this debt."
			totalIntPaid = "You will have paid " + addCommas(dollarize(totalIntPaid)) + " in interest.";
		}
		else {
			month = "It will take approx " + month + " month to pay off this debt."
			totalIntPaid = "You will have paid " + addCommas(dollarize(totalIntPaid)) + " in interest.";
		}
	}
}
else {
		if (month > 24) {
			year = month / 12;
			month = "It will take approx " + format(year,2) + " years to pay off this debt."
			totalIntPaid = "You will have paid " + addCommas(dollarize(totalIntPaid)) + " in interest.";
		}
		else {
			month = "It will take approx " + month + " months to pay off this debt."
			totalIntPaid = "You will have paid " + addCommas(dollarize(totalIntPaid)) + " in interest.";
		}
}
form.months.value = month;
form.interest.value = totalIntPaid
}
}

function validate_form2()
{
    valid = true;
    if ( document.form_1.bal.value == "" || document.form_1.add.value == "" || document.form_1.rate.value == "" || document.form_1.time.value == "") {
	document.form_1.total.value="Please complete the form!"; document.form_1.interest.value=valid = ''; valid = false;
	}
    return valid;
}

function futureValue(form) 
{
if (valid == true){
	var p = form.bal.value;
	form.bal.value = format(form.bal.value,2);
	var a = form.add.value;
	var t = 0;
	var r = form.rate.value;
	var y = form.time.value;
	var I = form.intervals.value;
	var insidePar = (1 + parseFloat(r/100)/I);

	a = parseFloat(a)/I;
	p = parseFloat(p);
	for (i=0; i < y * I; i++)
	{ 
		p = (p + a) * insidePar;
	}
	if (y <= 1)  {form.total.value = "After " + y + " year, you will have " + addCommas(dollarize(p));}
	else {form.total.value = "After " + y + " years, you will have " + addCommas(dollarize(p));}
}
}

function doubleValueRate(form) 
{
if (valid == true){
	var p = 1;
	form.bal.value = format(form.bal.value,2);
	var t = 0;
	var y = form.time.value;
	form.add.value = format(form.add.value,2);
	var expo = 1/y;
 
	p = parseFloat(p);
	y = parseFloat(y);
	t = (Math.pow(2 * p,expo)-1)*100;

	if (y < 0.5) {form.total.value = "Error: Target Years must be larger then .5 (6 months)"}
	else {form.total.value = "Rate of return: " + format(t,2) + "%"; }
}
}

function doubleValueTime(form) 
{
if (valid == true){
	var r = form.rate.value;
	var t = Math.log(2)/Math.log(1+(r/100))
	if (t <= 1)  {
		t = t * 12;
		form.total.value = "It will take " + format(t,2) + " months to double your investment";
	}
	else {form.total.value = "It will take " + format(t,2) + " years to double your investment";}
  }
}


function presentValue(form) 
{
if (valid == true){
	var p = form.bal.value;
	form.bal.value = format(form.bal.value,2);
	var t = 0;
	var r = form.rate.value;
	var y = form.time.value;
	var insidePar = (1 + parseFloat(r/100));
	p = parseFloat(p);
	t = p / Math.pow(insidePar,y);

	form.total.value = "You will need to deposit " + addCommas(dollarize(t));
}
}

function rateValue(form) 
{
if (valid == true){
	var p = form.bal.value;
	form.bal.value = format(form.bal.value,2);
	var t = 0;
	var y = form.time.value;
	var a = form.add.value;
	form.add.value = format(form.add.value,2);
	var insidePar = a/p;
	var expo = 1/y;
 
	p = parseFloat(p);
	t = (Math.pow(insidePar,expo)-1)*100;

	if (t < 0) {form.total.value = "Error: Future value must be larger then Present value"}
	else {form.total.value = "Rate of return: " + format(t,2) + "%"; }
}
}

function mortgagePayoff(form) 
{
if (valid == true){

	var p = form.bal.value;
	form.bal.value = format(form.bal.value,2);
	var r = form.rate.value;
	var t = 0;
	var y = form.time.value;
	if (r == 0) {r = .00001}
	if (y == 0) {form.total.value = "Error: Enter a vaild time table for mortgage pay off"}
	else {
		c = (r / 12) * .01; 
		var firPar = r / (12 * 100);
		var secPar = 1 - Math.pow(1 + firPar,-(y * 12)); 

		p = parseFloat(p);
		t = p * firPar / secPar;
		form.total.value = "The approx monthly payment will be " + addCommas(dollarize(t));
	}
}
}



