Merged changes in main branch up to v.2.1.2
[fa-stable.git] / js / inserts.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 var _focus;
12 var _hotkeys = {
13         'alt': false,   // whether is the Alt key pressed
14         'focus': -1             // currently selected indeks of document.links
15 };
16 var _validate = {}; // validation functions
17
18 function debug(msg) {
19     box = document.getElementById('msgbox')
20         box.innerHTML= box.innerHTML+'<br>'+msg
21 }
22
23 function save_focus(e) {
24   _focus = e.name||e.id;
25   var h = document.getElementById('hints');
26   if (h) {
27         h.style.display = e.title && e.title.length ? 'inline' : 'none';
28         h.innerHTML = e.title ? e.title : '';
29   }
30 }
31
32 function _expand(tabobj) {
33
34   var ul = tabobj.parentNode.parentNode;
35   var alltabs=ul.getElementsByTagName("input");
36   var frm = tabobj.form;
37
38   if (ul.getAttribute("rel")){
39         for (var i=0; i<alltabs.length; i++){
40           alltabs[i].className = "ajaxbutton"  //deselect all tabs
41         }
42         tabobj.className = "current";
43         JsHttpRequest.request(tabobj)
44   }
45 }
46
47 //interface for selecting a tab (plus expand corresponding content)
48 function expandtab(tabcontentid, tabnumber) {
49   var tabs = document.getElementById(tabcontentid);
50  _expand(tabs.getElementsByTagName("input")[tabnumber]);
51 }
52
53 function _set_combo_input(e) {
54                 e.setAttribute('_last', e.value);
55                 e.onblur=function() { 
56                   var but_name = this.name.substring(0, this.name.length-4)+'button';
57                   var button = document.getElementsByName(but_name)[0];
58                   var select = document.getElementsByName(this.getAttribute('rel'))[0];
59                   save_focus(select);
60 // submit request if there is submit_on_change option set and 
61 // search field has changed.
62                   if (button && (this.value != this.getAttribute('_last'))) {
63                         JsHttpRequest.request(button);
64                   } else if(this.className=='combo2') {
65                                 this.style.display = 'none';
66                                 select.style.display = 'inline';
67                                 setFocus(select);
68                   }
69                   return false;
70                 };
71                 e.onkeyup = function(ev) {
72                         var select = document.getElementsByName(this.getAttribute('rel'))[0];
73                         if(select && select.selectedIndex>=0) {
74                           var len = select.length;
75                           var byid = this.className=='combo';
76                           var ac = this.value.toUpperCase();
77                           select.options[select.selectedIndex].selected = false;
78                           for (i = 0; i < len; i++) {
79                                 var txt = byid ? select.options[i].value : select.options[i].text;
80                                 if (txt.toUpperCase().indexOf(ac) >= 0) {
81                                   select.options[i].selected = true;
82                                   break;
83                                 }
84                           }
85                         }
86                 };
87         e.onkeydown = function(ev) { 
88                         ev = ev||window.event;
89                         key = ev.keyCode||ev.which;
90                         if(key == 13) {
91                           this.blur();
92                           return false;
93                         }
94                 }
95 }
96
97 function _update_box(s) {
98         var byid = s.className=='combo';
99         var rel = s.getAttribute('rel');
100         var box = document.getElementsByName(rel)[0];
101                 if(box && s.selectedIndex>=0) {
102                           var opt = s.options[s.selectedIndex];
103                                 if(box) {
104                                   box.value = byid ? opt.value : opt.text;
105                                   box.setAttribute('_last', box.value);
106                                 }
107                 }
108 }
109
110 function _set_combo_select(e) {
111                 // When combo position is changed via js (eg from searchbox)
112                 // no onchange event is generated. To ensure proper change 
113                 // signaling we must track selectedIndex in onblur handler.
114                 e.setAttribute('_last', e.selectedIndex);
115                 e.onblur = function() {
116                         if(this.className=='combo')
117                             _update_box(this);
118                         if (this.selectedIndex != this.getAttribute('_last'))
119                                 this.onchange();
120                 }
121                 e.onchange = function() {
122                         var s = this;
123                         this.setAttribute('_last', this.selectedIndex);                 
124                         if(s.className=='combo')
125                             _update_box(s);
126                         if(s.selectedIndex>=0) {
127                                  var sname = '_'+s.name+'_update';
128                                  var update = document.getElementsByName(sname)[0];
129                                  if(update) {
130                                             JsHttpRequest.request(update);
131                                 } 
132                         }
133                         return true;
134                 }
135                 e.onkeydown = function(event) {
136                     event = event||window.event;
137                     key = event.keyCode||event.which;
138                     var box = document.getElementsByName(this.getAttribute('rel'))[0];
139                     if (box && key == 32 && this.className == 'combo2') {
140                             this.style.display = 'none';
141                             box.style.display = 'inline';
142                                 box.value='';
143                                 setFocus(box);
144                             return false;
145                          }
146                         if (this.getAttribute('aspect') == 'editable' && key==115) {
147                                 // F4: call related database editor - not available in non-js fallback mode
148                                 JsHttpRequest.request('_'+this.name+'_editor', this.form);
149                                 return false; // prevent default binding
150                                 // TODO: stopPropagation when needed
151                         }
152                 }
153 }               
154
155 /*
156  Behaviour definitions
157 */
158 var inserts = {
159         'input': function(e) {
160                 if(e.onfocus==undefined) {
161                         e.onfocus = function() {
162                             save_focus(this);
163                                 if (this.className == 'combo') 
164                                         this.select();
165                         };
166                 }
167                 if (e.className == 'combo' || e.className == 'combo2') {
168                                 _set_combo_input(e);
169                 } 
170                 else
171                 if(e.type == 'text' ) {
172                                 e.onkeydown = function(ev) { 
173                                         ev = ev||window.event;
174                                         key = ev.keyCode||ev.which;
175                                         if(key == 13) {
176                                                 if(e.className == 'searchbox') e.onblur();
177                                                 return false;
178                                         } 
179                                         return true;
180                                 }
181                         }
182         },
183         'input.combo2,input[aspect="fallback"]': 
184         function(e) {
185             // this hides search button for js enabled browsers
186             e.style.display = 'none';
187         },
188         'div.js_only': 
189         function(e) {
190             // this shows divs for js enabled browsers only
191             e.style.display = 'block';
192         },
193 //      '.ajaxsubmit,.editbutton,.navibutton': // much slower on IE7
194         'button.ajaxsubmit,input.ajaxsubmit,input.editbutton,button.editbutton,button.navibutton': 
195         function(e) {
196                     e.onclick = function() {
197                             save_focus(e);
198                                 if (e.getAttribute('aspect') == 'process')
199                                         JsHttpRequest.request(this, null, 30000);
200                                 else
201                                         JsHttpRequest.request(this);
202                                 return false;
203                     }
204         },
205         'button': function(e) {
206                 if (e.name) {
207                         var func = _validate[e.name];
208                         var old = e.onclick;
209                         if(func) {
210                                 if (typeof old != 'function') {
211                                         e.onclick = func;
212                                 } else {
213                                         e.onclick = function() {
214                                                 if(func()) 
215                                                         { old(); return true;}
216                                                 else
217                                                         return false;
218                                         }
219                                 }
220                         }
221                 }
222         },
223     '.amount': function(e) {
224                 if(e.onblur==undefined) {
225                   e.onblur = function() {
226                         var dec = this.getAttribute("dec");
227                         price_format(this.name, get_amount(this.name), dec);
228                   };
229                 }
230         },
231         '.searchbox': // emulated onchange event handling for text inputs
232                 function(e) {
233                         e.setAttribute('_last_val', e.value);
234                         e.setAttribute('autocomplete', 'off'); //must be off when calling onblur
235                         e.onblur = function() {
236                                 var val = this.getAttribute('_last_val');
237                                 if (val != this.value) {
238                                         this.setAttribute('_last_val', this.value);
239                                         JsHttpRequest.request('_'+this.name+'_changed', this.form);
240                                 }
241                         }
242                 },
243         'select': function(e) {
244                 if(e.onfocus==undefined) {
245                         e.onfocus = function() {
246                             save_focus(this);
247                         };
248                   var c = e.className;
249                   if (c == 'combo' || c == 'combo2')
250                         _set_combo_select(e);
251                 }
252         },
253         'textarea,a': function(e) {
254                 if(e.onfocus==undefined) {
255                         e.onfocus = function() {
256                             save_focus(this);
257                         };
258                         e.onmouseover = function(e) {
259                         setFocus(this);
260                                 return false;
261                         }
262                 }
263         },
264         'a.printlink':  function(l) {
265                 l.onclick = function() {
266                     save_focus(this);
267                         JsHttpRequest.request(this);
268                         return false;
269                 }
270         },
271         'a': function(e) { // traverse menu
272                 e.onkeydown = function(ev) { 
273                         ev = ev||window.event;
274                         key = ev.keyCode||ev.which;
275                         if(key==37 || key==38 || key==39 || key==40) {
276                                         move_focus(key, e, document.links);
277                                         ev.returnValue = false;
278                                         return false;
279                         }
280                 }
281         },
282         'ul.ajaxtabs':  function(ul) {
283             var ulist=ul.getElementsByTagName("li");
284             for (var x=0; x<ulist.length; x++){ //loop through each LI e
285                 var ulistlink=ulist[x].getElementsByTagName("input")[0];
286                 if(ulistlink.onclick==undefined) {
287 // ?  var modifiedurl=ulistlink.getAttribute("href").replace(/^http:\/\/[^\/]+\//i, "http://"+window.location.hostname+"/")
288                     var url = ulistlink.form.action
289                     ulistlink.onclick=function(){
290                         _expand(this);
291                         return false;
292                     }
293                 }
294             }
295         },
296 /*      'tr.editrow': function(e) {
297                         e.onkeydown = function(ev) { 
298                         ev = ev||window.event;
299                         key = ev.keyCode||ev.which;
300                         if(key == 13) {
301                           // Find & click additem/update button
302                           
303                         } else  if(key == 27) {
304                           return false;
305                         }
306                 }
307
308         },
309 */      '#msgbox': function(e) {
310         // this is to avoid changing div height after ajax update in IE7
311           e.style.display = e.innerHTML.length ? 'block' : 'none';
312         }
313 /* TODO
314         'a.date_picker':  function(e) {
315             // this un-hides data picker for js enabled browsers
316             e.href = date_picker(this.getAttribute('rel'));
317             e.style.display = '';
318             e.tabindex = -1; // skip in tabbing order
319         }
320 */
321 };
322 function stopEv(ev) {
323                         ev.returnValue = false;
324                         ev.cancelBubble = true;
325                         if(ev.preventDefault) ev.preventDefault();
326                         return false;
327 }
328 /*
329         Modified accesskey system. While Alt key is pressed letter keys moves 
330         focus to next marked link. Alt key release activates focused link.
331 */
332 function setHotKeys() {
333         document.onkeydown = function(ev) {
334                 ev = ev||window.event;
335                 key = ev.keyCode||ev.which;
336                 if (key == 18 && !ev.ctrlKey) { // start selection, skip Win AltGr
337                         _hotkeys.alt = true;
338                         _hotkeys.focus = -1;
339                         return stopEv(ev);
340                 }
341                 else if (_hotkeys.alt && ((key>47 && key<58) || (key>64 && key<91))) {
342                         var n = _hotkeys.focus;
343                         var l = document.links;
344                         var cnt = l.length;
345                         key = String.fromCharCode(key);
346                         for (var i=0; i<cnt; i++) { 
347                                 n = (n+1)%cnt;
348                                 // check also if the link is visible
349                                 if (l[n].accessKey==key && l[n].scrollWidth) {
350                                         _hotkeys.focus = n;
351             // The timeout is needed to prevent unpredictable behaviour on IE.
352                                         var tmp = function() {document.links[_hotkeys.focus].focus();};
353                                         setTimeout(tmp, 0);
354                                         break;
355                                 }
356                         }
357                         return stopEv(ev);
358                 }
359                 if((ev.ctrlKey && key == 13) || key == 27) {
360                         _hotkeys.alt = false; // cancel link selection
361                         _hotkeys.focus = -1;
362                         ev.cancelBubble = true;
363                         if(ev.stopPropagation) ev.stopPropagation();
364                         // activate submit/escape form
365                         for(var j=0; j<this.forms.length; j++) {
366                                 var form = this.forms[j];
367                                 for (var i=0; i<form.elements.length; i++){
368                                         var el = form.elements[i];
369                                         var asp = el.getAttribute('aspect');
370                                         if ((asp=='default' && key==13)||(asp=='cancel' && key==27)) {
371                                                 JsHttpRequest.request(el);
372                                                 ev.returnValue = false;
373                                                 return false;
374                                         }
375                                 }
376                         }
377                         ev.returnValue = false;
378                         return false;
379                 } 
380                 return true;
381         };
382         document.onkeyup = function(ev) {
383                 if (_hotkeys.alt==true) {
384                         ev = ev||window.event;
385                         key = ev.keyCode||ev.which;
386
387                         if (key == 18) {
388                                 _hotkeys.alt = false;
389                                 if (_hotkeys.focus>=0) {
390                                         var link = document.links[_hotkeys.focus];
391                                         if(link.onclick) 
392                                                 link.onclick();
393                                         else
394                                                 if (link.target=='_blank') {
395                                                         window.open(link.href,'','toolbar=no,scrollbar=no,resizable=yes,menubar=no,width=900,height=500');
396                                                         openWindow(link.href,'_blank');
397                                                 } else
398                                                         window.location = link.href;
399                                 }
400                         return stopEv(ev);
401                         } 
402                 }
403                 return true;
404         }
405 }
406
407 Behaviour.register(inserts);
408
409 Behaviour.addLoadEvent(setFocus);
410 Behaviour.addLoadEvent(setHotKeys);