2 // JsHttpRequest class extensions.
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
11 JsHttpRequest.request= function(trigger, form) {
12 var mark = document.getElementById('ajaxmark');
13 if(mark) mark.style.visibility = 'visible';
14 if (trigger.tagName=='A') {
17 var url = trigger.href;
18 if (trigger.id) content[trigger.id] = 1;
20 var submitObj = typeof(trigger) == "string" ?
21 document.getElementsByName(trigger)[0] : trigger;
23 form = form || (submitObj && submitObj.form);
25 var upload = form && form.enctype=='multipart/form-data';
27 var url = form ? form.action :
28 window.location.toString();
30 var content = this.formInputs(trigger, form, upload);
32 if (!form) url = url.substring(0, url.indexOf('?'));
38 // this is to avoid caching problems
39 content['_random'] = Math.random()*1234567;
42 (upload ? "form." : "")+"POST "+url, // force form loader
44 // Function is called when an answer arrives.
45 function(result, errors) {
49 for(var i in result ) {
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);
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;
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
71 } else if(cmd=='js') { // evaluate js code
73 } else if(cmd=='rd') { // client-side redirection
74 window.location = data;
75 } else if(cmd=='pu') { // pop-up
77 window.open(data,'REP_WINDOW','toolbar=no,scrollbar=no,resizable=yes,menubar=no');
79 errors = errors+'<br>Unknown ajax function: '+cmd;
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';
92 //document.getElementById('msgbox').scrollIntoView(true);
93 // Restore focus if we've just lost focus because of DOM element refresh
99 false // do not disable caching
102 // collect all form input values plus inp trigger value
103 JsHttpRequest.formInputs = function(inp, objForm, upload)
108 if (typeof(inp) == "string")
109 submitObj = document.getElementsByName(inp)[0];
113 objForm = objForm || (submitObj && submitObj.form);
117 var formElements = objForm.elements;
118 for( var i=0; i < formElements.length; i++)
120 var el = formElements[i];
122 if (!el.name) continue;
123 if(upload) { // for form containing file inputs collect all
124 // form elements and add value of trigger submit button
125 // (internally form is submitted via form.submit() not button click())
126 q[name] = submitObj.type=='submit' && el==submitObj ? el.value : el;
131 ((el.type == 'radio' || el.type == 'checkbox') && el.checked == false)
132 || (el.type == 'submit' && (!submitObj || el.name!=submitObj.name)))
134 if (el.disabled && el.disabled == true)
138 if(el.type=='select-multiple')
140 for (var j = 0; j < el.length; j++)
142 if (el.options[j].selected == true)
143 q[name] = el.options[j].value;
156 // User price formatting
158 function price_format(post, num, dec, label, color) {
159 var el = label ? document.getElementById(post) : document.getElementsByName(post)[0];
160 //num = num.toString().replace(/\$|\,/g,'');
163 sign = (num == (num = Math.abs(num)));
165 decsize = Math.pow(10, dec);
166 num = Math.floor(num*decsize+0.50000000001);
168 num = Math.floor(num/decsize).toString();
169 for( i=cents.toString().length; i<dec; i++){
172 for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
173 num = num.substring(0,num.length-(4*i+3))+user.ts+
174 num.substring(num.length-(4*i+3));
175 num = ((sign)?'':'-') + num;
176 if(dec!=0) num = num + user.ds + cents;
182 el.style.color = (sign) ? '' : '#FF0000';
186 function get_amount(doc, label) {
188 var val = document.getElementById(doc).innerHTML;
190 var val = document.getElementsByName(doc)[0].value;
191 val = val.replace(new RegExp('\\'+user.ts, 'g'),'');
192 val = val.replace(new RegExp('\\'+user.ds, 'g'),'.');
197 if (window.history.length <= 1)
200 window.history.go(-1);
203 function setFocus(name, byId) {
204 if(document.location.pathname.indexOf('index.php') != -1) {
205 // this is application menu page - set focus on first link
206 // var el = document.getElementById('msgbox');
207 // TODO find first link after msgbox and set focus
211 name = _focus; // last focus set in onfocus handlers
213 if (document.forms.length) { // no current focus (first page display) - set it from from last form
214 var cur = document.getElementsByName('_focus')[document.forms.length-1];
215 if(cur) name = cur.value;
219 el = document.getElementById(name);
221 el = document.getElementsByName(name)[0];
224 // The timeout is needed to prevent unpredictable behaviour on IE & Gecko.
225 // Using tmp var prevents crash on IE5
227 var tmp = function() {el.focus(); if (el.select) el.select();};