]> git.donarmstrong.com Git - roundcube.git/blob - program/js/common.js
Imported Upstream version 0.1~beta2.2~dfsg
[roundcube.git] / program / js / common.js
1 /*
2  +-----------------------------------------------------------------------+
3  | RoundCube common js library                                           |
4  |                                                                       |
5  | This file is part of the RoundCube web development suite              |
6  | Copyright (C) 2005, RoundCube Dev, - Switzerland                      |
7  | Licensed under the GNU GPL                                            |
8  |                                                                       |
9  +-----------------------------------------------------------------------+
10  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
11  +-----------------------------------------------------------------------+
12  
13  $Id: common.js 289 2006-08-02 03:42:08Z cmcnulty $
14 */
15
16
17 // default browsercheck
18 function roundcube_browser()
19   {
20   this.ver = parseFloat(navigator.appVersion);
21   this.appver = navigator.appVersion;
22   this.agent = navigator.userAgent;
23   this.name = navigator.appName;
24   this.vendor = navigator.vendor ? navigator.vendor : '';
25   this.vendver = navigator.vendorSub ? parseFloat(navigator.vendorSub) : 0;
26   this.product = navigator.product ? navigator.product : '';
27   this.platform = String(navigator.platform).toLowerCase();
28   this.lang = (navigator.language) ? navigator.language.substring(0,2) :
29               (navigator.browserLanguage) ? navigator.browserLanguage.substring(0,2) :
30               (navigator.systemLanguage) ? navigator.systemLanguage.substring(0,2) : 'en';
31
32   this.win = (this.platform.indexOf('win')>=0) ? true : false;
33   this.mac = (this.platform.indexOf('mac')>=0) ? true : false;
34   this.linux = (this.platform.indexOf('linux')>=0) ? true : false;
35   this.unix = (this.platform.indexOf('unix')>=0) ? true : false;
36
37   this.dom = document.getElementById ? true : false;
38   this.dom2 = (document.addEventListener && document.removeEventListener);
39
40   this.ie = (document.all) ? true : false;
41   this.ie4 = (this.ie && !this.dom);
42   this.ie5 = (this.dom && this.appver.indexOf('MSIE 5')>0);
43   this.ie6 = (this.dom && this.appver.indexOf('MSIE 6')>0);
44
45   this.mz = (this.dom && this.ver>=5);  // (this.dom && this.product=='Gecko')
46   this.ns = ((this.ver<5 && this.name=='Netscape') || (this.ver>=5 && this.vendor.indexOf('Netscape')>=0));
47   this.ns4 = (this.ns && parseInt(this.ver)==4);
48   this.ns6 = (this.ns && parseInt(this.vendver)==6);  // (this.mz && this.ns) ? true : false;
49   this.ns7 = (this.ns && parseInt(this.vendver)==7);  // this.agent.indexOf('Netscape/7')>0);
50   this.safari = (this.agent.toLowerCase().indexOf('safari')>0 || this.agent.toLowerCase().indexOf('applewebkit')>0);
51   this.konq   = (this.agent.toLowerCase().indexOf('konqueror')>0);
52
53   this.opera = (window.opera) ? true : false;
54   this.opera5 = (this.opera5 && this.agent.indexOf('Opera 5')>0) ? true : false;
55   this.opera6 = (this.opera && this.agent.indexOf('Opera 6')>0) ? true : false;
56   this.opera7 = (this.opera && this.agent.indexOf('Opera 7')>0) ? true : false;
57
58   if(this.opera && window.RegExp)
59     this.vendver = (/opera(\s|\/)([0-9\.]+)/i.test(navigator.userAgent)) ? parseFloat(RegExp.$2) : -1;
60   else if(!this.vendver && this.safari)
61     this.vendver = (/(safari|applewebkit)\/([0-9]+)/i.test(this.agent)) ? parseInt(RegExp.$2) : 0;
62   else if((!this.vendver && this.mz) || this.agent.indexOf('Camino')>0)
63     this.vendver = (/rv:([0-9\.]+)/.test(this.agent)) ? parseFloat(RegExp.$1) : 0;
64   else if(this.ie && window.RegExp)
65     this.vendver = (/msie\s+([0-9\.]+)/i.test(this.agent)) ? parseFloat(RegExp.$1) : 0;
66   else if(this.konq && window.RegExp)
67     this.vendver = (/khtml\/([0-9\.]+)/i.test(this.agent)) ? parseFloat(RegExp.$1) : 0;
68
69
70   // get real language out of safari's user agent
71   if(this.safari && (/;\s+([a-z]{2})-[a-z]{2}\)/i.test(this.agent)))
72     this.lang = RegExp.$1;
73
74   this.dhtml = ((this.ie4 && this.win) || this.ie5 || this.ie6 || this.ns4 || this.mz);
75   this.layers = this.ns4;  // (document.layers);
76   this.div = (this.ie4 || this.dom);
77   this.vml = (this.win && this.ie && this.dom && !this.opera);
78   this.linkborder = (this.ie || this.mz);
79   this.rollover = (this.ver>=4 || (this.ns && this.ver>=3));  // (document.images) ? true : false;
80   this.pngalpha = (this.mz || (this.opera && this.vendver>=6) || (this.ie && this.mac && this.vendver>=5) ||
81                    (this.ie && this.win && this.vendver>=5.5) || this.safari);
82   this.opacity = (this.mz || (this.ie && this.vendver>=5.5 && !this.opera) || (this.safari && this.vendver>=100));
83   this.cookies = navigator.cookieEnabled;
84   
85   // test for XMLHTTP support
86   this.xmlhttp_test = function()
87     {
88     var activeX_test = new Function("try{var o=new ActiveXObject('Microsoft.XMLHTTP');return true;}catch(err){return false;}");
89     this.xmlhttp = (window.XMLHttpRequest || (window.ActiveXObject && activeX_test())) ? true : false;
90     return this.xmlhttp;
91     }
92   }
93
94
95
96
97 var rcube_layer_objects = new Array();
98
99 function rcube_layer(id, attributes)
100   {
101   this.name = id;
102   
103   // create a new layer in the current document
104   this.create = function(arg)
105     {
106     var l = (arg.x) ? arg.x : 0;
107     var t = (arg.y) ? arg.y : 0;
108     var w = arg.width;
109     var h = arg.height;
110     var z = arg.zindex;
111     var vis = arg.vis;
112     var parent = arg.parent;
113     var obj;
114
115     obj = document.createElement('DIV');
116     with(obj)
117       {
118       id = this.name;
119       with(style)
120         {
121         position = 'absolute';
122         visibility = (vis) ? (vis==2) ? 'inherit' : 'visible' : 'hidden';
123         left = l+'px';
124         top = t+'px';
125         if(w) width = w+'px';
126         if(h) height = h+'px';
127         if(z) zIndex = z;
128         }
129       }
130       
131     if(parent) parent.appendChild(obj);
132     else document.body.appendChild(obj);
133
134     this.elm = obj;
135     };
136
137
138   // create new layer
139   if(attributes!=null)
140     {
141     this.create(attributes);
142     this.name = this.elm.id;
143     }
144   else  // just refer to the object
145     this.elm = document.getElementById(id);
146
147
148   if(!this.elm)
149     return false;
150
151
152   // ********* layer object properties *********
153
154   this.css = this.elm.style;
155   this.event = this.elm;
156   this.width = this.elm.offsetWidth;
157   this.height = this.elm.offsetHeight;
158   this.x = parseInt(this.elm.offsetLeft);
159   this.y = parseInt(this.elm.offsetTop);
160   this.visible = (this.css.visibility=='visible' || this.css.visibility=='show' || this.css.visibility=='inherit') ? true : false;
161
162   this.id = rcube_layer_objects.length;
163   this.obj = 'rcube_layer_objects['+this.id+']';
164   rcube_layer_objects[this.id] = this;
165
166
167   // ********* layer object methods *********
168
169
170   // move the layer to a specific position
171   this.move = function(x, y)
172     {
173     this.x = x;
174     this.y = y;
175     this.css.left = Math.round(this.x)+'px';
176     this.css.top = Math.round(this.y)+'px';
177     }
178
179
180   // move the layer for a specific step
181   this.shift = function(x,y)
182     {
183     x = Math.round(x*100)/100;
184     y = Math.round(y*100)/100;
185     this.move(this.x+x, this.y+y);
186     }
187
188
189   // change the layers width and height
190   this.resize = function(w,h)
191     {
192     this.css.width  = w+'px';
193     this.css.height = h+'px';
194     this.width = w;
195     this.height = h;
196     }
197
198
199   // cut the layer (top,width,height,left)
200   this.clip = function(t,w,h,l)
201     {
202     this.css.clip='rect('+t+' '+w+' '+h+' '+l+')';
203     this.clip_height = h;
204     this.clip_width = w;
205     }
206
207
208   // show or hide the layer
209   this.show = function(a)
210     {
211     if(a==1)
212       {
213       this.css.visibility = 'visible';
214       this.visible = true;
215       }
216     else if(a==2)
217       {
218       this.css.visibility = 'inherit';
219       this.visible = true;
220       }
221     else
222       {
223       this.css.visibility = 'hidden';
224       this.visible = false;
225       }
226     }
227
228
229   // write new content into a Layer
230   this.write = function(cont)
231     {
232     this.elm.innerHTML = cont;
233     }
234
235
236   // set the given color to the layer background
237   this.set_bgcolor = function(c)
238     {
239     if(!c || c=='#')
240       c = 'transparent';
241
242     this.css.backgroundColor = c;
243     }
244
245
246   // set the opacity of a layer to the given ammount (in %)
247   this.set_opacity = function(v)
248     {
249     if(!bw.opacity)
250       return;
251
252     var op = v<=1 ? Math.round(v*100) : parseInt(v);
253
254     if(bw.ie)
255       this.css.filter = 'alpha(opacity:'+op+')';
256     else if(bw.safari)
257       {
258       this.css.opacity = op/100;
259       this.css.KhtmlOpacity = op/100;
260       }
261     else if(bw.mz)
262       this.css.MozOpacity = op/100;
263     }
264   }
265
266 // check if input is a valid email address
267 // By Cal Henderson <cal@iamcal.com>
268 // http://code.iamcal.com/php/rfc822/
269 function rcube_check_email(input, inline)
270   {
271   if (input && window.RegExp)
272     {
273     var no_ws_ctl    = "[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]";
274     var alpha        = "[\\x41-\\x5a\\x61-\\x7a]";
275     var digit        = "[\\x30-\\x39]";
276     var cr        = "\\x0d";
277     var lf        = "\\x0a";
278     var crlf        = "(" + cr + lf + ")";
279
280     var obs_char    = "[\\x00-\\x09\\x0b\\x0c\\x0e-\\x7f]";
281     var obs_text    = "("+lf+"*"+cr+"*("+obs_char+lf+"*"+cr+"*)*)";
282     var text        = "([\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f]|"+obs_text+")";
283     var obs_qp        = "(\\x5c[\\x00-\\x7f])";
284     var quoted_pair    = "(\\x5c"+text+"|"+obs_qp+")";
285
286     var wsp        = "[\\x20\\x09]";
287     var obs_fws    = "("+wsp+"+("+crlf+wsp+"+)*)";
288     var fws        = "((("+wsp+"*"+crlf+")?"+wsp+"+)|"+obs_fws+")";
289     var ctext        = "("+no_ws_ctl+"|[\\x21-\\x27\\x2A-\\x5b\\x5d-\\x7e])";
290     var ccontent    = "("+ctext+"|"+quoted_pair+")";
291     var comment    = "(\\x28("+fws+"?"+ccontent+")*"+fws+"?\\x29)";
292     var cfws        = "(("+fws+"?"+comment+")*("+fws+"?"+comment+"|"+fws+"))";
293     var cfws        = fws+"*";
294
295     var atext        = "("+alpha+"|"+digit+"|[\\x21\\x23-\\x27\\x2a\\x2b\\x2d\\x2e\\x3d\\x3f\\x5e\\x5f\\x60\\x7b-\\x7e])";
296     var atom        = "("+cfws+"?"+atext+"+"+cfws+"?)";
297
298     var qtext        = "("+no_ws_ctl+"|[\\x21\\x23-\\x5b\\x5d-\\x7e])";
299     var qcontent    = "("+qtext+"|"+quoted_pair+")";
300     var quoted_string    = "("+cfws+"?\\x22("+fws+"?"+qcontent+")*"+fws+"?\\x22"+cfws+"?)";
301     var word        = "("+atom+"|"+quoted_string+")";
302
303     var obs_local_part    = "("+word+"(\\x2e"+word+")*)";
304     var obs_domain    = "("+atom+"(\\x2e"+atom+")*)";
305
306     var dot_atom_text    = "("+atext+"+(\\x2e"+atext+"+)*)";
307     var dot_atom    = "("+cfws+"?"+dot_atom_text+cfws+"?)";
308
309     var dtext        = "("+no_ws_ctl+"|[\\x21-\\x5a\\x5e-\\x7e])";
310     var dcontent    = "("+dtext+"|"+quoted_pair+")";
311     var domain_literal    = "("+cfws+"?\\x5b("+fws+"?"+dcontent+")*"+fws+"?\\x5d"+cfws+"?)";
312
313     var local_part    = "("+dot_atom+"|"+quoted_string+"|"+obs_local_part+")";
314     var domain        = "("+dot_atom+"|"+domain_literal+"|"+obs_domain+")";
315     var addr_spec    = "("+local_part+"\\x40"+domain+")";
316
317     var reg1 = inline ? new RegExp(addr_spec, 'i') : new RegExp('^'+addr_spec+'$', 'i');
318     return reg1.test(input) ? true : false;
319     }
320   return false;
321   }
322   
323
324 // find a value in a specific array and returns the index
325 function find_in_array()
326   {
327   var args = find_in_array.arguments;
328   if(!args.length) return -1;
329
330   var haystack = typeof(args[0])=='object' ? args[0] : args.length>1 && typeof(args[1])=='object' ? args[1] : new Array();
331   var needle = typeof(args[0])!='object' ? args[0] : args.length>1 && typeof(args[1])!='object' ? args[1] : '';
332   var nocase = args.length==3 ? args[2] : false;
333
334   if(!haystack.length) return -1;
335
336   for(var i=0; i<haystack.length; i++)
337     if(nocase && haystack[i].toLowerCase()==needle.toLowerCase())
338       return i;
339     else if(haystack[i]==needle)
340       return i;
341
342   return -1;
343   }
344
345
346 // get any type of html objects by id/name
347 function rcube_find_object(id, d)
348   {
349   var n, f, obj, e;
350   if(!d) d = document;
351
352   if(d.getElementsByName && (e = d.getElementsByName(id)))
353     obj = e[0];
354   if(!obj && d.getElementById)
355     obj = d.getElementById(id);
356   if(!obj && d.all)
357     obj = d.all[id];
358
359   if(!obj && d.images.length)
360     obj = d.images[id];
361
362   if(!obj && d.forms.length)
363     for(f=0; f<d.forms.length; f++)
364       {
365       if(d.forms[f].name == id)
366         obj = d.forms[f];
367       else if(d.forms[f].elements[id])
368         obj = d.forms[f].elements[id];
369       }
370
371   if(!obj && d.layers)
372     {
373     if(d.layers[id]) obj = d.layers[id];
374     for(n=0; !obj && n<d.layers.length; n++)
375       obj = nex_get_object(id, d.layers[n].document);
376     }
377
378   return obj;
379   }
380
381
382 // return the absolute position of an object within the document
383 function rcube_get_object_pos(obj)
384   {
385   if(typeof(obj)=='string')
386     obj = nex_get_object(obj);
387
388   if(!obj) return {x:0, y:0};
389
390   var iX = (bw.layers) ? obj.x : obj.offsetLeft;
391   var iY = (bw.layers) ? obj.y : obj.offsetTop;
392
393   if(bw.ie || bw.mz)
394     {
395     var elm = obj.offsetParent;
396     while(elm && elm!=null)
397       {
398       iX += elm.offsetLeft;
399       iY += elm.offsetTop;
400       elm = elm.offsetParent;
401       }
402     }
403
404   if(bw.mac && bw.ie5) iX += document.body.leftMargin;
405   if(bw.mac && bw.ie5) iY += document.body.topMargin;
406
407   return {x:iX, y:iY};
408   }
409   
410
411 // cookie functions by GoogieSpell
412 function setCookie(name, value, expires, path, domain, secure)
413   {
414   var curCookie = name + "=" + escape(value) +
415       (expires ? "; expires=" + expires.toGMTString() : "") +
416       (path ? "; path=" + path : "") +
417       (domain ? "; domain=" + domain : "") +
418       (secure ? "; secure" : "");
419   document.cookie = curCookie;
420   }
421
422 function getCookie(name)
423   {
424   var dc = document.cookie;
425   var prefix = name + "=";
426   var begin = dc.indexOf("; " + prefix);
427   if (begin == -1)
428     {
429     begin = dc.indexOf(prefix);
430     if (begin != 0) return null;
431     }
432   else
433     begin += 2;  
434   var end = document.cookie.indexOf(";", begin);
435   if (end == -1)
436     end = dc.length;
437   return unescape(dc.substring(begin + prefix.length, end));
438   }
439
440
441 var bw = new roundcube_browser();