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