+// Extend date object with format method
+Date.prototype.format = function(format) {
+ format = format || 'YYYY-MM-DD hh:mm';
+
+ var zeropad = function(number, length) {
+ number = number.toString();
+ length = length || 2;
+ while(number.length < length)
+ number = '0' + number;
+ return number;
+ },
+ formats = {
+ YYYY: this.getFullYear(),
+ MM: zeropad(this.getMonth() + 1),
+ DD: zeropad(this.getDate()),
+ hh: zeropad(this.getHours()),
+ mm: zeropad(this.getMinutes()),
+ O: (function() {
+ localDate = new Date;
+ sign = (localDate.getTimezoneOffset() > 0) ? '-' : '+';
+ offset = Math.abs(localDate.getTimezoneOffset());
+ hours = zeropad(Math.floor(offset / 60));
+ minutes = zeropad(offset % 60);
+ return sign + hours + ":" + minutes;
+ })()
+ },
+ pattern = '(' + Object.keys(formats).join(')|(') + ')';
+
+ return format.replace(new RegExp(pattern, 'g'), function(match) {
+ return formats[match];
+ });
+};
+
+function dateFromUtcString(datestring) {
+ // matches »YYYY-MM-DD hh:mm«
+ var m = datestring.match(/(\d+)-(\d+)-(\d+)\s+(\d+):(\d+)/);
+ return new Date(Date.UTC(+m[1], +m[2] - 1, +m[3], +m[4], +m[5], 0));
+}
+
+function dateFromUtcTimestamp(datetimestamp) {
+ return new Date(parseInt(datetimestamp) * 1000)
+}
+
+function dateToUtcString(datelocal) {
+ return new Date(
+ datelocal.getUTCFullYear(),
+ datelocal.getUTCMonth(),
+ datelocal.getUTCDate(),
+ datelocal.getUTCHours(),
+ datelocal.getUTCMinutes(),
+ datelocal.getUTCSeconds()
+ ).format();
+}
+
+function dateToUtcTimestamp(datelocal) {
+ return (Date.UTC(
+ datelocal.getUTCFullYear(),
+ datelocal.getUTCMonth(),
+ datelocal.getUTCDate(),
+ datelocal.getUTCHours(),
+ datelocal.getUTCMinutes(),
+ datelocal.getUTCSeconds()
+ ) / 1000);
+}
+
+function convertAllDatetimeFields() {
+ datefields = document.getElementsByClassName('datetime')
+ for(var i=0; i<datefields.length; i++) {
+ dateUTC = datefields[i].getAttribute('data-datetime');
+ datefields[i].setAttribute('title', dateUTC + ' (GMT)');
+ datefields[i].innerHTML = dateFromUtcString(dateUTC).format('YYYY-MM-DD hh:mm (GMT O)');
+ }
+}
+
+function show_link (reference, delete_code, crypt_key, date)