//account.js

//constructor 
function account(balance, cont, contIncr, returnRate, beginAge, contEndAge, allowNegBalance)
{
	this.mCont = Number(cont);
	this.mContIncrFactor = 1+contIncr;
	this.mReturnFactor = 1+returnRate;
	this.mBeginAge = beginAge;
	this.mContEndAge = contEndAge;
	this.mAllowNegBalance = allowNegBalance;
	
	this.mBalanceArray = new Array();
	this.mBalanceArray[beginAge] = balance;
	this.mNextCont = this.mCont;
	this.mAssetsRunOutAge = 0;
	
	//methods
	this.mProjectYear = projectYear;
	this.mPVBalance = pvBalance; //added 01/13/2005
	return true;
}

function projectYear(age, neededWithdrawal)
{
	
//	if (!isFloatValid(neededWithdrawal,0))
//		return false;

	var withdrawal = Math.min(Math.max(0,
	                                   this.mBalanceArray[age]), 
	                          neededWithdrawal);
		
	this.mBalanceArray[age+1] = this.mBalanceArray[age];
	age++;
	if (this.mAllowNegBalance)
		this.mBalanceArray[age] -= neededWithdrawal;
	else
		this.mBalanceArray[age] -= withdrawal;
	this.mBalanceArray[age] *= this.mReturnFactor;
	this.mBalanceArray[age] += this.mNextCont;
	
	if (this.mBalanceArray[age] < -0.5 && this.mAssetsRunOutAge == 0)
		this.mAssetsRunOutAge = age-1;	

	if (age < this.mContEndAge) 
		this.mNextCont *= this.mContIncrFactor;
	else		
		this.mNextCont = 0;
		
	return withdrawal;
}

function pvBalance(pvAge, futureAge) //added 01/13/2005
{
	return this.mBalanceArray[futureAge] / Math.pow(this.mReturnFactor,futureAge - pvAge);
}

