Thomas
2013-10-09 d016dcc6f6a3daf8c19e2ececd3c676cd274381a
commit | author | age
4e17e6 1 /*
T 2  +-----------------------------------------------------------------------+
e019f2 3  | Roundcube common js library                                           |
4e17e6 4  |                                                                       |
e019f2 5  | This file is part of the Roundcube web development suite              |
7fe381 6  | Copyright (C) 2005-2012, The Roundcube Dev Team                       |
T 7  |                                                                       |
8  | Licensed under the GNU General Public License version 3 or            |
9  | any later version with exceptions for skins & plugins.                |
10  | See the README file for a full license statement.                     |
4e17e6 11  |                                                                       |
T 12  +-----------------------------------------------------------------------+
13  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
14  +-----------------------------------------------------------------------+
15 */
16
6b47de 17 // Constants
T 18 var CONTROL_KEY = 1;
19 var SHIFT_KEY = 2;
20 var CONTROL_SHIFT_KEY = 3;
4e17e6 21
6b47de 22
T 23 /**
24  * Default browser check class
186537 25  * @constructor
6b47de 26  */
4e17e6 27 function roundcube_browser()
8fa922 28 {
8fd1cf 29   var n = navigator;
A 30
31   this.ver = parseFloat(n.appVersion);
32   this.appver = n.appVersion;
33   this.agent = n.userAgent;
34   this.agent_lc = n.userAgent.toLowerCase();
35   this.name = n.appName;
36   this.vendor = n.vendor ? n.vendor : '';
37   this.vendver = n.vendorSub ? parseFloat(n.vendorSub) : 0;
38   this.product = n.product ? n.product : '';
39   this.platform = String(n.platform).toLowerCase();
40   this.lang = (n.language) ? n.language.substring(0,2) :
41               (n.browserLanguage) ? n.browserLanguage.substring(0,2) :
42               (n.systemLanguage) ? n.systemLanguage.substring(0,2) : 'en';
4e17e6 43
89e31b 44   this.win = (this.platform.indexOf('win') >= 0);
T 45   this.mac = (this.platform.indexOf('mac') >= 0);
46   this.linux = (this.platform.indexOf('linux') >= 0);
47   this.unix = (this.platform.indexOf('unix') >= 0);
4e17e6 48
T 49   this.dom = document.getElementById ? true : false;
50   this.dom2 = (document.addEventListener && document.removeEventListener);
51
b231f6 52   this.ie = (document.all && !window.opera);
4e17e6 53   this.ie4 = (this.ie && !this.dom);
T 54   this.ie5 = (this.dom && this.appver.indexOf('MSIE 5')>0);
f8dae9 55   this.ie8 = (this.dom && this.appver.indexOf('MSIE 8')>0);
43d98b 56   this.ie9 = (this.dom && this.appver.indexOf('MSIE 9')>0);
8b7f5a 57   this.ie7 = (this.dom && this.appver.indexOf('MSIE 7')>0);
f8dae9 58   this.ie6 = (this.dom && !this.ie8 && !this.ie7 && this.appver.indexOf('MSIE 6')>0);
4e17e6 59
89e31b 60   this.ns = ((this.ver < 5 && this.name == 'Netscape') || (this.ver >= 5 && this.vendor.indexOf('Netscape') >= 0));
T 61   this.chrome = (this.agent_lc.indexOf('chrome') > 0);
8fd1cf 62   this.safari = (!this.chrome && (this.agent_lc.indexOf('safari') > 0 || this.agent_lc.indexOf('applewebkit') > 0));
2698d7 63   this.konq = (this.agent_lc.indexOf('konqueror') > 0);
AM 64   this.mz = (this.dom && !this.ie && !this.ns && !this.chrome && !this.safari && !this.konq && this.agent.indexOf('Mozilla') >= 0);
4910b0 65   this.iphone = (this.safari && (this.agent_lc.indexOf('iphone') > 0 || this.agent_lc.indexOf('ipod') > 0));
89e31b 66   this.ipad = (this.safari && this.agent_lc.indexOf('ipad') > 0);
T 67   this.opera = window.opera ? true : false;
4e17e6 68
89e31b 69   if (this.opera && window.RegExp)
8fd1cf 70     this.vendver = (/opera(\s|\/)([0-9\.]+)/.test(this.agent_lc)) ? parseFloat(RegExp.$2) : -1;
89e31b 71   else if (this.chrome && window.RegExp)
8fd1cf 72     this.vendver = (/chrome\/([0-9\.]+)/.test(this.agent_lc)) ? parseFloat(RegExp.$1) : 0;
89e31b 73   else if (!this.vendver && this.safari)
8fd1cf 74     this.vendver = (/(safari|applewebkit)\/([0-9]+)/.test(this.agent_lc)) ? parseInt(RegExp.$2) : 0;
89e31b 75   else if ((!this.vendver && this.mz) || this.agent.indexOf('Camino')>0)
4e17e6 76     this.vendver = (/rv:([0-9\.]+)/.test(this.agent)) ? parseFloat(RegExp.$1) : 0;
89e31b 77   else if (this.ie && window.RegExp)
8fd1cf 78     this.vendver = (/msie\s+([0-9\.]+)/.test(this.agent_lc)) ? parseFloat(RegExp.$1) : 0;
89e31b 79   else if (this.konq && window.RegExp)
8fd1cf 80     this.vendver = (/khtml\/([0-9\.]+)/.test(this.agent_lc)) ? parseFloat(RegExp.$1) : 0;
42b113 81
4e17e6 82   // get real language out of safari's user agent
129aef 83   if (this.safari && (/;\s+([a-z]{2})-[a-z]{2}\)/.test(this.agent_lc)))
4e17e6 84     this.lang = RegExp.$1;
T 85
4910b0 86   this.tablet = /ipad|android|xoom|sch-i800|playbook|tablet|kindle/i.test(this.agent_lc);
TB 87   this.mobile = /iphone|ipod|blackberry|iemobile|opera mini|opera mobi|mobile/i.test(this.agent_lc);
88   this.touch = this.mobile || this.tablet;
4e17e6 89   this.dhtml = ((this.ie4 && this.win) || this.ie5 || this.ie6 || this.ns4 || this.mz);
T 90   this.vml = (this.win && this.ie && this.dom && !this.opera);
89e31b 91   this.pngalpha = (this.mz || (this.opera && this.vendver >= 6) || (this.ie && this.mac && this.vendver >= 5) ||
T 92                    (this.ie && this.win && this.vendver >= 5.5) || this.safari);
93   this.opacity = (this.mz || (this.ie && this.vendver >= 5.5 && !this.opera) || (this.safari && this.vendver >= 100));
8fd1cf 94   this.cookies = n.cookieEnabled;
b231f6 95
a95e0e 96   // test for XMLHTTP support
89e31b 97   this.xmlhttp_test = function()
T 98   {
a95e0e 99     var activeX_test = new Function("try{var o=new ActiveXObject('Microsoft.XMLHTTP');return true;}catch(err){return false;}");
89e31b 100     this.xmlhttp = (window.XMLHttpRequest || (window.ActiveXObject && activeX_test()));
a95e0e 101     return this.xmlhttp;
89e31b 102   };
b231f6 103
89e31b 104   // set class names to html tag according to the current user agent detection
T 105   // this allows browser-specific css selectors like "html.chrome .someclass"
106   this.set_html_class = function()
107   {
108     var classname = ' js';
109
43d98b 110     if (this.ie)
T 111       classname += ' ie ie'+parseInt(this.vendver);
89e31b 112     else if (this.opera)
T 113       classname += ' opera';
114     else if (this.konq)
115       classname += ' konqueror';
116     else if (this.safari)
117       classname += ' chrome';
071c78 118     else if (this.chrome)
T 119       classname += ' chrome';
4188c6 120     else if (this.mz)
AM 121       classname += ' mozilla';
071c78 122
T 123     if (this.iphone)
89e31b 124       classname += ' iphone';
T 125     else if (this.ipad)
126       classname += ' ipad';
071c78 127     else if (this.safari || this.chrome)
T 128       classname += ' webkit';
89e31b 129
1257dd 130     if (this.mobile)
AM 131       classname += ' mobile';
4910b0 132     if (this.tablet)
TB 133       classname += ' tablet';
1257dd 134
89e31b 135     if (document.documentElement)
T 136       document.documentElement.className += classname;
137   };
8fa922 138 };
4e17e6 139
T 140
cc97ea 141 // static functions for DOM event handling
6b47de 142 var rcube_event = {
T 143
a0ce2f 144 /**
T 145  * returns the event target element
146  */
147 get_target: function(e)
148 {
149   e = e || window.event;
150   return e && e.target ? e.target : e.srcElement;
151 },
152
153 /**
154  * returns the event key code
155  */
156 get_keycode: function(e)
157 {
158   e = e || window.event;
159   return e && e.keyCode ? e.keyCode : (e && e.which ? e.which : 0);
160 },
9bbb17 161
6b47de 162 /**
f89f03 163  * returns the event key code
T 164  */
165 get_button: function(e)
166 {
167   e = e || window.event;
ef4f59 168   return e && e.button !== undefined ? e.button : (e && e.which ? e.which : 0);
f89f03 169 },
T 170
171 /**
6b47de 172  * returns modifier key (constants defined at top of file)
T 173  */
174 get_modifier: function(e)
175 {
176   var opcode = 0;
177   e = e || window.event;
178
699a25 179   if (bw.mac && e)
6b47de 180     opcode += (e.metaKey && CONTROL_KEY) + (e.shiftKey && SHIFT_KEY);
699a25 181   else if (e)
6b47de 182     opcode += (e.ctrlKey && CONTROL_KEY) + (e.shiftKey && SHIFT_KEY);
699a25 183
A 184   return opcode;
6b47de 185 },
T 186
187 /**
188  * Return absolute mouse position of an event
189  */
190 get_mouse_pos: function(e)
191 {
192   if (!e) e = window.event;
b231f6 193   var mX = (e.pageX) ? e.pageX : e.clientX,
A 194     mY = (e.pageY) ? e.pageY : e.clientY;
6b47de 195
8fa922 196   if (document.body && document.all) {
6b47de 197     mX += document.body.scrollLeft;
T 198     mY += document.body.scrollTop;
199   }
200
cf6bc5 201   if (e._offset) {
cc97ea 202     mX += e._offset.left;
T 203     mY += e._offset.top;
cf6bc5 204   }
A 205
6b47de 206   return { x:mX, y:mY };
T 207 },
208
209 /**
210  * Add an object method as event listener to a certain element
211  */
212 add_listener: function(p)
213 {
214   if (!p.object || !p.method)  // not enough arguments
215     return;
216   if (!p.element)
217     p.element = document;
218
219   if (!p.object._rc_events)
220     p.object._rc_events = [];
b231f6 221
6b47de 222   var key = p.event + '*' + p.method;
T 223   if (!p.object._rc_events[key])
224     p.object._rc_events[key] = function(e){ return p.object[p.method](e); };
225
226   if (p.element.addEventListener)
227     p.element.addEventListener(p.event, p.object._rc_events[key], false);
8fa922 228   else if (p.element.attachEvent) {
a265ab 229     // IE allows multiple events with the same function to be applied to the same object
T 230     // forcibly detach the event, then attach
231     p.element.detachEvent('on'+p.event, p.object._rc_events[key]);
6b47de 232     p.element.attachEvent('on'+p.event, p.object._rc_events[key]);
8fa922 233   }
6b47de 234   else
T 235     p.element['on'+p.event] = p.object._rc_events[key];
236 },
237
238 /**
239  * Remove event listener
240  */
241 remove_listener: function(p)
242 {
243   if (!p.element)
244     p.element = document;
245
246   var key = p.event + '*' + p.method;
247   if (p.object && p.object._rc_events && p.object._rc_events[key]) {
248     if (p.element.removeEventListener)
249       p.element.removeEventListener(p.event, p.object._rc_events[key], false);
250     else if (p.element.detachEvent)
251       p.element.detachEvent('on'+p.event, p.object._rc_events[key]);
252     else
253       p.element['on'+p.event] = null;
254   }
255 },
256
257 /**
3d78d5 258  * Prevent event propagation and bubbling
6b47de 259  */
T 260 cancel: function(evt)
261 {
262   var e = evt ? evt : window.event;
263   if (e.preventDefault)
264     e.preventDefault();
265   if (e.stopPropagation)
266     e.stopPropagation();
267
268   e.cancelBubble = true;
269   e.returnValue = false;
270   return false;
8ef2f3 271 },
T 272
273 touchevent: function(e)
274 {
275   return { pageX:e.pageX, pageY:e.pageY, offsetX:e.pageX - e.target.offsetLeft, offsetY:e.pageY - e.target.offsetTop, target:e.target, istouch:true };
6b47de 276 }
T 277
278 };
4e17e6 279
T 280
cc97ea 281 /**
T 282  * rcmail objects event interface
283  */
284 function rcube_event_engine()
285 {
286   this._events = {};
8fa922 287 };
cc97ea 288
T 289 rcube_event_engine.prototype = {
290
291 /**
292  * Setter for object event handlers
293  *
294  * @param {String}   Event name
295  * @param {Function} Handler function
296  * @return Listener ID (used to remove this handler later on)
297  */
298 addEventListener: function(evt, func, obj)
299 {
300   if (!this._events)
301     this._events = {};
302   if (!this._events[evt])
303     this._events[evt] = [];
b231f6 304
235504 305   this._events[evt].push({func:func, obj:obj ? obj : window});
cc97ea 306 },
T 307
308 /**
309  * Removes a specific event listener
310  *
311  * @param {String} Event name
312  * @param {Int}    Listener ID to remove
313  */
314 removeEventListener: function(evt, func, obj)
315 {
ef4f59 316   if (obj === undefined)
cc97ea 317     obj = window;
b231f6 318
cc97ea 319   for (var h,i=0; this._events && this._events[evt] && i < this._events[evt].length; i++)
T 320     if ((h = this._events[evt][i]) && h.func == func && h.obj == obj)
321       this._events[evt][i] = null;
322 },
323
324 /**
325  * This will execute all registered event handlers
326  *
327  * @param {String} Event to trigger
328  * @param {Object} Event object/arguments
329  */
330 triggerEvent: function(evt, e)
331 {
332   var ret, h;
ef4f59 333   if (e === undefined)
0e99d3 334     e = this;
ef4f59 335   else if (typeof e === 'object')
cc97ea 336     e.event = evt;
f52c93 337
cc97ea 338   if (this._events && this._events[evt] && !this._event_exec) {
T 339     this._event_exec = true;
340     for (var i=0; i < this._events[evt].length; i++) {
341       if ((h = this._events[evt][i])) {
ef4f59 342         if (typeof h.func === 'function')
0e99d3 343           ret = h.func.call ? h.func.call(h.obj, e) : h.func(e);
ef4f59 344         else if (typeof h.obj[h.func] === 'function')
0e99d3 345           ret = h.obj[h.func](e);
b231f6 346
cc97ea 347         // cancel event execution
ef4f59 348         if (ret !== undefined && !ret)
cc97ea 349           break;
T 350       }
351     }
6e1626 352     if (ret && ret.event) {
A 353       try {
354         delete ret.event;
355       } catch (err) {
356         // IE6-7 doesn't support deleting HTMLFormElement attributes (#1488017)
357         $(ret).removeAttr('event');
358       }
359     }
cc97ea 360   }
T 361
362   this._event_exec = false;
6e1626 363   if (e.event) {
A 364     try {
365       delete e.event;
366     } catch (err) {
367       // IE6-7 doesn't support deleting HTMLFormElement attributes (#1488017)
368       $(e).removeAttr('event');
369     }
370   }
24958a 371
cc97ea 372   return ret;
T 373 }
374
8fa922 375 };  // end rcube_event_engine.prototype
4e17e6 376
6b47de 377
10a699 378 // check if input is a valid email address
2c5762 379 // By Cal Henderson <cal@iamcal.com>
S 380 // http://code.iamcal.com/php/rfc822/
10a699 381 function rcube_check_email(input, inline)
8fa922 382 {
A 383   if (input && window.RegExp) {
e99991 384     var qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]',
A 385       dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]',
386       atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+',
387       quoted_pair = '\\x5c[\\x00-\\x7f]',
388       quoted_string = '\\x22('+qtext+'|'+quoted_pair+')*\\x22',
da2812 389       ipv4 = '\\[(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}\\]',
AM 390       ipv6 = '\\[IPv6:[0-9a-f:.]+\\]',
391       ip_addr = '(' + ipv4 + ')|(' + ipv6 + ')',
e99991 392       // Use simplified domain matching, because we need to allow Unicode characters here
A 393       // So, e-mail address should be validated also on server side after idn_to_ascii() use
394       //domain_literal = '\\x5b('+dtext+'|'+quoted_pair+')*\\x5d',
395       //sub_domain = '('+atom+'|'+domain_literal+')',
5c1dfb 396       // allow punycode/unicode top-level domain
ff8053 397       domain = '(('+ip_addr+')|(([^@\\x2e]+\\x2e)+([^\\x00-\\x40\\x5b-\\x60\\x7b-\\x7f]{2,}|xn--[a-z0-9]{2,})))',
47f55c 398       // ICANN e-mail test (http://idn.icann.org/E-mail_test)
A 399       icann_domains = [
400         '\\u0645\\u062b\\u0627\\u0644\\x2e\\u0625\\u062e\\u062a\\u0628\\u0627\\u0631',
401         '\\u4f8b\\u5b50\\x2e\\u6d4b\\u8bd5',
402         '\\u4f8b\\u5b50\\x2e\\u6e2c\\u8a66',
403         '\\u03c0\\u03b1\\u03c1\\u03ac\\u03b4\\u03b5\\u03b9\\u03b3\\u03bc\\u03b1\\x2e\\u03b4\\u03bf\\u03ba\\u03b9\\u03bc\\u03ae',
404         '\\u0909\\u0926\\u093e\\u0939\\u0930\\u0923\\x2e\\u092a\\u0930\\u0940\\u0915\\u094d\\u0937\\u093e',
405         '\\u4f8b\\u3048\\x2e\\u30c6\\u30b9\\u30c8',
406         '\\uc2e4\\ub840\\x2e\\ud14c\\uc2a4\\ud2b8',
407         '\\u0645\\u062b\\u0627\\u0644\\x2e\\u0622\\u0632\\u0645\\u0627\\u06cc\\u0634\u06cc',
408         '\\u043f\\u0440\\u0438\\u043c\\u0435\\u0440\\x2e\\u0438\\u0441\\u043f\\u044b\\u0442\\u0430\\u043d\\u0438\\u0435',
409         '\\u0b89\\u0ba4\\u0bbe\\u0bb0\\u0ba3\\u0bae\\u0bcd\\x2e\\u0baa\\u0bb0\\u0bbf\\u0b9f\\u0bcd\\u0b9a\\u0bc8',
410         '\\u05d1\\u05f2\\u05b7\\u05e9\\u05e4\\u05bc\\u05d9\\u05dc\\x2e\\u05d8\\u05e2\\u05e1\\u05d8'
411       ],
412       icann_addr = 'mailtest\\x40('+icann_domains.join('|')+')',
e99991 413       word = '('+atom+'|'+quoted_string+')',
A 414       delim = '[,;\s\n]',
415       local_part = word+'(\\x2e'+word+')*',
47f55c 416       addr_spec = '(('+local_part+'\\x40'+domain+')|('+icann_addr+'))',
e99991 417       reg1 = inline ? new RegExp('(^|<|'+delim+')'+addr_spec+'($|>|'+delim+')', 'i') : new RegExp('^'+addr_spec+'$', 'i');
A 418
2c5762 419     return reg1.test(input) ? true : false;
10a699 420   }
e99991 421
8fa922 422   return false;
A 423 };
4e17e6 424
7910c0 425 // recursively copy an object
T 426 function rcube_clone_object(obj)
427 {
428   var out = {};
b231f6 429
7910c0 430   for (var key in obj) {
ef4f59 431     if (obj[key] && typeof obj[key] === 'object')
abe798 432       out[key] = rcube_clone_object(obj[key]);
7910c0 433     else
T 434       out[key] = obj[key];
435   }
b231f6 436
7910c0 437   return out;
8fa922 438 };
4e17e6 439
be58b5 440 // make a string URL safe (and compatible with PHP's rawurlencode())
4d4264 441 function urlencode(str)
T 442 {
be58b5 443   if (window.encodeURIComponent)
A 444     return encodeURIComponent(str).replace('*', '%2A');
445
446   return escape(str)
447     .replace('+', '%2B')
448     .replace('*', '%2A')
449     .replace('/', '%2F')
450     .replace('@', '%40');
8fa922 451 };
4d4264 452
T 453
4e17e6 454 // get any type of html objects by id/name
T 455 function rcube_find_object(id, d)
cc97ea 456 {
4e17e6 457   var n, f, obj, e;
T 458   if(!d) d = document;
459
460   if(d.getElementsByName && (e = d.getElementsByName(id)))
461     obj = e[0];
462   if(!obj && d.getElementById)
463     obj = d.getElementById(id);
464   if(!obj && d.all)
465     obj = d.all[id];
466
467   if(!obj && d.images.length)
468     obj = d.images[id];
469
cc97ea 470   if (!obj && d.forms.length) {
T 471     for (f=0; f<d.forms.length; f++) {
4e17e6 472       if(d.forms[f].name == id)
T 473         obj = d.forms[f];
474       else if(d.forms[f].elements[id])
475         obj = d.forms[f].elements[id];
476     }
cc97ea 477   }
T 478
479   if (!obj && d.layers) {
480     if (d.layers[id]) obj = d.layers[id];
481     for (n=0; !obj && n<d.layers.length; n++)
482       obj = rcube_find_object(id, d.layers[n].document);
483   }
4e17e6 484
T 485   return obj;
8fa922 486 };
a7d5c6 487
f89f03 488 // determine whether the mouse is over the given object or not
T 489 function rcube_mouse_is_over(ev, obj)
490 {
129aef 491   var mouse = rcube_event.get_mouse_pos(ev),
A 492     pos = $(obj).offset();
cc97ea 493
T 494   return ((mouse.x >= pos.left) && (mouse.x < (pos.left + obj.offsetWidth)) &&
495     (mouse.y >= pos.top) && (mouse.y < (pos.top + obj.offsetHeight)));
8fa922 496 };
f89f03 497
dd53e2 498
T 499 // cookie functions by GoogieSpell
500 function setCookie(name, value, expires, path, domain, secure)
8fa922 501 {
dd53e2 502   var curCookie = name + "=" + escape(value) +
T 503       (expires ? "; expires=" + expires.toGMTString() : "") +
504       (path ? "; path=" + path : "") +
505       (domain ? "; domain=" + domain : "") +
506       (secure ? "; secure" : "");
507   document.cookie = curCookie;
8fa922 508 };
dd53e2 509
T 510 function getCookie(name)
8fa922 511 {
129aef 512   var dc = document.cookie,
A 513     prefix = name + "=",
514     begin = dc.indexOf("; " + prefix);
515
8fa922 516   if (begin == -1) {
dd53e2 517     begin = dc.indexOf(prefix);
129aef 518     if (begin != 0)
A 519       return null;
8fa922 520   }
129aef 521   else {
90a6af 522     begin += 2;
129aef 523   }
A 524
90a6af 525   var end = dc.indexOf(";", begin);
dd53e2 526   if (end == -1)
T 527     end = dc.length;
129aef 528
dd53e2 529   return unescape(dc.substring(begin + prefix.length, end));
8fa922 530 };
dd53e2 531
ae7027 532 // deprecated aliases, to be removed, use rcmail.set_cookie/rcmail.get_cookie
8fa922 533 roundcube_browser.prototype.set_cookie = setCookie;
a7d5c6 534 roundcube_browser.prototype.get_cookie = getCookie;
T 535
f11541 536 // tiny replacement for Firebox functionality
T 537 function rcube_console()
538 {
539   this.log = function(msg)
540   {
c1e9b0 541     var box = rcube_find_object('dbgconsole');
e5686f 542
a83697 543     if (box) {
e5686f 544       if (msg.charAt(msg.length-1)=='\n')
cc97ea 545         msg += '--------------------------------------\n';
715553 546       else
a83697 547         msg += '\n--------------------------------------\n';
A 548
129aef 549       // Konqueror doesn't allow to just change the value of hidden element
a83697 550       if (bw.konq) {
A 551         box.innerText += msg;
552         box.value = box.innerText;
553       } else
554         box.value += msg;
555     }
f11541 556   };
715553 557
f11541 558   this.reset = function()
T 559   {
c1e9b0 560     var box = rcube_find_object('dbgconsole');
715553 561     if (box)
a83697 562       box.innerText = box.value = '';
f11541 563   };
8fa922 564 };
f11541 565
b4b081 566 var bw = new roundcube_browser();
89e31b 567 bw.set_html_class();
b0dbf3 568
S 569
570 // Add escape() method to RegExp object
571 // http://dev.rubyonrails.org/changeset/7271
572 RegExp.escape = function(str)
8fa922 573 {
b0dbf3 574   return String(str).replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1');
8fa922 575 };
bafadd 576
65082b 577 // Extend Date prototype to detect Standard timezone without DST
T 578 // from http://www.michaelapproved.com/articles/timezone-detect-and-ignore-daylight-saving-time-dst/
579 Date.prototype.getStdTimezoneOffset = function()
580 {
581   var m = 12,
582     d = new Date(null, m, 1),
583     tzo = d.getTimezoneOffset();
584
585     while (--m) {
586       d.setUTCMonth(m);
587       if (tzo != d.getTimezoneOffset()) {
588         return Math.max(tzo, d.getTimezoneOffset());
589     }
590   }
591
592   return tzo;
593 }
bafadd 594
A 595 // Make getElementById() case-sensitive on IE
9e2c94 596 if (bw.ie) {
bafadd 597   document._getElementById = document.getElementById;
9e2c94 598   document.getElementById = function(id) {
27480b 599     var i = 0, obj = document._getElementById(id);
bafadd 600
283a37 601     if (obj && obj.id != id)
A 602       while ((obj = document.all[i]) && obj.id != id)
603         i++;
27480b 604
A 605     return obj;
bafadd 606   }
b231f6 607 }
fb6d86 608
9e2c94 609 // jQuery plugin to emulate HTML5 placeholder attributes on input elements
T 610 jQuery.fn.placeholder = function(text) {
611   return this.each(function() {
0c974b 612     var active = false, elem = $(this);
9e2c94 613     this.title = text;
T 614
39f401 615     // Try HTML5 placeholder attribute first
9e2c94 616     if ('placeholder' in this) {
39f401 617       elem.attr('placeholder', text);
9e2c94 618     }
39f401 619     // Fallback to Javascript emulation of placeholder
AM 620     else {
9e2c94 621       this._placeholder = text;
T 622       elem.blur(function(e) {
623         if ($.trim(elem.val()) == "")
624           elem.val(text);
625         elem.triggerHandler('change');
626       })
627       .focus(function(e) {
628         if ($.trim(elem.val()) == text)
629           elem.val("");
630         elem.triggerHandler('change');
631       })
632       .change(function(e) {
633         var active = elem.val() == text;
634         elem[(active ? 'addClass' : 'removeClass')]('placeholder').attr('spellcheck', active);
635       });
636
0c974b 637       // Do not blur currently focused element (catch exception: #1489008)
AM 638       try { active = this == document.activeElement; } catch(e) {}
639       if (!active)
f38d15 640         elem.blur();
9e2c94 641     }
T 642   });
643 };
644
645
fb6d86 646 // This code was written by Tyler Akins and has been placed in the
A 647 // public domain.  It would be nice if you left this header intact.
648 // Base64 code from Tyler Akins -- http://rumkin.com
649 var Base64 = (function () {
650   var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
651
652   var obj = {
653     /**
654      * Encodes a string in base64
655      * @param {String} input The string to encode in base64.
656      */
657     encode: function (input) {
658       if (typeof(window.btoa) === 'function')
659         return btoa(input);
660
661       var chr1, chr2, chr3, enc1, enc2, enc3, enc4, i = 0, output = '', len = input.length;
662
663       do {
664         chr1 = input.charCodeAt(i++);
665         chr2 = input.charCodeAt(i++);
666         chr3 = input.charCodeAt(i++);
667
668         enc1 = chr1 >> 2;
669         enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
670         enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
671         enc4 = chr3 & 63;
672
673         if (isNaN(chr2))
674           enc3 = enc4 = 64;
675         else if (isNaN(chr3))
676           enc4 = 64;
677
678         output = output
679           + keyStr.charAt(enc1) + keyStr.charAt(enc2)
680           + keyStr.charAt(enc3) + keyStr.charAt(enc4);
681       } while (i < len);
682
683       return output;
684     },
685
686     /**
687      * Decodes a base64 string.
688      * @param {String} input The string to decode.
689      */
690     decode: function (input) {
691       if (typeof(window.atob) === 'function')
692          return atob(input);
693
694       var chr1, chr2, chr3, enc1, enc2, enc3, enc4, len, i = 0, output = '';
695
696       // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
697       input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
698       len = input.length;
699
700       do {
701         enc1 = keyStr.indexOf(input.charAt(i++));
702         enc2 = keyStr.indexOf(input.charAt(i++));
703         enc3 = keyStr.indexOf(input.charAt(i++));
704         enc4 = keyStr.indexOf(input.charAt(i++));
705
706         chr1 = (enc1 << 2) | (enc2 >> 4);
707         chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
708         chr3 = ((enc3 & 3) << 6) | enc4;
709
710         output = output + String.fromCharCode(chr1);
711
712         if (enc3 != 64)
713           output = output + String.fromCharCode(chr2);
714         if (enc4 != 64)
715           output = output + String.fromCharCode(chr3);
716       } while (i < len);
717
718       return output;
719     }
720   };
721
722   return obj;
723 })();