//By Daniel C. Peterson
//Web Winder Website Services, 1997-2009  All Rights Reserved.
//Distribution, editing or reselling of this script is strictyly prohibited
//without expressed written permission from Daniel C. Peterson.
//For commercial grade (professional) versions of this and many other
//calculators, visit http://www.webwinder.com.
 
function sn(num) {
 
   num=num.toString();
 
 
   var len = num.length;
   var rnum = "";
   var test = "";
   var j = 0;
 
   var b = num.substring(0,1);
   if(b == "-") {
      rnum = "-";
   }
 
   for(i = 0; i <= len; i++) {
 
      b = num.substring(i,i+1);
 
      if(b == "0" || b == "1" || b == "2" || b == "3" || b == "4" || b == "5" || b == "6" || b == "7" || b == "8" || b == 
 
"9" || b == ".") {
         rnum = rnum + "" + b;
 
      }
 
   }
 
   if(rnum == "" || rnum == "-") {
      rnum = 0;
   }
 
   rnum = Number(rnum);
 
   return rnum;
 
}
 
function fc(num, places, comma) {
 
var isNeg=0;
 
    if(num < 0) {
       num=num*-1;
       isNeg=1;
    }
 
    var myDecFact = 1;
    var myPlaces = 0;
    var myZeros = "";
    while(myPlaces < places) {
       myDecFact = myDecFact * 10;
       myPlaces = eval(myPlaces) + eval(1);
       myZeros = myZeros + "0";
    }
    
	onum=Math.round(num*myDecFact)/myDecFact;
		
	integer=Math.floor(onum);
 
	if (Math.ceil(onum) == integer) {
		decimal=myZeros;
	} else{
		decimal=Math.round((onum-integer)* myDecFact)
	}
	decimal=decimal.toString();
	if (decimal.length<places) {
        fillZeroes = places - decimal.length;
	   for (z=0;z<fillZeroes;z++) {
        decimal="0"+decimal;
        }
     }
 
   if(places > 0) {
      decimal = "." + decimal;
   }
 
   if(comma == 1) {
	integer=integer.toString();
	var tmpnum="";
	var tmpinteger="";
	var y=0;
 
	for (x=integer.length;x>0;x--) {
		tmpnum=tmpnum+integer.charAt(x-1);
		y=y+1;
		if (y==3 & x>1) {
			tmpnum=tmpnum+",";
			y=0;
		}
	}
 
	for (x=tmpnum.length;x>0;x--) {
		tmpinteger=tmpinteger+tmpnum.charAt(x-1);
	}
 
 
	finNum=tmpinteger+""+decimal;
   } else {
      finNum=integer+""+decimal;
   }
 
    if(isNeg == 1) {
       finNum = "-" + finNum;
    }
 
	return finNum;
}
 
 
function computeForm(form) {
 
   if(form.principal.value.length == 0 || form.principal.value == "0") {
      alert("Please enter a principal amount.");
      form.principal.focus();
   } else
   if(form.interest.value.length == 0 || form.interest.value == "0") {
      alert("Please enter debt's annual interest rate as a percentage.");
      form.interest.focus();
   } else
   if(form.origPmt.value.length == 0 || form.origPmt.value == "0") {
      alert("Please enter the current monthly payment amount.");
      form.origPmt.focus();
   } else
   if(form.pmtAdd.value.length == 0 || form.pmtAdd.value == "0") {
      alert("Please enter an amount to add to your current monthly payment.");
      form.pmtAdd.focus();
   } else {
 
   var i = sn(form.interest.value);
 
    if (i >= 1.0) {
 
        i = i / 100.0;
 
    }
 
     i /= 12;
 
 
   var prin1 = sn(form.principal.value);
 
 
 
   var prin2 = sn(form.principal.value);
 
   var pmt1 = sn(form.origPmt.value);
 
   if(prin1 * i > pmt1) {
      alert("The terms you entered on not valid (monthly interest charge is greater than monthly payment amount). Please either reduce the principal, reduce the interest rate, increase the payment amount, or a combination of all three.");
      form.principal.focus();
 
   } else {
 
   var pmt1 = sn(form.origPmt.value);
   var v_pmt_add = sn(form.pmtAdd.value);
   var pmt2 = eval(pmt1) + eval(v_pmt_add);
 
   var prinPort1 = 0;
   var prinPort2 = 0;
 
   var intPort1 = 0;
   var intPort2 = 0;
 
   var count1 = 0;
   var count2 = 0;
 
   var accumInt1 = 0;
   var accumInt2 = 0;
    
   while(prin1 > 0) {
                    intPort1 = eval(i * prin1);
                    accumInt1 = eval(accumInt1) + eval(intPort1);
                    prinPort1 = eval(pmt1 - intPort1);
                    prin1 = eval(prin1 - prinPort1);
                    count1 = count1 + 1
                    if(count1 > 600) { break; } else { continue;}
                    }
 
   while(prin2 > 0) {
                    intPort2 = eval(i * prin2);
                    accumInt2 = eval(accumInt2) + eval(intPort2);
                    prinPort2 = eval(pmt2 - intPort2);
                    prin2 = eval(prin2 - prinPort2);
                    count2 = count2 + 1
                    if(count2 > 600) { break; } else { continue;}
                    }
 
      var v_intSave = eval(accumInt1) - eval(accumInt2);
 
      form.intSave.value = fc(v_intSave,2,1);
      var v_roi = (v_intSave / (count2 * v_pmt_add)) / (count2 / 12);
      form.roi.value = fc(v_roi * 100,2,0);
 
     var timSave = eval(count1) - eval(count2);
 
      form.results.value = ("If you add $" + v_pmt_add + " to your monthly payment, you will pay off this debt in " + count2 + " payments instead of " + count1 + ", and you will save $" + fc(v_intSave,2,1) + " in interest charges.  This savings translates into a guaranteed, tax-free, average annual return of " + fc(v_roi * 100,2,0) + "%.  And that's not even considering the emotional returns you'll get when you pay off this debt " + timSave + "-months (" + parseInt(timSave /12,10) + " years, " + (timSave %12) + " months) ahead of schedule!");
 
  }
  }
}
 
function clear_results(form) {
 
 
    form.intSave.value = "";
 
    form.roi.value = "";
 
    form.results.value = "";
 
 
 
}
 
function clearForm(form)
 
{
 
    form.principal.value = "";
 
    form.interest.value = "";
 
    form.origPmt.value = "";
 
    form.pmtAdd.value = "";
 
    form.intSave.value = "";
 
    form.roi.value = "";
 
    form.results.value = "";
}

