/* FUNCTION PSEUDO CLASS ----------- */
function marginalTaxRateAttributes( lid, aI, oI, cG )
	{
	this.databaseID = lid;
	this.annualIncome = aI;
	this.otherIncome = oI;
	this.capitalGains = cG;
	this.taxRate = oI;
	}
/* ----------- */


/* FUNCTION PSEUDO CLASS ----------- */
function moneyFilters( i, n, p )
	{
	this.id = i;
	this.name = n;
	this.percentage = p;
	}
/* ----------- */




/* FUNCTION PSEUDO CLASS ----------- */
function lookUpAttributes( contentDiv, mtra, aI, dip, hg, pir, pira, ema )
	{	
	//
	this.setRates = function( isSecured )
		{
		if( isSecured )
			this.adjustedAnnalInterestRate = this.primeInterestRate;
				else
					this.adjustedAnnalInterestRate = this.primeInterestRate + this.primeInterestRateAdjustment;
				
		for( var i = 0; i < this.MTRarr.length; i++ )
			{
			if( (i + 1) < this.MTRarr.length )
				{
				if( ( this.actualIncome >= this.MTRarr[ i ].annualIncome ) && this.actualIncome < this.MTRarr[ i+1 ].annualIncome )
					{
					this.marginalTaxRate = this.MTRarr[ i ].taxRate;
					this.capitalGainsTaxRate = this.MTRarr[ i ].capitalGains;
					break;
					}

				}
				else
					{
					this.marginalTaxRate = this.MTRarr[ i ].taxRate;
					this.capitalGainsTaxRate = this.MTRarr[ i ].capitalGains;
					}


			}

		};
	//

	//
	this.setFilters = function( fa )
		{
		var tempArr = new Array()

		for( var i = 0; i < fa.length; i++ )
			tempArr[i] = fa[i];

		this.filtersArr = tempArr;
		};
	//
	
	
	//
	this.updateRates = function( id, attribute, aValue )
		{
		for( var i = 0; i < this.MTRarr.length; i++ )
			{
			if( this.MTRarr[i].databaseID == id )
				{
				switch( attribute )
					{
					case "annualIncome" :
						if( isNaN( aValue ) )
							{
							alert( "Annual Income must be a number." );
							return false;
							}
						this.MTRarr[i].annualIncome = eval(aValue);
						break;
		
					case "otherIncome" :
						if( isNaN( aValue ) )
							{
							alert( "Annual Income must be a number." );
							return false;
							}
						this.MTRarr[i].otherIncome = eval(aValue);
						break;
		
					case "capitalGains" :
						if( isNaN( aValue ) )
							{
							alert( "Capital Gains must be a number." );
							return false;
							}
						this.MTRarr[i].capitalGains = eval(aValue);
						break;
						
						
					default:
						return false;
					}
				}	
			}
		return true;
		}	
	//

	this.MTRarr = mtra;

	this.actualIncome = aI;	
	this.marginalTaxRate;
	this.defaultInvestmentPrincipal = dip;
	this.housingGrowthRate = hg; // this is an annual interest rate

	this.primeInterestRateAdjustment = pira;
	this.primeInterestRate = pir;
	this.adjustedAnnalInterestRate = 0;
	this.capitalGainsTaxRate;
	this.redirectPaymentsToInvestments = true;
	this.contentObj = contentDiv;
	this.filtersArr;
	this.enableMortgageAccelerator = ema;
	}
/* ----------- */



/* FUNCTION PSEUDO CLASS ----------- */
function lookUps( contentDiv, mtra, aI, dip, hg, pir, pira, isi, mfa, ema )
	{
	//
	this.addRate = function()
		{
		this.lastRateID += 1;
		var marginalTaxRateAttr = new marginalTaxRateAttributes( this.lastRateID , 0, 0, 0 );
		this.attributes.MTRarr.push( marginalTaxRateAttr );		
		}
	//
	
	//
	this.deleteRate = function( rid )
		{
		var tempRate = new Array();
		var num = 0;
		
		for( var i = 0; i < this.attributes.MTRarr.length; i++ )
			{
			if( this.attributes.MTRarr[i].databaseID != rid )
				{
				tempRate[ num ] = this.attributes.MTRarr[i];
				num++;
				}
			}
		this.attributes.MTRarr = tempRate;	
		global_lMarginalTRA = tempRate;
		this.attributes.setRates( this.isSecuredInvestment );	
		
		};
	//	

	//
	this.updateLookup = function( id, attribute, aValue )
		{
		aValue = eval( aValue );

		switch( attribute )
			{
			case "defaultInvestmentPrincipal" :
				this.attributes.defaultInvestmentPrincipal = aValue;			
				break;
			case "housingGrowthRate" :
				this.attributes.housingGrowthRate = aValue;
				break;

			case "primeInterestRateAdjustment" :
				this.attributes.primeInterestRateAdjustment = aValue;
				break;

			case "primeInterestRate" :
				this.attributes.primeInterestRate = aValue;
				break;

			case "newAnnualIncome" :
				this.attributes.actualIncome = aValue;
				this.attributes.setRates( this.isSecuredInvestment );	
				break;

			case "enableMortgageAccelerator" :
				this.attributes.enableMortgageAccelerator = setBool( aValue );
				break;

			case "filter" :
				this.attributes.filtersArr[eval(id)-1] = aValue;
				break;
				
			default:
				if( !this.attributes.updateRates( id, attribute, aValue ) )
					return false;

			}
		this.attributes.setRates( this.isSecuredInvestment );	
		return true;	
		};
	//	

	//
	this.setSecuredInvestment = function( si )	
		{
		this.isSecuredInvestment = si;
		this.attributes.setRates( this.isSecuredInvestment );	
		}
	
	this.isSecuredInvestment = isi;
	this.attributes = new lookUpAttributes( contentDiv, mtra, aI, dip, hg, pir, pira, ema );
	this.attributes.setRates( isi );	
	this.attributes.setFilters( mfa );	

	this.lastRateID = 0;
	for( var j = 0; j < this.attributes.MTRarr.length; j++ )
		{
		if( this.attributes.MTRarr[ j ].databaseID > this.lastRateID )
			this.lastRateID = this.attributes.MTRarr[ j ].databaseID;
		}	


	}
/* ----------- */


/* ----------- */
function setLookupView( view, tid )
	{
	global_LookupView = view;
	getLookupView();
	}	
/* ----------- */



/* ----------- */
function getLookupView()
	{
	var top = "20px";
	document.getElementById("lookup_rates_tab").className = "lookup_taboutline_off";
	document.getElementById("lookup_taxes_tab").className = "lookup_taboutline_off";
	document.getElementById("lookup_filter_tab").className = "lookup_taboutline_off";


	document.getElementById("lookup_rates_content").style.position = "relative";
	document.getElementById("lookup_taxes_content").style.position = "relative";
	document.getElementById("lookup_filter_content").style.position = "relative";
	document.getElementById("lookup_rates_content").style.top = top;
	document.getElementById("lookup_taxes_content").style.top = top;
	document.getElementById("lookup_filter_content").style.top = top;
	document.getElementById("lookup_rates_content").style.visibility="hidden";
	document.getElementById("lookup_taxes_content").style.visibility="hidden"
	document.getElementById("lookup_filter_content").style.visibility="hidden"

	document.getElementById("lookup_rates_content").style.display="none";
	document.getElementById("lookup_taxes_content").style.display="none"
	document.getElementById("lookup_filter_content").style.display="none"


	document.getElementById("lookup_rates_tab")

	switch( global_LookupView )
		{
		case "lookup_rates" :
			document.getElementById("lookup_rates_content").style.display="block";
			document.getElementById("lookup_rates_content").style.visibility="visible";
			document.getElementById("lookup_rates_tab").className = "lookup_taboutline_on";
			break;

		case "lookup_taxes" :
			document.getElementById("lookup_taxes_content").style.display="block";
			document.getElementById("lookup_taxes_content").style.visibility="visible";
			document.getElementById("lookup_taxes_tab").className = "lookup_taboutline_on";
			break;

		case "lookup_filters" :
			document.getElementById("lookup_filter_content").style.display="block";
			document.getElementById("lookup_filter_content").style.visibility="visible";
			document.getElementById("lookup_filter_tab").className = "lookup_taboutline_on";
			break;

		default :
			document.getElementById("lookup_rates_content").style.visibility="visible"
			document.getElementById("lookup_rates_content").style.display="block";
			document.getElementById("lookup_rates_tab").className = "lookup_taboutline_on";

			break;
		}	
	}
/* ----------- */
	



/* ----------- */
function loadLookups( f, l )
	{
	var contentStr = "";   

	contentStr += "<table border=\"0\" bgcolor=\"white\" width=\"750\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">"
	contentStr += "<tr>"
	contentStr += 	"<td height=\"20\" class=\"lookup_tabend\">&nbsp;</td>"
	contentStr += 	"<td class=\"lookup_taboutline_on\" id=\"lookup_rates_tab\"><a class=\"tablink\" href=\"javascript:setLookupView('lookup_rates', 'lookup_rates_tab');\">Rates</a></td>"
	contentStr += 	"<td class=\"tabspacer\">&nbsp;</td>"
	contentStr += 	"<td class=\"lookup_taboutline_off\" id=\"lookup_taxes_tab\"><a class=\"tablink\" href=\"javascript:setLookupView('lookup_taxes', 'lookup_taxes_tab');\">Taxes</a></td>"
	contentStr += 	"<td class=\"tabspacer\">&nbsp;</td>"
	contentStr += 	"<td class=\"lookup_taboutline_off\" id=\"lookup_filter_tab\"><a class=\"tablink\" href=\"javascript:setLookupView('lookup_filters', 'lookup_filter_tab');\">Money Filters</a></td>"
	contentStr += 	"<td class=\"lookup_tabend\">&nbsp;</td>"
	contentStr += "</tr>"
		
	contentStr += "<tr>"
	contentStr += 	"<td  class=\"tabbody\" colspan=\"9\" height=\"300\">"

	contentStr += "<div>"

	contentStr += "<div id=\'lookup_taxes_content\'>"
		contentStr += "<table border=\"0\" width=\"200\" align=\"center\"><tr>";
		contentStr += "<td class=\"scenarioDataLabels\" width=\"60\" valign=\"bottom\">Annual Income</td>";
		contentStr += "<td class=\"scenarioDataLabels\" width=\"60\" valign=\"bottom\">Other Income</td>";
		contentStr += "<td class=\"scenarioDataLabels\" width=\"60\" valign=\"bottom\">Capital Gains</td>";
		contentStr += "</tr><tr>";
		for( var i = 0; i < l.attributes.MTRarr.length; i++ )
			{ 
			contentStr += "<tr>";
			contentStr += "<td align=\"right\">" + getInputField("10", "mtr_1_" + i, l.attributes.MTRarr[i].annualIncome.toFixed(2), l.attributes.MTRarr[i].databaseID, "annualIncome", "false", "input", "Lookup" ) + "</td>";
	
			contentStr += "<td align=\"right\">" + getInputField("10", "mtr_2_" + i, l.attributes.MTRarr[i].otherIncome.toFixed(2), l.attributes.MTRarr[i].databaseID, "otherIncome", "false", "input", "Lookup" ) + "</td>";
			contentStr += "<td align=\"right\">" + getInputField("10", "mtr_3_" + i, l.attributes.MTRarr[i].capitalGains.toFixed(2), l.attributes.MTRarr[i].databaseID, "capitalGains", "false", "input", "Lookup" ) + "</td>";
			contentStr += "<td align=\"right\"><a class=\"delete\" href=\"#\" onClick=\"action_Lookup_deleteRate(this.form, " + l.attributes.MTRarr[i].databaseID + ")\">delete</a></td>";
			contentStr += "</tr>";
			}
	
		contentStr += "<tr>";
		contentStr += "<td colspan=\"8\" align=\"right\"><a class=\"addNew\" href=\"#\" onclick=\"action_Lookup_addRate( this.form, getNowDateStr() )\">Add New Rate</a></td>";
		contentStr += "</tr>";
		contentStr += "</table>";
	contentStr += "</div>";

	contentStr += "<div id=\'lookup_rates_content\'>"
		contentStr += "<table border=\"0\" width=\"300\" align=\"center\">";
		contentStr += "<tr>";
		contentStr += "<td class=\"scenarioDataLabels\" width=\"250\">Rate Type</td>";
		contentStr += "<td class=\"scenarioDataLabels\">Rates</td>";
		contentStr += "</tr><tr>";
		contentStr += "<td class=\"formLabel\" width=\"250\" valign=\"bottom\">Marginal Tax Rate</td>";
		contentStr += "<td align=\"right\">" + getInputField("10", "mtrate", l.attributes.marginalTaxRate.toFixed(2), 0, "marginalTaxRate", "true", "input", "Lookup" ) + "</td>";
		contentStr += "</tr><tr>";


		contentStr += "<td class=\"formLabel\" width=\"250\" valign=\"bottom\">Default Investment Principal</td>";
		contentStr += "<td align=\"right\">" + getInputField("10", "dip", l.attributes.defaultInvestmentPrincipal.toFixed(2), 0, "defaultInvestmentPrincipal", "false", "input", "Lookup" ) + "</td>";
		contentStr += "</tr><tr>";
		contentStr += "<td class=\"formLabel\" width=\"250\" valign=\"bottom\">Housing Growth Rate</td>";
		contentStr += "<td align=\"right\">" + getInputField("10", "hg", l.attributes.housingGrowthRate.toFixed(2), 0, "housingGrowthRate", "false", "input", "Lookup" ) + "</td>";
		contentStr += "</tr><tr>";
		contentStr += "<td class=\"formLabel\" width=\"250\" valign=\"bottom\">Annual Interest Rate</td>";
		contentStr += "<td align=\"right\">" + getInputField("10", "air", l.attributes.adjustedAnnalInterestRate.toFixed(2), 0, "adjustedAnnalInterestRate", "true", "input", "Lookup" ) + "</td>";
		contentStr += "</tr><tr>";
		contentStr += "<td class=\"formLabel\" width=\"250\" valign=\"bottom\">Capital Gains Tax Rate</td>";
		contentStr += "<td align=\"right\">" + getInputField("10", "cgtr", l.attributes.capitalGainsTaxRate.toFixed(2), 0, "capitalGainsTaxRate", "true", "input", "Lookup" ) + "</td>";
		contentStr += "</tr><tr>";
		contentStr += "<td class=\"formLabel\" width=\"250\" valign=\"bottom\">Prime Interest Rate Adjustment</td>";
		contentStr += "<td align=\"right\">" + getInputField("10", "pira", l.attributes.primeInterestRateAdjustment.toFixed(2), 0, "primeInterestRateAdjustment", "false", "input", "Lookup" ) + "</td>";
		contentStr += "</tr></table>";

	contentStr += "</div>";
	contentStr += "<div id=\'lookup_filter_content\'>"
	
		contentStr += "<table border=\"0\" width=\"400\" align=\"center\">";
		contentStr += "<tr>";
		contentStr += "<td class=\"scenarioDataLabels\" align=\"center\" valign=\"bottom\"><b>Money Allocation Filters</b></td>";
		contentStr += "<td class=\"scenarioDataLabels\">Percentage</td></tr>";
 


		contentStr += "<tr><td class=\"formLabel\" width=\"350\" valign=\"bottom\">Enable Mortgage Accelerator</td>";
		contentStr += "<td align=\"right\">";
	
		var selectStr = "";
		var selectedTrue = "";
		var selectedFalse = "";
		var jsAction = "onChange=\"action_Lookup_update( this.form, this.id, 0, \'enableMortgageAccelerator\', this.value )\"";
		selectStr += "<select "+jsAction+" id=\"enableMortgageAccelerator\" name=\"enableMortgageAccelerator\">";

		if( l.attributes.enableMortgageAccelerator )
			selectedTrue = " SELECTED";
				else
					selectedFalse = " SELECTED";
					
		selectStr += "<option value=\"true\""+selectedTrue+">On</option>";
		selectStr += "<option value=\"false\""+selectedFalse+">Off</option>";
		selectStr += "</select>";
	
		contentStr += selectStr;
		contentStr += "</td>";
		contentStr += "</tr>";


		contentStr += "<tr><td class=\"helpnotes\" align=\"left\" height=\"40\" colspan=\"2\">";
		contentStr += "Input the percentage	in one or all the boxes below, the total for all fields below cannot exceed 100%, to redirect your payments"
		contentStr += "</td></tr>";

		for( var j = 0; j < l.attributes.filtersArr.length; j++ )
			{
			contentStr += "<tr>";
			contentStr += "<td class=\"formLabel\" width=\"350\" valign=\"bottom\">" +l.attributes.filtersArr[j].name + "</td>";
			contentStr += "<td align=\"right\">" + getInputField("2", ("filter_" + j), l.attributes.filtersArr[j].percentage, l.attributes.filtersArr[j].id, "filter", "false", "input", "Lookup" ) + "</td>";
			contentStr += "</tr>";
			}
		contentStr += "</table>";

	contentStr += "</div>";
	contentStr += "</div>";
	contentStr += "</td></tr></table>"

	document.getElementById( l.attributes.contentObj ).innerHTML = contentStr;


	getLookupView();


	}
/* ----------- */

