// JavaScript Document

/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by: Jeremy Zongker :: http://www.destroydebt.com/ */

function ddCalc(showAmort) {
	/* Checks to make sure the intial Deposit Amount was entered */
  if (document.getElementById('ddInitialDeposit').value=='') {alert('Please enter an initial deposit amount.'); return;}

	/* Checks to make sure the Monthly Contribution was entered */
  if (document.getElementById('ddContribution.value')=='') {alert('Please enter a monthly contribution amount.'); return;}
  
  	/* Checks to make sure the Assumed Monthly return was entered */
  if (document.getElementById('ddRate.value')=='') {alert('Please enter an assumed rate of return.'); return;}
  
  	/* Checks to make sure the Number of months was entered */
  if (document.getElementById('ddMonths.value')=='') {alert('Please enter the number of months.'); return;}

  	/* Converts Assumed monthly return from percent to decimal */
  var mRate=(document.getElementById('ddRate').value/100);
  
  	/* Sets the Maximum months allowed to 999999 */
  var maxMonths=999999;
  
  	/* Sets the Maximum value allowed to 99999999999 */
  var maxAmount=99999999999;

  if (document.getElementById('ddMonths').value!='') maxMonths=document.getElementById('ddMonths').value;

  var amort='<table width="100%" cellpadding="2" cellspacing="0" style="text-align:right; border: solid 1px #000000;"><tr><th>Month</th><th>Contributions</th><th>Interest</th><th>Total</th></tr>';

  //Get a decimal
  var currentAmount=0.1-0.1;
  var currentContributions=0.1-0.1;
  var currentInterest=0.1-0.1;
  
	/* Gets the intial deposit ammount */
  currentAmount+=document.getElementById('ddInitialDeposit').value
  
  /* Gets the monthly contribution */
  currentContributions+=document.getElementById('ddContribution').value;
  
  /* Gets the number of months */
  var monthlyAmount=Number(document.getElementById('ddMonths').value);

  var currentMonth=0;

  while (currentMonth<maxMonths && currentAmount<maxAmount) {
    currentMonth++;
    var interest=Number(currentAmount)*Number(mRate);
    currentInterest=Number(interest);
    currentContributions=Number(currentContributions);
    currentAmount=Number(currentAmount)+Number(interest)+ currentContributions;
	
    amort+='<tr><td style="text-align:center;">' + currentMonth 
	+ '</td><td style="text-align:center;">$' 
	+ Number(currentContributions).toFixed(2) 
	+ '</td><td  style="text-align:center;">$' 
	+ Number(currentInterest).toFixed(2) 
	+ '</td><td style="text-align:center;">$' 
	+ Number(currentAmount).toFixed(2) 
	+ '</td></tr>';
  }

  document.getElementById('ddResult').innerHTML="With an initial deposit of $" 
  + Number(document.getElementById('ddInitialDeposit').value).toFixed(2) 
  + ", monthly contributions of $" 
  + currentContributions.toFixed(2) 
  + " and an assumed monthly interest rate of " 
  + document.getElementById('ddRate').value 
  + "% for " + Number(currentMonth) 
  + " months, your total account balance will be: $" 
  + Number(currentAmount).toFixed(2) 
  + ".";
  
  if (showAmort) {
    document.getElementById('ddResult').innerHTML+='<br /><br /><strong>Amortization Schedule</strong><br /><div style="overflow:auto; height:600px; width:100%;">' + amort + '</table></div>';
  } else {
    document.getElementById('ddResult').innerHTML+=' <p style="text-align:center;"><a href="javascript:ddCalc(true);">Show Amortization</a></p>';
  }
}


