Added ajax to check helper
[fa-stable.git] / js / inserts.js
index c6c047f77f8a2238a88fd55319f7ed5aa48f40a7..33bff885f6bd22d0f6ed166aa2e68ca4f166ecc3 100644 (file)
@@ -1,9 +1,8 @@
-
 var _focus;
 
 function debug(msg) {
-    document.getElementById('msgbox').innerHTML=
-       document.getElementById('msgbox').innerHTML+'<br>'+msg
+    box = document.getElementById('msgbox')
+       box.innerHTML= box.innerHTML+'<br>'+msg
 }
 
 function progbar(container) {
@@ -13,7 +12,6 @@ function progbar(container) {
 }
 
 function save_focus(e) {
-//  document.getElementsByName('_focus')[0].value = e.name;
   _focus = e.name||e.id;
   var h = document.getElementById('hints');
   if (h) {
@@ -33,7 +31,7 @@ function _expand(tabobj) {
          alltabs[i].className = "ajaxbutton"  //deselect all tabs
        }
        tabobj.className = "current";
-       JsHttpRequest.request(tabobj.name)
+       JsHttpRequest.request(tabobj)
   }
 }
 
@@ -44,18 +42,22 @@ function expandtab(tabcontentid, tabnumber) {
 }
 
 function _set_combo_input(e) {
-                 e.onblur=function() { 
-//                     if(!this.back) {
-                         var but_name = this.name.substring(0, this.name.length-4)+'button';
-                         var button = document.getElementsByName(but_name)[0];
-                         var select = document.getElementsByName(this.getAttribute('rel'))[0];
-                         save_focus(select);
-//             this.style.display='none';
-                         if(button) { // if *_button set submit search request
-                           JsHttpRequest.request(but_name);
-                         }
-                         return false;
-//                     }
+               e.setAttribute('_last', e.value);
+               e.onblur=function() { 
+                 var but_name = this.name.substring(0, this.name.length-4)+'button';
+                 var button = document.getElementsByName(but_name)[0];
+                 var select = document.getElementsByName(this.getAttribute('rel'))[0];
+                 save_focus(select);
+// submit request if there is submit_on_change option set and 
+// search field has changed.
+                 if (button && (this.value != this.getAttribute('_last'))) {
+                       JsHttpRequest.request(button);
+                 } else if(this.className=='combo2') {
+                               this.style.display = 'none';
+                               select.style.display = 'inline';
+                               setFocus(select.name);
+                 }
+                 return false;
                };
                e.onkeyup = function(ev) {
                        var select = document.getElementsByName(this.getAttribute('rel'))[0];
@@ -72,16 +74,15 @@ function _set_combo_input(e) {
                                }
                          }
                        }
-                 };
-               e.onkeydown = function(ev) { 
+               };
+       e.onkeydown = function(ev) { 
                        ev = ev||window.event;
                        key = ev.keyCode||ev.which;
                        if(key == 13) {
                          this.blur();
                          return false;
                        }
-//         this.back = ev.shiftKey; // save shift state for onblur handler
-                 }
+               }
 }
 
 function _update_box(s) {
@@ -90,13 +91,24 @@ function _update_box(s) {
        var box = document.getElementsByName(rel)[0];
                if(box && s.selectedIndex>=0) {
                          var opt = s.options[s.selectedIndex];
-                         if (opt.value != 0) {
-                               if(box) box.value = byid ? opt.value : opt.text;
-                         }
+                               if(box) {
+                                 box.value = byid ? opt.value : opt.text;
+                                 box.setAttribute('_last', box.value);
+                               }
                }
 }
 
 function _set_combo_select(e) {
+               // When combo position is changed via js (eg from searchbox)
+               // no onchange event is generated. To ensure proper change 
+               // signaling we must track selectedIndex in onblur handler.
+               e.setAttribute('_last', e.selectedIndex);
+               e.onblur = function() {
+                       if(this.className=='combo')
+                           _update_box(this);
+                       if (this.selectedIndex != this.getAttribute('_last'))
+                               this.onchange();
+               }
                e.onchange = function() {
                        var s = this;
                        
@@ -106,27 +118,19 @@ function _set_combo_select(e) {
                                 var sname = '_'+s.name+'_update';
                                 var update = document.getElementsByName(sname)[0];
                                 if(update) {
-                                           JsHttpRequest.request(sname);
+                                           JsHttpRequest.request(update);
                                } 
                        }
                        return true;
                }
-//             e.onblur = function() {
-//                     if (this.className == 'combo')
-//                       _update_box(this);
-//             },
                e.onkeydown = function(event) {
                    event = event||window.event;
-//                 this.back = event.shiftKey; // save shift state for onblur handler
                    key = event.keyCode||event.which;
                    var box = document.getElementsByName(this.getAttribute('rel'))[0];
                    if (box && key == 32 && this.className == 'combo2') {
                            this.style.display = 'none';
                            box.style.display = 'inline';
                                box.value='';
-  // Konq does not like short syntax for nonstd attr
-//                             this.setAttribute('back', 'true');
-//                         this.back=true;
                                setFocus(box.name);
                            return false;
                         }
@@ -141,6 +145,8 @@ var inserts = {
                if(e.onfocus==undefined) {
                        e.onfocus = function() {
                            save_focus(this);
+                               if (this.className == 'combo') 
+                                       this.select();
                        };
                }
                if (e.className == 'combo' || e.className == 'combo2') {
@@ -155,28 +161,43 @@ var inserts = {
        'input.ajaxsubmit,input.editbutton,input.navibutton': 
        function(e) {
            e.onclick = function() {
-               JsHttpRequest.request(this.name);
-               return false;
+                       JsHttpRequest.request(this);
+                       return false;
            }
        },
     '.amount': function(e) {
                if(e.onblur==undefined) {
-                 var dec = e.getAttribute("dec");
                  e.onblur = function() {
+                       var dec = this.getAttribute("dec");
                        price_format(this.name, get_amount(this.name), dec);
                  };
                }
        },
+       '.searchbox': // emulated onchange event handling for text inputs
+               function(e) {
+                       e.setAttribute('_last_val', e.value);
+                       e.setAttribute('autocomplete', 'off'); //must be off when calling onblur
+                       e.onblur = function() {
+                               var val = this.getAttribute('_last_val');
+                               if (val != this.value) {
+                                       this.setAttribute('_last_val', this.value);
+                                       JsHttpRequest.request('_'+this.name+'_changed', this.form);
+                               }
+                       }
+               e.onkeydown = function(ev) { 
+                               ev = ev||window.event;
+                               key = ev.keyCode||ev.which;
+                               if (key == 13 && (this.value != this.getAttribute('_last_val'))) {
+                                       this.blur();
+                                       return false;
+                               }
+                       }
+               },
        'select': function(e) {
                if(e.onfocus==undefined) {
                        e.onfocus = function() {
                            save_focus(this);
                        };
-                       e.onkeydown = function(event) { 
-                         event = event||window.event;
-                         this.back = event.shiftKey; // save shift state for onblur handler
-                         this.lastkey = event.keyCode; 
-                       };
                  var c = e.className;
                  if (c == 'combo' || c == 'combo2')
                        _set_combo_select(e);
@@ -202,6 +223,10 @@ var inserts = {
                    }
                }
            }
+       },
+       '#msgbox': function(e) {
+       // this is to avoid changing div height after ajax update in IE7
+         e.style.display = e.innerHTML.length ? 'block' : 'none';
        }
 /* TODO
        'a.date_picker':  function(e) {