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