1/* Simple AJAX Code-Kit (SACK) v1.6 */
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
7function 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	this.setVar = function(name, value){
59		this.vars[name] = Array(value, false);
60	};
61
62	this.encVar = function(name, value, returnvars) {
63		if (true == returnvars) {
64			return Array(encodeURIComponent(name), encodeURIComponent(value));
65		} else {
66			this.vars[encodeURIComponent(name)] = Array(encodeURIComponent(value), true);
67		}
68	};
69
70	this.processURLString = function(string, encode) {
71		encoded = encodeURIComponent(this.argumentSeparator);
72		regexp = new RegExp(this.argumentSeparator + "|" + encoded);
73		varArray = string.split(regexp);
74		for (i = 0; i < varArray.length; i++){
75			urlVars = varArray[i].split("=");
76			if (true == encode){
77				this.encVar(urlVars[0], urlVars[1]);
78			} else {
79				this.setVar(urlVars[0], urlVars[1]);
80			}
81		}
82	};
83
84	this.createURLString = function(urlstring) {
85		if (this.encodeURIString && this.URLString.length) {
86			this.processURLString(this.URLString, true);
87		}
88
89		if (urlstring) {
90			if (this.URLString.length) {
91				this.URLString += this.argumentSeparator + urlstring;
92			} else {
93				this.URLString = urlstring;
94			}
95		}
96
97		// prevents caching of URLString
98		this.setVar("rndval", new Date().getTime());
99
100		urlstringtemp = new Array();
101		for (key in this.vars) {
102			if (false == this.vars[key][1] && true == this.encodeURIString) {
103				encoded = this.encVar(key, this.vars[key][0], true);
104				delete this.vars[key];
105				this.vars[encoded[0]] = Array(encoded[1], true);
106				key = encoded[0];
107			}
108
109			urlstringtemp[urlstringtemp.length] = key + "=" + this.vars[key][0];
110		}
111		this.URLString += urlstringtemp.join(this.argumentSeparator);
112	};
113
114	this.runResponse = function() {
115		eval(this.response);
116	};
117
118	this.runAJAX = function(urlstring) {
119		if (this.failed) {
120			this.onFail();
121		} else {
122			this.createURLString(urlstring);
123			if (this.element) {
124				this.elementObj = document.getElementById(this.element);
125			}
126			if (this.xmlhttp) {
127				var self = this;
128				if (this.method == "GET") {
129					totalurlstring = this.requestFile + this.queryStringSeparator + this.URLString;
130					this.xmlhttp.open(this.method, totalurlstring, true);
131				} else {
132					this.xmlhttp.open(this.method, this.requestFile, true);
133					try {
134						this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
135					} catch (e) { }
136				}
137
138				this.xmlhttp.onreadystatechange = function() {
139					switch (self.xmlhttp.readyState) {
140						case 1:
141							self.onLoading();
142							break;
143						case 2:
144							self.onLoaded();
145							break;
146						case 3:
147							self.onInteractive();
148							break;
149						case 4:
150							self.response = self.xmlhttp.responseText;
151							self.responseXML = self.xmlhttp.responseXML;
152							try {
153							   self.responseStatus[0] = self.xmlhttp.status;
154							} catch(e) {
155							   self.responseStatus[0] = -1;
156							}
157							try {
158							    self.responseStatus[1] = self.xmlhttp.statusText;
159							} catch(e) {
160							    self.responseStatus[1] = e;
161							}
162
163							if (self.execute) {
164								self.runResponse();
165							}
166
167							if (self.elementObj) {
168								elemNodeName = self.elementObj.nodeName;
169								elemNodeName.toLowerCase();
170								if (elemNodeName == "input"
171								|| elemNodeName == "select"
172								|| elemNodeName == "option"
173								|| elemNodeName == "textarea") {
174									self.elementObj.value = self.response;
175								} else {
176									self.elementObj.innerHTML = self.response;
177								}
178							}
179							if (self.responseStatus[0] == "200") {
180								self.onCompletion();
181							} else {
182								self.onError();
183							}
184
185							self.URLString = "";
186							break;
187					}
188				};
189
190				this.xmlhttp.send(this.URLString);
191			}
192		}
193	};
194
195	this.reset();
196	this.createAJAX();
197}
198