
//*******************************************
//DO NOT REMOVE THIS COPYWRITE INFO!
//Credit Card Minimum Payment Interest Calculator V2
//2001 Daniel C. Peterson ALL RIGHTS RESERVED
//Created: 01/17/2001
//Last Modified: 05/16/2002
//This script may not be copied, edited, distributed or reproduced
//without express written permission from
//Daniel C. Peterson of Web Winder Website Services
//For commercial use rates, contact:
//Dan Peterson:
//Web Winder Website Services
//P.O. Box 11
//Bemidji, MN  56619
//dan@webwinder.com
//http://www.webwinder.com
//Commercial User Licence #:4519-1056-12-1019
//Commercial Licence Date:2007-04-09
//*******************************************



function stripNum(num) {

var iPercent
var iDollar
var iSpace
var iComma
var numLength = num.length

//lalalla Line #114

if(numLength > 0) {

   num=num.toString();

   iPercent = num.indexOf("%");
   if(iPercent >= 0) {
      num=num.substring(0,iPercent) + "" + num.substring(iPercent + 1,numLength);
      numLength=num.length;
      }
   iDollar = num.indexOf("$");
   if(iDollar >= 0) {
      num=num.substring(0,iDollar) + "" + num.substring(iDollar + 1,numLength);
      numLength=num.length;
      }
   iSpace = num.indexOf(" ");
   if(iSpace >= 0) {
      num=num.substring(0,iSpace) + "" + num.substring(iSpace + 1,numLength);
      numLength=num.length;
      }
   iComma = num.indexOf(",");
   if(iComma >= 0) {
      while(iComma >=1) {
         num=num.substring(0,iComma) + "" + num.substring(iComma + 1,numLength);
         numLength=num.length;
         iComma = num.indexOf(",");
      }
      }

      num = eval(num);


} else {

num = 0;

}

return num;

}




function formatCurrency(num) {

var isNeg=0;

    if(num < 0) {
       num=num*-1;
       isNeg=1;
    }

    
	onum=Math.round(num*100)/100;
		
	integer=Math.floor(onum);

	if (Math.ceil(onum) == integer) {
		decimal="00";
	} else{
		decimal=Math.round((onum-integer)*100)
	}
	decimal=decimal.toString();
	if (decimal.length<2) decimal="0"+decimal;

	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;

    if(isNeg == 1) {
       finNum = "-" + finNum;
    }

	return finNum;
}




function formatNumber(num) {

var isNeg=0;

    if(num < 0) {
       num=num*-1;
       isNeg=1;
    }

    
	onum=Math.round(num*100)/100;
		
	integer=Math.floor(onum);

	if (Math.ceil(onum) == integer) {
		decimal="00";
	} else{
		decimal=Math.round((onum-integer)*100)
	}
	decimal=decimal.toString();
	if (decimal.length<2) decimal="0"+decimal;

	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;

    if(isNeg == 1) {
       finNum = "-" + finNum;
    }

	return finNum;
}


function computeForm(form) {

if(form.principal.value == 0 || form.principal.value == "") {
   alert("Please enter the balance on your credit card.");
   form.principal.focus();
} else
if(form.interest.value == 0 || form.interest.value == "") {
   alert("Please enter the credit card's annual interest rate.");
   form.interest.focus();
} else
if(form.minpaydol.value == 0 || form.minpaydol.value == "") {
   alert("Please enter the minimum dollar payment amount.");
   form.minpaydol.focus();
} else
if(form.minpayperc.value == 0 || form.minpayperc.value == "") {
   alert("Please enter the percentage your credit card company uses to compute your minimum payment amount.");
   form.minpayperc.focus();
} else {


var i = stripNum(form.interest.value);
if (i >= 1.0) {
 i = i / 100.0;
}
i /= 12;

var j = stripNum(form.minpayperc.value);
if (j >= 1.0) {
   j = j / 100.0;
}

var prin = stripNum(form.principal.value);
var displayPrin = formatCurrency(prin);

var pmt = 0;
var prinPort = 0;
var intPort = 0;
var count = 0;
var accruedInt = 0;

var Vminpaydol = stripNum(form.minpaydol.value);
    
while(prin > 0) {
   if(eval(prin * j) < Vminpaydol) {
      pmt = Vminpaydol;
   } else {
      pmt = eval(j * prin);
   }
   intPort = eval(i * prin);
   prinPort = eval(pmt - intPort);
   prin = eval(prin - prinPort);
   accruedInt = eval(accruedInt + intPort);
   count = count + 1
   if(count > 600) {
      break;
   } else {
      continue;
   }
}

var displayInt = formatCurrency(accruedInt);
form.ccInt.value = displayInt;

var displayPmts = parseInt(count,10);
form.nPer.value = displayPmts;

var displayYears = formatNumber(count / 12);
form.years.value = displayYears;

var moInvest = accruedInt / count;
var countInv = 0;
var investPrin = moInvest;
var accumEarnings = 0;
var investIntPort = 0;
var investPrinPort = 0;
var investInt = .07 / 12;

while(countInv < count) {
   investintPort = eval(investInt * investPrin);
   investPrin = eval(investPrin) + eval(investintPort) + eval(moInvest);
   countInv = countInv + 1
   if(countInv > 600) { break; } else { continue;}
   }

var displayLostInt = formatCurrency(investPrin);
form.lostInt.value = displayLostInt;

var totLost = eval(accruedInt) + eval(investPrin);
var displayTotLost = formatCurrency(totLost);
form.totalCost.value = displayTotLost;

form.summary.value = "If you make only the minimum monthly payment suggested by your credit card statement, by the time you pay off your " + displayPrin + " balance, you will have made " + displayPmts + " payments (that's " + displayYears + " years!) and you will have paid " + displayInt + " in interest charges!\r\rPlus, if you were investing the average monthly interest charge -- instead of sending it to the credit card company -- at only a 7% return, you could have earned roughly " + displayLostInt + " during the same " + displayYears + "-year period. That represents a total cost of " + displayTotLost + "!!";
}
}
