]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/tw-sack.js
Wordpress 2.0.2
[autoinstalls/wordpress.git] / wp-includes / js / tw-sack.js
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 */
5
6 function sack(file){
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;
9         this.method = "POST";
10         this.URLString = "";
11         this.encodeURIString = true;
12         this.execute = false;
13
14         this.onLoading = function() { };
15         this.onLoaded = function() { };
16         this.onInteractive = function() { };
17         this.onCompletion = function() { };
18
19         this.createAJAX = function() {
20                 try {
21                         this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
22                 } catch (e) {
23                         try {
24                                 this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
25                         } catch (err) {
26                                 this.xmlhttp = null;
27                         }
28                 }
29                 if(!this.xmlhttp && typeof XMLHttpRequest != "undefined")
30                         this.xmlhttp = new XMLHttpRequest();
31                 if (!this.xmlhttp){
32                         this.failed = true; 
33                 }
34         };
35         
36         this.setVar = function(name, value){
37                 if (this.URLString.length < 3){
38                         this.URLString = name + "=" + value;
39                 } else {
40                         this.URLString += "&" + name + "=" + value;
41                 }
42         }
43         
44         this.encVar = function(name, value){
45                 var varString = encodeURIComponent(name) + "=" + encodeURIComponent(value);
46         return varString;
47         }
48         
49         this.encodeURLString = function(string){
50                 varArray = string.split('&');
51                 for (i = 0; i < varArray.length; i++){
52                         urlVars = varArray[i].split('=');
53                         if (urlVars[0].indexOf('amp;') != -1){
54                                 urlVars[0] = urlVars[0].substring(4);
55                         }
56                         varArray[i] = this.encVar(urlVars[0],urlVars[1]);
57                 }
58         return varArray.join('&');
59         }
60         
61         this.runResponse = function(){
62                 eval(this.response);
63         }
64         
65         this.runAJAX = function(urlstring){
66                 this.responseStatus = new Array(2);
67                 if(this.failed && this.AjaxFailedAlert){ 
68                         alert(this.AjaxFailedAlert); 
69                 } else {
70                         if (urlstring){ 
71                                 if (this.URLString.length){
72                                         this.URLString = this.URLString + "&" + urlstring; 
73                                 } else {
74                                         this.URLString = urlstring; 
75                                 }
76                         }
77                         if (this.encodeURIString){
78                                 var timeval = new Date().getTime(); 
79                                 this.URLString = this.encodeURLString(this.URLString);
80                                 this.setVar("rndval", timeval);
81                         }
82                         if (this.element) { this.elementObj = document.getElementById(this.element); }
83                         if (this.xmlhttp) {
84                                 var self = this;
85                                 if (this.method == "GET") {
86                                         var totalurlstring = this.requestFile + "?" + this.URLString;
87                                         this.xmlhttp.open(this.method, totalurlstring, true);
88                                 } else {
89                                         this.xmlhttp.open(this.method, this.requestFile, true);
90                                 }
91                                 if (this.method == "POST"){
92                                         try {
93                                                 this.xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded')  
94                                         } catch (e) {}
95                                 }
96
97                                 this.xmlhttp.send(this.URLString);
98                                 this.xmlhttp.onreadystatechange = function() {
99                                         switch (self.xmlhttp.readyState){
100                                                 case 1:
101                                                         self.onLoading();
102                                                 break;
103                                                 case 2:
104                                                         self.onLoaded();
105                                                 break;
106                                                 case 3:
107                                                         self.onInteractive();
108                                                 break;
109                                                 case 4:
110                                                         self.response = self.xmlhttp.responseText;
111                                                         self.responseXML = self.xmlhttp.responseXML;
112                                                         self.responseStatus[0] = self.xmlhttp.status;
113                                                         self.responseStatus[1] = self.xmlhttp.statusText;
114                                                         self.onCompletion();
115                                                         if(self.execute){ self.runResponse(); }
116                                                         if (self.elementObj) {
117                                                                 var elemNodeName = self.elementObj.nodeName;
118                                                                 elemNodeName.toLowerCase();
119                                                                 if (elemNodeName == "input" || elemNodeName == "select" || elemNodeName == "option" || elemNodeName == "textarea"){
120                                                                         self.elementObj.value = self.response;
121                                                                 } else {
122                                                                         self.elementObj.innerHTML = self.response;
123                                                                 }
124                                                         }
125                                                         self.URLString = "";
126                                                 break;
127                                         }
128                                 };
129                         }
130                 }
131         };
132 this.createAJAX();
133 }