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