// JavaScript Document - Retirement Calculator Functions


function workhorse(p, n, i) 
{ 
  if (i > 0 && p > 0 && n > 0) 
    return (p * i)/ (Math.pow(1 + i, n) - 1); 
  else 
	{
	  alert("Please enter a numeric values");  
	  return 0;	
	} 

} 
 
 
function calculate() 
{ 
  var p = parseFloat(document.getElementById("amount").value); 
  var i = parseFloat(document.getElementById("apr").value) / 100; 
  var l = parseFloat(document.getElementById("term").value); 
 
  var result = workhorse(p, l*12, i / 12); 
  document.getElementById("total").value = Math.floor(result * 100)/100; 

  return 0; 
} 
 function formatCurrency(num) {
   num = num.toString().replace(/\$|\,/g,'');
   if(isNaN(num))
     num = "0";
     sign = (num == (num = Math.abs(num)));
     num = Math.floor(num*100+0.50000000001);
     cents = num%100;
     num = Math.floor(num/100).toString();
  if(cents<10)
    cents = "0" + cents;
 for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
  num = num.substring(0,num.length-(4*i+3))+','+
  num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + num + '.' + cents);
} 


function replace_punct(element) { //v2.0
   var c= element.value; 
   c=c.replace(/[|,|\$]| /g, '');
   element.value = c;
   return true;
}