]> git.wh0rd.org Git - tt-rss.git/blob - functions.js
add hotkey f w: resort feedlist by name or unread count
[tt-rss.git] / functions.js
1 var hotkeys_enabled = true;
2 var xmlhttp_rpc = Ajax.getTransport();
3 var notify_silent = false;
4 var last_progress_point = 0;
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, silent) {
19         var msg;
20
21         if (e.fileName) {
22                 var base_fname = e.fileName.substring(e.fileName.lastIndexOf("/") + 1);
23         
24                 msg = "Exception: " + e.name + ", " + e.message + 
25                         "\nFunction: " + location + "()" +
26                         "\nLocation: " + base_fname + ":" + e.lineNumber;
27
28         } else if (e.description) {
29                 msg = "Exception: " + e.description + "\nFunction: " + location + "()";
30         } else {
31                 msg = "Exception: " + e + "\nFunction: " + location + "()";
32         }
33
34         debug("<b>EXCEPTION: " + msg + "</b>");
35
36         if (!silent) {
37                 alert(msg);
38         }
39 }
40
41 function disableHotkeys() {
42         hotkeys_enabled = false;
43 }
44
45 function enableHotkeys() {
46         hotkeys_enabled = true;
47 }
48
49 function xmlhttp_ready(obj) {
50         return obj.readyState == 4 || obj.readyState == 0 || !obj.readyState;
51 }
52
53 function open_article_callback(transport) {
54         try {
55
56                 if (transport.responseXML) {
57                         
58                         var link = transport.responseXML.getElementsByTagName("link")[0];
59                         var id = transport.responseXML.getElementsByTagName("id")[0];
60
61                         debug("open_article_callback, received link: " + link);
62
63                         if (link && id) {
64
65                                 var wname = "ttrss_article_" + id.firstChild.nodeValue;
66
67                                 debug("link url: " + link.firstChild.nodeValue + ", wname " + wname);
68
69                                 var w = window.open(link.firstChild.nodeValue, wname);
70
71                                 if (!w) { notify_error("Failed to load article in new window"); }
72
73                                 if (id) {
74                                         id = id.firstChild.nodeValue;
75                                         if (!document.getElementById("headlinesList")) {
76                                                 window.setTimeout("toggleUnread(" + id + ", 0)", 100);
77                                         }
78                                 }
79                         } else {
80                                 notify_error("Can't open article: received invalid article link");
81                         }
82                 } else {
83                         notify_error("Can't open article: received invalid XML");
84                 }
85
86         } catch (e) {
87                 exception_error("open_article_callback", e);
88         }
89 }
90
91 function param_escape(arg) {
92         if (typeof encodeURIComponent != 'undefined')
93                 return encodeURIComponent(arg); 
94         else
95                 return escape(arg);
96 }
97
98 function param_unescape(arg) {
99         if (typeof decodeURIComponent != 'undefined')
100                 return decodeURIComponent(arg); 
101         else
102                 return unescape(arg);
103 }
104
105 function delay(gap) {
106         var then,now; 
107         then=new Date().getTime();
108         now=then;
109         while((now-then)<gap) {
110                 now=new Date().getTime();
111         }
112 }
113
114 var notify_hide_timerid = false;
115
116 function hide_notify() {
117         var n = document.getElementById("notify");
118         if (n) {
119                 n.style.display = "none";
120         }
121
122
123 function notify_silent_next() {
124         notify_silent = true;
125 }
126
127 function notify_real(msg, no_hide, n_type) {
128
129         if (notify_silent) {
130                 notify_silent = false;
131                 return;
132         }
133
134         var n = document.getElementById("notify");
135         var nb = document.getElementById("notify_body");
136
137         if (!n || !nb) return;
138
139         if (notify_hide_timerid) {
140                 window.clearTimeout(notify_hide_timerid);
141         }
142
143         if (msg == "") {
144                 if (n.style.display == "block") {
145                         notify_hide_timerid = window.setTimeout("hide_notify()", 0);
146                 }
147                 return;
148         } else {
149                 n.style.display = "block";
150         }
151
152         /* types:
153
154                 1 - generic
155                 2 - progress
156                 3 - error
157                 4 - info
158
159         */
160
161         if (typeof __ != 'undefined') {
162                 msg = __(msg);
163         }
164
165         if (n_type == 1) {
166                 n.className = "notify";
167         } else if (n_type == 2) {
168                 n.className = "notifyProgress";
169                 msg = "<img src='images/indicator_white.gif'> " + msg;
170         } else if (n_type == 3) {
171                 n.className = "notifyError";
172                 msg = "<img src='images/sign_excl.gif'> " + msg;
173         } else if (n_type == 4) {
174                 n.className = "notifyInfo";
175                 msg = "<img src='images/sign_info.gif'> " + msg;
176         }
177
178 //      msg = "<img src='images/live_com_loading.gif'> " + msg;
179
180         nb.innerHTML = msg;
181
182         if (!no_hide) {
183                 notify_hide_timerid = window.setTimeout("hide_notify()", 3000);
184         }
185 }
186
187 function notify(msg, no_hide) {
188         notify_real(msg, no_hide, 1);
189 }
190
191 function notify_progress(msg, no_hide) {
192         notify_real(msg, no_hide, 2);
193 }
194
195 function notify_error(msg, no_hide) {
196         notify_real(msg, no_hide, 3);
197
198 }
199
200 function notify_info(msg, no_hide) {
201         notify_real(msg, no_hide, 4);
202 }
203
204 function printLockingError() {
205         notify_info("Please wait until operation finishes.");
206 }
207
208 function cleanSelected(element) {
209         var content = document.getElementById(element);
210
211         for (i = 0; i < content.rows.length; i++) {
212                 content.rows[i].className = content.rows[i].className.replace("Selected", "");
213         }
214 }
215
216 function getVisibleUnreadHeadlines() {
217         var content = document.getElementById("headlinesList");
218
219         var rows = new Array();
220
221         if (!content) return rows;
222
223         for (i = 0; i < content.rows.length; i++) {
224                 var row_id = content.rows[i].id.replace("RROW-", "");
225                 if (row_id.length > 0 && content.rows[i].className.match("Unread")) {
226                                 rows.push(row_id);      
227                 }
228         }
229         return rows;
230 }
231
232 function getVisibleHeadlineIds() {
233
234         var content = document.getElementById("headlinesList");
235
236         var rows = new Array();
237
238         if (!content) return rows;
239
240         for (i = 0; i < content.rows.length; i++) {
241                 var row_id = content.rows[i].id.replace("RROW-", "");
242                 if (row_id.length > 0) {
243                                 rows.push(row_id);      
244                 }
245         }
246         return rows;
247 }
248
249 function getFirstVisibleHeadlineId() {
250         if (isCdmMode()) {
251                 var rows = cdmGetVisibleArticles();
252                 return rows[0];
253         } else {
254                 var rows = getVisibleHeadlineIds();
255                 return rows[0];
256         }
257 }
258
259 function getLastVisibleHeadlineId() {
260         if (isCdmMode()) {
261                 var rows = cdmGetVisibleArticles();
262                 return rows[rows.length-1];
263         } else {
264                 var rows = getVisibleHeadlineIds();
265                 return rows[rows.length-1];
266         }
267 }
268
269 function markHeadline(id) {
270         var row = document.getElementById("RROW-" + id);
271         if (row) {
272                 var is_active = false;
273         
274                 if (row.className.match("Active")) {
275                         is_active = true;
276                 }
277                 row.className = row.className.replace("Selected", "");
278                 row.className = row.className.replace("Active", "");
279                 row.className = row.className.replace("Insensitive", "");
280                 
281                 if (is_active) {
282                         row.className = row.className = "Active";
283                 }
284                 
285                 var check = document.getElementById("RCHK-" + id);
286
287                 if (check) {
288                         check.checked = true;
289                 }
290
291                 row.className = row.className + "Selected"; 
292                 
293         }
294 }
295
296 function getFeedIds() {
297         var content = document.getElementById("feedsList");
298
299         var rows = new Array();
300
301         for (i = 0; i < content.rows.length; i++) {
302                 var id = content.rows[i].id.replace("FEEDR-", "");
303                 if (id.length > 0) {
304                         rows.push(id);
305                 }
306         }
307
308         return rows;
309 }
310
311 function setCookie(name, value, lifetime, path, domain, secure) {
312         
313         var d = false;
314         
315         if (lifetime) {
316                 d = new Date();
317                 d.setTime(d.getTime() + (lifetime * 1000));
318         }
319
320         debug("setCookie: " + name + " => " + value + ": " + d);
321         
322         int_setCookie(name, value, d, path, domain, secure);
323
324 }
325
326 function int_setCookie(name, value, expires, path, domain, secure) {
327         document.cookie= name + "=" + escape(value) +
328                 ((expires) ? "; expires=" + expires.toGMTString() : "") +
329                 ((path) ? "; path=" + path : "") +
330                 ((domain) ? "; domain=" + domain : "") +
331                 ((secure) ? "; secure" : "");
332 }
333
334 function delCookie(name, path, domain) {
335         if (getCookie(name)) {
336                 document.cookie = name + "=" +
337                 ((path) ? ";path=" + path : "") +
338                 ((domain) ? ";domain=" + domain : "" ) +
339                 ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
340         }
341 }
342                 
343
344 function getCookie(name) {
345
346         var dc = document.cookie;
347         var prefix = name + "=";
348         var begin = dc.indexOf("; " + prefix);
349         if (begin == -1) {
350             begin = dc.indexOf(prefix);
351             if (begin != 0) return null;
352         }
353         else {
354             begin += 2;
355         }
356         var end = document.cookie.indexOf(";", begin);
357         if (end == -1) {
358             end = dc.length;
359         }
360         return unescape(dc.substring(begin + prefix.length, end));
361 }
362
363 function disableContainerChildren(id, disable, doc) {
364
365         if (!doc) doc = document;
366
367         var container = doc.getElementById(id);
368
369         if (!container) {
370                 //alert("disableContainerChildren: element " + id + " not found");
371                 return;
372         }
373
374         for (var i = 0; i < container.childNodes.length; i++) {
375                 var child = container.childNodes[i];
376
377                 try {
378                         child.disabled = disable;
379                 } catch (E) {
380
381                 }
382
383                 if (disable) {
384                         if (child.className && child.className.match("button")) {
385                                 child.className = "disabledButton";
386                         }
387                 } else {
388                         if (child.className && child.className.match("disabledButton")) {
389                                 child.className = "button";
390                         }
391                 } 
392         }
393
394 }
395
396 function gotoPreferences() {
397         document.location.href = "prefs.php";
398 }
399
400 function gotoMain() {
401         document.location.href = "tt-rss.php";
402 }
403
404 function gotoExportOpml() {
405         document.location.href = "opml.php?op=Export";
406 }
407
408 function getActiveFeedId() {
409 //      return getCookie("ttrss_vf_actfeed");
410         try {
411                 debug("gAFID: " + active_feed_id);
412                 return active_feed_id;
413         } catch (e) {
414                 exception_error("getActiveFeedId", e);
415         }
416 }
417
418 function activeFeedIsCat() {
419         return active_feed_is_cat;
420 }
421
422 function setActiveFeedId(id) {
423 //      return setCookie("ttrss_vf_actfeed", id);
424         try {
425                 debug("sAFID(" + id + ")");
426                 active_feed_id = id;
427         } catch (e) {
428                 exception_error("setActiveFeedId", e);
429         }
430 }
431
432 function parse_counters(reply, scheduled_call) {
433         try {
434
435                 var feeds_found = 0;
436
437                 var elems = reply.getElementsByTagName("counter");
438
439                 for (var l = 0; l < elems.length; l++) {
440
441                         var id = elems[l].getAttribute("id");
442                         var t = elems[l].getAttribute("type");
443                         var ctr = elems[l].getAttribute("counter");
444                         var error = elems[l].getAttribute("error");
445                         var has_img = elems[l].getAttribute("hi");
446                         var updated = elems[l].getAttribute("updated");
447                         var title = elems[l].getAttribute("title");
448                         var xmsg = elems[l].getAttribute("xmsg");
449         
450                         if (id == "global-unread") {
451                                 global_unread = ctr;
452                                 updateTitle();
453                                 continue;
454                         }
455
456                         if (id == "subscribed-feeds") {
457                                 feeds_found = ctr;
458                                 continue;
459                         }
460         
461                         if (t == "category") {
462                                 var catctr = document.getElementById("FCATCTR-" + id);
463                                 if (catctr) {
464                                         catctr.innerHTML = "(" + ctr + ")";
465                                         if (ctr > 0) {
466                                                 catctr.className = "catCtrHasUnread";
467                                         } else {
468                                                 catctr.className = "catCtrNoUnread";
469                                         }
470                                 }
471                                 continue;
472                         }
473                 
474                         var feedctr = document.getElementById("FEEDCTR-" + id);
475                         var feedu = document.getElementById("FEEDU-" + id);
476                         var feedr = document.getElementById("FEEDR-" + id);
477                         var feed_img = document.getElementById("FIMG-" + id);
478                         var feedlink = document.getElementById("FEEDL-" + id);
479                         var feedupd = document.getElementById("FLUPD-" + id);
480
481                         if (updated && feedlink) {
482                                 if (error) {
483                                         feedlink.title = "Error: " + error + " (" + updated + ")";
484                                 } else {
485                                         feedlink.title = "Updated: " + updated;
486                                 }
487                         }
488
489                         if (feedupd) {
490                                 if (!updated) updated = "";
491
492                                 if (error) {
493                                         if (xmsg) {
494                                                 feedupd.innerHTML = updated + " " + xmsg + " (Error)";
495                                         } else {
496                                                 feedupd.innerHTML = updated + " (Error)";
497                                         }
498                                 } else {
499                                         if (xmsg) {
500                                                 feedupd.innerHTML = updated + " " + xmsg;
501                                         } else {
502                                                 feedupd.innerHTML = updated;
503                                         }
504                                 }
505                         }
506
507                         if (has_img && feed_img) {
508                                 if (!feed_img.src.match(id + ".ico")) {
509                                         feed_img.src = getInitParam("icons_location") + "/" + id + ".ico";
510                                 }
511                         }
512
513                         if (feedlink && title) {
514                                 feedlink.innerHTML = title;
515                         }
516
517                         if (feedctr && feedu && feedr) {
518
519                                 if (feedu.innerHTML != ctr && id == getActiveFeedId() && scheduled_call) {
520                                         viewCurrentFeed();
521                                 }
522
523                                 var row_needs_hl = (ctr > 0 && ctr > parseInt(feedu.innerHTML));
524
525                                 feedu.innerHTML = ctr;
526
527                                 if (error) {
528                                         feedr.className = feedr.className.replace("feed", "error");
529                                 } else if (id > 0) {
530                                         feedr.className = feedr.className.replace("error", "feed");
531                                 }
532         
533                                 if (ctr > 0) {                                  
534                                         feedctr.className = "odd";
535                                         if (!feedr.className.match("Unread")) {
536                                                 var is_selected = feedr.className.match("Selected");
537                 
538                                                 feedr.className = feedr.className.replace("Selected", "");
539                                                 feedr.className = feedr.className.replace("Unread", "");
540                 
541                                                 feedr.className = feedr.className + "Unread";
542                 
543                                                 if (is_selected) {
544                                                         feedr.className = feedr.className + "Selected";
545                                                 }       
546                                                 
547                                         }
548
549                                         if (row_needs_hl) { 
550                                                 new Effect.Highlight(feedr, {duration: 1, startcolor: "#fff7d5",
551                                                         queue: { position:'end', scope: 'EFQ-' + id, limit: 1 } } );
552                                         }
553                                 } else {
554                                         feedctr.className = "invisible";
555                                         feedr.className = feedr.className.replace("Unread", "");
556                                 }                       
557                         }
558                 }
559
560                 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
561
562                 var feeds_stored = number_of_feeds;
563
564                 debug("Feed counters, C: " + feeds_found + ", S:" + feeds_stored);
565
566                 if (feeds_stored != feeds_found) {
567                         number_of_feeds = feeds_found;
568
569                         if (feeds_stored != 0 && feeds_found != 0) {
570                                 debug("Subscribed feed number changed, refreshing feedlist");
571                                 setTimeout('updateFeedList(false, false)', 50);
572                         }
573                 }
574
575         } catch (e) {
576                 exception_error("parse_counters", e);
577         }
578 }
579
580 function parse_counters_reply(transport, scheduled_call) {
581
582         if (!transport.responseXML) {
583                 notify_error("Backend did not return valid XML", true);
584                 return;
585         }
586
587         var reply = transport.responseXML.firstChild;
588         
589         if (!reply) {
590                 notify_error("Backend did not return expected XML object", true);
591                 updateTitle("");
592                 return;
593         } 
594         
595         var error_code = false;
596         var error_msg = false;
597
598         if (reply.firstChild) {
599                 error_code = reply.firstChild.getAttribute("error-code");
600                 error_msg = reply.firstChild.getAttribute("error-msg");
601         }
602
603         if (!error_code) {      
604                 error_code = reply.getAttribute("error-code");
605                 error_msg = reply.getAttribute("error-msg");
606         }
607         
608         if (error_code && error_code != 0) {
609                 debug("refetch_callback: got error code " + error_code);
610                 return fatalError(error_code, error_msg);
611         }
612
613         var counters = reply.getElementsByTagName("counters")[0];
614         
615         parse_counters(counters, scheduled_call);
616
617         var runtime_info = reply.getElementsByTagName("runtime-info")[0];
618
619         parse_runtime_info(runtime_info);
620
621         if (feedsSortByUnread()) {
622                 resort_feedlist();
623         }
624
625         hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
626
627 }
628
629 function all_counters_callback2(transport) {
630         try {
631                 debug("<b>all_counters_callback2 IN: " + transport + "</b>");
632                 parse_counters_reply(transport);
633                 debug("<b>all_counters_callback2 OUT: " + transport + "</b>");
634
635         } catch (e) {
636                 exception_error("all_counters_callback2", e);
637         }
638 }
639
640 function get_feed_unread(id) {
641         try {
642                 return parseInt(document.getElementById("FEEDU-" + id).innerHTML);      
643         } catch (e) {
644                 exception_error("get_feed_unread", e, true);
645                 return -1;
646         }
647 }
648
649 function get_feed_entry_unread(elem) {
650
651         var id = elem.id.replace("FEEDR-", "");
652
653         if (id <= 0) {
654                 return -1;
655         }
656
657         try {
658                 return parseInt(document.getElementById("FEEDU-" + id).innerHTML);      
659         } catch (e) {
660                 return -1;
661         }
662 }
663
664 function get_feed_entry_name(elem) {
665         var id = elem.id.replace("FEEDR-", "");
666         return getFeedName(id);
667 }
668
669
670 function resort_category(node) {
671         debug("resort_category: " + node);
672
673         var by_unread = feedsSortByUnread();
674
675         if (node.hasChildNodes() && node.firstChild.nextSibling != false) {  
676                 for (i = 0; i < node.childNodes.length; i++) {
677                         if (node.childNodes[i].nodeName != "LI") { continue; }
678
679                         if (get_feed_entry_unread(node.childNodes[i]) < 0) {
680                                 continue;
681                         }
682
683                         for (j = i+1; j < node.childNodes.length; j++) {                        
684                                 if (node.childNodes[j].nodeName != "LI") { continue; }  
685
686                                 var tmp_val = get_feed_entry_unread(node.childNodes[i]);
687                                 var cur_val = get_feed_entry_unread(node.childNodes[j]);
688
689                                 var tmp_name = get_feed_entry_name(node.childNodes[i]);
690                                 var cur_name = get_feed_entry_name(node.childNodes[j]);
691
692                                 if ((by_unread && (cur_val > tmp_val)) || (!by_unread && (cur_name < tmp_name))) {
693                                         tempnode_i = node.childNodes[i].cloneNode(true);
694                                         tempnode_j = node.childNodes[j].cloneNode(true);
695                                         node.replaceChild(tempnode_i, node.childNodes[j]);
696                                         node.replaceChild(tempnode_j, node.childNodes[i]);
697                                 }
698                         }
699
700                 }
701         }
702
703 }
704
705 function resort_feedlist() {
706         debug("resort_feedlist");
707
708         if (document.getElementById("FCATLIST--1")) {
709
710                 var lists = document.getElementsByTagName("UL");
711
712                 for (var i = 0; i < lists.length; i++) {
713                         if (lists[i].id && lists[i].id.match("FCATLIST-")) {
714                                 resort_category(lists[i]);
715                         }
716                 }
717
718         } else {
719                 resort_category(document.getElementById("feedList"));
720         }
721 }
722
723 /** * @(#)isNumeric.js * * Copyright (c) 2000 by Sundar Dorai-Raj
724   * * @author Sundar Dorai-Raj
725   * * Email: sdoraira@vt.edu
726   * * This program is free software; you can redistribute it and/or
727   * * modify it under the terms of the GNU General Public License 
728   * * as published by the Free Software Foundation; either version 2 
729   * * of the License, or (at your option) any later version, 
730   * * provided that any use properly credits the author. 
731   * * This program is distributed in the hope that it will be useful,
732   * * but WITHOUT ANY WARRANTY; without even the implied warranty of
733   * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
734   * * GNU General Public License for more details at http://www.gnu.org * * */
735
736   var numbers=".0123456789";
737   function isNumeric(x) {
738     // is x a String or a character?
739     if(x.length>1) {
740       // remove negative sign
741       x=Math.abs(x)+"";
742       for(j=0;j<x.length;j++) {
743         // call isNumeric recursively for each character
744         number=isNumeric(x.substring(j,j+1));
745         if(!number) return number;
746       }
747       return number;
748     }
749     else {
750       // if x is number return true
751       if(numbers.indexOf(x)>=0) return true;
752       return false;
753     }
754   }
755
756
757 function hideOrShowFeeds(hide) {
758
759         try {
760
761         debug("hideOrShowFeeds: " + hide);
762
763         if (document.getElementById("FCATLIST--1")) {
764
765                 var lists = document.getElementsByTagName("UL");
766
767                 for (var i = 0; i < lists.length; i++) {
768                         if (lists[i].id && lists[i].id.match("FCATLIST-")) {
769
770                                 var id = lists[i].id.replace("FCATLIST-", "");
771                                 hideOrShowFeedsCategory(id, hide);
772                         }
773                 }
774
775         } else {
776                 hideOrShowFeedsCategory(null, hide);
777         }
778
779         } catch (e) {
780                 exception_error("hideOrShowFeeds", e);
781         }
782 }
783
784 function hideOrShowFeedsCategory(id, hide) {
785
786         try {
787         
788                 var node = null;
789                 var cat_node = null;
790
791                 if (id) {
792                         node = document.getElementById("FCATLIST-" + id);
793                         cat_node = document.getElementById("FCAT-" + id);
794                 } else {
795                         node = document.getElementById("feedList"); // no categories
796                 }
797
798         //      debug("hideOrShowFeedsCategory: " + node + " (" + hide + ")");
799         
800                 var cat_unread = 0;
801         
802                 if (!node) {
803                         debug("hideOrShowFeeds: passed node is null, aborting");
804                         return;
805                 }
806         
807         //      debug("cat: " + node.id);
808         
809                 if (node.hasChildNodes() && node.firstChild.nextSibling != false) {  
810                         for (i = 0; i < node.childNodes.length; i++) {
811                                 if (node.childNodes[i].nodeName != "LI") { continue; }
812         
813                                 if (node.childNodes[i].style != undefined) {
814         
815                                         var has_unread = (node.childNodes[i].className != "feed" &&
816                                                 node.childNodes[i].className != "label" && 
817                                                 !(!getInitParam("hide_read_shows_special") && 
818                                                         node.childNodes[i].className == "virt") && 
819                                                 node.childNodes[i].className != "error" && 
820                                                 node.childNodes[i].className != "tag");
821                 
822         //                              debug(node.childNodes[i].id + " --> " + has_unread);
823                 
824                                         if (hide && !has_unread) {
825                                                 //node.childNodes[i].style.display = "none";
826                                                 var id = node.childNodes[i].id;
827                                                 Effect.Fade(node.childNodes[i], {duration : 0.3, 
828                                                         queue: { position: 'end', scope: 'FFADE-' + id, limit: 1 }});
829                                         }
830                 
831                                         if (!hide) {
832                                                 node.childNodes[i].style.display = "list-item";
833                                                 //Effect.Appear(node.childNodes[i], {duration : 0.3});
834                                         }
835                 
836                                         if (has_unread) {
837                                                 node.childNodes[i].style.display = "list-item";
838                                                 cat_unread++;
839                                                 //Effect.Appear(node.childNodes[i], {duration : 0.3});
840                                                 //Effect.Highlight(node.childNodes[i]);
841                                         }
842                                 }
843                         }
844                 }       
845         
846         //      debug("end cat: " + node.id + " unread " + cat_unread);
847         
848                 if (cat_unread == 0) {
849                         if (cat_node.style == undefined) {
850                                 debug("ERROR: supplied cat_node " + cat_node + 
851                                         " has no styles. WTF?");
852                                 return;
853                         }
854                         if (hide) {
855                                 //cat_node.style.display = "none";
856                                 Effect.Fade(cat_node, {duration : 0.3, 
857                                         queue: { position: 'end', scope: 'CFADE-' + node.id, limit: 1 }});
858                         } else {
859                                 cat_node.style.display = "list-item";
860                         }
861                 } else {
862                         try {
863                                 cat_node.style.display = "list-item";
864                         } catch (e) {
865                                 debug(e);
866                         }
867                 }
868
869 //      debug("unread for category: " + cat_unread);
870
871         } catch (e) {
872                 exception_error("hideOrShowFeedsCategory", e);
873         }
874 }
875
876 function selectTableRow(r, do_select) {
877         r.className = r.className.replace("Selected", "");
878         
879         if (do_select) {
880                 r.className = r.className + "Selected";
881         }
882 }
883
884 function selectTableRowById(elem_id, check_id, do_select) {
885
886         try {
887
888                 var row = document.getElementById(elem_id);
889
890                 if (row) {
891                         selectTableRow(row, do_select);
892                 }               
893
894                 var check = document.getElementById(check_id);
895
896                 if (check) {
897                         check.checked = do_select;
898                 }
899         } catch (e) {
900                 exception_error("selectTableRowById", e);
901         }
902 }
903
904 function selectTableRowsByIdPrefix(content_id, prefix, check_prefix, do_select, 
905         classcheck, reset_others) {
906
907         var content = document.getElementById(content_id);
908
909         if (!content) {
910                 alert("[selectTableRows] Element " + content_id + " not found.");
911                 return;
912         }
913
914         for (i = 0; i < content.rows.length; i++) {
915                 if (Element.visible(content.rows[i])) {
916                         if (!classcheck || content.rows[i].className.match(classcheck)) {
917                 
918                                 if (content.rows[i].id.match(prefix)) {
919                                         selectTableRow(content.rows[i], do_select);
920                                 
921                                         var row_id = content.rows[i].id.replace(prefix, "");
922                                         var check = document.getElementById(check_prefix + row_id);
923         
924                                         if (check) {
925                                                 check.checked = do_select;
926                                         }
927                                 } else if (reset_others) {
928                                         selectTableRow(content.rows[i], false);
929         
930                                         var row_id = content.rows[i].id.replace(prefix, "");
931                                         var check = document.getElementById(check_prefix + row_id);
932         
933                                         if (check) {
934                                                 check.checked = false;
935                                         }
936         
937                                 }
938                         } else if (reset_others) {
939                                 selectTableRow(content.rows[i], false);
940         
941                                 var row_id = content.rows[i].id.replace(prefix, "");
942                                 var check = document.getElementById(check_prefix + row_id);
943         
944                                 if (check) {
945                                         check.checked = false;
946                                 }
947         
948                         }
949                 }
950         }
951 }
952
953 function getSelectedTableRowIds(content_id, prefix) {
954
955         var content = document.getElementById(content_id);
956
957         if (!content) {
958                 alert("[getSelectedTableRowIds] Element " + content_id + " not found.");
959                 return;
960         }
961
962         var sel_rows = new Array();
963
964         for (i = 0; i < content.rows.length; i++) {
965                 if (content.rows[i].id.match(prefix) && 
966                                 content.rows[i].className.match("Selected")) {
967                                 
968                         var row_id = content.rows[i].id.replace(prefix + "-", "");
969                         sel_rows.push(row_id);  
970                 }
971         }
972
973         return sel_rows;
974
975 }
976
977 function toggleSelectRowById(sender, id) {
978         var row = document.getElementById(id);
979
980         if (sender.checked) {
981                 if (!row.className.match("Selected")) {
982                         row.className = row.className + "Selected";
983                 }
984         } else {
985                 if (row.className.match("Selected")) {
986                         row.className = row.className.replace("Selected", "");
987                 }
988         }
989 }
990
991 function toggleSelectListRow(sender) {
992         var parent_row = sender.parentNode;
993
994         if (sender.checked) {
995                 if (!parent_row.className.match("Selected")) {
996                         parent_row.className = parent_row.className + "Selected";
997                 }
998         } else {
999                 if (parent_row.className.match("Selected")) {
1000                         parent_row.className = parent_row.className.replace("Selected", "");
1001                 }
1002         }
1003 }
1004
1005 function tSR(sender) {
1006         return toggleSelectRow(sender);
1007 }
1008
1009 function toggleSelectRow(sender) {
1010         var parent_row = sender.parentNode.parentNode;
1011
1012         if (sender.checked) {
1013                 if (!parent_row.className.match("Selected")) {
1014                         parent_row.className = parent_row.className + "Selected";
1015                 }
1016         } else {
1017                 if (parent_row.className.match("Selected")) {
1018                         parent_row.className = parent_row.className.replace("Selected", "");
1019                 }
1020         }
1021 }
1022
1023 function getRelativeFeedId(list, id, direction, unread_only) {  
1024         var rows = list.getElementsByTagName("LI");
1025         var feeds = new Array();
1026
1027         for (var i = 0; i < rows.length; i++) {
1028                 if (rows[i].id.match("FEEDR-")) {
1029
1030                         if (rows[i].id == "FEEDR-" + id || (Element.visible(rows[i]) && Element.visible(rows[i].parentNode))) {
1031
1032                                 if (!unread_only || 
1033                                                 (rows[i].className.match("Unread") || rows[i].id == "FEEDR-" + id)) {
1034                                         feeds.push(rows[i].id.replace("FEEDR-", ""));
1035                                 }
1036                         }
1037                 }
1038         }
1039
1040         if (!id) {
1041                 if (direction == "next") {
1042                         return feeds.shift();
1043                 } else {
1044                         return feeds.pop();
1045                 }
1046         } else {
1047                 if (direction == "next") {
1048                         var idx = feeds.indexOf(id);
1049                         if (idx != -1 && idx < feeds.length) {
1050                                 return feeds[idx+1];                                    
1051                         } else {
1052                                 return getRelativeFeedId(list, false, direction, unread_only);
1053                         }
1054                 } else {
1055                         var idx = feeds.indexOf(id);
1056                         if (idx > 0) {
1057                                 return feeds[idx-1];
1058                         } else {
1059                                 return getRelativeFeedId(list, false, direction, unread_only);
1060                         }
1061                 }
1062
1063         }
1064 }
1065
1066 function showBlockElement(id, h_id) {
1067         var elem = document.getElementById(id);
1068
1069         if (elem) {
1070                 elem.style.display = "block";
1071
1072                 if (h_id) {
1073                         elem = document.getElementById(h_id);
1074                         if (elem) {
1075                                 elem.style.display = "none";
1076                         }
1077                 }
1078         } else {
1079                 alert("[showBlockElement] can't find element with id " + id);
1080         } 
1081 }
1082
1083 function appearBlockElement_afh(effect) {
1084
1085 }
1086
1087 function checkboxToggleElement(elem, id) {
1088         if (elem.checked) {
1089                 Effect.SlideDown(id, {duration : 0.5});
1090         } else {
1091                 Effect.SlideUp(id, {duration : 0.5});
1092         }
1093 }
1094
1095 function appearBlockElement(id, h_id) {
1096
1097         try {
1098                 if (h_id) {
1099                         Effect.Fade(h_id);
1100                 }
1101                 Effect.SlideDown(id, {duration : 1.0, afterFinish: appearBlockElement_afh});
1102         } catch (e) {
1103                 exception_error("appearBlockElement", e);
1104         }
1105
1106 }
1107
1108 function hideParentElement(e) {
1109         e.parentNode.style.display = "none";
1110 }
1111
1112 function dropboxSelect(e, v) {
1113         for (i = 0; i < e.length; i++) {
1114                 if (e[i].value == v) {
1115                         e.selectedIndex = i;
1116                         break;
1117                 }
1118         }
1119 }
1120
1121 // originally stolen from http://www.11tmr.com/11tmr.nsf/d6plinks/MWHE-695L9Z
1122 // bugfixed just a little bit :-)
1123 function getURLParam(strParamName){
1124   var strReturn = "";
1125   var strHref = window.location.href;
1126
1127   if (strHref.indexOf("#") == strHref.length-1) {
1128                 strHref = strHref.substring(0, strHref.length-1);
1129   }
1130
1131   if ( strHref.indexOf("?") > -1 ){
1132     var strQueryString = strHref.substr(strHref.indexOf("?"));
1133     var aQueryString = strQueryString.split("&");
1134     for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
1135       if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
1136         var aParam = aQueryString[iParam].split("=");
1137         strReturn = aParam[1];
1138         break;
1139       }
1140     }
1141   }
1142   return strReturn;
1143
1144
1145 function leading_zero(p) {
1146         var s = String(p);
1147         if (s.length == 1) s = "0" + s;
1148         return s;
1149 }
1150
1151 function closeInfoBox(cleanup) {
1152
1153         Element.hide("dialog_overlay");
1154
1155         var box = document.getElementById('infoBox');
1156         var shadow = document.getElementById('infoBoxShadow');
1157
1158         if (shadow) {
1159                 shadow.style.display = "none";
1160         } else if (box) {
1161                 box.style.display = "none";
1162         }
1163
1164         if (cleanup) box.innerHTML = "&nbsp;";
1165
1166         enableHotkeys();
1167
1168         return false;
1169 }
1170
1171
1172 function displayDlg(id, param) {
1173
1174         notify_progress("Loading, please wait...", true);
1175
1176         disableHotkeys();
1177
1178         var query = "backend.php?op=dlg&id=" +
1179                 param_escape(id) + "&param=" + param_escape(param);
1180
1181         new Ajax.Request(query, {
1182                 onComplete: function (transport) {
1183                         infobox_callback2(transport);
1184                 } });
1185
1186         return false;
1187 }
1188
1189 function infobox_submit_callback2(transport) {
1190         closeInfoBox();
1191
1192         try {
1193                 // called from prefs, reload tab
1194                 if (typeof active_tab != 'undefined' && active_tab) {
1195                         selectTab(active_tab, false);
1196                 }
1197         } catch (e) { }
1198
1199         if (transport.responseText) {
1200                 notify_info(transport.responseText);
1201         }
1202 }
1203
1204 function infobox_callback2(transport) {
1205         try {
1206
1207                 debug("infobox_callback2");
1208
1209                 if (!getInitParam("infobox_disable_overlay")) {
1210                         Element.show("dialog_overlay");
1211                 }
1212
1213                 var box = document.getElementById('infoBox');
1214                 var shadow = document.getElementById('infoBoxShadow');
1215                 if (box) {                      
1216
1217                         box.innerHTML=transport.responseText;                   
1218                         if (shadow) {
1219                                 shadow.style.display = "block";
1220                         } else {
1221                                 box.style.display = "block";                            
1222                         }
1223                 }
1224
1225                 /* FIXME this needs to be moved out somewhere */
1226
1227                 if (document.getElementById("tags_choices")) {
1228                         new Ajax.Autocompleter('tags_str', 'tags_choices',
1229                                 "backend.php?op=rpc&subop=completeTags",
1230                                 { tokens: ',', paramName: "search" });
1231                 }
1232
1233                 disableHotkeys();
1234
1235                 notify("");
1236         } catch (e) {
1237                 exception_error("infobox_callback2", e);
1238         }
1239 }
1240
1241 function createFilter() {
1242
1243         try {
1244
1245                 var form = document.forms['filter_add_form'];
1246                 var reg_exp = form.reg_exp.value;
1247         
1248                 if (reg_exp == "") {
1249                         alert(__("Can't add filter: nothing to match on."));
1250                         return false;
1251                 }
1252         
1253                 var query = Form.serialize("filter_add_form");
1254         
1255                 // we can be called from some other tab in Prefs                
1256                 if (typeof active_tab != 'undefined' && active_tab) {
1257                         active_tab = "filterConfig";
1258                 }
1259         
1260                 new Ajax.Request("backend.php?" + query, {
1261                         onComplete: function (transport) {
1262                                 infobox_submit_callback2(transport);
1263                         } });
1264                 
1265                 return true;
1266
1267         } catch (e) {
1268                 exception_error("createFilter", e);
1269         }
1270 }
1271
1272 function toggleSubmitNotEmpty(e, submit_id) {
1273         try {
1274                 document.getElementById(submit_id).disabled = (e.value == "")
1275         } catch (e) {
1276                 exception_error("toggleSubmitNotEmpty", e);
1277         }
1278 }
1279
1280 function isValidURL(s) {
1281         return s.match("http://") != null || s.match("https://") != null || s.match("feed://") != null;
1282 }
1283
1284 function subscribeToFeed() {
1285
1286         var form = document.forms['feed_add_form'];
1287         var feed_url = form.feed_url.value;
1288
1289         if (feed_url == "") {
1290                 alert(__("Can't subscribe: no feed URL given."));
1291                 return false;
1292         }
1293
1294         notify_progress(__("Subscribing to feed..."), true);
1295
1296         closeInfoBox();
1297
1298         var feeds_doc = document;
1299
1300 //      feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
1301         
1302         var query = Form.serialize("feed_add_form");
1303         
1304         debug("subscribe q: " + query);
1305
1306         new Ajax.Request("backend.php", {
1307                 parameters: query,
1308                 onComplete: function(transport) { 
1309                         dlg_frefresh_callback(transport); 
1310                 } });
1311
1312         return false;
1313 }
1314
1315 function filterCR(e, f)
1316 {
1317      var key;
1318
1319      if(window.event)
1320           key = window.event.keyCode;     //IE
1321      else
1322           key = e.which;     //firefox
1323
1324         if (key == 13) {
1325                 if (typeof f != 'undefined') {
1326                         f();
1327                         return false;
1328                 } else {
1329                         return false;
1330                 }
1331         } else {
1332                 return true;
1333         }
1334 }
1335
1336 function getMainContext() {
1337         return this.window;
1338 }
1339
1340 function getFeedsContext() {
1341         return this.window;
1342 }
1343
1344 function getContentContext() {
1345         return this.window;
1346 }
1347
1348 function getHeadlinesContext() {
1349         return this.window;
1350 }
1351
1352 var debug_last_class = "even";
1353
1354 function debug(msg) {
1355
1356         if (debug_last_class == "even") {
1357                 debug_last_class = "odd";
1358         } else {
1359                 debug_last_class = "even";
1360         }
1361
1362         var c = document.getElementById('debug_output');
1363         if (c && Element.visible(c)) {
1364                 while (c.lastChild != 'undefined' && c.childNodes.length > 100) {
1365                         c.removeChild(c.lastChild);
1366                 }
1367         
1368                 var d = new Date();
1369                 var ts = leading_zero(d.getHours()) + ":" + leading_zero(d.getMinutes()) +
1370                         ":" + leading_zero(d.getSeconds());
1371                 c.innerHTML = "<li class=\"" + debug_last_class + "\"><span class=\"debugTS\">[" + ts + "]</span> " + 
1372                         msg + "</li>" + c.innerHTML;
1373         }
1374 }
1375
1376 function getInitParam(key) {
1377         return init_params[key];
1378 }
1379
1380 function storeInitParam(key, value) {
1381         debug("<b>storeInitParam is OBSOLETE: " + key + " => " + value + "</b>");
1382         init_params[key] = value;
1383 }
1384
1385 function fatalError(code, message) {
1386         try {   
1387
1388                 if (code == 6) {
1389                         window.location.href = "tt-rss.php";                    
1390                 } else if (code == 5) {
1391                         window.location.href = "update.php";
1392                 } else {
1393                         var fe = document.getElementById("fatal_error");
1394                         var fc = document.getElementById("fatal_error_msg");
1395         
1396                         if (message == "") message = "Unknown error";
1397
1398                         fc.innerHTML = "<img src='images/sign_excl.gif'> " + message + " (Code " + code + ")";
1399         
1400                         fe.style.display = "block";
1401                 }
1402
1403         } catch (e) {
1404                 exception_error("fatalError", e);
1405         }
1406 }
1407
1408 function getFeedName(id, is_cat) {      
1409         var d = getFeedsContext().document;
1410
1411         var e;
1412
1413         if (is_cat) {
1414                 e = d.getElementById("FCATN-" + id);
1415         } else {
1416                 e = d.getElementById("FEEDN-" + id);
1417         }
1418         if (e) {
1419                 return e.innerHTML.stripTags();
1420         } else {
1421                 return null;
1422         }
1423 }
1424
1425 function viewContentUrl(url) {
1426         getContentContext().location = url;
1427 }
1428
1429 function filterDlgCheckAction(sender) {
1430
1431         try {
1432
1433                 var action = sender[sender.selectedIndex].value;
1434
1435                 var form = document.forms["filter_add_form"];
1436         
1437                 if (!form) {
1438                         form = document.forms["filter_edit_form"];
1439                 }
1440
1441                 if (!form) {
1442                         debug("filterDlgCheckAction: can't find form!");
1443                         return;
1444                 }
1445
1446                 var action_param = form.action_param;
1447
1448                 if (!action_param) {
1449                         debug("filterDlgCheckAction: can't find action param!");
1450                         return;
1451                 }
1452
1453                 // if selected action supports parameters, enable params field
1454                 if (action == 4 || action == 6) {
1455                         action_param.disabled = false;
1456                 } else {
1457                         action_param.disabled = true;
1458                 }
1459
1460         } catch (e) {
1461                 exception_error("filterDlgCheckAction", e);
1462         }
1463
1464 }
1465
1466 function explainError(code) {
1467         return displayDlg("explainError", code);
1468 }
1469
1470 // this only searches loaded headlines list, not in CDM
1471 function getRelativePostIds(id, limit) {
1472
1473         if (!limit) limit = 3;
1474
1475         debug("getRelativePostIds: " + id + " limit=" + limit);
1476
1477         var ids = new Array();
1478         var container = document.getElementById("headlinesList");
1479
1480         if (container) {
1481                 var rows = container.rows;
1482
1483                 for (var i = 0; i < rows.length; i++) {
1484                         var r_id = rows[i].id.replace("RROW-", "");
1485
1486                         if (r_id == id) {
1487                                 for (var k = 1; k <= limit; k++) {
1488                                         var nid = false;
1489
1490                                         if (i > k-1) var nid = rows[i-k].id.replace("RROW-", "");
1491                                         if (nid) ids.push(nid);
1492
1493                                         if (i < rows.length-k) nid = rows[i+k].id.replace("RROW-", "");
1494                                         if (nid) ids.push(nid);
1495                                 }
1496
1497                                 return ids;
1498                         }
1499                 }
1500         }
1501
1502         return false;
1503 }
1504
1505 function openArticleInNewWindow(id) {
1506         try {
1507                 debug("openArticleInNewWindow: " + id);
1508
1509                 var query = "backend.php?op=rpc&subop=getArticleLink&id=" + id;
1510                 var wname = "ttrss_article_" + id;
1511
1512                 debug(query + " " + wname);
1513
1514                 var w = window.open("", wname);
1515
1516                 if (!w) notify_error("Failed to open window for the article");
1517
1518                 new Ajax.Request(query, {
1519                         onComplete: function(transport) { 
1520                                 open_article_callback(transport); 
1521                         } });
1522
1523
1524         } catch (e) {
1525                 exception_error("openArticleInNewWindow", e);
1526         }
1527 }
1528
1529 /* http://textsnippets.com/posts/show/835 */
1530
1531 Position.GetWindowSize = function(w) {
1532         w = w ? w : window;
1533         var width = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
1534         var height = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);
1535         return [width, height]
1536 }
1537
1538 /* http://textsnippets.com/posts/show/836 */
1539
1540 Position.Center = function(element, parent) {
1541         var w, h, pw, ph;
1542         var d = Element.getDimensions(element);
1543         w = d.width;
1544         h = d.height;
1545         Position.prepare();
1546         if (!parent) {
1547                 var ws = Position.GetWindowSize();
1548                 pw = ws[0];
1549                 ph = ws[1];
1550         } else {
1551                 pw = parent.offsetWidth;
1552                 ph = parent.offsetHeight;
1553         }
1554         element.style.top = (ph/2) - (h/2) -  Position.deltaY + "px";
1555         element.style.left = (pw/2) - (w/2) -  Position.deltaX + "px";
1556 }
1557
1558
1559 function labeltest_callback(transport) {
1560         try {
1561                 var container = document.getElementById('label_test_result');
1562         
1563                 container.innerHTML = transport.responseText;
1564                 if (!Element.visible(container)) {
1565                         Effect.SlideDown(container, { duration : 0.5 });
1566                 }
1567
1568                 notify("");
1569         } catch (e) {
1570                 exception_error("labeltest_callback", e);
1571         }
1572 }
1573
1574 function labelTest() {
1575
1576         try {
1577                 var container = document.getElementById('label_test_result');
1578         
1579                 var form = document.forms['label_edit_form'];
1580         
1581                 var sql_exp = form.sql_exp.value;
1582                 var description = form.description.value;
1583         
1584                 notify_progress("Loading, please wait...");
1585         
1586                 var query = "backend.php?op=pref-labels&subop=test&expr=" +
1587                         param_escape(sql_exp) + "&descr=" + param_escape(description);
1588         
1589                 new Ajax.Request(query, {
1590                         onComplete: function (transport) {
1591                                 labeltest_callback(transport);
1592                         } });
1593         
1594                 return false;
1595
1596         } catch (e) {
1597                 exception_error("labelTest", e);
1598         }
1599 }
1600
1601 function isCdmMode() {
1602         return !document.getElementById("headlinesList");
1603 }
1604
1605 function getSelectedArticleIds2() {
1606         var rows = new Array();
1607         var cdm_mode = isCdmMode();
1608
1609         if (cdm_mode) {
1610                 rows = cdmGetSelectedArticles();
1611         } else {        
1612                 rows = getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
1613         }
1614
1615         var ids = new Array();
1616
1617         for (var i = 0; i < rows.length; i++) {
1618                 var chk = document.getElementById("RCHK-" + rows[i]);
1619                 if (chk && chk.checked) {
1620                         ids.push(rows[i]);
1621                 }
1622         }
1623
1624         return ids;
1625 }
1626
1627 function displayHelpInfobox(topic_id) {
1628
1629         var url = "backend.php?op=help&tid=" + param_escape(topic_id);
1630
1631         var w = window.open(url, "ttrss_help", 
1632                 "status=0,toolbar=0,location=0,width=450,height=500,scrollbars=1,menubar=0");
1633
1634         return false;
1635 }
1636
1637 function focus_element(id) {
1638         try {
1639                 var e = document.getElementById(id);
1640                 if (e) e.focus();
1641         } catch (e) {
1642                 exception_error("focus_element", e);
1643         }
1644         return false;
1645 }
1646
1647 function loading_set_progress(p) {
1648         try {
1649                 if (p < last_progress_point || !Element.visible("overlay")) return;
1650
1651                 debug("<b>loading_set_progress : " + p + " (" + last_progress_point + ")</b>");
1652
1653                 var o = document.getElementById("l_progress_i");
1654
1655 //              o.style.width = (p * 2) + "px";
1656
1657                 new Effect.Scale(o, p, { 
1658                         scaleY : false,
1659                         scaleFrom : last_progress_point,
1660                         scaleMode: { originalWidth : 200 },
1661                         queue: { position: 'end', scope: 'LSP-Q', limit: 3 } }); 
1662
1663                 last_progress_point = p;
1664
1665         } catch (e) {
1666                 exception_error("loading_set_progress", e);
1667         }
1668 }
1669
1670 function remove_splash() {
1671         if (Element.visible("overlay")) {
1672                 debug("about to remove splash, OMG!");
1673                 Element.hide("overlay");
1674                 debug("removed splash!");
1675         }
1676 }