
if (Calculators == undefined) {
    var Calculators = {};
}
if (Calculators.planning == undefined) {
    Calculators.planning = {};
}
Calculators.planning.IRA = function IRA(contributions, workingRateOfReturn,
	retiredRateOfReturn, currentAge, retirementAge, yearsOfRetirement, 
	federalTaxRate, stateTaxRate, stateTaxDeducted, retiredTaxRate) {

	this.contributions = new Calculators.type.Money(contributions);
	this.workingRateOfReturn = new Calculators.type.Rate(workingRateOfReturn);
	this.retiredRateOfReturn = new Calculators.type.Rate(retiredRateOfReturn);
	this.currentAge = new Number(currentAge);
	this.retirementAge = new Number(retirementAge);
	this.yearsOfRetirement = new Number(yearsOfRetirement);
	this.federalTaxRate = new Calculators.type.Rate(federalTaxRate);
	this.stateTaxRate = new Calculators.type.Rate(stateTaxRate);
	this.stateTaxDeducted = new Boolean(stateTaxDeducted);
	this.retiredTaxRate = new Calculators.type.Rate(retiredTaxRate);

	this.snapshots = [];
	
	/*
	this.calculate = function() {
		var startingPoint = this.currentAge;
		var endingPoint = this.retirementAge;
		
		var runningTotal = 0.00;
		for (var i=startingPoint; i<endingPoint; i++) {
			runningTotal = runningTotal + this.contribution.getAmount();
			var interest = runningTotal * this.rateOfReturn.getAmount();
			runningTotal = runningTotal + interest;

			//var taxRate = this.currentTaxRate;
			var snapshot = new Calculators.type.IRASnapshot(runningTotal, 
				this.contribution, this.rateOfReturn.getAmount(), this.currentTaxRate.getAmount(), this.retiredTaxRate.getAmount());
			
			this.snapshots[i] = snapshot;
		}
	}
	
	this.calculate();
	*/

	this.getAnnualContributions = function() {
		return this.contributions;
	}
	
	this.getPreRetirementRateOfReturn = function() {
		return this.workingRateOfReturn;
	}
	
	this.getRetirementRateOfReturn = function() {
		return this.retiredRateOfReturn;
	}
	
	this.getCurrentAge = function() {
		return this.currentAge;
	}
	
	this.getRetirementAge = function() {
		return this.retirementAge;
	}
	
	this.getYearsUntilRetirement = function() {
		return (this.retirementAge - this.currentAge);
	}
	
	this.getYearsOfRetirement = function() {
		return this.yearsOfRetirement;
	}
	
	this.getFederalTaxRate = function() {
		return this.federalTaxRate;
	}
	
	this.getStateTaxRate = function() {
		return this.stateTaxRate;
	}
	
	this.getIncludeStateTax = function() {
		return this.stateTaxDeducted;
	}
	
	this.getRetiredTaxRate = function() {
		return this.retiredTaxRate;
	}

	this.getCombinedTaxRate = function() {
		// Rate = B6+B7
		var rate = this.federalTaxRate.getAmount() + this.stateTaxRate.getAmount();
		if (this.stateTaxDeducted) {
			// Rate = B6+B7-(B6*B7)
			rate -= (this.federalTaxRate.getAmount() * this.stateTaxRate.getAmount())
		}
		
		return new Calculators.type.Rate(rate);
	}
	
	this.getGrossPayNeededForContribution = function() {
		// (contribution / (1 - tax rate)
		var taxRate = this.getCombinedTaxRate().getAmount();
		return new Calculators.type.Money(this.contributions.getAmount() / (1 - taxRate));
	}
	
	this.getTaxSavings = function() {
		return this.getGrossPayNeededForContribution().subtract(this.contributions);
	}
	
	this.getTotalAnnualContribution = function() {
		return this.contributions.add(this.getTaxSavings());
	}
	
	this.getTotalAccumulatedAtRetirement = function() {
		// -(FV((B12),B11,B16,0,0))
		var years = this.getYearsUntilRetirement();
		var totalContributions = this.getTotalAnnualContribution().getAmount();
		
		var formula = new Calculators.math.Formulas();
		var value = formula.fv(this.workingRateOfReturn.getAmount(), years, totalContributions, 0, 0);
		return new Calculators.type.Money(-1 * value);
	}
	
	this.getRetirementMonthlyIncome = function() {
		// -(PMT(B23/12,B22*12,B17,0,0))
		var rateOfReturn = this.retiredRateOfReturn.monthly();
		var months = (this.yearsOfRetirement * 12);
		var total = this.getTotalAccumulatedAtRetirement().getAmount();
		
		var formula = new Calculators.math.Formulas();
		var income = formula.pmt(rateOfReturn, months, total, 0, 0);
		return new Calculators.type.Money(-1 * income);
	}
	
	this.getRetirementMonthlyIncomeAfterTaxes = function() {
		// B24-(B24*B21)
		var income = this.getRetirementMonthlyIncome().getAmount();
		return new Calculators.type.Money(income - (income * this.retiredTaxRate.getAmount()));
	}
}


