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