Added fallback flag for non-js mode only ui elements.
[fa-stable.git] / js / inserts.js
1 var _focus;
2
3 function debug(msg) {
4     box = document.getElementById('msgbox')
5         box.innerHTML= box.innerHTML+'<br>'+msg
6 }
7
8 function progbar(container) {
9     container.innerHTML= "<center><img src='"+
10         user.theme+"images/progressbar1.gif' /> "+
11         user.loadtxt+"</center>";
12 }
13
14 function save_focus(e) {
15   _focus = e.name||e.id;
16   var h = document.getElementById('hints');
17   if (h) {
18         h.style.display = e.title && e.title.length ? 'inline' : 'none';
19         h.innerHTML = e.title ? e.title : '';
20   }
21 }
22
23 function _expand(tabobj) {
24
25   var ul = tabobj.parentNode.parentNode;
26   var alltabs=ul.getElementsByTagName("input");
27   var frm = tabobj.form;
28
29   if (ul.getAttribute("rel")){
30         for (var i=0; i<alltabs.length; i++){
31           alltabs[i].className = "ajaxbutton"  //deselect all tabs
32         }
33         tabobj.className = "current";
34         JsHttpRequest.request(tabobj)
35   }
36 }
37
38 //interface for selecting a tab (plus expand corresponding content)
39 function expandtab(tabcontentid, tabnumber) {
40   var tabs = document.getElementById(tabcontentid);
41  _expand(tabs.getElementsByTagName("input")[tabnumber]);
42 }
43
44 function _set_combo_input(e) {
45                 e.setAttribute('_last', e.value);
46                 e.onblur=function() { 
47                   var but_name = this.name.substring(0, this.name.length-4)+'button';
48                   var button = document.getElementsByName(but_name)[0];
49                   var select = document.getElementsByName(this.getAttribute('rel'))[0];
50                   save_focus(select);
51 // submit request if there is submit_on_change option set and 
52 // search field has changed.
53                   if (button && (this.value != this.getAttribute('_last'))) {
54                         JsHttpRequest.request(button);
55                   } else if(this.className=='combo2') {
56                                 this.style.display = 'none';
57                                 select.style.display = 'inline';
58                                 setFocus(select.name);
59                   }
60                   return false;
61                 };
62                 e.onkeyup = function(ev) {
63                         var select = document.getElementsByName(this.getAttribute('rel'))[0];
64                         if(select && select.selectedIndex>=0) {
65                           var len = select.length;
66                           var byid = this.className=='combo';
67                           var ac = this.value.toUpperCase();
68                           select.options[select.selectedIndex].selected = false;
69                           for (i = 0; i < len; i++) {
70                                 var txt = byid ? select.options[i].value : select.options[i].text;
71                                 if (txt.toUpperCase().indexOf(ac) >= 0) {
72                                   select.options[i].selected = true;
73                                   break;
74                                 }
75                           }
76                         }
77                 };
78         e.onkeydown = function(ev) { 
79                         ev = ev||window.event;
80                         key = ev.keyCode||ev.which;
81                         if(key == 13) {
82                           this.blur();
83                           return false;
84                         }
85                 }
86 }
87
88 function _update_box(s) {
89         var byid = s.className=='combo';
90         var rel = s.getAttribute('rel');
91         var box = document.getElementsByName(rel)[0];
92                 if(box && s.selectedIndex>=0) {
93                           var opt = s.options[s.selectedIndex];
94                                 if(box) {
95                                   box.value = byid ? opt.value : opt.text;
96                                   box.setAttribute('_last', box.value);
97                                 }
98                 }
99 }
100
101 function _set_combo_select(e) {
102                 // When combo position is changed via js (eg from searchbox)
103                 // no onchange event is generated. To ensure proper change 
104                 // signaling we must track selectedIndex in onblur handler.
105                 e.setAttribute('_last', e.selectedIndex);
106                 e.onblur = function() {
107                         if(this.className=='combo')
108                             _update_box(this);
109                         if (this.selectedIndex != this.getAttribute('_last'))
110                                 this.onchange();
111                 }
112                 e.onchange = function() {
113                         var s = this;
114                         this.setAttribute('_last', this.selectedIndex);                 
115                         if(s.className=='combo')
116                             _update_box(s);
117                         if(s.selectedIndex>=0) {
118                                  var sname = '_'+s.name+'_update';
119                                  var update = document.getElementsByName(sname)[0];
120                                  if(update) {
121                                             JsHttpRequest.request(update);
122                                 } 
123                         }
124                         return true;
125                 }
126                 e.onkeydown = function(event) {
127                     event = event||window.event;
128                     key = event.keyCode||event.which;
129                     var box = document.getElementsByName(this.getAttribute('rel'))[0];
130                     if (box && key == 32 && this.className == 'combo2') {
131                             this.style.display = 'none';
132                             box.style.display = 'inline';
133                                 box.value='';
134                                 setFocus(box.name);
135                             return false;
136                          }
137                 }
138 }               
139
140 /*
141  Behaviour definitions
142 */
143 var inserts = {
144         'input': function(e) {
145                 if(e.onfocus==undefined) {
146                         e.onfocus = function() {
147                             save_focus(this);
148                                 if (this.className == 'combo') 
149                                         this.select();
150                         };
151                 }
152                 if (e.className == 'combo' || e.className == 'combo2') {
153                                 _set_combo_input(e);
154                 }
155         },
156         'input.combo2,input[fallback]': 
157         function(e) {
158             // this hides search button for js enabled browsers
159             e.style.display = 'none';
160         },
161         'input.ajaxsubmit,input.editbutton,input.navibutton': 
162         function(e) {
163             e.onclick = function() {
164                         JsHttpRequest.request(this);
165                         return false;
166             }
167         },
168     '.amount': function(e) {
169                 if(e.onblur==undefined) {
170                   e.onblur = function() {
171                         var dec = this.getAttribute("dec");
172                         price_format(this.name, get_amount(this.name), dec);
173                   };
174                 }
175         },
176         '.searchbox': // emulated onchange event handling for text inputs
177                 function(e) {
178                         e.setAttribute('_last_val', e.value);
179                         e.setAttribute('autocomplete', 'off'); //must be off when calling onblur
180                         e.onblur = function() {
181                                 var val = this.getAttribute('_last_val');
182                                 if (val != this.value) {
183                                         this.setAttribute('_last_val', this.value);
184                                         JsHttpRequest.request('_'+this.name+'_changed', this.form);
185                                 }
186                         }
187 /*              e.onkeydown = function(ev) { 
188                                 ev = ev||window.event;
189                                 key = ev.keyCode||ev.which;
190                                 if (key == 13 && (this.value != this.getAttribute('_last_val'))) {
191                                         this.blur();
192                                         return false;
193                                 }
194                         }
195 */              },
196         'select': function(e) {
197                 if(e.onfocus==undefined) {
198                         e.onfocus = function() {
199                             save_focus(this);
200                         };
201                   var c = e.className;
202                   if (c == 'combo' || c == 'combo2')
203                         _set_combo_select(e);
204                 }
205         },
206         'textarea,a': function(e) {
207                 if(e.onfocus==undefined) {
208                         e.onfocus = function() {
209                             save_focus(this);
210                         };
211                 }
212         },
213         'ul.ajaxtabs':  function(ul) {
214             var ulist=ul.getElementsByTagName("li");
215             for (var x=0; x<ulist.length; x++){ //loop through each LI e
216                 var ulistlink=ulist[x].getElementsByTagName("input")[0];
217                 if(ulistlink.onclick==undefined) {
218 // ?  var modifiedurl=ulistlink.getAttribute("href").replace(/^http:\/\/[^\/]+\//i, "http://"+window.location.hostname+"/")
219                     var url = ulistlink.form.action
220                     ulistlink.onclick=function(){
221                         _expand(this);
222                         return false;
223                     }
224                 }
225             }
226         },
227         '#msgbox': function(e) {
228         // this is to avoid changing div height after ajax update in IE7
229           e.style.display = e.innerHTML.length ? 'block' : 'none';
230         }
231 /* TODO
232         'a.date_picker':  function(e) {
233             // this un-hides data picker for js enabled browsers
234             e.href = date_picker(this.getAttribute('rel'));
235             e.style.display = '';
236             e.tabindex = -1; // skip in tabbing order
237         }
238 */
239 };
240
241 Behaviour.register(inserts);
242
243 Behaviour.addLoadEvent(setFocus);