]> git.wh0rd.org Git - tt-rss.git/blob - functions.js
72638dcca3e4fcb64df3aa86ae7238c309522fdd
[tt-rss.git] / functions.js
1 var hotkeys_enabled = true;
2
3 var xmlhttp_rpc = Ajax.getTransport();
4
5 function browser_has_opacity() {
6         return navigator.userAgent.match("Gecko") != null || 
7                 navigator.userAgent.match("Opera") != null;
8 }
9
10 function is_opera() {
11         return navigator.userAgent.match("Opera");
12 }
13
14 function exception_error(location, e, silent) {
15         var msg;
16
17         if (e.fileName) {
18                 var base_fname = e.fileName.substring(e.fileName.lastIndexOf("/") + 1);
19         
20                 msg = "Exception: " + e.name + ", " + e.message + 
21                         "\nFunction: " + location + "()" +
22                         "\nLocation: " + base_fname + ":" + e.lineNumber;
23                 
24         } else {
25                 msg = "Exception: " + e + "\nFunction: " + location + "()";
26         }
27
28         debug("<b>EXCEPTION: " + msg + "</b>");
29
30         if (!silent) {
31                 alert(msg);
32         }
33 }
34
35 function disableHotkeys() {
36         hotkeys_enabled = false;
37 }
38
39 function enableHotkeys() {
40         hotkeys_enabled = true;
41 }
42
43 function xmlhttp_ready(obj) {
44         return obj.readyState == 4 || obj.readyState == 0 || !obj.readyState;
45 }
46
47 function notify_callback() {
48         var container = document.getElementById('notify');
49         if (xmlhttp.readyState == 4) {
50                 container.innerHTML=xmlhttp.responseText;
51         }
52 }
53
54 function rpc_notify_callback() {
55         var container = document.getElementById('notify');
56         if (xmlhttp_rpc.readyState == 4) {
57                 container.innerHTML=xmlhttp_rpc.responseText;
58         }
59 }
60
61 function param_escape(arg) {
62         if (typeof encodeURIComponent != 'undefined')
63                 return encodeURIComponent(arg); 
64         else
65                 return escape(arg);
66 }
67
68 function param_unescape(arg) {
69         if (typeof decodeURIComponent != 'undefined')
70                 return decodeURIComponent(arg); 
71         else
72                 return unescape(arg);
73 }
74
75 function delay(gap) {
76         var then,now; 
77         then=new Date().getTime();
78         now=then;
79         while((now-then)<gap) {
80                 now=new Date().getTime();
81         }
82 }
83
84 var notify_hide_timerid = false;
85 var notify_last_doc = false;
86
87 var notify_effect = false;
88
89 function hide_notify() {
90         if (notify_last_doc) {
91                 var n = notify_last_doc.getElementById("notify");               
92                 if (browser_has_opacity()) {
93                         if (notify_opacity >= 0) {
94                                 notify_opacity = notify_opacity - 0.1;
95                                 n.style.opacity = notify_opacity;
96                                 notify_hide_timerid = window.setTimeout("hide_notify()", 20);   
97                         } else {
98                                 n.style.display = "none";
99                                 n.style.opacity = 1;
100                         }
101                 } else {
102                         n.style.display = "none";
103                 }
104         }
105 }
106
107 function notify_real(msg, doc, no_hide, is_err) {
108
109         var n = doc.getElementById("notify");
110         var nb = doc.getElementById("notify_body");
111
112         if (!n || !nb) return;
113
114         if (notify_hide_timerid) {
115                 window.clearTimeout(notify_hide_timerid);
116         }
117
118         notify_last_doc = doc;
119         notify_opacity = 1;
120
121         if (msg == "") {
122                 if (n.style.display == "block") {
123                         notify_hide_timerid = window.setTimeout("hide_notify()", 0);
124                 }
125                 return;
126         } else {
127                 n.style.display = "block";
128         }
129
130         if (is_err) {
131                 n.style.backgroundColor = "#ffcccc";
132                 n.style.color = "black";
133                 n.style.borderColor = "#ff0000";
134         } else {
135                 n.style.backgroundColor = "#fff7d5";
136                 n.style.borderColor = "#d7c47a";
137                 n.style.color = "black";
138         }
139
140         nb.innerHTML = msg;
141
142         if (!no_hide) {
143                 notify_hide_timerid = window.setTimeout("hide_notify()", 3000);
144         }
145 }
146
147 function p_notify(msg, no_hide, is_err) {
148         notify_real(msg, document, no_hide, is_err);
149 }
150
151 function notify(msg, no_hide, is_err) {
152         notify_real(msg, document, no_hide, is_err);
153 }
154
155 function printLockingError() {
156         notify("Please wait until operation finishes");}
157
158 function hotkey_handler(e) {
159
160         try {
161
162                 var keycode;
163         
164                 if (!hotkeys_enabled) return;
165         
166                 if (window.event) {
167                         keycode = window.event.keyCode;
168                 } else if (e) {
169                         keycode = e.which;
170                 }
171
172                 var m_ctx = getMainContext();
173                 var f_ctx = getFeedsContext();
174                 var h_ctx = getHeadlinesContext();
175
176                 if (keycode == 82) { // r
177                         return m_ctx.scheduleFeedUpdate(true);
178                 }
179
180                 if (keycode == 83) { // r
181                         return m_ctx.displayDlg("search", getActiveFeedId());
182                 }
183
184                 if (keycode == 85) { // u
185                         if (getActiveFeedId()) {
186                                 return f_ctx.viewfeed(getActiveFeedId(), "ForceUpdate");
187                         }
188                 }
189         
190                 if (keycode == 65) { // a
191                         return m_ctx.toggleDispRead();
192                 }
193         
194                 var f_doc = document;
195                 var feedlist = f_doc.getElementById('feedList');
196         
197                 if (keycode == 74) { // j
198                         var feed = getActiveFeedId();
199                         var new_feed = getRelativeFeedId(feedlist, feed, 'prev');
200                         if (new_feed) viewfeed(new_feed, '');
201                 }
202         
203                 if (keycode == 75) { // k
204                         var feed = getActiveFeedId();
205                         var new_feed = getRelativeFeedId(feedlist, feed, 'next');
206                         if (new_feed) viewfeed(new_feed, '');
207                 }
208
209                 if (keycode == 78 || keycode == 40) { // n, down
210                         if (typeof h_ctx.moveToPost != 'undefined') {
211                                 return h_ctx.moveToPost('next');
212                         }
213                 }
214         
215                 if (keycode == 80 || keycode == 38) { // p, up
216                         if (typeof h_ctx.moveToPost != 'undefined') {
217                                 return h_ctx.moveToPost('prev');
218                         }
219                 }
220                 
221                 if (typeof localHotkeyHandler != 'undefined') {
222                         try {
223                                 localHotkeyHandler(keycode);
224                         } catch (e) {
225                                 exception_error("hotkey_handler, local:", e);
226                         }
227                 }
228         } catch (e) {
229                 exception_error("hotkey_handler", e);
230         }
231 }
232
233 function cleanSelectedList(element) {
234         var content = document.getElementById(element);
235
236         if (!document.getElementById("feedCatHolder")) {
237                 for (i = 0; i < content.childNodes.length; i++) {
238                         var child = content.childNodes[i];
239                         try {
240                                 child.className = child.className.replace("Selected", "");
241                         } catch (e) {
242                                 //
243                         }
244                 }
245         } else {
246                 for (i = 0; i < content.childNodes.length; i++) {
247                         var child = content.childNodes[i];
248                         if (child.id == "feedCatHolder") {
249                                 debug(child.id);
250                                 var fcat = child.lastChild;
251                                 for (j = 0; j < fcat.childNodes.length; j++) {
252                                         var feed = fcat.childNodes[j];
253                                         feed.className = feed.className.replace("Selected", "");
254                                 }               
255                         }
256                 } 
257         }
258 }
259
260
261 function cleanSelected(element) {
262         var content = document.getElementById(element);
263
264         for (i = 0; i < content.rows.length; i++) {
265                 content.rows[i].className = content.rows[i].className.replace("Selected", "");
266         }
267 }
268
269 function getVisibleUnreadHeadlines() {
270         var content = document.getElementById("headlinesList");
271
272         var rows = new Array();
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 && content.rows[i].className.match("Unread")) {
277                                 rows.push(row_id);      
278                 }
279         }
280         return rows;
281 }
282
283 function getVisibleHeadlineIds() {
284
285         var content = document.getElementById("headlinesList");
286
287         var rows = new Array();
288
289         for (i = 0; i < content.rows.length; i++) {
290                 var row_id = content.rows[i].id.replace("RROW-", "");
291                 if (row_id.length > 0) {
292                                 rows.push(row_id);      
293                 }
294         }
295         return rows;
296 }
297
298 function getFirstVisibleHeadlineId() {
299         var rows = getVisibleHeadlineIds();
300         return rows[0];
301 }
302
303 function getLastVisibleHeadlineId() {
304         var rows = getVisibleHeadlineIds();
305         return rows[rows.length-1];
306 }
307
308 function markHeadline(id) {
309         var row = document.getElementById("RROW-" + id);
310         if (row) {
311                 var is_active = false;
312         
313                 if (row.className.match("Active")) {
314                         is_active = true;
315                 }
316                 row.className = row.className.replace("Selected", "");
317                 row.className = row.className.replace("Active", "");
318                 row.className = row.className.replace("Insensitive", "");
319                 
320                 if (is_active) {
321                         row.className = row.className = "Active";
322                 }
323                 
324                 var check = document.getElementById("RCHK-" + id);
325
326                 if (check) {
327                         check.checked = true;
328                 }
329
330                 row.className = row.className + "Selected"; 
331                 
332         }
333 }
334
335 function getFeedIds() {
336         var content = document.getElementById("feedsList");
337
338         var rows = new Array();
339
340         for (i = 0; i < content.rows.length; i++) {
341                 var id = content.rows[i].id.replace("FEEDR-", "");
342                 if (id.length > 0) {
343                         rows.push(id);
344                 }
345         }
346
347         return rows;
348 }
349
350 function setCookie(name, value, lifetime, path, domain, secure) {
351         
352         var d = false;
353         
354         if (lifetime) {
355                 d = new Date();
356                 d.setTime(lifetime * 1000);
357         }
358         
359         int_setCookie(name, value, d, path, domain, secure);
360
361 }
362
363 function int_setCookie(name, value, expires, path, domain, secure) {
364         document.cookie= name + "=" + escape(value) +
365                 ((expires) ? "; expires=" + expires.toGMTString() : "") +
366                 ((path) ? "; path=" + path : "") +
367                 ((domain) ? "; domain=" + domain : "") +
368                 ((secure) ? "; secure" : "");
369 }
370
371 function delCookie(name, path, domain) {
372         if (getCookie(name)) {
373                 document.cookie = name + "=" +
374                 ((path) ? ";path=" + path : "") +
375                 ((domain) ? ";domain=" + domain : "" ) +
376                 ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
377         }
378 }
379                 
380
381 function getCookie(name) {
382
383         var dc = document.cookie;
384         var prefix = name + "=";
385         var begin = dc.indexOf("; " + prefix);
386         if (begin == -1) {
387             begin = dc.indexOf(prefix);
388             if (begin != 0) return null;
389         }
390         else {
391             begin += 2;
392         }
393         var end = document.cookie.indexOf(";", begin);
394         if (end == -1) {
395             end = dc.length;
396         }
397         return unescape(dc.substring(begin + prefix.length, end));
398 }
399
400 function disableContainerChildren(id, disable, doc) {
401
402         if (!doc) doc = document;
403
404         var container = doc.getElementById(id);
405
406         if (!container) {
407                 //alert("disableContainerChildren: element " + id + " not found");
408                 return;
409         }
410
411         for (var i = 0; i < container.childNodes.length; i++) {
412                 var child = container.childNodes[i];
413
414                 try {
415                         child.disabled = disable;
416                 } catch (E) {
417
418                 }
419
420                 if (disable) {
421                         if (child.className && child.className.match("button")) {
422                                 child.className = "disabledButton";
423                         }
424                 } else {
425                         if (child.className && child.className.match("disabledButton")) {
426                                 child.className = "button";
427                         }
428                 } 
429         }
430
431 }
432
433 function gotoPreferences() {
434         document.location.href = "prefs.php";
435 }
436
437 function gotoMain() {
438         document.location.href = "tt-rss.php";
439 }
440
441 function gotoExportOpml() {
442         document.location.href = "opml.php?op=Export";
443 }
444
445 function getActiveFeedId() {
446 //      return getCookie("ttrss_vf_actfeed");
447         try {
448                 debug("gAFID: " + getMainContext().active_feed_id);
449                 return getMainContext().active_feed_id;
450         } catch (e) {
451                 exception_error("getActiveFeedId", e);
452         }
453 }
454
455 function activeFeedIsCat() {
456         return getMainContext().active_feed_is_cat;
457 }
458
459 function setActiveFeedId(id) {
460 //      return setCookie("ttrss_vf_actfeed", id);
461         try {
462                 debug("sAFID(" + id + ")");
463                 getMainContext().active_feed_id = id;
464         } catch (e) {
465                 exception_error("setActiveFeedId", e);
466         }
467 }
468
469 function parse_counters(reply, scheduled_call) {
470         try {
471                 var f_document = document;
472                 var title_obj = this.window;
473
474                 var feeds_found = 0;
475
476                 if (reply.firstChild && reply.firstChild.firstChild) {
477                         debug("<b>wrong element passed to parse_counters, adjusting.</b>");
478                         reply = reply.firstChild;
479                 }
480
481                 debug("F_DOC: " + f_document + ", T_OBJ: " + title_obj);
482
483                 for (var l = 0; l < reply.childNodes.length; l++) {
484                         if (!reply.childNodes[l] ||
485                                 typeof(reply.childNodes[l].getAttribute) == "undefined") {
486                                 // where did this come from?
487                                 continue;
488                         }
489
490                         var id = reply.childNodes[l].getAttribute("id");
491                         var t = reply.childNodes[l].getAttribute("type");
492                         var ctr = reply.childNodes[l].getAttribute("counter");
493                         var error = reply.childNodes[l].getAttribute("error");
494                         var has_img = reply.childNodes[l].getAttribute("hi");
495                         var updated = reply.childNodes[l].getAttribute("updated");
496         
497                         if (id == "global-unread") {
498                                 title_obj.global_unread = ctr;
499                                 title_obj.updateTitle();
500                                 continue;
501                         }
502
503                         if (id == "subscribed-feeds") {
504                                 feeds_found = ctr;
505                                 continue;
506                         }
507         
508                         if (t == "category") {
509                                 var catctr = f_document.getElementById("FCATCTR-" + id);
510                                 if (catctr) {
511                                         catctr.innerHTML = "(" + ctr + " unread)";
512                                 }
513                                 continue;
514                         }
515                 
516                         var feedctr = f_document.getElementById("FEEDCTR-" + id);
517                         var feedu = f_document.getElementById("FEEDU-" + id);
518                         var feedr = f_document.getElementById("FEEDR-" + id);
519                         var feed_img = f_document.getElementById("FIMG-" + id);
520                         var feedlink = f_document.getElementById("FEEDL-" + id);
521                         var feedupd = f_document.getElementById("FLUPD-" + id);
522
523                         if (updated && feedlink) {
524                                 if (error) {
525                                         feedlink.title = "Error: " + error + " (" + updated + ")";
526                                 } else {
527                                         feedlink.title = "Updated: " + updated;
528                                 }
529                         }
530
531                         if (updated && feedupd) {
532                                 if (error) {
533                                         feedupd.innerHTML = updated + " (Error)";
534                                 } else {
535                                         feedupd.innerHTML = updated;
536                                 }
537                         }
538
539                         if (feedctr && feedu && feedr) {
540
541                                 if (feedu.innerHTML != ctr && id == getActiveFeedId() && scheduled_call) {
542                                         viewCurrentFeed();
543                                 }
544                 
545                                 feedu.innerHTML = ctr;
546
547                                 if (error) {
548                                         feedr.className = feedr.className.replace("feed", "error");
549                                 } else if (id > 0) {
550                                         feedr.className = feedr.className.replace("error", "feed");
551                                 }
552         
553                                 if (ctr > 0) {                                  
554                                         feedctr.className = "odd";
555                                         if (!feedr.className.match("Unread")) {
556                                                 var is_selected = feedr.className.match("Selected");
557                 
558                                                 feedr.className = feedr.className.replace("Selected", "");
559                                                 feedr.className = feedr.className.replace("Unread", "");
560                 
561                                                 feedr.className = feedr.className + "Unread";
562                 
563                                                 if (is_selected) {
564                                                         feedr.className = feedr.className + "Selected";
565                                                 }
566                 
567                                         }
568                                 } else {
569                                         feedctr.className = "invisible";
570                                         feedr.className = feedr.className.replace("Unread", "");
571                                 }                       
572                         }
573                 }
574
575                 hideOrShowFeeds(getFeedsContext().document, 
576                         getInitParam("hide_read_feeds") == 1);
577
578                 var feeds_stored = getMainContext().number_of_feeds;
579
580                 debug("Feed counters, C: " + feeds_found + ", S:" + feeds_stored);
581
582                 if (feeds_stored != feeds_found) {
583                         getMainContext().number_of_feeds = feeds_found;
584
585                         if (feeds_stored != 0) {
586                                 debug("Subscribed feed number changed, refreshing feedlist");
587                                 updateFeedList();
588                         }
589                 }
590
591         } catch (e) {
592                 exception_error("parse_counters", e);
593         }
594 }
595
596 function all_counters_callback() {
597         if (xmlhttp_rpc.readyState == 4) {
598                 try {
599                         if (!xmlhttp_rpc.responseXML || !xmlhttp_rpc.responseXML.firstChild) {
600                                 debug("[all_counters_callback] backend did not return valid XML");
601                                 return;
602                         }
603
604                         debug("in all_counters_callback : " + xmlhttp_rpc.responseXML);
605
606                         var reply = xmlhttp_rpc.responseXML.firstChild;
607
608                         var counters = reply.firstChild;
609
610                         parse_counters(counters);
611
612                         var runtime = counters.nextSibling;
613
614                         if (runtime) {
615                                 getMainContext().parse_runtime_info(runtime);
616                         }
617
618                         if (getInitParam("feeds_sort_by_unread") == 1) {
619                                 resort_feedlist();              
620                         }       
621
622                         hideOrShowFeeds(document, getInitParam("hide_read_feeds") == 1);
623
624                 } catch (e) {
625                         exception_error("all_counters_callback", e);
626                 }
627         }
628 }
629
630 function get_feed_entry_unread(doc, elem) {
631
632         var id = elem.id.replace("FEEDR-", "");
633
634         if (id <= 0) {
635                 return -1;
636         }
637
638         try {
639                 return parseInt(doc.getElementById("FEEDU-" + id).innerHTML);   
640         } catch (e) {
641                 return -1;
642         }
643 }
644
645 function resort_category(doc, node) {
646         debug("resort_category: " + node);
647
648         if (node.hasChildNodes() && node.firstChild.nextSibling != false) {  
649                 for (i = 0; i < node.childNodes.length; i++) {
650                         if (node.childNodes[i].nodeName != "LI") { continue; }
651
652                         if (get_feed_entry_unread(doc, node.childNodes[i]) < 0) {
653                                 continue;
654                         }
655
656                         for (j = i+1; j < node.childNodes.length; j++) {                        
657                                 if (node.childNodes[j].nodeName != "LI") { continue; }  
658
659                                 var tmp_val = get_feed_entry_unread(doc, node.childNodes[i]);
660                                 var cur_val = get_feed_entry_unread(doc, node.childNodes[j]);
661
662                                 if (cur_val > tmp_val) {
663                                         tempnode_i = node.childNodes[i].cloneNode(true);
664                                         tempnode_j = node.childNodes[j].cloneNode(true);
665                                         node.replaceChild(tempnode_i, node.childNodes[j]);
666                                         node.replaceChild(tempnode_j, node.childNodes[i]);
667                                 }
668                         }
669
670                 }
671         }
672
673 }
674
675 function resort_feedlist() {
676         debug("resort_feedlist");
677
678         var fd = getFeedsContext().document;
679
680         if (fd.getElementById("feedCatHolder")) {
681
682                 var feeds = fd.getElementById("feedList");
683                 var child = feeds.firstChild;
684
685                 while (child) {
686
687                         if (child.id == "feedCatHolder") {
688                                 resort_category(fd, child.firstChild);
689                         }
690         
691                         child = child.nextSibling;
692                 }
693
694         } else {
695                 resort_category(fd, fd.getElementById("feedList"));
696         }
697 }
698
699 function update_all_counters(feed) {
700         if (xmlhttp_ready(xmlhttp_rpc)) {
701                 var query = "backend.php?op=rpc&subop=getAllCounters";
702
703                 if (feed > 0) {
704                         query = query + "&aid=" + feed;
705                 }
706
707                 xmlhttp_rpc.open("GET", query, true);
708                 xmlhttp_rpc.onreadystatechange=all_counters_callback;
709                 xmlhttp_rpc.send(null);
710         }
711 }
712
713 function popupHelp(tid) {
714         var w = window.open("backend.php?op=help&tid=" + tid,
715                 "Popup Help", 
716                 "menubar=no,location=no,resizable=yes,scrollbars=yes,status=no");
717 }
718
719 /** * @(#)isNumeric.js * * Copyright (c) 2000 by Sundar Dorai-Raj
720   * * @author Sundar Dorai-Raj
721   * * Email: sdoraira@vt.edu
722   * * This program is free software; you can redistribute it and/or
723   * * modify it under the terms of the GNU General Public License 
724   * * as published by the Free Software Foundation; either version 2 
725   * * of the License, or (at your option) any later version, 
726   * * provided that any use properly credits the author. 
727   * * This program is distributed in the hope that it will be useful,
728   * * but WITHOUT ANY WARRANTY; without even the implied warranty of
729   * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
730   * * GNU General Public License for more details at http://www.gnu.org * * */
731
732   var numbers=".0123456789";
733   function isNumeric(x) {
734     // is x a String or a character?
735     if(x.length>1) {
736       // remove negative sign
737       x=Math.abs(x)+"";
738       for(j=0;j<x.length;j++) {
739         // call isNumeric recursively for each character
740         number=isNumeric(x.substring(j,j+1));
741         if(!number) return number;
742       }
743       return number;
744     }
745     else {
746       // if x is number return true
747       if(numbers.indexOf(x)>=0) return true;
748       return false;
749     }
750   }
751
752
753 function hideOrShowFeeds(doc, hide) {
754
755         debug("hideOrShowFeeds: " + doc + ", " + hide);
756
757         var fd = getFeedsContext().document;
758
759         var list = fd.getElementById("feedList");
760
761         if (fd.getElementById("feedCatHolder")) {
762
763                 var feeds = fd.getElementById("feedList");
764                 var child = feeds.firstChild;
765
766                 while (child) {
767
768                         if (child.id == "feedCatHolder") {
769                                 hideOrShowFeedsCategory(fd, child.firstChild, hide, child.previousSibling);
770                         }
771         
772                         child = child.nextSibling;
773                 }
774
775         } else {
776                 hideOrShowFeedsCategory(fd, fd.getElementById("feedList"), hide);
777         }
778 }
779
780 function hideOrShowFeedsCategory(doc, node, hide, cat_node) {
781
782 //      debug("hideOrShowFeedsCategory: " + node + " (" + hide + ")");
783
784         var cat_unread = 0;
785
786         if (node.hasChildNodes() && node.firstChild.nextSibling != false) {  
787                 for (i = 0; i < node.childNodes.length; i++) {
788                         if (node.childNodes[i].nodeName != "LI") { continue; }
789
790                         if (node.childNodes[i].style != undefined) {
791
792                                 var has_unread = (node.childNodes[i].className != "feed");
793         
794         //                      debug(node.childNodes[i].id + " --> " + has_unread);
795         
796                                 if (hide && !has_unread) {
797                                         node.childNodes[i].style.display = "none";
798                                 }
799         
800                                 if (!hide) {
801                                         node.childNodes[i].style.display = "list-item";
802                                 }
803         
804                                 if (has_unread) {
805                                         node.childNodes[i].style.display = "list-item";
806                                         cat_unread++;
807                                 }
808                         }
809                 }
810         }       
811
812         if (cat_unread == 0) {
813                 if (cat_node.style == undefined) {
814                         debug("ERROR: supplied cat_node " + cat_node + 
815                                 " has no styles. WTF?");
816                         return;
817                 }
818                 if (hide) {
819                         cat_node.style.display = "none";
820                 } else {
821                         cat_node.style.display = "list-item";
822                 }
823         } else {
824                 try {
825                         cat_node.style.display = "list-item";
826                 } catch (e) {
827                         debug(e);
828                 }
829         }
830
831 //      debug("unread for category: " + cat_unread);
832 }
833
834 function selectTableRow(r, do_select) {
835         r.className = r.className.replace("Selected", "");
836         
837         if (do_select) {
838                 r.className = r.className + "Selected";
839         }
840 }
841
842 function selectTableRowById(elem_id, check_id, do_select) {
843
844         try {
845
846                 var row = document.getElementById(elem_id);
847
848                 if (row) {
849                         selectTableRow(row, do_select);
850                 }               
851
852                 var check = document.getElementById(check_id);
853
854                 if (check) {
855                         check.checked = do_select;
856                 }
857         } catch (e) {
858                 exception_error("selectTableRowById", e);
859         }
860 }
861
862 function selectTableRowsByIdPrefix(content_id, prefix, check_prefix, do_select, 
863         classcheck, reset_others) {
864
865         var content = document.getElementById(content_id);
866
867         if (!content) {
868                 alert("[selectTableRows] Element " + content_id + " not found.");
869                 return;
870         }
871
872         for (i = 0; i < content.rows.length; i++) {
873                 if (!classcheck || content.rows[i].className.match(classcheck)) {
874         
875                         if (content.rows[i].id.match(prefix)) {
876                                 selectTableRow(content.rows[i], do_select);
877                         
878                                 var row_id = content.rows[i].id.replace(prefix, "");
879                                 var check = document.getElementById(check_prefix + row_id);
880
881                                 if (check) {
882                                         check.checked = do_select;
883                                 }
884                         } else if (reset_others) {
885                                 selectTableRow(content.rows[i], false);
886
887                                 var row_id = content.rows[i].id.replace(prefix, "");
888                                 var check = document.getElementById(check_prefix + row_id);
889
890                                 if (check) {
891                                         check.checked = false;
892                                 }
893
894                         }
895                 } else if (reset_others) {
896                         selectTableRow(content.rows[i], false);
897
898                         var row_id = content.rows[i].id.replace(prefix, "");
899                         var check = document.getElementById(check_prefix + row_id);
900
901                         if (check) {
902                                 check.checked = false;
903                         }
904
905                 }
906         }
907 }
908
909 function getSelectedTableRowIds(content_id, prefix) {
910
911         var content = document.getElementById(content_id);
912
913         if (!content) {
914                 alert("[getSelectedTableRowIds] Element " + content_id + " not found.");
915                 return;
916         }
917
918         var sel_rows = new Array();
919
920         for (i = 0; i < content.rows.length; i++) {
921                 if (content.rows[i].id.match(prefix) && 
922                                 content.rows[i].className.match("Selected")) {
923                                 
924                         var row_id = content.rows[i].id.replace(prefix + "-", "");
925                         sel_rows.push(row_id);  
926                 }
927         }
928
929         return sel_rows;
930
931 }
932
933 function toggleSelectRowById(sender, id) {
934         var row = document.getElementById(id);
935
936         if (sender.checked) {
937                 if (!row.className.match("Selected")) {
938                         row.className = row.className + "Selected";
939                 }
940         } else {
941                 if (row.className.match("Selected")) {
942                         row.className = row.className.replace("Selected", "");
943                 }
944         }
945 }
946
947 function toggleSelectListRow(sender) {
948         var parent_row = sender.parentNode;
949
950         if (sender.checked) {
951                 if (!parent_row.className.match("Selected")) {
952                         parent_row.className = parent_row.className + "Selected";
953                 }
954         } else {
955                 if (parent_row.className.match("Selected")) {
956                         parent_row.className = parent_row.className.replace("Selected", "");
957                 }
958         }
959 }
960
961
962 function toggleSelectRow(sender) {
963         var parent_row = sender.parentNode.parentNode;
964
965         if (sender.checked) {
966                 if (!parent_row.className.match("Selected")) {
967                         parent_row.className = parent_row.className + "Selected";
968                 }
969         } else {
970                 if (parent_row.className.match("Selected")) {
971                         parent_row.className = parent_row.className.replace("Selected", "");
972                 }
973         }
974 }
975
976 function openExternalUrl(url) {
977         var w = window.open(url);
978 }
979
980 function getRelativeFeedId(list, id, direction, unread_only) {  
981         if (!id) {
982                 if (direction == "next") {
983                         for (i = 0; i < list.childNodes.length; i++) {
984                                 var child = list.childNodes[i];
985                                 if (child.id && child.id == "feedCatHolder") {
986                                         if (child.lastChild) {
987                                                 var cr = getRelativeFeedId(child.firstChild, id, direction);
988                                                 if (cr) return cr;                                      
989                                         }
990                                 } else if (child.id && child.id.match("FEEDR-")) {
991                                         return child.id.replace('FEEDR-', '');
992                                 }                               
993                         }
994                 }
995
996                 // FIXME select last feed doesn't work when only unread feeds are visible
997
998                 if (direction == "prev") {
999                         for (i = list.childNodes.length-1; i >= 0; i--) {
1000                                 var child = list.childNodes[i];                         
1001                                 if (child.id == "feedCatHolder") {
1002                                         if (child.firstChild) {
1003                                                 var cr = getRelativeFeedId(child.firstChild, id, direction);
1004                                                 if (cr) return cr;                                      
1005                                         }
1006                                 } else if (child.id.match("FEEDR-")) {
1007                                 
1008                                         if (getInitParam("hide_read_feeds") == 1) {
1009                                                 if (child.className != "feed") {
1010                                                         alert(child.className);
1011                                                         return child.id.replace('FEEDR-', '');                                          
1012                                                 }                                                       
1013                                         } else {
1014                                                         return child.id.replace('FEEDR-', '');                                          
1015                                         }                               
1016                                 }                               
1017                         }
1018                 }
1019         } else {
1020         
1021                 var feed = list.ownerDocument.getElementById("FEEDR-" + getActiveFeedId());
1022                 
1023                 if (getInitParam("hide_read_feeds") == 1) {
1024                         unread_only = true;
1025                 }
1026
1027                 if (direction == "next") {
1028
1029                         var e = feed;
1030
1031                         while (e) {
1032
1033                                 if (e.nextSibling) {
1034                                 
1035                                         e = e.nextSibling;
1036                                         
1037                                 } else if (e.parentNode.parentNode.nextSibling) {
1038
1039                                         var this_cat = e.parentNode.parentNode;
1040
1041                                         e = false;
1042
1043                                         if (this_cat && this_cat.nextSibling) {
1044                                                 while (!e && this_cat.nextSibling) {
1045                                                         this_cat = this_cat.nextSibling;
1046                                                         if (this_cat.id == "feedCatHolder") {
1047                                                                 e = this_cat.firstChild.firstChild;
1048                                                         }
1049                                                 }
1050                                         }
1051
1052                                 } else {
1053                                         e = false;
1054                            }
1055
1056                                 if (e) {
1057                                         if (!unread_only || (unread_only && e.className != "feed" && 
1058                                                         e.className != "error"))        {
1059                                                 return e.id.replace("FEEDR-", "");
1060                                         }
1061                                 }
1062                         }
1063                         
1064                 } else if (direction == "prev") {
1065
1066                         var e = feed;
1067
1068                         while (e) {
1069
1070                                 if (e.previousSibling) {
1071                                 
1072                                         e = e.previousSibling;
1073                                         
1074                                 } else if (e.parentNode.parentNode.previousSibling) {
1075
1076                                         var this_cat = e.parentNode.parentNode;
1077
1078                                         e = false;
1079
1080                                         if (this_cat && this_cat.previousSibling) {
1081                                                 while (!e && this_cat.previousSibling) {
1082                                                         this_cat = this_cat.previousSibling;
1083                                                         if (this_cat.id == "feedCatHolder") {
1084                                                                 e = this_cat.firstChild.lastChild;
1085                                                         }
1086                                                 }
1087                                         }
1088
1089                                 } else {
1090                                         e = false;
1091                            }
1092
1093                                 if (e) {
1094                                         if (!unread_only || (unread_only && e.className != "feed" && 
1095                                                         e.className != "error"))        {
1096                                                 return e.id.replace("FEEDR-", "");
1097                                         }
1098                                 }
1099                         }
1100                 }
1101         }
1102 }
1103
1104 function showBlockElement(id) {
1105         var elem = document.getElementById(id);
1106
1107         if (elem) {
1108                 elem.style.display = "block";
1109         } else {
1110                 alert("[showBlockElement] can't find element with id " + id);
1111         } 
1112 }
1113
1114 function hideParentElement(e) {
1115         e.parentNode.style.display = "none";
1116 }
1117
1118 function dropboxSelect(e, v) {
1119         for (i = 0; i < e.length; i++) {
1120                 if (e[i].value == v) {
1121                         e.selectedIndex = i;
1122                         break;
1123                 }
1124         }
1125 }
1126
1127 // originally stolen from http://www.11tmr.com/11tmr.nsf/d6plinks/MWHE-695L9Z
1128 // bugfixed just a little bit :-)
1129 function getURLParam(strParamName){
1130   var strReturn = "";
1131   var strHref = window.location.href;
1132
1133   if (strHref.indexOf("#") == strHref.length-1) {
1134                 strHref = strHref.substring(0, strHref.length-1);
1135   }
1136
1137   if ( strHref.indexOf("?") > -1 ){
1138     var strQueryString = strHref.substr(strHref.indexOf("?"));
1139     var aQueryString = strQueryString.split("&");
1140     for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
1141       if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
1142         var aParam = aQueryString[iParam].split("=");
1143         strReturn = aParam[1];
1144         break;
1145       }
1146     }
1147   }
1148   return strReturn;
1149
1150
1151 function leading_zero(p) {
1152         var s = String(p);
1153         if (s.length == 1) s = "0" + s;
1154         return s;
1155 }
1156
1157 function closeInfoBox(cleanup) {
1158         var box = document.getElementById('infoBox');
1159         var shadow = document.getElementById('infoBoxShadow');
1160
1161         if (shadow) {
1162                 shadow.style.display = "none";
1163         } else if (box) {
1164                 box.style.display = "none";
1165         }
1166
1167         if (cleanup) box.innerHTML = "&nbsp;";
1168
1169         enableHotkeys();
1170
1171         return false;
1172 }
1173
1174
1175 function displayDlg(id, param) {
1176
1177         if (!xmlhttp_ready(xmlhttp)) {
1178                 printLockingError();
1179                 return
1180         }
1181
1182         notify("");
1183
1184         xmlhttp.open("GET", "backend.php?op=dlg&id=" +
1185                 param_escape(id) + "&param=" + param_escape(param), true);
1186         xmlhttp.onreadystatechange=infobox_callback;
1187         xmlhttp.send(null);
1188
1189         disableHotkeys();
1190
1191         return false;
1192 }
1193
1194 function infobox_submit_callback() {
1195         if (xmlhttp.readyState == 4) {
1196                 closeInfoBox();
1197
1198                 // called from prefs, reload tab
1199                 if (active_tab) {
1200                         selectTab(active_tab, false);
1201                 }
1202
1203                 notify(xmlhttp.responseText);
1204
1205         } 
1206 }
1207
1208 function infobox_callback() {
1209         if (xmlhttp.readyState == 4) {
1210                 var box = document.getElementById('infoBox');
1211                 var shadow = document.getElementById('infoBoxShadow');
1212                 if (box) {                      
1213                         box.innerHTML=xmlhttp.responseText;                     
1214                         if (shadow) {
1215                                 shadow.style.display = "block";
1216                         } else {
1217                                 box.style.display = "block";                            
1218                         }
1219                 }
1220         }
1221 }
1222
1223 function qaddFilter() {
1224
1225         if (!xmlhttp_ready(xmlhttp)) {
1226                 printLockingError();
1227                 return
1228         }
1229
1230         var query = Form.serialize("filter_add_form");
1231
1232         xmlhttp.open("GET", "backend.php?" + query, true);
1233         xmlhttp.onreadystatechange=infobox_submit_callback;
1234         xmlhttp.send(null);
1235
1236         return true;
1237 }
1238
1239 function toggleSubmitNotEmpty(e, submit_id) {
1240         try {
1241                 document.getElementById(submit_id).disabled = (e.value == "")
1242         } catch (e) {
1243                 exception_error("toggleSubmitNotEmpty", e);
1244         }
1245 }
1246
1247 function isValidURL(s) {
1248         return s.match("http://") != null || s.match("https://") != null;
1249 }
1250
1251 function qafAdd() {
1252
1253         if (!xmlhttp_ready(xmlhttp)) {
1254                 printLockingError();
1255                 return
1256         }
1257
1258         notify("Adding feed...");
1259
1260         closeInfoBox();
1261
1262         var feeds_doc = getFeedsContext().document;
1263
1264 //      feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
1265         
1266         var query = Form.serialize("feed_add_form");
1267         
1268         xmlhttp.open("GET", "backend.php?" + query, true);
1269         xmlhttp.onreadystatechange=dlg_frefresh_callback;
1270         xmlhttp.send(null);
1271 }
1272
1273 function filterCR(e)
1274 {
1275      var key;
1276
1277      if(window.event)
1278           key = window.event.keyCode;     //IE
1279      else
1280           key = e.which;     //firefox
1281
1282      if(key == 13)
1283           return false;
1284      else
1285           return true;
1286 }
1287
1288 function getMainContext() {
1289         return this.window;
1290 }
1291
1292 function getFeedsContext() {
1293         return this.window;
1294 }
1295
1296 function getContentContext() {
1297         return this.window;
1298 }
1299
1300
1301 function getHeadlinesContext() {
1302         return this.window;
1303 }
1304
1305 var debug_last_class = "even";
1306
1307 function debug(msg) {
1308         var ctx = getMainContext();
1309
1310         if (ctx.debug_last_class == "even") {
1311                 ctx.debug_last_class = "odd";
1312         } else {
1313                 ctx.debug_last_class = "even";
1314         }
1315
1316         var c = ctx.document.getElementById('debug_output');
1317         if (c && c.style.display == "block") {
1318                 while (c.lastChild != 'undefined' && c.childNodes.length > 100) {
1319                         c.removeChild(c.lastChild);
1320                 }
1321         
1322                 var d = new Date();
1323                 var ts = leading_zero(d.getHours()) + ":" + leading_zero(d.getMinutes()) +
1324                         ":" + leading_zero(d.getSeconds());
1325                 c.innerHTML = "<li class=\"" + ctx.debug_last_class + "\"><span class=\"debugTS\">[" + ts + "]</span> " + 
1326                         msg + "</li>" + c.innerHTML;
1327         }
1328 }
1329
1330 function getInitParam(key) {
1331         return getMainContext().init_params[key];
1332 }
1333
1334 function storeInitParam(key, value, is_client) {
1335         try {
1336                 if (!is_client) {
1337                         if (getMainContext().init_params[key] != value) {
1338                                 debug("storeInitParam: " + key + " => " + value);
1339                                 //new Ajax.Request("backend.php?op=rpc&subop=storeParam&key=" + 
1340                                 //      param_escape(key) + "&value=" + param_escape(value));   
1341                                 var f = getMainContext().document.getElementById("backReqBox");
1342                                 f.src = "backend.php?op=rpc&subop=storeParam&key=" + 
1343                                         param_escape(key) + "&value=" + param_escape(value);
1344                         }
1345                 }
1346                 getMainContext().init_params[key] = value;
1347         } catch (e) {
1348                 exception_error("storeInitParam", e);
1349         }
1350 }
1351
1352 /*
1353 function storeInitParams(params, is_client) {
1354         try {
1355                 var s = "";
1356
1357                 for (k in params) {
1358                         if (getMainContext().init_params[k] != params[k]) {
1359                                 s += k + "=" + params[k] + ";";
1360                                 getMainContext().init_params[k] = params[k];
1361                         }
1362                 } 
1363
1364                 debug("storeInitParams: " + s);
1365         
1366                 if (!is_client) {
1367                         new Ajax.Request("backend.php?op=rpc&subop=storeParams&str=" + s);
1368                 }
1369         } catch (e) {
1370                 exception_error("storeInitParams", e);
1371         }
1372 }*/
1373
1374 function fatalError(code, message) {
1375         try {   
1376                 var fe = document.getElementById("fatal_error");
1377                 var fc = document.getElementById("fatal_error_msg");
1378
1379                 fc.innerHTML = "Code " + code + ": " + message;
1380
1381                 fe.style.display = "block";
1382
1383         } catch (e) {
1384                 exception_error("fatalError", e);
1385         }
1386 }
1387
1388 function getFeedName(id, is_cat) {      
1389         var d = getFeedsContext().document;
1390
1391         var e;
1392
1393         if (is_cat) {
1394                 e = d.getElementById("FCATN-" + id);
1395         } else {
1396                 e = d.getElementById("FEEDN-" + id);
1397         }
1398         if (e) {
1399                 return e.innerHTML.stripTags();
1400         } else {
1401                 return null;
1402         }
1403 }
1404
1405 function viewContentUrl(url) {
1406         getContentContext().location = url;
1407 }