
function Rate(r_id, r_description,r_tradesinplan,r_inplantradefee, r_subscriptionfee, r_outofplantradefee,r_inactivityfee,r_termcode,r_hasoneqqqfree,r_noOfPurchaseTrades)
{
    this.ID=r_id;
    this.Description=r_description;
    this.TradesInPlan=r_tradesinplan;
    this.InPlanTradeFee=r_inplantradefee;
    this.SubscriptionFee=r_subscriptionfee;
    this.OutOfPlanTradeFee=r_outofplantradefee;
    this.InactivityFee=r_inactivityfee;
    this.TermCode=r_termcode;
    this.HasOneQQQFree=r_hasoneqqqfree;
    this.NoOfTrades=r_noOfPurchaseTrades;
    this.EffectiveCostPerPurchase=GetEffectiveCostPerPurchase(this);
    this.AverageCost=GetAverageCost(this);
}
	
function GetEffectiveCostPerPurchase(objRate)
{
    var result="(n/a)";
    var noOfPurchaseTrades=objRate.NoOfTrades;
    if(noOfPurchaseTrades == 0 || isNaN(noOfPurchaseTrades))
	{
		return; 
	}
	if(objRate.HasOneQQQFree == "True")
	{
		noOfPurchaseTrades=noOfPurchaseTrades-1;
	}	

	if(objRate.TermCode == 'T')
	{			
		var totalCostPerMonth = _getTradeCost(noOfPurchaseTrades,objRate.TradesInPlan,objRate.InPlanTradeFee,objRate.OutOfPlanTradeFee);
		
		if (objRate.InactivityFee > totalCostPerMonth)
		{
			result = objRate.InactivityFee;				
		}
		else
		{
			result = totalCostPerMonth;				
		}
		
		if(noOfPurchaseTrades > 0)
		{
			result = RoundNumber(result/noOfPurchaseTrades);
		}
		else
		{
			result = "(n/a)";
		}
		
	}
	else if(objRate.TermCode == 'Y')
	{			
		var monthlyEffectiveSubscriptionCost = objRate.SubscriptionFee/12;
		var stockPurchaseCost = _getTradeCost(noOfPurchaseTrades,objRate.TradesInPlan,objRate.InPlanTradeFee,objRate.OutOfPlanTradeFee);
		
		if (stockPurchaseCost < objRate.InactivityFee)
		{
			stockPurchaseCost = objRate.InactivityFee;
		} 
		
		var totalCost = parseFloat(monthlyEffectiveSubscriptionCost) + parseFloat(stockPurchaseCost);
		
		if(noOfPurchaseTrades > 0)
		{
			result = RoundNumber(totalCost/noOfPurchaseTrades);
		}
		else
		{
			result = "(n/a)";
		}
		
	}
	else if(objRate.TermCode == 'M')
	{	
		var monthlyEffectiveSubscriptionCost = objRate.SubscriptionFee;
		var stockPurchaseCost = _getTradeCost(noOfPurchaseTrades,objRate.TradesInPlan,objRate.InPlanTradeFee,objRate.OutOfPlanTradeFee);
		if (stockPurchaseCost < objRate.InactivityFee)
		{
			stockPurchaseCost = objRate.InactivityFee;
		} 
		
		var totalCost = parseFloat(monthlyEffectiveSubscriptionCost) + parseFloat(stockPurchaseCost);
		
		if(noOfPurchaseTrades > 0)
		{
			result = RoundNumber(totalCost/noOfPurchaseTrades);
		}
		else
		{
			result = "(n/a)";
		}		
	}
	return result;
}

function _getTradeCost(numTrades,tradesInPlan,inPlanTradeFee,outOfPlanTradeFee)
{
	if ((numTrades <= tradesInPlan) || (tradesInPlan==0))
	{
		return numTrades*inPlanTradeFee;
	}
	return (tradesInPlan*inPlanTradeFee) + (numTrades-tradesInPlan)*outOfPlanTradeFee;
}

function GetAverageCost(objRate)
{
	var result="(n/a)";
    var noOfPurchaseTrades=objRate.NoOfTrades;		
	if(noOfPurchaseTrades == 0 || isNaN(noOfPurchaseTrades))
	{
		return; 
	}
	if(objRate.HasOneQQQFree == "1")
	{
		noOfPurchaseTrades =noOfPurchaseTrades-1;
	}		
		
	if(objRate.TermCode == 'T')
	{				
		var totalCostPerMonth = _getTradeCost(noOfPurchaseTrades,objRate.TradesInPlan,objRate.InPlanTradeFee,objRate.OutOfPlanTradeFee);		
		if (objRate.InactivityFee > totalCostPerMonth)
			result = objRate.InactivityFee;				
		else
			result = totalCostPerMonth;				
		result = RoundNumber(result);
	}
	else if(objRate.TermCode == 'Y')
	{
		var monthlyEffectiveSubscriptionCost = objRate.SubscriptionFee/12;	
		var stockPurchaseCost = _getTradeCost(noOfPurchaseTrades,objRate.TradesInPlan,objRate.InPlanTradeFee,objRate.OutOfPlanTradeFee);
		if (stockPurchaseCost < objRate.InactivityFee)
		{
			stockPurchaseCost = objRate.InactivityFee;
		} 

		result = RoundNumber(parseFloat(monthlyEffectiveSubscriptionCost) + parseFloat(stockPurchaseCost));
	}
	else if(objRate.TermCode == 'M')
	{
		var monthlyEffectiveSubscriptionCost = objRate.SubscriptionFee;
		var stockPurchaseCost = _getTradeCost(noOfPurchaseTrades,tradesInPlan,inPlanTradeFee,outOfPlanTradeFee);
		if (stockPurchaseCost < objRate.InactivityFee)
		{
			stockPurchaseCost = objRate.InactivityFee;
		} 
		
		result = RoundNumber(parseFloat(monthlyEffectiveSubscriptionCost) + parseFloat(stockPurchaseCost));
	}
	return result;
}

function RoundNumber(numberval)
{
	var rlength = 2; // The number of decimal places to round to

	var newnumber = Math.round(numberval*Math.pow(10,rlength))/Math.pow(10,rlength);
	
	var strVal = String(newnumber);
	if(strVal.indexOf('.') == -1)
	{
		return strVal + '.00';
	}
	else
	{
		if(strVal.substring(strVal.indexOf('.')).length - 1 == 1)
		{
			return strVal + '0';
		}
		
		return strVal;
	}
}

function sortRateArray(objRateArray)
{
    objRateArray.sort(funcSortRate);
}

function funcSortRate(objFRate, objSRate)
{
    return objFRate.EffectiveCostPerPurchase - objSRate.EffectiveCostPerPurchase;
}