moved qdb here because matt is lazy
[public/www-new.git] / pub / qdb / res / themes / default / js / live_rating.js
1 /*\r
2 ###############################################################################\r
3 # Chirpy! 0.3, a quote management system                                      #\r
4 # Copyright (C) 2005-2007 Tim De Pauw <ceetee@users.sourceforge.net>          #\r
5 ###############################################################################\r
6 # This program is free software; you can redistribute it and/or modify it     #\r
7 # under the terms of the GNU General Public License as published by the Free  #\r
8 # Software Foundation; either version 2 of the License, or (at your option)   #\r
9 # any later version.                                                          #\r
10 #                                                                             #\r
11 # This program is distributed in the hope that it will be useful, but WITHOUT #\r
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #\r
13 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #\r
14 # more details.                                                               #\r
15 #                                                                             #\r
16 # You should have received a copy of the GNU General Public License along     #\r
17 # with this program; if not, write to the Free Software Foundation, Inc., 51  #\r
18 # Franklin St, Fifth Floor, Boston, MA  02110-1301  USA                       #\r
19 ###############################################################################\r
20 \r
21 ###############################################################################\r
22 # live_rating.js                                                              #\r
23 # Enables rating and reporting of quotes without leaving the page             #\r
24 ###############################################################################\r
25 # $Id:: live_rating.js 291 2007-02-05 21:24:46Z ceetee                      $ #\r
26 ###############################################################################\r
27 */\r
28 \r
29 var useBlinking = true;\r
30 var confirmationTimeout = 3000;\r
31 var errorTimeout = 10000;\r
32 var connectionTimeout = 30000;\r
33 var pollingInterval = 1000;\r
34 var okText = (/MSIE/.test(navigator.userAgent) && !window.opera\r
35         ? "OK" : String.fromCharCode(0x2713));\r
36 \r
37 var requests = new Array();\r
38 \r
39 function QuoteActionRequest (url, id, isReport) {\r
40         var t = getTimestamp();\r
41         var req = this;\r
42         this.id = id;\r
43         if (requests[this.id]) requests[this.id].verbose = false;\r
44         requests[this.id] = this;\r
45         this.url = url + "&output=xml&ignore=" + t;\r
46         this.isReport = isReport;\r
47         this.verbose = true;\r
48         this.resultField = document.getElementById('quote-live-vote-result-' + id);\r
49         this.setResult(locale["processing"], true);\r
50         this.ajax = getAjaxObject();\r
51         this.ajax.onreadystatechange = function() { readyStateChanged(req); };\r
52         this.startTime = t;\r
53         this.interval = setInterval(\r
54                 function () { checkRequestTime(req); }, pollingInterval);\r
55         this.ajax.open("POST", this.url, true);\r
56         this.ajax.send("");\r
57 }\r
58 \r
59 QuoteActionRequest.prototype.setResult = function (text, blink, timeout) {\r
60         if (!this.verbose) return;\r
61         if (this.fadeTimeout) {\r
62                 clearTimeout(this.fadeTimeout);\r
63                 this.fadeTimeout = null;\r
64         }\r
65         setText(this.resultField, text);\r
66         if (useBlinking)\r
67                 this.resultField.style.textDecoration = blink ? "blink" : "";\r
68         var field = this.resultField;\r
69         if (timeout)\r
70                 this.fadeTimeout = setTimeout(\r
71                         function () { setText(field, ""); }, timeout);\r
72 };\r
73 \r
74 function sendRating (anchor, id) {\r
75         if (!ajaxSupported()) return true;\r
76         new QuoteActionRequest(anchor.href, id, false);\r
77         var other;\r
78         if (anchor.id.indexOf("down") >= 0) {\r
79                 other = document.getElementById(anchor.id.replace("down", "up"));\r
80         }\r
81         else {\r
82                 other = document.getElementById(anchor.id.replace("up", "down"));\r
83         }\r
84         addClassName(anchor, "casted-vote");\r
85         removeClassName(other, "casted-vote");\r
86         return false;\r
87 }\r
88 \r
89 function sendReport (anchor, id) {\r
90         if (!ajaxSupported()) return true;\r
91         new QuoteActionRequest(anchor.href, id, true);\r
92         return false;\r
93 }\r
94 \r
95 function readyStateChanged (req) {\r
96         if (req.ajax.readyState != 4) return;\r
97         if (req.ajax.status == 200) {\r
98                 if (req.isReport)\r
99                         reportRequestCompleted(req, processXML(req.ajax.responseXML));\r
100                 else\r
101                         ratingRequestCompleted(req, processXML(req.ajax.responseXML));\r
102         }\r
103         else\r
104                 req.setResult(locale["error"], false, errorTimeout);\r
105         removeRequest(req);\r
106 }\r
107 \r
108 function ratingRequestCompleted (req, result) {\r
109         if (result) {\r
110                 switch (result["status"]) {\r
111                         case "1":\r
112                                 req.setResult("");\r
113                                 setText(document.getElementById('quote-rating-' + req.id),\r
114                                         result["rating"]);\r
115                                 var vc = document.getElementById('quote-vote-count-' + req.id);\r
116                                 if (vc) setText(vc, result["votes"]);\r
117                                 req.setResult(okText, false, confirmationTimeout);\r
118                                 break;\r
119                         case "2":\r
120                                 req.setResult(locale["error"], false, errorTimeout);\r
121                                 if (req.verbose) alert(locale["already_rated_text"]);\r
122                                 break;\r
123                         case "3":\r
124                                 req.setResult(locale["error"], false, errorTimeout);\r
125                                 if (req.verbose) alert(locale["limit_exceeded_text"]);\r
126                                 break;\r
127                         case "4":\r
128                                 req.setResult(locale["error"], false, errorTimeout);\r
129                                 if (req.verbose) alert(locale["quote_not_found_text"]);\r
130                                 break;\r
131                         case "5":\r
132                                 req.setResult(locale["error"], false, errorTimeout);\r
133                                 if (req.verbose) alert(locale["session_required_text"]);\r
134                                 break;\r
135                         default:\r
136                                 req.setResult(locale["error"], false, errorTimeout);\r
137                                 break;\r
138                 }\r
139         }\r
140         else\r
141                 req.setResult(locale["error"], false, errorTimeout);\r
142 }\r
143 \r
144 function reportRequestCompleted (req, result) {\r
145         if (result) {\r
146                 switch (result["status"]) {\r
147                         case "1":\r
148                                 req.setResult("");\r
149                                 document.getElementById('quote-' + req.id).className\r
150                                         += " flagged";\r
151                                 var rep = document.getElementById('quote-report-' + req.id);\r
152                                 var el = document.createElement("span");\r
153                                 var at = document.createAttribute("class");\r
154                                 at.value = "quote-flagged";\r
155                                 el.setAttributeNode(at);\r
156                                 el.appendChild(document.createTextNode(\r
157                                         "[" + locale["flagged"] + "]"));\r
158                                 rep.parentNode.replaceChild(el, rep);\r
159                                 req.setResult(okText, false, confirmationTimeout);\r
160                                 break;\r
161                         case "5":\r
162                                 req.setResult(locale["error"], false, errorTimeout);\r
163                                 if (req.verbose) alert(locale["session_required_text"]);\r
164                                 break;\r
165                         default:\r
166                                 req.setResult(locale["error"], false, errorTimeout);\r
167                                 break;\r
168                 }\r
169         }\r
170         else\r
171                 req.setResult(locale["error"], false, errorTimeout);\r
172 }\r
173 \r
174 function removeRequest (req) {\r
175         requests[req.id] = null;\r
176         clearInterval(req.interval);\r
177 }\r
178 \r
179 function checkRequestTime (req) {\r
180         if (getTimestamp() - req.startTime >= connectionTimeout) {\r
181                 clearInterval(req.interval);\r
182                 req.setResult(locale["error"], false, errorTimeout);\r
183                 if (req.verbose) alert(locale["timeout_text"]);\r
184         }\r
185 }\r
186 \r
187 function processXML (xml) {\r
188         if (!xml || !xml.childNodes\r
189         || xml.childNodes[xml.childNodes.length - 1].nodeName != "result")\r
190                 return null;\r
191         var a = new Array();\r
192         var root = xml.childNodes[xml.childNodes.length - 1];\r
193         for (var i = 0; i < root.childNodes.length; i++) {\r
194                 var node = root.childNodes[i];\r
195                 a[node.nodeName] = node.firstChild.nodeValue;\r
196         }\r
197         return a;\r
198 }\r
199 \r
200 function getTimestamp () {\r
201         return (new Date()).getTime();\r
202 }\r
203 \r
204 function setText (element, text) {\r
205         if (element.firstChild)\r
206                 element.firstChild.nodeValue = text;\r
207         else\r
208                 element.appendChild(document.createTextNode(text));\r
209 }\r
210 \r
211 function addClassName (element, className) {\r
212         element.className += " " + className;\r
213 }\r
214 \r
215 function removeClassName (element, className) {\r
216         var c = getClassNames(element);\r
217         if (c.length == 0) return;\r
218         var newCN = c[0];\r
219         for (var i = 1; i < c.length; c++) {\r
220                 if (c[i] == className) continue;\r
221                 newCN += " " + c[i];\r
222         }\r
223         element.className = newCN;\r
224 }\r
225 \r
226 function getClassNames (element) {\r
227         if (element.className == null || element.className.length == 0)\r
228                 return new Array();\r
229         return element.className.split(/ +/);\r
230 }