// Calculator Social Securuty

//for documentation of the calculations, see the spreadsheet "SocSec.xls".

//constructor
function SocialSecurity(setDefaults)
{
	//inputs
	this.type = -1;
	if (setDefaults == 0) {

  		this.bMarried = true;
  
 		this.iYourCurrentAge = "";
		this.iSpCurrentAge = "";

		this.dYourAnnualSalary = "";
		this.dSpAnnualSalary = "";

		this.iYourRetirementAge = "";
		this.iSpRetirementAge = "";

		this.iYourBenefitAge = "";
		this.iSpBenefitAge = "";

	} 
	else 
	{
		this.bMarried = true;

 		this.iYourCurrentAge = "44";
		this.iSpCurrentAge = "38";

		this.dYourAnnualSalary = "59000";
		this.dSpAnnualSalary = "67000";

		this.iYourRetirementAge = "60";
		this.iSpRetirementAge = "55";

		this.iYourBenefitAge = "65";
		this.iSpBenefitAge = "62";
	}
	
	this.dInflation = 3;

	//intermediate calcs
	this.iMarBenAge = 0;
	this.iSpMarBenAge = 0;	

	//outputs
	this.dRetBen = "";
	this.dRetMarBen = "";
	this.dRetSurvBen = "";

	this.dSpRetBen = "";
	this.dSpRetMarBen = "";
	this.dSpRetSurvBen = "";

	this.dDisBen = "";

	this.dChild = "";
	this.dSpouseNoWork = "";
	this.dSpouseWork = "";
	this.dFamilyMax = "";

	this.dSpDisBen = "";

	this.dSpChild = "";
	this.dSpSpouseNoWork = "";
	this.dSpSpouseWork = "";
	this.dSpFamilyMax = "";
	
	//methods
 	this.mDoCalc = DoCalc;
 	this.mGetYear = getYear;
 	this.mGetSpYear = getSpYear;
	this.mYourMarBenAge = YourMarBenAge;
	this.mSpouseMarBenAge = SpouseMarBenAge;
	this.mAnnualTD = AnnualTD;	
	this.mSetMarAge = SetMarAge;
 	
	return true;
}

function getYear(userAge)
{
	return parseInt(GetBirthYear(this.iYourCurrentAge)) + parseInt(userAge);
}

function getSpYear(spAge)
{
	return parseInt(GetBirthYear(this.iSpCurrentAge)) + parseInt(spAge);
}

function SetMarAge()
{
	if (this.bMarried)
	{
		this.iMarBenAge = Math.max(this.iYourBenefitAge, 
						  parseInt(this.iSpBenefitAge) 
						+ parseInt(this.iYourCurrentAge)
						- parseInt(this.iSpCurrentAge));
		this.iSpMarBenAge = Math.max(this.iSpBenefitAge, 
						  parseInt(this.iYourBenefitAge) 
						+ parseInt(this.iSpCurrentAge)
						- parseInt(this.iYourCurrentAge));
	} 
	else
	{
		this.iMarBenAge = 0;
		this.iSpMarBenAge = 0;
	}
	return true;		
}

function AnnualTD(amount, year, socSec)
{
	amount = 12*socSec.TodayDollars(amount, year); 
	return fmtNum(amount,",","$");
}

function DoCalc()
{
	this.mSetMarAge();

	var spSocSec;
	var PIAYear;
	var benefitYear;
	var TodayPIA;
	var socSec = new createSocSec(this.dYourAnnualSalary,
                      				this.mGetYear(0), //birthyear
                      				this.mGetYear(this.iYourRetirementAge-1), //ProjectedLastWorkYear,  
                      				this.dInflation/100,                 
                      				this.dInflation/100);
	
	benefitYear = this.mGetYear(this.iYourBenefitAge);
	this.dRetBen = socSec.RetireBenefit(benefitYear);
	this.dRetBen = this.mAnnualTD(this.dRetBen,benefitYear,socSec);

	TodayPIA = socSec.PIA(tDisability,cCurrentYear);
	this.dFamilyMax = socSec.FamilyMax(TodayPIA,cCurrentYear);
	this.dChild = 0.75*TodayPIA;

	this.dDisBen = this.mAnnualTD(TodayPIA,cCurrentYear,socSec);
	this.dChild = this.mAnnualTD(this.dChild,cCurrentYear,socSec);
	this.dFamilyMax = this.mAnnualTD(this.dFamilyMax,cCurrentYear,socSec);

	if (this.bMarried)
	{
		var spTodayPIA;
		spSocSec = new createSocSec(this.dSpAnnualSalary,
									this.mGetSpYear(0), //birthyear
									this.mGetSpYear(this.iSpRetirementAge-1), //ProjectedLastWorkYear,  
									this.dInflation/100,                 
									this.dInflation/100);
		
		benefitYear = this.mGetSpYear(this.iSpBenefitAge);
		this.dSpRetBen = spSocSec.RetireBenefit(benefitYear);
		this.dSpRetBen = this.mAnnualTD(this.dSpRetBen, benefitYear, spSocSec);

		benefitYear = this.mGetYear(this.iMarBenAge);
		
		PIAYear = this.mGetSpYear(62);
		
		this.dRetMarBen = socSec.MarriageBenefit(spSocSec.PIA(tRetire, PIAYear), 
													PIAYear, 
													benefitYear);
													
		this.dRetMarBen = this.mAnnualTD(this.dRetMarBen,benefitYear,socSec);
				
		benefitYear = this.mGetYear(Math.max(this.iMarBenAge,88));
		
		this.dRetSurvBen = socSec.SurvivorBenefit(spSocSec.PIA(tRetire,PIAYear),
													PIAYear,
                                                    spSocSec.mAdjustedPIA,
                                                    benefitYear);
                                                            
		this.dRetSurvBen = this.mAnnualTD(this.dRetSurvBen,benefitYear,socSec);

		benefitYear = this.mGetSpYear(this.iSpMarBenAge, 88);
		PIAYear = this.mGetYear(62);
		this.dSpRetMarBen = spSocSec.MarriageBenefit(socSec.PIA(tRetire,PIAYear), 
														PIAYear, 
														benefitYear);
		this.dSpRetMarBen = this.mAnnualTD(this.dSpRetMarBen, 
											benefitYear, 
											socSec);
				
		benefitYear = this.mGetSpYear(Math.max(this.iSpMarBenAge, 70));
		this.dSpRetSurvBen = spSocSec.SurvivorBenefit(socSec.PIA(tRetire,PIAYear),
														PIAYear,
														socSec.mAdjustedPIA, 
														benefitYear);
														
		this.dSpRetSurvBen = this.mAnnualTD(this.dSpRetSurvBen,benefitYear,socSec);

		spTodayPIA = spSocSec.PIA(tDisability, cCurrentYear);
		this.dSpFamilyMax = spSocSec.FamilyMax(spTodayPIA, cCurrentYear);
		this.dSpChild = 0.75 * spTodayPIA;
		this.dSpDisBen = this.mAnnualTD(spTodayPIA, cCurrentYear, socSec);
		this.dSpChild = this.mAnnualTD(this.dSpChild, cCurrentYear, socSec);
		this.dSpFamilyMax = this.mAnnualTD(this.dSpFamilyMax, cCurrentYear, socSec);

		this.dSpouseNoWork = this.dChild;
		this.dSpouseWork = socSec.WorkingSpouseBenefit(TodayPIA, this.dSpAnnualSalary);
		this.dSpouseWork = this.mAnnualTD(this.dSpouseWork, cCurrentYear, socSec);

		this.dSpSpouseNoWork = this.dSpChild;
		this.dSpSpouseWork = spSocSec.WorkingSpouseBenefit(spTodayPIA, this.dYourAnnualSalary);
		this.dSpSpouseWork = this.mAnnualTD(this.dSpSpouseWork, cCurrentYear, socSec);

	} 
	else
	{
		this.dRetMarBen = "N/A";
		this.dRetSurvBen = "N/A";
		this.dSpouseNoWork = "N/A";
		this.dSpouseWork = "N/A";
	}

	return true;
}

function YourMarBenAge()
{
	if (this.bMarried)
	{
		return  "Beginning at Age " + this.iMarBenAge;
	} 
	else 
	{
		return  "";
	}
}

function SpouseMarBenAge()
{
	if (this.bMarried)
	{
		return  "Beginning at Age " + this.iSpMarBenAge;
	} 
	else 
	{
		return  "";
	}
}
