backup de julho
[h2N7SspZmY.git] / lib / scripts / tw-sack.js
blobcfcbe0ea9d3d19d1485e7ddf70ef7eb7a87ab88f
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 */
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;
14 this.onLoading = function() { };
15 this.onLoaded = function() { };
16 this.onInteractive = function() { };
17 this.onCompletion = function() { };
18 this.afterCompletion = function() { };
20 this.createAJAX = function() {
21 try {
22 this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
23 } catch (e) {
24 try {
25 this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
26 } catch (err) {
27 this.xmlhttp = null;
30 if(!this.xmlhttp && typeof XMLHttpRequest != "undefined"){
31 this.xmlhttp = new XMLHttpRequest();
33 if (!this.xmlhttp){
34 this.failed = true;
38 this.setVar = function(name, value){
39 if (this.URLString.length < 3){
40 this.URLString = name + "=" + value;
41 } else {
42 this.URLString += "&" + name + "=" + value;
46 this.encVar = function(name, value){
47 var varString = encodeURIComponent(name) + "=" + encodeURIComponent(value);
48 return varString;
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(){
64 eval(this.response);
67 this.runAJAX = function(urlstring){
68 this.responseStatus = new Array(2);
69 if(this.failed && this.AjaxFailedAlert){
70 alert(this.AjaxFailedAlert);
71 } else {
72 if (urlstring){
73 if (this.URLString.length){
74 this.URLString = this.URLString + "&" + urlstring;
75 } else {
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); }
85 if (this.xmlhttp) {
86 var self = this;
87 if (this.method == "GET") {
88 var totalurlstring = this.requestFile + "?" + this.URLString;
89 this.xmlhttp.open(this.method, totalurlstring, true);
90 } else {
91 this.xmlhttp.open(this.method, this.requestFile, true);
93 if (this.method == "POST"){
94 try {
95 this.xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
96 } catch (e) {}
99 this.xmlhttp.onreadystatechange = function() {
100 switch (self.xmlhttp.readyState){
101 case 1:
102 self.onLoading();
103 break;
104 case 2:
105 self.onLoaded();
106 break;
107 case 3:
108 self.onInteractive();
109 break;
110 case 4:
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;
115 self.onCompletion();
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;
122 } else {
123 self.elementObj.innerHTML = self.response;
126 self.afterCompletion();
127 self.URLString = "";
128 break;
131 this.xmlhttp.send(this.URLString);
135 this.createAJAX();