]> git.wh0rd.org Git - tt-rss.git/blob - functions.js
limit resort_feedlist() to category mode
[tt-rss.git] / functions.js
1 var hotkeys_enabled = true;
2 var notify_silent = false;
3 var last_progress_point = 0;
4 var async_counters_work = false;
5
6 /* add method to remove element from array */
7
8 Array.prototype.remove = function(s) {
9         for (var i=0; i < this.length; i++) {
10                 if (s == this[i]) this.splice(i, 1);
11         }
12 }
13
14 function is_opera() {
15         return window.opera;
16 }
17
18 function exception_error(location, e, ext_info) {
19         var msg = format_exception_error(location, e);
20
21         if (!ext_info) ext_info = "N/A";
22
23         disableHotkeys();
24
25         try {
26
27                 var ebc = $("xebContent");
28         
29                 if (ebc) {
30         
31                         Element.show("dialog_overlay");
32                         Element.show("errorBoxShadow");
33         
34                         if (ext_info) {
35                                 if (ext_info.responseText) {
36                                         ext_info = ext_info.responseText;
37                                 }
38                         }
39         
40                         ebc.innerHTML = 
41                                 "<div><b>Error message:</b></div>" +
42                                 "<pre>" + msg + "</pre>" +
43                                 "<div><b>Additional information:</b></div>" +
44                                 "<textarea readonly=\"1\">" + ext_info + "</textarea>";
45         
46                 } else {
47                         alert(msg);
48                 }
49
50         } catch (e) {
51                 alert(msg);
52
53         }
54
55 }
56
57 function format_exception_error(location, e) {
58         var msg;
59
60         if (e.fileName) {
61                 var base_fname = e.fileName.substring(e.fileName.lastIndexOf("/") + 1);
62         
63                 msg = "Exception: " + e.name + ", " + e.message + 
64                         "\nFunction: " + location + "()" +
65                         "\nLocation: " + base_fname + ":" + e.lineNumber;
66
67         } else if (e.description) {
68                 msg = "Exception: " + e.description + "\nFunction: " + location + "()";
69         } else {
70                 msg = "Exception: " + e + "\nFunction: " + location + "()";
71         }
72
73         debug("<b>EXCEPTION: " + msg + "</b>");
74
75         return msg;
76 }
77
78
79 function disableHotkeys() {
80         hotkeys_enabled = false;
81 }
82
83 function enableHotkeys() {
84         hotkeys_enabled = true;
85 }
86
87 function open_article_callback(transport) {
88         try {
89
90                 if (transport.responseXML) {
91                         
92                         var link = transport.responseXML.getElementsByTagName("link")[0];
93                         var id = transport.responseXML.getElementsByTagName("id")[0];
94
95                         debug("open_article_callback, received link: " + link);
96
97                         if (link && id) {
98
99                                 var wname = "ttrss_article_" + id.firstChild.nodeValue;
100
101                                 debug("link url: " + link.firstChild.nodeValue + ", wname " + wname);
102
103                                 var w = window.open(link.firstChild.nodeValue, wname);
104
105                                 if (!w) { notify_error("Failed to load article in new window"); }
106
107                                 if (id) {
108                                         id = id.firstChild.nodeValue;
109                                         if (!$("headlinesList")) {
110                                                 window.setTimeout("toggleUnread(" + id + ", 0)", 100);
111                                         }
112                                 }
113                         } else {
114                                 notify_error("Can't open article: received invalid article link");
115                         }
116                 } else {
117                         notify_error("Can't open article: received invalid XML");
118                 }
119
120         } catch (e) {
121                 exception_error("open_article_callback", e);
122         }
123 }
124
125 function param_escape(arg) {
126         if (typeof encodeURIComponent != 'undefined')
127                 return encodeURIComponent(arg); 
128         else
129                 return escape(arg);
130 }
131
132 function param_unescape(arg) {
133         if (typeof decodeURIComponent != 'undefined')
134                 return decodeURIComponent(arg); 
135         else
136                 return unescape(arg);
137 }
138
139 function delay(gap) {
140         var then,now; 
141         then=new Date().getTime();
142         now=then;
143         while((now-then)<gap) {
144                 now=new Date().getTime();
145         }
146 }
147
148 var notify_hide_timerid = false;
149
150 function hide_notify() {
151         var n = $("notify");
152         if (n) {
153                 n.style.display = "none";
154         }
155
156
157 function notify_silent_next() {
158         notify_silent = true;
159 }
160
161 function notify_real(msg, no_hide, n_type) {
162
163         if (notify_silent) {
164                 notify_silent = false;
165                 return;
166         }
167
168         var n = $("notify");
169         var nb = $("notify_body");
170
171         if (!n || !nb) return;
172
173         if (notify_hide_timerid) {
174                 window.clearTimeout(notify_hide_timerid);
175         }
176
177         if (msg == "") {
178                 if (n.style.display == "block") {
179                         notify_hide_timerid = window.setTimeout("hide_notify()", 0);
180                 }
181                 return;
182         } else {
183                 n.style.display = "block";
184         }
185
186         /* types:
187
188                 1 - generic
189                 2 - progress
190                 3 - error
191                 4 - info
192
193         */
194
195         if (typeof __ != 'undefined') {
196                 msg = __(msg);
197         }
198
199         if (n_type == 1) {
200                 n.className = "notify";
201         } else if (n_type == 2) {
202                 n.className = "notifyProgress";
203                 msg = "<img src='images/indicator_white.gif'> " + msg;
204         } else if (n_type == 3) {
205                 n.className = "notifyError";
206                 msg = "<img src='images/sign_excl.gif'> " + msg;
207         } else if (n_type == 4) {
208                 n.className = "notifyInfo";
209                 msg = "<img src='images/sign_info.gif'> " + msg;
210         }
211
212 //      msg = "<img src='images/live_com_loading.gif'> " + msg;
213
214         nb.innerHTML = msg;
215
216         if (!no_hide) {
217                 notify_hide_timerid = window.setTimeout("hide_notify()", 3000);
218         }
219 }
220
221 function notify(msg, no_hide) {
222         notify_real(msg, no_hide, 1);
223 }
224
225 function notify_progress(msg, no_hide) {
226         notify_real(msg, no_hide, 2);
227 }
228
229 function notify_error(msg, no_hide) {
230         notify_real(msg, no_hide, 3);
231
232 }
233
234 function notify_info(msg, no_hide) {
235         notify_real(msg, no_hide, 4);
236 }
237
238 function printLockingError() {
239         notify_info("Please wait until operation finishes.");
240 }
241
242 function cleanSelected(element) {
243         var content = $(element);
244
245         for (i = 0; i < content.rows.length; i++) {
246                 content.rows[i].className = content.rows[i].className.replace("Selected", "");
247         }
248 }
249
250 function getVisibleUnreadHeadlines() {
251         var content = $("headlinesList");
252
253         var rows = new Array();
254
255         if (!content) return rows;
256
257         for (i = 0; i < content.rows.length; i++) {
258                 var row_id = content.rows[i].id.replace("RROW-", "");
259                 if (row_id.length > 0 && content.rows[i].className.match("Unread")) {
260                                 rows.push(row_id);      
261                 }
262         }
263         return rows;
264 }
265
266 function getVisibleHeadlineIds() {
267
268         var content = $("headlinesList");
269
270         var rows = new Array();
271
272         if (!content) return rows;
273
274         for (i = 0; i < content.rows.length; i++) {
275                 var row_id = content.rows[i].id.replace("RROW-", "");
276                 if (row_id.length > 0) {
277                                 rows.push(row_id);      
278                 }
279         }
280         return rows;
281 }
282
283 function getFirstVisibleHeadlineId() {
284         if (isCdmMode()) {
285                 var rows = cdmGetVisibleArticles();
286                 return rows[0];
287         } else {
288                 var rows = getVisibleHeadlineIds();
289                 return rows[0];
290         }
291 }
292
293 function getLastVisibleHeadlineId() {
294         if (isCdmMode()) {
295                 var rows = cdmGetVisibleArticles();
296                 return rows[rows.length-1];
297         } else {
298                 var rows = getVisibleHeadlineIds();
299                 return rows[rows.length-1];
300         }
301 }
302
303 function markHeadline(id) {
304         var row = $("RROW-" + id);
305         if (row) {
306                 var is_active = false;
307         
308                 if (row.className.match("Active")) {
309                         is_active = true;
310                 }
311                 row.className = row.className.replace("Selected", "");
312                 row.className = row.className.replace("Active", "");
313                 row.className = row.className.replace("Insensitive", "");
314                 
315                 if (is_active) {
316                         row.className = row.className = "Active";
317                 }
318                 
319                 var check = $("RCHK-" + id);
320
321                 if (check) {
322                         check.checked = true;
323                 }
324
325                 row.className = row.className + "Selected"; 
326                 
327         }
328 }
329
330 function getFeedIds() {
331         var content = $("feedsList");
332
333         var rows = new Array();
334
335         for (i = 0; i < content.rows.length; i++) {
336                 var id = content.rows[i].id.replace("FEEDR-", "");
337                 if (id.length > 0) {
338                         rows.push(id);
339                 }
340         }
341
342         return rows;
343 }
344
345 function setCookie(name, value, lifetime, path, domain, secure) {
346         
347         var d = false;
348         
349         if (lifetime) {
350                 d = new Date();
351                 d.setTime(d.getTime() + (lifetime * 1000));
352         }
353
354         debug("setCookie: " + name + " => " + value + ": " + d);
355         
356         int_setCookie(name, value, d, path, domain, secure);
357
358 }
359
360 function int_setCookie(name, value, expires, path, domain, secure) {
361         document.cookie= name + "=" + escape(value) +
362                 ((expires) ? "; expires=" + expires.toGMTString() : "") +
363                 ((path) ? "; path=" + path : "") +
364                 ((domain) ? "; domain=" + domain : "") +
365                 ((secure) ? "; secure" : "");
366 }
367
368 function delCookie(name, path, domain) {
369         if (getCookie(name)) {
370                 document.cookie = name + "=" +
371                 ((path) ? ";path=" + path : "") +
372                 ((domain) ? ";domain=" + domain : "" ) +
373                 ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
374         }
375 }
376                 
377
378 function getCookie(name) {
379
380         var dc = document.cookie;
381         var prefix = name + "=";
382         var begin = dc.indexOf("; " + prefix);
383         if (begin == -1) {
384             begin = dc.indexOf(prefix);
385             if (begin != 0) return null;
386         }
387         else {
388             begin += 2;
389         }
390         var end = document.cookie.indexOf(";", begin);
391         if (end == -1) {
392             end = dc.length;
393         }
394         return unescape(dc.substring(begin + prefix.length, end));
395 }
396
397 function disableContainerChildren(id, disable, doc) {
398
399         if (!doc) doc = document;
400
401         var container = $(id);
402
403         if (!container) {
404                 //alert("disableContainerChildren: element " + id + " not found");
405                 return;
406         }
407
408         for (var i = 0; i < container.childNodes.length; i++) {
409                 var child = container.childNodes[i];
410
411                 try {
412                         child.disabled = disable;
413                 } catch (E) {
414
415                 }
416
417                 if (disable) {
418                         if (child.className && child.className.match("button")) {
419                                 child.className = "disabledButton";
420                         }
421                 } else {
422                         if (child.className && child.className.match("disabledButton")) {
423                                 child.className = "button";
424                         }
425                 } 
426         }
427
428 }
429
430 function gotoPreferences() {
431         document.location.href = "prefs.php";
432 }
433
434 function gotoMain() {
435         document.location.href = "tt-rss.php";
436 }
437
438 function gotoExportOpml() {
439         document.location.href = "opml.php?op=Export";
440 }
441
442 function parse_counters(reply, scheduled_call) {
443         try {
444
445                 var feeds_found = 0;
446
447                 var elems = reply.getElementsByTagName("counter");
448
449                 for (var l = 0; l < elems.length; l++) {
450
451                         var id = elems[l].getAttribute("id");
452                         var t = elems[l].getAttribute("type");
453                         var ctr = elems[l].getAttribute("counter");
454                         var error = elems[l].getAttribute("error");
455                         var has_img = elems[l].getAttribute("hi");
456                         var updated = elems[l].getAttribute("updated");
457                         var title = elems[l].getAttribute("title");
458                         var xmsg = elems[l].getAttribute("xmsg");
459         
460                         if (id == "global-unread") {
461
462                                 if (ctr > global_unread) {
463                                         offlineDownloadStart(1);
464                                 }
465
466                                 global_unread = ctr;
467                                 updateTitle();
468                                 continue;
469                         }
470
471                         if (id == "subscribed-feeds") {
472                                 feeds_found = ctr;
473                                 continue;
474                         }
475         
476                         if (t == "category") {
477                                 var catctr = $("FCATCTR-" + id);
478                                 if (catctr) {
479                                         catctr.innerHTML = "(" + ctr + ")";
480                                         if (ctr > 0) {
481                                                 catctr.className = "catCtrHasUnread";
482                                         } else {
483                                                 catctr.className = "catCtrNoUnread";
484                                         }
485                                 }
486                                 continue;
487                         }
488                 
489                         var feedctr = $("FEEDCTR-" + id);
490                         var feedu = $("FEEDU-" + id);
491                         var feedr = $("FEEDR-" + id);
492                         var feed_img = $("FIMG-" + id);
493                         var feedlink = $("FEEDL-" + id);
494                         var feedupd = $("FLUPD-" + id);
495
496                         if (updated && feedlink) {
497                                 if (error) {
498                                         feedlink.title = "Error: " + error + " (" + updated + ")";
499                                 } else {
500                                         feedlink.title = "Updated: " + updated;
501                                 }
502                         }
503
504                         if (feedupd) {
505                                 if (!updated) updated = "";
506
507                                 if (error) {
508                                         if (xmsg) {
509                                                 feedupd.innerHTML = updated + " " + xmsg + " (Error)";
510                                         } else {
511                                                 feedupd.innerHTML = updated + " (Error)";
512                                         }
513                                 } else {
514                                         if (xmsg) {
515                                                 feedupd.innerHTML = updated + " " + xmsg;
516                                         } else {
517                                                 feedupd.innerHTML = updated;
518                                         }
519                                 }
520                         }
521
522                         if (has_img && feed_img) {
523                                 if (!feed_img.src.match(id + ".ico")) {
524                                         feed_img.src = getInitParam("icons_location") + "/" + id + ".ico";
525                                 }
526                         }
527
528                         if (feedlink && title) {
529                                 feedlink.innerHTML = title;
530                         }
531
532                         if (feedctr && feedu && feedr) {
533
534                                 if (parseInt(ctr) > 0 && 
535                                                 parseInt(feedu.innerHTML) < parseInt(ctr) && 
536                                                 id == getActiveFeedId() && scheduled_call) {
537
538                                         displayNewContentPrompt(id);
539                                 }
540
541                                 var row_needs_hl = (ctr > 0 && ctr > parseInt(feedu.innerHTML));
542
543                                 feedu.innerHTML = ctr;
544
545                                 if (error) {
546                                         feedr.className = feedr.className.replace("feed", "error");
547                                 } else if (id > 0) {
548                                         feedr.className = feedr.className.replace("error", "feed");
549                                 }
550         
551                                 if (ctr > 0) {                                  
552                                         feedctr.className = "feedCtrHasUnread";
553                                         if (!feedr.className.match("Unread")) {
554                                                 var is_selected = feedr.className.match("Selected");
555                 
556                                                 feedr.className = feedr.className.replace("Selected", "");
557                                                 feedr.className = feedr.className.replace("Unread", "");
558                 
559                                                 feedr.className = feedr.className + "Unread";
560                 
561                                                 if (is_selected) {
562                                                         feedr.className = feedr.className + "Selected";
563                                                 }       
564                                                 
565                                         }
566
567                                         if (row_needs_hl) { 
568                                                 new Effect.Highlight(feedr, {duration: 1, startcolor: "#fff7d5",
569                                                         queue: { position:'end', scope: 'EFQ-' + id, limit: 1 } } );
570
571                                                 cache_invalidate("F:" + id);
572                                         }
573                                 } else {
574                                         feedctr.className = "feedCtrNoUnread";
575                                         feedr.className = feedr.className.replace("Unread", "");
576                                 }                       
577                         }
578                 }
579
580                 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
581
582                 var feeds_stored = number_of_feeds;
583
584                 debug("Feed counters, C: " + feeds_found + ", S:" + feeds_stored);
585
586                 if (feeds_stored != feeds_found) {
587                         number_of_feeds = feeds_found;
588
589                         if (feeds_stored != 0 && feeds_found != 0) {
590                                 debug("Subscribed feed number changed, refreshing feedlist");
591                                 setTimeout('updateFeedList(false, false)', 50);
592                         }
593                 } else {
594 /*                      var fl = $("feeds-frame").innerHTML;
595                         if (fl) {
596                                 cache_invalidate("FEEDLIST");
597                                 cache_inject("FEEDLIST", fl, getInitParam("num_feeds"));
598                         } */
599                 }
600
601         } catch (e) {
602                 exception_error("parse_counters", e);
603         }
604 }
605
606 function parse_counters_reply(transport, scheduled_call) {
607
608         if (!transport.responseXML) {
609                 notify_error("Backend did not return valid XML", true);
610                 return;
611         }
612
613         var reply = transport.responseXML.firstChild;
614         
615         if (!reply) {
616                 notify_error("Backend did not return expected XML object", true);
617                 updateTitle("");
618                 return;
619         } 
620
621         if (!transport_error_check(transport)) return;
622
623         var counters = reply.getElementsByTagName("counters")[0];
624         
625         parse_counters(counters, scheduled_call);
626
627         var runtime_info = reply.getElementsByTagName("runtime-info")[0];
628
629         parse_runtime_info(runtime_info);
630
631         if (feedsSortByUnread()) {
632                 resort_feedlist();
633         }
634
635         hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
636
637 }
638
639 function all_counters_callback2(transport, async_call) {
640         try {
641                 if (async_call) async_counters_work = true;
642                 
643                 if (offline_mode) return;
644
645                 debug("<b>all_counters_callback2 IN: " + transport + "</b>");
646                 parse_counters_reply(transport);
647                 debug("<b>all_counters_callback2 OUT: " + transport + "</b>");
648
649         } catch (e) {
650                 exception_error("all_counters_callback2", e, transport);
651         }
652 }
653
654 function get_feed_unread(id) {
655         try {
656                 return parseInt($("FEEDU-" + id).innerHTML);    
657         } catch (e) {
658                 return -1;
659         }
660 }
661
662 function get_cat_unread(id) {
663         try {
664                 var ctr = $("FCATCTR-" + id).innerHTML;
665                 ctr = ctr.replace("(", "");
666                 ctr = ctr.replace(")", "");
667                 return parseInt(ctr);
668         } catch (e) {
669                 return -1;
670         }
671 }
672
673 function get_feed_entry_unread(elem) {
674
675         var id = elem.id.replace("FEEDR-", "");
676
677         if (id <= 0) {
678                 return -1;
679         }
680
681         try {
682                 return parseInt($("FEEDU-" + id).innerHTML);    
683         } catch (e) {
684                 return -1;
685         }
686 }
687
688 function get_feed_entry_name(elem) {
689         var id = elem.id.replace("FEEDR-", "");
690         return getFeedName(id);
691 }
692
693
694 function resort_category(node) {
695
696         try {
697
698                 debug("resort_category: " + node);
699         
700                 var by_unread = feedsSortByUnread();
701         
702                 var list = node.getElementsByTagName("LI");
703         
704                 for (i = 0; i < list.length; i++) {
705         
706                         for (j = i+1; j < list.length; j++) {                   
707         
708                                 var tmp_val = get_feed_entry_unread(list[i]);
709                                 var cur_val = get_feed_entry_unread(list[j]);
710         
711                                 var tmp_name = get_feed_entry_name(list[i]);
712                                 var cur_name = get_feed_entry_name(list[j]);
713         
714                                 if ((by_unread && (cur_val > tmp_val)) || (!by_unread && (cur_name < tmp_name))) {
715                                         tempnode_i = list[i].cloneNode(true);
716                                         tempnode_j = list[j].cloneNode(true);
717                                         node.replaceChild(tempnode_i, list[j]);
718                                         node.replaceChild(tempnode_j, list[i]);
719                                 }
720                         }
721                 }
722
723         } catch (e) {
724                 exception_error("resort_category", e);
725         }
726
727 }
728
729 function resort_feedlist() {
730         debug("resort_feedlist");
731
732         if ($("FCATLIST--1")) {
733
734                 var lists = document.getElementsByTagName("UL");
735
736                 for (var i = 0; i < lists.length; i++) {
737                         if (lists[i].id && lists[i].id.match("FCATLIST-")) {
738                                 resort_category(lists[i]);
739                         }
740                 }
741
742         } else {
743                 //resort_category($("feedList"));
744         }
745 }
746
747 /** * @(#)isNumeric.js * * Copyright (c) 2000 by Sundar Dorai-Raj
748   * * @author Sundar Dorai-Raj
749   * * Email: sdoraira@vt.edu
750   * * This program is free software; you can redistribute it and/or
751   * * modify it under the terms of the GNU General Public License 
752   * * as published by the Free Software Foundation; either version 2 
753   * * of the License, or (at your option) any later version, 
754   * * provided that any use properly credits the author. 
755   * * This program is distributed in the hope that it will be useful,
756   * * but WITHOUT ANY WARRANTY; without even the implied warranty of
757   * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
758   * * GNU General Public License for more details at http://www.gnu.org * * */
759
760   var numbers=".0123456789";
761   function isNumeric(x) {
762     // is x a String or a character?
763     if(x.length>1) {
764       // remove negative sign
765       x=Math.abs(x)+"";
766       for(j=0;j<x.length;j++) {
767         // call isNumeric recursively for each character
768         number=isNumeric(x.substring(j,j+1));
769         if(!number) return number;
770       }
771       return number;
772     }
773     else {
774       // if x is number return true
775       if(numbers.indexOf(x)>=0) return true;
776       return false;
777     }
778   }
779
780
781 function hideOrShowFeeds(hide) {
782
783         try {
784
785         debug("hideOrShowFeeds: " + hide);
786
787         if ($("FCATLIST--1")) {
788
789                 var lists = document.getElementsByTagName("UL");
790
791                 for (var i = 0; i < lists.length; i++) {
792                         if (lists[i].id && lists[i].id.match("FCATLIST-")) {
793
794                                 var id = lists[i].id.replace("FCATLIST-", "");
795                                 hideOrShowFeedsCategory(id, hide);
796                         }
797                 }
798
799         } else {
800                 hideOrShowFeedsCategory(null, hide);
801         }
802
803         } catch (e) {
804                 exception_error("hideOrShowFeeds", e);
805         }
806 }
807
808 function hideOrShowFeedsCategory(id, hide) {
809
810         try {
811         
812                 var node = null;
813                 var cat_node = null;
814
815                 if (id) {
816                         node = $("FCATLIST-" + id);
817                         cat_node = $("FCAT-" + id);
818                 } else {
819                         node = $("feedList"); // no categories
820                 }
821
822         //      debug("hideOrShowFeedsCategory: " + node + " (" + hide + ")");
823         
824                 var cat_unread = 0;
825         
826                 if (!node) {
827                         debug("hideOrShowFeeds: passed node is null, aborting");
828                         return;
829                 }
830         
831         //      debug("cat: " + node.id);
832         
833                 if (node.hasChildNodes() && node.firstChild.nextSibling != false) {  
834                         for (i = 0; i < node.childNodes.length; i++) {
835                                 if (node.childNodes[i].nodeName != "LI") { continue; }
836         
837                                 if (node.childNodes[i].style != undefined) {
838         
839                                         var has_unread = (node.childNodes[i].className != "feed" &&
840                                                 node.childNodes[i].className != "label" && 
841                                                 !(!getInitParam("hide_read_shows_special") && 
842                                                         node.childNodes[i].className == "virt") && 
843                                                 node.childNodes[i].className != "error" && 
844                                                 node.childNodes[i].className != "tag");
845                 
846         //                              debug(node.childNodes[i].id + " --> " + has_unread);
847                 
848                                         if (hide && !has_unread) {
849                                                 //node.childNodes[i].style.display = "none";
850                                                 var id = node.childNodes[i].id;
851                                                 Effect.Fade(node.childNodes[i], {duration : 0.3, 
852                                                         queue: { position: 'end', scope: 'FFADE-' + id, limit: 1 }});
853                                         }
854                 
855                                         if (!hide) {
856                                                 node.childNodes[i].style.display = "list-item";
857                                                 //Effect.Appear(node.childNodes[i], {duration : 0.3});
858                                         }
859                 
860                                         if (has_unread) {
861                                                 node.childNodes[i].style.display = "list-item";
862                                                 cat_unread++;
863                                                 //Effect.Appear(node.childNodes[i], {duration : 0.3});
864                                                 //Effect.Highlight(node.childNodes[i]);
865                                         }
866                                 }
867                         }
868                 }       
869         
870         //      debug("end cat: " + node.id + " unread " + cat_unread);
871
872                 if (cat_node) {
873
874                         if (cat_unread == 0) {
875                                 if (cat_node.style == undefined) {
876                                         debug("ERROR: supplied cat_node " + cat_node + 
877                                                 " has no styles. WTF?");
878                                         return;
879                                 }
880                                 if (hide) {
881                                         //cat_node.style.display = "none";
882                                         Effect.Fade(cat_node, {duration : 0.3, 
883                                                 queue: { position: 'end', scope: 'CFADE-' + node.id, limit: 1 }});
884                                 } else {
885                                         cat_node.style.display = "list-item";
886                                 }
887                         } else {
888                                 try {
889                                         cat_node.style.display = "list-item";
890                                 } catch (e) {
891                                         debug(e);
892                                 }
893                         }
894                 }
895
896 //      debug("unread for category: " + cat_unread);
897
898         } catch (e) {
899                 exception_error("hideOrShowFeedsCategory", e);
900         }
901 }
902
903 function selectTableRow(r, do_select) {
904         r.className = r.className.replace("Selected", "");
905         
906         if (do_select) {
907                 r.className = r.className + "Selected";
908         }
909 }
910
911 function selectTableRowById(elem_id, check_id, do_select) {
912
913         try {
914
915                 var row = $(elem_id);
916
917                 if (row) {
918                         selectTableRow(row, do_select);
919                 }               
920
921                 var check = $(check_id);
922
923                 if (check) {
924                         check.checked = do_select;
925                 }
926         } catch (e) {
927                 exception_error("selectTableRowById", e);
928         }
929 }
930
931 function selectTableRowsByIdPrefix(content_id, prefix, check_prefix, do_select, 
932         classcheck, reset_others) {
933
934         var content = $(content_id);
935
936         if (!content) {
937                 alert("[selectTableRows] Element " + content_id + " not found.");
938                 return;
939         }
940
941         for (i = 0; i < content.rows.length; i++) {
942                 if (Element.visible(content.rows[i])) {
943                         if (!classcheck || content.rows[i].className.match(classcheck)) {
944                 
945                                 if (content.rows[i].id.match(prefix)) {
946                                         selectTableRow(content.rows[i], do_select);
947                                 
948                                         var row_id = content.rows[i].id.replace(prefix, "");
949                                         var check = $(check_prefix + row_id);
950         
951                                         if (check) {
952                                                 check.checked = do_select;
953                                         }
954                                 } else if (reset_others) {
955                                         selectTableRow(content.rows[i], false);
956         
957                                         var row_id = content.rows[i].id.replace(prefix, "");
958                                         var check = $(check_prefix + row_id);
959         
960                                         if (check) {
961                                                 check.checked = false;
962                                         }
963         
964                                 }
965                         } else if (reset_others) {
966                                 selectTableRow(content.rows[i], false);
967         
968                                 var row_id = content.rows[i].id.replace(prefix, "");
969                                 var check = $(check_prefix + row_id);
970         
971                                 if (check) {
972                                         check.checked = false;
973                                 }
974         
975                         }
976                 }
977         }
978 }
979
980 function getSelectedTableRowIds(content_id, prefix) {
981
982         var content = $(content_id);
983
984         if (!content) {
985                 alert("[getSelectedTableRowIds] Element " + content_id + " not found.");
986                 return;
987         }
988
989         var sel_rows = new Array();
990
991         for (i = 0; i < content.rows.length; i++) {
992                 if (content.rows[i].id.match(prefix) && 
993                                 content.rows[i].className.match("Selected")) {
994                                 
995                         var row_id = content.rows[i].id.replace(prefix + "-", "");
996                         sel_rows.push(row_id);  
997                 }
998         }
999
1000         return sel_rows;
1001
1002 }
1003
1004 function toggleSelectRowById(sender, id) {
1005         var row = $(id);
1006
1007         if (sender.checked) {
1008                 if (!row.className.match("Selected")) {
1009                         row.className = row.className + "Selected";
1010                 }
1011         } else {
1012                 if (row.className.match("Selected")) {
1013                         row.className = row.className.replace("Selected", "");
1014                 }
1015         }
1016 }
1017
1018 function toggleSelectListRow(sender) {
1019         var parent_row = sender.parentNode;
1020
1021         if (sender.checked) {
1022                 if (!parent_row.className.match("Selected")) {
1023                         parent_row.className = parent_row.className + "Selected";
1024                 }
1025         } else {
1026                 if (parent_row.className.match("Selected")) {
1027                         parent_row.className = parent_row.className.replace("Selected", "");
1028                 }
1029         }
1030 }
1031
1032 function tSR(sender) {
1033         return toggleSelectRow(sender);
1034 }
1035
1036 function toggleSelectRow(sender) {
1037         var parent_row = sender.parentNode.parentNode;
1038
1039         if (sender.checked) {
1040                 if (!parent_row.className.match("Selected")) {
1041                         parent_row.className = parent_row.className + "Selected";
1042                 }
1043         } else {
1044                 if (parent_row.className.match("Selected")) {
1045                         parent_row.className = parent_row.className.replace("Selected", "");
1046                 }
1047         }
1048 }
1049
1050 function getNextUnreadCat(id) {
1051         try {
1052                 var rows = $("feedList").getElementsByTagName("LI");
1053                 var feeds = new Array();
1054
1055                 var unread_only = true;
1056                 var is_cat = true;
1057
1058                 for (var i = 0; i < rows.length; i++) {
1059                         if (rows[i].id.match("FCAT-")) {
1060                                 if (rows[i].id == "FCAT-" + id && is_cat || (Element.visible(rows[i]) && Element.visible(rows[i].parentNode))) {
1061
1062                                         var cat_id = parseInt(rows[i].id.replace("FCAT-", ""));
1063
1064                                         if (cat_id >= 0) {
1065                                                 if (!unread_only || get_cat_unread(cat_id) > 0) {
1066                                                         feeds.push(cat_id);
1067                                                 }
1068                                         }
1069                                 }
1070                         }
1071                 }
1072
1073                 var idx = feeds.indexOf(id);
1074                 if (idx != -1 && idx < feeds.length) {
1075                         return feeds[idx+1];                                    
1076                 } else {
1077                         return feeds.shift();
1078                 }
1079
1080         } catch (e) {
1081                 exception_error("getNextUnreadCat", e);
1082         }
1083 }
1084
1085 function getRelativeFeedId2(id, is_cat, direction, unread_only) {       
1086         try {
1087
1088 //              alert(id + " IC: " + is_cat + " D: " + direction + " U: " + unread_only);
1089
1090                 var rows = $("feedList").getElementsByTagName("LI");
1091                 var feeds = new Array();
1092         
1093                 for (var i = 0; i < rows.length; i++) {
1094                         if (rows[i].id.match("FEEDR-")) {
1095         
1096                                 if (rows[i].id == "FEEDR-" + id && !is_cat || (Element.visible(rows[i]) && Element.visible(rows[i].parentNode))) {
1097         
1098                                         if (!unread_only || 
1099                                                         (rows[i].className.match("Unread") || rows[i].id == "FEEDR-" + id)) {
1100                                                 feeds.push(rows[i].id.replace("FEEDR-", ""));
1101                                         }
1102                                 }
1103                         }
1104
1105                         if (rows[i].id.match("FCAT-")) {
1106                                 if (rows[i].id == "FCAT-" + id && is_cat || (Element.visible(rows[i]) && Element.visible(rows[i].parentNode))) {
1107
1108                                         var cat_id = parseInt(rows[i].id.replace("FCAT-", ""));
1109
1110                                         if (cat_id >= 0) {
1111                                                 if (!unread_only || get_cat_unread(cat_id) > 0) {
1112                                                         feeds.push("CAT:"+cat_id);
1113                                                 }
1114                                         }
1115                                 }
1116                         }
1117                 }
1118         
1119 //              alert(feeds.toString());
1120
1121                 if (!id) {
1122                         if (direction == "next") {
1123                                 return feeds.shift();
1124                         } else {
1125                                 return feeds.pop();
1126                         }
1127                 } else {
1128                         if (direction == "next") {
1129                                 if (is_cat) id = "CAT:" + id;
1130                                 var idx = feeds.indexOf(id);
1131                                 if (idx != -1 && idx < feeds.length) {
1132                                         return feeds[idx+1];                                    
1133                                 } else {
1134                                         return getRelativeFeedId2(false, is_cat, direction, unread_only);
1135                                 }
1136                         } else {
1137                                 if (is_cat) id = "CAT:" + id;
1138                                 var idx = feeds.indexOf(id);
1139                                 if (idx > 0) {
1140                                         return feeds[idx-1];
1141                                 } else {
1142                                         return getRelativeFeedId2(false, is_cat, direction, unread_only);
1143                                 }
1144                         }
1145         
1146                 }
1147
1148         } catch (e) {
1149                 exception_error("getRelativeFeedId2", e);
1150         }
1151 }
1152
1153
1154 function getRelativeFeedId(list, id, direction, unread_only) {  
1155         var rows = list.getElementsByTagName("LI");
1156         var feeds = new Array();
1157
1158         for (var i = 0; i < rows.length; i++) {
1159                 if (rows[i].id.match("FEEDR-")) {
1160
1161                         if (rows[i].id == "FEEDR-" + id || (Element.visible(rows[i]) && Element.visible(rows[i].parentNode))) {
1162
1163                                 if (!unread_only || 
1164                                                 (rows[i].className.match("Unread") || rows[i].id == "FEEDR-" + id)) {
1165                                         feeds.push(rows[i].id.replace("FEEDR-", ""));
1166                                 }
1167                         }
1168                 }
1169         }
1170
1171         if (!id) {
1172                 if (direction == "next") {
1173                         return feeds.shift();
1174                 } else {
1175                         return feeds.pop();
1176                 }
1177         } else {
1178                 if (direction == "next") {
1179                         var idx = feeds.indexOf(id);
1180                         if (idx != -1 && idx < feeds.length) {
1181                                 return feeds[idx+1];                                    
1182                         } else {
1183                                 return getRelativeFeedId(list, false, direction, unread_only);
1184                         }
1185                 } else {
1186                         var idx = feeds.indexOf(id);
1187                         if (idx > 0) {
1188                                 return feeds[idx-1];
1189                         } else {
1190                                 return getRelativeFeedId(list, false, direction, unread_only);
1191                         }
1192                 }
1193
1194         }
1195 }
1196
1197 function showBlockElement(id, h_id) {
1198         var elem = $(id);
1199
1200         if (elem) {
1201                 elem.style.display = "block";
1202
1203                 if (h_id) {
1204                         elem = $(h_id);
1205                         if (elem) {
1206                                 elem.style.display = "none";
1207                         }
1208                 }
1209         } else {
1210                 alert("[showBlockElement] can't find element with id " + id);
1211         } 
1212 }
1213
1214 function appearBlockElement_afh(effect) {
1215
1216 }
1217
1218 function checkboxToggleElement(elem, id) {
1219         if (elem.checked) {
1220                 Effect.Appear(id, {duration : 0.5});
1221         } else {
1222                 Effect.Fade(id, {duration : 0.5});
1223         }
1224 }
1225
1226 function appearBlockElement(id, h_id) {
1227
1228         try {
1229                 if (h_id) {
1230                         Effect.Fade(h_id);
1231                 }
1232                 Effect.SlideDown(id, {duration : 1.0, afterFinish: appearBlockElement_afh});
1233         } catch (e) {
1234                 exception_error("appearBlockElement", e);
1235         }
1236
1237 }
1238
1239 function hideParentElement(e) {
1240         e.parentNode.style.display = "none";
1241 }
1242
1243 function dropboxSelect(e, v) {
1244         for (i = 0; i < e.length; i++) {
1245                 if (e[i].value == v) {
1246                         e.selectedIndex = i;
1247                         break;
1248                 }
1249         }
1250 }
1251
1252 // originally stolen from http://www.11tmr.com/11tmr.nsf/d6plinks/MWHE-695L9Z
1253 // bugfixed just a little bit :-)
1254 function getURLParam(strParamName){
1255   var strReturn = "";
1256   var strHref = window.location.href;
1257
1258   if (strHref.indexOf("#") == strHref.length-1) {
1259                 strHref = strHref.substring(0, strHref.length-1);
1260   }
1261
1262   if ( strHref.indexOf("?") > -1 ){
1263     var strQueryString = strHref.substr(strHref.indexOf("?"));
1264     var aQueryString = strQueryString.split("&");
1265     for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
1266       if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
1267         var aParam = aQueryString[iParam].split("=");
1268         strReturn = aParam[1];
1269         break;
1270       }
1271     }
1272   }
1273   return strReturn;
1274
1275
1276 function leading_zero(p) {
1277         var s = String(p);
1278         if (s.length == 1) s = "0" + s;
1279         return s;
1280 }
1281
1282 function closeErrorBox() {
1283
1284         if (Element.visible("errorBoxShadow")) {
1285                 Element.hide("dialog_overlay");
1286                 Element.hide("errorBoxShadow");
1287
1288                 enableHotkeys();
1289         }
1290
1291         return false;
1292 }
1293
1294 function closeInfoBox(cleanup) {
1295
1296         try {
1297                 enableHotkeys();
1298
1299                 if (Element.visible("infoBoxShadow")) {
1300                         Element.hide("dialog_overlay");
1301                         Element.hide("infoBoxShadow");
1302
1303                         if (cleanup) $("infoBoxShadow").innerHTML = "&nbsp;";
1304                 }
1305         } catch (e) {
1306                 exception_error("closeInfoBox", e);
1307         }
1308         
1309         return false;
1310 }
1311
1312
1313 function displayDlg(id, param) {
1314
1315         notify_progress("Loading, please wait...", true);
1316
1317         disableHotkeys();
1318
1319         var query = "backend.php?op=dlg&id=" +
1320                 param_escape(id) + "&param=" + param_escape(param);
1321
1322         new Ajax.Request(query, {
1323                 onComplete: function (transport) {
1324                         infobox_callback2(transport);
1325                 } });
1326
1327         return false;
1328 }
1329
1330 function infobox_submit_callback2(transport) {
1331         closeInfoBox();
1332
1333         try {
1334                 // called from prefs, reload tab
1335                 if (typeof active_tab != 'undefined' && active_tab) {
1336                         selectTab(active_tab, false);
1337                 }
1338         } catch (e) { }
1339
1340         if (transport.responseText) {
1341                 notify_info(transport.responseText);
1342         }
1343 }
1344
1345 function infobox_callback2(transport) {
1346         try {
1347
1348                 debug("infobox_callback2");
1349
1350                 var box = $('infoBox');
1351                 
1352                 if (box) {                      
1353
1354                         if (!getInitParam("infobox_disable_overlay")) {
1355                                 Element.show("dialog_overlay");
1356                         }
1357
1358                         box.innerHTML=transport.responseText;                   
1359                         Element.show("infoBoxShadow");
1360                         //Effect.SlideDown("infoBoxShadow", {duration : 1.0});
1361
1362
1363                 }
1364
1365                 /* FIXME this needs to be moved out somewhere */
1366
1367                 if ($("tags_choices")) {
1368                         new Ajax.Autocompleter('tags_str', 'tags_choices',
1369                                 "backend.php?op=rpc&subop=completeTags",
1370                                 { tokens: ',', paramName: "search" });
1371                 }
1372
1373                 disableHotkeys();
1374
1375                 notify("");
1376         } catch (e) {
1377                 exception_error("infobox_callback2", e);
1378         }
1379 }
1380
1381 function createFilter() {
1382
1383         try {
1384
1385                 var form = document.forms['filter_add_form'];
1386                 var reg_exp = form.reg_exp.value;
1387         
1388                 if (reg_exp == "") {
1389                         alert(__("Can't add filter: nothing to match on."));
1390                         return false;
1391                 }
1392         
1393                 var query = Form.serialize("filter_add_form");
1394         
1395                 // we can be called from some other tab in Prefs                
1396                 if (typeof active_tab != 'undefined' && active_tab) {
1397                         active_tab = "filterConfig";
1398                 }
1399         
1400                 new Ajax.Request("backend.php?" + query, {
1401                         onComplete: function (transport) {
1402                                 infobox_submit_callback2(transport);
1403                         } });
1404                 
1405                 return true;
1406
1407         } catch (e) {
1408                 exception_error("createFilter", e);
1409         }
1410 }
1411
1412 function toggleSubmitNotEmpty(e, submit_id) {
1413         try {
1414                 $(submit_id).disabled = (e.value == "")
1415         } catch (e) {
1416                 exception_error("toggleSubmitNotEmpty", e);
1417         }
1418 }
1419
1420 function isValidURL(s) {
1421         return s.match("http://") != null || s.match("https://") != null || s.match("feed://") != null;
1422 }
1423
1424 function subscribeToFeed() {
1425
1426         var form = document.forms['feed_add_form'];
1427         var feed_url = form.feed_url.value;
1428
1429         if (feed_url == "") {
1430                 alert(__("Can't subscribe: no feed URL given."));
1431                 return false;
1432         }
1433
1434         notify_progress(__("Subscribing to feed..."), true);
1435
1436         closeInfoBox();
1437
1438         var feeds_doc = document;
1439
1440 //      feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
1441         
1442         var query = Form.serialize("feed_add_form");
1443         
1444         debug("subscribe q: " + query);
1445
1446         new Ajax.Request("backend.php", {
1447                 parameters: query,
1448                 onComplete: function(transport) { 
1449                         dlg_frefresh_callback(transport); 
1450                 } });
1451
1452         return false;
1453 }
1454
1455 function filterCR(e, f)
1456 {
1457      var key;
1458
1459      if(window.event)
1460           key = window.event.keyCode;     //IE
1461      else
1462           key = e.which;     //firefox
1463
1464         if (key == 13) {
1465                 if (typeof f != 'undefined') {
1466                         f();
1467                         return false;
1468                 } else {
1469                         return false;
1470                 }
1471         } else {
1472                 return true;
1473         }
1474 }
1475
1476 var debug_last_class = "even";
1477
1478 function debug(msg) {
1479
1480         if (debug_last_class == "even") {
1481                 debug_last_class = "odd";
1482         } else {
1483                 debug_last_class = "even";
1484         }
1485
1486         var c = $('debug_output');
1487         if (c && Element.visible(c)) {
1488                 while (c.lastChild != 'undefined' && c.childNodes.length > 100) {
1489                         c.removeChild(c.lastChild);
1490                 }
1491         
1492                 var d = new Date();
1493                 var ts = leading_zero(d.getHours()) + ":" + leading_zero(d.getMinutes()) +
1494                         ":" + leading_zero(d.getSeconds());
1495                 c.innerHTML = "<li class=\"" + debug_last_class + "\"><span class=\"debugTS\">[" + ts + "]</span> " + 
1496                         msg + "</li>" + c.innerHTML;
1497         }
1498 }
1499
1500 function getInitParam(key) {
1501         return init_params[key];
1502 }
1503
1504 function storeInitParam(key, value) {
1505         debug("<b>storeInitParam is OBSOLETE: " + key + " => " + value + "</b>");
1506         init_params[key] = value;
1507 }
1508
1509 function fatalError(code, msg, ext_info) {
1510         try {   
1511
1512                 if (!ext_info) ext_info = "N/A";
1513
1514                 if (code == 6) {
1515                         window.location.href = "tt-rss.php";                    
1516                 } else if (code == 5) {
1517                         window.location.href = "update.php";
1518                 } else {
1519         
1520                         if (msg == "") msg = "Unknown error";
1521
1522                         var ebc = $("xebContent");
1523         
1524                         if (ebc) {
1525         
1526                                 Element.show("dialog_overlay");
1527                                 Element.show("errorBoxShadow");
1528                                 Element.hide("xebBtn");
1529
1530                                 if (ext_info) {
1531                                         if (ext_info.responseText) {
1532                                                 ext_info = ext_info.responseText;
1533                                         }
1534                                 }
1535         
1536                                 ebc.innerHTML = 
1537                                         "<div><b>Error message:</b></div>" +
1538                                         "<pre>" + msg + "</pre>" +
1539                                         "<div><b>Additional information:</b></div>" +
1540                                         "<textarea readonly=\"1\">" + ext_info + "</textarea>";
1541                         }
1542                 }
1543
1544         } catch (e) {
1545                 exception_error("fatalError", e);
1546         }
1547 }
1548
1549 function getFeedName(id, is_cat) {      
1550         var e;
1551
1552         if (is_cat) {
1553                 e = $("FCATN-" + id);
1554         } else {
1555                 e = $("FEEDN-" + id);
1556         }
1557         if (e) {
1558                 return e.innerHTML.stripTags();
1559         } else {
1560                 return null;
1561         }
1562 }
1563
1564 function filterDlgCheckType(sender) {
1565
1566         try {
1567
1568                 var ftype = sender[sender.selectedIndex].value;
1569
1570                 var form = document.forms["filter_add_form"];
1571         
1572                 if (!form) {
1573                         form = document.forms["filter_edit_form"];
1574                 }
1575
1576                 if (!form) {
1577                         debug("filterDlgCheckType: can't find form!");
1578                         return;
1579                 }
1580
1581                 // if selected filter type is 5 (Date) enable the modifier dropbox
1582                 if (ftype == 5) {
1583                         Element.show("filter_dlg_date_mod_box");
1584                         Element.show("filter_dlg_date_chk_box");
1585                 } else {
1586                         Element.hide("filter_dlg_date_mod_box");
1587                         Element.hide("filter_dlg_date_chk_box");
1588
1589                 }
1590
1591         } catch (e) {
1592                 exception_error("filterDlgCheckType", e);
1593         }
1594
1595 }
1596
1597 function filterDlgCheckAction(sender) {
1598
1599         try {
1600
1601                 var action = sender[sender.selectedIndex].value;
1602
1603                 var form = document.forms["filter_add_form"];
1604         
1605                 if (!form) {
1606                         form = document.forms["filter_edit_form"];
1607                 }
1608
1609                 if (!form) {
1610                         debug("filterDlgCheckAction: can't find form!");
1611                         return;
1612                 }
1613
1614                 var action_param = $("filter_dlg_param_box");
1615
1616                 if (!action_param) {
1617                         debug("filterDlgCheckAction: can't find action param box!");
1618                         return;
1619                 }
1620
1621                 // if selected action supports parameters, enable params field
1622                 if (action == 4 || action == 6 || action == 7) {
1623                         Element.show(action_param);
1624                         if (action != 7) {
1625                                 Element.show(form.action_param);
1626                                 Element.hide(form.action_param_label);
1627                         } else {
1628                                 Element.show(form.action_param_label);
1629                                 Element.hide(form.action_param);
1630                         }
1631                 } else {
1632                         Element.hide(action_param);
1633                 }
1634
1635         } catch (e) {
1636                 exception_error("filterDlgCheckAction", e);
1637         }
1638
1639 }
1640
1641 function filterDlgCheckDate() {
1642         try {
1643                 var form = document.forms["filter_add_form"];
1644         
1645                 if (!form) {
1646                         form = document.forms["filter_edit_form"];
1647                 }
1648
1649                 if (!form) {
1650                         debug("filterDlgCheckAction: can't find form!");
1651                         return;
1652                 }
1653
1654                 var reg_exp = form.reg_exp.value;
1655
1656                 var query = "backend.php?op=rpc&subop=checkDate&date=" + reg_exp;
1657
1658                 new Ajax.Request(query, {
1659                         onComplete: function(transport) { 
1660
1661                                 var form = document.forms["filter_add_form"];
1662         
1663                                 if (!form) {
1664                                         form = document.forms["filter_edit_form"];
1665                                 }
1666
1667                                 if (transport.responseXML) {
1668                                         var result = transport.responseXML.getElementsByTagName("result")[0];
1669
1670                                         if (result && result.firstChild) {
1671                                                 if (result.firstChild.nodeValue == "1") {
1672
1673                                                         new Effect.Highlight(form.reg_exp, {startcolor : '#00ff00'});
1674
1675                                                         return;
1676                                                 }
1677                                         }
1678                                 }
1679
1680                                 new Effect.Highlight(form.reg_exp, {startcolor : '#ff0000'});
1681
1682                         } });
1683
1684
1685         } catch (e) {
1686                 exception_error("filterDlgCheckDate", e);
1687         }
1688 }
1689
1690 function explainError(code) {
1691         return displayDlg("explainError", code);
1692 }
1693
1694 // this only searches loaded headlines list, not in CDM
1695 function getRelativePostIds(id, limit) {
1696
1697         if (!limit) limit = 3;
1698
1699         debug("getRelativePostIds: " + id + " limit=" + limit);
1700
1701         var ids = new Array();
1702         var container = $("headlinesList");
1703
1704         if (container) {
1705                 var rows = container.rows;
1706
1707                 for (var i = 0; i < rows.length; i++) {
1708                         var r_id = rows[i].id.replace("RROW-", "");
1709
1710                         if (r_id == id) {
1711                                 for (var k = 1; k <= limit; k++) {
1712                                         var nid = false;
1713
1714                                         if (i > k-1) var nid = rows[i-k].id.replace("RROW-", "");
1715                                         if (nid) ids.push(nid);
1716
1717                                         if (i < rows.length-k) nid = rows[i+k].id.replace("RROW-", "");
1718                                         if (nid) ids.push(nid);
1719                                 }
1720
1721                                 return ids;
1722                         }
1723                 }
1724         }
1725
1726         return false;
1727 }
1728
1729 function openArticleInNewWindow(id) {
1730         try {
1731                 debug("openArticleInNewWindow: " + id);
1732
1733                 var query = "backend.php?op=rpc&subop=getArticleLink&id=" + id;
1734                 var wname = "ttrss_article_" + id;
1735
1736                 debug(query + " " + wname);
1737
1738                 var w = window.open("", wname);
1739
1740                 if (!w) notify_error("Failed to open window for the article");
1741
1742                 new Ajax.Request(query, {
1743                         onComplete: function(transport) { 
1744                                 open_article_callback(transport); 
1745                         } });
1746
1747
1748         } catch (e) {
1749                 exception_error("openArticleInNewWindow", e);
1750         }
1751 }
1752
1753 /* http://textsnippets.com/posts/show/835 */
1754
1755 Position.GetWindowSize = function(w) {
1756         w = w ? w : window;
1757         var width = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
1758         var height = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);
1759         return [width, height]
1760 }
1761
1762 /* http://textsnippets.com/posts/show/836 */
1763
1764 Position.Center = function(element, parent) {
1765         var w, h, pw, ph;
1766         var d = Element.getDimensions(element);
1767         w = d.width;
1768         h = d.height;
1769         Position.prepare();
1770         if (!parent) {
1771                 var ws = Position.GetWindowSize();
1772                 pw = ws[0];
1773                 ph = ws[1];
1774         } else {
1775                 pw = parent.offsetWidth;
1776                 ph = parent.offsetHeight;
1777         }
1778         element.style.top = (ph/2) - (h/2) -  Position.deltaY + "px";
1779         element.style.left = (pw/2) - (w/2) -  Position.deltaX + "px";
1780 }
1781
1782
1783 function labeltest_callback(transport) {
1784         try {
1785                 var container = $('label_test_result');
1786         
1787                 container.innerHTML = transport.responseText;
1788                 if (!Element.visible(container)) {
1789                         Effect.SlideDown(container, { duration : 0.5 });
1790                 }
1791
1792                 notify("");
1793         } catch (e) {
1794                 exception_error("labeltest_callback", e);
1795         }
1796 }
1797
1798 function labelTest() {
1799
1800         try {
1801                 var container = $('label_test_result');
1802         
1803                 var form = document.forms['label_edit_form'];
1804         
1805                 var sql_exp = form.sql_exp.value;
1806                 var description = form.description.value;
1807         
1808                 notify_progress("Loading, please wait...");
1809         
1810                 var query = "backend.php?op=pref-labels&subop=test&expr=" +
1811                         param_escape(sql_exp) + "&descr=" + param_escape(description);
1812         
1813                 new Ajax.Request(query, {
1814                         onComplete: function (transport) {
1815                                 labeltest_callback(transport);
1816                         } });
1817         
1818                 return false;
1819
1820         } catch (e) {
1821                 exception_error("labelTest", e);
1822         }
1823 }
1824
1825 function isCdmMode() {
1826         return !$("headlinesList");
1827 }
1828
1829 function getSelectedArticleIds2() {
1830         var rows = new Array();
1831         var cdm_mode = isCdmMode();
1832
1833         if (cdm_mode) {
1834                 rows = cdmGetSelectedArticles();
1835         } else {        
1836                 rows = getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
1837         }
1838
1839         var ids = new Array();
1840
1841         for (var i = 0; i < rows.length; i++) {
1842                 var chk = $("RCHK-" + rows[i]);
1843                 if (chk && chk.checked) {
1844                         ids.push(rows[i]);
1845                 }
1846         }
1847
1848         return ids;
1849 }
1850
1851 function displayHelpInfobox(topic_id) {
1852
1853         var url = "backend.php?op=help&tid=" + param_escape(topic_id);
1854
1855         var w = window.open(url, "ttrss_help", 
1856                 "status=0,toolbar=0,location=0,width=450,height=500,scrollbars=1,menubar=0");
1857
1858 }
1859
1860 function focus_element(id) {
1861         try {
1862                 var e = $(id);
1863                 if (e) e.focus();
1864         } catch (e) {
1865                 exception_error("focus_element", e);
1866         }
1867         return false;
1868 }
1869
1870 function loading_set_progress(p) {
1871         try {
1872                 if (p < last_progress_point || !Element.visible("overlay")) return;
1873
1874                 debug("<b>loading_set_progress : " + p + " (" + last_progress_point + ")</b>");
1875
1876                 var o = $("l_progress_i");
1877
1878 //              o.style.width = (p * 2) + "px";
1879
1880                 new Effect.Scale(o, p, { 
1881                         scaleY : false,
1882                         scaleFrom : last_progress_point,
1883                         scaleMode: { originalWidth : 200 },
1884                         queue: { position: 'end', scope: 'LSP-Q', limit: 3 } }); 
1885
1886                 last_progress_point = p;
1887
1888         } catch (e) {
1889                 exception_error("loading_set_progress", e);
1890         }
1891 }
1892
1893 function remove_splash() {
1894         if (Element.visible("overlay")) {
1895                 debug("about to remove splash, OMG!");
1896                 Element.hide("overlay");
1897                 debug("removed splash!");
1898         }
1899 }
1900
1901 function addLabelExample() {
1902         try {
1903                 var form = document.forms["label_edit_form"];
1904
1905                 var text = form.sql_exp;
1906                 var op = form.label_fields[form.label_fields.selectedIndex];
1907                 var p = form.label_fields_param;
1908
1909                 if (op) {
1910                         op = op.value;
1911
1912                         var tmp = "";
1913
1914                         if (text.value != "") {                 
1915                                 if (text.value.substring(text.value.length-3, 3).toUpperCase() != "AND") {
1916                                         tmp = " AND ";
1917                                 } else {
1918                                         tmp = " ";
1919                                 }
1920                         }
1921
1922                         if (op == "unread") {
1923                                 tmp = tmp + "unread = true";
1924                         }
1925
1926                         if (op == "updated") {
1927                                 tmp = tmp + "last_read is null and unread = false";
1928                         }
1929
1930                         if (op == "kw_title") {
1931                                 if (p.value == "") {
1932                                         alert("This action requires a parameter.");
1933                                         return false;
1934                                 }
1935                                 tmp = tmp + "ttrss_entries.title like '%"+p.value+"%'";
1936                         }
1937
1938                         if (op == "kw_content") {
1939                                 if (p.value == "") {
1940                                         alert("This action requires a parameter.");
1941                                         return false;
1942                                 }
1943
1944                                 tmp = tmp + "ttrss_entries.content like '%"+p.value+"%'";
1945                         }
1946
1947                         if (op == "scoreE") {
1948                                 if (isNaN(parseInt(p.value))) {
1949                                         alert("This action expects numeric parameter.");
1950                                         return false;
1951                                 }
1952                                 tmp = tmp + "score = " + p.value;
1953                         }
1954
1955                         if (op == "scoreG") {
1956                                 if (isNaN(parseInt(p.value))) {
1957                                         alert("This action expects numeric parameter.");
1958                                         return false;
1959                                 }
1960                                 tmp = tmp + "score > " + p.value;
1961                         }
1962
1963                         if (op == "scoreL") {
1964                                 if (isNaN(parseInt(p.value))) {
1965                                         alert("This action expects numeric parameter.");
1966                                         return false;
1967                                 }
1968                                 tmp = tmp + "score < " + p.value;
1969                         }
1970
1971                         if (op == "newerD") {
1972                                 if (isNaN(parseInt(p.value))) {
1973                                         alert("This action expects numeric parameter.");
1974                                         return false;
1975                                 }
1976                                 tmp = tmp + "updated > NOW() - INTERVAL '"+parseInt(p.value)+" days'";
1977                         }
1978
1979                         if (op == "newerH") {
1980                                 if (isNaN(parseInt(p.value))) {
1981                                         alert("This action expects numeric parameter.");
1982                                         return false;
1983                                 }
1984
1985                                 tmp = tmp + "updated > NOW() - INTERVAL '"+parseInt(p.value)+" hours'";
1986                         }
1987
1988                         text.value = text.value + tmp;
1989
1990                         p.value = "";
1991
1992                 }
1993                 
1994         } catch (e) {
1995                 exception_error("addLabelExample", e);
1996         }
1997
1998         return false;
1999 }
2000
2001 function labelFieldsCheck(elem) {
2002         try {
2003                 var op = elem[elem.selectedIndex].value;
2004
2005                 var p = document.forms["label_edit_form"].label_fields_param;
2006
2007                 if (op == "kw_title" || op == "kw_content" || op == "scoreL" || 
2008                                 op == "scoreG" ||       op == "scoreE" || op == "newerD" ||
2009                                 op == "newerH" ) {
2010                         Element.show(p);
2011                 } else {
2012                         Element.hide(p);
2013                 }
2014
2015         } catch (e) {
2016                 exception_error("labelFieldsCheck", e);
2017
2018         }
2019 }
2020
2021 function getSelectedFeedsFromBrowser() {
2022
2023         var list = $("browseFeedList");
2024         if (!list) list = $("browseBigFeedList");
2025
2026         var selected = new Array();
2027         
2028         for (i = 0; i < list.childNodes.length; i++) {
2029                 var child = list.childNodes[i];
2030                 if (child.id && child.id.match("FBROW-")) {
2031                         var id = child.id.replace("FBROW-", "");
2032                         
2033                         var cb = $("FBCHK-" + id);
2034
2035                         if (cb.checked) {
2036                                 selected.push(id);
2037                         }
2038                 }
2039         }
2040
2041         return selected;
2042 }
2043
2044 function updateFeedBrowser() {
2045         try {
2046
2047                 var query = "backend.php?op=rpc&subop=feedBrowser";
2048
2049                 var search = $("feed_browser_search");
2050                 var limit = $("feed_browser_limit");
2051
2052                 if (limit) {
2053                         query = query + "&limit=" + limit[limit.selectedIndex].value;
2054                 }
2055
2056                 if (search) {
2057                         query = query + "&search=" + param_escape(search.value);
2058                 }
2059
2060                 //notify_progress("Loading, please wait...", true);
2061
2062                 Element.show('feed_browser_spinner');
2063
2064                 new Ajax.Request(query, {
2065                         onComplete: function(transport) { 
2066                                 notify('');
2067
2068                                 Element.hide('feed_browser_spinner');
2069
2070                                 var c = $("browseFeedList");
2071                                 var r = transport.responseXML.getElementsByTagName("content")[0];
2072                                 var nr = transport.responseXML.getElementsByTagName("num-results")[0];
2073                                 var sb = $("feed_browser_subscribe");
2074
2075                                 if (c && r) {
2076                                         c.innerHTML = r.firstChild.nodeValue;
2077                                 }
2078         
2079                                 if (nr && sb) {
2080                                         if (nr.getAttribute("value") > 0) {
2081                                                 sb.disabled = false;
2082                                         } else {
2083                                                 sb.disabled = true;
2084                                         }
2085                                 }
2086
2087                         } });
2088
2089
2090         } catch (e) {
2091                 exception_error("updateFeedBrowser", e);
2092         }
2093 }
2094
2095 function browseFeeds(limit) {
2096
2097         try {
2098
2099                 var query = "backend.php?op=pref-feeds&subop=browse";
2100
2101                 notify_progress("Loading, please wait...", true);
2102
2103                 new Ajax.Request(query, {
2104                         onComplete: function(transport) { 
2105                                 infobox_callback2(transport);
2106                         } });
2107
2108                 return false;
2109         } catch (e) {
2110                 exception_error("browseFeeds", e);
2111         }
2112 }
2113
2114 function transport_error_check(transport) {
2115         try {
2116                 if (transport.responseXML) {
2117                         var error = transport.responseXML.getElementsByTagName("error")[0];
2118
2119                         if (error) {
2120                                 var code = error.getAttribute("error-code");
2121                                 var msg = error.getAttribute("error-msg");
2122                                 if (code != 0) {
2123                                         fatalError(code, msg);
2124                                         return false;
2125                                 }
2126                         }
2127                 }
2128         } catch (e) {
2129                 exception_error("check_for_error_xml", e);
2130         }
2131         return true;
2132 }
2133
2134 function strip_tags(s) {
2135         return s.replace(/<\/?[^>]+(>|$)/g, "");
2136 }
2137
2138 function truncate_string(s, length) {
2139         if (!length) length = 30;
2140         var tmp = s.substring(0, length);
2141         if (s.length > length) tmp += "&hellip;";
2142         return tmp;
2143 }
2144
2145 /*
2146 function switchToFlash(e) {
2147         try {
2148                 var targ = e;
2149                 if (!e) var e = window.event;
2150                 if (e.target) targ = e.target;
2151                 else if (e.srcElement) targ = e.srcElement;
2152                 if (targ.nodeType == 3) // defeat Safari bug
2153                         targ = targ.parentNode;
2154                 
2155                 //targ is the link that was clicked
2156                 var audioTag=targ;
2157                 do {
2158                         audioTag=audioTag.previousSibling;
2159                 } while(audioTag && audioTag.nodeType != 1)
2160                 
2161                 var flashPlayer = audioTag.getElementsByTagName('span')[0];
2162                 targ.parentNode.insertBefore(flashPlayer,targ);
2163                 targ.parentNode.removeChild(targ);
2164                 audioTag.parentNode.removeChild(audioTag);
2165
2166                 return false;
2167         } catch (e) {
2168                 exception_error("switchToFlash", e);
2169         }
2170 }
2171
2172 function html5AudioOrFlash(type) {
2173         var audioTag = document.createElement('audio');
2174         if(! audioTag.canPlayType || audioTag.canPlayType(type) == "no" ||
2175                         audioTag.canPlayType(type) == ""){
2176                 if($('switchToFlashLink')){
2177                         switchToFlash($('switchToFlashLink'));
2178                 }
2179         }
2180 } */
2181
2182 function hotkey_prefix_timeout() {
2183         try {
2184
2185                 var date = new Date();
2186                 var ts = Math.round(date.getTime() / 1000);
2187
2188                 if (hotkey_prefix_pressed && ts - hotkey_prefix_pressed >= 5) {
2189                         debug("hotkey_prefix seems to be stuck, aborting");
2190                         hotkey_prefix_pressed = false;
2191                         hotkey_prefix = false;
2192                         Element.hide('cmdline');
2193                 }
2194
2195                 setTimeout("hotkey_prefix_timeout()", 1000);
2196
2197         } catch  (e) {
2198                 exception_error("hotkey_prefix_timeout", e);
2199         }
2200 }
2201
2202 function hideAuxDlg() {
2203         try {
2204                 Element.hide('auxDlg');
2205         } catch (e) {
2206                 exception_error("hideAuxDlg", e);
2207         }
2208 }
2209
2210 function displayNewContentPrompt(id) {
2211         try {
2212                 var msg = __("New articles in &laquo;%s&raquo;.") +
2213                         " <a href='#' onclick='viewfeed("+id+")'>" + __('Click to view') + "</a>.";
2214
2215                 msg = msg.replace("%s", getFeedName(id));
2216
2217                 $('auxDlg').innerHTML = msg;
2218
2219                 Element.show('auxDlg');
2220
2221         } catch (e) {
2222                 exception_error("displayNewContentPrompt", e);
2223         }
2224 }