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 */
10 this.resetData = function() {
12 this.queryStringSeparator = "?";
13 this.argumentSeparator = "&";
15 this.encodeURIString = true;
18 this.elementObj = null;
19 this.requestFile = file;
20 this.vars = new Object();
21 this.responseStatus = new Array(2);
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() { };
33 this.reset = function() {
34 this.resetFunctions();
38 this.createAJAX = function() {
40 this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
43 this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
50 if (typeof XMLHttpRequest != "undefined") {
51 this.xmlhttp = new XMLHttpRequest();
58 /*Modified by Sundar: to accept POST request*/
59 this.setVar = function(name, value, index){
61 this.vars[name+'~'+index] = Array(value, false);
63 this.vars[name+'~'+0] = Array(value, false);
66 this.encVar = function(name, value, returnvars) {
67 if (true == returnvars) {
68 return Array(encodeURIComponent(name), encodeURIComponent(value));
70 this.vars[encodeURIComponent(name)] = Array(encodeURIComponent(value), true);
74 this.processURLString = function(string, encode) {
75 encoded = encodeURIComponent(this.argumentSeparator);
76 regexp = new RegExp(this.argumentSeparator + "|" + encoded);
77 varArray = string.split(regexp);
78 for (i = 0; i < varArray.length; i++){
79 urlVars = varArray[i].split("=");
81 this.encVar(urlVars[0], urlVars[1]);
83 this.setVar(urlVars[0], urlVars[1]);
88 this.createURLString = function(urlstring) {
89 if (this.encodeURIString && this.URLString.length) {
90 this.processURLString(this.URLString, true);
94 if (this.URLString.length) {
95 this.URLString += this.argumentSeparator + urlstring;
97 this.URLString = urlstring;
101 // prevents caching of URLString
102 this.setVar("rndval", new Date().getTime());
104 urlstringtemp = new Array();
105 /*Modified by Sundar: to accept POST request*/
107 for (key in this.vars) {
108 if (false == this.vars[key][1] && true == this.encodeURIString) {
109 encoded = this.encVar(key, this.vars[key][0], true);
110 delete this.vars[key];
111 this.vars[encoded[0]] = Array(encoded[1], true);
114 key1 = key.substr(0,key.indexOf('~'));
115 //alert("Key " + key1);
116 urlstringtemp[urlstringtemp.length] = key1 + "=" + this.vars[key][0];
119 this.URLString += this.argumentSeparator + urlstringtemp.join(this.argumentSeparator);
121 this.URLString += urlstringtemp.join(this.argumentSeparator);
123 //alert("URLString " + this.URLString);
127 this.runResponse = function() {
132 this.runAJAX = function(urlstring,noWaitForResponse,timeOut) {
134 if(noWaitForResponse == null){
135 noWaitForResponse = true;
141 this.createURLString(urlstring);
143 this.elementObj = document.getElementById(this.element);
147 //SUNDAR: Added timeout for synchronous AJAX
148 if(!noWaitForResponse && timeOut != null && !isNaN(timeOut)){
149 window.setTimeout("dummyFunction()",timeOut);
151 if (this.method == "GET") {
152 totalurlstring = this.requestFile + this.queryStringSeparator + this.URLString;
153 this.xmlhttp.open(this.method, totalurlstring, noWaitForResponse);
156 this.xmlhttp.open(this.method, this.requestFile, noWaitForResponse);
158 this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
162 this.xmlhttp.onreadystatechange = function() {
163 //alert("AJAX READY " + self.xmlhttp.readyState);
164 switch (self.xmlhttp.readyState) {
172 self.onInteractive();
175 self.response = self.xmlhttp.responseText;
176 self.responseXML = self.xmlhttp.responseXML;
177 self.responseStatus[0] = self.xmlhttp.status;
178 self.responseStatus[1] = self.xmlhttp.statusText;
184 if (self.elementObj) {
185 elemNodeName = self.elementObj.nodeName;
186 elemNodeName.toLowerCase();
187 if (elemNodeName == "input"
188 || elemNodeName == "select"
189 || elemNodeName == "option"
190 || elemNodeName == "textarea") {
191 self.elementObj.value = self.response;
193 self.elementObj.innerHTML = self.response;
196 if (self.responseStatus[0] == "200") {
207 this.xmlhttp.send(this.URLString);