annotate ajax.js @ 105:a85f258f6beb

Move some things around and modularize the code.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 21 Oct 2013 16:26:42 +0300
parents
children b5981168ed85
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
105
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 function statusMsg(msg)
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 {
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 document.getElementById("nstatus").innerHTML = msg;
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4 }
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 function strtrim(str)
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 {
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 if (!str || str == null)
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 return "";
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 return str.replace(/^\s+|\s+$/g,'')
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 }
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15 function strencode(str)
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16 {
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17 return encodeURIComponent(str);
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18 }
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21 function createXMLRequest()
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22 {
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23 var req;
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24 if (window.XMLHttpRequest)
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25 {
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 // Modern browsers
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27 req = new XMLHttpRequest();
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28 }
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 else
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30 {
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31 // Old IE versions
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32 req = new ActiveXObject("Microsoft.XMLHTTP");
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 }
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34 return req;
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35 }
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38 function sendPOSTRequest(params, success, failure)
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39 {
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40 var req = createXMLRequest();
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41 req.open("POST", "admajax.php", true);
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42 req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43 req.setRequestHeader("Content-length", params.length);
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44 req.setRequestHeader("Connection", "close");
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 req.onreadystatechange = function()
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47 {
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48 if (req.readyState == 4)
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49 {
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 if (req.status == 404)
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51 {
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52 window.location = "admlogout.php";
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53 }
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54 else
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 if (req.status == 200)
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 {
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 if (success)
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 {
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 success(req.responseText);
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60 }
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61 statusMsg(req.statusText);
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62 }
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63 else
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 {
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 if (failure)
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 {
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 failure(req.status, req.statusText, req.responseText);
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68 }
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 else
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 {
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 statusMsg("["+req.status+" - "+req.statusText+"] "+ req.responseText);
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 }
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73 }
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 }
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 }
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76 req.send(params);
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 }
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 function makePostArgs(fields, fprefix, fsuffix)
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 {
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 var res = [];
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 for (var id in fields)
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84 {
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 var elem = document.getElementById(fprefix + id + fsuffix);
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 if (!elem)
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87 {
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 alert("No such DOM element '"+ fprefix + id + fsuffix +"'.");
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 return "";
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 }
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 switch (fields[id])
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 {
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 case 0:
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95 case 1:
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 case 4:
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97 {
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98 var str = strtrim(elem.value);
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99 if ((fields[id] == 1 || fields[id] == 4) && str == "")
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 {
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 alert("One or more of the required fields are empty.");
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102 return "";
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103 }
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 if (fields[id] == 4)
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105 res.push(id+"="+parseInt(elem.value));
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106 else
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107 res.push(id+"="+strencode(str));
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 }
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 break;
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111 case 2:
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112 res.push(id+"="+parseInt(elem.value));
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 break;
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115 case 3:
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116 res.push(id+"="+(elem.checked ? "1" : "0"));
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117 break;
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
118
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119 }
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120 }
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121 return res.join("&");
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122 }
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125 function refreshItems(id,name,msgname)
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126 {
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127 var msuccess = function(txt)
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128 {
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129 var nitem = document.getElementById(id);
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130 nitem.innerHTML = txt;
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131 }
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133 sendPOSTRequest("action=get&type="+name, msuccess);
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134 }
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137 function deleteItem(id,prefix,type,func,dsc)
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138 {
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
139 var msuccess = function(txt)
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
140 {
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
141 var item = document.getElementById(prefix+id);
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
142 item.style.display = "none";
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143 setTimeout(func, 50);
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
144 }
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
145
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
146 if (confirm("Are you sure you want to delete "+dsc+" #"+id+"?"))
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
147 {
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
148 sendPOSTRequest("action=delete&type="+type+"&id="+id, msuccess);
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
149 }
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
150 }
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151
a85f258f6beb Move some things around and modularize the code.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
152