165732795345a6a951348c7b24b2ed365d00807b
[fa-stable.git] / js / payalloc.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 = Math.min(get_amount(i.name), get_amount('maxval'+i.name.substr(6), 1))
18
19                 price_format(i.name, change, user.pdec);
20                 if (i.name != 'amount' && i.name != 'charge') {
21                         if (change<0) change = 0;
22                         change = change-i.getAttribute('_last');
23                         if (i.name == 'discount') change = -change;
24
25                         var total = get_amount('amount')+change;
26                         price_format('amount', total, user.pdec, 0);
27                 }
28 }
29
30 function allocate_all(doc) {
31         var amount = get_amount('amount'+doc);
32         var unallocated = get_amount('un_allocated'+doc);
33         var total = get_amount('amount');
34         var left = 0;
35         total -=  (amount-unallocated);
36         left -= (amount-unallocated);
37         amount = unallocated;
38         if(left<0) {
39                 total  += left;
40                 amount += left;
41                 left = 0;
42         }
43         price_format('amount'+doc, amount, user.pdec);
44         price_format('amount', total, user.pdec);
45 }
46
47 function allocate_none(doc) {
48         amount = get_amount('amount'+doc);
49         total = get_amount('amount');
50         price_format('amount'+doc, 0, user.pdec);
51         price_format('amount', total-amount, user.pdec);
52 }
53
54 var allocations = {
55         '.amount': function(e) {
56                 e.onblur = function() {
57                         blur_alloc(this);
58                   };
59                 e.onfocus = function() {
60                         focus_alloc(this);
61                 };
62         }
63 }
64
65 Behaviour.register(allocations);