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 ***********************************************************************/
12 // JsHttpRequest class extensions.
14 // Main functions for asynchronus form submitions
15 // Trigger is the source of request and can have following forms:
16 // - input object - all form values are also submited
17 // - arbitrary string - POST var trigger with value 1 is added to request;
18 // if form parameter exists also form values are submited, otherwise
19 // request is directed to current location
21 JsHttpRequest.request= function(trigger, form) {
22 var mark = document.getElementById('ajaxmark');
23 if(mark) mark.style.visibility = 'visible';
24 if (trigger.tagName=='A') {
27 var url = trigger.href;
28 if (trigger.id) content[trigger.id] = 1;
30 var submitObj = typeof(trigger) == "string" ?
31 document.getElementsByName(trigger)[0] : trigger;
33 form = form || (submitObj && submitObj.form);
35 var upload = form && form.enctype=='multipart/form-data';
37 var url = form ? form.action :
38 window.location.toString();
40 var content = this.formInputs(trigger, form, upload);
42 if (!form) url = url.substring(0, url.indexOf('?'));
48 // this is to avoid caching problems
49 content['_random'] = Math.random()*1234567;
52 (upload ? "form." : "")+"POST "+url, // force form loader
54 // Function is called when an answer arrives.
55 function(result, errors) {
59 for(var i in result ) {
66 // debug(cmd+':'+property+':'+type+':'+id);
67 // seek element by id if there is no elemnt with given name
68 objElement = document.getElementsByName(id)[0] || document.getElementById(id);
70 eval("objElement.setAttribute('"+property+"',"+data+");");
71 } else if(cmd=='up') {
72 // if(!objElement) alert('No element "'+id+'"');
74 if (objElement.tagName == 'INPUT' || objElement.tagName == 'TEXTAREA')
75 objElement.value = data;
77 objElement.innerHTML = data; // selector, div, span etc
79 } else if(cmd=='di') { // disable/enable element
80 objElement.disabled = data;
81 } else if(cmd=='fc') { // set focus
83 } else if(cmd=='js') { // evaluate js code
85 } else if(cmd=='rd') { // client-side redirection
86 window.location = data;
87 } else if(cmd=='pu') { // pop-up
89 window.open(data,'REP_WINDOW','toolbar=no,scrollbar=no,resizable=yes,menubar=no');
91 errors = errors+'<br>Unknown ajax function: '+cmd;
95 // Write errors to the debug div.
96 document.getElementById('msgbox').innerHTML = errors;
97 var mark = document.getElementById('ajaxmark');
98 if(mark) mark.style.visibility = 'hidden';
103 window.scrollTo(0,0);
104 //document.getElementById('msgbox').scrollIntoView(true);
105 // Restore focus if we've just lost focus because of DOM element refresh
111 false // do not disable caching
114 // collect all form input values plus inp trigger value
115 JsHttpRequest.formInputs = function(inp, objForm, upload)
120 if (typeof(inp) == "string")
121 submitObj = document.getElementsByName(inp)[0];
125 objForm = objForm || (submitObj && submitObj.form);
129 var formElements = objForm.elements;
130 for( var i=0; i < formElements.length; i++)
132 var el = formElements[i];
134 if (!el.name) continue;
135 if(upload) { // for form containing file inputs collect all
136 // form elements and add value of trigger submit button
137 // (internally form is submitted via form.submit() not button click())
138 q[name] = submitObj.type=='submit' && el==submitObj ? el.value : el;
143 ((el.type == 'radio' || el.type == 'checkbox') && el.checked == false)
144 || (el.type == 'submit' && (!submitObj || el.name!=submitObj.name)))
146 if (el.disabled && el.disabled == true)
150 if(el.type=='select-multiple')
152 for (var j = 0; j < el.length; j++)
154 if (el.options[j].selected == true)
155 q[name] = el.options[j].value;
168 // User price formatting
170 function price_format(post, num, dec, label, color) {
171 var el = label ? document.getElementById(post) : document.getElementsByName(post)[0];
172 //num = num.toString().replace(/\$|\,/g,'');
175 sign = (num == (num = Math.abs(num)));
177 decsize = Math.pow(10, dec);
178 num = Math.floor(num*decsize+0.50000000001);
180 num = Math.floor(num/decsize).toString();
181 for( i=cents.toString().length; i<dec; i++){
184 for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
185 num = num.substring(0,num.length-(4*i+3))+user.ts+
186 num.substring(num.length-(4*i+3));
187 num = ((sign)?'':'-') + num;
188 if(dec!=0) num = num + user.ds + cents;
194 el.style.color = (sign) ? '' : '#FF0000';
198 function get_amount(doc, label) {
200 var val = document.getElementById(doc).innerHTML;
202 var val = document.getElementsByName(doc)[0].value;
203 val = val.replace(new RegExp('\\'+user.ts, 'g'),'');
204 val = +val.replace(new RegExp('\\'+user.ds, 'g'),'.');
205 return isNaN(val) ? 0 : val;
209 if (window.history.length <= 1)
212 window.history.go(-1);
215 function setFocus(name, byId) {
216 if(document.location.pathname.indexOf('index.php') != -1) {
217 // this is application menu page - set focus on first link
218 // var el = document.getElementById('msgbox');
219 // TODO find first link after msgbox and set focus
223 name = _focus; // last focus set in onfocus handlers
225 if (document.forms.length) { // no current focus (first page display) - set it from from last form
226 var cur = document.getElementsByName('_focus')[document.forms.length-1];
227 if(cur) name = cur.value;
231 el = document.getElementById(name);
233 el = document.getElementsByName(name)[0];
236 // The timeout is needed to prevent unpredictable behaviour on IE & Gecko.
237 // Using tmp var prevents crash on IE5
239 var tmp = function() {el.focus(); if (el.select) el.select();};