﻿var daCalculator =
{
    initialInvestment: 0,
    interestRate: 0,
    contributionAmount: 0,
    contributionPeriod: 1,
    compoundPeriod: 1,
    compoundRate: 0,
    years:1,
    getNumeric: function (input) {
        var result=input.replace('$','').replace(/,/g,'').replace('%','');
        return parseFloat(result);
    },
    populateValues: function () {
        var initialField=document.getElementById('daCalc_initial');
        var contributionField=document.getElementById('daCalc_contribution');
        var rateField=document.getElementById('daCalc_rate');
        var contributionPeriodField=document.getElementById('daCalc_contributionPeriod');
        //var compoundField=document.getElementById('daCalc_compound');
        var yearsField=document.getElementById('daCalc_years');
        
        this.initialInvestment=this.getNumeric(initialField.value);
        this.contributionAmount=this.getNumeric(contributionField.value);
        this.interestRate=this.getNumeric(rateField.value);
        this.contributionPeriod=this.getNumeric(contributionPeriodField.options[contributionPeriodField.selectedIndex].value);
        //this.compoundPeriod=this.getNumeric(compoundField.options[compoundField.selectedIndex].value);
        this.years=this.getNumeric(yearsField.value);
        
        var monthlyRate=Math.pow(1 + this.interestRate/100,1/12)-1;
        this.compoundRate=Math.pow(1 + monthlyRate,this.compoundPeriod)-1;
    },
    calculate: function ()
    {
        this.populateValues();
        var months=this.years*12;
        var totalContributions=this.initialInvestment;
        var totalValue=this.initialInvestment;
        var rows='';
        rows+=this.outputRow(0,totalContributions,totalValue);
        var totalSinceLastCompound=0;
        for (i=1;i<=months;i++) {
            totalSinceLastCompound+=totalValue;
            if (i % this.compoundPeriod == 0) {
                averageValue=(totalSinceLastCompound/this.compoundPeriod);
                totalValue+=averageValue*this.compoundRate;
                totalSinceLastCompound=0;
            }
            if (i % this.contributionPeriod == 0) {totalContributions+=this.contributionAmount;totalValue+=this.contributionAmount;}
            rows+=this.outputRow(i,totalContributions,totalValue);
        }
        
        var totalInterest=Math.round((totalValue-totalContributions)*100)/100;
        document.getElementById('daCalcTable').innerHTML=rows;
        document.getElementById('daCalc_totalContributions').innerHTML=this.formatCurrency(Math.round(totalContributions*100)/100);
        document.getElementById('daCalc_totalInterest').innerHTML=this.formatCurrency(totalInterest);
        document.getElementById('daCalc_totalValue').innerHTML=this.formatCurrency(Math.round(totalValue*100)/100);
    },
    outputRow: function(month, principal, value)
    {
        var alt=(month % 2 == 1);
        value=Math.round(value*100)/100;
        var interest=Math.round((value-principal)*100)/100;
        var monthString=this.formatDate(this.addMonths(month));
        
        var result='<tr';
        if (alt) result+=' class="alt"';
        result+='><td>' + monthString + '</td><td>' + this.formatCurrency(principal) + '</td><td>' + this.formatCurrency(interest) + '</td><td>' + this.formatCurrency(value) + '</td></tr>';
        return result;
    },
    formatCurrency: function(input)
    {
        var parts=String(input).split('.');
        var dollars=parts[0];
        var cents=parts[1];
        if (cents==null) cents='00';
        if (dollars==null) dollars='0';
        if (cents.length==0) cents='00';
        if (cents.length==1) cents+='0';
        if (dollars.length>9) {dollars=dollars.substr(0,dollars.length-9) + ',' + dollars.substr(dollars.length-9,9);}
        if (dollars.length>6) {dollars=dollars.substr(0,dollars.length-6) + ',' + dollars.substr(dollars.length-6,6);}
        if (dollars.length>3) {dollars=dollars.substr(0,dollars.length-3) + ',' + dollars.substr(dollars.length-3,3);}
        return '$' + dollars + '.' + cents;
    },
    formatDate: function(input)
    {
        var monthNames=new Array('January','February','March','April','May','June','July','August','September','October','November','December','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
        return monthNames[input.getMonth()] + ', ' + input.getFullYear();
    },
    addMonths: function(months)
    {
        var tempDate=new Date();
        var year=tempDate.getFullYear();
        var month=tempDate.getMonth();
        for (i=0;i<months;i++)
        {
            month++;
            if (month>12) {year++;month=1;}
        }
        tempDate.setDate(1);
        tempDate.setMonth(month);
        tempDate.setFullYear(year);
        return tempDate;
    }
}
