Function line misplaced.
[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         'list': false, // list of all elements with hotkey used recently
15         'focus': -1             // currently selected list element
16 };
17
18 function validate(e) {
19         if (e.name && (typeof _validate[e.name] == 'function'))
20                 return _validate[e.name](e);
21         else {
22                 var n = e.name.indexOf('[');
23                 if(n!=-1) {
24                         var key = e.name.substring(n+1, e.name.length-1);
25                         if (key.length>1 && _validate[e.name.substring(0,n)])
26                                 return _validate[e.name.substring(0,n)][key](e);
27                 }
28         }
29         return true;
30 }
31
32 function set_fullmode() {
33         document.getElementById('ui_mode').value = 1;
34         document.loginform.submit();
35         return true;
36 }
37
38 function save_focus(e) {
39   _focus = e.name||e.id;
40   var h = document.getElementById('hints');
41   if (h) {
42         h.style.display = e.title && e.title.length ? 'inline' : 'none';
43         h.innerHTML = e.title ? e.title : '';
44   }
45 }
46
47 function _expand(tabobj) {
48
49   var ul = tabobj.parentNode.parentNode;
50   var alltabs=ul.getElementsByTagName("button");
51   var frm = tabobj.form;
52
53   if (ul.getAttribute("rel")){
54         for (var i=0; i<alltabs.length; i++){
55           alltabs[i].className = "ajaxbutton"  //deselect all tabs // Review CP 2014-11 This will remove all other classes from the element.
56         }
57         tabobj.className = "current";
58         JsHttpRequest.request(tabobj)
59   }
60 }
61
62 //interface for selecting a tab (plus expand corresponding content)
63 function expandtab(tabcontentid, tabnumber) {
64   var tabs = document.getElementById(tabcontentid);
65  _expand(tabs.getElementsByTagName("input")[tabnumber]);
66 }
67
68 function _set_combo_input(e) {
69                 e.setAttribute('_last', e.value);
70                 e.onblur=function() {
71                   var but_name = this.name.substring(0, this.name.length-4)+'button';
72                   var button = document.getElementsByName(but_name)[0];
73                   var select = document.getElementsByName(this.getAttribute('rel'))[0];
74                   save_focus(select);
75 // submit request if there is submit_on_change option set and
76 // search field has changed.
77
78                   if (button && (this.value != this.getAttribute('_last'))) {
79                         JsHttpRequest.request(button);
80                   } else if(string_contains(this.className, 'combo2')) {
81                                 this.style.display = 'none';
82                                 select.style.display = 'inline';
83                                 setFocus(select);
84                   }
85                   return false;
86                 };
87                 e.onkeyup = function(ev) {
88                         var select = document.getElementsByName(this.getAttribute('rel'))[0];
89                         if(select && select.selectedIndex>=0) {
90                           var len = select.length;
91                           var byid = string_contains(this.className, 'combo') || string_contains(this.className, 'combo3');
92                           var ac = this.value.toUpperCase();
93                           select.options[select.selectedIndex].selected = false;
94                           for (i = 0; i < len; i++) {
95                                 var txt = byid ? select.options[i].value : select.options[i].text;
96                                 if (string_contains(this.className, 'combo3')) {
97                                   if(txt.toUpperCase().indexOf(ac) == 0) {
98                                         select.options[i].selected = true;
99                                         break;
100                                   }
101                                 } else {
102                                   if(txt.toUpperCase().indexOf(ac) >= 0) {
103                                         select.options[i].selected = true;
104                                         break;
105                                   }
106                                 }
107                           }
108                         }
109                 };
110         e.onkeydown = function(ev) {
111                         ev = ev||window.event;
112                         key = ev.keyCode||ev.which;
113                         if(key == 13) {
114                           this.blur();
115                           return false;
116                         }
117                 }
118 }
119
120 function _update_box(s) {
121         var byid = string_contains(s.className, 'combo') || string_contains(s.className, 'combo3');
122         var rel = s.getAttribute('rel');
123         var box = document.getElementsByName(rel)[0];
124                 if(box && s.selectedIndex>=0) {
125                           var opt = s.options[s.selectedIndex];
126                                 if(box) {
127                                   var old = box.value;
128                                   box.value = byid ? opt.value : opt.text;
129                                   box.setAttribute('_last', box.value);
130                                   return old != box.value
131                                 }
132                 }
133 }
134
135 function _set_combo_select(e) {
136                 // When combo position is changed via js (eg from searchbox)
137                 // no onchange event is generated. To ensure proper change
138                 // signaling we must track selectedIndex in onblur handler.
139                 e.setAttribute('_last', e.selectedIndex);
140                 e.onblur = function() {
141                     var box = document.getElementsByName(this.getAttribute('rel'))[0];
142                         if ((this.selectedIndex != this.getAttribute('_last'))
143                                 ||((string_contains(this.className, 'combo') || string_contains(this.className, 'combo3')) && _update_box(this))
144                                 )
145                                         this.onchange();
146                 }
147                 e.onchange = function() {
148                         var s = this;
149                         this.setAttribute('_last', this.selectedIndex);
150                         if(string_contains(s.className, 'combo') || string_contains(this.className, 'combo3'))
151                             _update_box(s);
152                         if(s.selectedIndex>=0) {
153                                  var sname = '_'+s.name+'_update';
154                                  var update = document.getElementsByName(sname)[0];
155                                  if(update) {
156                                             JsHttpRequest.request(update);
157                                 }
158                         }
159                         return true;
160                 }
161                 e.onkeydown = function(event) {
162                     event = event||window.event;
163                     key = event.keyCode||event.which;
164                     var box = document.getElementsByName(this.getAttribute('rel'))[0];
165                         if(key == 8 || (key==37 && event.altKey)) {
166                                 event.returnValue = false;
167                                 return false;
168                         }
169                     if (box && (key == 32) && (string_contains(this.className, 'combo2'))) {
170                             this.style.display = 'none';
171                             box.style.display = 'inline';
172                                 box.value='';
173                                 setFocus(box);
174                             return false;
175                          } else {
176                                 if (key == 13 && !e.length) // prevent chrome issue (blocked cursor after CR on empty selector)
177                                         return false;
178                          }
179                 }
180 }
181
182 var _w;
183
184 function callEditor(key) {
185   var el = document.getElementsByName(editors[key][1])[0];
186   if(_w) _w.close(); // this is really necessary to have window on top in FF2 :/
187   var left = (screen.width - editors[key][2]) / 2;
188   var top = (screen.height - editors[key][3]) / 2;
189   _w = open(editors[key][0]+el.value+'&popup=1',
190           "edit","scrollbars=yes,resizable=0,width="+editors[key][2]+",height="+editors[key][3]+",left="+left+",top="+top+",screenX="+left+",screenY="+top);
191   if (_w.opener == null)
192           _w.opener = self;
193   editors._call = key; // store call point for passBack
194   _w.focus();
195 }
196
197 function passBack(value) {
198         var o = opener;
199         if(value != false) {
200                 var back = o.editors[o.editors._call]; // form input bindings
201                 var to = o.document.getElementsByName(back[1])[0];
202                 if (to) {
203                         if (to[0] != undefined)
204                                 to[0].value = value; // ugly hack to set selector to any value
205                         to.value = value;
206                         // update page after item selection
207                         o.JsHttpRequest.request('_'+to.name+'_update', to.form);
208                         o.setFocus(to.name);
209                 }
210         }
211         close();
212 }
213
214 /*
215         Normalize date format using previous input value to guess missing date elements.
216         Helps fast date input field change with only single or double numbers (for day or day-and-month fragments)
217 */
218 function fix_date(date, last)
219 {
220         var dat = last.split(user.datesep);
221         var cur = date.split(user.datesep);
222         var day, month, year;
223
224 // TODO: user.date as default?
225 // TODO: user.datesys
226         if (date == "" || date == last) // should we return an empty date or should we return last?
227                 return date;
228         if (user.datefmt == 0 || user.datefmt == 3) // set defaults
229         {
230                 day = dat[1]; month = dat[0]; year = dat[2];
231         } else if (user.datefmt == 1 || user.datefmt == 4){
232                 day = dat[0]; month = dat[1]; year = dat[2];
233         } else {
234                 day = dat[2]; month = dat[1]; year = dat[0];
235         }
236         if (cur[1] != undefined && cur[1] != "") // day or month entered, could be string 3
237         {
238                 if (user.datefmt == 0 || user.datefmt == 3 || ((user.datefmt == 2 || user.datefmt == 5) && (cur[2] == undefined || cur[2] == "")))
239                         day = cur[1];
240                 else
241                         month = cur[1];
242         }
243         if (cur[0] != undefined && cur[0] != "") // day or month entered. could be string 3
244         {
245                 if (cur[1] == undefined || cur[1] == "")
246                         day = cur[0];
247                 else if (user.datefmt == 0 || user.datefmt == 3 || ((user.datefmt == 2 || user.datefmt == 5) && (cur[2] == undefined || cur[2] == "")))
248                         month = cur[0];
249                 else if (user.datefmt == 2 || user.datefmt == 5)
250                         year = cur[0];
251                 else
252                         day = cur[0];
253         }
254         if (cur[2] != undefined && cur[2] != "") // year,
255         {
256                 if (user.datefmt == 2 || user.datefmt == 5)
257                         day = cur[2];
258                 else
259                         year = cur[2];
260         }
261         if (user.datefmt<3) {
262                 if (day<10) day = '0'+parseInt(day, 10);
263                 if (month<10) month = '0'+parseInt(month, 10);
264         }
265         if (year<100) year = year<60 ? (2000+parseInt(year,10)) : (1900+parseInt(year,10));
266
267 //      console.info(day,month,year)
268         if (user.datefmt == 0 || user.datefmt==3)
269                 return month+user.datesep+day+user.datesep+year;
270         if (user.datefmt == 1 || user.datefmt==4)
271                 return day+user.datesep+month+user.datesep+year;
272         return year+user.datesep+month+user.datesep+day;
273 }
274
275 /*
276  Behaviour definitions
277 */
278 var inserts = {
279         'input': function(e) {
280                 if(e.onfocus==undefined) {
281                         e.onfocus = function() {
282                                 save_focus(this);
283                                 if (string_contains(this.className, 'combo') || string_contains(this.className, 'combo3'))
284                                         this.select();
285                         };
286                 }
287                 if (string_contains(e.className, 'combo') || string_contains(e.className, 'combo2') || string_contains(e.className, 'combo3')) {
288                                 _set_combo_input(e);
289                 }
290                 else
291                 if(e.type == 'text' ) {
292                                 e.onkeydown = function(ev) {
293                                         ev = ev||window.event;
294                                         key = ev.keyCode||ev.which;
295                                         if(key == 13) {
296                                                 if(e.className == 'searchbox') e.onblur();
297                                                 return false;
298                                         }
299                                         return true;
300                                 }
301                         }
302         },
303         'input.combo2,input[aspect="fallback"]':
304         function(e) {
305             // this hides search button for js enabled browsers
306             e.style.display = 'none';
307         },
308         'div.js_only':
309         function(e) {
310             // this shows divs for js enabled browsers only
311             e.style.display = 'block';
312         },
313         'button': function(e) {
314                 e.onclick = function(){
315                         if (validate(e)) {
316                                 setTimeout(function() { var asp = e.getAttribute('aspect');
317                                         if (asp && asp.indexOf('download') === -1)
318                                                 set_mark((asp && ((asp.indexOf('process') !== -1) || (asp.indexOf('nonajax') !== -1))) ? 'progressbar.gif' : 'ajax-loader.gif');
319                                 }, 100);
320                                 return true;
321                         }
322                 },
323                 e.onkeydown = function(ev) {    // block unintentional page escape with 'history back' key pressed on buttons
324                         ev = ev||window.event;
325                         key = ev.keyCode||ev.which;
326                         if(key == 8 || (key==37 && ev.altKey)) {
327                                 ev.returnValue = false;
328                                 return false;
329                         }
330                 }
331
332         },
333 //      '.ajaxsubmit,.editbutton,.navibutton': // much slower on IE7
334         'button.ajaxsubmit,input.ajaxsubmit,input.editbutton,button.editbutton,button.navibutton':
335         function(e) {
336                         e.onclick = function() {
337                                 if (validate(e)) {
338                                         save_focus(e);
339                                         var asp = e.getAttribute('aspect')
340                                         if (asp && (asp.indexOf('process') !== -1))
341                                                 JsHttpRequest.request(this, null, 600000); // ten minutes for backup
342                                         else
343                                                 JsHttpRequest.request(this);
344                                 }
345                                 return false;
346                         }
347         },
348     '.amount': function(e) {
349                 if(e.onblur==undefined) {
350                   e.onblur = function() {
351                         var dec = this.getAttribute("dec");
352                         price_format(this.name, get_amount(this.name), dec);
353                   };
354                 }
355         },
356         '.searchbox': // emulated onchange event handling for text inputs
357                 function(e) {
358                         e.setAttribute('_last_val', e.value);
359                         e.setAttribute('autocomplete', 'off'); //must be off when calling onblur
360                         e.onblur = function() {
361                                 var val = this.getAttribute('_last_val');
362                                 if (val != this.value) {
363                                         this.setAttribute('_last_val', this.value);
364                                         JsHttpRequest.request('_'+this.name+'_changed', this.form);
365                                 }
366                         }
367         },
368         '.date':
369                 function(e) {
370                         e.setAttribute('_last_val', e.value);
371                         e.setAttribute('autocomplete', 'off');
372                         e.onblur = function() {
373                                 var val = this.getAttribute('_last_val');
374                                 if (val != this.value) {
375                                         this.value = fix_date(this.value, val);
376                                         this.setAttribute('_last_val', this.value);
377                                         if (e.className.match(/\bactive\b/))
378                                                 JsHttpRequest.request('_'+this.name+'_changed', this.form);
379                                 }
380                         }
381         },
382         'button[aspect*selector], button[aspect*abort], input[aspect*selector]': function(e) {
383                 e.onclick = function() {
384                         passBack(this.getAttribute('rel'));
385                         return false;
386                 }
387         },
388         'button[aspect=popup]': function(e) {
389                 e.onclick = function() {
390                         if(_w) _w.close(); // this is really necessary to have window on top in FF2 :/
391                          var left = (screen.width - 800)/2;
392                          var top = (screen.height - 600)/2;
393                           _w = open(document.location+'popup=1',
394                                   "edit","Scrollbars=0,resizable=0,width=800,height=600, top="+top+",left="+left+",screenX="+left+",screenY="+top);
395                           if (_w.opener == null)
396                                   _w.opener = self;
397                         //  editors._call = key; // store call point for passBack
398 //                        _w.moveTo(50, 50);
399                           _w.focus();
400                         return false;
401                 }
402         },
403         'select': function(e) {
404                 if(e.onfocus==undefined) {
405                         e.onfocus = function() {
406                             save_focus(this);
407                         };
408                 }
409                 var c = e.className;
410                 if (string_contains(c, 'combo') || string_contains(c, 'combo2') || string_contains(c, 'combo3'))
411                         _set_combo_select(e);
412                 else {
413                         e.onkeydown = function(ev) {    // block unintentional page escape with 'history back' key pressed on buttons
414                                 ev = ev||window.event;
415                                 key = ev.keyCode||ev.which;
416                                 if(key == 8 || (key=37 && ev.altKey)) {
417                                         ev.returnValue = false;
418                                         return false;
419                                 }
420                         }
421                 }
422         },
423         'a.printlink':  function(l) {
424                 l.onclick = function() {
425                     save_focus(this);
426                         JsHttpRequest.request(this, null, 60000);
427                         return false;
428                 }
429         },
430         'a.repopts_link':       function(l) {
431                 l.onclick = function() {
432                     save_focus(this);
433                     var replinks = document.getElementsBySelector('a.repopts_link');
434                                 for(var i in replinks)
435                                         replinks[i].style.fontWeight = replinks[i]==this ? 'bold' : 'normal';
436                         JsHttpRequest.request(this, null);
437                         return false;
438                 }
439         },
440         'a': function(e) { // traverse menu
441                 e.onkeydown = function(ev) {
442                         ev = ev||window.event;
443                         key = ev.keyCode||ev.which;
444                         if(key==37 || key==38 || key==39 || key==40) {
445                                         move_focus(key, e, document.links);
446                                         ev.returnValue = false;
447                                         return false;
448                         }
449                 }
450                 // prevent unneeded transaction entry abortion
451                 if (e.className == 'shortcut'
452                  || e.className == 'menu_option'
453                  || e.className == 'menu_tab'
454                  || e.className == 'selected')
455                         e.onclick = function(ev) {
456                                 if (_validate._processing
457                                  && _validate._modified
458                                  && !confirm(_validate._processing)) {
459                                         ev.returnValue = false;
460                                         return false;
461                                 }
462                                 if (_hotkeys.alt)       // ommit Chrome accesskeys
463                                         return false;
464                                 window.location = e.href;
465                         }
466         },
467         'ul.ajaxtabs':  function(ul) {
468             var ulist=ul.getElementsByTagName("li");
469             for (var x=0; x<ulist.length; x++){ //loop through each LI e
470                 var tab=ulist[x].getElementsByTagName("button")[0];
471                     var url = tab.form.action
472                     tab.onclick=function(){
473                     if (!_hotkeys.alt && !this.disabled)
474                                 _expand(this);
475                         return false;
476                     }
477                 }
478 //          }
479         },
480         'textarea': function(e) {
481                 if(e.onfocus==undefined) {
482                         e.onfocus = function() {
483                             save_focus(this);
484                         };
485                 }
486         }
487 /* TODO
488         'a.date_picker':  function(e) {
489             // this un-hides data picker for js enabled browsers
490             e.href = date_picker(this.getAttribute('rel'));
491             e.style.display = '';
492             e.tabindex = -1; // skip in tabbing order
493         }
494 */
495 };
496
497 function stopEv(ev) {
498                         if(ev.preventDefault) {
499                                 ev.preventDefault();
500                                 ev.stopPropagation();
501                         } else {
502                                 ev.returnValue = false;
503                                 ev.cancelBubble = true;
504                                 window.keycode = 0;
505                         }
506                         return false;
507 }
508 /*
509         Modified accesskey system. While Alt key is pressed letter keys moves
510         focus to next marked link. Alt key release activates focused link.
511 */
512 function setHotKeys() {
513         document.onkeydown = function(ev) {
514                 ev = ev||window.event;
515                 key = ev.keyCode||ev.which;
516                 if (key == 18 && !ev.ctrlKey) { // start selection, skip Win AltGr
517                         _hotkeys.alt = true;
518                         _hotkeys.focus = -1;
519                         return stopEv(ev);
520                 }
521                 else if (ev.altKey && !ev.ctrlKey && ((key>47 && key<58) || (key>64 && key<91))) {
522                         key = String.fromCharCode(key);
523                         var n = _hotkeys.focus;
524                         var l = document.getElementsBySelector('[accesskey='+key+']');
525                         var cnt = l.length;
526                         _hotkeys.list = l;
527                         for (var i=0; i<cnt; i++) {
528                                 n = (n+1)%cnt;
529                                 // check also if the link is visible
530                                 if (l[n].accessKey==key && (l[n].offsetWidth || l[n].offsetHeight)) {
531                                         _hotkeys.focus = n;
532             // The timeout is needed to prevent unpredictable behaviour on IE.
533                                         var tmp = function() {l[_hotkeys.focus].focus();};
534                                         setTimeout(tmp, 0);
535                                         break;
536                                 }
537                         }
538                         return stopEv(ev);
539                 }
540                 if((ev.ctrlKey && key == 13) || key == 27) {
541                         _hotkeys.alt = false; // cancel link selection
542                         _hotkeys.focus = -1;
543                         ev.cancelBubble = true;
544                         if(ev.stopPropagation) ev.stopPropagation();
545                         // activate submit/escape form
546                         for(var j=0; j<this.forms.length; j++) {
547                                 var form = this.forms[j];
548                                 for (var i=0; i<form.elements.length; i++){
549                                         var el = form.elements[i];
550                                         var asp = el.getAttribute('aspect');
551
552                                         if (!string_contains(el.className, 'editbutton') && (asp && asp.indexOf('selector') !== -1) && (key==13 || key==27)) {
553                                                 passBack(key==13 ? el.getAttribute('rel') : false);
554                                                 ev.returnValue = false;
555                                                 return false;
556                                         }
557                                         if (((asp && asp.indexOf('default') !== -1) && key==13)||((asp && asp.indexOf('cancel') !== -1) && key==27)) {
558                                                 if (validate(el)) {
559                                                         if (asp.indexOf('nonajax') !== -1)
560                                                                 el.click();
561                                                         else
562                                                         if (asp.indexOf('process') !== -1)
563                                                                 JsHttpRequest.request(el, null, 600000);
564                                                         else
565                                                                 JsHttpRequest.request(el);
566                                                 }
567                                                 ev.returnValue = false;
568                                                 return false;
569                                         }
570                                 }
571                         }
572                         ev.returnValue = false;
573                         return false;
574                 }
575                 if (editors!=='undefined' && editors[key]) {
576                         callEditor(key);
577                         return stopEv(ev); // prevent default binding
578                 }
579                 return true;
580         };
581         document.onkeyup = function(ev) {
582                 ev = ev||window.event;
583                 key = ev.keyCode||ev.which;
584
585                 if (_hotkeys.alt==true) {
586                         if (key == 18) {
587                                 _hotkeys.alt = false;
588                                 if (_hotkeys.focus >= 0) {
589                                         var link = _hotkeys.list[_hotkeys.focus];
590                                         if(link.onclick)
591                                                 link.onclick();
592                                         else
593                                                 if (link.target=='_blank') {
594                                                         window.open(link.href,'','toolbar=no,scrollbar=no,resizable=yes,menubar=no,width=900,height=500');
595                                                         openWindow(link.href,'_blank');
596                                                 } else
597                                                         window.location = link.href;
598                                 }
599                         return stopEv(ev);
600                         }
601                 }
602                 return true;
603         }
604 }
605
606 Behaviour.register(inserts);
607
608 Behaviour.addLoadEvent(setFocus);
609 Behaviour.addLoadEvent(setHotKeys);