raw
mp-wp_genesis           1 /* Simple AJAX Code-Kit (SACK) v1.6.1 */
mp-wp_genesis 2 /* www.twilightuniverse.com */
mp-wp_genesis 3 /* Software licenced under a modified X11 licence,
mp-wp_genesis 4 see documentation or authors website for more details */
mp-wp_genesis 5
mp-wp_genesis 6 function sack(file) {
mp-wp_genesis 7 this.xmlhttp = null;
mp-wp_genesis 8
mp-wp_genesis 9 this.resetData = function() {
mp-wp_genesis 10 this.method = "POST";
mp-wp_genesis 11 this.queryStringSeparator = "?";
mp-wp_genesis 12 this.argumentSeparator = "&";
mp-wp_genesis 13 this.URLString = "";
mp-wp_genesis 14 this.encodeURIString = true;
mp-wp_genesis 15 this.execute = false;
mp-wp_genesis 16 this.element = null;
mp-wp_genesis 17 this.elementObj = null;
mp-wp_genesis 18 this.requestFile = file;
mp-wp_genesis 19 this.vars = new Object();
mp-wp_genesis 20 this.responseStatus = new Array(2);
mp-wp_genesis 21 };
mp-wp_genesis 22
mp-wp_genesis 23 this.resetFunctions = function() {
mp-wp_genesis 24 this.onLoading = function() { };
mp-wp_genesis 25 this.onLoaded = function() { };
mp-wp_genesis 26 this.onInteractive = function() { };
mp-wp_genesis 27 this.onCompletion = function() { };
mp-wp_genesis 28 this.onError = function() { };
mp-wp_genesis 29 this.onFail = function() { };
mp-wp_genesis 30 };
mp-wp_genesis 31
mp-wp_genesis 32 this.reset = function() {
mp-wp_genesis 33 this.resetFunctions();
mp-wp_genesis 34 this.resetData();
mp-wp_genesis 35 };
mp-wp_genesis 36
mp-wp_genesis 37 this.createAJAX = function() {
mp-wp_genesis 38 try {
mp-wp_genesis 39 this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
mp-wp_genesis 40 } catch (e1) {
mp-wp_genesis 41 try {
mp-wp_genesis 42 this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
mp-wp_genesis 43 } catch (e2) {
mp-wp_genesis 44 this.xmlhttp = null;
mp-wp_genesis 45 }
mp-wp_genesis 46 }
mp-wp_genesis 47
mp-wp_genesis 48 if (! this.xmlhttp) {
mp-wp_genesis 49 if (typeof XMLHttpRequest != "undefined") {
mp-wp_genesis 50 this.xmlhttp = new XMLHttpRequest();
mp-wp_genesis 51 } else {
mp-wp_genesis 52 this.failed = true;
mp-wp_genesis 53 }
mp-wp_genesis 54 }
mp-wp_genesis 55 };
mp-wp_genesis 56
mp-wp_genesis 57 this.setVar = function(name, value){
mp-wp_genesis 58 this.vars[name] = Array(value, false);
mp-wp_genesis 59 };
mp-wp_genesis 60
mp-wp_genesis 61 this.encVar = function(name, value, returnvars) {
mp-wp_genesis 62 if (true == returnvars) {
mp-wp_genesis 63 return Array(encodeURIComponent(name), encodeURIComponent(value));
mp-wp_genesis 64 } else {
mp-wp_genesis 65 this.vars[encodeURIComponent(name)] = Array(encodeURIComponent(value), true);
mp-wp_genesis 66 }
mp-wp_genesis 67 }
mp-wp_genesis 68
mp-wp_genesis 69 this.processURLString = function(string, encode) {
mp-wp_genesis 70 encoded = encodeURIComponent(this.argumentSeparator);
mp-wp_genesis 71 regexp = new RegExp(this.argumentSeparator + "|" + encoded);
mp-wp_genesis 72 varArray = string.split(regexp);
mp-wp_genesis 73 for (i = 0; i < varArray.length; i++){
mp-wp_genesis 74 urlVars = varArray[i].split("=");
mp-wp_genesis 75 if (true == encode){
mp-wp_genesis 76 this.encVar(urlVars[0], urlVars[1]);
mp-wp_genesis 77 } else {
mp-wp_genesis 78 this.setVar(urlVars[0], urlVars[1]);
mp-wp_genesis 79 }
mp-wp_genesis 80 }
mp-wp_genesis 81 }
mp-wp_genesis 82
mp-wp_genesis 83 this.createURLString = function(urlstring) {
mp-wp_genesis 84 if (this.encodeURIString && this.URLString.length) {
mp-wp_genesis 85 this.processURLString(this.URLString, true);
mp-wp_genesis 86 }
mp-wp_genesis 87
mp-wp_genesis 88 if (urlstring) {
mp-wp_genesis 89 if (this.URLString.length) {
mp-wp_genesis 90 this.URLString += this.argumentSeparator + urlstring;
mp-wp_genesis 91 } else {
mp-wp_genesis 92 this.URLString = urlstring;
mp-wp_genesis 93 }
mp-wp_genesis 94 }
mp-wp_genesis 95
mp-wp_genesis 96 // prevents caching of URLString
mp-wp_genesis 97 this.setVar("rndval", new Date().getTime());
mp-wp_genesis 98
mp-wp_genesis 99 urlstringtemp = new Array();
mp-wp_genesis 100 for (key in this.vars) {
mp-wp_genesis 101 if (false == this.vars[key][1] && true == this.encodeURIString) {
mp-wp_genesis 102 encoded = this.encVar(key, this.vars[key][0], true);
mp-wp_genesis 103 delete this.vars[key];
mp-wp_genesis 104 this.vars[encoded[0]] = Array(encoded[1], true);
mp-wp_genesis 105 key = encoded[0];
mp-wp_genesis 106 }
mp-wp_genesis 107
mp-wp_genesis 108 urlstringtemp[urlstringtemp.length] = key + "=" + this.vars[key][0];
mp-wp_genesis 109 }
mp-wp_genesis 110 if (urlstring){
mp-wp_genesis 111 this.URLString += this.argumentSeparator + urlstringtemp.join(this.argumentSeparator);
mp-wp_genesis 112 } else {
mp-wp_genesis 113 this.URLString += urlstringtemp.join(this.argumentSeparator);
mp-wp_genesis 114 }
mp-wp_genesis 115 }
mp-wp_genesis 116
mp-wp_genesis 117 this.runResponse = function() {
mp-wp_genesis 118 eval(this.response);
mp-wp_genesis 119 }
mp-wp_genesis 120
mp-wp_genesis 121 this.runAJAX = function(urlstring) {
mp-wp_genesis 122 if (this.failed) {
mp-wp_genesis 123 this.onFail();
mp-wp_genesis 124 } else {
mp-wp_genesis 125 this.createURLString(urlstring);
mp-wp_genesis 126 if (this.element) {
mp-wp_genesis 127 this.elementObj = document.getElementById(this.element);
mp-wp_genesis 128 }
mp-wp_genesis 129 if (this.xmlhttp) {
mp-wp_genesis 130 var self = this;
mp-wp_genesis 131 if (this.method == "GET") {
mp-wp_genesis 132 totalurlstring = this.requestFile + this.queryStringSeparator + this.URLString;
mp-wp_genesis 133 this.xmlhttp.open(this.method, totalurlstring, true);
mp-wp_genesis 134 } else {
mp-wp_genesis 135 this.xmlhttp.open(this.method, this.requestFile, true);
mp-wp_genesis 136 try {
mp-wp_genesis 137 this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
mp-wp_genesis 138 } catch (e) { }
mp-wp_genesis 139 }
mp-wp_genesis 140
mp-wp_genesis 141 this.xmlhttp.onreadystatechange = function() {
mp-wp_genesis 142 switch (self.xmlhttp.readyState) {
mp-wp_genesis 143 case 1:
mp-wp_genesis 144 self.onLoading();
mp-wp_genesis 145 break;
mp-wp_genesis 146 case 2:
mp-wp_genesis 147 self.onLoaded();
mp-wp_genesis 148 break;
mp-wp_genesis 149 case 3:
mp-wp_genesis 150 self.onInteractive();
mp-wp_genesis 151 break;
mp-wp_genesis 152 case 4:
mp-wp_genesis 153 self.response = self.xmlhttp.responseText;
mp-wp_genesis 154 self.responseXML = self.xmlhttp.responseXML;
mp-wp_genesis 155 self.responseStatus[0] = self.xmlhttp.status;
mp-wp_genesis 156 self.responseStatus[1] = self.xmlhttp.statusText;
mp-wp_genesis 157
mp-wp_genesis 158 if (self.execute) {
mp-wp_genesis 159 self.runResponse();
mp-wp_genesis 160 }
mp-wp_genesis 161
mp-wp_genesis 162 if (self.elementObj) {
mp-wp_genesis 163 elemNodeName = self.elementObj.nodeName;
mp-wp_genesis 164 elemNodeName.toLowerCase();
mp-wp_genesis 165 if (elemNodeName == "input"
mp-wp_genesis 166 || elemNodeName == "select"
mp-wp_genesis 167 || elemNodeName == "option"
mp-wp_genesis 168 || elemNodeName == "textarea") {
mp-wp_genesis 169 self.elementObj.value = self.response;
mp-wp_genesis 170 } else {
mp-wp_genesis 171 self.elementObj.innerHTML = self.response;
mp-wp_genesis 172 }
mp-wp_genesis 173 }
mp-wp_genesis 174 if (self.responseStatus[0] == "200") {
mp-wp_genesis 175 self.onCompletion();
mp-wp_genesis 176 } else {
mp-wp_genesis 177 self.onError();
mp-wp_genesis 178 }
mp-wp_genesis 179
mp-wp_genesis 180 self.URLString = "";
mp-wp_genesis 181 break;
mp-wp_genesis 182 }
mp-wp_genesis 183 };
mp-wp_genesis 184
mp-wp_genesis 185 this.xmlhttp.send(this.URLString);
mp-wp_genesis 186 }
mp-wp_genesis 187 }
mp-wp_genesis 188 };
mp-wp_genesis 189
mp-wp_genesis 190 this.reset();
mp-wp_genesis 191 this.createAJAX();
mp-wp_genesis 192 }