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