43b5f43ba2ed1511723b12d48936863d456b0bca
[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.name)
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.onblur=function() { 
46                   var but_name = this.name.substring(0, this.name.length-4)+'button';
47                   var button = document.getElementsByName(but_name)[0];
48                   var select = document.getElementsByName(this.getAttribute('rel'))[0];
49                   save_focus(select);
50 //              this.style.display='none';
51                   if(button) { // if *_button set submit search request
52                         JsHttpRequest.request(but_name);
53                   }
54                   return false;
55                 };
56                 e.onkeyup = function(ev) {
57                         var select = document.getElementsByName(this.getAttribute('rel'))[0];
58                         if(select && select.selectedIndex>=0) {
59                           var len = select.length;
60                           var byid = this.className=='combo';
61                           var ac = this.value.toUpperCase();
62                           select.options[select.selectedIndex].selected = false;
63                           for (i = 0; i < len; i++) {
64                                 var txt = byid ? select.options[i].value : select.options[i].text;
65                                 if (txt.toUpperCase().indexOf(ac) >= 0) {
66                                   select.options[i].selected = true;
67                                   break;
68                                 }
69                           }
70                         }
71                   };
72           e.onkeydown = function(ev) { 
73                         ev = ev||window.event;
74                         key = ev.keyCode||ev.which;
75                         if(key == 13) {
76                           this.blur();
77                           return false;
78                         }
79                   }
80 }
81
82 function _update_box(s) {
83         var byid = s.className=='combo';
84         var rel = s.getAttribute('rel');
85         var box = document.getElementsByName(rel)[0];
86                 if(box && s.selectedIndex>=0) {
87                           var opt = s.options[s.selectedIndex];
88                           if (opt.value != 0) {
89                                 if(box) box.value = byid ? opt.value : opt.text;
90                           }
91                 }
92 }
93
94 function _set_combo_select(e) {
95                 e.onblur = function() {
96                         if(this.className=='combo')
97                             _update_box(this);
98                 }
99                 e.onchange = function() {
100                         var s = this;
101                         
102                         if(s.className=='combo')
103                             _update_box(s);
104                         if(s.selectedIndex>=0) {
105                                  var sname = '_'+s.name+'_update';
106                                  var update = document.getElementsByName(sname)[0];
107                                  if(update) {
108                                             JsHttpRequest.request(sname);
109                                 } 
110                         }
111                         return true;
112                 }
113                 e.onkeydown = function(event) {
114                     event = event||window.event;
115                     key = event.keyCode||event.which;
116                     var box = document.getElementsByName(this.getAttribute('rel'))[0];
117                     if (box && key == 32 && this.className == 'combo2') {
118                             this.style.display = 'none';
119                             box.style.display = 'inline';
120                                 box.value='';
121                                 setFocus(box.name);
122                             return false;
123                          }
124                 }
125 }               
126
127 /*
128  Behaviour definitions
129 */
130 var inserts = {
131         'input': function(e) {
132                 if(e.onfocus==undefined) {
133                         e.onfocus = function() {
134                             save_focus(this);
135                         };
136                 }
137                 if (e.className == 'combo' || e.className == 'combo2') {
138                                 _set_combo_input(e);
139                 }
140         },
141         'input.combo_submit,input.combo_select,input.combo2': 
142         function(e) {
143             // this hides search button for js enabled browsers
144             e.style.display = 'none';
145         },
146 /*      'select.combo,select.combo2':
147         function(e) {
148                 var box = document.getElementsByName(e.getAttribute('rel'))[0];
149                 if(box) {
150                   box.style.width = 200+'px';
151                   e.style.width = 200+'px';
152                   debug(e.name+':'+e.style.width)
153                 }
154         },
155 */      'input.ajaxsubmit,input.editbutton,input.navibutton': 
156         function(e) {
157             e.onclick = function() {
158                 JsHttpRequest.request(this.name);
159                 return false;
160             }
161         },
162     '.amount': function(e) {
163                 if(e.onblur==undefined) {
164                   var dec = e.getAttribute("dec");
165                   e.onblur = function() {
166                         price_format(this.name, get_amount(this.name), dec);
167                   };
168                 }
169         },
170         'select': function(e) {
171                 if(e.onfocus==undefined) {
172                         e.onfocus = function() {
173                             save_focus(this);
174                         };
175                   var c = e.className;
176                   if (c == 'combo' || c == 'combo2')
177                         _set_combo_select(e);
178                 }
179         },
180         'textarea,a': function(e) {
181                 if(e.onfocus==undefined) {
182                         e.onfocus = function() {
183                             save_focus(this);
184                         };
185                 }
186         },
187         'ul.ajaxtabs':  function(ul) {
188             var ulist=ul.getElementsByTagName("li");
189             for (var x=0; x<ulist.length; x++){ //loop through each LI e
190                 var ulistlink=ulist[x].getElementsByTagName("input")[0];
191                 if(ulistlink.onclick==undefined) {
192 // ?  var modifiedurl=ulistlink.getAttribute("href").replace(/^http:\/\/[^\/]+\//i, "http://"+window.location.hostname+"/")
193                     var url = ulistlink.form.action
194                     ulistlink.onclick=function(){
195                         _expand(this);
196                         return false;
197                     }
198                 }
199             }
200         },
201         '#msgbox': function(e) {
202         // this is to avoid changing div height after ajax update in IE7
203           e.style.display = e.innerHTML.length ? 'block' : 'none';
204         }
205 /* TODO
206         'a.date_picker':  function(e) {
207             // this un-hides data picker for js enabled browsers
208             e.href = date_picker(this.getAttribute('rel'));
209             e.style.display = '';
210             e.tabindex = -1; // skip in tabbing order
211         }
212 */
213 };
214
215 Behaviour.register(inserts);
216
217 Behaviour.addLoadEvent(setFocus);