/*
 * Client:		PSCA
 *
 * Language:	JavaScript
 *

 *
 * Description:	401kday.org Calculator JavaScript
 * 

 *

 */

function calculateRetirement(form)
 {
	if (form.CurrentAge.value == "")
	 {
		alert ("Please enter your age.");
		form.CurrentAge.focus();
		return false;
	 }
	else if (form.CurrentAge.value < 18 || form.CurrentAge.value > 66)
	 {
		alert ("Please enter an age between 18 and 66.");
		form.CurrentAge.value = "";
		form.CurrentAge.focus();
		return false;
	 }
	
	else if (form.StartIncome.value == "")
	 {
		alert ("Please enter your annual income.");
		form.StartIncome.focus();
		return false;
	 }
	else if (form.StartIncome.value < 0)
	 {
		alert ("Please enter a positive value for your income.");
		form.StartIncome.focus();
		return false;
	 }


	else if (form.CurrentSavings.value == "")
	 {
		alert ("Please enter your current retirement balance.");
		form.CurrentSavings.focus();
		return false;
	 }
	else if (form.CurrentSavings.value < 0)
	 {
		alert ("Please enter a positive value for your current retirement balance.");
		form.CurrentSavings.focus();
		return false;
	 }


	else if (form.EmployeeContrib.value == "")
	 {
		alert ("Please enter your annual contribution.");
		form.EmployeeContrib.focus();
		return false;
	 }
	else if (form.EmployeeContrib.value < 0)
	 {
		alert ("Please enter a positive value for your annual contribution.");
		form.EmployeeContrib.focus();
		return false;
	 }


	else if (form.CompanyContrib.value == "")
	 {
		alert ("Please enter your company's annual contribution.");
		form.CompanyContrib.focus();
		return false;
	 }
	else if (form.CompanyContrib.value < 0)
	 {
		alert ("Please enter a positive value for your company's annual contribution.");
		form.CompanyContrib.focus();
		return false;
	 }

	else
	 {
		YearsToRetirement(form);
		CalcFinalPay(form);
		CalcSavingsGoal();
		CalcFutureValueCurrentSavings(form);
		CalcFutureValueAnnualSavings(form);
		CalcTotalSavings(form);
		CalcSurplusOrDeficit(form);
	
		form.SavingsGoal.value = goal;
		form.TotalAssets.value = Math.round(TotalSavings);
		form.Difference.value = Math.round(SurplusDeficit);

		return true;
	 }
 }

/* -------------------------------------------------------------- */
/* ## DON'T GO ANY FURTHER UNLESS YOU KNOW WHAT YOU ARE DOING! ## */
/* ##     NOTHING BELOW THIS LINE NEEDS TO BE CHANGED!!        ## */
/* -------------------------------------------------------------- */

// Set rate of return: constant of 8% per year
	var RR = .08;

// function to create definition windows
	function newWindow(definitionpage)
	 {
		definitionWindow= window.open(definitionpage,
		'Definitions', 'width=757,height=300, scrollbars=yes, scrollbars, resizable=yes');
		definitionWindow.focus();
	 }

// Calculate number of years to retirement (retirement is always at age 67)
	function YearsToRetirement(theForm)
	 {
		YtoR = 67 - theForm.CurrentAge.value;
	 }
	
// Calculate final salary at retirement. Salary increases each year by 3%	
	function CalcFinalPay(theForm)
	 {
		finalpay = theForm.StartIncome.value * Math.pow(1.03, YtoR);
		finalpay = Math.round(finalpay);
	 }


// Calculate how much savings is needed when retirement occurs at age 67
// Savings goal is ten times the salary the person earns when they are age 67
	function CalcSavingsGoal()
	 {
		goal = finalpay * 10;
	 }


// Calculate what the current savings will be worth when the person retires at age 67
// Current savings earns 8% return each year until retirement at age 67
	function CalcFutureValueCurrentSavings(theForm)
	 {
		CurrentSavingsRetire = theForm.CurrentSavings.value;
		
		for (YearsLeft = 67 - theForm.CurrentAge.value; YearsLeft >0; YearsLeft = YearsLeft - 1)
		 {
			CurrentSavingsRetire = (CurrentSavingsRetire * 1) + (CurrentSavingsRetire * RR);
		 }
	 }
		

// Calculate what the yearly contributions will be worth when the person retires at age 67
// Yearly contributions increase each year by 3%
// These contributions earn 8% return each year		
	function CalcFutureValueAnnualSavings(theForm)
	 {
		StartingYearlyContrib = theForm.EmployeeContrib.value*1 + theForm.CompanyContrib.value*1;
		AnnualContrib = StartingYearlyContrib;
		ContribRetire = 0
		
		for (YearsLeft = 67 - theForm.CurrentAge.value; YearsLeft > 0; YearsLeft = YearsLeft-1)
		 {
			ContribRetire = (ContribRetire+AnnualContrib) * 1.08;
			AnnualContrib = AnnualContrib * 1.03;
		 }
	}

// Calculate total amount saved at retirement age age 67
	function CalcTotalSavings(theForm)
	 {
		TotalSavings = CurrentSavingsRetire + ContribRetire;
	 }

// Determine the difference between the total amount saved at retirement, and
// the savings goal for retirement
	function CalcSurplusOrDeficit(theForm)
	 {
		SurplusDeficit = TotalSavings-goal;
	 }

// EOF
