Added searchbox class selector (onchange event in text input), text selection after...
[fa-stable.git] / js / utils.js
1 //
2 //      JsHttpRequest class extensions.
3 //
4 // Main functions for asynchronus form submitions
5 //      Trigger is the source of request and can have following forms:
6 //      - input object - all form values are also submited
7 //  - arbitrary string - POST var trigger with value 1 is added to request;
8 //              if form parameter exists also form values are submited, otherwise
9 //              request is directed to current location 
10 // 
11     JsHttpRequest.request= function(trigger, form) {
12
13                 
14                 var submitObj = typeof(trigger) == "string" ? 
15                         document.getElementsByName(trigger)[0] : trigger;
16                 
17                 form = form || (submitObj && submitObj.form);
18
19                 var url = form ? form.action : 
20                   window.location.toString();
21
22                 if (!form) url = url.substring(0, url.indexOf('?'));
23
24                 var values = this.formValues(trigger, form);
25                 if (!submitObj) 
26                         values[trigger] = 1;
27                 // this is to avoid caching problems
28                 values['_random'] = Math.random()*1234567;
29
30         JsHttpRequest.query(
31             'POST '+url, // backend
32                 values,
33             // Function is called when an answer arrives. 
34             function(result, errors) {
35                 // Write the answer.
36                 if (result) {
37                           for(var i in result ) { 
38                           atom = result[i];
39                           cmd = atom['n'];
40                           property = atom['p'];
41                           type = atom['c'];
42                           id = atom['t'];
43                           data = atom['data'];
44 //                              debug(cmd+':'+property+':'+type+':'+id);
45                         // seek element by id if there is no elemnt with given name
46                           objElement = document.getElementsByName(id)[0] || document.getElementById(id);
47                   if(cmd=='as') {
48                                   eval("objElement."+property+"=data;");
49                           } else if(cmd=='up') {
50 //                              if(!objElement) debug('No element "'+id+'"');
51                             if (objElement.tagName == 'INPUT' || objElement.tagName == 'TEXTAREA')
52                                   objElement.value = data;
53                             else
54                                   objElement.innerHTML = data; // selector, div, span etc
55                           } else if(cmd=='di') { // disable/enable element
56                                   objElement.disabled = data;
57                           } else if(cmd=='fc') { // set focus
58                                   _focus = data;
59                           } else if(cmd=='js') {        // evaluate js code
60                                   eval(data);
61                           } else if(cmd=='rd') {        // client-side redirection
62                                   window.location = data;
63                           } else {
64                                   errors = errors+'<br>Unknown ajax function: '+cmd;
65                         }
66                   }
67
68         // Write errors to the debug div.
69                   document.getElementById('msgbox').innerHTML = errors;
70
71                   Behaviour.apply();
72                   if (errors.length>0)
73                         window.scrollTo(0,0);
74                         //document.getElementById('msgbox').scrollIntoView(true);
75           // Restore focus if we've just lost focus because of DOM element refresh
76                   setFocus();
77                 }
78             },
79             false  // do not disable caching
80         );
81     }
82         // returns input field values submitted when form button 'name' is pressed
83         //
84         JsHttpRequest.formValues = function(inp, objForm)
85         {
86                 var submitObj = inp;
87                 var q = {};
88                 
89
90                 if (typeof(inp) == "string")
91                         submitObj = document.getElementsByName(inp)[0];
92                 else
93                         submitObj = inp;
94                 
95                 objForm = objForm || (submitObj && submitObj.form);
96
97                 if (objForm)
98                 {
99                         var formElements = objForm.elements;
100                         for( var i=0; i < formElements.length; i++)
101                         {
102                           var el = formElements[i];
103                                 if (!el.name) continue;
104                                 if (el.type )
105                                   if( 
106                                   ((el.type == 'radio' || el.type == 'checkbox') && el.checked == false)
107                                   || (el.type == 'submit' && (!submitObj || el.name!=submitObj.name)))
108                                         continue;
109                                 if (el.disabled && el.disabled == true)
110                                         continue;
111                                 var name = el.name;
112                                 if (name)
113                                 {
114                                         if(el.type=='select-multiple')
115                                         {
116                                                 for (var j = 0; j < el.length; j++)
117                                                 {
118                                                         if (el.options[j].selected == true)
119                                                                 q[name] = el.options[j].value;
120                                                 }
121                                         }
122                                         else
123                                         {
124                                                 q[name] = el.value;
125                                         }
126                                 } 
127                         }
128                 }
129                 return q;
130         }
131 //
132 //      User price formatting
133 //
134 function price_format(post, num, dec, label) {
135         //num = num.toString().replace(/\$|\,/g,'');
136         if(isNaN(num))
137                 num = "0";
138         sign = (num == (num = Math.abs(num)));
139         if(dec<0) dec = 2;
140         decsize = Math.pow(10, dec);
141         num = Math.floor(num*decsize+0.50000000001);
142         cents = num%decsize;
143         num = Math.floor(num/decsize).toString();
144         for( i=cents.toString().length; i<dec; i++){
145                 cents = "0" + cents;
146         }
147         for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
148                 num = num.substring(0,num.length-(4*i+3))+user.ts+
149                         num.substring(num.length-(4*i+3));
150          num = ((sign)?'':'-') + num;
151          if(dec!=0) num = num + user.ds + cents;
152         if(label)
153             document.getElementById(post).innerHTML = num;
154         else
155             document.getElementsByName(post)[0].value = num;
156 }
157
158 function get_amount(doc, label) {
159             if(label)
160                 var val = document.getElementById(doc).innerHTML;
161             else
162                 var val = document.getElementsByName(doc)[0].value;
163                 val = val.replace(new RegExp('\\'+user.ts, 'g'),'');
164                 val = val.replace(new RegExp('\\'+user.ds, 'g'),'.');
165                 return 1*val;
166 }
167
168 function goBack() {
169         if (window.history.length <= 1)
170          window.close();
171         else
172          window.history.go(-1);
173 }
174
175 function setFocus(name, byId) {
176
177   if(!name) {
178         if (_focus)     
179                 name = _focus;  // last focus set in onfocus handlers
180         else {  // no current focus -  set it from from hidden var (first page display)
181           var cur = document.getElementsByName('_focus')[0];
182           if(cur) name = cur.value;
183         }
184   }
185   if(byId)
186         el = document.getElementById(name);
187   else
188         el = document.getElementsByName(name)[0];
189
190   if(el && el.focus) {
191     // The timeout is needed to prevent unpredictable behaviour on IE & Gecko.
192     // Using tmp var prevents crash on IE5
193         
194     var tmp = function() {el.focus(); if (el.select) el.select();};
195         setTimeout(tmp, 0);
196   }
197 }