]> git.wh0rd.org Git - tt-rss.git/blob - prefs.js
UI improvements
[tt-rss.git] / prefs.js
1 var xmlhttp = false;
2
3 var active_feed_cat = false;
4 var active_label = false;
5 var active_tab = false;
6 var feed_to_expand = false;
7
8 var piggie_top = -400;
9 var piggie_fwd = true;
10
11 var xmlhttp = Ajax.getTransport();
12
13 var init_params = new Array();
14
15 var caller_subop = false;
16
17 function expand_feed_callback() {
18         if (xmlhttp.readyState == 4) {
19                 try {   
20                         var container = document.getElementById("BRDET-" + feed_to_expand);     
21                         container.innerHTML=xmlhttp.responseText;
22                         container.style.display = "block";
23 //                      p_notify("");
24                 } catch (e) {
25                         exception_error("expand_feed_callback", e);
26                 }
27         }
28 }
29
30 function feedlist_callback() {
31         if (xmlhttp.readyState == 4) {
32                 try {   
33                         var container = document.getElementById('prefContent'); 
34                         container.innerHTML=xmlhttp.responseText;
35                         selectTab("feedConfig", true);
36
37                         if (caller_subop) {
38                                 var tuple = caller_subop.split(":");
39                                 if (tuple[0] == 'editFeed') {
40                                         window.setTimeout('editFeed('+tuple[1]+')', 100);
41                                 }                               
42
43                                 caller_subop = false;
44                         }
45
46                         notify("");
47                 } catch (e) {
48                         exception_error("feedlist_callback", e);
49                 }
50         }
51 }
52
53 function filterlist_callback() {
54         var container = document.getElementById('prefContent');
55         if (xmlhttp.readyState == 4) {
56                 container.innerHTML=xmlhttp.responseText;
57                 notify("");
58         }
59 }
60
61 function labellist_callback() {
62         var container = document.getElementById('prefContent');
63         if (xmlhttp.readyState == 4) {
64                 closeInfoBox();
65                 container.innerHTML=xmlhttp.responseText;
66                 if (active_label) {
67                         var row = document.getElementById("LILRR-" + active_label);
68                         if (row) {
69                                 if (!row.className.match("Selected")) {
70                                         row.className = row.className + "Selected";
71                                 }               
72                         }
73                         var checkbox = document.getElementById("LICHK-" + active_label);
74                         
75                         if (checkbox) {
76                                 checkbox.checked = true;
77                         }
78                 }
79                 notify("");
80         }
81 }
82
83 function labeltest_callback() {
84         var container = document.getElementById('label_test_result');
85         if (xmlhttp.readyState == 4) {
86                 container.innerHTML=xmlhttp.responseText;
87                 notify("");
88         }
89 }
90
91 function feed_browser_callback() {
92         var container = document.getElementById('prefContent');
93         if (xmlhttp.readyState == 4) {
94                 container.innerHTML=xmlhttp.responseText;
95                 notify("");
96         }
97 }
98
99 function userlist_callback() {
100         var container = document.getElementById('prefContent');
101         if (xmlhttp.readyState == 4) {
102                 container.innerHTML=xmlhttp.responseText;
103                 notify("");
104         }
105 }
106
107 function prefslist_callback() {
108         var container = document.getElementById('prefContent');
109         if (xmlhttp.readyState == 4) {
110
111                 container.innerHTML=xmlhttp.responseText;
112                 
113                 notify("");
114         }
115 }
116
117 function gethelp_callback() {
118         var container = document.getElementById('prefHelpBox');
119         if (xmlhttp.readyState == 4) {
120
121                 container.innerHTML = xmlhttp.responseText;
122                 container.style.display = "block";
123
124         }
125 }
126
127
128 function notify_callback() {
129         if (xmlhttp.readyState == 4) {
130                 notify_info(xmlhttp.responseText);
131         } 
132 }
133
134 function updateFeedList(sort_key) {
135
136         if (!xmlhttp_ready(xmlhttp)) {
137                 printLockingError();
138                 return
139         }
140
141 //      document.getElementById("prefContent").innerHTML = "Loading feeds, please wait...";
142
143 //      p_notify("Loading, please wait...");
144
145         var feed_search = document.getElementById("feed_search");
146         var search = "";
147         if (feed_search) { search = feed_search.value; }
148
149         xmlhttp.open("GET", "backend.php?op=pref-feeds" +
150                 "&sort=" + param_escape(sort_key) + 
151                 "&search=" + param_escape(search), true);
152         xmlhttp.onreadystatechange=feedlist_callback;
153         xmlhttp.send(null);
154
155 }
156
157 function updateUsersList(sort_key) {
158
159         if (!xmlhttp_ready(xmlhttp)) {
160                 printLockingError();
161                 return
162         }
163
164 //      document.getElementById("prefContent").innerHTML = "Loading feeds, please wait...";
165
166 //      p_notify("Loading, please wait...");
167
168         xmlhttp.open("GET", "backend.php?op=pref-users&sort="
169                 + param_escape(sort_key), true);
170         xmlhttp.onreadystatechange=userlist_callback;
171         xmlhttp.send(null);
172
173 }
174
175 function addLabel() {
176
177         if (!xmlhttp_ready(xmlhttp)) {
178                 printLockingError();
179                 return
180         }
181
182         var form = document.forms['label_edit_form'];
183
184         var sql_exp = form.sql_exp.value;
185         var description = form.description.value;
186
187         if (sql_exp == "") {
188                 alert("Can't create label: missing SQL expression.");
189                 return false;
190         }
191
192         if (description == "") {
193                 alert("Can't create label: missing caption.");
194                 return false;
195         }
196
197         var query = Form.serialize("label_edit_form");
198
199         xmlhttp.open("GET", "backend.php?op=pref-labels&subop=add&" + query, true);                     
200         xmlhttp.onreadystatechange=infobox_submit_callback;
201         xmlhttp.send(null);
202 }
203
204 function addFeed() {
205
206         if (!xmlhttp_ready(xmlhttp)) {
207                 printLockingError();
208                 return
209         }
210
211         var link = document.getElementById("fadd_link");
212
213         if (link.value.length == 0) {
214                 alert("Error: No feed URL given.");
215         } else if (!isValidURL(link.value)) {
216                 alert("Error: Invalid feed URL.");
217         } else {
218                 notify_progress("Adding feed...");
219
220                 xmlhttp.open("GET", "backend.php?op=pref-feeds&subop=add&from=tt-rss&feed_url=" +
221                         param_escape(link.value), true);
222                 xmlhttp.onreadystatechange=feedlist_callback;
223                 xmlhttp.send(null);
224
225                 link.value = "";
226
227         }
228
229 }
230
231 function addFeedCat() {
232
233         if (!xmlhttp_ready(xmlhttp)) {
234                 printLockingError();
235                 return
236         }
237
238         var cat = document.getElementById("fadd_cat");
239
240         if (cat.value.length == 0) {
241                 alert("Can't add category: no name specified.");
242         } else {
243                 notify_progress("Adding feed category...");
244
245                 xmlhttp.open("GET", "backend.php?op=pref-feeds&subop=editCats&action=add&cat=" +
246                         param_escape(cat.value), true);
247                 xmlhttp.onreadystatechange=infobox_callback;
248                 xmlhttp.send(null);
249
250                 link.value = "";
251
252         }
253
254 }
255 function addUser() {
256
257         if (!xmlhttp_ready(xmlhttp)) {
258                 printLockingError();
259                 return
260         }
261
262         var sqlexp = document.getElementById("uadd_box");
263
264         if (sqlexp.value.length == 0) {
265                 alert("Can't add user: no login specified.");
266         } else {
267                 notify_progress("Adding user...");
268
269                 xmlhttp.open("GET", "backend.php?op=pref-users&subop=add&login=" +
270                         param_escape(sqlexp.value), true);                      
271                         
272                 xmlhttp.onreadystatechange=userlist_callback;
273                 xmlhttp.send(null);
274
275                 sqlexp.value = "";
276         }
277
278 }
279
280 function editLabel(id) {
281
282         if (!xmlhttp_ready(xmlhttp)) {
283                 printLockingError();
284                 return
285         }
286
287         document.getElementById("label_create_btn").disabled = true;
288
289         active_label = id;
290
291         selectTableRowsByIdPrefix('prefLabelList', 'LILRR-', 'LICHK-', false);
292         selectTableRowById('LILRR-'+id, 'LICHK-'+id, true);
293
294         xmlhttp.open("GET", "backend.php?op=pref-labels&subop=edit&id=" +
295                 param_escape(id), true);
296         xmlhttp.onreadystatechange=infobox_callback;
297         xmlhttp.send(null);
298
299 }
300
301 function editUser(id) {
302
303         if (!xmlhttp_ready(xmlhttp)) {
304                 printLockingError();
305                 return
306         }
307
308         selectTableRowsByIdPrefix('prefUserList', 'UMRR-', 'UMCHK-', false);
309         selectTableRowById('UMRR-'+id, 'UMCHK-'+id, true);
310
311         xmlhttp.open("GET", "backend.php?op=pref-users&subop=edit&id=" +
312                 param_escape(id), true);
313         xmlhttp.onreadystatechange=infobox_callback;
314         xmlhttp.send(null);
315
316 }
317
318 function editFilter(id) {
319
320         if (!xmlhttp_ready(xmlhttp)) {
321                 printLockingError();
322                 return
323         }
324
325         document.getElementById("create_filter_btn").disabled = true;
326
327         selectTableRowsByIdPrefix('prefFilterList', 'FILRR-', 'FICHK-', false);
328         selectTableRowById('FILRR-'+id, 'FICHK-'+id, true);
329
330         xmlhttp.open("GET", "backend.php?op=pref-filters&subop=edit&id=" + param_escape(id), true);
331         xmlhttp.onreadystatechange=infobox_callback;
332         xmlhttp.send(null);
333 }
334
335 function editFeed(feed) {
336
337 //      notify("Editing feed...");
338
339         if (!xmlhttp_ready(xmlhttp)) {
340                 printLockingError();
341                 return
342         }
343
344         // clean selection from all rows & select row being edited
345         selectTableRowsByIdPrefix('prefFeedList', 'FEEDR-', 'FRCHK-', false);
346         selectTableRowById('FEEDR-'+feed, 'FRCHK-'+feed, true);
347
348         xmlhttp.open("GET", "backend.php?op=pref-feeds&subop=editfeed&id=" +
349                 param_escape(feed), true);
350
351         xmlhttp.onreadystatechange=infobox_callback;
352         xmlhttp.send(null);
353
354 }
355
356 function editFeedCat(cat) {
357
358         if (!xmlhttp_ready(xmlhttp)) {
359                 printLockingError();
360                 return
361         }
362
363         active_feed_cat = cat;
364
365         xmlhttp.open("GET", "backend.php?op=pref-feeds&subop=editCats&action=edit&id=" +
366                 param_escape(cat), true);
367         xmlhttp.onreadystatechange=infobox_callback;
368         xmlhttp.send(null);
369
370 }
371
372 function getSelectedLabels() {
373         return getSelectedTableRowIds("prefLabelList", "LILRR");
374 }
375
376 function getSelectedUsers() {
377         return getSelectedTableRowIds("prefUserList", "UMRR");
378 }
379
380 function getSelectedFeeds() {
381         return getSelectedTableRowIds("prefFeedList", "FEEDR");
382 }
383
384 function getSelectedFilters() {
385         return getSelectedTableRowIds("prefFilterList", "FILRR");
386 }
387
388 function getSelectedFeedCats() {
389         return getSelectedTableRowIds("prefFeedCatList", "FCATR");
390 }
391
392 function getSelectedFeedsFromBrowser() {
393
394         var list = document.getElementById("browseFeedList");
395         if (!list) list = document.getElementById("browseBigFeedList");
396
397         var selected = new Array();
398         
399         for (i = 0; i < list.childNodes.length; i++) {
400                 var child = list.childNodes[i];
401                 if (child.id && child.id.match("FBROW-")) {
402                         var id = child.id.replace("FBROW-", "");
403                         
404                         var cb = document.getElementById("FBCHK-" + id);
405
406                         if (cb.checked) {
407                                 selected.push(id);
408                         }
409                 }
410         }
411
412         return selected;
413 }
414
415 function removeSelectedLabels() {
416
417         if (!xmlhttp_ready(xmlhttp)) {
418                 printLockingError();
419                 return
420         }
421
422         var sel_rows = getSelectedLabels();
423
424         if (sel_rows.length > 0) {
425
426                 var ok = confirm("Remove selected labels?");
427
428                 if (ok) {
429                         notify_progress("Removing selected labels...");
430         
431                         xmlhttp.open("GET", "backend.php?op=pref-labels&subop=remove&ids="+
432                                 param_escape(sel_rows.toString()), true);
433                         xmlhttp.onreadystatechange=labellist_callback;
434                         xmlhttp.send(null);
435                 }
436         } else {
437                 alert("No labels are selected.");
438         }
439
440         return false;
441 }
442
443 function removeSelectedUsers() {
444
445         if (!xmlhttp_ready(xmlhttp)) {
446                 printLockingError();
447                 return
448         }
449
450         var sel_rows = getSelectedUsers();
451
452         if (sel_rows.length > 0) {
453
454                 var ok = confirm("Remove selected users?");
455
456                 if (ok) {
457                         notify_progress("Removing selected users...");
458         
459                         xmlhttp.open("GET", "backend.php?op=pref-users&subop=remove&ids="+
460                                 param_escape(sel_rows.toString()), true);
461                         xmlhttp.onreadystatechange=userlist_callback;
462                         xmlhttp.send(null);
463                 }
464
465         } else {
466                 alert("No users are selected.");
467         }
468
469         return false;
470 }
471
472 function removeSelectedFilters() {
473
474         if (!xmlhttp_ready(xmlhttp)) {
475                 printLockingError();
476                 return
477         }
478
479         var sel_rows = getSelectedFilters();
480
481         if (sel_rows.length > 0) {
482
483                 var ok = confirm("Remove selected filters?");
484
485                 if (ok) {
486                         notify_progress("Removing selected filters...");
487         
488                         xmlhttp.open("GET", "backend.php?op=pref-filters&subop=remove&ids="+
489                                 param_escape(sel_rows.toString()), true);
490                         xmlhttp.onreadystatechange=filterlist_callback;
491                         xmlhttp.send(null);
492                 }
493         } else {
494                 alert("No filters are selected.");
495         }
496
497         return false;
498 }
499
500
501 function removeSelectedFeeds() {
502
503         if (!xmlhttp_ready(xmlhttp)) {
504                 printLockingError();
505                 return
506         }
507
508         var sel_rows = getSelectedFeeds();
509
510         if (sel_rows.length > 0) {
511
512                 var ok = confirm("Unsubscribe from selected feeds?");
513
514                 if (ok) {
515
516                         notify_progress("Unsubscribing from selected feeds...");
517         
518                         xmlhttp.open("GET", "backend.php?op=pref-feeds&subop=remove&ids="+
519                                 param_escape(sel_rows.toString()), true);
520                         xmlhttp.onreadystatechange=feedlist_callback;
521                         xmlhttp.send(null);
522                 }
523
524         } else {
525
526                 alert("No feeds are selected.");
527
528         }
529         
530         return false;
531 }
532
533 function removeSelectedFeedCats() {
534
535         if (!xmlhttp_ready(xmlhttp)) {
536                 printLockingError();
537                 return
538         }
539
540         var sel_rows = getSelectedFeedCats();
541
542         if (sel_rows.length > 0) {
543
544                 var ok = confirm("Remove selected categories?");
545
546                 if (ok) {
547                         notify_progress("Removing selected categories...");
548         
549                         xmlhttp.open("GET", "backend.php?op=pref-feeds&subop=editCats&action=remove&ids="+
550                                 param_escape(sel_rows.toString()), true);
551                         xmlhttp.onreadystatechange=infobox_callback;
552                         xmlhttp.send(null);
553                 }
554
555         } else {
556
557                 alert("No categories are selected.");
558
559         }
560
561         return false;
562 }
563
564 function feedEditCancel() {
565
566         if (!xmlhttp_ready(xmlhttp)) {
567                 printLockingError();
568                 return
569         }
570
571         closeInfoBox();
572
573         selectPrefRows('feed', false); // cleanup feed selection
574
575         return false;
576 }
577
578 function feedCatEditCancel() {
579
580         if (!xmlhttp_ready(xmlhttp)) {
581                 printLockingError();
582                 return
583         }
584
585         active_feed_cat = false;
586
587 //      notify("Operation cancelled.");
588
589         xmlhttp.open("GET", "backend.php?op=pref-feeds&subop=editCats", true);
590         xmlhttp.onreadystatechange=infobox_callback;
591         xmlhttp.send(null);
592
593         return false;
594 }
595
596 function feedEditSave() {
597
598         try {
599         
600                 if (!xmlhttp_ready(xmlhttp)) {
601                         printLockingError();
602                         return
603                 }
604
605                 // FIXME: add parameter validation
606
607                 var query = Form.serialize("edit_feed_form");
608
609                 notify_progress("Saving feed...");
610
611                 xmlhttp.open("POST", "backend.php", true);
612                 xmlhttp.onreadystatechange=feedlist_callback;
613                 xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
614                 xmlhttp.send(query);
615
616                 closeInfoBox();
617
618                 return false;
619
620         } catch (e) {
621                 exception_error("feedEditSave", e);
622         } 
623 }
624
625 function feedCatEditSave() {
626
627         if (!xmlhttp_ready(xmlhttp)) {
628                 printLockingError();
629                 return
630         }
631
632         notify_progress("Saving category...");
633
634         var query = Form.serialize("feed_cat_edit_form");
635
636         xmlhttp.open("GET", "backend.php?" + query, true);
637         xmlhttp.onreadystatechange=infobox_callback;
638         xmlhttp.send(null);
639
640         active_feed_cat = false;
641
642         return false;
643 }
644
645
646 function labelTest() {
647
648         var container = document.getElementById('label_test_result');
649         container.style.display = "block";
650         container.innerHTML = "<p>Loading, please wait...</p>";
651
652         var form = document.forms['label_edit_form'];
653
654         var sql_exp = form.sql_exp.value;
655         var description = form.description.value;
656
657         xmlhttp.open("GET", "backend.php?op=pref-labels&subop=test&expr=" +
658                 param_escape(sql_exp) + "&descr=" + param_escape(description), true);
659
660         xmlhttp.onreadystatechange=labeltest_callback;
661         xmlhttp.send(null);
662
663         return false;
664 }
665
666 function displayHelpInfobox(topic_id) {
667
668         xmlhttp.open("GET", "backend.php?op=help&tid=" +
669                 param_escape(topic_id) + "&noheaders=1", true);
670
671         xmlhttp.onreadystatechange=infobox_callback;
672         xmlhttp.send(null);
673
674 }
675
676 function labelEditCancel() {
677
678         if (!xmlhttp_ready(xmlhttp)) {
679                 printLockingError();
680                 return
681         }
682
683         document.getElementById("label_create_btn").disabled = false;
684
685         active_label = false;
686
687         selectPrefRows('label', false); // cleanup feed selection
688         closeInfoBox();
689
690         return false;
691 }
692
693 function userEditCancel() {
694
695         if (!xmlhttp_ready(xmlhttp)) {
696                 printLockingError();
697                 return
698         }
699
700         selectPrefRows('user', false); // cleanup feed selection
701         closeInfoBox();
702
703         return false;
704 }
705
706 function filterEditCancel() {
707
708         if (!xmlhttp_ready(xmlhttp)) {
709                 printLockingError();
710                 return
711         }
712
713         document.getElementById("create_filter_btn").disabled = false;
714         
715         selectPrefRows('filter', false); // cleanup feed selection
716         closeInfoBox();
717
718         return false;
719 }
720
721 function labelEditSave() {
722
723         var label = active_label;
724
725         if (!xmlhttp_ready(xmlhttp)) {
726                 printLockingError();
727                 return
728         }
729
730 /*      if (!is_opera()) {
731
732                 var sql_exp = document.forms["label_edit_form"].sql_exp.value;
733                 var description = document.forms["label_edit_form"].description.value;
734         
735                 if (sql_exp.length == 0) {
736                         alert("SQL Expression cannot be blank.");
737                         return false;
738                 }
739         
740                 if (description.length == 0) {
741                         alert("Caption field cannot be blank.");
742                         return false;
743                 }
744         } */
745
746         closeInfoBox();
747
748         notify_progress("Saving label...");
749
750         active_label = false;
751
752         query = Form.serialize("label_edit_form");
753
754         xmlhttp.open("GET", "backend.php?" + query, true);              
755         xmlhttp.onreadystatechange=labellist_callback;
756         xmlhttp.send(null);
757
758         return false;
759 }
760
761 function userEditSave() {
762
763         if (!xmlhttp_ready(xmlhttp)) {
764                 printLockingError();
765                 return
766         }
767
768         var login = document.forms["user_edit_form"].login.value;
769
770         if (login.length == 0) {
771                 alert("Login field cannot be blank.");
772                 return;
773         }
774         
775         notify_progress("Saving user...");
776
777         closeInfoBox();
778
779         var query = Form.serialize("user_edit_form");
780         
781         xmlhttp.open("GET", "backend.php?" + query, true);                      
782         xmlhttp.onreadystatechange=userlist_callback;
783         xmlhttp.send(null);
784
785         return false;
786 }
787
788
789 function filterEditSave() {
790
791         if (!xmlhttp_ready(xmlhttp)) {
792                 printLockingError();
793                 return
794         }
795
796 /*      if (!is_opera()) {
797                 var reg_exp = document.forms["filter_edit_form"].reg_exp.value;
798         
799                 if (reg_exp.length == 0) {
800                         alert("Filter expression field cannot be blank.");
801                         return;
802                 }
803         } */
804
805         notify_progress("Saving filter...");
806
807         var query = Form.serialize("filter_edit_form");
808
809         closeInfoBox();
810
811         document.getElementById("create_filter_btn").disabled = false;
812
813         xmlhttp.open("GET", "backend.php?" + query, true);
814         xmlhttp.onreadystatechange=filterlist_callback;
815         xmlhttp.send(null);
816
817         return false;
818 }
819
820 function editSelectedLabel() {
821         var rows = getSelectedLabels();
822
823         if (rows.length == 0) {
824                 alert("No labels are selected.");
825                 return;
826         }
827
828         if (rows.length > 1) {
829                 alert("Please select only one label.");
830                 return;
831         }
832
833         notify("");
834
835         editLabel(rows[0]);
836
837 }
838
839 function editSelectedUser() {
840         var rows = getSelectedUsers();
841
842         if (rows.length == 0) {
843                 alert("No users are selected.");
844                 return;
845         }
846
847         if (rows.length > 1) {
848                 alert("Please select only one user.");
849                 return;
850         }
851
852         notify("");
853
854         editUser(rows[0]);
855 }
856
857 function resetSelectedUserPass() {
858         var rows = getSelectedUsers();
859
860         if (rows.length == 0) {
861                 alert("No users are selected.");
862                 return;
863         }
864
865         if (rows.length > 1) {
866                 alert("Please select only one user.");
867                 return;
868         }
869
870         var ok = confirm("Reset password of selected user?");
871
872         if (ok) {
873                 notify_progress("Resetting password for selected user...");
874         
875                 var id = rows[0];
876         
877                 xmlhttp.open("GET", "backend.php?op=pref-users&subop=resetPass&id=" +
878                         param_escape(id), true);
879                 xmlhttp.onreadystatechange=userlist_callback;
880                 xmlhttp.send(null);
881         }
882 }
883
884 function selectedUserDetails() {
885
886         if (!xmlhttp_ready(xmlhttp)) {
887                 printLockingError();
888                 return
889         }
890
891         var rows = getSelectedUsers();
892
893         if (rows.length == 0) {
894                 alert("No users are selected.");
895                 return;
896         }
897
898         if (rows.length > 1) {
899                 alert("Please select only one user.");
900                 return;
901         }
902
903         var id = rows[0];
904
905         notify("");
906
907         xmlhttp.open("GET", "backend.php?op=user-details&id=" + id, true);
908         xmlhttp.onreadystatechange=infobox_callback;
909         xmlhttp.send(null);
910
911 }
912
913 function selectedFeedDetails() {
914
915         if (!xmlhttp_ready(xmlhttp)) {
916                 printLockingError();
917                 return
918         }
919
920         var rows = getSelectedFeeds();
921
922         if (rows.length == 0) {
923                 alert("No feeds are selected.");
924                 return;
925         }
926
927         if (rows.length > 1) {
928                 alert("Please select only one feed.");
929                 return;
930         }
931
932 //      var id = rows[0];
933
934         notify("");
935
936         xmlhttp.open("GET", "backend.php?op=feed-details&id=" + 
937                 param_escape(rows.toString()), true);
938         xmlhttp.onreadystatechange=infobox_callback;
939         xmlhttp.send(null);
940
941 }
942
943 function editSelectedFilter() {
944         var rows = getSelectedFilters();
945
946         if (rows.length == 0) {
947                 alert("No filters are selected.");
948                 return;
949         }
950
951         if (rows.length > 1) {
952                 alert("Please select only one filter.");
953                 return;
954         }
955
956         notify("");
957
958         editFilter(rows[0]);
959
960 }
961
962
963 function editSelectedFeed() {
964         var rows = getSelectedFeeds();
965
966         if (rows.length == 0) {
967                 alert("No feeds are selected.");
968                 return;
969         }
970
971         if (rows.length > 1) {
972                 alert("Please select one feed.");
973                 return;
974         }
975
976         notify("");
977
978         editFeed(rows[0]);
979
980 }
981
982 function editSelectedFeedCat() {
983         var rows = getSelectedFeedCats();
984
985         if (rows.length == 0) {
986                 alert("No categories are selected.");
987                 return;
988         }
989
990         if (rows.length > 1) {
991                 alert("Please select only one category.");
992                 return;
993         }
994
995         notify("");
996
997         editFeedCat(rows[0]);
998
999 }
1000
1001 function piggie_callback() {
1002         var piggie = document.getElementById("piggie");
1003
1004         piggie.style.top = piggie_top;
1005         piggie.style.backgroundColor = "white";
1006         piggie.style.borderWidth = "1px";
1007
1008         if (piggie_fwd && piggie_top < 0) {
1009                 setTimeout("piggie_callback()", 50);
1010                 piggie_top = piggie_top + 10;
1011         } else if (piggie_fwd && piggie_top >= 0) {
1012                 piggie_fwd = false;
1013                 setTimeout("piggie_callback()", 50);
1014         } else if (!piggie_fwd && piggie_top > -400) {
1015                 setTimeout("piggie_callback()", 50);
1016                 piggie_top = piggie_top - 10;
1017         } else if (!piggie_fwd && piggie_top <= -400) {
1018                 piggie.style.display = "none";
1019                 piggie_fwd = true;
1020         }
1021 }
1022
1023 var piggie_opacity = 0;
1024
1025 function piggie2_callback() {
1026         var piggie = document.getElementById("piggie");
1027         piggie.style.top = 0;
1028         piggie.style.opacity = piggie_opacity;
1029         piggie.style.backgroundColor = "transparent";
1030         piggie.style.borderWidth = "0px";
1031
1032         if (piggie_fwd && piggie_opacity < 1) {
1033                 setTimeout("piggie2_callback()", 50);
1034                 piggie_opacity = piggie_opacity + 0.03;
1035         } else if (piggie_fwd && piggie_opacity >= 1) {
1036                 piggie_fwd = false;
1037                 setTimeout("piggie2_callback()", 50);
1038         } else if (!piggie_fwd && piggie_opacity > 0) {
1039                 setTimeout("piggie2_callback()", 50);
1040                 piggie_opacity = piggie_opacity - 0.03;
1041         } else if (!piggie_fwd && piggie_opacity <= 0) {
1042                 piggie.style.display = "none";
1043                 piggie_fwd = true;
1044         }
1045 }
1046
1047 function localPiggieFunction(enable) {
1048         if (enable) {
1049                 debug("I LOVEDED IT!");
1050                 var piggie = document.getElementById("piggie");
1051                 piggie.style.display = "block";
1052
1053                 if (navigator.userAgent.match("Gecko") && Math.random(1) > 0.5) {       
1054                         piggie2_callback();
1055                 } else {
1056                         piggie_callback();
1057                 }
1058         }
1059 }
1060
1061 function validateOpmlImport() {
1062         
1063         var opml_file = document.getElementById("opml_file");
1064
1065         if (opml_file.value.length == 0) {
1066                 alert("No OPML file to upload.");
1067                 return false;
1068         } else {
1069                 return true;
1070         }
1071 }
1072
1073 function updateFilterList(sort_key) {
1074
1075         if (!xmlhttp_ready(xmlhttp)) {
1076                 printLockingError();
1077                 return
1078         }
1079
1080 //      document.getElementById("prefContent").innerHTML = "Loading filters, please wait...";
1081
1082 //      p_notify("Loading, please wait...");
1083
1084         xmlhttp.open("GET", "backend.php?op=pref-filters&sort=" + 
1085                 param_escape(sort_key), true);
1086         xmlhttp.onreadystatechange=filterlist_callback;
1087         xmlhttp.send(null);
1088
1089 }
1090
1091 function updateLabelList(sort_key) {
1092
1093         if (!xmlhttp_ready(xmlhttp)) {
1094                 printLockingError();
1095                 return
1096         }
1097
1098 //      p_notify("Loading, please wait...");
1099
1100 //      document.getElementById("prefContent").innerHTML = "Loading labels, please wait...";
1101
1102         xmlhttp.open("GET", "backend.php?op=pref-labels&sort=" + 
1103                 param_escape(sort_key), true);
1104         xmlhttp.onreadystatechange=labellist_callback;
1105         xmlhttp.send(null);
1106 }
1107
1108 function updatePrefsList() {
1109
1110         if (!xmlhttp_ready(xmlhttp)) {
1111                 printLockingError();
1112                 return
1113         }
1114
1115 //      p_notify("Loading, please wait...");
1116
1117         xmlhttp.open("GET", "backend.php?op=pref-prefs", true);
1118         xmlhttp.onreadystatechange=prefslist_callback;
1119         xmlhttp.send(null);
1120
1121 }
1122
1123 function selectTab(id, noupdate, subop) {
1124
1125 //      alert(id);
1126
1127         if (!id) id = active_tab;
1128
1129         try {
1130
1131                 if (!xmlhttp_ready(xmlhttp)) {
1132                         printLockingError();
1133                         return
1134                 }
1135
1136                 try {
1137                         var c = document.getElementById('prefContent'); 
1138                         c.scrollTop = 0;
1139                 } catch (e) { };
1140
1141                 if (!noupdate) {
1142
1143                         debug("selectTab: " + id + "(NU: " + noupdate + ")");
1144         
1145                         notify_progress("Loading, please wait...", true);
1146         
1147                         // close active infobox if needed
1148                         closeInfoBox();
1149         
1150                         // clean up all current selections, just in case
1151                         active_feed_cat = false;
1152                         active_label = false;
1153         
1154                         if (id == "feedConfig") {
1155                                 updateFeedList();
1156                         } else if (id == "filterConfig") {
1157                                 updateFilterList();
1158                         } else if (id == "labelConfig") {
1159                                 updateLabelList();
1160                         } else if (id == "genConfig") {
1161                                 updatePrefsList();
1162                         } else if (id == "userConfig") {
1163                                 updateUsersList();
1164                         } else if (id == "feedBrowser") {
1165                                 updateBigFeedBrowser();
1166                         }
1167                 }
1168         
1169                 var tab = document.getElementById(active_tab + "Tab");
1170         
1171                 if (tab) {
1172                         if (tab.className.match("Selected")) {
1173                                 tab.className = "prefsTab";
1174                         }
1175                 }
1176         
1177                 tab = document.getElementById(id + "Tab");
1178         
1179                 if (tab) {
1180                         if (!tab.className.match("Selected")) {
1181                                 tab.className = tab.className + "Selected";
1182                         }
1183                 }
1184         
1185                 if (active_tab != id) {
1186                         storeInitParam("prefs_active_tab", id);
1187                 }
1188         
1189                 active_tab = id;
1190
1191         } catch (e) {
1192                 exception_error("selectTab", e);
1193         }
1194 }
1195
1196 function backend_sanity_check_callback() {
1197
1198         if (xmlhttp.readyState == 4) {
1199
1200                 try {
1201                 
1202                         if (!xmlhttp.responseXML) {
1203                                 fatalError(3, "[D001, Received reply is not XML]: " + xmlhttp.responseText);
1204                                 return;
1205                         }
1206         
1207                         var reply = xmlhttp.responseXML.firstChild.firstChild;
1208         
1209                         if (!reply) {
1210                                 fatalError(3, "[D002, Invalid RPC reply]: " + xmlhttp.responseText);
1211                                 return;
1212                         }
1213         
1214                         var error_code = reply.getAttribute("error-code");
1215                 
1216                         if (error_code && error_code != 0) {
1217                                 return fatalError(error_code, reply.getAttribute("error-msg"));
1218                         }
1219         
1220                         debug("sanity check ok");
1221
1222                         var params = reply.nextSibling;
1223
1224                         if (params) {
1225                                 debug('reading init-params...');
1226                                 var param = params.firstChild;
1227
1228                                 while (param) {
1229                                         var k = param.getAttribute("key");
1230                                         var v = param.getAttribute("value");
1231                                         debug(k + " => " + v);
1232                                         init_params[k] = v;                                     
1233                                         param = param.nextSibling;
1234                                 }
1235                         }
1236
1237                         init_second_stage();
1238
1239                 } catch (e) {
1240                         exception_error("backend_sanity_check_callback", e);
1241                 }
1242         } 
1243 }
1244
1245 function init_second_stage() {
1246
1247         try {
1248                 active_tab = getInitParam("prefs_active_tab");
1249                 if (!active_tab) active_tab = "genConfig";
1250
1251                 document.onkeydown = pref_hotkey_handler;
1252
1253                 var tab = getURLParam('tab');
1254                 
1255                 caller_subop = getURLParam('subop');
1256
1257                 if (tab) {
1258                         active_tab = tab;
1259                 }
1260
1261                 if (navigator.userAgent.match("Opera")) {       
1262                         setTimeout("selectTab()", 500);
1263                 } else {
1264                         selectTab(active_tab);
1265                 }
1266                 notify("");
1267         } catch (e) {
1268                 exception_error("init_second_stage", e);
1269         }
1270 }
1271
1272 function init() {
1273
1274         try {
1275         
1276                 if (arguments.callee.done) return;
1277                 arguments.callee.done = true;           
1278
1279                 if (getURLParam('debug')) {
1280                         document.getElementById('debug_output').style.display = 'block';
1281                         debug('debug mode activated');
1282                 }
1283
1284                 // IE kludge
1285                 if (!xmlhttp) {
1286                         document.getElementById("prefContent").innerHTML = 
1287                                 "<b>Fatal error:</b> This program needs XmlHttpRequest " + 
1288                                 "to function properly. Your browser doesn't seem to support it.";
1289                         return;
1290                 }
1291
1292                 xmlhttp.open("GET", "backend.php?op=rpc&subop=sanityCheck", true);
1293                 xmlhttp.onreadystatechange=backend_sanity_check_callback;
1294                 xmlhttp.send(null);
1295
1296         } catch (e) {
1297                 exception_error("init", e);
1298         }
1299 }
1300
1301 function categorizeSelectedFeeds() {
1302
1303         if (!xmlhttp_ready(xmlhttp)) {
1304                 printLockingError();
1305                 return
1306         }
1307
1308         var sel_rows = getSelectedFeeds();
1309
1310         var cat_sel = document.getElementById("sfeed_set_fcat");
1311         var cat_id = cat_sel[cat_sel.selectedIndex].value;
1312
1313         if (sel_rows.length > 0) {
1314
1315                 notify_progress("Changing category of selected feeds...");
1316
1317                 xmlhttp.open("GET", "backend.php?op=pref-feeds&subop=categorize&ids="+
1318                         param_escape(sel_rows.toString()) + "&cat_id=" + param_escape(cat_id), true);
1319                 xmlhttp.onreadystatechange=feedlist_callback;
1320                 xmlhttp.send(null);
1321
1322         } else {
1323
1324                 alert("No feeds are selected.");
1325
1326         }
1327
1328 }
1329
1330 function validatePrefsReset() {
1331         return confirm("Reset to defaults?");
1332 }
1333
1334 function browseFeeds(limit) {
1335
1336         xmlhttp.open("GET", "backend.php?op=pref-feeds&subop=browse", true);
1337         xmlhttp.onreadystatechange=infobox_callback;
1338         xmlhttp.send(null);
1339
1340 }
1341
1342 function feedBrowserSubscribe() {
1343         try {
1344
1345                 var selected = getSelectedFeedsFromBrowser();
1346
1347                 if (selected.length > 0) {
1348                         closeInfoBox();
1349                         xmlhttp.open("GET", "backend.php?op=pref-feeds&subop=massSubscribe&ids="+
1350                                 param_escape(selected.toString()), true);
1351                         xmlhttp.onreadystatechange=feedlist_callback;
1352                         xmlhttp.send(null);
1353                 } else {
1354                         alert("No feeds are selected.");
1355                 }
1356
1357         } catch (e) {
1358                 exception_error("feedBrowserSubscribe", e);
1359         }
1360 }
1361
1362 function updateBigFeedBrowser(limit) {
1363
1364         if (!xmlhttp_ready(xmlhttp)) {
1365                 printLockingError();
1366                 return
1367         }
1368
1369 //      p_notify("Loading, please wait...");
1370
1371         var query = "backend.php?op=pref-feed-browser";
1372
1373         var limit_sel = document.getElementById("feedBrowserLimit");
1374
1375         if (limit_sel) {
1376                 var limit = limit_sel[limit_sel.selectedIndex].value;
1377                 query = query + "&limit=" + param_escape(limit);
1378         }
1379
1380         xmlhttp.open("GET", query, true);
1381         xmlhttp.onreadystatechange=feed_browser_callback;
1382         xmlhttp.send(null);
1383 }
1384
1385 function browserToggleExpand(id) {
1386         try {
1387 /*              if (feed_to_expand && feed_to_expand != id) {
1388                         var d = document.getElementById("BRDET-" + feed_to_expand);
1389                         d.style.display = "none";
1390                 } */
1391
1392                 if (!xmlhttp_ready(xmlhttp)) {
1393                         printLockingError();
1394                         return
1395                 }
1396
1397                 var d = document.getElementById("BRDET-" + id);
1398
1399                 if (d.style.display == "block") {               
1400                         d.style.display = "none";
1401                         
1402                 } else {
1403         
1404                         feed_to_expand = id;
1405
1406                         d.style.display = "block";
1407                         d.innerHTML = "Loading, please wait...";
1408
1409                         xmlhttp.open("GET", "backend.php?op=pref-feed-browser&subop=details&id="
1410                                 + param_escape(id), true);
1411                         xmlhttp.onreadystatechange=expand_feed_callback;
1412                         xmlhttp.send(null);
1413                 }
1414
1415         } catch (e) {
1416                 exception_error("browserExpand", e);
1417         }
1418 }
1419
1420 function validateNewPassword(form) {
1421         if (form.OLD_PASSWORD.value == "") {
1422                 alert("Old password cannot be blank");
1423                 return false;
1424         }
1425         if (form.NEW_PASSWORD.value == "") {
1426                 alert("New password cannot be blank");
1427                 return false;
1428         }
1429         return true;
1430 }
1431
1432 function selectPrefRows(kind, select) {
1433
1434         if (kind) {
1435                 var opbarid = false;    
1436                 var nchk = false;
1437                 var nrow = false;
1438                 var lname = false;
1439
1440                 if (kind == "feed") {
1441                         opbarid = "feedOpToolbar";
1442                         nrow = "FEEDR-";
1443                         nchk = "FRCHK-";                        
1444                         lname = "prefFeedList";
1445                 } else if (kind == "fcat") {
1446                         opbarid = "catOpToolbar";
1447                         nrow = "FCATR-";
1448                         nchk = "FCCHK-";
1449                         lname = "prefFeedCatList";
1450                 } else if (kind == "filter") {
1451                         opbarid = "filterOpToolbar";
1452                         nrow = "FILRR-";
1453                         nchk = "FICHK-";
1454                         lname = "prefFilterList";
1455                 } else if (kind == "label") {
1456                         opbarid = "labelOpToolbar";
1457                         nrow = "LILRR-";
1458                         nchk = "LICHK-";
1459                         lname = "prefLabelList";
1460                 } else if (kind == "user") {
1461                         opbarid = "userOpToolbar";
1462                         nrow = "UMRR-";
1463                         nchk = "UMCHK-";
1464                         lname = "prefUserList";
1465                 }
1466
1467                 if (opbarid) {
1468                         selectTableRowsByIdPrefix(lname, nrow, nchk, select);
1469                         disableContainerChildren(opbarid, !select);
1470                 }
1471
1472         } 
1473 }
1474
1475
1476 function toggleSelectPrefRow(sender, kind) {
1477
1478         toggleSelectRow(sender);
1479
1480         if (kind) {
1481                 var opbarid = false;    
1482                 var nsel = -1;
1483                 
1484                 if (kind == "feed") {
1485                         opbarid = "feedOpToolbar";
1486                         nsel = getSelectedFeeds();
1487                 } else if (kind == "fcat") {
1488                         opbarid = "catOpToolbar";
1489                         nsel = getSelectedFeedCats();
1490                 } else if (kind == "filter") {
1491                         opbarid = "filterOpToolbar";
1492                         nsel = getSelectedFilters();
1493                 } else if (kind == "label") {
1494                         opbarid = "labelOpToolbar";
1495                         nsel = getSelectedLabels();
1496                 } else if (kind == "user") {
1497                         opbarid = "userOpToolbar";
1498                         nsel = getSelectedUsers();
1499                 }
1500
1501                 if (opbarid && nsel != -1) {
1502                         disableContainerChildren(opbarid, nsel == false);
1503                 }
1504
1505         } 
1506 }
1507
1508 function toggleSelectFBListRow(sender) {
1509         toggleSelectListRow(sender);
1510         disableContainerChildren("fbrOpToolbar", getSelectedFeedsFromBrowser() == 0);
1511 }
1512
1513 var seq = "";
1514
1515 function pref_hotkey_handler(e) {
1516         try {
1517
1518                 var keycode;
1519         
1520                 if (!hotkeys_enabled) return;
1521         
1522                 if (window.event) {
1523                         keycode = window.event.keyCode;
1524                 } else if (e) {
1525                         keycode = e.which;
1526                 }
1527         
1528                 if (keycode == 13 || keycode == 27) {
1529                         seq = "";
1530                 } else {
1531                         seq = seq + "" + keycode;
1532                 }
1533
1534
1535         if (document.getElementById("piggie")) {
1536         
1537                 if (seq.match("807371717369")) {
1538                         seq = "";
1539                         localPiggieFunction(true);
1540                 } else {
1541                         localPiggieFunction(false);
1542                 }
1543         }
1544
1545         } catch (e) {
1546                 exception_error("pref_hotkey_handler", e);
1547         }
1548 }
1549
1550 function userSwitch() {
1551         var chooser = document.getElementById("userSwitch");
1552         var user = chooser[chooser.selectedIndex].value;
1553         window.location = "prefs.php?swu=" + user;
1554 }
1555
1556 function editFeedCats() {
1557         if (!xmlhttp_ready(xmlhttp)) {
1558                 printLockingError();
1559                 return
1560         }
1561
1562         xmlhttp.open("GET", "backend.php?op=pref-feeds&subop=editCats", true);
1563         xmlhttp.onreadystatechange=infobox_callback;
1564         xmlhttp.send(null);
1565 }
1566
1567 function showFeedsWithErrors() {
1568         displayDlg('feedUpdateErrors');
1569 }
1570
1571