Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / webapp / static / fusion / raptor / js / tree / ajax.js
1 /* Simple AJAX Code-Kit (SACK) v1.6.1 */
2 /* ©2005 Gregory Wild-Smith */
3 /* www.twilightuniverse.com */
4 /* Software licenced under a modified X11 licence,
5    see documentation or authors website for more details */
6
7 function sack(file) {
8         this.xmlhttp = null;
9         this.resetData = function() {
10                 this.method = "POST";
11                 this.queryStringSeparator = "?";
12                 this.argumentSeparator = "&";
13                 this.URLString = "";
14                 this.encodeURIString = true;
15                 this.execute = false;
16                 this.element = null;
17                 this.elementObj = null;
18                 this.requestFile = file;
19                 this.vars = new Object();
20                 this.responseStatus = new Array(2);
21         };
22
23         this.resetFunctions = function() {
24                 this.onLoading = function() { };
25                 this.onLoaded = function() { };
26                 this.onInteractive = function() { };
27                 this.onCompletion = function() { };
28                 this.onError = function() { };
29                 this.onFail = function() { };
30         };
31
32         this.reset = function() {
33                 this.resetFunctions();
34                 this.resetData();
35         };
36
37         this.createAJAX = function() {
38                 try {
39                         this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
40                 } catch (e1) {
41                         try {
42                                 this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
43                         } catch (e2) {
44                                 this.xmlhttp = null;
45                         }
46                 }
47
48                 if (! this.xmlhttp) {
49                         if (typeof XMLHttpRequest != "undefined") {
50                                 this.xmlhttp = new XMLHttpRequest();
51                         } else {
52                                 this.failed = true;
53                         }
54                 }
55         };
56
57         this.setVar = function(name, value){
58                 this.vars[name] = Array(value, false);
59         };
60
61         this.encVar = function(name, value, returnvars) {
62                 if (true == returnvars) {
63                         return Array(encodeURIComponent(name), encodeURIComponent(value));
64                 } else {
65                         this.vars[encodeURIComponent(name)] = Array(encodeURIComponent(value), true);
66                 }
67         }
68
69         this.processURLString = function(string, encode) {
70                 encoded = encodeURIComponent(this.argumentSeparator);
71                 regexp = new RegExp(this.argumentSeparator + "|" + encoded);
72                 varArray = string.split(regexp);
73                 for (i = 0; i < varArray.length; i++){
74                         urlVars = varArray[i].split("=");
75                         if (true == encode){
76                                 this.encVar(urlVars[0], urlVars[1]);
77                         } else {
78                                 this.setVar(urlVars[0], urlVars[1]);
79                         }
80                 }
81         }
82
83         this.createURLString = function(urlstring) {
84                 if (this.encodeURIString && this.URLString.length) {
85                         this.processURLString(this.URLString, true);
86                 }
87                 if (urlstring) {
88                         if (this.URLString.length) {
89                                 this.URLString += this.argumentSeparator + urlstring;
90                         } else {
91                                 this.URLString = urlstring;
92                         }
93                 }
94
95                 // prevents caching of URLString
96                 this.setVar("rndval", new Date().getTime());
97
98                 urlstringtemp = new Array();
99                 for (key in this.vars) {
100                         if (false == this.vars[key][1] && true == this.encodeURIString) {
101                                 encoded = this.encVar(key, this.vars[key][0], true);
102                                 delete this.vars[key];
103                                 this.vars[encoded[0]] = Array(encoded[1], true);
104                                 key = encoded[0];
105                         }
106
107                         urlstringtemp[urlstringtemp.length] = key + "=" + this.vars[key][0];
108                 }
109                 if (urlstring){
110                         this.URLString += this.argumentSeparator + urlstringtemp.join(this.argumentSeparator);
111                 } else {
112                         this.URLString += urlstringtemp.join(this.argumentSeparator);
113                 }               
114         }
115
116         this.runResponse = function() {
117                 eval(this.response);
118         }
119
120         this.runAJAX = function(urlstring) {
121                 if (this.failed) {
122                         this.onFail();
123                 } else {
124                         this.createURLString(urlstring);
125                                 
126                         if (this.element) {
127                                 this.elementObj = document.getElementById(this.element);
128                         }
129                         
130                         if (this.xmlhttp) {
131                                 var self = this;
132                                 if (this.method == "GET") {
133                                         totalurlstring = this.requestFile + this.queryStringSeparator + this.URLString;
134                                         this.xmlhttp.open(this.method, totalurlstring, true);
135                                 } else {
136                                         this.xmlhttp.open(this.method, this.requestFile, true);
137                                         try {
138                                                 this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
139                                         } catch (e) { }
140                                 }
141                                 //document.myForm.action=urlstring;
142                                 //document.myForm.submit();
143                                 this.xmlhttp.onreadystatechange = function() {
144                                         switch (self.xmlhttp.readyState) {
145                                                 case 1:
146                                                         self.onLoading();
147                                                         break;
148                                                 case 2:
149                                                         self.onLoaded();
150                                                         break;
151                                                 case 3:
152                                                         self.onInteractive();
153                                                         break;
154                                                 case 4:
155                                                         self.response = self.xmlhttp.responseText;
156                                                         self.responseXML = self.xmlhttp.responseXML;
157                                                         self.responseStatus[0] = self.xmlhttp.status;
158                                                         self.responseStatus[1] = self.xmlhttp.statusText;
159
160                                                         if (self.execute) {
161                                                                 self.runResponse();
162                                                         }
163
164                                                         if (self.elementObj) {
165                                                                 elemNodeName = self.elementObj.nodeName;
166                                                                 elemNodeName.toLowerCase();
167                                                                 if (elemNodeName == "input"
168                                                                 || elemNodeName == "select"
169                                                                 || elemNodeName == "option"
170                                                                 || elemNodeName == "textarea") {
171                                                                         self.elementObj.value = self.response;
172                                                                 } else {
173                                                                         self.elementObj.innerHTML = self.response;
174                                                                 }
175                                                         }
176                                                         if (self.responseStatus[0] == "200") {
177                                                                 self.onCompletion();
178                                                         } else {
179                                                                 self.onError();
180                                                         }
181
182                                                         self.URLString = "";
183                                                         break;
184                                         }
185                                 };
186                                 this.xmlhttp.send(this.URLString);
187                                 
188                         }
189                 }
190         };
191
192         this.reset() ;
193         this.createAJAX();
194 }