--- /dev/null
+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<textvals.length; i++) {
+ v = validateSplit(textvals[i].value);
+ if (!isNaN(v)) {
+ total += v;
+ }
+ values[i] = v;
+ }
+ for (i=0; i<textvals.length; i++) {
+ id = textvals[i].id+'-calc';
+ val = (amount*values[i]/total).toFixed(2);
+ document.getElementById(id).innerHTML = val;
+ }
+}
+
+window.onload=calcSplit;
</tr>
<tr>
<th><label for="amount">Amount</label></th>
- <td>${h.currency('amount', c.expenditure.amount, size=8)}</td>
+ <td>${h.currency('amount', c.expenditure.amount, size=8, onkeyup="calcSplit();")}</td>
</tr>
<tr>
<th><label for="date">Date</label></th>
<tr>
<th><label for="shares-${ii}amount">${user.name}</label></th>
<td>
- ${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)}
</td>
+ <td id="shares-${ii}amount-calc" align="right">
+ 0.00
+ </td>
</tr>
% endfor
<tr>
</tr>
</table>
</form>
+${h.javascript_link('%s/js/calculator.js' % request.script_name)}