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