
if (Calculators == undefined) {
    var Calculators = {};
}
if (Calculators.planning == undefined) {
    Calculators.planning = {};
}
Calculators.planning.Retirement = function Retirement(currentAge, retirementAge, income, savings, currentRateOfReturn, retiredRateOfReturn, contribution, raise, yearsOfRetirement, expensesAsPercentOfIncome, rateOfInflation, includeSocialSecurity, married){

    this.currentAge = new Number(currentAge);
    this.retirementAge = new Number(retirementAge);
    this.income = new Calculators.type.Money(income);
    this.savings = new Calculators.type.Money(savings);
    this.currentRateOfReturn = new Calculators.type.Rate(currentRateOfReturn);
    this.retiredRateOfReturn = new Calculators.type.Rate(retiredRateOfReturn);
    this.contribution = new Calculators.type.Rate(contribution);
    this.raise = new Calculators.type.Rate(raise);
    this.yearsOfRetirement = new Number(yearsOfRetirement);
    this.expensesAsPercentOfIncome = new Calculators.type.Rate(expensesAsPercentOfIncome);
    this.rateOfInflation = new Calculators.type.Rate(rateOfInflation);
    this.includeSocialSecurity = new Boolean(includeSocialSecurity);
    this.married = new Boolean(married);
    
	this.yearlySnapshots = [];
    this.savingsAtRetirement = new Calculators.type.Money(0);
    this.ageWhenSavingsExpires = new Number(0);
    this.salaryAtRetirement = new Calculators.type.Money(0);
    this.socialSecurityAmount = new Calculators.type.Money(0);
    
    this.calculate = function(){
		
        var runningTotal = this.savings.getAmount();
        var runningSalary = this.income.getAmount();
        var runningExpenses = 0;
        var startingPoint = this.getStartingAge();
        var endingPoint = this.getEndingAge();
        for (var i = startingPoint; i <= endingPoint; ++i) {

			var beginningBalance = runningTotal;
			var investmentGrowth = new Calculators.type.Money(0);
			var currentContribution = new Calculators.type.Money(runningSalary * this.contribution.getAmount());

            if (i < this.retirementAge) {
				runningTotal = runningTotal * (1 + (this.currentRateOfReturn.getAmount()));
				runningTotal = runningTotal + currentContribution.getAmount();

				// increment salary for raise
				runningSalary = runningSalary * (1 + (this.raise.getAmount()));

				investmentGrowth = new Calculators.type.Money(beginningBalance * this.currentRateOfReturn.getAmount());
			}
			else {
				if (i == this.retirementAge) {
					runningExpenses = runningSalary * (this.expensesAsPercentOfIncome.getAmount());
					this.salaryAtRetirement = new Calculators.type.Money(runningSalary);
					this.savingsAtRetirement = new Calculators.type.Money(runningTotal);
				}
				else {
					if (i > this.retirementAge) {
						runningTotal = runningTotal + this.getSocialSecurityAmount().getAmount();
						runningExpenses = runningExpenses * (1 + (this.rateOfInflation.getAmount()));
					}
				}

				currentContribution = new Calculators.type.Money(0);
				currentExpenses = runningExpenses;
				investmentGrowth = new Calculators.type.Money(beginningBalance * this.retiredRateOfReturn.getAmount());
			}

            if (runningTotal < 0) {
                runningTotal = 0;
            }

			var currentYear = new Calculators.type.YearlySnapshot(beginningBalance, investmentGrowth, currentContribution, runningExpenses);

			this.yearlySnapshots[i] = currentYear;

			runningTotal = currentYear.getEndingBalance().getAmount();
			
			if (this.yearlySnapshots[i - 1] && this.yearlySnapshots[i]) {
				if (this.yearlySnapshots[i].getEndingBalance().getAmount() == 0 && this.yearlySnapshots[i - 1].getEndingBalance().getAmount() > 0) {
					this.ageWhenSavingsExpires = i;
				}
			}
        }
    }
	
	this.getSocialSecurityAmount = function(){
		return new Calculators.type.Money(0);
	}

	this.getYearlySnapshots = function() {
		return this.yearlySnapshots;
	}
	
	this.getCurrentAge = function() {
		return this.currentAge;
	}

	this.getStartingAge = function() {
		return this.currentAge + 1;
	}

	this.getEndingAge = function() {
		return (this.retirementAge + this.yearsOfRetirement);
	}

	this.getRetirementAge = function(){
		return this.retirementAge;
	}
	
	this.getIncome = function(){
		return this.income;
	}
	
	this.getSavings = function(){
		return this.savings;
	}
	
	this.getCurrentRateOfReturn = function(){
		return this.currentRateOfReturn;
	}
	
	this.getRetiredRateOfReturn = function(){
		return this.retiredRateOfReturn;
	}
	
	this.getContribution = function(){
		return this.contribution;
	}
	
	this.getRaise = function(){
		return this.raise;
	}
	
	this.getYearsOfRetirement = function(){
		return this.yearsOfRetirement;
	}
	
	this.getExpenses = function(){
		return this.expensesAsPercentOfIncome;
	}
	
	this.getRateOfInflation = function(){
		return this.rateOfInflation;
	}
	
	this.getIncludeSocialSecurity = function(){
		return this.includeSocialSecurity;
	}
	
	this.getMarried = function(){
		return this.married;
	}

	this.getSavingsAtRetirement = function(){
		return this.savingsAtRetirement;
	}
	this.getAgeWhenSavingsExpires = function(){
		return this.ageWhenSavingsExpires;
	}
	this.getSalaryAtRetirement = function(){
		return this.salaryAtRetirement;
	}
	this.getSocialSecurityAmount = function(){
		return this.socialSecurityAmount;
	}
}

