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