// steadyhand.com fee calculator
// Neil Jensen, October 2011

/////////////////////////// Fee Calculator ///////////////////////////////////

FeeCalculator = function () {
    bindMethods(this);
};

FeeCalculator.prototype.initialize = function () {

    default_assets = 0;
    this.funds = funds;	// get fund data from array in CMS
  
    // check the querystring for asset sizes or use defaults
    var args = parseQueryString(window.location.search);
    if( isEmpty(keys(args))){
  	  default_assets = 30000;
    };
  
	  //populate array and table with data (and querystring with amounts if available)
	  for( var fund in this.funds ) {
        if (isUndefinedOrNull(args[fund])) { 
  	        this.funds[fund]['amount'] = default_assets;
        } else {
  	        this.funds[fund]['amount'] = args[fund];
        }
	  	
        $(fund + '_fee').innerHTML = this.funds[fund].simple.toFixed(2);
        $(fund + '_fee_rebate').innerHTML = this.funds[fund].simple.toFixed(2);
        $(fund + '_cat_fee').innerHTML = this.funds[fund].category.toFixed(2);
        $(fund).value = this.funds[fund].amount;

        //connect events for data entry
        connect(fund,"onkeyup", this.updateTotal);
	  }
  
 
	  // initialize or reset the data
	  var no_year_discount = "0";
	  var five_year_discount = "7"; //in percent
	  var ten_year_discount = "14";	
	
  	// draw the select box... do this so we can easily reset
	  var newSelect = SELECT({"name":"select_years", "id":"select_years"}, OPTION({"value":no_year_discount, "selected":"selected"},"less than 5"), OPTION({"value":five_year_discount},"between 5 and 10"), OPTION({"value":ten_year_discount},"10 or more")); 
	  swapDOM( $('select_years'), newSelect );
	  connect($('select_years'),'onchange', this.updateTotal);	

    // initialize rebate levels
    // first $100k @ 100%, next $150k at 80%, next $200k at 70%, rest at %60%
    this.rebates = new Array();
    this.rebates[0] = new Array( 100000, 100 );
    this.rebates[1] = new Array( 150000, 80 );
    this.rebates[2] = new Array( 250000, 70 );
    this.rebates[3] = new Array( 0, 60 );    
        

    // create the column chart
    var dt = new google.visualization.DataTable();   
    dt.addColumn('string','Category');          
    dt.addColumn('number','Fee'); 
    dt.addRows(2);

    dt.setCell(0, 0,'Category Average');
    dt.setCell(1, 0,'Steadyhand');

    dt.setCell(0, 1,0);
    dt.setCell(1, 1,0);
    self.dt = dt;

    self.chartoptions =  {width: 400, 
                          height: 400, 
                          legendTextStyle: {fontName:'Helvetica',fontSize:11},  
                          legend: 'none',
                          is3D: false,
                          vAxis: {minValue:0}, 
                          series: [{color: '#7daecb'}],
                          chartArea:{left:40,top:20,width:"80%",height:"90%"}
                          };

     self.view = new google.visualization.ColumnChart(document.getElementById('chart1'));
     self.view.draw(dt, self.chartoptions);


     this.updateTotal();
};

// format numbers
FeeCalculator.prototype.dollarFormatter = numberFormatter("-###,###");

FeeCalculator.prototype.updateTotal = function() {
     
  	//populate fund object with data
	var total = 0;
	for( var fund in this.funds ) {
		var i = 0;
		if (!isNaN(parseInt($(fund).value,10) )) i = parseInt($(fund).value,10);
		this.funds[fund].amount = i;
		total = total + i;
	}
 	 		
	// determine the weighted fee with the rebates
	var weighted_fee = 100;
	var incremental_assets = total; // the incremental amount to apply the fee to
	
	// TODO: clean this up and turn into a loop
	if (incremental_assets > this.rebates[0][0]) {
		weighted_fee = this.rebates[0][1] * (this.rebates[0][0]/total);	
	} else {
		weighted_fee = this.rebates[0][1] * (incremental_assets/total);
		incremental_assets = 0;
	}

	incremental_assets = incremental_assets - this.rebates[0][0];
	if (incremental_assets > this.rebates[1][0]) {
		weighted_fee = weighted_fee + this.rebates[1][1] * (this.rebates[1][0]/total); 
	} else if (incremental_assets > 0) {
		weighted_fee = weighted_fee + this.rebates[1][1] * (incremental_assets/total);
		incremental_assets = 0;
	}
	
	incremental_assets = incremental_assets - this.rebates[1][0];
	if (incremental_assets > this.rebates[2][0]) {
		weighted_fee = weighted_fee + this.rebates[2][1] * (this.rebates[2][0]/total); 
	} else if (incremental_assets > 0) {
		weighted_fee = weighted_fee + this.rebates[2][1] * (incremental_assets/total);
		incremental_assets = 0;
	}

	incremental_assets = incremental_assets - this.rebates[2][0];
	if (incremental_assets > 0) {
		weighted_fee = weighted_fee + this.rebates[3][1] * (incremental_assets/total);
		incremental_assets = 0;
	}

	// apply the tenure rebates
	weighted_fee = weighted_fee * (1 - parseInt($('select_years').value)/100);

	// calculate the weighted averages and totals
	var simple_total = 0;
	var rebate_total = 0;
	var average_total = 0;
	var total_dollars_saved = 0;
	for( var fund in this.funds ) {
		var weighting = this.funds[fund].amount/total;
	
		// simple fees
		simple_total = simple_total + this.funds[fund].simple * weighting;
	
		// rebates
		var rebate_fee = (this.funds[fund].simple * weighted_fee/100 );
		$(fund + '_fee_rebate').innerHTML = rebate_fee.toFixed(2);
		rebate_total = rebate_total + rebate_fee*weighting;
		
		// category average fees
		average_total = average_total + this.funds[fund].category * weighting;
		
		// actual dollars saved
		var dollars = (this.funds[fund].category - rebate_fee)/100 * this.funds[fund].amount;
		$(fund + '_dollars_saved').innerHTML = dollars.toFixed(0);
		total_dollars_saved = total_dollars_saved + dollars;
	}
	
	// update totals on the table
	$('total').innerHTML = this.dollarFormatter(total);
	$('simple_total').innerHTML = simple_total.toFixed(2);
	$('rebate_total').innerHTML = rebate_total.toFixed(2);
	$('average_total').innerHTML = average_total.toFixed(2);
	$('total_dollars_saved').innerHTML = this.dollarFormatter(total_dollars_saved.toFixed(0));

	$('rebate_percent').innerHTML = (100 - weighted_fee).toFixed(2);
	
	// render the updated chart
        self.dt.setCell(0, 1,average_total);
        self.dt.setCell(1, 1,rebate_total);

        self.view.draw(self.dt, self.chartoptions);

};

google.load("visualization", "1", {packages:["corechart"]});
fc = new FeeCalculator();
addLoadEvent(fc.initialize);




