1 /* Simple AJAX Code-Kit (SACK) */
2 /* ©2005 Gregory Wild-Smith */
3 /* www.twilightuniverse.com */
4 /* Software licenced under a modified X11 licence, see documentation or authors website for more details */
7 this.AjaxFailedAlert
= "Your browser does not support the enhanced functionality of this website, and therefore you will have an experience that differs from the intended one.\n";
8 this.requestFile
= file
;
11 this.encodeURIString
= true;
14 this.onLoading = function() { };
15 this.onLoaded = function() { };
16 this.onInteractive = function() { };
17 this.onCompletion = function() { };
18 this.afterCompletion = function() { };
20 this.createAJAX = function() {
22 this.xmlhttp
= new ActiveXObject("Msxml2.XMLHTTP");
25 this.xmlhttp
= new ActiveXObject("Microsoft.XMLHTTP");
30 if(!this.xmlhttp
&& typeof XMLHttpRequest
!= "undefined"){
31 this.xmlhttp
= new XMLHttpRequest();
38 this.setVar = function(name
, value
){
39 if (this.URLString
.length
< 3){
40 this.URLString
= name
+ "=" + value
;
42 this.URLString
+= "&" + name
+ "=" + value
;
46 this.encVar = function(name
, value
){
47 var varString
= encodeURIComponent(name
) + "=" + encodeURIComponent(value
);
51 this.encodeURLString = function(string
){
52 varArray
= string
.split('&');
53 for (i
= 0; i
< varArray
.length
; i
++){
54 urlVars
= varArray
[i
].split('=');
55 if (urlVars
[0].indexOf('amp;') != -1){
56 urlVars
[0] = urlVars
[0].substring(4);
58 varArray
[i
] = this.encVar(urlVars
[0],urlVars
[1]);
60 return varArray
.join('&');
63 this.runResponse = function(){
67 this.runAJAX = function(urlstring
){
68 this.responseStatus
= new Array(2);
69 if(this.failed
&& this.AjaxFailedAlert
){
70 alert(this.AjaxFailedAlert
);
73 if (this.URLString
.length
){
74 this.URLString
= this.URLString
+ "&" + urlstring
;
76 this.URLString
= urlstring
;
79 if (this.encodeURIString
){
80 var timeval
= new Date().getTime();
81 this.URLString
= this.encodeURLString(this.URLString
);
82 this.setVar("rndval", timeval
);
84 if (this.element
) { this.elementObj
= document
.getElementById(this.element
); }
87 if (this.method
== "GET") {
88 var totalurlstring
= this.requestFile
+ "?" + this.URLString
;
89 this.xmlhttp
.open(this.method
, totalurlstring
, true);
91 this.xmlhttp
.open(this.method
, this.requestFile
, true);
93 if (this.method
== "POST"){
95 this.xmlhttp
.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
99 this.xmlhttp
.onreadystatechange = function() {
100 switch (self
.xmlhttp
.readyState
){
108 self
.onInteractive();
111 self
.response
= self
.xmlhttp
.responseText
;
112 self
.responseXML
= self
.xmlhttp
.responseXML
;
113 self
.responseStatus
[0] = self
.xmlhttp
.status
;
114 self
.responseStatus
[1] = self
.xmlhttp
.statusText
;
116 if(self
.execute
){ self
.runResponse(); }
117 if (self
.elementObj
) {
118 var elemNodeName
= self
.elementObj
.nodeName
;
119 elemNodeName
.toLowerCase();
120 if (elemNodeName
== "input" || elemNodeName
== "select" || elemNodeName
== "option" || elemNodeName
== "textarea"){
121 self
.elementObj
.value
= self
.response
;
123 self
.elementObj
.innerHTML
= self
.response
;
126 self
.afterCompletion();
131 this.xmlhttp
.send(this.URLString
);