Script Categories













Calculators >>> Adjusted Cost Base.

Computes the average cost base (or cost per share) for a series of stock or mutual fund purchases. When a sale is entered, the capital gain (or loss) is calculated.

Adjusted Cost Base Calculator

# of Units
Cost per Unit
Transaction Type Bought
Sold
Total Units
Total Cost
Adjusted Base Cost
Gain or Loss

Add the below code to the <body> section of your page:

<script language="javascript" type="text/javascript">
<!-- Original:  Gordon Coverley (gordonc@mondenet.com) -->
/* Visit http://www.yaldex.com/ for full source code
and get more free JavaScript, CSS and DHTML scripts! */
<!-- Begin
var tot_units = 0;
var tot_cost = 0;
function acbCalc(acbForm) {
with (acbForm) {
if (!unitsnum.value || isNaN(unitsnum.value) || !unitscost.value || isNaN(unitscost.value) ) {
alert("Please re-enter the units and cost amounts again.");
return;
}
else {
if (saletype[0].checked) { // bought
CapGain.value = "";
tot_units += unitsnum.value*1;
total_units.value = tot_units;
tot_cost += (unitsnum.value*1) * (unitscost.value*1);
total_cost.value = d_places(tot_cost, 4);
acb.value = d_places(tot_cost/tot_units, 4);
CapGain.value = "-- sales only --";
}
else { // sold
if ((unitsnum.value*-1 + tot_units*1) >= 0) {
tot_units -= unitsnum.value;
total_units.value = tot_units;
tot_cost = tot_units * acb.value;
total_cost.value = d_places(tot_cost, 4);
CapGain.value = d_places(unitsnum.value*(unitscost.value - acb.value), 2);
}
else {
alert("You don't have that many units to sell."); return;
         }
      }
   }
}
// sets the decimal precision
function d_places(n,p) {
var factor = 1;
var ans  = 0;
var j = 0;
for (j = 1; j <= p; j++)
factor =  factor * 10.0;
ans = (Math.round((n + 0.05 / (factor) ) * factor)) / factor;
return ans;
   }
}
//  End -->
</script>
<center>
<form>
<table border=1 cellpadding=5 cellspacing=0><tr><td>
<table>
<th
colspan=2>Adjusted Cost Base Calculator<p></th>
<tr>
<td>
# of Units</td>
<td>
<input type=text name=unitsnum size=15 maxlength=10></td>
</tr>
<tr>
<td>
Cost per Unit</td>
<td>
<input type=text name=unitscost size=15 maxlength=10></td>
</tr>
<tr>
<td>
Transaction Type</td>
<td>
<input type=radio name=saletype value="1" checked>Bought
<br><input type=radio name=saletype value="-1">Sold
</td>
</tr>
<tr>
<td
colspan=2 align=center><input type=button value="Add This Transaction" onClick="acbCalc(this.form);"></td>
</tr>
<tr>
<td>
Total Units</td>
<td>
<input type=text name=total_units size=15 readonly></td>
</tr>
<td>
Total Cost</td>
<td>
<input type=text name=total_cost size=15 maxlength=10 readonly></td>
</tr>
<tr>
<td>
Adjusted Base Cost</td>
<td>
<input type=text name=acb size=15 readonly></td>
</tr>
<tr>
<td>
Gain or Loss</td>
<td>
<input type=text name=CapGain size=15 maxlength=20 readonly></td>
</tr>
</table>
</td></tr></table>
</form>
</center>

JavaScript Editor Get Advanced
JavaScript and Ajax Editor,
Validator and Debugger!

1st JavaScript Editor.



Code was highlighted by 1st JavaScript Editor (The Best JavaScript Editor!).




©