c550a2985dea9dc4f5c520aed0c38e7c65fbdd8d
[fa-stable.git] / js / allocate.js
1 function focus_alloc(i) {
2     save_focus(i);
3         i.setAttribute('_last', get_amount(i.name));
4 }
5
6 function blur_alloc(i) {
7         var change = get_amount(i.name);
8                 price_format(i.name, change, user.pdec);                
9                 if(change<0) change = 0;
10                 change = change-i.getAttribute('_last');
11         var total = get_amount('total_allocated', 1)+change;
12         var left = get_amount('left_to_allocate', 1)-change;
13         
14         price_format('left_to_allocate', left, user.pdec, 1, 1);
15         price_format('total_allocated', total, user.pdec, 1, 1);
16 }
17
18 function allocate_all(doc) {
19         var amount = get_amount('amount'+doc);
20         var unallocated = get_amount('un_allocated'+doc);
21         var total = get_amount('total_allocated', 1);
22         var left = get_amount('left_to_allocate', 1);
23         total -=  (amount-unallocated);
24         left += (amount-unallocated);
25         amount = unallocated;
26         if(left<0) {
27                 total  += left;
28                 amount += left;
29                 left = 0;
30         }
31         price_format('amount'+doc, amount, user.pdec);
32         price_format('left_to_allocate', left, user.pdec, 1,1);
33         price_format('total_allocated', total, user.pdec, 1, 1);
34 }
35
36 function allocate_none(doc) {
37         amount = get_amount('amount'+doc);
38         left = get_amount('left_to_allocate', 1);
39         total = get_amount('total_allocated', 1);
40         price_format('left_to_allocate',amount+left, user.pdec, 1, 1);
41         price_format('amount'+doc, 0, user.pdec);
42         price_format('total_allocated', total-amount, user.pdec, 1, 1);
43 }
44
45 var allocations = {
46         '.amount': function(e) {
47                 e.onblur = function() {
48                         blur_alloc(this);
49                   };
50                 e.onfocus = function() {
51                         focus_alloc(this);
52                 };
53         }
54 }
55
56 Behaviour.register(allocations);