Separated ajax search box features from core ajax framework.
[lhc/web/wiklou.git] / skins / common / ajax.js
1 // remote scripting library
2 // (c) copyright 2005 modernmethod, inc
3 var sajax_debug_mode = false;
4 var sajax_request_type = "GET";
5
6 function sajax_debug(text) {
7 if (sajax_debug_mode)
8 alert("RSD: " + text)
9 }
10
11 function sajax_init_object() {
12 sajax_debug("sajax_init_object() called..")
13 var A;
14 try {
15 A=new ActiveXObject("Msxml2.XMLHTTP");
16 } catch (e) {
17 try {
18 A=new ActiveXObject("Microsoft.XMLHTTP");
19 } catch (oc) {
20 A=null;
21 }
22 }
23 if(!A && typeof XMLHttpRequest != "undefined")
24 A = new XMLHttpRequest();
25 if (!A)
26 sajax_debug("Could not create connection object.");
27 return A;
28 }
29
30
31 function sajax_do_call(func_name, args) {
32 var i, x, n;
33 var uri;
34 var post_data;
35 uri = wgServer + "/" + wgScriptPath + "/index.php?action=ajax";
36 if (sajax_request_type == "GET") {
37 if (uri.indexOf("?") == -1)
38 uri = uri + "?rs=" + escape(func_name);
39 else
40 uri = uri + "&rs=" + escape(func_name);
41 for (i = 0; i < args.length-1; i++)
42 uri = uri + "&rsargs[]=" + escape(args[i]);
43 //uri = uri + "&rsrnd=" + new Date().getTime();
44 post_data = null;
45 } else {
46 post_data = "rs=" + escape(func_name);
47 for (i = 0; i < args.length-1; i++)
48 post_data = post_data + "&rsargs[]=" + escape(args[i]);
49 }
50 x = sajax_init_object();
51 x.open(sajax_request_type, uri, true);
52 if (sajax_request_type == "POST") {
53 x.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
54 x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
55 }
56 x.setRequestHeader("Pragma", "cache=yes");
57 x.setRequestHeader("Cache-Control", "no-transform");
58 x.onreadystatechange = function() {
59 if (x.readyState != 4)
60 return;
61 sajax_debug("received " + x.responseText);
62 var status;
63 var data;
64 status = x.responseText.charAt(0);
65 data = x.responseText.substring(2);
66 if (status == "-")
67 alert("Error: " + data);
68 else
69 args[args.length-1](data);
70 }
71 x.send(post_data);
72 sajax_debug(func_name + " uri = " + uri + "/post = " + post_data);
73 sajax_debug(func_name + " waiting..");
74 delete x;
75 }