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