summaryrefslogtreecommitdiff
path: root/tw-sack.js
blob: 42e9322ed56ae809314921fc36ee193483681999 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// ====== Simple Ajax Code Kit =======
// code by Gregory Wild-Smith (c)2005
// http://twilightuniverse.com
// If you use this code please keep this credit intact.
// A link or email would be nice, but is not required.
// v1.01

function sack(file){
	this.AjaxFailedAlert = "Your browser doesn't support the extended functionality of this website, and therefore you have have an experience that differs from the intended one.\n";
	this.requestFile = file;
	this.method = "POST";
	this.URLString = "";
	this.encodeURIString = true;
	this.execute = false;

	this.onLoading = function() { };
	this.onLoaded = function() { };
	this.onInteractive = function() { };
	this.onCompletion = function() { };
	
	this.createAJAX = function() {
		try {
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (oc) {
				this.xmlhttp = null;
			}
		}
		if(!this.xmlhttp && typeof XMLHttpRequest != "undefined")
			this.xmlhttp = new XMLHttpRequest();
		if (!this.xmlhttp){
			// no XMLHttpRequest support, so no AJAX.
			this.failed = true; 
		}
	};

	this.encodeURLString = function(string){
		varArray = string.split('&');
		for (i = 0; i < varArray.length; i++){
			urlVars = varArray[i].split('=');
			if (urlVars[0].indexOf('amp;') != -1){
				urlVars[0] = urlVars[0].substring(4);
			}
			urlVars[0] = encodeURIComponent(urlVars[0]);
			urlVars[1] = encodeURIComponent(urlVars[1]);
			varArray[i] = urlVars.join("=");
		}
	return varArray.join('&');
	}
	
	this.runResponse = function(){
		eval(this.response);
	}
	
	this.runAJAX = function(urlstring){
		if (urlstring){ this.URLString = urlstring; }
		if (this.element) { this.elementObj = document.getElementById(this.element); }
		if (this.xmlhttp) {
			var self = this; // wierd fix for odd behavior where "this" wouldn't work in the readystate function.
			this.xmlhttp.open(this.method, this.requestFile ,true);
			if (this.method == "POST") { this.xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded') }
			if (this.encodeURIString){ this.URLString = this.encodeURLString(this.URLString); }
			this.xmlhttp.send(this.URLString);
			this.xmlhttp.onreadystatechange = function() {
				switch (self.xmlhttp.readyState){
					case 1: // Loading.
						self.onLoading();
					break;
					case 2: // Loaded.
						self.onLoaded();
					break;
					case 3: // Interactive - is called every 4096 bytes.. pretty much just tells you it's downloading data.
						self.onInteractive();
					break;
					case 4: // Completed.
						self.response = self.xmlhttp.responseText
						self.onCompletion();
						if(self.execute){ self.runResponse(); }
						if (self.elementObj) { 
							self.elementObj.innerHTML = self.response;
						}
					break;
				}
			};
		}
	};
this.createAJAX();
if(this.failed && this.AjaxFailedAlert){ alert(this.AjaxFailedAlert); }
}