Add support for multiple fomrs on the client side in case of TARGET_J2EE portal support.
[mono.git] / mcs / class / System.Web / resources / callback.js
1 function WebForm_DoCallback (id, arg, callback, ctx, errorCallback)
2 {
3         var myForm = WebForm_GetFormFromCtrl (id);
4         var qs = WebForm_getFormData (myForm) + "&__CALLBACKTARGET=" + id + "&&__CALLBACKARGUMENT=" + escape(arg);
5         // WebForm_httpPost (myForm.serverURL, qs, function (httpPost) { WebForm_ClientCallback (httpPost, ctx, callback, errorCallback); });
6         WebForm_httpPost (document.URL, qs, function (httpPost) { WebForm_ClientCallback (httpPost, ctx, callback, errorCallback); });
7 }
8
9 function WebForm_ClientCallback (httpPost, ctx, callback, errorCallback)
10 {
11         try {
12                 var doc = httpPost.responseText;
13         } catch (e) {
14                 if (errorCallback != null)
15                         errorCallback (httpPost.responseText, ctx);
16                 return;
17         }
18         callback (doc, ctx);
19 }
20
21 function WebForm_getFormData (theForm)
22 {
23         var qs = "";
24         var len = theForm.elements.length;
25         for (n=0; n<len; n++) {
26                 var elem = theForm.elements [n];
27                 if (qs.length > 0) qs += "&";
28                 qs += elem.name + "=" + encodeURIComponent (elem.value);
29         }
30         return qs;
31 }
32
33 var axName = null;
34 function WebForm_httpPost (url, data, callback)
35 {
36         var httpPost = null;
37         
38         if (typeof XMLHttpRequest != "undefined") {
39                 httpPost = new XMLHttpRequest ();
40         } else {
41                 if (axName != null)
42                         httpPost = new ActiveXObject (axName);
43                 else {
44                         var clsnames = new Array ("MSXML", "MSXML2", "MSXML3", "Microsoft");
45                         for (n = 0; n < clsnames.length && httpPost == null; n++) {
46                                 axName = clsnames [n] + ".XMLHTTP";
47                                 try {
48                                         httpPost = new ActiveXObject (axName);
49                                 } catch (e) { axName = null; }
50                         }
51                         if (httpPost == null)
52                                 throw new Error ("XMLHTTP object could not be created.");
53                 }
54         }
55         httpPost.onreadystatechange = function () { if (httpPost.readyState == 4) callback (httpPost); };
56         
57         httpPost.open ("POST", url, true);      // async
58         httpPost.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
59         setTimeout (function () { httpPost.send (data); }, 10);
60 }