// dividend mutual fund fees script January 2008
  
  var rate;
  var mumber_payments;
  var interest_rate;
  var amount;
  var salesload;
  var annualfee;
  var redemptionfee;
  var yearsheld;
  var returnwofees;
  var totalfees;
  var returnwithfees;
  var percentreduction;
  
  function CurrencyFormatted(amount)
    {
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
    }
// end of function CurrencyFormatted()

function CommaFormatted(amount)
   {
	var delimiter = ","; // replace comma if desired
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return amount;
    }
// end of function CommaFormatted()
  
	
	
	function formatCurrency(strValue)
    {
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + '$' + dblValue + '.' + strCents);
    } 
	
    function power(interest_rate,yearsheld)
    {
    var add_one = 1 + interest_rate;
    var pow = Math.pow(add_one,yearsheld);
    return pow;
    } 
	
  function calculate(form) {
  amount = form.amount.value;
  salesload = form.salesload.value;
  annualfee = form.annualfee.value;
  redemptionfee = form.redemptionfee.value;
  rate = form.rate.value;
  yearsheld = form.yearsheld.value;
  
  //without fees 
  var interest_rate = rate/100;
  returnwofees = amount*(power(interest_rate,yearsheld));
  
  returnwofees = Math.round(100*returnwofees)/100;
  var result = CurrencyFormatted(returnwofees);
  form.returnwofees.value = CommaFormatted(result);
  var result = formatCurrency(returnwofees);
  form.returnwofees.value = result; 
  //without fees - end
  
 // begin with fees
  var costsalesload;
  var adj_amount;
  
  costsalesload = (amount*salesload/100)*(power(interest_rate,yearsheld)); //salesload tested OK 1/1/08
  adj_amount =  amount*(1 - (salesload/100));  // subtract sales load from initial amount
  var fee;
  var oppcostfee;
  var valuewith = adj_amount;
  var redemptionamt = 0;
  var totaloppcostfees = 0;
  var totalannualfees = 0;
  
  for (var i = 0; i < yearsheld; i++) // compute annual fees
  {
  valuewith = valuewith*(1 + interest_rate); // compute annual gain
  fee = valuewith*annualfee/100; // compute annual fee
  valuewith = valuewith - fee;
  totalannualfees += 1*fee; // sum annual fees
  oppcostfee = fee*power(interest_rate,yearsheld - i - 1); // compute opportunity cost of annual fee
  totaloppcostfees += 1*oppcostfee // sum opportunity costs
  }
 
  totaloppcostfees = 1*totaloppcostfees - totalannualfees + 1*costsalesload; // sum opportunity costs 
  redemptionamt = valuewith*(redemptionfee/100); // compute redemption amount
  totalfees =  totaloppcostfees  + 1*totalannualfees + 1*redemptionamt; //redemptionamt
  returnwithfees = valuewith - redemptionamt; // compute final value
  percentreduction = (1 - (returnwithfees/returnwofees));
  returnwithfees = Math.round(100*returnwithfees)/100;
  
  var result = CurrencyFormatted(returnwithfees);
  form.returnwithfees.value = CommaFormatted(result);
  var result = formatCurrency(returnwithfees);
  form.returnwithfees.value = result; 
  
  var result = CurrencyFormatted(totalfees);
  form.totalfees.value = CommaFormatted(result);
  var result = formatCurrency(totalfees);
  form.totalfees.value = result;
  
  var result = CurrencyFormatted(totaloppcostfees);
  form.totaloppcostfees.value = CommaFormatted(result);
  var result = formatCurrency(totaloppcostfees);
  form.totaloppcostfees.value = result;
  
  var result = CurrencyFormatted(totalannualfees);
  form.totalannualfees.value = CommaFormatted(result);
  var result = formatCurrency(totalannualfees);
  form.totalannualfees.value = result;
  
  var result = CurrencyFormatted(redemptionamt);
  form.redemptionamt.value = CommaFormatted(result);
  var result = formatCurrency(redemptionamt);
  form.redemptionamt.value = result;
  
  percentreduction = (percentreduction)*100;
  percentreduction = Math.round(100*percentreduction)/100;
  form.percentreduction.value = percentreduction;
  }
  
  // Copyright ©Richard A. Howard 2003-2008