
 (function(){
 if(document.balancecalculator.creditcardbalance) {
  document.balancecalculator.creditcardbalance.focus();
 }
});

 function number_format( number, decimals, decpoint, thousandsep ) {

  var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;

  var d = decpoint == undefined ? "," : decpoint;

  var t = thousandsep == undefined ? "." : thousandsep, s = n < 0 ? "-" : "";

  var i = parseInt(n = Math.abs(+n || 0).tofix(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;

   
  return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).tofix(c).slice(2) : "");

  }

var n=0
var interest=0
var ibal=0
var curbal=0
var mpay=0
var fincharge=0
var nomonths=0
var tint=0
  

function creditcardbalancecalculator()
{
 if(document.balancecalculator.interest.value==""||document.balancecalculator.period.selectedIndex<0||document.balancecalculator.creditcardbalance.value=="")
  {
    alert("Please fill in the required fields.") 
  }
 else
  { 

n = document.balancecalculator.period.value;
    interest=(document.balancecalculator.interest.value/100)/12
    curbal=document.balancecalculator.creditcardbalance.value;

if(document.balancecalculator.payment.value==""){
      mpay=document.balancecalculator.creditcardbalance.value * 0.03;
    }
    else {
      mpay=document.balancecalculator.payment.value;
      
    }

	nomonths=1
    tint=0
    fincharge=0
    while(nomonths <= n && curbal>0){
      fincharge=curbal*interest
      curbal=curbal-mpay+fincharge
      nomonths++
      tint+=fincharge
    }  
  }
 document.balancecalculator.totalinterest.value = Math.round(tint*100)/100;
}
