Additional fix.
[fa-stable.git] / js / inserts.js
1 var _focus;
2 var _hotkeys = {
3         'alt': false,   // whether is the Alt key pressed
4         'focus': -1     // currently selected indeks of document.links
5 };
6
7 function debug(msg) {
8     box = document.getElementById('msgbox')
9         box.innerHTML= box.innerHTML+'<br>'+msg
10 }
11
12 function progbar() {
13         box = document.getElementById('msgbox');
14     box.innerHTML= "<center><table width='98%' border='1' cellpadding=3 "
15         +"bordercolor='#007700' style='border-collapse: collapse'>"
16         +"<tr><td align='center' bgcolor='#ccffcc' >"
17                 +"<img src='"+user.theme+"images/progressbar.gif' alt='"
18                 +user.loadtxt+"' /></td></tr></table></center><br>";
19         box.style.display = 'block';
20 }
21
22 function save_focus(e) {
23   _focus = e.name||e.id;
24   var h = document.getElementById('hints');
25   if (h) {
26         h.style.display = e.title && e.title.length ? 'inline' : 'none';
27         h.innerHTML = e.title ? e.title : '';
28   }
29 }
30
31 function _expand(tabobj) {
32
33   var ul = tabobj.parentNode.parentNode;
34   var alltabs=ul.getElementsByTagName("input");
35   var frm = tabobj.form;
36
37   if (ul.getAttribute("rel")){
38         for (var i=0; i<alltabs.length; i++){
39           alltabs[i].className = "ajaxbutton"  //deselect all tabs
40         }
41         tabobj.className = "current";
42         JsHttpRequest.request(tabobj)
43   }
44 }
45
46 //interface for selecting a tab (plus expand corresponding content)
47 function expandtab(tabcontentid, tabnumber) {
48   var tabs = document.getElementById(tabcontentid);
49  _expand(tabs.getElementsByTagName("input")[tabnumber]);
50 }
51
52 function _set_combo_input(e) {
53                 e.setAttribute('_last', e.value);
54                 e.onblur=function() { 
55                   var but_name = this.name.substring(0, this.name.length-4)+'button';
56                   var button = document.getElementsByName(but_name)[0];
57                   var select = document.getElementsByName(this.getAttribute('rel'))[0];
58                   save_focus(select);
59 // submit request if there is submit_on_change option set and 
60 // search field has changed.
61                   if (button && (this.value != this.getAttribute('_last'))) {
62                         JsHttpRequest.request(button);
63                   } else if(this.className=='combo2') {
64                                 this.style.display = 'none';
65                                 select.style.display = 'inline';
66                                 setFocus(select.name);
67                   }
68                   return false;
69                 };
70                 e.onkeyup = function(ev) {
71                         var select = document.getElementsByName(this.getAttribute('rel'))[0];
72                         if(select && select.selectedIndex>=0) {
73                           var len = select.length;
74                           var byid = this.className=='combo';
75                           var ac = this.value.toUpperCase();
76                           select.options[select.selectedIndex].selected = false;
77                           for (i = 0; i < len; i++) {
78                                 var txt = byid ? select.options[i].value : select.options[i].text;
79                                 if (txt.toUpperCase().indexOf(ac) >= 0) {
80                                   select.options[i].selected = true;
81                                   break;
82                                 }
83                           }
84                         }
85                 };
86         e.onkeydown = function(ev) { 
87                         ev = ev||window.event;
88                         key = ev.keyCode||ev.which;
89                         if(key == 13) {
90                           this.blur();
91                           return false;
92                         }
93                 }
94 }
95
96 function _update_box(s) {
97         var byid = s.className=='combo';
98         var rel = s.getAttribute('rel');
99         var box = document.getElementsByName(rel)[0];
100                 if(box && s.selectedIndex>=0) {
101                           var opt = s.options[s.selectedIndex];
102                                 if(box) {
103                                   box.value = byid ? opt.value : opt.text;
104                                   box.setAttribute('_last', box.value);
105                                 }
106                 }
107 }
108
109 function _set_combo_select(e) {
110                 // When combo position is changed via js (eg from searchbox)
111                 // no onchange event is generated. To ensure proper change 
112                 // signaling we must track selectedIndex in onblur handler.
113                 e.setAttribute('_last', e.selectedIndex);
114                 e.onblur = function() {
115                         if(this.className=='combo')
116                             _update_box(this);
117                         if (this.selectedIndex != this.getAttribute('_last'))
118                                 this.onchange();
119                 }
120                 e.onchange = function() {
121                         var s = this;
122                         this.setAttribute('_last', this.selectedIndex);                 
123                         if(s.className=='combo')
124                             _update_box(s);
125                         if(s.selectedIndex>=0) {
126                                  var sname = '_'+s.name+'_update';
127                                  var update = document.getElementsByName(sname)[0];
128                                  if(update) {
129                                             JsHttpRequest.request(update);
130                                 } 
131                         }
132                         return true;
133                 }
134                 e.onkeydown = function(event) {
135                     event = event||window.event;
136                     key = event.keyCode||event.which;
137                     var box = document.getElementsByName(this.getAttribute('rel'))[0];
138                     if (box && key == 32 && this.className == 'combo2') {
139                             this.style.display = 'none';
140                             box.style.display = 'inline';
141                                 box.value='';
142                                 setFocus(box.name);
143                             return false;
144                          }
145                         if (this.getAttribute('aspect') == 'editable' && key==115) {
146                                 // F4: call related database editor - not available in non-js fallback mode
147                                 JsHttpRequest.request('_'+this.name+'_editor', this.form);
148                                 return false; // prevent default binding
149                                 // TODO: stopPropagation when needed
150                         }
151                 }
152 }               
153
154 /*
155  Behaviour definitions
156 */
157 var inserts = {
158         'form': function(e) {
159                 e.onkeydown = function(ev) { 
160                         ev = ev||window.event;
161                         key = ev.keyCode||ev.which;
162                         if((ev.ctrlKey && key == 13) || key == 27) {
163                                 ev.cancelBubble = true;
164                         if(ev.stopPropagation) ev.stopPropagation();
165 // here ctrl-enter/escape support
166                                 ev.returnValue = false;
167                                 return false;
168                         } 
169                         return true;
170                 }
171         },
172         'input': function(e) {
173                 if(e.onfocus==undefined) {
174                         e.onfocus = function() {
175                             save_focus(this);
176                                 if (this.className == 'combo') 
177                                         this.select();
178                         };
179                 }
180                 if (e.className == 'combo' || e.className == 'combo2') {
181                                 _set_combo_input(e);
182                 } 
183                 else
184                 if(e.type == 'text' ) {
185                                 e.onkeydown = function(ev) { 
186                                         ev = ev||window.event;
187                                         key = ev.keyCode||ev.which;
188                                         if(key == 13) {
189                                                 if(e.className == 'searchbox') e.onblur();
190                                                 return false;
191                                         } 
192                                         return true;
193                                 }
194                         }
195         },
196         'input.combo2,input[aspect="fallback"]': 
197         function(e) {
198             // this hides search button for js enabled browsers
199             e.style.display = 'none';
200         },
201         'div.js_only': 
202         function(e) {
203             // this shows divs for js enabled browsers only
204             e.style.display = 'block';
205         },
206         'input.ajaxsubmit,button.ajaxsubmit,button.editbutton,button.navibutton': 
207         function(e) {
208             e.onclick = function() {
209                         if (this.getAttribute('aspect') == 'process')
210                                 progbar();
211                         JsHttpRequest.request(this);
212                         return false;
213             }
214         },
215     '.amount': function(e) {
216                 if(e.onblur==undefined) {
217                   e.onblur = function() {
218                         var dec = this.getAttribute("dec");
219                         price_format(this.name, get_amount(this.name), dec);
220                   };
221                 }
222         },
223         '.searchbox': // emulated onchange event handling for text inputs
224                 function(e) {
225                         e.setAttribute('_last_val', e.value);
226                         e.setAttribute('autocomplete', 'off'); //must be off when calling onblur
227                         e.onblur = function() {
228                                 var val = this.getAttribute('_last_val');
229                                 if (val != this.value) {
230                                         this.setAttribute('_last_val', this.value);
231                                         JsHttpRequest.request('_'+this.name+'_changed', this.form);
232                                 }
233                         }
234 /*              e.onkeydown = function(ev) { 
235                                 ev = ev||window.event;
236                                 key = ev.keyCode||ev.which;
237                                 if (key == 13 && (this.value != this.getAttribute('_last_val'))) {
238                                         this.blur();
239                                         return false;
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                 }
259         },
260         'ul.ajaxtabs':  function(ul) {
261             var ulist=ul.getElementsByTagName("li");
262             for (var x=0; x<ulist.length; x++){ //loop through each LI e
263                 var ulistlink=ulist[x].getElementsByTagName("input")[0];
264                 if(ulistlink.onclick==undefined) {
265 // ?  var modifiedurl=ulistlink.getAttribute("href").replace(/^http:\/\/[^\/]+\//i, "http://"+window.location.hostname+"/")
266                     var url = ulistlink.form.action
267                     ulistlink.onclick=function(){
268                         _expand(this);
269                         return false;
270                     }
271                 }
272             }
273         },
274         '#msgbox': function(e) {
275         // this is to avoid changing div height after ajax update in IE7
276           e.style.display = e.innerHTML.length ? 'block' : 'none';
277         }
278 /* TODO
279         'a.date_picker':  function(e) {
280             // this un-hides data picker for js enabled browsers
281             e.href = date_picker(this.getAttribute('rel'));
282             e.style.display = '';
283             e.tabindex = -1; // skip in tabbing order
284         }
285 */
286 };
287 function stopEv(ev) {
288                         ev.returnValue = false;
289                         ev.cancelBubble = true;
290                         if(ev.preventDefault) ev.preventDefault();
291                         return false;
292 }
293 /*
294         Modified accesskey system. While Alt key is pressed letter keys moves 
295         focus to next marked link. Alt key release activates focused link.
296 */
297 function setHotKeys() {
298         document.onkeydown = function(ev) {
299                 ev = ev||window.event;
300                 key = ev.keyCode||ev.which;
301                 if (key == 18) {        // start selection
302                         _hotkeys.alt = true;
303                         _hotkeys.focus = -1;
304                         return stopEv(ev);
305                 } else
306                 if (key == 27) { // cancel selection
307                         _hotkeys.alt = false;
308                         _hotkeys.focus = -1;
309                         return stopEv(ev);
310                 } 
311                 else if (_hotkeys.alt && ((key>47 && key<58) || (key>64 && key<91))) {
312                         var n = _hotkeys.focus;
313                         var l = document.links;
314                         var cnt = l.length;
315                         key = String.fromCharCode(key);
316                         for (var i=0; i<cnt; i++) { 
317                                 n = (n+1)%cnt;
318                                 // check also if the link is visible
319                                 if (l[n].accessKey==key && l[n].scrollWidth) {
320                                         _hotkeys.focus = n;
321             // The timeout is needed to prevent unpredictable behaviour on IE.
322                                         var tmp = function() {document.links[_hotkeys.focus].focus();};
323                                         setTimeout(tmp, 0);
324                                         break;
325                                 }
326                         }
327                         return stopEv(ev);
328                 }
329                 return true;
330         };
331         document.onkeyup = function(ev) {
332                 if (_hotkeys.alt==true) {
333                         ev = ev||window.event;
334                         key = ev.keyCode||ev.which;
335
336                         if (key == 18) {
337                                 _hotkeys.alt = false;
338                                 if (_hotkeys.focus>=0) {
339                                         window.location = document.links[_hotkeys.focus].href;
340                                 }
341                         } 
342                         return stopEv(ev);
343                 }
344                 return true;
345         }
346 }
347
348 Behaviour.register(inserts);
349
350 Behaviour.addLoadEvent(setFocus);
351 Behaviour.addLoadEvent(setHotKeys);