]> git.wh0rd.org Git - tt-rss.git/blob - js/functions.js
rework filter dialog to make feed/category selection easier
[tt-rss.git] / js / functions.js
1 var notify_silent = false;
2 var loading_progress = 0;
3 var sanity_check_done = false;
4 var init_params = {};
5
6 Ajax.Base.prototype.initialize = Ajax.Base.prototype.initialize.wrap(
7         function (callOriginal, options) {
8
9                 if (getInitParam("csrf_token") != undefined) {
10                         Object.extend(options, options || { });
11
12                         if (Object.isString(options.parameters))
13                                 options.parameters = options.parameters.toQueryParams();
14                         else if (Object.isHash(options.parameters))
15                                 options.parameters = options.parameters.toObject();
16
17                         options.parameters["csrf_token"] = getInitParam("csrf_token");
18                 }
19
20                 return callOriginal(options);
21         }
22 );
23
24 /* add method to remove element from array */
25
26 Array.prototype.remove = function(s) {
27         for (var i=0; i < this.length; i++) {
28                 if (s == this[i]) this.splice(i, 1);
29         }
30 };
31
32 /* create console.log if it doesn't exist */
33
34 if (!window.console) console = {};
35 console.log = console.log || function(msg) { };
36 console.warn = console.warn || function(msg) { };
37 console.error = console.error || function(msg) { };
38
39 function exception_error(location, e, ext_info) {
40         var msg = format_exception_error(location, e);
41
42         if (!ext_info) ext_info = false;
43
44         try {
45
46                 if (ext_info) {
47                         if (ext_info.responseText) {
48                                 ext_info = ext_info.responseText;
49                         }
50                 }
51
52                 var content = "<div class=\"fatalError\">" +
53                         "<pre>" + msg + "</pre>";
54
55                 content += "<form name=\"exceptionForm\" id=\"exceptionForm\" target=\"_blank\" "+
56                   "action=\"http://tt-rss.org/report.php\" method=\"POST\">";
57
58                 content += "<textarea style=\"display : none\" name=\"message\">" + msg + "</textarea>";
59                 content += "<textarea style=\"display : none\" name=\"params\">N/A</textarea>";
60
61                 if (ext_info) {
62                         content += "<div><b>Additional information:</b></div>" +
63                         "<textarea name=\"xinfo\" readonly=\"1\">" + ext_info + "</textarea>";
64                 }
65
66                 content += "<div><b>Stack trace:</b></div>" +
67                         "<textarea name=\"stack\" readonly=\"1\">" + e.stack + "</textarea>";
68
69                 content += "</form>";
70
71                 content += "</div>";
72
73                 content += "<div class='dlgButtons'>";
74
75                 content += "<button dojoType=\"dijit.form.Button\""+
76                                 "onclick=\"dijit.byId('exceptionDlg').report()\">" +
77                                 __('Report to tt-rss.org') + "</button> ";
78                 content += "<button dojoType=\"dijit.form.Button\" "+
79                                 "onclick=\"dijit.byId('exceptionDlg').hide()\">" +
80                                 __('Close') + "</button>";
81                 content += "</div>";
82
83                 if (dijit.byId("exceptionDlg"))
84                         dijit.byId("exceptionDlg").destroyRecursive();
85
86                 var dialog = new dijit.Dialog({
87                         id: "exceptionDlg",
88                         title: "Unhandled exception",
89                         style: "width: 600px",
90                         report: function() {
91                                 if (confirm(__("Are you sure to report this exception to tt-rss.org? The report will include your browser information. Your IP would be saved in the database."))) {
92
93                                         document.forms['exceptionForm'].params.value = $H({
94                                                 browserName: navigator.appName,
95                                                 browserVersion: navigator.appVersion,
96                                                 browserPlatform: navigator.platform,
97                                                 browserCookies: navigator.cookieEnabled,
98                                         }).toQueryString();
99
100                                         document.forms['exceptionForm'].submit();
101
102                                 }
103                         },
104                         content: content});
105
106                 dialog.show();
107
108         } catch (e) {
109                 alert(msg);
110         }
111
112 }
113
114 function format_exception_error(location, e) {
115         var msg;
116
117         if (e.fileName) {
118                 var base_fname = e.fileName.substring(e.fileName.lastIndexOf("/") + 1);
119
120                 msg = "Exception: " + e.name + ", " + e.message +
121                         "\nFunction: " + location + "()" +
122                         "\nLocation: " + base_fname + ":" + e.lineNumber;
123
124         } else if (e.description) {
125                 msg = "Exception: " + e.description + "\nFunction: " + location + "()";
126         } else {
127                 msg = "Exception: " + e + "\nFunction: " + location + "()";
128         }
129
130         console.error("EXCEPTION: " + msg);
131
132         return msg;
133 }
134
135 function param_escape(arg) {
136         if (typeof encodeURIComponent != 'undefined')
137                 return encodeURIComponent(arg);
138         else
139                 return escape(arg);
140 }
141
142 function param_unescape(arg) {
143         if (typeof decodeURIComponent != 'undefined')
144                 return decodeURIComponent(arg);
145         else
146                 return unescape(arg);
147 }
148
149 var notify_hide_timerid = false;
150
151 function hide_notify() {
152         var n = $("notify");
153         if (n) {
154                 n.style.display = "none";
155         }
156 }
157
158 function notify_silent_next() {
159         notify_silent = true;
160 }
161
162 function notify_real(msg, no_hide, n_type) {
163
164         if (notify_silent) {
165                 notify_silent = false;
166                 return;
167         }
168
169         var n = $("notify");
170         var nb = $("notify_body");
171
172         if (!n || !nb) return;
173
174         if (notify_hide_timerid) {
175                 window.clearTimeout(notify_hide_timerid);
176         }
177
178         if (msg == "") {
179                 if (n.style.display == "block") {
180                         notify_hide_timerid = window.setTimeout("hide_notify()", 0);
181                 }
182                 return;
183         } else {
184                 n.style.display = "block";
185         }
186
187         /* types:
188
189                 1 - generic
190                 2 - progress
191                 3 - error
192                 4 - info
193
194         */
195
196         if (typeof __ != 'undefined') {
197                 msg = __(msg);
198         }
199
200         if (n_type == 1) {
201                 n.className = "notify";
202         } else if (n_type == 2) {
203                 n.className = "notifyProgress";
204                 msg = "<img src='"+getInitParam("sign_progress")+"'> " + msg;
205         } else if (n_type == 3) {
206                 n.className = "notifyError";
207                 msg = "<img src='"+getInitParam("sign_excl")+"'> " + msg;
208         } else if (n_type == 4) {
209                 n.className = "notifyInfo";
210                 msg = "<img src='"+getInitParam("sign_info")+"'> " + msg;
211         }
212
213 //      msg = "<img src='images/live_com_loading.gif'> " + msg;
214
215         nb.innerHTML = msg;
216
217         if (!no_hide) {
218                 notify_hide_timerid = window.setTimeout("hide_notify()", 3000);
219         }
220 }
221
222 function notify(msg, no_hide) {
223         notify_real(msg, no_hide, 1);
224 }
225
226 function notify_progress(msg, no_hide) {
227         notify_real(msg, no_hide, 2);
228 }
229
230 function notify_error(msg, no_hide) {
231         notify_real(msg, no_hide, 3);
232
233 }
234
235 function notify_info(msg, no_hide) {
236         notify_real(msg, no_hide, 4);
237 }
238
239 function setCookie(name, value, lifetime, path, domain, secure) {
240
241         var d = false;
242
243         if (lifetime) {
244                 d = new Date();
245                 d.setTime(d.getTime() + (lifetime * 1000));
246         }
247
248         console.log("setCookie: " + name + " => " + value + ": " + d);
249
250         int_setCookie(name, value, d, path, domain, secure);
251
252 }
253
254 function int_setCookie(name, value, expires, path, domain, secure) {
255         document.cookie= name + "=" + escape(value) +
256                 ((expires) ? "; expires=" + expires.toGMTString() : "") +
257                 ((path) ? "; path=" + path : "") +
258                 ((domain) ? "; domain=" + domain : "") +
259                 ((secure) ? "; secure" : "");
260 }
261
262 function delCookie(name, path, domain) {
263         if (getCookie(name)) {
264                 document.cookie = name + "=" +
265                 ((path) ? ";path=" + path : "") +
266                 ((domain) ? ";domain=" + domain : "" ) +
267                 ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
268         }
269 }
270
271
272 function getCookie(name) {
273
274         var dc = document.cookie;
275         var prefix = name + "=";
276         var begin = dc.indexOf("; " + prefix);
277         if (begin == -1) {
278             begin = dc.indexOf(prefix);
279             if (begin != 0) return null;
280         }
281         else {
282             begin += 2;
283         }
284         var end = document.cookie.indexOf(";", begin);
285         if (end == -1) {
286             end = dc.length;
287         }
288         return unescape(dc.substring(begin + prefix.length, end));
289 }
290
291 function gotoPreferences() {
292         document.location.href = "prefs.php";
293 }
294
295 function gotoMain() {
296         document.location.href = "index.php";
297 }
298
299 /** * @(#)isNumeric.js * * Copyright (c) 2000 by Sundar Dorai-Raj
300   * * @author Sundar Dorai-Raj
301   * * Email: sdoraira@vt.edu
302   * * This program is free software; you can redistribute it and/or
303   * * modify it under the terms of the GNU General Public License
304   * * as published by the Free Software Foundation; either version 2
305   * * of the License, or (at your option) any later version,
306   * * provided that any use properly credits the author.
307   * * This program is distributed in the hope that it will be useful,
308   * * but WITHOUT ANY WARRANTY; without even the implied warranty of
309   * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
310   * * GNU General Public License for more details at http://www.gnu.org * * */
311
312   var numbers=".0123456789";
313   function isNumeric(x) {
314     // is x a String or a character?
315     if(x.length>1) {
316       // remove negative sign
317       x=Math.abs(x)+"";
318       for(var j=0;j<x.length;j++) {
319         // call isNumeric recursively for each character
320         number=isNumeric(x.substring(j,j+1));
321         if(!number) return number;
322       }
323       return number;
324     }
325     else {
326       // if x is number return true
327       if(numbers.indexOf(x)>=0) return true;
328       return false;
329     }
330   }
331
332
333 function toggleSelectRowById(sender, id) {
334         var row = $(id);
335         return toggleSelectRow(sender, row);
336 }
337
338 function toggleSelectListRow(sender) {
339         var row = sender.parentNode;
340         return toggleSelectRow(sender, row);
341 }
342
343 /* this is for dijit Checkbox */
344 function toggleSelectListRow2(sender) {
345         var row = sender.domNode.parentNode;
346         return toggleSelectRow(sender, row);
347 }
348
349 function tSR(sender, row) {
350         return toggleSelectRow(sender, row);
351 }
352
353 /* this is for dijit Checkbox */
354 function toggleSelectRow2(sender, row) {
355
356         if (!row) row = sender.domNode.parentNode.parentNode;
357
358         if (sender.checked && !row.hasClassName('Selected'))
359                 row.addClassName('Selected');
360         else
361                 row.removeClassName('Selected');
362 }
363
364
365 function toggleSelectRow(sender, row) {
366
367         if (!row) row = sender.parentNode.parentNode;
368
369         if (sender.checked && !row.hasClassName('Selected'))
370                 row.addClassName('Selected');
371         else
372                 row.removeClassName('Selected');
373 }
374
375 function checkboxToggleElement(elem, id) {
376         if (elem.checked) {
377                 Effect.Appear(id, {duration : 0.5});
378         } else {
379                 Effect.Fade(id, {duration : 0.5});
380         }
381 }
382
383 function dropboxSelect(e, v) {
384         for (var i = 0; i < e.length; i++) {
385                 if (e[i].value == v) {
386                         e.selectedIndex = i;
387                         break;
388                 }
389         }
390 }
391
392 function getURLParam(param){
393         return String(window.location.href).parseQuery()[param];
394 }
395
396 function closeInfoBox(cleanup) {
397         try {
398                 dialog = dijit.byId("infoBox");
399
400                 if (dialog)     dialog.hide();
401
402         } catch (e) {
403                 //exception_error("closeInfoBox", e);
404         }
405         return false;
406 }
407
408
409 function displayDlg(id, param, callback) {
410
411         notify_progress("Loading, please wait...", true);
412
413         var query = "?op=dlg&method=" +
414                 param_escape(id) + "&param=" + param_escape(param);
415
416         new Ajax.Request("backend.php", {
417                 parameters: query,
418                 onComplete: function (transport) {
419                         infobox_callback2(transport);
420                         if (callback) callback(transport);
421                 } });
422
423         return false;
424 }
425
426 function infobox_callback2(transport) {
427         try {
428                 var dialog = false;
429
430                 if (dijit.byId("infoBox")) {
431                         dialog = dijit.byId("infoBox");
432                 }
433
434                 //console.log("infobox_callback2");
435                 notify('');
436
437                 var title = transport.responseXML.getElementsByTagName("title")[0];
438                 if (title)
439                         title = title.firstChild.nodeValue;
440
441                 var content = transport.responseXML.getElementsByTagName("content")[0];
442
443                 content = content.firstChild.nodeValue;
444
445                 if (!dialog) {
446                         dialog = new dijit.Dialog({
447                                 title: title,
448                                 id: 'infoBox',
449                                 style: "width: 600px",
450                                 onCancel: function() {
451                                         return true;
452                                 },
453                                 onExecute: function() {
454                                         return true;
455                                 },
456                                 onClose: function() {
457                                         return true;
458                                         },
459                                 content: content});
460                 } else {
461                         dialog.attr('title', title);
462                         dialog.attr('content', content);
463                 }
464
465                 dialog.show();
466
467                 notify("");
468         } catch (e) {
469                 exception_error("infobox_callback2", e);
470         }
471 }
472
473 function filterCR(e, f)
474 {
475      var key;
476
477      if(window.event)
478           key = window.event.keyCode;     //IE
479      else
480           key = e.which;     //firefox
481
482         if (key == 13) {
483                 if (typeof f != 'undefined') {
484                         f();
485                         return false;
486                 } else {
487                         return false;
488                 }
489         } else {
490                 return true;
491         }
492 }
493
494 function getInitParam(key) {
495         return init_params[key];
496 }
497
498 function setInitParam(key, value) {
499         init_params[key] = value;
500 }
501
502 function fatalError(code, msg, ext_info) {
503         try {
504
505                 if (code == 6) {
506                         window.location.href = "index.php";
507                 } else if (code == 5) {
508                         window.location.href = "db-updater.php";
509                 } else {
510
511                         if (msg == "") msg = "Unknown error";
512
513                         if (ext_info) {
514                                 if (ext_info.responseText) {
515                                         ext_info = ext_info.responseText;
516                                 }
517                         }
518
519                         if (ERRORS && ERRORS[code] && !msg) {
520                                 msg = ERRORS[code];
521                         }
522
523                         var content = "<div><b>Error code:</b> " + code + "</div>" +
524                                 "<p>" + msg + "</p>";
525
526                         if (ext_info) {
527                                 content = content + "<div><b>Additional information:</b></div>" +
528                                         "<textarea style='width: 100%' readonly=\"1\">" +
529                                         ext_info + "</textarea>";
530                         }
531
532                         var dialog = new dijit.Dialog({
533                                 title: "Fatal error",
534                                 style: "width: 600px",
535                                 content: content});
536
537                         dialog.show();
538
539                 }
540
541                 return false;
542
543         } catch (e) {
544                 exception_error("fatalError", e);
545         }
546 }
547
548 function filterDlgCheckType(sender) {
549
550         try {
551
552                 var ftype = sender.value;
553
554                 // if selected filter type is 5 (Date) enable the modifier dropbox
555                 if (ftype == 5) {
556                         Element.show("filterDlg_dateModBox");
557                         Element.show("filterDlg_dateChkBox");
558                 } else {
559                         Element.hide("filterDlg_dateModBox");
560                         Element.hide("filterDlg_dateChkBox");
561
562                 }
563
564         } catch (e) {
565                 exception_error("filterDlgCheckType", e);
566         }
567
568 }
569
570 function filterDlgCheckAction(sender) {
571
572         try {
573
574                 var action = sender.value;
575
576                 var action_param = $("filterDlg_paramBox");
577
578                 if (!action_param) {
579                         console.log("filterDlgCheckAction: can't find action param box!");
580                         return;
581                 }
582
583                 // if selected action supports parameters, enable params field
584                 if (action == 4 || action == 6 || action == 7) {
585                         new Effect.Appear(action_param, {duration : 0.5});
586                         if (action != 7) {
587                                 Element.show(dijit.byId("filterDlg_actionParam").domNode);
588                                 Element.hide(dijit.byId("filterDlg_actionParamLabel").domNode);
589                         } else {
590                                 Element.show(dijit.byId("filterDlg_actionParamLabel").domNode);
591                                 Element.hide(dijit.byId("filterDlg_actionParam").domNode);
592                         }
593                 } else {
594                         Element.hide(action_param);
595                 }
596
597         } catch (e) {
598                 exception_error("filterDlgCheckAction", e);
599         }
600
601 }
602
603 function filterDlgCheckDate() {
604         try {
605                 var dialog = dijit.byId("filterEditDlg");
606
607                 var reg_exp = dialog.attr('value').reg_exp;
608
609                 var query = "?op=rpc&method=checkDate&date=" + reg_exp;
610
611                 new Ajax.Request("backend.php", {
612                         parameters: query,
613                         onComplete: function(transport) {
614
615                                 var reply = JSON.parse(transport.responseText);
616
617                                 if (reply['result'] == true) {
618                                         alert(__("Date syntax appears to be correct:") + " " + reply['date']);
619                                         return;
620                                 } else {
621                                         alert(__("Date syntax is incorrect."));
622                                 }
623
624                         } });
625
626
627         } catch (e) {
628                 exception_error("filterDlgCheckDate", e);
629         }
630 }
631
632 function explainError(code) {
633         return displayDlg("explainError", code);
634 }
635
636 function loading_set_progress(p) {
637         try {
638                 loading_progress += p;
639
640                 if (dijit.byId("loading_bar"))
641                         dijit.byId("loading_bar").update({progress: loading_progress});
642
643                 if (loading_progress >= 90)
644                         remove_splash();
645
646         } catch (e) {
647                 exception_error("loading_set_progress", e);
648         }
649 }
650
651 function remove_splash() {
652
653         if (Element.visible("overlay")) {
654                 console.log("about to remove splash, OMG!");
655                 Element.hide("overlay");
656                 console.log("removed splash!");
657         }
658 }
659
660 function transport_error_check(transport) {
661         try {
662                 if (transport.responseXML) {
663                         var error = transport.responseXML.getElementsByTagName("error")[0];
664
665                         if (error) {
666                                 var code = error.getAttribute("error-code");
667                                 var msg = error.getAttribute("error-msg");
668                                 if (code != 0) {
669                                         fatalError(code, msg);
670                                         return false;
671                                 }
672                         }
673                 }
674         } catch (e) {
675                 exception_error("check_for_error_xml", e);
676         }
677         return true;
678 }
679
680 function strip_tags(s) {
681         return s.replace(/<\/?[^>]+(>|$)/g, "");
682 }
683
684 function truncate_string(s, length) {
685         if (!length) length = 30;
686         var tmp = s.substring(0, length);
687         if (s.length > length) tmp += "&hellip;";
688         return tmp;
689 }
690
691 function hotkey_prefix_timeout() {
692         try {
693
694                 var date = new Date();
695                 var ts = Math.round(date.getTime() / 1000);
696
697                 if (hotkey_prefix_pressed && ts - hotkey_prefix_pressed >= 5) {
698                         console.log("hotkey_prefix seems to be stuck, aborting");
699                         hotkey_prefix_pressed = false;
700                         hotkey_prefix = false;
701                         Element.hide('cmdline');
702                 }
703
704                 setTimeout("hotkey_prefix_timeout()", 1000);
705
706         } catch  (e) {
707                 exception_error("hotkey_prefix_timeout", e);
708         }
709 }
710
711 function hideAuxDlg() {
712         try {
713                 Element.hide('auxDlg');
714         } catch (e) {
715                 exception_error("hideAuxDlg", e);
716         }
717 }
718
719
720 function uploadIconHandler(rc) {
721         try {
722                 switch (rc) {
723                         case 0:
724                                 notify_info("Upload complete.");
725                                 if (inPreferences()) {
726                                         updateFeedList();
727                                 } else {
728                                         setTimeout('updateFeedList(false, false)', 50);
729                                 }
730                                 break;
731                         case 1:
732                                 notify_error("Upload failed: icon is too big.");
733                                 break;
734                         case 2:
735                                 notify_error("Upload failed.");
736                                 break;
737                 }
738
739         } catch (e) {
740                 exception_error("uploadIconHandler", e);
741         }
742 }
743
744 function removeFeedIcon(id) {
745
746         try {
747
748                 if (confirm(__("Remove stored feed icon?"))) {
749                         var query = "backend.php?op=pref-feeds&method=removeicon&feed_id=" + param_escape(id);
750
751                         console.log(query);
752
753                         notify_progress("Removing feed icon...", true);
754
755                         new Ajax.Request("backend.php", {
756                                 parameters: query,
757                                 onComplete: function(transport) {
758                                         notify_info("Feed icon removed.");
759                                         if (inPreferences()) {
760                                                 updateFeedList();
761                                         } else {
762                                                 setTimeout('updateFeedList(false, false)', 50);
763                                         }
764                                 } });
765                 }
766
767                 return false;
768         } catch (e) {
769                 exception_error("removeFeedIcon", e);
770         }
771 }
772
773 function uploadFeedIcon() {
774
775         try {
776
777                 var file = $("icon_file");
778
779                 if (file.value.length == 0) {
780                         alert(__("Please select an image file to upload."));
781                 } else {
782                         if (confirm(__("Upload new icon for this feed?"))) {
783                                 notify_progress("Uploading, please wait...", true);
784                                 return true;
785                         }
786                 }
787
788                 return false;
789
790         } catch (e) {
791                 exception_error("uploadFeedIcon", e);
792         }
793 }
794
795 function addLabel(select, callback) {
796
797         try {
798
799                 var caption = prompt(__("Please enter label caption:"), "");
800
801                 if (caption != undefined) {
802
803                         if (caption == "") {
804                                 alert(__("Can't create label: missing caption."));
805                                 return false;
806                         }
807
808                         var query = "?op=pref-labels&method=add&caption=" +
809                                 param_escape(caption);
810
811                         if (select)
812                                 query += "&output=select";
813
814                         notify_progress("Loading, please wait...", true);
815
816                         if (inPreferences() && !select) active_tab = "labelConfig";
817
818                         new Ajax.Request("backend.php", {
819                                 parameters: query,
820                                 onComplete: function(transport) {
821                                         if (callback) {
822                                                 callback(transport);
823                                         } else if (inPreferences()) {
824                                                 updateLabelList();
825                                         } else {
826                                                 updateFeedList();
827                                         }
828                         } });
829
830                 }
831
832         } catch (e) {
833                 exception_error("addLabel", e);
834         }
835 }
836
837 function quickAddFeed() {
838         try {
839                 var query = "backend.php?op=dlg&method=quickAddFeed";
840
841                 if (dijit.byId("feedAddDlg"))
842                         dijit.byId("feedAddDlg").destroyRecursive();
843
844                 var dialog = new dijit.Dialog({
845                         id: "feedAddDlg",
846                         title: __("Subscribe to Feed"),
847                         style: "width: 600px",
848                         execute: function() {
849                                 if (this.validate()) {
850                                         console.log(dojo.objectToQuery(this.attr('value')));
851
852                                         var feed_url = this.attr('value').feed;
853
854                                         Element.show("feed_add_spinner");
855
856                                         new Ajax.Request("backend.php", {
857                                                 parameters: dojo.objectToQuery(this.attr('value')),
858                                                 onComplete: function(transport) {
859                                                         try {
860
861                                                                 var reply = JSON.parse(transport.responseText);
862
863                                                                 var rc = reply['result'];
864
865                                                                 notify('');
866                                                                 Element.hide("feed_add_spinner");
867
868                                                                 console.log("GOT RC: " + rc);
869
870                                                                 switch (parseInt(rc['code'])) {
871                                                                 case 1:
872                                                                         dialog.hide();
873                                                                         notify_info(__("Subscribed to %s").replace("%s", feed_url));
874
875                                                                         updateFeedList();
876                                                                         break;
877                                                                 case 2:
878                                                                         alert(__("Specified URL seems to be invalid."));
879                                                                         break;
880                                                                 case 3:
881                                                                         alert(__("Specified URL doesn't seem to contain any feeds."));
882                                                                         break;
883                                                                 case 4:
884                                                                         notify_progress("Searching for feed urls...", true);
885
886                                                                         new Ajax.Request("backend.php", {
887                                                                                 parameters: 'op=rpc&method=extractfeedurls&url=' + param_escape(feed_url),
888                                                                                 onComplete: function(transport, dialog, feed_url) {
889
890                                                                                         notify('');
891
892                                                                                         var reply = JSON.parse(transport.responseText);
893
894                                                                                         var feeds = reply['urls'];
895
896                                                                                         console.log(transport.responseText);
897
898                                                                                         var select = dijit.byId("feedDlg_feedContainerSelect");
899
900                                                                                         while (select.getOptions().length > 0)
901                                                                                                 select.removeOption(0);
902
903                                                                                         var count = 0;
904                                                                                         for (var feedUrl in feeds) {
905                                                                                                 select.addOption({value: feedUrl, label: feeds[feedUrl]});
906                                                                                                 count++;
907                                                                                         }
908
909 //                                                                                      if (count > 5) count = 5;
910 //                                                                                      select.size = count;
911
912                                                                                         Effect.Appear('feedDlg_feedsContainer', {duration : 0.5});
913                                                                                 }
914                                                                         });
915                                                                         break;
916                                                                 case 5:
917                                                                         alert(__("Couldn't download the specified URL: %s").
918                                                                                         replace("%s", rc['message']));
919                                                                         break;
920                                                                 case 0:
921                                                                         alert(__("You are already subscribed to this feed."));
922                                                                         break;
923                                                                 }
924
925                                                         } catch (e) {
926                                                                 exception_error("subscribeToFeed", e, transport);
927                                                         }
928
929                                                 } });
930
931                                         }
932                         },
933                         href: query});
934
935                 dialog.show();
936         } catch (e) {
937                 exception_error("quickAddFeed", e);
938         }
939 }
940
941 function quickAddFilter() {
942         try {
943                 var query = "backend.php?op=dlg&method=quickAddFilter";
944
945                 if (dijit.byId("feedEditDlg"))
946                         dijit.byId("feedEditDlg").destroyRecursive();
947
948                 if (dijit.byId("filterEditDlg"))
949                         dijit.byId("filterEditDlg").destroyRecursive();
950
951                 dialog = new dijit.Dialog({
952                         id: "filterEditDlg",
953                         title: __("Create Filter"),
954                         style: "width: 600px",
955                         test: function() {
956                                 if (this.validate()) {
957
958                                         var query = "?op=rpc&method=verifyRegexp&reg_exp=" +
959                                                 param_escape(dialog.attr('value').reg_exp);
960
961                                         notify_progress("Verifying regular expression...");
962
963                                         new Ajax.Request("backend.php", {
964                                                 parameters: query,
965                                                 onComplete: function(transport) {
966                                                         var reply = JSON.parse(transport.responseText);
967
968                                                         if (reply) {
969                                                                 notify('');
970
971                                                                 if (!reply['status']) {
972                                                                         alert("Invalid regular expression.");
973                                                                         return;
974                                                                 } else {
975
976                                                                         if (dijit.byId("filterTestDlg"))
977                                                                                 dijit.byId("filterTestDlg").destroyRecursive();
978
979                                                                         tdialog = new dijit.Dialog({
980                                                                                 id: "filterTestDlg",
981                                                                                 title: __("Filter Test Results"),
982                                                                                 style: "width: 600px",
983                                                                                 href: "backend.php?savemode=test&" +
984                                                                                 dojo.objectToQuery(dialog.attr('value')),
985                                                                         });
986
987                                                                         tdialog.show();
988                                                                 }
989                                                         }
990                                         }});
991                                 }
992                         },
993                         execute: function() {
994                                 if (this.validate()) {
995
996                                         var query = "?op=rpc&method=verifyRegexp&reg_exp=" +
997                                                 param_escape(dialog.attr('value').reg_exp);
998
999                                         notify_progress("Verifying regular expression...");
1000
1001                                         new Ajax.Request("backend.php", {
1002                                                 parameters: query,
1003                                                 onComplete: function(transport) {
1004                                                         var reply = JSON.parse(transport.responseText);
1005
1006                                                         if (reply) {
1007                                                                 notify('');
1008
1009                                                                 if (!reply['status']) {
1010                                                                         alert("Invalid regular expression.");
1011                                                                         return;
1012                                                                 } else {
1013                                                                         notify_progress("Saving data...", true);
1014
1015                                                                         console.log(dojo.objectToQuery(dialog.attr('value')));
1016
1017                                                                         new Ajax.Request("backend.php", {
1018                                                                                 parameters: dojo.objectToQuery(dialog.attr('value')),
1019                                                                                 onComplete: function(transport) {
1020                                                                                         dialog.hide();
1021                                                                                         notify_info(transport.responseText);
1022                                                                                         if (inPreferences()) {
1023                                                                                                 updateFilterList();
1024                                                                                         }
1025                                                                         }});
1026                                                                 }
1027                                                         }
1028                                         }});
1029                                 }
1030                         },
1031                         href: query});
1032
1033                 dialog.show();
1034
1035                 var lh = dojo.connect(dialog, "onLoad",
1036                                 function() {
1037                                         dojo.disconnect(lh);
1038                                         var title = $("PTITLE-FULL-" + active_post_id);
1039
1040                                         if (title) {
1041                                                 $("filterDlg_regExp").value = title.innerHTML;
1042                                         }
1043                         });
1044
1045         } catch (e) {
1046                 exception_error("quickAddFilter", e);
1047         }
1048 }
1049
1050 function resetPubSub(feed_id, title) {
1051
1052         var msg = __("Reset subscription? Tiny Tiny RSS will try to subscribe to the notification hub again on next feed update.").replace("%s", title);
1053
1054         if (title == undefined || confirm(msg)) {
1055                 notify_progress("Loading, please wait...");
1056
1057                 var query = "?op=pref-feeds&quiet=1&method=resetPubSub&ids=" + feed_id;
1058
1059                 new Ajax.Request("backend.php", {
1060                         parameters: query,
1061                         onComplete: function(transport) {
1062                                 dijit.byId("pubsubReset_Btn").attr('disabled', true);
1063                                 notify_info("Subscription reset.");
1064                         } });
1065         }
1066
1067         return false;
1068 }
1069
1070
1071 function unsubscribeFeed(feed_id, title) {
1072
1073         var msg = __("Unsubscribe from %s?").replace("%s", title);
1074
1075         if (title == undefined || confirm(msg)) {
1076                 notify_progress("Removing feed...");
1077
1078                 var query = "?op=pref-feeds&quiet=1&method=remove&ids=" + feed_id;
1079
1080                 new Ajax.Request("backend.php", {
1081                         parameters: query,
1082                         onComplete: function(transport) {
1083
1084                                         if (dijit.byId("feedEditDlg")) dijit.byId("feedEditDlg").hide();
1085
1086                                         if (inPreferences()) {
1087                                                 updateFeedList();
1088                                         } else {
1089                                                 if (feed_id == getActiveFeedId())
1090                                                         setTimeout("viewfeed(-5)", 100);
1091                                         }
1092
1093                                 } });
1094         }
1095
1096         return false;
1097 }
1098
1099
1100 function backend_sanity_check_callback(transport) {
1101
1102         try {
1103
1104                 if (sanity_check_done) {
1105                         fatalError(11, "Sanity check request received twice. This can indicate "+
1106                       "presence of Firebug or some other disrupting extension. "+
1107                                 "Please disable it and try again.");
1108                         return;
1109                 }
1110
1111                 var reply = JSON.parse(transport.responseText);
1112
1113                 if (!reply) {
1114                         fatalError(3, "Sanity check: invalid RPC reply", transport.responseText);
1115                         return;
1116                 }
1117
1118                 var error_code = reply['error']['code'];
1119
1120                 if (error_code && error_code != 0) {
1121                         return fatalError(error_code, reply['error']['message']);
1122                 }
1123
1124                 console.log("sanity check ok");
1125
1126                 var params = reply['init-params'];
1127
1128                 if (params) {
1129                         console.log('reading init-params...');
1130
1131                         if (params) {
1132                                 for (k in params) {
1133                                         var v = params[k];
1134                                         console.log("IP: " + k + " => " + v);
1135                                 }
1136                         }
1137
1138                         init_params = params;
1139                 }
1140
1141                 sanity_check_done = true;
1142
1143                 init_second_stage();
1144
1145         } catch (e) {
1146                 exception_error("backend_sanity_check_callback", e, transport);
1147         }
1148 }
1149
1150 /*function has_local_storage() {
1151         try {
1152                 return 'sessionStorage' in window && window['sessionStorage'] != null;
1153         } catch (e) {
1154                 return false;
1155         }
1156 } */
1157
1158 function catSelectOnChange(elem) {
1159         try {
1160 /*              var value = elem[elem.selectedIndex].value;
1161                 var def = elem.getAttribute('default');
1162
1163                 if (value == "ADD_CAT") {
1164
1165                         if (def)
1166                                 dropboxSelect(elem, def);
1167                         else
1168                                 elem.selectedIndex = 0;
1169
1170                         quickAddCat(elem);
1171                 } */
1172
1173         } catch (e) {
1174                 exception_error("catSelectOnChange", e);
1175         }
1176 }
1177
1178 function quickAddCat(elem) {
1179         try {
1180                 var cat = prompt(__("Please enter category title:"));
1181
1182                 if (cat) {
1183
1184                         var query = "?op=rpc&method=quickAddCat&cat=" + param_escape(cat);
1185
1186                         notify_progress("Loading, please wait...", true);
1187
1188                         new Ajax.Request("backend.php", {
1189                                 parameters: query,
1190                                 onComplete: function (transport) {
1191                                         var response = transport.responseXML;
1192                                         var select = response.getElementsByTagName("select")[0];
1193                                         var options = select.getElementsByTagName("option");
1194
1195                                         dropbox_replace_options(elem, options);
1196
1197                                         notify('');
1198
1199                         } });
1200
1201                 }
1202
1203         } catch (e) {
1204                 exception_error("quickAddCat", e);
1205         }
1206 }
1207
1208 function genUrlChangeKey(feed, is_cat) {
1209
1210         try {
1211                 var ok = confirm(__("Generate new syndication address for this feed?"));
1212
1213                 if (ok) {
1214
1215                         notify_progress("Trying to change address...", true);
1216
1217                         var query = "?op=rpc&method=regenFeedKey&id=" + param_escape(feed) +
1218                                 "&is_cat=" + param_escape(is_cat);
1219
1220                         new Ajax.Request("backend.php", {
1221                                 parameters: query,
1222                                 onComplete: function(transport) {
1223                                                 var reply = JSON.parse(transport.responseText);
1224                                                 var new_link = reply.link;
1225
1226                                                 var e = $('gen_feed_url');
1227
1228                                                 if (new_link) {
1229
1230                                                         e.innerHTML = e.innerHTML.replace(/\&amp;key=.*$/,
1231                                                                 "&amp;key=" + new_link);
1232
1233                                                         e.href = e.href.replace(/\&key=.*$/,
1234                                                                 "&key=" + new_link);
1235
1236                                                         new Effect.Highlight(e);
1237
1238                                                         notify('');
1239
1240                                                 } else {
1241                                                         notify_error("Could not change feed URL.");
1242                                                 }
1243                                 } });
1244                 }
1245         } catch (e) {
1246                 exception_error("genUrlChangeKey", e);
1247         }
1248         return false;
1249 }
1250
1251 function labelSelectOnChange(elem) {
1252         try {
1253 /*              var value = elem[elem.selectedIndex].value;
1254                 var def = elem.getAttribute('default');
1255
1256                 if (value == "ADD_LABEL") {
1257
1258                         if (def)
1259                                 dropboxSelect(elem, def);
1260                         else
1261                                 elem.selectedIndex = 0;
1262
1263                         addLabel(elem, function(transport) {
1264
1265                                         try {
1266
1267                                                 var response = transport.responseXML;
1268                                                 var select = response.getElementsByTagName("select")[0];
1269                                                 var options = select.getElementsByTagName("option");
1270
1271                                                 dropbox_replace_options(elem, options);
1272
1273                                                 notify('');
1274                                         } catch (e) {
1275                                                 exception_error("addLabel", e);
1276                                         }
1277                         });
1278                 } */
1279
1280         } catch (e) {
1281                 exception_error("labelSelectOnChange", e);
1282         }
1283 }
1284
1285 function dropbox_replace_options(elem, options) {
1286
1287         try {
1288                 while (elem.hasChildNodes())
1289                         elem.removeChild(elem.firstChild);
1290
1291                 var sel_idx = -1;
1292
1293                 for (var i = 0; i < options.length; i++) {
1294                         var text = options[i].firstChild.nodeValue;
1295                         var value = options[i].getAttribute("value");
1296
1297                         if (value == undefined) value = text;
1298
1299                         var issel = options[i].getAttribute("selected") == "1";
1300
1301                         var option = new Option(text, value, issel);
1302
1303                         if (options[i].getAttribute("disabled"))
1304                                 option.setAttribute("disabled", true);
1305
1306                         elem.insert(option);
1307
1308                         if (issel) sel_idx = i;
1309                 }
1310
1311                 // Chrome doesn't seem to just select stuff when you pass new Option(x, y, true)
1312                 if (sel_idx >= 0) elem.selectedIndex = sel_idx;
1313
1314         } catch (e) {
1315                 exception_error("dropbox_replace_options", e);
1316         }
1317 }
1318
1319 // mode = all, none, invert
1320 function selectTableRows(id, mode) {
1321         try {
1322                 var rows = $(id).rows;
1323
1324                 for (var i = 0; i < rows.length; i++) {
1325                         var row = rows[i];
1326                         var cb = false;
1327                         var dcb = false;
1328
1329                         if (row.id && row.className) {
1330                                 var bare_id = row.id.replace(/^[A-Z]*?-/, "");
1331                                 var inputs = rows[i].getElementsByTagName("input");
1332
1333                                 for (var j = 0; j < inputs.length; j++) {
1334                                         var input = inputs[j];
1335
1336                                         if (input.getAttribute("type") == "checkbox" &&
1337                                                         input.id.match(bare_id)) {
1338
1339                                                 cb = input;
1340                                                 dcb = dijit.getEnclosingWidget(cb);
1341                                                 break;
1342                                         }
1343                                 }
1344
1345                                 if (cb || dcb) {
1346                                         var issel = row.hasClassName("Selected");
1347
1348                                         if (mode == "all" && !issel) {
1349                                                 row.addClassName("Selected");
1350                                                 cb.checked = true;
1351                                                 if (dcb) dcb.set("checked", true);
1352                                         } else if (mode == "none" && issel) {
1353                                                 row.removeClassName("Selected");
1354                                                 cb.checked = false;
1355                                                 if (dcb) dcb.set("checked", false);
1356
1357                                         } else if (mode == "invert") {
1358
1359                                                 if (issel) {
1360                                                         row.removeClassName("Selected");
1361                                                         cb.checked = false;
1362                                                         if (dcb) dcb.set("checked", false);
1363                                                 } else {
1364                                                         row.addClassName("Selected");
1365                                                         cb.checked = true;
1366                                                         if (dcb) dcb.set("checked", true);
1367                                                 }
1368                                         }
1369                                 }
1370                         }
1371                 }
1372
1373         } catch (e) {
1374                 exception_error("selectTableRows", e);
1375
1376         }
1377 }
1378
1379 function getSelectedTableRowIds(id) {
1380         var rows = [];
1381
1382         try {
1383                 var elem_rows = $(id).rows;
1384
1385                 for (var i = 0; i < elem_rows.length; i++) {
1386                         if (elem_rows[i].hasClassName("Selected")) {
1387                                 var bare_id = elem_rows[i].id.replace(/^[A-Z]*?-/, "");
1388                                 rows.push(bare_id);
1389                         }
1390                 }
1391
1392         } catch (e) {
1393                 exception_error("getSelectedTableRowIds", e);
1394         }
1395
1396         return rows;
1397 }
1398
1399 function editFeed(feed, event) {
1400         try {
1401                 if (feed <= 0)
1402                         return alert(__("You can't edit this kind of feed."));
1403
1404                 var query = "backend.php?op=pref-feeds&method=editfeed&id=" +
1405                         param_escape(feed);
1406
1407                 console.log(query);
1408
1409                 if (dijit.byId("filterEditDlg"))
1410                         dijit.byId("filterEditDlg").destroyRecursive();
1411
1412                 if (dijit.byId("feedEditDlg"))
1413                         dijit.byId("feedEditDlg").destroyRecursive();
1414
1415                 dialog = new dijit.Dialog({
1416                         id: "feedEditDlg",
1417                         title: __("Edit Feed"),
1418                         style: "width: 600px",
1419                         execute: function() {
1420                                 if (this.validate()) {
1421 //                                      console.log(dojo.objectToQuery(this.attr('value')));
1422
1423                                         notify_progress("Saving data...", true);
1424
1425                                         new Ajax.Request("backend.php", {
1426                                                 parameters: dojo.objectToQuery(dialog.attr('value')),
1427                                                 onComplete: function(transport) {
1428                                                         dialog.hide();
1429                                                         notify('');
1430                                                         updateFeedList();
1431                                         }});
1432                                 }
1433                         },
1434                         href: query});
1435
1436                 dialog.show();
1437
1438         } catch (e) {
1439                 exception_error("editFeed", e);
1440         }
1441 }
1442
1443 function feedBrowser() {
1444         try {
1445                 var query = "backend.php?op=dlg&method=feedBrowser";
1446
1447                 if (dijit.byId("feedAddDlg"))
1448                         dijit.byId("feedAddDlg").hide();
1449
1450                 if (dijit.byId("feedBrowserDlg"))
1451                         dijit.byId("feedBrowserDlg").destroyRecursive();
1452
1453                 var dialog = new dijit.Dialog({
1454                         id: "feedBrowserDlg",
1455                         title: __("More Feeds"),
1456                         style: "width: 600px",
1457                         getSelectedFeedIds: function() {
1458                                 var list = $$("#browseFeedList li[id*=FBROW]");
1459                                 var selected = new Array();
1460
1461                                 list.each(function(child) {
1462                                         var id = child.id.replace("FBROW-", "");
1463
1464                                         if (child.hasClassName('Selected')) {
1465                                                 selected.push(id);
1466                                         }
1467                                 });
1468
1469                                 return selected;
1470                         },
1471                         getSelectedFeeds: function() {
1472                                 var list = $$("#browseFeedList li.Selected");
1473                                 var selected = new Array();
1474
1475                                 list.each(function(child) {
1476                                         var title = child.getElementsBySelector("span.fb_feedTitle")[0].innerHTML;
1477                                         var url = child.getElementsBySelector("a.fb_feedUrl")[0].href;
1478
1479                                         selected.push([title,url]);
1480
1481                                 });
1482
1483                                 return selected;
1484                         },
1485
1486                         subscribe: function() {
1487                                 var mode = this.attr('value').mode;
1488                                 var selected = [];
1489
1490                                 if (mode == "1")
1491                                         selected = this.getSelectedFeeds();
1492                                 else
1493                                         selected = this.getSelectedFeedIds();
1494
1495                                 if (selected.length > 0) {
1496                                         dijit.byId("feedBrowserDlg").hide();
1497
1498                                         notify_progress("Loading, please wait...", true);
1499
1500                                         // we use dojo.toJson instead of JSON.stringify because
1501                                         // it somehow escapes everything TWICE, at least in Chrome 9
1502
1503                                         var query = "?op=rpc&method=massSubscribe&payload="+
1504                                                 param_escape(dojo.toJson(selected)) + "&mode=" + param_escape(mode);
1505
1506                                         console.log(query);
1507
1508                                         new Ajax.Request("backend.php", {
1509                                                 parameters: query,
1510                                                 onComplete: function(transport) {
1511                                                         notify('');
1512                                                         updateFeedList();
1513                                                 } });
1514
1515                                 } else {
1516                                         alert(__("No feeds are selected."));
1517                                 }
1518
1519                         },
1520                         update: function() {
1521                                 var query = dojo.objectToQuery(dialog.attr('value'));
1522
1523                                 Element.show('feed_browser_spinner');
1524
1525                                 new Ajax.Request("backend.php", {
1526                                         parameters: query,
1527                                         onComplete: function(transport) {
1528                                                 notify('');
1529
1530                                                 Element.hide('feed_browser_spinner');
1531
1532                                                 var c = $("browseFeedList");
1533
1534                                                 var reply = JSON.parse(transport.responseText);
1535
1536                                                 var r = reply['content'];
1537                                                 var mode = reply['mode'];
1538
1539                                                 if (c && r) {
1540                                                         c.innerHTML = r;
1541                                                 }
1542
1543                                                 dojo.parser.parse("browseFeedList");
1544
1545                                                 if (mode == 2) {
1546                                                         Element.show(dijit.byId('feed_archive_remove').domNode);
1547                                                 } else {
1548                                                         Element.hide(dijit.byId('feed_archive_remove').domNode);
1549                                                 }
1550
1551                                         } });
1552                         },
1553                         removeFromArchive: function() {
1554                                 var selected = this.getSelectedFeeds();
1555
1556                                 if (selected.length > 0) {
1557
1558                                         var pr = __("Remove selected feeds from the archive? Feeds with stored articles will not be removed.");
1559
1560                                         if (confirm(pr)) {
1561                                                 Element.show('feed_browser_spinner');
1562
1563                                                 var query = "?op=rpc&method=remarchived&ids=" +
1564                                                         param_escape(selected.toString());;
1565
1566                                                 new Ajax.Request("backend.php", {
1567                                                         parameters: query,
1568                                                         onComplete: function(transport) {
1569                                                                 dialog.update();
1570                                                         } });
1571                                         }
1572                                 }
1573                         },
1574                         execute: function() {
1575                                 if (this.validate()) {
1576                                         this.subscribe();
1577                                 }
1578                         },
1579                         href: query});
1580
1581                 dialog.show();
1582
1583         } catch (e) {
1584                 exception_error("editFeed", e);
1585         }
1586 }
1587
1588 function showFeedsWithErrors() {
1589         try {
1590                 var query = "backend.php?op=dlg&method=feedsWithErrors";
1591
1592                 if (dijit.byId("errorFeedsDlg"))
1593                         dijit.byId("errorFeedsDlg").destroyRecursive();
1594
1595                 dialog = new dijit.Dialog({
1596                         id: "errorFeedsDlg",
1597                         title: __("Feeds with update errors"),
1598                         style: "width: 600px",
1599                         getSelectedFeeds: function() {
1600                                 return getSelectedTableRowIds("prefErrorFeedList");
1601                         },
1602                         removeSelected: function() {
1603                                 var sel_rows = this.getSelectedFeeds();
1604
1605                                 console.log(sel_rows);
1606
1607                                 if (sel_rows.length > 0) {
1608                                         var ok = confirm(__("Remove selected feeds?"));
1609
1610                                         if (ok) {
1611                                                 notify_progress("Removing selected feeds...", true);
1612
1613                                                 var query = "?op=pref-feeds&method=remove&ids="+
1614                                                         param_escape(sel_rows.toString());
1615
1616                                                 new Ajax.Request("backend.php", {
1617                                                         parameters: query,
1618                                                         onComplete: function(transport) {
1619                                                                 notify('');
1620                                                                 dialog.hide();
1621                                                                 updateFeedList();
1622                                                         } });
1623                                         }
1624
1625                                 } else {
1626                                         alert(__("No feeds are selected."));
1627                                 }
1628                         },
1629                         execute: function() {
1630                                 if (this.validate()) {
1631                                 }
1632                         },
1633                         href: query});
1634
1635                 dialog.show();
1636
1637         } catch (e) {
1638                 exception_error("showFeedsWithErrors", e);
1639         }
1640
1641 }
1642
1643 /* new support functions for SelectByTag */
1644
1645 function get_all_tags(selObj){
1646         try {
1647                 if( !selObj ) return "";
1648
1649                 var result = "";
1650                 var len = selObj.options.length;
1651
1652                 for (var i=0; i < len; i++){
1653                         if (selObj.options[i].selected) {
1654                                 result += selObj[i].value + "%2C";   // is really a comma
1655                         }
1656                 }
1657
1658                 if (result.length > 0){
1659                         result = result.substr(0, result.length-3);  // remove trailing %2C
1660                 }
1661
1662                 return(result);
1663
1664         } catch (e) {
1665                 exception_error("get_all_tags", e);
1666         }
1667 }
1668
1669 function get_radio_checked(radioObj) {
1670         try {
1671                 if (!radioObj) return "";
1672
1673                 var len = radioObj.length;
1674
1675                 if (len == undefined){
1676                         if(radioObj.checked){
1677                                 return(radioObj.value);
1678                         } else {
1679                                 return("");
1680                         }
1681                 }
1682
1683                 for( var i=0; i < len; i++ ){
1684                         if( radioObj[i].checked ){
1685                                 return( radioObj[i].value);
1686                         }
1687                 }
1688
1689         } catch (e) {
1690                 exception_error("get_radio_checked", e);
1691         }
1692         return("");
1693 }
1694
1695 function get_timestamp() {
1696         var date = new Date();
1697         return Math.round(date.getTime() / 1000);
1698 }