Client side ajax library
[fa-stable.git] / js / utils.js
1 //
2 //      JsHttpRequest class extensions.
3 //
4     JsHttpRequest.request= function(submit) {
5         JsHttpRequest.query(
6             'POST '+window.location.toString(), // backend
7
8             this.formValues(submit),
9                         
10             // Function is called when an answer arrives. 
11             function(result, errors) {
12                         // Write errors to the debug div.
13             document.getElementById('msgbox').innerHTML = errors; 
14                 // Write the answer.
15                 if (result) {
16                     for(var i in result ) { 
17                         atom = result[i];
18                         
19                         cmd = atom['n'];
20                         property = atom['p'];
21                         type = atom['c'];
22                         id = atom['t'];
23                         data = atom['data'];
24 //                      alert(cmd+':'+property+':'+type+':'+id+':'+data);
25                         // seek element by id if there is no elemnt with given name
26                         objElement = document.getElementsByName(id)[0] || document.getElementById(id);
27                 if(cmd=='as') {
28                                   eval("objElement."+property+"=data;");
29                         } else if(cmd=='up') {
30                           if(!objElement) debug('No element "'+id+'"');
31                             if (objElement.tagName == 'INPUT' || objElement.tagName == 'TEXTAREA')
32                                   objElement.value = data;
33                             else
34                                   objElement.innerHTML = data; // selector, div, span etc
35                         } else if(cmd=='di') { // disable element
36                                   objElement.disabled = data;
37                         } else if(cmd=='js') {  // evaluate js code
38                                   eval(data);
39                         } else if(cmd=='rd') {  // client-side redirection
40                             debug('redirecting '+data);
41                                   window.location = data;
42                         } else {
43                                   errors = errors+'<br>Unknown ajax function: '+cmd;
44                         }
45                     }
46                         Behaviour.apply();
47                         if (errors.length>0)
48                         // window.scrollTo(0,0);
49                         document.getElementById('msgbox').scrollIntoView(true);
50                 }
51             },
52             false  // do not disable caching
53         );
54     }
55 /*      //      calls form validation function
56         //
57     JsHttpRequest.validate= function(submit) {
58         JsHttpRequest.query(
59           'POST '+window.location.toString(), // backend
60             this.formValues('_validate_form'),
61             // Function is called when an answer arrives. 
62             function(result, errors) {
63                   if (result) {   
64                     window.location = result;
65                   } else
66                         return false;
67                 return true;
68             },
69             false
70         );
71     }
72 */      // returns input field values submitted when form button 'name' is pressed
73         //
74         JsHttpRequest.formValues = function(inp)
75         {
76                 var objForm;
77                 var submitObj;
78                 var q = {};
79                 
80                 if (typeof(inp) == "string")
81                         submitObj = document.getElementsByName(inp)[0];
82                 else
83                         submitObj = inp;
84                 if(submitObj) {
85                   objForm = submitObj.form;
86                 }
87                 if (objForm)
88                 {
89                         var formElements = objForm.elements;
90                         for( var i=0; i < formElements.length; i++)
91                         {
92                           var el = formElements[i];
93                                 if (!el.name) continue;
94                                 if (el.type )
95                                   if( 
96                                   ((el.type == 'radio' || el.type == 'checkbox') && el.checked == false)
97                                   || (el.type == 'submit' && el.name!=submitObj.name))
98                                         continue;
99                                 if (el.disabled && el.disabled == true)
100                                         continue;
101                                 var name = el.name;
102                                 if (name)
103                                 {
104                                         if(el.type=='select-multiple')
105                                         {
106                                                 for (var j = 0; j < el.length; j++)
107                                                 {
108                                                         if (el.options[j].selected == true)
109                                                                 q[name] = el.options[j].value;
110                                                 }
111                                         }
112                                         else
113                                         {
114                                                 q[name] = el.value;
115                                         }
116                                 } 
117                         }
118                 }
119                 // this is to avoid caching problems
120                 q['_random'] = Math.random()*1234567;
121                 return q;
122         }
123 //
124 //      User price formatting
125 //
126 function price_format(post, num, dec, label) {
127         //num = num.toString().replace(/\$|\,/g,'');
128         if(isNaN(num))
129                 num = "0";
130         sign = (num == (num = Math.abs(num)));
131         if(dec<0) dec = 2;
132         decsize = Math.pow(10, dec);
133         num = Math.floor(num*decsize+0.50000000001);
134         cents = num%decsize;
135         num = Math.floor(num/decsize).toString();
136         for( i=cents.toString().length; i<dec; i++){
137                 cents = "0" + cents;
138         }
139         for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
140                 num = num.substring(0,num.length-(4*i+3))+user.ts+
141                         num.substring(num.length-(4*i+3));
142          num = ((sign)?'':'-') + num;
143          if(dec!=0) num = num + user.ds + cents;
144         if(label)
145             document.getElementById(post).innerHTML = num;
146         else
147             document.getElementsByName(post)[0].value = num;
148         }
149         function get_amount(doc, label) {
150             if(label)
151                 var val = document.getElementById(doc).innerHTML;
152             else
153                 var val = document.getElementsByName(doc)[0].value;
154                 val = val.replace(new RegExp('\\'+user.ts, 'g'),'');
155                 val = val.replace(new RegExp('\\'+user.ds, 'g'),'.');
156                 return 1*val;
157         }
158
159 function goBack() {
160         if (window.history.length <= 1)
161          window.close();
162         else
163          window.history.go(-1);
164 }
165