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