initial import from onelab svn codebase
[plewww.git] / planetlab / includes / js / bsn.Ajax.js
1 /**
2  *  author:             Timothy Groves - http://www.brandspankingnew.net
3  *      version:        1.0 - 2006-08-04
4  *
5  *      requires:       nothing
6  *
7  */
8
9 var useBSNns;
10
11 if (useBSNns)
12 {
13         if (typeof(bsn) == "undefined")
14                 bsn = {}
15         _bsn = bsn;
16 }
17 else
18 {
19         _bsn = this;
20 }
21
22
23
24
25
26
27
28
29
30 _bsn.Ajax = function ()
31 {
32         this.req = {};
33         this.isIE = false;
34 }
35
36
37
38 _bsn.Ajax.prototype.makeRequest = function (url, meth, onComp, onErr)
39 {
40         
41         if (meth != "POST")
42                 meth = "GET";
43         
44         this.onComplete = onComp;
45         this.onError = onErr;
46         
47         var pointer = this;
48         
49         // branch for native XMLHttpRequest object
50         if (window.XMLHttpRequest)
51         {
52                 this.req = new XMLHttpRequest();
53                 this.req.onreadystatechange = function () { pointer.processReqChange() };
54                 this.req.open("GET", url, true); //
55                 this.req.send(null);
56         // branch for IE/Windows ActiveX version
57         }
58         else if (window.ActiveXObject)
59         {
60                 this.req = new ActiveXObject("Microsoft.XMLHTTP");
61                 if (this.req)
62                 {
63                         this.req.onreadystatechange = function () { pointer.processReqChange() };
64                         this.req.open(meth, url, true);
65                         this.req.send();
66                 }
67         }
68 }
69
70
71 _bsn.Ajax.prototype.processReqChange = function()
72 {
73         
74         // only if req shows "loaded"
75         if (this.req.readyState == 4) {
76                 // only if "OK"
77                 if (this.req.status == 200)
78                 {
79                         this.onComplete( this.req );
80                 } else {
81                         this.onError( this.req.status );
82                 }
83         }
84 }