1 /**********************************************************************
2 Copyright (C) FrontAccounting, LLC.
3 Released under the terms of the GNU General Public License, GPL,
4 as published by the Free Software Foundation, either version 3
5 of the License, or (at your option) any later version.
6 This program is distributed in the hope that it will be useful,
7 but WITHOUT ANY WARRANTY; without even the implied warranty of
8 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
10 ***********************************************************************/
11 function set_mark(img) {
12 var box = document.getElementById('ajaxmark');
13 if(img) box.src = user.theme+'images/'+ img;
14 box.style.visibility = img ? 'visible' : 'hidden'
18 // JsHttpRequest class extensions.
20 // Main functions for asynchronus form submitions
21 // Trigger is the source of request and can have following forms:
22 // - input object - all form values are also submited
23 // - arbitrary string - POST var trigger with value 1 is added to request;
24 // if form parameter exists also form values are submited, otherwise
25 // request is directed to current location
27 JsHttpRequest.request= function(trigger, form, tout) {
28 tout = tout | 3000; // default timeout value
29 set_mark(tout>5000 ? 'progressbar.gif' : 'ajax-loader.gif');
30 JsHttpRequest._request(trigger, form, tout, 2);
33 JsHttpRequest._request = function(trigger, form, tout, retry) {
35 if (trigger.tagName=='A') {
38 var url = trigger.href;
39 if (trigger.id) content[trigger.id] = 1;
41 var submitObj = typeof(trigger) == "string" ?
42 document.getElementsByName(trigger)[0] : trigger;
44 form = form || (submitObj && submitObj.form);
46 var upload = form && form.enctype=='multipart/form-data';
48 var url = form ? form.action :
49 window.location.toString();
51 var content = this.formInputs(trigger, form, upload);
53 if (!form) url = url.substring(0, url.indexOf('?'));
59 // this is to avoid caching problems
60 content['_random'] = Math.random()*1234567;
62 var tcheck = setTimeout(
64 for(var id in JsHttpRequest.PENDING) {
65 var call = JsHttpRequest.PENDING[id];
67 if (call._ldObj.xr) // needed for gecko
68 call._ldObj.xr.onreadystatechange = function(){};
69 call.abort(); // why this doesn't kill request in firebug?
70 // call._ldObj.xr.abort();
71 delete JsHttpRequest.PENDING[id];
74 set_mark(retry ? 'ajax-loader2.gif':'warning.png' );
76 JsHttpRequest._request(trigger, form, tout, retry-1);
80 (upload ? "form." : "")+"POST "+url, // force form loader
82 // Function is called when an answer arrives.
83 function(result, errors) {
87 for(var i in result ) {
94 // debug(cmd+':'+property+':'+type+':'+id);
95 // seek element by id if there is no elemnt with given name
96 objElement = document.getElementsByName(id)[0] || document.getElementById(id);
98 eval("objElement.setAttribute('"+property+"',"+data+");");
99 } else if(cmd=='up') {
100 // if(!objElement) alert('No element "'+id+'"');
102 if (objElement.tagName == 'INPUT' || objElement.tagName == 'TEXTAREA')
103 objElement.value = data;
105 objElement.innerHTML = data; // selector, div, span etc
107 } else if(cmd=='di') { // disable/enable element
108 objElement.disabled = data;
109 } else if(cmd=='fc') { // set focus
111 } else if(cmd=='js') { // evaluate js code
113 } else if(cmd=='rd') { // client-side redirection
114 window.location = data;
115 } else if(cmd=='pu') { // pop-up
117 window.open(data,'REP_WINDOW','toolbar=no,scrollbar=no,resizable=yes,menubar=no');
119 errors = errors+'<br>Unknown ajax function: '+cmd;
123 JsHttpRequest.clearTimeout(tcheck);
124 // Write errors to the debug div.
125 document.getElementById('msgbox').innerHTML = errors;
131 window.scrollTo(0,0);
132 //document.getElementById('msgbox').scrollIntoView(true);
133 // Restore focus if we've just lost focus because of DOM element refresh
139 false // do not disable caching
142 // collect all form input values plus inp trigger value
143 JsHttpRequest.formInputs = function(inp, objForm, upload)
148 if (typeof(inp) == "string")
149 submitObj = document.getElementsByName(inp)[0];
153 objForm = objForm || (submitObj && submitObj.form);
157 var formElements = objForm.elements;
158 for( var i=0; i < formElements.length; i++)
160 var el = formElements[i];
162 if (!el.name) continue;
163 if(upload) { // for form containing file inputs collect all
164 // form elements and add value of trigger submit button
165 // (internally form is submitted via form.submit() not button click())
166 q[name] = submitObj.type=='submit' && el==submitObj ? el.value : el;
171 ((el.type == 'radio' || el.type == 'checkbox') && el.checked == false)
172 || (el.type == 'submit' && (!submitObj || el.name!=submitObj.name)))
174 if (el.disabled && el.disabled == true)
178 if(el.type=='select-multiple')
180 for (var j = 0; j < el.length; j++)
182 if (el.options[j].selected == true)
183 q[name] = el.options[j].value;
196 // User price formatting
198 function price_format(post, num, dec, label, color) {
199 var el = label ? document.getElementById(post) : document.getElementsByName(post)[0];
200 //num = num.toString().replace(/\$|\,/g,'');
203 sign = (num == (num = Math.abs(num)));
205 decsize = Math.pow(10, dec);
206 num = Math.floor(num*decsize+0.50000000001);
208 num = Math.floor(num/decsize).toString();
209 for( i=cents.toString().length; i<dec; i++){
212 for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
213 num = num.substring(0,num.length-(4*i+3))+user.ts+
214 num.substring(num.length-(4*i+3));
215 num = ((sign)?'':'-') + num;
216 if(dec!=0) num = num + user.ds + cents;
222 el.style.color = (sign) ? '' : '#FF0000';
226 function get_amount(doc, label) {
228 var val = document.getElementById(doc).innerHTML;
230 var val = document.getElementsByName(doc)[0].value;
231 val = val.replace(new RegExp('\\'+user.ts, 'g'),'');
232 val = +val.replace(new RegExp('\\'+user.ds, 'g'),'.');
233 return isNaN(val) ? 0 : val;
237 if (window.history.length <= 1)
240 window.history.go(-1);
243 function setFocus(name, byId) {
244 if(document.location.pathname.indexOf('index.php') != -1) {
245 // this is application menu page - set focus on first link
246 // var el = document.getElementById('msgbox');
247 // TODO find first link after msgbox and set focus
251 name = _focus; // last focus set in onfocus handlers
253 if (document.forms.length) { // no current focus (first page display) - set it from from last form
254 var cur = document.getElementsByName('_focus')[document.forms.length-1];
255 if(cur) name = cur.value;
259 el = document.getElementById(name);
261 el = document.getElementsByName(name)[0];
264 // The timeout is needed to prevent unpredictable behaviour on IE & Gecko.
265 // Using tmp var prevents crash on IE5
267 var tmp = function() {el.focus(); if (el.select) el.select();};