]> git.wh0rd.org Git - tt-rss.git/blob - js/functions.js
css cleanup; remove auxDlg; add separate prefs.css
[tt-rss.git] / js / functions.js
1 var notify_silent = false;
2 var loading_progress = 0;
3 var sanity_check_done = false;
4 var init_params = {};
5 var _label_base_index = -1024;
6
7 Ajax.Base.prototype.initialize = Ajax.Base.prototype.initialize.wrap(
8         function (callOriginal, options) {
9
10                 if (getInitParam("csrf_token") != undefined) {
11                         Object.extend(options, options || { });
12
13                         if (Object.isString(options.parameters))
14                                 options.parameters = options.parameters.toQueryParams();
15                         else if (Object.isHash(options.parameters))
16                                 options.parameters = options.parameters.toObject();
17
18                         options.parameters["csrf_token"] = getInitParam("csrf_token");
19                 }
20
21                 return callOriginal(options);
22         }
23 );
24
25 /* add method to remove element from array */
26
27 Array.prototype.remove = function(s) {
28         for (var i=0; i < this.length; i++) {
29                 if (s == this[i]) this.splice(i, 1);
30         }
31 };
32
33 /* create console.log if it doesn't exist */
34
35 if (!window.console) console = {};
36 console.log = console.log || function(msg) { };
37 console.warn = console.warn || function(msg) { };
38 console.error = console.error || function(msg) { };
39
40 function exception_error(location, e, ext_info) {
41         var msg = format_exception_error(location, e);
42
43         if (!ext_info) ext_info = false;
44
45         try {
46
47                 if (ext_info) {
48                         if (ext_info.responseText) {
49                                 ext_info = ext_info.responseText;
50                         }
51                 }
52
53                 var content = "<div class=\"fatalError\">" +
54                         "<pre>" + msg + "</pre>";
55
56                 content += "<form name=\"exceptionForm\" id=\"exceptionForm\" target=\"_blank\" "+
57                   "action=\"http://tt-rss.org/report.php\" method=\"POST\">";
58
59                 content += "<textarea style=\"display : none\" name=\"message\">" + msg + "</textarea>";
60                 content += "<textarea style=\"display : none\" name=\"params\">N/A</textarea>";
61
62                 if (ext_info) {
63                         content += "<div><b>Additional information:</b></div>" +
64                         "<textarea name=\"xinfo\" readonly=\"1\">" + ext_info + "</textarea>";
65                 }
66
67                 content += "<div><b>Stack trace:</b></div>" +
68                         "<textarea name=\"stack\" readonly=\"1\">" + e.stack + "</textarea>";
69
70                 content += "</form>";
71
72                 content += "</div>";
73
74                 content += "<div class='dlgButtons'>";
75
76                 content += "<button dojoType=\"dijit.form.Button\""+
77                                 "onclick=\"dijit.byId('exceptionDlg').report()\">" +
78                                 __('Report to tt-rss.org') + "</button> ";
79                 content += "<button dojoType=\"dijit.form.Button\" "+
80                                 "onclick=\"dijit.byId('exceptionDlg').hide()\">" +
81                                 __('Close') + "</button>";
82                 content += "</div>";
83
84                 if (dijit.byId("exceptionDlg"))
85                         dijit.byId("exceptionDlg").destroyRecursive();
86
87                 var dialog = new dijit.Dialog({
88                         id: "exceptionDlg",
89                         title: "Unhandled exception",
90                         style: "width: 600px",
91                         report: function() {
92                                 if (confirm(__("Are you sure to report this exception to tt-rss.org? The report will include your browser information. Your IP would be saved in the database."))) {
93
94                                         document.forms['exceptionForm'].params.value = $H({
95                                                 browserName: navigator.appName,
96                                                 browserVersion: navigator.appVersion,
97                                                 browserPlatform: navigator.platform,
98                                                 browserCookies: navigator.cookieEnabled,
99                                         }).toQueryString();
100
101                                         document.forms['exceptionForm'].submit();
102
103                                 }
104                         },
105                         content: content});
106
107                 dialog.show();
108
109         } catch (e) {
110                 alert(msg);
111         }
112
113 }
114
115 function format_exception_error(location, e) {
116         var msg;
117
118         if (e.fileName) {
119                 var base_fname = e.fileName.substring(e.fileName.lastIndexOf("/") + 1);
120
121                 msg = "Exception: " + e.name + ", " + e.message +
122                         "\nFunction: " + location + "()" +
123                         "\nLocation: " + base_fname + ":" + e.lineNumber;
124
125         } else if (e.description) {
126                 msg = "Exception: " + e.description + "\nFunction: " + location + "()";
127         } else {
128                 msg = "Exception: " + e + "\nFunction: " + location + "()";
129         }
130
131         console.error("EXCEPTION: " + msg);
132
133         return msg;
134 }
135
136 function param_escape(arg) {
137         if (typeof encodeURIComponent != 'undefined')
138                 return encodeURIComponent(arg);
139         else
140                 return escape(arg);
141 }
142
143 function param_unescape(arg) {
144         if (typeof decodeURIComponent != 'undefined')
145                 return decodeURIComponent(arg);
146         else
147                 return unescape(arg);
148 }
149
150 var notify_hide_timerid = false;
151
152 function hide_notify() {
153         var n = $("notify");
154         if (n) {
155                 n.style.display = "none";
156         }
157 }
158
159 function notify_silent_next() {
160         notify_silent = true;
161 }
162
163 function notify_real(msg, no_hide, n_type) {
164
165         if (notify_silent) {
166                 notify_silent = false;
167                 return;
168         }
169
170         var n = $("notify");
171         var nb = $("notify_body");
172
173         if (!n || !nb) return;
174
175         if (notify_hide_timerid) {
176                 window.clearTimeout(notify_hide_timerid);
177         }
178
179         if (msg == "") {
180                 if (n.style.display == "block") {
181                         notify_hide_timerid = window.setTimeout("hide_notify()", 0);
182                 }
183                 return;
184         } else {
185                 n.style.display = "block";
186         }
187
188         /* types:
189
190                 1 - generic
191                 2 - progress
192                 3 - error
193                 4 - info
194
195         */
196
197         msg = __(msg);
198
199         if (n_type == 1) {
200                 n.className = "notify";
201         } else if (n_type == 2) {
202                 n.className = "notify progress";
203                 msg = "<img src='images/indicator_white.gif'> " + msg;
204         } else if (n_type == 3) {
205                 n.className = "notify error";
206                 msg = "<img src='images/sign_excl.svg'> " + msg;
207         } else if (n_type == 4) {
208                 n.className = "notify info";
209                 msg = "<img src='images/sign_info.svg'> " + msg;
210         }
211
212         if (no_hide) {
213                 msg += " <span>(<a href='#' onclick=\"notify('')\">" +
214                         __("close") + "</a>)</span>";
215         }
216
217
218 //      msg = "<img src='images/live_com_loading.gif'> " + msg;
219
220         nb.innerHTML = msg;
221
222         if (!no_hide) {
223                 notify_hide_timerid = window.setTimeout("hide_notify()", 3000);
224         }
225 }
226
227 function notify(msg, no_hide) {
228         notify_real(msg, no_hide, 1);
229 }
230
231 function notify_progress(msg, no_hide) {
232         notify_real(msg, no_hide, 2);
233 }
234
235 function notify_error(msg, no_hide) {
236         notify_real(msg, no_hide, 3);
237
238 }
239
240 function notify_info(msg, no_hide) {
241         notify_real(msg, no_hide, 4);
242 }
243
244 function setCookie(name, value, lifetime, path, domain, secure) {
245
246         var d = false;
247
248         if (lifetime) {
249                 d = new Date();
250                 d.setTime(d.getTime() + (lifetime * 1000));
251         }
252
253         console.log("setCookie: " + name + " => " + value + ": " + d);
254
255         int_setCookie(name, value, d, path, domain, secure);
256
257 }
258
259 function int_setCookie(name, value, expires, path, domain, secure) {
260         document.cookie= name + "=" + escape(value) +
261                 ((expires) ? "; expires=" + expires.toGMTString() : "") +
262                 ((path) ? "; path=" + path : "") +
263                 ((domain) ? "; domain=" + domain : "") +
264                 ((secure) ? "; secure" : "");
265 }
266
267 function delCookie(name, path, domain) {
268         if (getCookie(name)) {
269                 document.cookie = name + "=" +
270                 ((path) ? ";path=" + path : "") +
271                 ((domain) ? ";domain=" + domain : "" ) +
272                 ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
273         }
274 }
275
276
277 function getCookie(name) {
278
279         var dc = document.cookie;
280         var prefix = name + "=";
281         var begin = dc.indexOf("; " + prefix);
282         if (begin == -1) {
283             begin = dc.indexOf(prefix);
284             if (begin != 0) return null;
285         }
286         else {
287             begin += 2;
288         }
289         var end = document.cookie.indexOf(";", begin);
290         if (end == -1) {
291             end = dc.length;
292         }
293         return unescape(dc.substring(begin + prefix.length, end));
294 }
295
296 function gotoPreferences() {
297         document.location.href = "prefs.php";
298 }
299
300 function gotoLogout() {
301         document.location.href = "backend.php?op=logout";
302 }
303
304 function gotoMain() {
305         document.location.href = "index.php";
306 }
307
308 /** * @(#)isNumeric.js * * Copyright (c) 2000 by Sundar Dorai-Raj
309   * * @author Sundar Dorai-Raj
310   * * Email: sdoraira@vt.edu
311   * * This program is free software; you can redistribute it and/or
312   * * modify it under the terms of the GNU General Public License
313   * * as published by the Free Software Foundation; either version 2
314   * * of the License, or (at your option) any later version,
315   * * provided that any use properly credits the author.
316   * * This program is distributed in the hope that it will be useful,
317   * * but WITHOUT ANY WARRANTY; without even the implied warranty of
318   * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
319   * * GNU General Public License for more details at http://www.gnu.org * * */
320
321   var numbers=".0123456789";
322   function isNumeric(x) {
323     // is x a String or a character?
324     if(x.length>1) {
325       // remove negative sign
326       x=Math.abs(x)+"";
327       for(var j=0;j<x.length;j++) {
328         // call isNumeric recursively for each character
329         number=isNumeric(x.substring(j,j+1));
330         if(!number) return number;
331       }
332       return number;
333     }
334     else {
335       // if x is number return true
336       if(numbers.indexOf(x)>=0) return true;
337       return false;
338     }
339   }
340
341
342 function toggleSelectRowById(sender, id) {
343         var row = $(id);
344         return toggleSelectRow(sender, row);
345 }
346
347 function toggleSelectListRow(sender) {
348         var row = sender.parentNode;
349         return toggleSelectRow(sender, row);
350 }
351
352 /* this is for dijit Checkbox */
353 function toggleSelectListRow2(sender) {
354         var row = sender.domNode.parentNode;
355         return toggleSelectRow(sender, row);
356 }
357
358 /* this is for dijit Checkbox */
359 function toggleSelectRow2(sender, row, is_cdm) {
360
361         if (!row)
362                 if (!is_cdm)
363                         row = sender.domNode.parentNode.parentNode;
364                 else
365                         row = sender.domNode.parentNode.parentNode.parentNode; // oh ffs
366
367         if (sender.checked && !row.hasClassName('Selected'))
368                 row.addClassName('Selected');
369         else
370                 row.removeClassName('Selected');
371 }
372
373
374 function toggleSelectRow(sender, row) {
375
376         if (!row) row = sender.parentNode.parentNode;
377
378         if (sender.checked && !row.hasClassName('Selected'))
379                 row.addClassName('Selected');
380         else
381                 row.removeClassName('Selected');
382 }
383
384 function checkboxToggleElement(elem, id) {
385         if (elem.checked) {
386                 Effect.Appear(id, {duration : 0.5});
387         } else {
388                 Effect.Fade(id, {duration : 0.5});
389         }
390 }
391
392 function dropboxSelect(e, v) {
393         for (var i = 0; i < e.length; i++) {
394                 if (e[i].value == v) {
395                         e.selectedIndex = i;
396                         break;
397                 }
398         }
399 }
400
401 function getURLParam(param){
402         return String(window.location.href).parseQuery()[param];
403 }
404
405 function closeInfoBox(cleanup) {
406         try {
407                 dialog = dijit.byId("infoBox");
408
409                 if (dialog)     dialog.hide();
410
411         } catch (e) {
412                 //exception_error("closeInfoBox", e);
413         }
414         return false;
415 }
416
417
418 function displayDlg(id, param, callback) {
419
420         notify_progress("Loading, please wait...", true);
421
422         var query = "?op=dlg&method=" +
423                 param_escape(id) + "&param=" + param_escape(param);
424
425         new Ajax.Request("backend.php", {
426                 parameters: query,
427                 onComplete: function (transport) {
428                         infobox_callback2(transport);
429                         if (callback) callback(transport);
430                 } });
431
432         return false;
433 }
434
435 function infobox_callback2(transport) {
436         try {
437                 var dialog = false;
438
439                 if (dijit.byId("infoBox")) {
440                         dialog = dijit.byId("infoBox");
441                 }
442
443                 //console.log("infobox_callback2");
444                 notify('');
445
446                 var title = transport.responseXML.getElementsByTagName("title")[0];
447                 if (title)
448                         title = title.firstChild.nodeValue;
449
450                 var content = transport.responseXML.getElementsByTagName("content")[0];
451
452                 content = content.firstChild.nodeValue;
453
454                 if (!dialog) {
455                         dialog = new dijit.Dialog({
456                                 title: title,
457                                 id: 'infoBox',
458                                 style: "width: 600px",
459                                 onCancel: function() {
460                                         return true;
461                                 },
462                                 onExecute: function() {
463                                         return true;
464                                 },
465                                 onClose: function() {
466                                         return true;
467                                         },
468                                 content: content});
469                 } else {
470                         dialog.attr('title', title);
471                         dialog.attr('content', content);
472                 }
473
474                 dialog.show();
475
476                 notify("");
477         } catch (e) {
478                 exception_error("infobox_callback2", e);
479         }
480 }
481
482 function filterCR(e, f)
483 {
484      var key;
485
486      if(window.event)
487           key = window.event.keyCode;     //IE
488      else
489           key = e.which;     //firefox
490
491         if (key == 13) {
492                 if (typeof f != 'undefined') {
493                         f();
494                         return false;
495                 } else {
496                         return false;
497                 }
498         } else {
499                 return true;
500         }
501 }
502
503 function getInitParam(key) {
504         return init_params[key];
505 }
506
507 function setInitParam(key, value) {
508         init_params[key] = value;
509 }
510
511 function fatalError(code, msg, ext_info) {
512         try {
513
514                 if (code == 6) {
515                         window.location.href = "index.php";
516                 } else if (code == 5) {
517                         window.location.href = "db-updater.php";
518                 } else {
519
520                         if (msg == "") msg = "Unknown error";
521
522                         if (ext_info) {
523                                 if (ext_info.responseText) {
524                                         ext_info = ext_info.responseText;
525                                 }
526                         }
527
528                         if (ERRORS && ERRORS[code] && !msg) {
529                                 msg = ERRORS[code];
530                         }
531
532                         var content = "<div><b>Error code:</b> " + code + "</div>" +
533                                 "<p>" + msg + "</p>";
534
535                         if (ext_info) {
536                                 content = content + "<div><b>Additional information:</b></div>" +
537                                         "<textarea style='width: 100%' readonly=\"1\">" +
538                                         ext_info + "</textarea>";
539                         }
540
541                         var dialog = new dijit.Dialog({
542                                 title: "Fatal error",
543                                 style: "width: 600px",
544                                 content: content});
545
546                         dialog.show();
547
548                 }
549
550                 return false;
551
552         } catch (e) {
553                 exception_error("fatalError", e);
554         }
555 }
556
557 /* function filterDlgCheckType(sender) {
558
559         try {
560
561                 var ftype = sender.value;
562
563                 // if selected filter type is 5 (Date) enable the modifier dropbox
564                 if (ftype == 5) {
565                         Element.show("filterDlg_dateModBox");
566                         Element.show("filterDlg_dateChkBox");
567                 } else {
568                         Element.hide("filterDlg_dateModBox");
569                         Element.hide("filterDlg_dateChkBox");
570
571                 }
572
573         } catch (e) {
574                 exception_error("filterDlgCheckType", e);
575         }
576
577 } */
578
579 function filterDlgCheckAction(sender) {
580
581         try {
582
583                 var action = sender.value;
584
585                 var action_param = $("filterDlg_paramBox");
586
587                 if (!action_param) {
588                         console.log("filterDlgCheckAction: can't find action param box!");
589                         return;
590                 }
591
592                 // if selected action supports parameters, enable params field
593                 if (action == 4 || action == 6 || action == 7) {
594                         new Effect.Appear(action_param, {duration : 0.5});
595                         if (action != 7) {
596                                 Element.show(dijit.byId("filterDlg_actionParam").domNode);
597                                 Element.hide(dijit.byId("filterDlg_actionParamLabel").domNode);
598                         } else {
599                                 Element.show(dijit.byId("filterDlg_actionParamLabel").domNode);
600                                 Element.hide(dijit.byId("filterDlg_actionParam").domNode);
601                         }
602                 } else {
603                         Element.hide(action_param);
604                 }
605
606         } catch (e) {
607                 exception_error("filterDlgCheckAction", e);
608         }
609
610 }
611
612 function filterDlgCheckDate() {
613         try {
614                 var dialog = dijit.byId("filterEditDlg");
615
616                 var reg_exp = dialog.attr('value').reg_exp;
617
618                 var query = "?op=rpc&method=checkDate&date=" + reg_exp;
619
620                 new Ajax.Request("backend.php", {
621                         parameters: query,
622                         onComplete: function(transport) {
623
624                                 var reply = JSON.parse(transport.responseText);
625
626                                 if (reply['result'] == true) {
627                                         alert(__("Date syntax appears to be correct:") + " " + reply['date']);
628                                         return;
629                                 } else {
630                                         alert(__("Date syntax is incorrect."));
631                                 }
632
633                         } });
634
635
636         } catch (e) {
637                 exception_error("filterDlgCheckDate", e);
638         }
639 }
640
641 function explainError(code) {
642         return displayDlg("explainError", code);
643 }
644
645 function loading_set_progress(p) {
646         try {
647                 loading_progress += p;
648
649                 if (dijit.byId("loading_bar"))
650                         dijit.byId("loading_bar").update({progress: loading_progress});
651
652                 if (loading_progress >= 90)
653                         remove_splash();
654
655         } catch (e) {
656                 exception_error("loading_set_progress", e);
657         }
658 }
659
660 function remove_splash() {
661
662         if (Element.visible("overlay")) {
663                 console.log("about to remove splash, OMG!");
664                 Element.hide("overlay");
665                 console.log("removed splash!");
666         }
667 }
668
669 function transport_error_check(transport) {
670         try {
671                 if (transport.responseXML) {
672                         var error = transport.responseXML.getElementsByTagName("error")[0];
673
674                         if (error) {
675                                 var code = error.getAttribute("error-code");
676                                 var msg = error.getAttribute("error-msg");
677                                 if (code != 0) {
678                                         fatalError(code, msg);
679                                         return false;
680                                 }
681                         }
682                 }
683         } catch (e) {
684                 exception_error("check_for_error_xml", e);
685         }
686         return true;
687 }
688
689 function strip_tags(s) {
690         return s.replace(/<\/?[^>]+(>|$)/g, "");
691 }
692
693 function truncate_string(s, length) {
694         if (!length) length = 30;
695         var tmp = s.substring(0, length);
696         if (s.length > length) tmp += "&hellip;";
697         return tmp;
698 }
699
700 function hotkey_prefix_timeout() {
701         try {
702
703                 var date = new Date();
704                 var ts = Math.round(date.getTime() / 1000);
705
706                 if (hotkey_prefix_pressed && ts - hotkey_prefix_pressed >= 5) {
707                         console.log("hotkey_prefix seems to be stuck, aborting");
708                         hotkey_prefix_pressed = false;
709                         hotkey_prefix = false;
710                         Element.hide('cmdline');
711                 }
712
713                 setTimeout("hotkey_prefix_timeout()", 1000);
714
715         } catch  (e) {
716                 exception_error("hotkey_prefix_timeout", e);
717         }
718 }
719
720 function uploadIconHandler(rc) {
721         try {
722                 switch (rc) {
723                         case 0:
724                                 notify_info("Upload complete.");
725                                 if (inPreferences()) {
726                                         updateFeedList();
727                                 } else {
728                                         setTimeout('updateFeedList(false, false)', 50);
729                                 }
730                                 break;
731                         case 1:
732                                 notify_error("Upload failed: icon is too big.");
733                                 break;
734                         case 2:
735                                 notify_error("Upload failed.");
736                                 break;
737                 }
738
739         } catch (e) {
740                 exception_error("uploadIconHandler", e);
741         }
742 }
743
744 function removeFeedIcon(id) {
745
746         try {
747
748                 if (confirm(__("Remove stored feed icon?"))) {
749                         var query = "backend.php?op=pref-feeds&method=removeicon&feed_id=" + param_escape(id);
750
751                         console.log(query);
752
753                         notify_progress("Removing feed icon...", true);
754
755                         new Ajax.Request("backend.php", {
756                                 parameters: query,
757                                 onComplete: function(transport) {
758                                         notify_info("Feed icon removed.");
759                                         if (inPreferences()) {
760                                                 updateFeedList();
761                                         } else {
762                                                 setTimeout('updateFeedList(false, false)', 50);
763                                         }
764                                 } });
765                 }
766
767                 return false;
768         } catch (e) {
769                 exception_error("removeFeedIcon", e);
770         }
771 }
772
773 function uploadFeedIcon() {
774
775         try {
776
777                 var file = $("icon_file");
778
779                 if (file.value.length == 0) {
780                         alert(__("Please select an image file to upload."));
781                 } else {
782                         if (confirm(__("Upload new icon for this feed?"))) {
783                                 notify_progress("Uploading, please wait...", true);
784                                 return true;
785                         }
786                 }
787
788                 return false;
789
790         } catch (e) {
791                 exception_error("uploadFeedIcon", e);
792         }
793 }
794
795 function addLabel(select, callback) {
796
797         try {
798
799                 var caption = prompt(__("Please enter label caption:"), "");
800
801                 if (caption != undefined) {
802
803                         if (caption == "") {
804                                 alert(__("Can't create label: missing caption."));
805                                 return false;
806                         }
807
808                         var query = "?op=pref-labels&method=add&caption=" +
809                                 param_escape(caption);
810
811                         if (select)
812                                 query += "&output=select";
813
814                         notify_progress("Loading, please wait...", true);
815
816                         if (inPreferences() && !select) active_tab = "labelConfig";
817
818                         new Ajax.Request("backend.php", {
819                                 parameters: query,
820                                 onComplete: function(transport) {
821                                         if (callback) {
822                                                 callback(transport);
823                                         } else if (inPreferences()) {
824                                                 updateLabelList();
825                                         } else {
826                                                 updateFeedList();
827                                         }
828                         } });
829
830                 }
831
832         } catch (e) {
833                 exception_error("addLabel", e);
834         }
835 }
836
837 function quickAddFeed() {
838         try {
839                 var query = "backend.php?op=dlg&method=quickAddFeed";
840
841                 // overlapping widgets
842                 if (dijit.byId("batchSubDlg")) dijit.byId("batchSubDlg").destroyRecursive();
843                 if (dijit.byId("feedAddDlg"))   dijit.byId("feedAddDlg").destroyRecursive();
844
845                 var dialog = new dijit.Dialog({
846                         id: "feedAddDlg",
847                         title: __("Subscribe to Feed"),
848                         style: "width: 600px",
849                         execute: function() {
850                                 if (this.validate()) {
851                                         console.log(dojo.objectToQuery(this.attr('value')));
852
853                                         var feed_url = this.attr('value').feed;
854
855                                         Element.show("feed_add_spinner");
856
857                                         new Ajax.Request("backend.php", {
858                                                 parameters: dojo.objectToQuery(this.attr('value')),
859                                                 onComplete: function(transport) {
860                                                         try {
861
862                                                                 var reply = JSON.parse(transport.responseText);
863
864                                                                 var rc = reply['result'];
865
866                                                                 notify('');
867                                                                 Element.hide("feed_add_spinner");
868
869                                                                 console.log("GOT RC: " + rc);
870
871                                                                 switch (parseInt(rc['code'])) {
872                                                                 case 1:
873                                                                         dialog.hide();
874                                                                         notify_info(__("Subscribed to %s").replace("%s", feed_url));
875
876                                                                         updateFeedList();
877                                                                         break;
878                                                                 case 2:
879                                                                         alert(__("Specified URL seems to be invalid."));
880                                                                         break;
881                                                                 case 3:
882                                                                         alert(__("Specified URL doesn't seem to contain any feeds."));
883                                                                         break;
884                                                                 case 4:
885                                                                         /* notify_progress("Searching for feed urls...", true);
886
887                                                                         new Ajax.Request("backend.php", {
888                                                                                 parameters: 'op=rpc&method=extractfeedurls&url=' + param_escape(feed_url),
889                                                                                 onComplete: function(transport, dialog, feed_url) {
890
891                                                                                         notify('');
892
893                                                                                         var reply = JSON.parse(transport.responseText);
894
895                                                                                         var feeds = reply['urls'];
896
897                                                                                         console.log(transport.responseText);
898
899                                                                                         var select = dijit.byId("feedDlg_feedContainerSelect");
900
901                                                                                         while (select.getOptions().length > 0)
902                                                                                                 select.removeOption(0);
903
904                                                                                         var count = 0;
905                                                                                         for (var feedUrl in feeds) {
906                                                                                                 select.addOption({value: feedUrl, label: feeds[feedUrl]});
907                                                                                                 count++;
908                                                                                         }
909
910 //                                                                                      if (count > 5) count = 5;
911 //                                                                                      select.size = count;
912
913                                                                                         Effect.Appear('feedDlg_feedsContainer', {duration : 0.5});
914                                                                                 }
915                                                                         });
916                                                                         break; */
917
918                                                                         feeds = rc['feeds'];
919
920                                                                         var select = dijit.byId("feedDlg_feedContainerSelect");
921
922                                                                         while (select.getOptions().length > 0)
923                                                                                 select.removeOption(0);
924
925                                                                         var count = 0;
926                                                                         for (var feedUrl in feeds) {
927                                                                                 select.addOption({value: feedUrl, label: feeds[feedUrl]});
928                                                                                 count++;
929                                                                         }
930
931                                                                         Effect.Appear('feedDlg_feedsContainer', {duration : 0.5});
932
933                                                                         break;
934                                                                 case 5:
935                                                                         alert(__("Couldn't download the specified URL: %s").
936                                                                                         replace("%s", rc['message']));
937                                                                         break;
938                                                                 case 0:
939                                                                         alert(__("You are already subscribed to this feed."));
940                                                                         break;
941                                                                 }
942
943                                                         } catch (e) {
944                                                                 exception_error("subscribeToFeed", e, transport);
945                                                         }
946
947                                                 } });
948
949                                         }
950                         },
951                         href: query});
952
953                 dialog.show();
954         } catch (e) {
955                 exception_error("quickAddFeed", e);
956         }
957 }
958
959 function createNewRuleElement(parentNode, replaceNode) {
960         try {
961                 var form = document.forms["filter_new_rule_form"];
962
963                 form.reg_exp.value = form.reg_exp.value.replace(/(<([^>]+)>)/ig,"");
964
965                 var query = "backend.php?op=pref-filters&method=printrulename&rule="+
966                         param_escape(dojo.formToJson(form));
967
968                 console.log(query);
969
970                 new Ajax.Request("backend.php", {
971                         parameters: query,
972                         onComplete: function (transport) {
973                                 try {
974                                         var li = dojo.create("li");
975
976                                         var cb = dojo.create("input", { type: "checkbox" }, li);
977
978                                         new dijit.form.CheckBox({
979                                                 onChange: function() {
980                                                         toggleSelectListRow2(this) },
981                                         }, cb);
982
983                                         dojo.create("input", { type: "hidden",
984                                                 name: "rule[]",
985                                                 value: dojo.formToJson(form) }, li);
986
987                                         dojo.create("span", {
988                                                 onclick: function() {
989                                                         dijit.byId('filterEditDlg').editRule(this);
990                                                 },
991                                                 innerHTML: transport.responseText }, li);
992
993                                         if (replaceNode) {
994                                                 parentNode.replaceChild(li, replaceNode);
995                                         } else {
996                                                 parentNode.appendChild(li);
997                                         }
998                                 } catch (e) {
999                                         exception_error("createNewRuleElement", e);
1000                                 }
1001                 } });
1002         } catch (e) {
1003                 exception_error("createNewRuleElement", e);
1004         }
1005 }
1006
1007 function createNewActionElement(parentNode, replaceNode) {
1008         try {
1009                 var form = document.forms["filter_new_action_form"];
1010
1011                 if (form.action_id.value == 7) {
1012                         form.action_param.value = form.action_param_label.value;
1013                 }
1014
1015                 var query = "backend.php?op=pref-filters&method=printactionname&action="+
1016                         param_escape(dojo.formToJson(form));
1017
1018                 console.log(query);
1019
1020                 new Ajax.Request("backend.php", {
1021                         parameters: query,
1022                         onComplete: function (transport) {
1023                                 try {
1024                                         var li = dojo.create("li");
1025
1026                                         var cb = dojo.create("input", { type: "checkbox" }, li);
1027
1028                                         new dijit.form.CheckBox({
1029                                                 onChange: function() {
1030                                                         toggleSelectListRow2(this) },
1031                                         }, cb);
1032
1033                                         dojo.create("input", { type: "hidden",
1034                                                 name: "action[]",
1035                                                 value: dojo.formToJson(form) }, li);
1036
1037                                         dojo.create("span", {
1038                                                 onclick: function() {
1039                                                         dijit.byId('filterEditDlg').editAction(this);
1040                                                 },
1041                                                 innerHTML: transport.responseText }, li);
1042
1043                                         if (replaceNode) {
1044                                                 parentNode.replaceChild(li, replaceNode);
1045                                         } else {
1046                                                 parentNode.appendChild(li);
1047                                         }
1048
1049                                 } catch (e) {
1050                                         exception_error("createNewActionElement", e);
1051                                 }
1052                         } });
1053         } catch (e) {
1054                 exception_error("createNewActionElement", e);
1055         }
1056 }
1057
1058
1059 function addFilterRule(replaceNode, ruleStr) {
1060         try {
1061                 if (dijit.byId("filterNewRuleDlg"))
1062                         dijit.byId("filterNewRuleDlg").destroyRecursive();
1063
1064                 var query = "backend.php?op=pref-filters&method=newrule&rule=" +
1065                         param_escape(ruleStr);
1066
1067                 var rule_dlg = new dijit.Dialog({
1068                         id: "filterNewRuleDlg",
1069                         title: ruleStr ? __("Edit rule") : __("Add rule"),
1070                         style: "width: 600px",
1071                         execute: function() {
1072                                 if (this.validate()) {
1073                                         createNewRuleElement($("filterDlg_Matches"), replaceNode);
1074                                         this.hide();
1075                                 }
1076                         },
1077                         href: query});
1078
1079                 rule_dlg.show();
1080         } catch (e) {
1081                 exception_error("addFilterRule", e);
1082         }
1083 }
1084
1085 function addFilterAction(replaceNode, actionStr) {
1086         try {
1087                 if (dijit.byId("filterNewActionDlg"))
1088                         dijit.byId("filterNewActionDlg").destroyRecursive();
1089
1090                 var query = "backend.php?op=pref-filters&method=newaction&action=" +
1091                         param_escape(actionStr);
1092
1093                 var rule_dlg = new dijit.Dialog({
1094                         id: "filterNewActionDlg",
1095                         title: actionStr ? __("Edit action") : __("Add action"),
1096                         style: "width: 600px",
1097                         execute: function() {
1098                                 if (this.validate()) {
1099                                         createNewActionElement($("filterDlg_Actions"), replaceNode);
1100                                         this.hide();
1101                                 }
1102                         },
1103                         href: query});
1104
1105                 rule_dlg.show();
1106         } catch (e) {
1107                 exception_error("addFilterAction", e);
1108         }
1109 }
1110
1111 function quickAddFilter() {
1112         try {
1113                 var query = "";
1114                 if (!inPreferences()) {
1115                         query = "backend.php?op=pref-filters&method=newfilter&feed=" +
1116                                 param_escape(getActiveFeedId()) + "&is_cat=" +
1117                                 param_escape(activeFeedIsCat());
1118                 } else {
1119                         query = "backend.php?op=pref-filters&method=newfilter";
1120                 }
1121
1122                 console.log(query);
1123
1124                 if (dijit.byId("feedEditDlg"))
1125                         dijit.byId("feedEditDlg").destroyRecursive();
1126
1127                 if (dijit.byId("filterEditDlg"))
1128                         dijit.byId("filterEditDlg").destroyRecursive();
1129
1130                 dialog = new dijit.Dialog({
1131                         id: "filterEditDlg",
1132                         title: __("Create Filter"),
1133                         style: "width: 600px",
1134                         test: function() {
1135                                 var query = "backend.php?" + dojo.formToQuery("filter_new_form") + "&savemode=test";
1136
1137                                 if (dijit.byId("filterTestDlg"))
1138                                         dijit.byId("filterTestDlg").destroyRecursive();
1139
1140                                 var test_dlg = new dijit.Dialog({
1141                                         id: "filterTestDlg",
1142                                         title: "Test Filter",
1143                                         style: "width: 600px",
1144                                         href: query});
1145
1146                                 test_dlg.show();
1147                         },
1148                         selectRules: function(select) {
1149                                 $$("#filterDlg_Matches input[type=checkbox]").each(function(e) {
1150                                         e.checked = select;
1151                                         if (select)
1152                                                 e.parentNode.addClassName("Selected");
1153                                         else
1154                                                 e.parentNode.removeClassName("Selected");
1155                                 });
1156                         },
1157                         selectActions: function(select) {
1158                                 $$("#filterDlg_Actions input[type=checkbox]").each(function(e) {
1159                                         e.checked = select;
1160
1161                                         if (select)
1162                                                 e.parentNode.addClassName("Selected");
1163                                         else
1164                                                 e.parentNode.removeClassName("Selected");
1165
1166                                 });
1167                         },
1168                         editRule: function(e) {
1169                                 var li = e.parentNode;
1170                                 var rule = li.getElementsByTagName("INPUT")[1].value;
1171                                 addFilterRule(li, rule);
1172                         },
1173                         editAction: function(e) {
1174                                 var li = e.parentNode;
1175                                 var action = li.getElementsByTagName("INPUT")[1].value;
1176                                 addFilterAction(li, action);
1177                         },
1178                         addAction: function() { addFilterAction(); },
1179                         addRule: function() { addFilterRule(); },
1180                         deleteAction: function() {
1181                                 $$("#filterDlg_Actions li.[class*=Selected]").each(function(e) { e.parentNode.removeChild(e) });
1182                         },
1183                         deleteRule: function() {
1184                                 $$("#filterDlg_Matches li.[class*=Selected]").each(function(e) { e.parentNode.removeChild(e) });
1185                         },
1186                         execute: function() {
1187                                 if (this.validate()) {
1188
1189                                         var query = dojo.formToQuery("filter_new_form");
1190
1191                                         console.log(query);
1192
1193                                         new Ajax.Request("backend.php", {
1194                                                 parameters: query,
1195                                                 onComplete: function (transport) {
1196                                                         if (inPreferences()) {
1197                                                                 updateFilterList();
1198                                                         }
1199
1200                                                         dialog.hide();
1201                                         } });
1202                                 }
1203                         },
1204                         href: query});
1205
1206                 if (!inPreferences()) {
1207                         var lh = dojo.connect(dialog, "onLoad", function(){
1208                                 dojo.disconnect(lh);
1209
1210                                 var query = "op=rpc&method=getlinktitlebyid&id=" + getActiveArticleId();
1211
1212                                 new Ajax.Request("backend.php", {
1213                                 parameters: query,
1214                                 onComplete: function(transport) {
1215                                         var reply = JSON.parse(transport.responseText);
1216
1217                                         var title = false;
1218
1219                                         if (reply && reply) title = reply.title;
1220
1221                                         if (title || getActiveFeedId() || activeFeedIsCat()) {
1222
1223                                                 console.log(title + " " + getActiveFeedId());
1224
1225                                                 var feed_id = activeFeedIsCat() ? 'CAT:' + parseInt(getActiveFeedId()) :
1226                                                         getActiveFeedId();
1227
1228                                                 var rule = { reg_exp: title, feed_id: feed_id, filter_type: 1 };
1229
1230                                                 addFilterRule(null, dojo.toJson(rule));
1231                                         }
1232
1233                                 } });
1234
1235                         });
1236                 }
1237
1238                 dialog.show();
1239
1240         } catch (e) {
1241                 exception_error("quickAddFilter", e);
1242         }
1243 }
1244
1245 function resetPubSub(feed_id, title) {
1246
1247         var msg = __("Reset subscription? Tiny Tiny RSS will try to subscribe to the notification hub again on next feed update.").replace("%s", title);
1248
1249         if (title == undefined || confirm(msg)) {
1250                 notify_progress("Loading, please wait...");
1251
1252                 var query = "?op=pref-feeds&quiet=1&method=resetPubSub&ids=" + feed_id;
1253
1254                 new Ajax.Request("backend.php", {
1255                         parameters: query,
1256                         onComplete: function(transport) {
1257                                 dijit.byId("pubsubReset_Btn").attr('disabled', true);
1258                                 notify_info("Subscription reset.");
1259                         } });
1260         }
1261
1262         return false;
1263 }
1264
1265
1266 function unsubscribeFeed(feed_id, title) {
1267
1268         var msg = __("Unsubscribe from %s?").replace("%s", title);
1269
1270         if (title == undefined || confirm(msg)) {
1271                 notify_progress("Removing feed...");
1272
1273                 var query = "?op=pref-feeds&quiet=1&method=remove&ids=" + feed_id;
1274
1275                 new Ajax.Request("backend.php", {
1276                         parameters: query,
1277                         onComplete: function(transport) {
1278
1279                                         if (dijit.byId("feedEditDlg")) dijit.byId("feedEditDlg").hide();
1280
1281                                         if (inPreferences()) {
1282                                                 updateFeedList();
1283                                         } else {
1284                                                 if (feed_id == getActiveFeedId())
1285                                                         setTimeout("viewfeed(-5)", 100);
1286
1287                                                 if (feed_id < 0) updateFeedList();
1288                                         }
1289
1290                                 } });
1291         }
1292
1293         return false;
1294 }
1295
1296
1297 function backend_sanity_check_callback(transport) {
1298
1299         try {
1300
1301                 if (sanity_check_done) {
1302                         fatalError(11, "Sanity check request received twice. This can indicate "+
1303                       "presence of Firebug or some other disrupting extension. "+
1304                                 "Please disable it and try again.");
1305                         return;
1306                 }
1307
1308                 var reply = JSON.parse(transport.responseText);
1309
1310                 if (!reply) {
1311                         fatalError(3, "Sanity check: invalid RPC reply", transport.responseText);
1312                         return;
1313                 }
1314
1315                 var error_code = reply['error']['code'];
1316
1317                 if (error_code && error_code != 0) {
1318                         return fatalError(error_code, reply['error']['message']);
1319                 }
1320
1321                 console.log("sanity check ok");
1322
1323                 var params = reply['init-params'];
1324
1325                 if (params) {
1326                         console.log('reading init-params...');
1327
1328                         if (params) {
1329                                 for (k in params) {
1330                                         var v = params[k];
1331                                         console.log("IP: " + k + " => " + v);
1332
1333                                         if (k == "label_base_index") _label_base_index = parseInt(v);
1334                                 }
1335                         }
1336
1337                         init_params = params;
1338                 }
1339
1340                 sanity_check_done = true;
1341
1342                 init_second_stage();
1343
1344         } catch (e) {
1345                 exception_error("backend_sanity_check_callback", e, transport);
1346         }
1347 }
1348
1349 /*function has_local_storage() {
1350         try {
1351                 return 'sessionStorage' in window && window['sessionStorage'] != null;
1352         } catch (e) {
1353                 return false;
1354         }
1355 } */
1356
1357 function catSelectOnChange(elem) {
1358         try {
1359 /*              var value = elem[elem.selectedIndex].value;
1360                 var def = elem.getAttribute('default');
1361
1362                 if (value == "ADD_CAT") {
1363
1364                         if (def)
1365                                 dropboxSelect(elem, def);
1366                         else
1367                                 elem.selectedIndex = 0;
1368
1369                         quickAddCat(elem);
1370                 } */
1371
1372         } catch (e) {
1373                 exception_error("catSelectOnChange", e);
1374         }
1375 }
1376
1377 function quickAddCat(elem) {
1378         try {
1379                 var cat = prompt(__("Please enter category title:"));
1380
1381                 if (cat) {
1382
1383                         var query = "?op=rpc&method=quickAddCat&cat=" + param_escape(cat);
1384
1385                         notify_progress("Loading, please wait...", true);
1386
1387                         new Ajax.Request("backend.php", {
1388                                 parameters: query,
1389                                 onComplete: function (transport) {
1390                                         var response = transport.responseXML;
1391                                         var select = response.getElementsByTagName("select")[0];
1392                                         var options = select.getElementsByTagName("option");
1393
1394                                         dropbox_replace_options(elem, options);
1395
1396                                         notify('');
1397
1398                         } });
1399
1400                 }
1401
1402         } catch (e) {
1403                 exception_error("quickAddCat", e);
1404         }
1405 }
1406
1407 function genUrlChangeKey(feed, is_cat) {
1408
1409         try {
1410                 var ok = confirm(__("Generate new syndication address for this feed?"));
1411
1412                 if (ok) {
1413
1414                         notify_progress("Trying to change address...", true);
1415
1416                         var query = "?op=rpc&method=regenFeedKey&id=" + param_escape(feed) +
1417                                 "&is_cat=" + param_escape(is_cat);
1418
1419                         new Ajax.Request("backend.php", {
1420                                 parameters: query,
1421                                 onComplete: function(transport) {
1422                                                 var reply = JSON.parse(transport.responseText);
1423                                                 var new_link = reply.link;
1424
1425                                                 var e = $('gen_feed_url');
1426
1427                                                 if (new_link) {
1428
1429                                                         e.innerHTML = e.innerHTML.replace(/\&amp;key=.*$/,
1430                                                                 "&amp;key=" + new_link);
1431
1432                                                         e.href = e.href.replace(/\&key=.*$/,
1433                                                                 "&key=" + new_link);
1434
1435                                                         new Effect.Highlight(e);
1436
1437                                                         notify('');
1438
1439                                                 } else {
1440                                                         notify_error("Could not change feed URL.");
1441                                                 }
1442                                 } });
1443                 }
1444         } catch (e) {
1445                 exception_error("genUrlChangeKey", e);
1446         }
1447         return false;
1448 }
1449
1450 function labelSelectOnChange(elem) {
1451         try {
1452 /*              var value = elem[elem.selectedIndex].value;
1453                 var def = elem.getAttribute('default');
1454
1455                 if (value == "ADD_LABEL") {
1456
1457                         if (def)
1458                                 dropboxSelect(elem, def);
1459                         else
1460                                 elem.selectedIndex = 0;
1461
1462                         addLabel(elem, function(transport) {
1463
1464                                         try {
1465
1466                                                 var response = transport.responseXML;
1467                                                 var select = response.getElementsByTagName("select")[0];
1468                                                 var options = select.getElementsByTagName("option");
1469
1470                                                 dropbox_replace_options(elem, options);
1471
1472                                                 notify('');
1473                                         } catch (e) {
1474                                                 exception_error("addLabel", e);
1475                                         }
1476                         });
1477                 } */
1478
1479         } catch (e) {
1480                 exception_error("labelSelectOnChange", e);
1481         }
1482 }
1483
1484 function dropbox_replace_options(elem, options) {
1485
1486         try {
1487                 while (elem.hasChildNodes())
1488                         elem.removeChild(elem.firstChild);
1489
1490                 var sel_idx = -1;
1491
1492                 for (var i = 0; i < options.length; i++) {
1493                         var text = options[i].firstChild.nodeValue;
1494                         var value = options[i].getAttribute("value");
1495
1496                         if (value == undefined) value = text;
1497
1498                         var issel = options[i].getAttribute("selected") == "1";
1499
1500                         var option = new Option(text, value, issel);
1501
1502                         if (options[i].getAttribute("disabled"))
1503                                 option.setAttribute("disabled", true);
1504
1505                         elem.insert(option);
1506
1507                         if (issel) sel_idx = i;
1508                 }
1509
1510                 // Chrome doesn't seem to just select stuff when you pass new Option(x, y, true)
1511                 if (sel_idx >= 0) elem.selectedIndex = sel_idx;
1512
1513         } catch (e) {
1514                 exception_error("dropbox_replace_options", e);
1515         }
1516 }
1517
1518 // mode = all, none, invert
1519 function selectTableRows(id, mode) {
1520         try {
1521                 var rows = $(id).rows;
1522
1523                 for (var i = 0; i < rows.length; i++) {
1524                         var row = rows[i];
1525                         var cb = false;
1526                         var dcb = false;
1527
1528                         if (row.id && row.className) {
1529                                 var bare_id = row.id.replace(/^[A-Z]*?-/, "");
1530                                 var inputs = rows[i].getElementsByTagName("input");
1531
1532                                 for (var j = 0; j < inputs.length; j++) {
1533                                         var input = inputs[j];
1534
1535                                         if (input.getAttribute("type") == "checkbox" &&
1536                                                         input.id.match(bare_id)) {
1537
1538                                                 cb = input;
1539                                                 dcb = dijit.getEnclosingWidget(cb);
1540                                                 break;
1541                                         }
1542                                 }
1543
1544                                 if (cb || dcb) {
1545                                         var issel = row.hasClassName("Selected");
1546
1547                                         if (mode == "all" && !issel) {
1548                                                 row.addClassName("Selected");
1549                                                 cb.checked = true;
1550                                                 if (dcb) dcb.set("checked", true);
1551                                         } else if (mode == "none" && issel) {
1552                                                 row.removeClassName("Selected");
1553                                                 cb.checked = false;
1554                                                 if (dcb) dcb.set("checked", false);
1555
1556                                         } else if (mode == "invert") {
1557
1558                                                 if (issel) {
1559                                                         row.removeClassName("Selected");
1560                                                         cb.checked = false;
1561                                                         if (dcb) dcb.set("checked", false);
1562                                                 } else {
1563                                                         row.addClassName("Selected");
1564                                                         cb.checked = true;
1565                                                         if (dcb) dcb.set("checked", true);
1566                                                 }
1567                                         }
1568                                 }
1569                         }
1570                 }
1571
1572         } catch (e) {
1573                 exception_error("selectTableRows", e);
1574
1575         }
1576 }
1577
1578 function getSelectedTableRowIds(id) {
1579         var rows = [];
1580
1581         try {
1582                 var elem_rows = $(id).rows;
1583
1584                 for (var i = 0; i < elem_rows.length; i++) {
1585                         if (elem_rows[i].hasClassName("Selected")) {
1586                                 var bare_id = elem_rows[i].id.replace(/^[A-Z]*?-/, "");
1587                                 rows.push(bare_id);
1588                         }
1589                 }
1590
1591         } catch (e) {
1592                 exception_error("getSelectedTableRowIds", e);
1593         }
1594
1595         return rows;
1596 }
1597
1598 function editFeed(feed, event) {
1599         try {
1600                 if (feed <= 0)
1601                         return alert(__("You can't edit this kind of feed."));
1602
1603                 var query = "backend.php?op=pref-feeds&method=editfeed&id=" +
1604                         param_escape(feed);
1605
1606                 console.log(query);
1607
1608                 if (dijit.byId("filterEditDlg"))
1609                         dijit.byId("filterEditDlg").destroyRecursive();
1610
1611                 if (dijit.byId("feedEditDlg"))
1612                         dijit.byId("feedEditDlg").destroyRecursive();
1613
1614                 dialog = new dijit.Dialog({
1615                         id: "feedEditDlg",
1616                         title: __("Edit Feed"),
1617                         style: "width: 600px",
1618                         execute: function() {
1619                                 if (this.validate()) {
1620 //                                      console.log(dojo.objectToQuery(this.attr('value')));
1621
1622                                         notify_progress("Saving data...", true);
1623
1624                                         new Ajax.Request("backend.php", {
1625                                                 parameters: dojo.objectToQuery(dialog.attr('value')),
1626                                                 onComplete: function(transport) {
1627                                                         dialog.hide();
1628                                                         notify('');
1629                                                         updateFeedList();
1630                                         }});
1631                                 }
1632                         },
1633                         href: query});
1634
1635                 dialog.show();
1636
1637         } catch (e) {
1638                 exception_error("editFeed", e);
1639         }
1640 }
1641
1642 function feedBrowser() {
1643         try {
1644                 var query = "backend.php?op=dlg&method=feedBrowser";
1645
1646                 if (dijit.byId("feedAddDlg"))
1647                         dijit.byId("feedAddDlg").hide();
1648
1649                 if (dijit.byId("feedBrowserDlg"))
1650                         dijit.byId("feedBrowserDlg").destroyRecursive();
1651
1652                 var dialog = new dijit.Dialog({
1653                         id: "feedBrowserDlg",
1654                         title: __("More Feeds"),
1655                         style: "width: 600px",
1656                         getSelectedFeedIds: function() {
1657                                 var list = $$("#browseFeedList li[id*=FBROW]");
1658                                 var selected = new Array();
1659
1660                                 list.each(function(child) {
1661                                         var id = child.id.replace("FBROW-", "");
1662
1663                                         if (child.hasClassName('Selected')) {
1664                                                 selected.push(id);
1665                                         }
1666                                 });
1667
1668                                 return selected;
1669                         },
1670                         getSelectedFeeds: function() {
1671                                 var list = $$("#browseFeedList li.Selected");
1672                                 var selected = new Array();
1673
1674                                 list.each(function(child) {
1675                                         var title = child.getElementsBySelector("span.fb_feedTitle")[0].innerHTML;
1676                                         var url = child.getElementsBySelector("a.fb_feedUrl")[0].href;
1677
1678                                         selected.push([title,url]);
1679
1680                                 });
1681
1682                                 return selected;
1683                         },
1684
1685                         subscribe: function() {
1686                                 var mode = this.attr('value').mode;
1687                                 var selected = [];
1688
1689                                 if (mode == "1")
1690                                         selected = this.getSelectedFeeds();
1691                                 else
1692                                         selected = this.getSelectedFeedIds();
1693
1694                                 if (selected.length > 0) {
1695                                         dijit.byId("feedBrowserDlg").hide();
1696
1697                                         notify_progress("Loading, please wait...", true);
1698
1699                                         // we use dojo.toJson instead of JSON.stringify because
1700                                         // it somehow escapes everything TWICE, at least in Chrome 9
1701
1702                                         var query = "?op=rpc&method=massSubscribe&payload="+
1703                                                 param_escape(dojo.toJson(selected)) + "&mode=" + param_escape(mode);
1704
1705                                         console.log(query);
1706
1707                                         new Ajax.Request("backend.php", {
1708                                                 parameters: query,
1709                                                 onComplete: function(transport) {
1710                                                         notify('');
1711                                                         updateFeedList();
1712                                                 } });
1713
1714                                 } else {
1715                                         alert(__("No feeds are selected."));
1716                                 }
1717
1718                         },
1719                         update: function() {
1720                                 var query = dojo.objectToQuery(dialog.attr('value'));
1721
1722                                 Element.show('feed_browser_spinner');
1723
1724                                 new Ajax.Request("backend.php", {
1725                                         parameters: query,
1726                                         onComplete: function(transport) {
1727                                                 notify('');
1728
1729                                                 Element.hide('feed_browser_spinner');
1730
1731                                                 var c = $("browseFeedList");
1732
1733                                                 var reply = JSON.parse(transport.responseText);
1734
1735                                                 var r = reply['content'];
1736                                                 var mode = reply['mode'];
1737
1738                                                 if (c && r) {
1739                                                         c.innerHTML = r;
1740                                                 }
1741
1742                                                 dojo.parser.parse("browseFeedList");
1743
1744                                                 if (mode == 2) {
1745                                                         Element.show(dijit.byId('feed_archive_remove').domNode);
1746                                                 } else {
1747                                                         Element.hide(dijit.byId('feed_archive_remove').domNode);
1748                                                 }
1749
1750                                         } });
1751                         },
1752                         removeFromArchive: function() {
1753                                 var selected = this.getSelectedFeedIds();
1754
1755                                 if (selected.length > 0) {
1756
1757                                         var pr = __("Remove selected feeds from the archive? Feeds with stored articles will not be removed.");
1758
1759                                         if (confirm(pr)) {
1760                                                 Element.show('feed_browser_spinner');
1761
1762                                                 var query = "?op=rpc&method=remarchive&ids=" +
1763                                                         param_escape(selected.toString());;
1764
1765                                                 new Ajax.Request("backend.php", {
1766                                                         parameters: query,
1767                                                         onComplete: function(transport) {
1768                                                                 dialog.update();
1769                                                         } });
1770                                         }
1771                                 }
1772                         },
1773                         execute: function() {
1774                                 if (this.validate()) {
1775                                         this.subscribe();
1776                                 }
1777                         },
1778                         href: query});
1779
1780                 dialog.show();
1781
1782         } catch (e) {
1783                 exception_error("editFeed", e);
1784         }
1785 }
1786
1787 function showFeedsWithErrors() {
1788         try {
1789                 var query = "backend.php?op=pref-feeds&method=feedsWithErrors";
1790
1791                 if (dijit.byId("errorFeedsDlg"))
1792                         dijit.byId("errorFeedsDlg").destroyRecursive();
1793
1794                 dialog = new dijit.Dialog({
1795                         id: "errorFeedsDlg",
1796                         title: __("Feeds with update errors"),
1797                         style: "width: 600px",
1798                         getSelectedFeeds: function() {
1799                                 return getSelectedTableRowIds("prefErrorFeedList");
1800                         },
1801                         removeSelected: function() {
1802                                 var sel_rows = this.getSelectedFeeds();
1803
1804                                 console.log(sel_rows);
1805
1806                                 if (sel_rows.length > 0) {
1807                                         var ok = confirm(__("Remove selected feeds?"));
1808
1809                                         if (ok) {
1810                                                 notify_progress("Removing selected feeds...", true);
1811
1812                                                 var query = "?op=pref-feeds&method=remove&ids="+
1813                                                         param_escape(sel_rows.toString());
1814
1815                                                 new Ajax.Request("backend.php", {
1816                                                         parameters: query,
1817                                                         onComplete: function(transport) {
1818                                                                 notify('');
1819                                                                 dialog.hide();
1820                                                                 updateFeedList();
1821                                                         } });
1822                                         }
1823
1824                                 } else {
1825                                         alert(__("No feeds are selected."));
1826                                 }
1827                         },
1828                         execute: function() {
1829                                 if (this.validate()) {
1830                                 }
1831                         },
1832                         href: query});
1833
1834                 dialog.show();
1835
1836         } catch (e) {
1837                 exception_error("showFeedsWithErrors", e);
1838         }
1839
1840 }
1841
1842 /* new support functions for SelectByTag */
1843
1844 function get_all_tags(selObj){
1845         try {
1846                 if( !selObj ) return "";
1847
1848                 var result = "";
1849                 var len = selObj.options.length;
1850
1851                 for (var i=0; i < len; i++){
1852                         if (selObj.options[i].selected) {
1853                                 result += selObj[i].value + "%2C";   // is really a comma
1854                         }
1855                 }
1856
1857                 if (result.length > 0){
1858                         result = result.substr(0, result.length-3);  // remove trailing %2C
1859                 }
1860
1861                 return(result);
1862
1863         } catch (e) {
1864                 exception_error("get_all_tags", e);
1865         }
1866 }
1867
1868 function get_radio_checked(radioObj) {
1869         try {
1870                 if (!radioObj) return "";
1871
1872                 var len = radioObj.length;
1873
1874                 if (len == undefined){
1875                         if(radioObj.checked){
1876                                 return(radioObj.value);
1877                         } else {
1878                                 return("");
1879                         }
1880                 }
1881
1882                 for( var i=0; i < len; i++ ){
1883                         if( radioObj[i].checked ){
1884                                 return( radioObj[i].value);
1885                         }
1886                 }
1887
1888         } catch (e) {
1889                 exception_error("get_radio_checked", e);
1890         }
1891         return("");
1892 }
1893
1894 function get_timestamp() {
1895         var date = new Date();
1896         return Math.round(date.getTime() / 1000);
1897 }
1898
1899 function helpDialog(topic) {
1900         try {
1901                 var query = "backend.php?op=backend&method=help&topic=" + param_escape(topic);
1902
1903                 if (dijit.byId("helpDlg"))
1904                         dijit.byId("helpDlg").destroyRecursive();
1905
1906                 dialog = new dijit.Dialog({
1907                         id: "helpDlg",
1908                         title: __("Help"),
1909                         style: "width: 600px",
1910                         href: query,
1911                 });
1912
1913                 dialog.show();
1914
1915         } catch (e) {
1916                 exception_error("helpDialog", e);
1917         }
1918 }
1919
1920 function htmlspecialchars_decode (string, quote_style) {
1921   // http://kevin.vanzonneveld.net
1922   // +   original by: Mirek Slugen
1923   // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
1924   // +   bugfixed by: Mateusz "loonquawl" Zalega
1925   // +      input by: ReverseSyntax
1926   // +      input by: Slawomir Kaniecki
1927   // +      input by: Scott Cariss
1928   // +      input by: Francois
1929   // +   bugfixed by: Onno Marsman
1930   // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
1931   // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
1932   // +      input by: Ratheous
1933   // +      input by: Mailfaker (http://www.weedem.fr/)
1934   // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
1935   // +    bugfixed by: Brett Zamir (http://brett-zamir.me)
1936   // *     example 1: htmlspecialchars_decode("<p>this -&gt; &quot;</p>", 'ENT_NOQUOTES');
1937   // *     returns 1: '<p>this -> &quot;</p>'
1938   // *     example 2: htmlspecialchars_decode("&amp;quot;");
1939   // *     returns 2: '&quot;'
1940   var optTemp = 0,
1941     i = 0,
1942     noquotes = false;
1943   if (typeof quote_style === 'undefined') {
1944     quote_style = 2;
1945   }
1946   string = string.toString().replace(/&lt;/g, '<').replace(/&gt;/g, '>');
1947   var OPTS = {
1948     'ENT_NOQUOTES': 0,
1949     'ENT_HTML_QUOTE_SINGLE': 1,
1950     'ENT_HTML_QUOTE_DOUBLE': 2,
1951     'ENT_COMPAT': 2,
1952     'ENT_QUOTES': 3,
1953     'ENT_IGNORE': 4
1954   };
1955   if (quote_style === 0) {
1956     noquotes = true;
1957   }
1958   if (typeof quote_style !== 'number') { // Allow for a single string or an array of string flags
1959     quote_style = [].concat(quote_style);
1960     for (i = 0; i < quote_style.length; i++) {
1961       // Resolve string input to bitwise e.g. 'PATHINFO_EXTENSION' becomes 4
1962       if (OPTS[quote_style[i]] === 0) {
1963         noquotes = true;
1964       } else if (OPTS[quote_style[i]]) {
1965         optTemp = optTemp | OPTS[quote_style[i]];
1966       }
1967     }
1968     quote_style = optTemp;
1969   }
1970   if (quote_style & OPTS.ENT_HTML_QUOTE_SINGLE) {
1971     string = string.replace(/&#0*39;/g, "'"); // PHP doesn't currently escape if more than one 0, but it should
1972     // string = string.replace(/&apos;|&#x0*27;/g, "'"); // This would also be useful here, but not a part of PHP
1973   }
1974   if (!noquotes) {
1975     string = string.replace(/&quot;/g, '"');
1976   }
1977   // Put this in last place to avoid escape being double-decoded
1978   string = string.replace(/&amp;/g, '&');
1979
1980   return string;
1981 }
1982
1983
1984 function label_to_feed_id(label) {
1985         return _label_base_index - 1 - Math.abs(label);
1986 }
1987
1988 function feed_to_label_id(feed) {
1989         return _label_base_index - 1 + Math.abs(feed);
1990 }
1991