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 */
9 this.resetData = function() {
11 this.queryStringSeparator = "?";
12 this.argumentSeparator = "&";
14 this.encodeURIString = true;
17 this.elementObj = null;
18 this.requestFile = file;
19 this.vars = new Object();
20 this.responseStatus = new Array(2);
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() { };
32 this.reset = function() {
33 this.resetFunctions();
37 this.createAJAX = function() {
39 this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
42 this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
49 if (typeof XMLHttpRequest != "undefined") {
50 this.xmlhttp = new XMLHttpRequest();
57 this.setVar = function(name, value){
58 this.vars[name] = Array(value, false);
61 this.encVar = function(name, value, returnvars) {
62 if (true == returnvars) {
63 return Array(encodeURIComponent(name), encodeURIComponent(value));
65 this.vars[encodeURIComponent(name)] = Array(encodeURIComponent(value), true);
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("=");
76 this.encVar(urlVars[0], urlVars[1]);
78 this.setVar(urlVars[0], urlVars[1]);
83 this.createURLString = function(urlstring) {
84 if (this.encodeURIString && this.URLString.length) {
85 this.processURLString(this.URLString, true);
88 if (this.URLString.length) {
89 this.URLString += this.argumentSeparator + urlstring;
91 this.URLString = urlstring;
95 // prevents caching of URLString
96 this.setVar("rndval", new Date().getTime());
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);
107 urlstringtemp[urlstringtemp.length] = key + "=" + this.vars[key][0];
110 this.URLString += this.argumentSeparator + urlstringtemp.join(this.argumentSeparator);
112 this.URLString += urlstringtemp.join(this.argumentSeparator);
116 this.runResponse = function() {
120 this.runAJAX = function(urlstring) {
124 this.createURLString(urlstring);
127 this.elementObj = document.getElementById(this.element);
132 if (this.method == "GET") {
133 totalurlstring = this.requestFile + this.queryStringSeparator + this.URLString;
134 this.xmlhttp.open(this.method, totalurlstring, true);
136 this.xmlhttp.open(this.method, this.requestFile, true);
138 this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
141 //document.myForm.action=urlstring;
142 //document.myForm.submit();
143 this.xmlhttp.onreadystatechange = function() {
144 switch (self.xmlhttp.readyState) {
152 self.onInteractive();
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;
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;
173 self.elementObj.innerHTML = self.response;
176 if (self.responseStatus[0] == "200") {
186 this.xmlhttp.send(this.URLString);