Updated to latest version.
[fa-stable.git] / js / allocate.js
1 /**********************************************************************
2     Copyright (C) FrontAccounting, LLC.
3         Released under the terms of the GNU General Public License, GPL, 
4         as published by the Free Software Foundation, either version 3 
5         of the License, or (at your option) any later version.
6     This program is distributed in the hope that it will be useful,
7     but WITHOUT ANY WARRANTY; without even the implied warranty of
8     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
9     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
10 ***********************************************************************/
11 function focus_alloc(i) {
12     save_focus(i);
13         i.setAttribute('_last', get_amount(i.name));
14 }
15
16 function blur_alloc(i) {
17         var change = get_amount(i.name);
18                 price_format(i.name, change, user.pdec);                
19                 if(change<0) change = 0;
20                 change = change-i.getAttribute('_last');
21         var total = get_amount('total_allocated', 1)+change;
22         var left = get_amount('left_to_allocate', 1)-change;
23         
24         price_format('left_to_allocate', left, user.pdec, 1, 1);
25         price_format('total_allocated', total, user.pdec, 1, 1);
26 }
27
28 function allocate_all(doc) {
29         var amount = get_amount('amount'+doc);
30         var unallocated = get_amount('un_allocated'+doc);
31         var total = get_amount('total_allocated', 1);
32         var left = get_amount('left_to_allocate', 1);
33         total -=  (amount-unallocated);
34         left += (amount-unallocated);
35         amount = unallocated;
36         if(left<0) {
37                 total  += left;
38                 amount += left;
39                 left = 0;
40         }
41         price_format('amount'+doc, amount, user.pdec);
42         price_format('left_to_allocate', left, user.pdec, 1,1);
43         price_format('total_allocated', total, user.pdec, 1, 1);
44 }
45
46 function allocate_none(doc) {
47         amount = get_amount('amount'+doc);
48         left = get_amount('left_to_allocate', 1);
49         total = get_amount('total_allocated', 1);
50         price_format('left_to_allocate',amount+left, user.pdec, 1, 1);
51         price_format('amount'+doc, 0, user.pdec);
52         price_format('total_allocated', total-amount, user.pdec, 1, 1);
53 }
54
55 var allocations = {
56         '.amount': function(e) {
57                 e.onblur = function() {
58                         blur_alloc(this);
59                   };
60                 e.onfocus = function() {
61                         focus_alloc(this);
62                 };
63         }
64 }
65
66 Behaviour.register(allocations);