Fixed ajax popup flicker.
[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                 var mark = document.getElementById('ajaxmark');
13                 if(mark) mark.style.visibility = 'visible';
14                 if (trigger.tagName=='A') {
15                         var content = {};
16                         var upload = 0;
17                         var url = trigger.href;
18                         if (trigger.id) content[trigger.id] = 1;
19                 } else {
20                 var submitObj = typeof(trigger) == "string" ? 
21                         document.getElementsByName(trigger)[0] : trigger;
22                 
23                 form = form || (submitObj && submitObj.form);
24
25                 var upload = form && form.enctype=='multipart/form-data';
26                 
27                 var url = form ? form.action : 
28                   window.location.toString();
29
30                 var content = this.formInputs(trigger, form, upload);
31
32                 if (!form) url = url.substring(0, url.indexOf('?'));
33                 
34                 if (!submitObj) 
35                         content[trigger] = 1;
36                         
37                 }
38                         // this is to avoid caching problems
39                 content['_random'] = Math.random()*1234567;
40
41         JsHttpRequest.query(
42             (upload ? "form." : "")+"POST "+url, // force form loader
43                 content,
44             // Function is called when an answer arrives. 
45             function(result, errors) {
46                 // Write the answer.
47                         var newwin = 0;
48                 if (result) {
49                           for(var i in result ) { 
50                           atom = result[i];
51                           cmd = atom['n'];
52                           property = atom['p'];
53                           type = atom['c'];
54                           id = atom['t'];
55                           data = atom['data'];
56 //                              debug(cmd+':'+property+':'+type+':'+id);
57                         // seek element by id if there is no elemnt with given name
58                           objElement = document.getElementsByName(id)[0] || document.getElementById(id);
59                   if(cmd=='as') {
60                                   eval("objElement.setAttribute('"+property+"',"+data+");");
61                           } else if(cmd=='up') {
62 //                              if(!objElement) debug('No element "'+id+'"');
63                             if (objElement.tagName == 'INPUT' || objElement.tagName == 'TEXTAREA')
64                                   objElement.value = data;
65                             else
66                                   objElement.innerHTML = data; // selector, div, span etc
67                           } else if(cmd=='di') { // disable/enable element
68                                   objElement.disabled = data;
69                           } else if(cmd=='fc') { // set focus
70                                   _focus = data;
71                           } else if(cmd=='js') {        // evaluate js code
72                                   eval(data);
73                           } else if(cmd=='rd') {        // client-side redirection
74                                   window.location = data;
75                           } else if(cmd=='pu') {        // pop-up
76                                   newwin = 1;
77                                   window.open(data,'REP_WINDOW','toolbar=no,scrollbar=no,resizable=yes,menubar=no');
78                           } else {
79                                   errors = errors+'<br>Unknown ajax function: '+cmd;
80                         }
81                   }
82
83         // Write errors to the debug div.
84                   document.getElementById('msgbox').innerHTML = errors;
85                   var mark = document.getElementById('ajaxmark');
86                   if(mark) mark.style.visibility = 'hidden';
87
88                   Behaviour.apply();
89                   if (errors.length>0)
90                         window.scrollTo(0,0);
91                         //document.getElementById('msgbox').scrollIntoView(true);
92           // Restore focus if we've just lost focus because of DOM element refresh
93                   if(!newwin) { 
94                         setFocus();
95                   }
96                 }
97             },
98             false  // do not disable caching
99         );
100     }
101         // collect all form input values plus inp trigger value
102         JsHttpRequest.formInputs = function(inp, objForm, upload)
103         {
104                 var submitObj = inp;
105                 var q = {};
106
107                 if (typeof(inp) == "string")
108                         submitObj = document.getElementsByName(inp)[0];
109                 else
110                         submitObj = inp;
111                 
112                 objForm = objForm || (submitObj && submitObj.form);
113
114                 if (objForm)
115                 {
116                         var formElements = objForm.elements;
117                         for( var i=0; i < formElements.length; i++)
118                         {
119                           var el = formElements[i];
120                           var name = el.name;
121                                 if (!el.name) continue;
122                                 if(upload) { // for form containing file inputs collect all 
123                                         // form elements and add value of trigger submit button
124                                         // (internally form is submitted via form.submit() not button click())
125                                         q[name] = submitObj.type=='submit' && el==submitObj ? el.value : el;
126                                         continue;
127                                 }
128                                 if (el.type )
129                                   if( 
130                                   ((el.type == 'radio' || el.type == 'checkbox') && el.checked == false)
131                                   || (el.type == 'submit' && (!submitObj || el.name!=submitObj.name)))
132                                         continue;
133                                 if (el.disabled && el.disabled == true)
134                                         continue;
135                                 if (name)
136                                 {
137                                         if(el.type=='select-multiple')
138                                         {
139                                                 for (var j = 0; j < el.length; j++)
140                                                 {
141                                                         if (el.options[j].selected == true)
142                                                                 q[name] = el.options[j].value;
143                                                 }
144                                         }
145                                         else
146                                         {
147                                                 q[name] = el.value;
148                                         }
149                                 } 
150                         }
151                 }
152                 return q;
153         }
154 //
155 //      User price formatting
156 //
157 function price_format(post, num, dec, label, color) {
158         var el = label ? document.getElementById(post) : document.getElementsByName(post)[0];
159         //num = num.toString().replace(/\$|\,/g,'');
160         if(isNaN(num))
161                 num = "0";
162         sign = (num == (num = Math.abs(num)));
163         if(dec<0) dec = 2;
164         decsize = Math.pow(10, dec);
165         num = Math.floor(num*decsize+0.50000000001);
166         cents = num%decsize;
167         num = Math.floor(num/decsize).toString();
168         for( i=cents.toString().length; i<dec; i++){
169                 cents = "0" + cents;
170         }
171         for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
172                 num = num.substring(0,num.length-(4*i+3))+user.ts+
173                         num.substring(num.length-(4*i+3));
174          num = ((sign)?'':'-') + num;
175          if(dec!=0) num = num + user.ds + cents;
176         if(label)
177             el.innerHTML = num;
178         else
179             el.value = num;
180         if(color) {
181                         el.style.color = (sign) ? '' : '#FF0000';
182         }
183 }
184
185 function get_amount(doc, label) {
186             if(label)
187                 var val = document.getElementById(doc).innerHTML;
188             else
189                 var val = document.getElementsByName(doc)[0].value;
190                 val = val.replace(new RegExp('\\'+user.ts, 'g'),'');
191                 val = val.replace(new RegExp('\\'+user.ds, 'g'),'.');
192                 return 1*val;
193 }
194
195 function goBack() {
196         if (window.history.length <= 1)
197          window.close();
198         else
199          window.history.go(-1);
200 }
201
202 function setFocus(name, byId) {
203   if(document.location.pathname.indexOf('index.php') != -1) {
204         // this is application menu page - set focus on first link
205         // var el = document.getElementById('msgbox');
206         // TODO find first link after msgbox and set focus
207   }
208   if(!name) {
209         if (_focus)     
210                 name = _focus;  // last focus set in onfocus handlers
211         else 
212          if (document.forms.length) {   // no current focus (first page display) -  set it from from last form
213           var cur = document.getElementsByName('_focus')[document.forms.length-1];
214           if(cur) name = cur.value;
215         }
216   }
217   if(byId)
218         el = document.getElementById(name);
219   else
220         el = document.getElementsByName(name)[0];
221
222   if(el && el.focus) {
223     // The timeout is needed to prevent unpredictable behaviour on IE & Gecko.
224     // Using tmp var prevents crash on IE5
225         
226     var tmp = function() {el.focus(); if (el.select) el.select();};
227         setTimeout(tmp, 0);
228   }
229 }