/******************************************************************************
*	Web Application: fincl_plngl_pgrms
*	Original programming from MS-Excel
*	Copyright (c) 2007 Professional Representation Inc. All rights reserved.
*
*	Conversion from MS-Excel to JavaScript Web Application performed
*	by Ryan Provost, Inc. (http://www.ryanprovost.com/)
*
*	NOTE:   Please no comments on the code. We are well	aware that it is
*			far from optimized. It does work, though - on all browsers, no less!
*
*******************************************************************************/

var ret_plng_pgrms = function()
{
	var age							= parseInt($('AGE').value);
	var retireAge					= parseInt($('RETIREMENT_AGE').value);
	var periods						= retireAge - age;

	if(periods < 0 ){
		$('ret_plng_pgrms').innerHTML	= "Projected age  must be less then Current age";
		return;
	}
	
	var resources					= parseFloat($('LIQUID_ASSETS').value);
	var inflationRate				= parseFloat($('PERCENT_RATE_INFLATION').value)/100;

	var totalInflow					= FV(inflationRate, periods, parseFloat($('INFLOW').value));
	var inflowTaxRate				= parseFloat($('INFLOW_TAX').value)/100;
	if(DEBUG) alert("totalInflow - "+totalInflow);

	var totalOutflow				= FV(inflationRate, periods, parseFloat($('OUTFLOW').value));
	if(DEBUG) alert("totalOutflow - "+totalOutflow);

	var bondAllocation				= parseFloat($('PERCENT_ALLOCATE_BONDS').value)/100;
	var stockAllocation				= parseFloat($('PERCENT_ALLOCATE_STOCKS').value)/100;

	// Stnd Ret Pgrm Tax Calc
	var investmentDeferred			= parseFloat($('PERCENT_DEFERRED_INVESTMENT').value)/100;
	var investmentNonDeferred		= parseFloat($('PERCENT_NONDEFERRED_INVESTMENT').value)/100;
	var investmentTaxRate			= parseFloat($('PERCENT_INVESTMENT_TAX').value)/100;
	var deferredTaxRate				= investmentDeferred * 0;
	var nonDeferredTaxRate			= investmentNonDeferred * inflowTaxRate;
	var combinedTaxRate				= deferredTaxRate + nonDeferredTaxRate;

	var investmentTaxRate			= $('PERCENT_INVESTMENT_TAX').value = combinedTaxRate;

	var bondsPercentReturn			= parseFloat($('PERCENT_RETURN_BONDS').value)/100;
	var stockPercentReturn			= parseFloat($('PERCENT_RETURN_STOCK').value)/100;
	var bondsAdjReturnPercent		= $('BONDS_ADJ_TAXES').value	= (1-investmentTaxRate) * bondsPercentReturn;
	var stocksAdjReturnPercent		= $('STOCKS_ADJ_TAX').value		= (1-investmentTaxRate) * stockPercentReturn;
	
	var inflowAdjInflation			= (totalInflow * inflationRate) + totalInflow;
	var inflowAdjTaxes				= totalInflow - ( totalInflow * inflowTaxRate);
	if(DEBUG) alert("inflowAdjTaxes - "+inflowAdjTaxes);

	var outflowAdjInflation			= (totalOutflow *  inflationRate) + totalOutflow;
	if(DEBUG) alert("outflowAdjInflation - "+outflowAdjInflation);

	var surplusDeficit				= inflowAdjTaxes - outflowAdjInflation;
	var netResources				= resources + surplusDeficit;
	if(DEBUG) alert("netResources - "+netResources);

	var bondsBySuggAllocation		= netResources * bondAllocation;
	var bondsReturn					= bondsBySuggAllocation * bondsAdjReturnPercent;
	if(DEBUG) alert("bondsReturn - "+bondsReturn);
	
	var stocksBySuggAllocation		= netResources * stockAllocation;
	var stocksReturn				= stocksBySuggAllocation * stocksAdjReturnPercent;
	if(DEBUG) alert("stocksReturn - "+stocksReturn);

	var combinedreturn				= bondsReturn + stocksReturn;
	var combinedRateReturn			= combinedreturn / netResources;

	var resourcesAdjCombinedReturn = (netResources * combinedRateReturn) + netResources;
	if(DEBUG) alert("resourcesAdjCombinedReturn - "+resourcesAdjCombinedReturn);

	var resultsResources			= resources;
	var resultsNetResources			= resultsResources + surplusDeficit;
	var availableResources			= Math.round((resultsNetResources * combinedRateReturn) + resultsNetResources);
	if(DEBUG) alert("availableResources - "+availableResources);

	for(var i=1; periods > i; i++)
	{
		var newretireAge			= retireAge + 1;
		totalInflow					= inflowAdjInflation;
		inflowAdjInflation			= (totalInflow * inflationRate) + totalInflow;
		inflowAdjTaxes				= totalInflow - ( totalInflow * inflowTaxRate);

		totalOutflow				= outflowAdjInflation;
		outflowAdjInflation			= (totalOutflow *  inflationRate) + totalOutflow;

		surplusDeficit				= inflowAdjTaxes - outflowAdjInflation;
		
		resources					= resourcesAdjCombinedReturn;
		netResources				= resources + surplusDeficit;

		bondsBySuggAllocation		= netResources * bondAllocation;
		bondsReturn					= bondsBySuggAllocation * bondsAdjReturnPercent;
	
		stocksBySuggAllocation		= netResources * stockAllocation;
		stocksReturn				= stocksBySuggAllocation * stocksAdjReturnPercent;
		
		combinedreturn				= bondsReturn + stocksReturn;
		combinedRateReturn			= combinedreturn / netResources;
		resourcesAdjCombinedReturn	= (netResources * combinedRateReturn) + netResources;

		resultsResources			= resources;                                                                   
		resultsNetResources			= resultsResources + surplusDeficit;                                           
		var new_availableResources	= Math.round((resultsNetResources * combinedRateReturn) + resultsNetResources) 
		if(DEBUG) alert("newretireAge - "+newretireAge+"\n new_availableResources - "+new_availableResources);

		if(new_availableResources < 0)
		{			
			break;
		}
		else
		{
			retireAge = newretireAge;
			availableResources = new_availableResources
		}
	}
	
	$('age_a').innerHTML = retireAge;
	$('age_b').innerHTML = retireAge;
	$('ret_plng_pgrms').innerHTML = addCommas(availableResources);
}


function ret_plng_pgrms_OLD () {
	// Calculate M21 Value
	if ( IsNumeric(document.getElementById("M19").value) && IsNumeric(document.getElementById("M20").value) )
		document.getElementById("M21").value = roundNumber((1-(document.getElementById("M19").value/100))*(document.getElementById("M20").value/100), 4);

	// Calculate M23 Value
	if ( IsNumeric(document.getElementById("M19").value) && IsNumeric(document.getElementById("M22").value) )
		document.getElementById("M23").value = roundNumber((1-(document.getElementById("M19").value/100))*(document.getElementById("M22").value/100), 4);

	// Set Calculation Array
	var prop_table	= new Array();
	prop_table[0]	= new Array();

	// Set Form Variables
	prop_table[0]['stop_point']	= 0;
	prop_table[0]['age']		= 0;

	prop_table[0]['M4']		= parseFloat ( document.getElementById("M4").value );
	prop_table[0]['M5']		= parseFloat ( document.getElementById("M5").value );
	prop_table[0]['M6']		= parseFloat ( document.getElementById("M6").value );
	prop_table[0]['M13']	= parseFloat ( document.getElementById("M13").value );
	prop_table[0]['M14']	= parseFloat ( document.getElementById("M14").value );
	prop_table[0]['M15']	= parseFloat ( document.getElementById("M15").value );
	prop_table[0]['M17']	= parseFloat ( document.getElementById("M17").value );
	prop_table[0]['M18']	= parseFloat ( document.getElementById("M18").value );
	prop_table[0]['M19']	= parseFloat ( document.getElementById("M19").value );
	prop_table[0]['M20']	= parseFloat ( document.getElementById("M20").value );
	prop_table[0]['M21']	= (1-(document.getElementById("M19").value/100))*(document.getElementById("M20").value/100);
	prop_table[0]['M22']	= parseFloat ( document.getElementById("M22").value );
	prop_table[0]['M23']	= (1-(document.getElementById("M19").value/100))*(document.getElementById("M22").value/100);

	prop_table[0]['periods']= prop_table[0]['M5'] - prop_table[0]['M4'];

	// Set Percentages
	prop_table[0]['M15']	= prop_table[0]['M15'] / 100;
	prop_table[0]['M17']	= prop_table[0]['M17'] / 100;
	prop_table[0]['M18']	= prop_table[0]['M18'] / 100;
	prop_table[0]['M19']	= prop_table[0]['M19'] / 100;
	prop_table[0]['M20']	= prop_table[0]['M20'] / 100;
	prop_table[0]['M22']	= prop_table[0]['M22'] / 100;
	prop_table[0]['inflowtax']	= parseFloat ( document.getElementById("M1_inflowtax").value )/100;


	// 
	//prop_table[0]['M21']	= prop_table[0]['M19'] * prop_table[0]['M20'];
	//prop_table[0]['M23']	= prop_table[0]['M19'] * prop_table[0]['M22'];

	// Set "Sheet 1" Variables
	prop_table[0]['K4']		= prop_table[0]['M4'];
	prop_table[0]['K5']		= prop_table[0]['M5'];
	prop_table[0]['K7']		= prop_table[0]['M6'];
	prop_table[0]['K10']	= prop_table[0]['M15'];
	prop_table[0]['K11']	= prop_table[0]['M15'];
	prop_table[0]['K16']	= prop_table[0]['M13'];
	prop_table[0]['K17']	= FV(prop_table[0]['K10'],prop_table[0]['periods'], prop_table[0]['K16']);
	prop_table[0]['K20']	= prop_table[0]['M14'];
	prop_table[0]['K22']	= FV(prop_table[0]['K10'],prop_table[0]['periods'], prop_table[0]['K20']);
	prop_table[0]['K25']	= 0.045;
	prop_table[0]['K26']	= 0.22;
	prop_table[0]['K27']	= (1-prop_table[0]['K26'])*prop_table[0]['K25'];
	prop_table[0]['K30']	= .10;
	prop_table[0]['K31']	= .22;
	prop_table[0]['K32']	= (1-prop_table[0]['K31'])*prop_table[0]['K30'];

	/***
	prop_table[0]['K39']	= ""; // FUNCTION GOES HERE
	prop_table[0]['K40']	= ""; // FUNCTION GOES HERE
	***/


	// Set "Sheet 2" Variables
	prop_table[0]['B7']		= prop_table[0]['M5']; // age
	prop_table[0]['B11']	= prop_table[0]['M6']; // Resources
	prop_table[0]['B19']	= prop_table[0]['M13'];	// total inflow
	prop_table[0]['B20']	= prop_table[0]['M15'];	// Inflation Rate for Inflow
	
	// Inflow Adjusted for Inflation = total inflow * Inflation Rate for Inflow +total inflow
	prop_table[0]['B21']	= ( prop_table[0]['B19'] * prop_table[0]['B20'] ) + prop_table[0]['B19'];

	//Inflow adjusted for taxes = total inflow - (total inflow*inflow tax)
	prop_table[0]['inflowAdj'] = prop_table[0]['B19'] - (prop_table[0]['B19'] * prop_table[0]['inflowtax']);

	prop_table[0]['B23']	= prop_table[0]['M14']; //Total Outflow 
	prop_table[0]['B24']	= prop_table[0]['M15']; // Inflation Rate for Outflow
	//Outflow Adjusted for Inflation = Total Outflow  *  Inflation Rate for Outflow + Total Outflow
	prop_table[0]['B25']	= ( prop_table[0]['B23'] * prop_table[0]['B24'] ) + prop_table[0]['B23'];

	prop_table[0]['B32']	= prop_table[0]['B25'];
	//prop_table[0]['B34']	= prop_table[0]['B21'] - prop_table[0]['B32'];
	prop_table[0]['B34']	= prop_table[0]['inflowAdj'] - prop_table[0]['B32'];
	prop_table[0]['B12']	= prop_table[0]['B34'];
	prop_table[0]['B13']	= prop_table[0]['B11'] + prop_table[0]['B12'];
	prop_table[0]['B38']	= prop_table[0]['M6'];
	prop_table[0]['B39']	= prop_table[0]['B34'];
	prop_table[0]['B40']	= prop_table[0]['B38'] + prop_table[0]['B39'];
	prop_table[0]['B43']	= prop_table[0]['M17'];
	prop_table[0]['B44']	= prop_table[0]['B40'] * prop_table[0]['B43'];
	prop_table[0]['B45']	= prop_table[0]['M21'];
	prop_table[0]['B46']	= prop_table[0]['B44'] * prop_table[0]['B45'];
	prop_table[0]['B49']	= prop_table[0]['M18'];
	prop_table[0]['B50']	= prop_table[0]['B40'] * prop_table[0]['B49'];
	prop_table[0]['B51']	= prop_table[0]['M23'];
	prop_table[0]['B52']	= prop_table[0]['B50'] * prop_table[0]['B51'];
	prop_table[0]['B54']	= prop_table[0]['B46'] + prop_table[0]['B52'];
	prop_table[0]['B40']	= prop_table[0]['B39'] + prop_table[0]['B38'];
	prop_table[0]['B55']	= prop_table[0]['B54'] / prop_table[0]['B40'];
	prop_table[0]['B15']	= prop_table[0]['B55'];
	prop_table[0]['B16']	= ( prop_table[0]['B13'] * prop_table[0]['B15'] ) + prop_table[0]['B13'];

	prop_table[0]['B57']	= ( prop_table[0]['B40'] * prop_table[0]['B55'] ) + prop_table[0]['B40'];

	if ( prop_table[0]['periods'] == 0 ) {
		document.getElementById("ret_plng_pgrms").innerHTML = "$" + addCommas(roundVal(prop_table[0]['B16']));
	} else if ( prop_table[0]['periods'] < 0 ) {
		document.getElementById("ret_plng_pgrms").innerHTML = "Not enough periods";
	} else {
		for ( i=1; i<prop_table[0]['periods']+1; i++ ) {
			prop_table[i]	= new Array();

			prop_table[i]['B7']		= prop_table[i-1]['B7'] + 1;
			prop_table[i]['B11']	= prop_table[i-1]['B16'];
			prop_table[i]['B19']	= prop_table[i-1]['B21'];
			prop_table[i]['B20']	= prop_table[i-1]['B20'];
			prop_table[i]['B23']	= prop_table[i-1]['B25'];
			prop_table[i]['B24']	= prop_table[i-1]['B24'];
			prop_table[i]['B38']	= prop_table[i-1]['B57'];
			prop_table[i]['B43']	= prop_table[i-1]['B43'];
			prop_table[i]['B49']	= prop_table[i-1]['B49'];
			prop_table[i]['B51']	= prop_table[i-1]['B51'];
			prop_table[i]['B45']	= prop_table[i-1]['B45'];
			prop_table[i]['inflowtax'] = prop_table[i-1]['inflowtax']

			prop_table[i]['B21']	= ( prop_table[i]['B19'] * prop_table[i]['B20'] ) + prop_table[i]['B19'];
			prop_table[i]['B25']	= ( prop_table[i]['B23'] * prop_table[i]['B24'] ) + prop_table[i]['B23'];
			//Inflow adjusted for taxes = total inflow - (total inflow*inflow tax)
			prop_table[i]['inflowAdj'] = prop_table[i]['B19'] - (prop_table[i]['B19'] * prop_table[i]['inflowtax']);

			prop_table[i]['B32']	= prop_table[i]['B25'];
			
			//prop_table[i]['B34']	= prop_table[i]['B21'] - prop_table[i]['B32'];			
			prop_table[i]['B34']	= prop_table[i]['inflowAdj'] - prop_table[i]['B32'];

			prop_table[i]['B12']	= prop_table[i]['B34'];
			prop_table[i]['B13']	= prop_table[i]['B11'] + prop_table[i]['B12'];
			prop_table[i]['B39']	= prop_table[i]['B34'];
			prop_table[i]['B40']	= prop_table[i]['B38'] + prop_table[i]['B39'];
			prop_table[i]['B44']	= prop_table[i]['B40'] * prop_table[i]['B43'];
			prop_table[i]['B46']	= prop_table[i]['B44'] * prop_table[i]['B45'];
			prop_table[i]['B50']	= prop_table[i]['B40'] * prop_table[i]['B49'];
			prop_table[i]['B52']	= prop_table[i]['B50'] * prop_table[i]['B51'];
			prop_table[i]['B54']	= prop_table[i]['B46'] + prop_table[i]['B52'];
			prop_table[i]['B40']	= prop_table[i]['B39'] + prop_table[i]['B38'];
			prop_table[i]['B55']	= prop_table[i]['B54'] / prop_table[i]['B40'];
			prop_table[i]['B15']	= prop_table[i]['B55'];

			// Needed for Propagation
			prop_table[i]['B57']	= ( prop_table[i]['B40'] * prop_table[i]['B55'] ) + prop_table[i]['B40'];

			// Total for Period
			prop_table[i]['B16']	= Math.round((prop_table[i]['B13'] * prop_table[i]['B15']) + prop_table[i]['B13']);
		}

		for ( var i=1; i<prop_table[0]['periods']+1; i++ ) {
			if ( prop_table[i]['B16'] < 0 ) {
				// Output Final Data
				prop_table[0]['age']		= prop_table[0]['M5'] + (i-1);
				prop_table[0]['periods']	= prop_table[0]['age'] - prop_table[0]['M5'];
				
				document.getElementById("age_a").innerHTML = prop_table[0]['age'];
				document.getElementById("age_b").innerHTML = prop_table[0]['age'];

				document.getElementById("ret_plng_pgrms").innerHTML = "$" + addCommas(roundVal(prop_table[prop_table[0]['periods']]['B16']));

				break;
			}
		}
	}

	// Set Summary Paragraph Items
	document.getElementById("results_bonds").innerHTML			= document.getElementById("M17").value;
	document.getElementById("results_stocks").innerHTML			= document.getElementById("M18").value;
	document.getElementById("results_tax_bracket").innerHTML	= document.getElementById("M19").value;
	document.getElementById("results_bond_return").innerHTML	= document.getElementById("M20").value;
	document.getElementById("results_stock_return").innerHTML	= document.getElementById("M22").value;
}
