Projects : mp-wp : mp-wp_genesis
1 | /* Simple AJAX Code-Kit (SACK) v1.6.1 */ |
2 | /* www.twilightuniverse.com */ |
3 | /* Software licenced under a modified X11 licence, |
4 | see documentation or authors website for more details */ |
5 | |
6 | function sack(file) { |
7 | this.xmlhttp = null; |
8 | |
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 | |
88 | if (urlstring) { |
89 | if (this.URLString.length) { |
90 | this.URLString += this.argumentSeparator + urlstring; |
91 | } else { |
92 | this.URLString = urlstring; |
93 | } |
94 | } |
95 | |
96 | // prevents caching of URLString |
97 | this.setVar("rndval", new Date().getTime()); |
98 | |
99 | urlstringtemp = new Array(); |
100 | for (key in this.vars) { |
101 | if (false == this.vars[key][1] && true == this.encodeURIString) { |
102 | encoded = this.encVar(key, this.vars[key][0], true); |
103 | delete this.vars[key]; |
104 | this.vars[encoded[0]] = Array(encoded[1], true); |
105 | key = encoded[0]; |
106 | } |
107 | |
108 | urlstringtemp[urlstringtemp.length] = key + "=" + this.vars[key][0]; |
109 | } |
110 | if (urlstring){ |
111 | this.URLString += this.argumentSeparator + urlstringtemp.join(this.argumentSeparator); |
112 | } else { |
113 | this.URLString += urlstringtemp.join(this.argumentSeparator); |
114 | } |
115 | } |
116 | |
117 | this.runResponse = function() { |
118 | eval(this.response); |
119 | } |
120 | |
121 | this.runAJAX = function(urlstring) { |
122 | if (this.failed) { |
123 | this.onFail(); |
124 | } else { |
125 | this.createURLString(urlstring); |
126 | if (this.element) { |
127 | this.elementObj = document.getElementById(this.element); |
128 | } |
129 | if (this.xmlhttp) { |
130 | var self = this; |
131 | if (this.method == "GET") { |
132 | totalurlstring = this.requestFile + this.queryStringSeparator + this.URLString; |
133 | this.xmlhttp.open(this.method, totalurlstring, true); |
134 | } else { |
135 | this.xmlhttp.open(this.method, this.requestFile, true); |
136 | try { |
137 | this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded") |
138 | } catch (e) { } |
139 | } |
140 | |
141 | this.xmlhttp.onreadystatechange = function() { |
142 | switch (self.xmlhttp.readyState) { |
143 | case 1: |
144 | self.onLoading(); |
145 | break; |
146 | case 2: |
147 | self.onLoaded(); |
148 | break; |
149 | case 3: |
150 | self.onInteractive(); |
151 | break; |
152 | case 4: |
153 | self.response = self.xmlhttp.responseText; |
154 | self.responseXML = self.xmlhttp.responseXML; |
155 | self.responseStatus[0] = self.xmlhttp.status; |
156 | self.responseStatus[1] = self.xmlhttp.statusText; |
157 | |
158 | if (self.execute) { |
159 | self.runResponse(); |
160 | } |
161 | |
162 | if (self.elementObj) { |
163 | elemNodeName = self.elementObj.nodeName; |
164 | elemNodeName.toLowerCase(); |
165 | if (elemNodeName == "input" |
166 | || elemNodeName == "select" |
167 | || elemNodeName == "option" |
168 | || elemNodeName == "textarea") { |
169 | self.elementObj.value = self.response; |
170 | } else { |
171 | self.elementObj.innerHTML = self.response; |
172 | } |
173 | } |
174 | if (self.responseStatus[0] == "200") { |
175 | self.onCompletion(); |
176 | } else { |
177 | self.onError(); |
178 | } |
179 | |
180 | self.URLString = ""; |
181 | break; |
182 | } |
183 | }; |
184 | |
185 | this.xmlhttp.send(this.URLString); |
186 | } |
187 | } |
188 | }; |
189 | |
190 | this.reset(); |
191 | this.createAJAX(); |
192 | } |