PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/TinyAjax.js

https://github.com/patadejaguar/S.A.F.E.-Open-Source-Microfinance-Suite
JavaScript | 432 lines | 422 code | 8 blank | 2 comment | 15 complexity | 187887cec934fcba7d4cb31f8aaceb92 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-2.0, GPL-3.0, BSD-3-Clause
  1. var form_data = '';
  2. function urlDecode( encoded )
  3. {
  4. var HEXCHARS = "0123456789ABCDEFabcdef";
  5. var plaintext = "";
  6. var i = 0;
  7. while (i < encoded.length) {
  8. var ch = encoded.charAt(i);
  9. if (ch == "+") {
  10. plaintext += " ";
  11. i++;
  12. } else if (ch == "%") {
  13. if (i < (encoded.length-2)
  14. && HEXCHARS.indexOf(encoded.charAt(i+1)) != -1
  15. && HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
  16. plaintext += unescape( encoded.substr(i,3) );
  17. i += 3;
  18. } else {
  19. plaintext += "%[ERROR]";
  20. i++;
  21. }
  22. } else {
  23. plaintext += ch;
  24. i++;
  25. }
  26. }
  27. return plaintext;
  28. };
  29. function arrayDecode( encoded )
  30. {
  31. var row = encoded.split("~");
  32. var numRows = row.length ;
  33. var arr = new Array(numRows);
  34. for(var x = 0; x < numRows; x++){
  35. var tmp = row[x].split("|");
  36. //MK - FIX ###plus###
  37. for(var y = 0; y < arr.length; y++){
  38. //tmp[y] = urlDecode(tmp[y]);
  39. tmp[y] = decodeSpecialChars(tmp[y]);
  40. }
  41. arr[x] = tmp;
  42. }
  43. return arr;
  44. }
  45. function decodeSpecialChars(data)
  46. {
  47. s = new String(data);
  48. s = s.replace(/\#\#plus\#\#/g,"+");
  49. s = s.replace(/\#\#backslash\#\#/g,"\\");
  50. s = s.replace(/\#\#pipe\#\#/g,"|");
  51. s = s.replace(/\#\#tilde\#\#/g,"~");
  52. return s;
  53. }
  54. function encodeSpecialChars(data)
  55. {
  56. s = new String(data);
  57. s = s.replace(/\+/g,"##plus##") ;
  58. s = s.replace(/\\/g,"##backslash##") ;
  59. s = s.replace(/\|/g,"##pipe##") ;
  60. s = s.replace(/\~/g,"##tilde##") ;
  61. return s;
  62. //return escape(s);
  63. }
  64. var numLoading = 0;
  65. function loading_show()
  66. {
  67. var loading = document.getElementById('loading');
  68. if (!loading)
  69. {
  70. loading = document.createElement('div');
  71. loading.id = 'loading';
  72. loading.innerHTML = '<font style="font-family:verdana; font-size:12px; color:white;">Loading...</' + 'font>';
  73. loading.style.position = 'absolute';
  74. loading.style.top = '4px';
  75. loading.style.right = '4px';
  76. loading.style.backgroundColor = 'red';
  77. loading.style.width = '65px';
  78. loading.style.padding = '2px';
  79. document.getElementsByTagName('body').item(0).appendChild(loading);
  80. }
  81. loading.style.display = 'block';
  82. numLoading++;
  83. }
  84. function loading_hide()
  85. {
  86. numLoading--;
  87. if(numLoading < 1) {
  88. var loading = document.getElementById('loading');
  89. if (loading) {
  90. loading.style.display = 'none';
  91. }
  92. }
  93. }
  94. function aj_init_object() {
  95. if(use_iframe) {
  96. var xmlhttp = new XMLHttpRequestI();
  97. return xmlhttp;
  98. }
  99. var xmlhttp=false;
  100. /*@cc_on @*/
  101. /*@if (@_jscript_version >= 5)
  102. // JScript gives us Conditional compilation, we can cope with old IE versions.
  103. // and security blocked creation of the objects.
  104. try {
  105. xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  106. } catch (e) {
  107. try {
  108. xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  109. } catch (E) {
  110. xmlhttp = false;
  111. }
  112. }
  113. @end @*/
  114. if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
  115. xmlhttp = new XMLHttpRequest();
  116. } else if(!xmlhttp) {
  117. //IFrame fallback for IE
  118. var xmlhttp = new XMLHttpRequestI();
  119. }
  120. return xmlhttp;
  121. }
  122. function aj_process(data)
  123. {
  124. //alert(data.length);
  125. //print_2d_string_array(data);
  126. for(var x = 0; x < data.length; x++)
  127. aj_process2(data[x]);
  128. }
  129. function aj_call(func_name, args, custom_cb) {
  130. var i, x, n;
  131. var uri;
  132. var post_data;
  133. uri = request_uri;
  134. if (xml_request_type == "GET") {
  135. if (uri.indexOf("?") == -1)
  136. uri = uri + "?rs=" + escape(func_name);
  137. else
  138. uri = uri + "&rs=" + escape(func_name);
  139. for (i = 0; i < args.length; i++) {
  140. if(args[i] == 'post_data') {
  141. uri += form_data;
  142. form_data = '';
  143. } else {
  144. uri = uri + "&rsargs[]=" + args[i];
  145. }
  146. }
  147. uri = uri + "&rsrnd=" + new Date().getTime();
  148. post_data = null;
  149. } else {
  150. post_data = "rs=" + escape(func_name);
  151. for (i = 0; i < args.length; i++) {
  152. if(args[i] == 'post_data') {
  153. post_data += form_data;
  154. form_data = '';
  155. }
  156. post_data = post_data + "&rsargs[]=" + args[i];
  157. }
  158. }
  159. x = aj_init_object();
  160. if(!x) { return true; }
  161. if(show_loading)
  162. loading_show();
  163. //alert(uri);
  164. x.open(xml_request_type, uri, true);
  165. if (xml_request_type == "POST") {
  166. x.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
  167. x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  168. }
  169. x.onreadystatechange = function() {
  170. try {
  171. if (x.readyState != 4) {
  172. return;
  173. }
  174. loading_hide();
  175. if(x.status != 200)
  176. {
  177. console.log('Error invalid status: ' + x.responseText + ' status: ' + x.status);
  178. delete x;
  179. return;
  180. }
  181. } catch ( e ) {
  182. return;
  183. }
  184. var status = x.responseText.charAt(0);
  185. var data = x.responseText.substring(2);
  186. if (status == "-")
  187. {
  188. alert("Callback error: " + data);
  189. delete x;
  190. return;
  191. }
  192. if (custom_cb == undefined ) {
  193. aj_process(arrayDecode(urlDecode(data)));
  194. } else if(custom_cb) {
  195. args[args.length-1]( "" + data);
  196. } else {
  197. setValue(args[args.length-1], data);
  198. }
  199. }
  200. x.send(post_data);
  201. delete x;
  202. return false;
  203. }
  204. /*
  205. coded by Kae - http://verens.com/
  206. use this code as you wish, but retain this notice
  207. MK - notice retained, but renamed function to XMLHttpRequestI and
  208. modified initial timeout
  209. */
  210. XMLHttpRequestI = function() {
  211. var i=0;
  212. var url='';
  213. var responseText='';
  214. this.onreadystatechange=function(){
  215. return false;
  216. }
  217. this.open=function(method,url){
  218. //TODO: POST methods
  219. this.i=++kXHR_instances; // id number of this request
  220. this.url=url;
  221. var iframe = document.createElement('iframe');
  222. iframe.id= 'kXHR_iframe_'+this.i+'';
  223. iframe.type = "text/plain";
  224. iframe.style.display = 'none';
  225. //alert(iframe.id);
  226. document.body.appendChild(iframe);
  227. }
  228. this.send=function(postdata){
  229. //TODO: use the postdata
  230. var el=document.getElementById('kXHR_iframe_'+this.i);
  231. el.src=this.url;
  232. kXHR_objs[this.i]=this;
  233. setTimeout('XMLHttpRequestI_checkState('+this.i+')',200);
  234. }
  235. return true;
  236. }
  237. function XMLHttpRequestI_checkState(inst){
  238. var el=document.getElementById('kXHR_iframe_'+inst);
  239. if(el.readyState=='complete'){
  240. var responseText=window.frames['kXHR_iframe_'+inst].document.body.childNodes[0].data;
  241. kXHR_objs[inst].responseText=responseText;
  242. kXHR_objs[inst].readyState=4;
  243. kXHR_objs[inst].status=200;
  244. kXHR_objs[inst].onreadystatechange();
  245. el.parentNode.removeChild(el);
  246. }else{
  247. setTimeout('XMLHttpRequestI_checkState('+inst+')',500);
  248. }
  249. }
  250. var kXHR_instances=0;
  251. var kXHR_objs=[];
  252. function getValue(element) {
  253. //alert(element);
  254. var itm = document.getElementById(element);
  255. var value = "";
  256. if(itm == null) {
  257. itm = document.getElementsByName(element);
  258. if(itm != null) {
  259. itm = itm[0];
  260. }
  261. }
  262. if(itm != null) {
  263. if(itm.value != undefined) {
  264. value = encodeSpecialChars(itm.value);
  265. } else {
  266. value = encodeSpecialChars(itm.innerHTML);
  267. }
  268. }
  269. if(itm == null) {
  270. return '';
  271. }
  272. if(itm.type != undefined) {
  273. if(itm.type == 'select-one') {
  274. value = encodeSpecialChars(itm[itm.selectedIndex].value);
  275. } else if(itm.type == 'select-multiple') {
  276. value = '';
  277. for (var x = 0; x < itm.length; x++) {
  278. if(itm.options[x].selected) {
  279. value += encodeSpecialChars(itm.options[x].value) + ',';
  280. }
  281. }
  282. if(value.length > 0) {
  283. value = value.substr(0, value.length - 1);
  284. }
  285. } else if(itm.type == 'checkbox') {
  286. if(itm.checked) {
  287. value = encodeSpecialChars(itm.value);
  288. } else {
  289. value = '';
  290. }
  291. } else if(itm.type == 'radio') {
  292. if(itm.checked) {
  293. value = encodeSpecialChars(itm.value);
  294. } else {
  295. value = '';
  296. }
  297. }
  298. }
  299. if(itm.elements != undefined) {
  300. var col = '!COL!';
  301. var row = '!ROW!';
  302. var name;
  303. var first = true;
  304. value = 'post_data';
  305. form_data = '&rsargs[]=';
  306. for(var x = 0; x < itm.elements.length; x++) {
  307. if(!first) {
  308. form_data += row;
  309. }
  310. first = false;
  311. var y = itm.elements[x];
  312. name = '';
  313. if(y.getAttribute('id') != null && y.id != '') {
  314. name = y.id;
  315. }
  316. if(y.getAttribute('name') != null && y.name != '') {
  317. name = y.name;
  318. }
  319. if(y.type == 'select-one') {
  320. form_data += name + col + y[y.selectedIndex].value;
  321. } else if(y.type == 'select-multiple') {
  322. var sel = false;
  323. form_data += name + col;
  324. for (var z = 0; z < y.length; z++) {
  325. if(y.options[z].selected) {
  326. form_data += encodeSpecialChars(y.options[z].value) + ',';
  327. sel = true;
  328. }
  329. }
  330. if(sel) {
  331. form_data = form_data.substr(0, form_data.length - 1);
  332. }
  333. } else if(y.type == 'checkbox') {
  334. if(y.checked) {
  335. form_data += name + col + encodeSpecialChars(y.value);
  336. } else {
  337. first = true;
  338. }
  339. } else if(y.type == 'radio') {
  340. if(y.checked) {
  341. form_data += name + col + encodeSpecialChars(y.value);
  342. } else {
  343. first = true;
  344. }
  345. } else {
  346. form_data += name + col + encodeSpecialChars(y.value);
  347. }
  348. }
  349. }
  350. return value;
  351. }
  352. function setValue(element, data) {
  353. var itm = document.getElementById(element);
  354. var value = "";
  355. if(itm == null) {
  356. itm = document.getElementsByName(element);
  357. if(itm != null) {
  358. itm = itm[0];
  359. }
  360. }
  361. if(itm != null) {
  362. //alert(itm.value);
  363. //alert(itm.type);
  364. if(itm.value != undefined) {
  365. itm.value = data;
  366. } else {
  367. itm.innerHTML = data;
  368. }
  369. }
  370. }