2006-04-05 16:58:57 -04:00
|
|
|
|
/* Simple AJAX Code-Kit (SACK) v1.6.1 */
|
2005-08-27 14:48:56 -04:00
|
|
|
|
/* <20>2005 Gregory Wild-Smith */
|
|
|
|
|
/* www.twilightuniverse.com */
|
2006-04-05 16:58:57 -04:00
|
|
|
|
/* Software licenced under a modified X11 licence,
|
|
|
|
|
see documentation or authors website for more details */
|
2005-08-27 14:48:56 -04:00
|
|
|
|
|
2006-04-05 16:58:57 -04:00
|
|
|
|
function sack(file) {
|
|
|
|
|
this.xmlhttp = null;
|
2005-08-27 14:48:56 -04:00
|
|
|
|
|
2006-04-05 16:58:57 -04:00
|
|
|
|
this.resetData = function() {
|
|
|
|
|
this.method = "POST";
|
|
|
|
|
this.queryStringSeparator = "?";
|
|
|
|
|
this.argumentSeparator = "&";
|
|
|
|
|
this.URLString = "";
|
|
|
|
|
this.encodeURIString = true;
|
|
|
|
|
this.execute = false;
|
|
|
|
|
this.element = null;
|
|
|
|
|
this.elementObj = null;
|
|
|
|
|
this.requestFile = file;
|
|
|
|
|
this.vars = new Object();
|
|
|
|
|
this.responseStatus = new Array(2);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.resetFunctions = function() {
|
|
|
|
|
this.onLoading = function() { };
|
|
|
|
|
this.onLoaded = function() { };
|
|
|
|
|
this.onInteractive = function() { };
|
|
|
|
|
this.onCompletion = function() { };
|
|
|
|
|
this.onError = function() { };
|
|
|
|
|
this.onFail = function() { };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.reset = function() {
|
|
|
|
|
this.resetFunctions();
|
|
|
|
|
this.resetData();
|
|
|
|
|
};
|
2005-08-27 14:48:56 -04:00
|
|
|
|
|
|
|
|
|
this.createAJAX = function() {
|
|
|
|
|
try {
|
|
|
|
|
this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
|
2006-04-05 16:58:57 -04:00
|
|
|
|
} catch (e1) {
|
2005-08-27 14:48:56 -04:00
|
|
|
|
try {
|
|
|
|
|
this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
|
2006-04-05 16:58:57 -04:00
|
|
|
|
} catch (e2) {
|
2005-08-27 14:48:56 -04:00
|
|
|
|
this.xmlhttp = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-04-05 16:58:57 -04:00
|
|
|
|
|
|
|
|
|
if (! this.xmlhttp) {
|
|
|
|
|
if (typeof XMLHttpRequest != "undefined") {
|
|
|
|
|
this.xmlhttp = new XMLHttpRequest();
|
|
|
|
|
} else {
|
|
|
|
|
this.failed = true;
|
|
|
|
|
}
|
2005-08-27 14:48:56 -04:00
|
|
|
|
}
|
|
|
|
|
};
|
2006-02-12 02:53:23 -05:00
|
|
|
|
|
2005-08-27 14:48:56 -04:00
|
|
|
|
this.setVar = function(name, value){
|
2006-04-05 16:58:57 -04:00
|
|
|
|
this.vars[name] = Array(value, false);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.encVar = function(name, value, returnvars) {
|
|
|
|
|
if (true == returnvars) {
|
|
|
|
|
return Array(encodeURIComponent(name), encodeURIComponent(value));
|
2005-08-27 14:48:56 -04:00
|
|
|
|
} else {
|
2006-04-05 16:58:57 -04:00
|
|
|
|
this.vars[encodeURIComponent(name)] = Array(encodeURIComponent(value), true);
|
2005-08-27 14:48:56 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2006-02-12 02:53:23 -05:00
|
|
|
|
|
2006-04-05 16:58:57 -04:00
|
|
|
|
this.processURLString = function(string, encode) {
|
|
|
|
|
encoded = encodeURIComponent(this.argumentSeparator);
|
|
|
|
|
regexp = new RegExp(this.argumentSeparator + "|" + encoded);
|
|
|
|
|
varArray = string.split(regexp);
|
|
|
|
|
for (i = 0; i < varArray.length; i++){
|
|
|
|
|
urlVars = varArray[i].split("=");
|
|
|
|
|
if (true == encode){
|
|
|
|
|
this.encVar(urlVars[0], urlVars[1]);
|
|
|
|
|
} else {
|
|
|
|
|
this.setVar(urlVars[0], urlVars[1]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2005-08-27 14:48:56 -04:00
|
|
|
|
}
|
2006-02-12 02:53:23 -05:00
|
|
|
|
|
2006-04-05 16:58:57 -04:00
|
|
|
|
this.createURLString = function(urlstring) {
|
|
|
|
|
if (this.encodeURIString && this.URLString.length) {
|
|
|
|
|
this.processURLString(this.URLString, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (urlstring) {
|
|
|
|
|
if (this.URLString.length) {
|
|
|
|
|
this.URLString += this.argumentSeparator + urlstring;
|
|
|
|
|
} else {
|
|
|
|
|
this.URLString = urlstring;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// prevents caching of URLString
|
|
|
|
|
this.setVar("rndval", new Date().getTime());
|
|
|
|
|
|
|
|
|
|
urlstringtemp = new Array();
|
|
|
|
|
for (key in this.vars) {
|
|
|
|
|
if (false == this.vars[key][1] && true == this.encodeURIString) {
|
|
|
|
|
encoded = this.encVar(key, this.vars[key][0], true);
|
|
|
|
|
delete this.vars[key];
|
|
|
|
|
this.vars[encoded[0]] = Array(encoded[1], true);
|
|
|
|
|
key = encoded[0];
|
2005-08-27 14:48:56 -04:00
|
|
|
|
}
|
2006-04-05 16:58:57 -04:00
|
|
|
|
|
|
|
|
|
urlstringtemp[urlstringtemp.length] = key + "=" + this.vars[key][0];
|
|
|
|
|
}
|
|
|
|
|
if (urlstring){
|
|
|
|
|
this.URLString += this.argumentSeparator + urlstringtemp.join(this.argumentSeparator);
|
|
|
|
|
} else {
|
|
|
|
|
this.URLString += urlstringtemp.join(this.argumentSeparator);
|
2005-08-27 14:48:56 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2006-02-12 02:53:23 -05:00
|
|
|
|
|
2006-04-05 16:58:57 -04:00
|
|
|
|
this.runResponse = function() {
|
2005-08-27 14:48:56 -04:00
|
|
|
|
eval(this.response);
|
|
|
|
|
}
|
2006-02-12 02:53:23 -05:00
|
|
|
|
|
2006-04-05 16:58:57 -04:00
|
|
|
|
this.runAJAX = function(urlstring) {
|
|
|
|
|
if (this.failed) {
|
|
|
|
|
this.onFail();
|
2005-08-27 14:48:56 -04:00
|
|
|
|
} else {
|
2006-04-05 16:58:57 -04:00
|
|
|
|
this.createURLString(urlstring);
|
|
|
|
|
if (this.element) {
|
|
|
|
|
this.elementObj = document.getElementById(this.element);
|
2005-08-27 14:48:56 -04:00
|
|
|
|
}
|
|
|
|
|
if (this.xmlhttp) {
|
|
|
|
|
var self = this;
|
|
|
|
|
if (this.method == "GET") {
|
2006-04-05 16:58:57 -04:00
|
|
|
|
totalurlstring = this.requestFile + this.queryStringSeparator + this.URLString;
|
2005-08-27 14:48:56 -04:00
|
|
|
|
this.xmlhttp.open(this.method, totalurlstring, true);
|
|
|
|
|
} else {
|
|
|
|
|
this.xmlhttp.open(this.method, this.requestFile, true);
|
2006-04-05 16:58:57 -04:00
|
|
|
|
try {
|
|
|
|
|
this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
|
} catch (e) { }
|
2005-08-27 14:48:56 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.xmlhttp.onreadystatechange = function() {
|
2006-04-05 16:58:57 -04:00
|
|
|
|
switch (self.xmlhttp.readyState) {
|
2005-08-27 14:48:56 -04:00
|
|
|
|
case 1:
|
|
|
|
|
self.onLoading();
|
2006-04-05 16:58:57 -04:00
|
|
|
|
break;
|
2005-08-27 14:48:56 -04:00
|
|
|
|
case 2:
|
|
|
|
|
self.onLoaded();
|
2006-04-05 16:58:57 -04:00
|
|
|
|
break;
|
2005-08-27 14:48:56 -04:00
|
|
|
|
case 3:
|
|
|
|
|
self.onInteractive();
|
2006-04-05 16:58:57 -04:00
|
|
|
|
break;
|
2005-08-27 14:48:56 -04:00
|
|
|
|
case 4:
|
|
|
|
|
self.response = self.xmlhttp.responseText;
|
|
|
|
|
self.responseXML = self.xmlhttp.responseXML;
|
|
|
|
|
self.responseStatus[0] = self.xmlhttp.status;
|
|
|
|
|
self.responseStatus[1] = self.xmlhttp.statusText;
|
2006-04-05 16:58:57 -04:00
|
|
|
|
|
|
|
|
|
if (self.execute) {
|
|
|
|
|
self.runResponse();
|
|
|
|
|
}
|
|
|
|
|
|
2005-08-27 14:48:56 -04:00
|
|
|
|
if (self.elementObj) {
|
2006-04-05 16:58:57 -04:00
|
|
|
|
elemNodeName = self.elementObj.nodeName;
|
2005-08-27 14:48:56 -04:00
|
|
|
|
elemNodeName.toLowerCase();
|
2006-04-05 16:58:57 -04:00
|
|
|
|
if (elemNodeName == "input"
|
|
|
|
|
|| elemNodeName == "select"
|
|
|
|
|
|| elemNodeName == "option"
|
|
|
|
|
|| elemNodeName == "textarea") {
|
2005-08-27 14:48:56 -04:00
|
|
|
|
self.elementObj.value = self.response;
|
|
|
|
|
} else {
|
|
|
|
|
self.elementObj.innerHTML = self.response;
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-04-05 16:58:57 -04:00
|
|
|
|
if (self.responseStatus[0] == "200") {
|
|
|
|
|
self.onCompletion();
|
|
|
|
|
} else {
|
|
|
|
|
self.onError();
|
|
|
|
|
}
|
|
|
|
|
|
2005-08-27 14:48:56 -04:00
|
|
|
|
self.URLString = "";
|
2006-04-05 16:58:57 -04:00
|
|
|
|
break;
|
2005-08-27 14:48:56 -04:00
|
|
|
|
}
|
|
|
|
|
};
|
2006-04-05 16:58:57 -04:00
|
|
|
|
|
|
|
|
|
this.xmlhttp.send(this.URLString);
|
2005-08-27 14:48:56 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2006-04-05 16:58:57 -04:00
|
|
|
|
|
|
|
|
|
this.reset();
|
|
|
|
|
this.createAJAX();
|
|
|
|
|
}
|