From: Ray Speth Date: Wed, 17 Mar 2010 19:50:09 +0000 (-0400) Subject: Added javascript to calculate the dollar value of each share on-the-fly. X-Git-Url: https://asedeno.scripts.mit.edu/gitweb/?a=commitdiff_plain;h=9d82b09b61eb6e838e1cbb7b5a72426b5a6b4727;hp=6a7b56469afbbe2dab200f6c7a7b523cf2604f37;p=bluechips.git Added javascript to calculate the dollar value of each share on-the-fly. --- diff --git a/bluechips/public/js/calculator.js b/bluechips/public/js/calculator.js new file mode 100644 index 0000000..3c5509f --- /dev/null +++ b/bluechips/public/js/calculator.js @@ -0,0 +1,38 @@ +function validateSplit(input) { + if (!input.match(/^[\d\+\/\*\-\(\)\. ]*$/)) { + return Number.NaN; + } + if (input.match(/([\+\/\*\-])\1/)) { + return Number.NaN; + } + try { + v = eval(input); + } catch (err) { + return Number.NaN; + } + if (v == null) { + return 0; + } + return v; +} + +function calcSplit() { + amount = document.getElementById("amount").value; + total = 0; + var values = new Array(); + textvals = document.getElementsByClassName("share-text"); + for (i=0; i - ${h.currency('amount', c.expenditure.amount, size=8)} + ${h.currency('amount', c.expenditure.amount, size=8, onkeyup="calcSplit();")} @@ -32,9 +32,12 @@ - ${h.text('shares-%d.amount' % ii, percent)} + ${h.text('shares-%d.amount' % ii, percent, class_="share-text", onkeyup="calcSplit();")} ${h.hidden('shares-%d.user_id' % ii, user.id)} + + 0.00 + % endfor @@ -44,3 +47,4 @@ +${h.javascript_link('%s/js/calculator.js' % request.script_name)}