var _iraCompare_CurrentYear;

//constructor
function IRACompare(setDefaults)
{
	var d = new Date();
	_iraCompare_CurrentYear = d.getFullYear();
	
	//inputs
	if (setDefaults == 0) 
	{
		this.iCurrentAge = "";
		this.iWithdrawAge = "";
		this.iLifeExp = "";
		this.dPreROR = "";
		this.dPostROR = "";
		this.dPreTax = "";
		this.dPostTax = "";
		//added 10/27/2004
		this.dBalance = "";

		this.iCont2004 = "";
		this.iCont2005 = "";
		this.iCont2006 = "";
		this.iCont2007 = "";
		this.iCont2008 = "";
	} 
	else 
	{
		this.iCurrentAge = "47";
		this.iWithdrawAge = "65";
		this.iLifeExp = "90";
		this.dPreROR = "8";
		this.dPostROR = "6";
		this.dPreTax = "33";
		this.dPostTax = "25";
		//added 10/27/2004
		this.dBalance = "10000";

		this.iCont2004 = "3000";
		this.iCont2005 = "3000";
		this.iCont2006 = "3000";
		this.iCont2007 = "4000";
		this.iCont2008 = "4000";
	}

	//intermediate calcs
	
	//outputs
	this.dTradIRA1 = "";
	this.dTax1 = "";
	this.dTotal1 = "";
	this.dRoth1 = "";
	
	this.dTradIRA2 = "";
	this.dTax2 = "";
	this.dTotal2 = "";
	this.dRoth2 = "";

	this.tBestIRA = "";
		
	//methods
	this.mDoCalc = DoCalc;
	this.mGetBalance = GetBalance; 

	return true;
}

function DoCalc()
{
	var contrib = new Array(this.iWithdrawAge - this.iCurrentAge + 1);
	var index = 0;

	for (var age = this.iCurrentAge; age < this.iWithdrawAge; age++, index++)
	{
		switch (_iraCompare_CurrentYear + index)
		{
			case 2004: {contrib[index] = this.iCont2004; break;}
			case 2005: {contrib[index] = this.iCont2005; break;}
			case 2006: {contrib[index] = this.iCont2006; break;}
			case 2007: {contrib[index] = this.iCont2007; break;}
			default:   {contrib[index] = this.iCont2008; break;}
		}
	}
	contrib[index] = 0;	

	//10/27/2004
	this.dTradIRA1 = this.mGetBalance(this.dBalance, contrib,1 + this.dPreROR/100, 1);
	this.dTax1 = this.mGetBalance(0, contrib,1 + this.dPreROR/100 * (1 - this.dPreTax/100), this.dPreTax/100);
	this.dTotal1 = this.dTradIRA1 + this.dTax1;
	this.dRoth1= this.dTradIRA1;

	this.dTradIRA2 = this.dTradIRA1 / FixedAnnuity(1, this.dPostROR/100, this.iLifeExp - this.iWithdrawAge,1) * (1 - this.dPostTax/100);
	this.dTax2 = this.dTax1 / FixedAnnuity(1,this.dPostROR / 100 * (1 - this.dPostTax/100), this.iLifeExp - this.iWithdrawAge, 1);
	this.dTotal2 = Math.round(this.dTradIRA2) + Math.round(this.dTax2);
	this.dRoth2 = this.dRoth1 / FixedAnnuity(1,this.dPostROR/100,this.iLifeExp-this.iWithdrawAge,1);

	if (this.dRoth2 >= this.dTotal2)
	{
		this.tBestIRA = "Roth IRA";
	}
	else
	{
		this.tBestIRA = "Traditional IRA";
	}

	this.dTradIRA1 = fmtNum(this.dTradIRA1,",", "$");
	this.dTax1 = fmtNum(this.dTax1, ",", "$");
	this.dTotal1 = fmtNum(this.dTotal1, ",", "$");
	this.dRoth1 = fmtNum(this.dRoth1, ",", "$");

	this.dTradIRA2 = fmtNum(this.dTradIRA2, ",", "$");
	this.dTax2 = fmtNum(this.dTax2, ",", "$");
	this.dTotal2 = fmtNum(this.dTotal2, ",", "$");
	this.dRoth2 = fmtNum(this.dRoth2, ",", "$");

	return true;
}

//12/27/2004
function GetBalance(balance, contrib, ROR, adjustFactor)
{
	for (var age=this.iCurrentAge, index = 0; age < this.iWithdrawAge; age++, index++)
	{
		//10/27/2004
		balance = (balance + contrib[index] * adjustFactor) * ROR;	
	}
	return  Math.round(balance);
}

function MaxContrib(CurrentAge, Year)
{
	var Age = parseInt(CurrentAge) + parseInt(Year) - parseInt(_iraCompare_CurrentYear);
	var CatchUp = (Age >= 50);
	var Result;

	switch (Year)
	{	
		case 2004:
		{
			Result = 3000;
			if (CatchUp) {Result = Result + 500;}			
			break;
		}
		case 2005:
		{
			Result = 4000;
			if (CatchUp) {Result = Result + 500;}			
			break;
		}

		case 2006:
		{
			Result = 4000;
			if (CatchUp) {Result = Result + 1000;}			
			break;
		}

		case 2007:
		{
			Result = 4000;
			if (CatchUp) {Result = Result + 1000;}			
			break;
		}
		default:
		{
			Result = 5000;
			if (CatchUp) {Result = Result + 1000;}			
			break;
		}
	}
	return Result;
}

