
if (Calculators == undefined) {
    var Calculators = {};
}
if (Calculators.planning == undefined) {
    Calculators.planning = {};
}

Calculators.planning.CollegeSavings = function CollegeSavings(savings, monthly, rateOfReturn, inflation){
	this.savings = new Calculators.type.Money(savings);
	this.monthly = new Calculators.type.Money(monthly);
	this.rateOfReturn = new Calculators.type.Rate(rateOfReturn);
	this.inflation = new Calculators.type.Rate(inflation);
	this.expenses = new Array();
	this.yearlySnapshots = new Array();
	
	this.addExpense = function(currentAge, collegeAge, tuition, board){
		var expense = new Calculators.planning.CollegeExpense(currentAge, collegeAge, tuition, board);
		this.expenses.push(expense);
	}
	
	this.clearExpenses = function(){
		while (this.expenses.length > 0) {
			this.expenses.pop();
		}
	}

	this.getSavings = function() {
		return this.savings;
	}
	
	this.getMonthlyContributions = function() {
		return this.monthly;
	}
	
	this.getRateOfReturn = function() {
		return this.rateOfReturn;
	}
	
	this.getRateOfInflation = function() {
		return this.inflation;
	}
	
	this.getNumberOfExpenses = function() {
		return this.expenses.length;
	}

	this.getExpenses = function() {
		return this.expenses;
	}
	
	this.getCostsByExpense = function() {
		var costs = [];
		for (var i = 0; i<this.expenses.length; i++) {
			costs[i] = this.expenses[i].calculateCosts(this.inflation);
		}
		return costs;
	}
	
	this.clearYearlySnapshots = function() {
		while (this.yearlySnapshots.length > 0) {
			this.yearlySnapshots.pop();
		}
	}
	
	this.calculate = function() {
		var taxRate = 0.535;
//		var taxRate = 0.5333333333333333333;
//		var capitalGainsTax = 0.15;
//		var shortTermGainsTax = 0.35;
		
		this.clearYearlySnapshots();
		var runningTotal = this.savings.getAmount();
		
		var keepGoing = true;
		for (var i=1; keepGoing; i++) {
			var yearlyExpenses = 0.00;
			
			// When all the expenses are passed college, exit
			keepGoing = false;
			for (var j=0; (j<this.expenses.length) && !keepGoing; j++) {
				if (this.expenses[j].isPostCollege(i) == false) {
					keepGoing = true;
					yearlyExpenses = yearlyExpenses + this.expenses[j].getExpenseAmount(this.inflation, i).getAmount();
				}
			}

			if (keepGoing) {
				// Create a new yearly snapshot taking into account
				//  + rate of return on investments
				//  + additional monthly contributions (beginning of the month)
				//  - expenses for kids in college
				var beginningBalance = runningTotal;
				var contributions = 12 * this.monthly.getAmount();
//				var investmentGrowth = new Calculators.type.Money((beginningBalance * this.rateOfReturn.getAmount()) + 
//					(contributions * this.rateOfReturn.getAmount()));


				var investmentGrowth = new Calculators.type.Money((beginningBalance * this.rateOfReturn.getAmount()) + 
					(contributions * taxRate * this.rateOfReturn.getAmount()));

//				var investmentGrowth = new Calculators.type.Money(
//					(beginningBalance * (1.0 - capitalGainsTax) * this.rateOfReturn.getAmount()) +
//					(contributions * taxRate * this.rateOfReturn.getAmount())
//					);

				var currentYear = new Calculators.type.CollegeSavingsYearlySnapshot(beginningBalance, investmentGrowth,
					contributions, new Calculators.type.Money(yearlyExpenses));
				this.yearlySnapshots[i] = currentYear;

				runningTotal = currentYear.getEndingBalance().getAmount();
			}			
		}
	}

	this.getYearlySnapshots = function() {
		return this.yearlySnapshots;
	}
	
	this.getMaxYears = function() {
		var highestYet = 0;
		for (var i = 0; i < this.expenses.length; i++) {
			var expense = this.expenses[i];
			var years = expense.getYearsUntilCollege() + 4;
			if (highestYet < years) {
				highestYet = years;
			}
		}
		return highestYet;
	}
	
	this.getTotalCosts = function() {
		var total = 0;
		var expenseCosts = this.getCostsByExpense();
		for (var i = 0; i < expenseCosts.length; i++) {
			total += expenseCosts[i].getAmount();
		}
		return new Calculators.type.Money(total);
	}
	
	this.getMonthlyContributionsRequired = function() {
		var total = this.getTotalCosts();
		var formula = new Calculators.math.Formulas();
		var test = formula.required_deposits(total.getAmount(), this.rateOfReturn.getAmount(), this.getMaxYears());
		return new Calculators.type.Money(test / 12);
		//alert(total.getAmount() + " / " + this.getMaxYears() + " compared to " + test);
		//return new Calculators.type.Money(total.getAmount() / this.getMaxYears());
	}
}

