updated files from live website
[www/www.git] / ajax.js
1 // X-Mode AJAX Core "Yume" 2.0
2 // Compatible with X-Mode 3 "Yotsuba" and any other external apps.
3 // Originally developed by Dave Crane, Eric Pascarello and Darren James
4 // Developed by Juti Noppornpitak
5
6 var net = new Object();
7 net.READY_STATE_UNINITIALIZED = 0;
8 net.READY_STATE_LOADING = 1;
9 net.READY_STATE_LOADED = 2;
10 net.READY_STATE_INTERACTIVE = 3;
11 net.READY_STATE_COMPLETE = 4;
12 net.CONNECTION_TERMINATION = "<blink>Terminating the connection...</blink>";
13 net.core = function(url, console, onload, loading, onerror,
14         method, data, contentType) {
15         this.url = url;
16         this.currurl = url;
17         this.req = null;
18         this.console = console;
19         this.data = (data) ? data : null;
20         this.contentType = (contentType) ? contentType : null;
21         this.method = (method) ? method : "GET";
22         this.onload = (onload) ? onload : this.defaultLoad;
23         this.loading = (loading) ? loading : this.defaultLoading;
24         this.onerror = (onerror) ? onerror : this.defaultError;
25         // Require the user to manually start the connection.
26         // this.connect(url);
27 }
28 net.core.prototype = {
29         defaultConnect:function() {
30                 this.connect(this.url);
31         },
32
33         connect:function(url, method, data, contentType) {
34                 // Create the requester
35                 if(window.XMLHttpRequest) { // for Mozilla/Safari
36                         this.req = new XMLHttpRequest();
37                 } else if(window.ActiveXObject) { // for MSIE
38                         this.req = new ActiveXObject("Microsoft.XMLHTTP");
39                 }
40                 
41                 if(!contentType && method == "POST") {
42                         contentType="application/x-www-form-urlencoded";
43                 }
44
45                 this.currurl = url;
46                 
47                 if(this.req) {
48                         try {
49                                 var loader = this;
50                                 this.req.onreadystatechange = function() {
51                                         loader.process.call(loader);
52                                 }
53                                 if(!method) { method = "GET"; }
54                                 if(!data) { data = null; }
55                                 this.req.open(method, this.currurl, true);
56                                 if(contentType) {
57                                         this.req.setRequestHeader("Content-Type", contentType);
58                                 }
59                                 this.req.send(data);
60                         } catch(err) {
61                                 this.onerror.call(this);
62                         }
63                 }
64         },
65         
66         process:function() {
67                 var req = this.req;
68                 var status = req.readyState;
69
70                 if(status == net.READY_STATE_COMPLETE) {
71                         var httpstatus = req.status;
72                         if(httpstatus == 200 || httpstatus == 0) {
73                                 this.onload.call(this);
74                         } else {
75                                 this.onerror.call(this);
76                         }
77                 } else {
78                         // handling loading/connecting
79                         this.loading.call(this);
80                 }
81         },
82
83         defaultLoad:function() {
84                 xcie__setElement(this.console, 
85                         this.req.responseText);
86         },
87         
88         defaultError:function() {
89                 alert("Error while connecting to: " + this.currurl);
90         },
91
92         defaultLoading:function() {
93                 xcie__setElement(this.console, "Loading");
94         }
95 }
96
97 function xcie__element(id) {
98         return document.getElementById(id);
99 }
100
101 function xcie__getElement(id) {
102         if(!xcie__element(id)) return;
103         return xcie__element(id).innerHTML;
104 }
105
106 function xcie__getElementValue(id) {
107         if(!xcie__element(id)) return;
108         return xcie__element(id).value;
109 }
110
111 function xcie__setElementValue(id, value) {
112         if(!xcie__element(id)) return;
113         xcie__element(id).value = value;
114 }
115
116 function xcie__setElement(id, value) {
117         if(!xcie__element(id)) return;
118         xcie__element(id).innerHTML = value;
119 }
120
121 function xcie__setWindowTitle(value) {
122         document.title = value;
123 }
124
125 function xcie__setClass(id, nc) {
126         xcie__element(id).className = nc;
127 }
128
129 function xcie__img(src, align, other) {
130         return "<img border=\"0\" src=\"" + src + "\" align=\""
131                 + ((align)?align:"absmiddle")
132                 + "\" " + ((other)?other:"") + "/>";
133 }
134
135 function xcie__a(label, value, ajax, other, command) {
136         return "<a " + (!ajax?"href=\"" + value + "\"":"onclick=\""
137                 + ((command == null)?
138                         "connect('" + net.PREFIX_LINK + value + "');"
139                         :command)
140                 + "\"")
141                 + ((other)?" " + other:"") + "\">"
142                 + label + "</a>";
143 }
144
145 function xcie__in(type, name, value, other, cmd) {
146         return "<input name=\"" + name + "\" id=\"" + name + "\""
147                 + " type=\"" + type + "\" value=\"" + ((value)?value:"")
148                 + "\"" + " onclick=\"" + cmd + "\""
149                 + ((other)?" " + other:"") + "/>";
150 }
151
152 function addslashes(str) {
153         // SRC: Stephen Chapman of About.com
154         str=str.replace(/\\/g,'\\\\');
155         str=str.replace(/\'/g,'\\\'');
156         str=str.replace(/\"/g,'\\"');
157         return str;
158 }
159
160 function stripslashes(str) {
161         // SRC: Stephen Chapman of About.com
162         str=str.replace(/\\'/g,'\'');
163         str=str.replace(/\\"/g,'"');
164         str=str.replace(/\\\\/g,'\\');
165         return str;
166 }