/* FUNCTION PSEUDO CLASS ----------- */
function fundAttributes( dbid, f, aa, mdf, nav, nDate ) 
	{
	//
	this.setUnitsPurchased = function( investmentLoanPrincipal )
		{
		if( isNaN(this.NetAssetValue) || this.NetAssetValue == 0 )
			this.unitsPurchased = 0;
				else
					if( isNaN(investmentLoanPrincipal) || investmentLoanPrincipal == 0 )
						this.unitsPurchased = 0;
							else
								if( isNaN(this.assestAllocation) || this.assestAllocation == 0 )
									this.unitsPurchased = 0;
										else
											this.unitsPurchased = investmentLoanPrincipal * ( (this.assestAllocation*.01) / this.NetAssetValue );
		};	
	//
	
	//	
	this.setMonthlyDistribution = function()
		{
		if( this.unitsPurchased == 0 )
			{
			this.monthlyDistribution = 0;
			return;
			}
			
		if( isNaN( this.monthlyDistributionFactor ) && isNaN( this.unitsPurchased ) )
			this.monthlyDistribution = 0;
				else
					this.monthlyDistribution = (this.unitsPurchased * this.monthlyDistributionFactor);
		};
	//

	//
	this.setAnnualDistribution = function()
		{
		if( this.unitsPurchased == 0 )
			{
			this.annualDistribution = 0;
			return;
			}

		if( isNaN( this.monthlyDistributionFactor ) )
			this.annualDistribution = 0;
				else
					this.annualDistribution = ( this.monthlyDistribution * 12 );
		};
	//
	
	
	
			
	this.dataBaseID = dbid;
	this.fundName = f;

	this.unitsPurchased = 0;
	this.assestAllocation = aa;
  	this.monthlyDistributionFactor = mdf;
	this.monthlyDistribution = 0;
  	this.NetAssetValue = nav;
	// remove NAVdate
	this.NAVdate = nDate; // changed date
	this.annualDistribution = 0;

	}
/* ----------- */


/*
portfolioFund - this is the actual fund
this object contains methods for working with the fund.
*/

/* FUNCTION PSEUDO CLASS ----------- */
function portfolioFund( dbid, f, aa, mdf, nav, nDate, iLoanPrincipal ) 
	{
	//
	this.updateFund = function( attribute, aValue )
		{
		
		switch( attribute )
			{
			case "fundName" :
				if( aValue == "" )
					{
					alert( "Fund Name cannot be blank" );
					return;
					}
				this.fund.fundName = aValue;
				break;

			case "assestAllocation" :
				if( isNaN( aValue ) )
					{
					alert( "Assest Allocation must be a number" );
					return;
					}
				this.fund.assestAllocation = eval(aValue);
				break;

			case "monthlyDistributionFactor" :
				if( isNaN( aValue ) )
					{
					alert( "Monthly Distribution Factor must be a number" );
					return;
					}
				this.fund.monthlyDistributionFactor = eval(aValue);
				break;
				
			case "NetAssetValue" :
				if( isNaN( aValue ) )
					{
					alert( "Net Asset Value - NAV must be a number" );
					return;
					}
				this.fund.NetAssetValue = eval(aValue);
				break;
				
			default:
				return;
			}

		this.fund.setUnitsPurchased( this.loanPrincipal );
		this.fund.setMonthlyDistribution();
		this.fund.setAnnualDistribution();
		};
	//
	
	//
	this.setInvestmentLoanPrincipal = function( lPrincipal )
		{
		this.loanPrincipal = lPrincipal;
		this.fund.setUnitsPurchased( this.loanPrincipal );
		this.fund.setMonthlyDistribution();
		this.fund.setAnnualDistribution();
		};
	//
	
	
	
	this.dbID = dbid;
	this.loanPrincipal = iLoanPrincipal;
	this.fund = new fundAttributes( dbid, f, aa, mdf, nav, nDate );
	this.fund.setUnitsPurchased( iLoanPrincipal );
	this.fund.setMonthlyDistribution();
	this.fund.setAnnualDistribution();
	}
/* ----------- */









/*
portfolio - is the portfolio and contains all the funds
this object also calculates the portfolio totals of various fund(s) values.

var portfolioArr = new Array();
	portfolioArr[0] = new portfolio( 0, "Clarington Canadian Dividend", 100, .08, 7.67, "May 07, 2007", global_investmentLoanPrincipal );
*/

/* FUNCTION PSEUDO CLASS ----------- */
function portfolio( contentDiv, pfArr, iLoanPrincipal )
	{
	//
	this.setInvestmentLoanPrincipal = function ( lPrincipal )
		{
		this.loanPrincipal = lPrincipal
		for( var j = 0; j < this.pFundArr.length; j++ )
			{
			this.pFundArr[ j ].setInvestmentLoanPrincipal( lPrincipal );
			}	
		}
	//
	
	
	//
	this.getAssestAllocationTotal = function ()
		{
		var sum = 0;
		for( var i = 0; i < this.pFundArr.length; i++ )
			{
			sum += this.pFundArr[i].fund.assestAllocation;
			}
		return sum.toFixed(1);
		};
	//	

	//
	this.getMonthlyDistributionTotal = function()
		{
		var sum = 0;
		for( var i = 0; i < this.pFundArr.length; i++ )
			{
			sum += this.pFundArr[i].fund.monthlyDistribution;
			}
		return sum.toFixed(2);
		};
	//
	
	//	
	this.getAnnualDistributionTotal = function()
		{
		var sum = 0;
		for( var i = 0; i < this.pFundArr.length; i++ )
			{
			sum += this.pFundArr[i].fund.annualDistribution;
			}
		return sum.toFixed(2);
		};
	//
		
	//	
	this.getMonthlyDistributionCompoundFactor = function()
		{
		return ((this.getMonthlyDistributionTotal() / this.loanPrincipal) *100).toFixed(4);
		};
	//	

	//
	this.updatePortfolio = function( id, attribute, aValue )
		{
		if( attribute == "assestAllocation")
			if(!this.testAssestAllocationTotal( id, aValue ))
				return false;

		if( attribute == "fundName")
			if( this.isDuplicatFundName( aValue ))
				return false;
				
				
		for( var i = 0; i < this.pFundArr.length; i++ )
			{
			if( this.pFundArr[i].dbID == id )
				{
				this.pFundArr[i].updateFund( attribute, aValue );
				break;
				}
			}
		
		return true;	
		};
	//	

	//
	this.testAssestAllocationTotal = function( id, fundAllocation )
		{
		var aaTotal = 0;
		for( var i = 0; i < this.pFundArr.length; i++ )
			{
			if( this.pFundArr[i].dbID == id )
				aaTotal += eval(fundAllocation);
					else
						{
						if(!isNaN(this.pFundArr[i].fund.assestAllocation) )
							aaTotal += eval(this.pFundArr[i].fund.assestAllocation);
						}
					
			}

		if( aaTotal > 100 )
			{
			alert("The Total Assest Allocation Cannot be greater than 100%" );
			return false;
			}
			else
				{
				return true;
				}
		};
	//	

	//
	this.isDuplicatFundName = function( fName )
		{
		for( var i = 0; i < this.pFundArr.length; i++ )
			{
			if( this.pFundArr[i].fund.fundName == fName )
				{
				alert("The Fund Name \""+fName+"\" already exists.");
				return true;
				}
			}
		return false;
		};
	//	

	//
	this.deleteFund = function( fid )
		{
		var tempFund = new Array();
		var num = 0;
		
		for( var i = 0; i < this.pFundArr.length; i++ )
			{
			if( this.pFundArr[i].dbID != fid )
				{
				tempFund[ num ] = this.pFundArr[i];
				num++;
				}
			}
		this.pFundArr = tempFund;	
		global_pFunds = tempFund;
		};
	//	

	//
	this.addFund = function( iLoanPrincipal, fDate )
		{
		this.lastFundID += 1;
		var pFund = new portfolioFund( this.lastFundID , "", 0, 0, 0, fDate, iLoanPrincipal );
		this.pFundArr.push( pFund );		
		};
	//

	

	this.pFundArr = pfArr;
	this.loanPrincipal = iLoanPrincipal;
	this.contentObj = contentDiv;

	var n = 0;
	this.lastFundID = 0;
	for( var j = 0; j < this.pFundArr.length; j++ )
		{
		if( this.pFundArr[ j ].dbID > this.lastFundID )
			this.lastFundID = this.pFundArr[ j ].dbID;
		}	

	}
/* ----------- */


	
	
/* ----------- */
//
function loadPortfolio( f, p )
	{
	var contentStr = "";
	contentStr += "<table border=\"0\" width=\"100%\"><tr>";
	contentStr += "<td class=\"scenarioDataLabels\" valign=\"bottom\">Funds</td>";
	contentStr += "<td class=\"scenarioDataLabels\" width=\"60\" valign=\"bottom\">Assest Allocation</td>";
	contentStr += "<td class=\"scenarioDataLabels\" width=\"60\" valign=\"bottom\">Monthly Distribution Factor</td>";
	contentStr += "<td class=\"scenarioDataLabels\" width=\"60\" valign=\"bottom\">*NAV</td>";
	contentStr += "<td class=\"scenarioDataLabels\" width=\"60\" valign=\"bottom\">Units Purchased</td>";
	contentStr += "<td class=\"scenarioDataLabels\" width=\"60\" valign=\"bottom\">Monthly Distribution</td>";
	contentStr += "<td class=\"scenarioDataLabels\" width=\"60\" valign=\"bottom\">Annual Distribution</td>";
	contentStr += "<td></td>";

	contentStr += "</tr><tr>";
	
	for( var i = 0; i < p.pFundArr.length; i++ )
		{ 
		contentStr += "<tr>";
		contentStr += "<td><input type=\"hidden\" value=\"" + p.pFundArr[i].fund.databaseID + "\">" + getInputField("40", "1_" + i, p.pFundArr[i].fund.fundName, p.pFundArr[i].fund.dataBaseID, "fundName", "false", "input", "Portfolio" ) + "</td>";
		contentStr += "<td align=\"right\">" + getInputField("10", "portfolio_1_" + i, p.pFundArr[i].fund.assestAllocation.toFixed(1), p.pFundArr[i].fund.dataBaseID, "assestAllocation", "false", "input", "Portfolio" ) + "</td>";
		contentStr += "<td align=\"right\">" + getInputField("10", "portfolio_2_" + i, p.pFundArr[i].fund.monthlyDistributionFactor.toFixed(4), p.pFundArr[i].fund.dataBaseID, "monthlyDistributionFactor", "false", "input", "Portfolio" ) + "</td>";
		contentStr += "<td align=\"right\">" + getInputField("10", "portfolio_3_" + i, p.pFundArr[i].fund.NetAssetValue.toFixed(4), p.pFundArr[i].fund.dataBaseID, "NetAssetValue", "false", "input", "Portfolio" ) + "</td>";
		contentStr += "<td align=\"right\">" + getInputField("10", "portfolio_4_" + i, p.pFundArr[i].fund.unitsPurchased.toFixed(2), 0, "unitsPurchased", "true", "input", "Portfolio" ) + "</td>";
		contentStr += "<td align=\"right\">" + getInputField("10", "portfolio_5_" + i, p.pFundArr[i].fund.monthlyDistribution.toFixed(2), 0, "monthlyDistribution", "true", "input", "Portfolio" ) + "</td>";
		contentStr += "<td align=\"right\">" + getInputField("10", "portfolio_6_" + i, p.pFundArr[i].fund.annualDistribution.toFixed(2), 0, "annualDistribution", "true", "input", "Portfolio" ) + "</td>";
		contentStr += "<td align=\"right\"><a class=\"delete\" href=\"#\" onClick=\"action_Portfolio_deleteFund(this.form, " + p.pFundArr[i].fund.dataBaseID + ")\">delete</a></td>";
		contentStr += "</tr>";
		}
	contentStr += "<tr>";
	contentStr += "<td><b>Total</b></td>";
	contentStr += "<td align=\"right\">" + getInputField("10", "fAssestAllocationTotal", p.getAssestAllocationTotal(), 0, " ", "true", " ", "Portfolio" ) + "</td>";
	contentStr += "<td colspan=\"3\"></td>";
	contentStr += "<td align=\"right\">" + getInputField("10", "fMonthlyDistributionTotal", p.getMonthlyDistributionTotal(), 0, " ", "true", " ", "Portfolio" ) + "</td>";
	contentStr += "<td align=\"right\">" + getInputField("10", "fMonthlyDistributionTotal", p.getAnnualDistributionTotal(), 0, " ", "true", " ", "Portfolio" )+ "</td>";
	contentStr += "<td></td>";
	contentStr += "</tr>";
	contentStr += "<tr>";
	contentStr += "<td colspan=\"5\"></td>";
	contentStr += "<td align=\"right\">" + getInputField("10", "fMonthlyDistributionCompoundFactor", p.getMonthlyDistributionCompoundFactor(), 0, " ", "true", " ", "Portfolio" ) + "</td>";
	contentStr += "<td colspan=\"2\"></td>";
	contentStr += "</tr>";
	contentStr += "<tr>";
	contentStr += "<td colspan=\"8\" align=\"right\"><a class=\"addNew\" href=\"#\" onclick=\"action_Portfolio_addFund( this.form, getNowDateStr() )\">Add New Fund</a></td>";
	contentStr += "</tr>";
	contentStr += "<tr>";
	contentStr += "<td colspan=\"8\" align=\"right\">* NAV - <i>Net Asset Value on day of purchase</i>.</td>";
	contentStr += "</tr>";
	contentStr += "</table>";

	document.getElementById( p.contentObj ).innerHTML = contentStr;
	}
	
