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