thomascube
2006-05-01 6204390af16bcf50f82da61a1aefc2ad0c0adf94
commit | author | age
e0ed97 1 /*
4e17e6 2  +-----------------------------------------------------------------------+
T 3  | RoundCube Webmail Client Script                                       |
4  |                                                                       |
5  | This file is part of the RoundCube Webmail client                     |
6  | Copyright (C) 2005, RoundCube Dev, - Switzerland                      |
30233b 7  | Licensed under the GNU GPL                                            |
4e17e6 8  |                                                                       |
T 9  +-----------------------------------------------------------------------+
8c2e58 10  | Authors: Thomas Bruederli <roundcube@gmail.com>                       |
T 11  |          Charles McNulty <charles@charlesmcnulty.com>                 |
4e17e6 12  +-----------------------------------------------------------------------+
15a9d1 13  
T 14   $Id$
4e17e6 15 */
b11a00 16 // Constants
T 17 var CONTROL_KEY = 1;
18 var SHIFT_KEY = 2;
19 var CONTROL_SHIFT_KEY = 3;
4e17e6 20
T 21 var rcube_webmail_client;
22
23 function rcube_webmail()
24   {
25   this.env = new Object();
10a699 26   this.labels = new Object();
4e17e6 27   this.buttons = new Object();
T 28   this.gui_objects = new Object();
29   this.commands = new Object();
30   this.selection = new Array();
e8adc7 31   this.last_selected = 0;
4e17e6 32
T 33   // create public reference to myself
34   rcube_webmail_client = this;
35   this.ref = 'rcube_webmail_client';
36  
37   // webmail client settings
38   this.dblclick_time = 600;
39   this.message_time = 5000;
e66f5b 40   this.request_timeout = 180000;
ecf759 41   this.kepp_alive_interval = 60000;
4e17e6 42   this.mbox_expression = new RegExp('[^0-9a-z\-_]', 'gi');
T 43   this.env.blank_img = 'skins/default/images/blank.gif';
44   
45   // mimetypes supported by the browser (default settings)
46   this.mimetypes = new Array('text/plain', 'text/html', 'text/xml',
47                              'image/jpeg', 'image/gif', 'image/png',
48                              'application/x-javascript', 'application/pdf',
49                              'application/x-shockwave-flash');
50
51
52   // set environment variable
53   this.set_env = function(name, value)
54     {
55     //if (!this.busy)
56       this.env[name] = value;    
57     };
10a699 58
T 59
60   // add a localized label to the client environment
61   this.add_label = function(key, value)
62     {
63     this.labels[key] = value;
64     };
65
4e17e6 66
T 67   // add a button to the button list
68   this.register_button = function(command, id, type, act, sel, over)
69     {
70     if (!this.buttons[command])
71       this.buttons[command] = new Array();
72       
73     var button_prop = {id:id, type:type};
74     if (act) button_prop.act = act;
75     if (sel) button_prop.sel = sel;
76     if (over) button_prop.over = over;
77
78     this.buttons[command][this.buttons[command].length] = button_prop;    
79     };
80
81
82   // register a specific gui object
83   this.gui_object = function(name, id)
84     {
85     this.gui_objects[name] = id;
86     };
87
88
89   // initialize webmail client
90   this.init = function()
91     {
92     this.task = this.env.task;
93     
94     // check browser
a95e0e 95     if (!bw.dom || !bw.xmlhttp_test())
4e17e6 96       {
T 97       location.href = this.env.comm_path+'&_action=error&_code=0x199';
98       return;
99       }
100     
101     // find all registered gui objects
102     for (var n in this.gui_objects)
103       this.gui_objects[n] = rcube_find_object(this.gui_objects[n]);
104       
105     // tell parent window that this frame is loaded
106     if (this.env.framed && parent.rcmail && parent.rcmail.set_busy)
107       parent.rcmail.set_busy(false);
108
109     // enable general commands
110     this.enable_command('logout', 'mail', 'addressbook', 'settings', true);
111     
112     switch (this.task)
113       {
114       case 'mail':
d58c69 115         var msg_list_frame = this.gui_objects.mailcontframe;
4e17e6 116         var msg_list = this.gui_objects.messagelist;
T 117         if (msg_list)
118           {
d58c69 119           msg_list_frame.onmousedown = function(e){return rcube_webmail_client.click_on_list(e);};
4e17e6 120           this.init_messagelist(msg_list);
T 121           this.enable_command('markread', true);
122           }
123
124         // enable mail commands
4647e1 125         this.enable_command('list', 'compose', 'add-contact', 'search', 'reset-search', true);
4e17e6 126         
T 127         if (this.env.action=='show')
128           {
583f1c 129           this.enable_command('show', 'reply', 'reply-all', 'forward', 'moveto', 'delete', 'viewsource', 'print', 'load-attachment', true);
4e17e6 130           if (this.env.next_uid)
T 131             this.enable_command('nextmessage', true);
132           if (this.env.prev_uid)
133             this.enable_command('previousmessage', true);
134           }
135
136         if (this.env.action=='show' && this.env.blockedobjects)
137           {
138           if (this.gui_objects.remoteobjectsmsg)
139             this.gui_objects.remoteobjectsmsg.style.display = 'block';
140           this.enable_command('load-images', true);
141           }  
142
143         if (this.env.action=='compose')
144           this.enable_command('add-attachment', 'send-attachment', 'send', true);
145           
146         if (this.env.messagecount)
15a9d1 147           this.enable_command('select-all', 'select-none', 'sort', 'expunge', true);
4e17e6 148
5e3512 149         if (this.env.messagecount && this.env.mailbox==this.env.trash_mailbox)
T 150           this.enable_command('purge', true);
151
4e17e6 152         this.set_page_buttons();
T 153
154         // focus this window
155         window.focus();
156
157         // init message compose form
158         if (this.env.action=='compose')
159           this.init_messageform();
160
161         // show printing dialog
162         if (this.env.action=='print')
163           window.print();
15a9d1 164           
T 165         // get unread count for each mailbox
166         if (this.gui_objects.mailboxlist)
167           this.http_request('getunread', '');
4e17e6 168
T 169         break;
170
171
172       case 'addressbook':
d1d2c4 173         var contacts_list      = this.gui_objects.contactslist;
S 174         var ldap_contacts_list = this.gui_objects.ldapcontactslist;
175
4e17e6 176         if (contacts_list)
T 177           this.init_contactslist(contacts_list);
178       
d1d2c4 179         if (ldap_contacts_list)
S 180           this.init_ldapsearchlist(ldap_contacts_list);
181
4e17e6 182         this.set_page_buttons();
T 183           
184         if (this.env.cid)
185           this.enable_command('show', 'edit', true);
186
187         if ((this.env.action=='add' || this.env.action=='edit') && this.gui_objects.editform)
188           this.enable_command('save', true);
189       
c0da98 190         this.enable_command('list', 'add', true);
S 191
192         this.enable_command('ldappublicsearch', this.env.ldappublicsearch);
193
4e17e6 194         break;
T 195
196
197       case 'settings':
198         this.enable_command('preferences', 'identities', 'save', 'folders', true);
199         
200         if (this.env.action=='identities' || this.env.action=='edit-identity' || this.env.action=='add-identity')
201           this.enable_command('edit', 'add', 'delete', true);
202
203         if (this.env.action=='edit-identity' || this.env.action=='add-identity')
204           this.enable_command('save', true);
205           
206         if (this.env.action=='folders')
207           this.enable_command('subscribe', 'unsubscribe', 'create-folder', 'delete-folder', true);
208           
209         var identities_list = this.gui_objects.identitieslist;
210         if (identities_list)
211           this.init_identitieslist(identities_list);
212
213         break;
214
215       case 'login':
216         var input_user = rcube_find_object('_user');
217         var input_pass = rcube_find_object('_pass');
218         if (input_user && input_user.value=='')
219           input_user.focus();
220         else if (input_pass)
221           input_pass.focus();
222           
223         this.enable_command('login', true);
224         break;
225       
226       default:
227         break;
228       }
229
230
231     // enable basic commands
232     this.enable_command('logout', true);
233
234     // disable browser's contextmenus
8c2e58 235     // document.oncontextmenu = function(){ return false; }
4e17e6 236
d58c69 237     // load body click event
S 238     document.onmousedown = function(){ return rcube_webmail_client.reset_click(); };
356b6e 239     document.onkeydown   = function(e){ return rcube_webmail_client.key_pressed(e, msg_list_frame); };
d58c69 240
S 241     
4e17e6 242     // flag object as complete
T 243     this.loaded = true;
7902df 244           
4e17e6 245     // show message
T 246     if (this.pending_message)
247       this.display_message(this.pending_message[0], this.pending_message[1]);
ecf759 248       
15a9d1 249     // start interval for keep-alive/recent_check signal
3f9edb 250     if (this.kepp_alive_interval && this.task=='mail' && this.gui_objects.messagelist)
T 251       this.kepp_alive_int = setInterval(this.ref+'.check_for_recent()', this.kepp_alive_interval);
8c2e58 252     else if (this.task!='login')
3f9edb 253       this.kepp_alive_int = setInterval(this.ref+'.send_keep_alive()', this.kepp_alive_interval);
4e17e6 254     };
T 255
d58c69 256   // reset last clicked if user clicks on anything other than the message table
S 257   this.reset_click = function()
258     {
259     this.in_message_list = false;
260     };
261     
262   this.click_on_list = function(e)
263     {
264     if (!e)
265       e = window.event;
266
267     this.in_message_list = true;
268     e.cancelBubble = true;
269     };
270
356b6e 271   this.key_pressed = function(e, msg_list_frame) {
d58c69 272     if (this.in_message_list != true) 
S 273       return true;
274     var keyCode = document.layers ? e.which : document.all ? event.keyCode : document.getElementById ? e.keyCode : 0;
275     var mod_key = this.get_modifier(e);
356b6e 276     switch (keyCode) {
S 277       case 40:
278       case 38: 
279         return this.use_arrow_key(keyCode, mod_key, msg_list_frame);
280         break;
281       case 46:
282         return this.use_delete_key(keyCode, mod_key, msg_list_frame);
283         break;
284       default:
285         return true;
286     }
287   }
288
289   this.use_arrow_key = function(keyCode, mod_key, msg_list_frame) {
d58c69 290     var scroll_to = 0;
S 291     if (keyCode == 40) { // down arrow key pressed
e8adc7 292       new_row = this.get_next_row();
d58c69 293       if (!new_row) return false;
S 294       scroll_to = (Number(new_row.offsetTop) + Number(new_row.offsetHeight)) - Number(msg_list_frame.offsetHeight);
295     } else if (keyCode == 38) { // up arrow key pressed
e8adc7 296       new_row = this.get_prev_row();
d58c69 297       if (!new_row) return false;
S 298       scroll_to = new_row.offsetTop;
f108b9 299     } else {return true;}
d58c69 300     
e8adc7 301     this.select_row(new_row.uid,mod_key,true);
d58c69 302
S 303     if (((Number(new_row.offsetTop)) < (Number(msg_list_frame.scrollTop))) || 
304        ((Number(new_row.offsetTop) + Number(new_row.offsetHeight)) > (Number(msg_list_frame.scrollTop) + Number(msg_list_frame.offsetHeight)))) {
305       msg_list_frame.scrollTop = scroll_to;
306     }
307     return false;
308   };
356b6e 309   
S 310   this.use_delete_key = function(keyCode, mod_key, msg_list_frame){
311     this.command('delete','',this);
312     return false;
313   }
4e17e6 314
T 315   // get all message rows from HTML table and init each row
316   this.init_messagelist = function(msg_list)
317     {
318     if (msg_list && msg_list.tBodies[0])
319       {
d58c69 320           
4e17e6 321       this.message_rows = new Array();
T 322
323       var row;
324       for(var r=0; r<msg_list.tBodies[0].childNodes.length; r++)
325         {
326         row = msg_list.tBodies[0].childNodes[r];
327         //row = msg_list.tBodies[0].rows[r];
328         this.init_message_row(row);
329         }
330       }
331       
332     // alias to common rows array
333     this.list_rows = this.message_rows;
334     };
335     
336     
337   // make references in internal array and set event handlers
338   this.init_message_row = function(row)
339     {
340     var uid, msg_icon;
341     
342     if (String(row.id).match(/rcmrow([0-9]+)/))
343       {
344       uid = RegExp.$1;
345       row.uid = uid;
346               
347       this.message_rows[uid] = {id:row.id, obj:row,
348                                 classname:row.className,
349                                 unread:this.env.messages[uid] ? this.env.messages[uid].unread : null,
350                                 replied:this.env.messages[uid] ? this.env.messages[uid].replied : null};
351               
352       // set eventhandlers to table row
353       row.onmousedown = function(e){ return rcube_webmail_client.drag_row(e, this.uid); };
354       row.onmouseup = function(e){ return rcube_webmail_client.click_row(e, this.uid); };
d58c69 355
b11a00 356       if (document.all)
T 357         row.onselectstart = function() { return false; };
358
4e17e6 359       // set eventhandler to message icon
T 360       if ((msg_icon = row.cells[0].childNodes[0]) && row.cells[0].childNodes[0].nodeName=='IMG')
361         {                
362         msg_icon.id = 'msgicn_'+uid;
363         msg_icon._row = row;
364         msg_icon.onmousedown = function(e) { rcube_webmail_client.command('markread', this); };
365                 
366         // get message icon and save original icon src
367         this.message_rows[uid].icon = msg_icon;
368         }
369       }
370     };
371
372
373   // init message compose form: set focus and eventhandlers
374   this.init_messageform = function()
375     {
376     if (!this.gui_objects.messageform)
377       return false;
378     
379     //this.messageform = this.gui_objects.messageform;
1cded8 380     var input_from = rcube_find_object('_from');
4e17e6 381     var input_to = rcube_find_object('_to');
T 382     var input_cc = rcube_find_object('_cc');
383     var input_bcc = rcube_find_object('_bcc');
384     var input_replyto = rcube_find_object('_replyto');
385     var input_subject = rcube_find_object('_subject');
386     var input_message = rcube_find_object('_message');
387     
388     // init live search events
389     if (input_to)
390       this.init_address_input_events(input_to);
391     if (input_cc)
392       this.init_address_input_events(input_cc);
393     if (input_bcc)
394       this.init_address_input_events(input_bcc);
1cded8 395       
T 396     // add signature according to selected identity
397     if (input_from && input_from.type=='select-one')
398       this.change_identity(input_from);
4e17e6 399
T 400     if (input_to && input_to.value=='')
401       input_to.focus();
402     else if (input_subject && input_subject.value=='')
403       input_subject.focus();
404     else if (input_message)
405       this.set_caret2start(input_message); // input_message.focus();
977a29 406     
T 407     // get summary of all field values
408     this.cmp_hash = this.compose_field_hash();
4e17e6 409     };
T 410
411
412   this.init_address_input_events = function(obj)
413     {
414     var handler = function(e){ return rcube_webmail_client.ksearch_keypress(e,this); };
415     var handler2 = function(e){ return rcube_webmail_client.ksearch_blur(e,this); };
416     
417     if (bw.safari)
418       {
419       obj.addEventListener('keydown', handler, false);
420       // obj.addEventListener('blur', handler2, false);
421       }
422     else if (bw.mz)
423       {
424       obj.addEventListener('keypress', handler, false);
425       obj.addEventListener('blur', handler2, false);
426       }
427     else if (bw.ie)
428       {
429       obj.onkeydown = handler;
430       //obj.attachEvent('onkeydown', handler);
431       // obj.attachEvent('onblur', handler2, false);
432       }
433     
434     obj.setAttribute('autocomplete', 'off');       
435     };
436
437
438
439   // get all contact rows from HTML table and init each row
440   this.init_contactslist = function(contacts_list)
441     {
442     if (contacts_list && contacts_list.tBodies[0])
443       {
444       this.contact_rows = new Array();
445
446       var row;
447       for(var r=0; r<contacts_list.tBodies[0].childNodes.length; r++)
448         {
449         row = contacts_list.tBodies[0].childNodes[r];
450         this.init_table_row(row, 'contact_rows');
451         }
452       }
453
454     // alias to common rows array
455     this.list_rows = this.contact_rows;
456     
457     if (this.env.cid)
2a1e18 458       this.highlight_row(this.env.cid);
4e17e6 459     };
T 460
461
d1d2c4 462   // get all contact rows from HTML table and init each row
S 463   this.init_ldapsearchlist = function(ldap_contacts_list)
464     {
465     if (ldap_contacts_list && ldap_contacts_list.tBodies[0])
466       {
467       this.ldap_contact_rows = new Array();
468
469       var row;
470       for(var r=0; r<ldap_contacts_list.tBodies[0].childNodes.length; r++)
471         {
472         row = ldap_contacts_list.tBodies[0].childNodes[r];
473         this.init_table_row(row, 'ldap_contact_rows');
474         }
475       }
476
477     // alias to common rows array
478     this.list_rows = this.ldap_contact_rows;
479     };
480
481
4e17e6 482   // make references in internal array and set event handlers
T 483   this.init_table_row = function(row, array_name)
484     {
485     var cid;
486     
487     if (String(row.id).match(/rcmrow([0-9]+)/))
488       {
489       cid = RegExp.$1;
490       row.cid = cid;
491
492       this[array_name][cid] = {id:row.id,
493                                obj:row,
494                                classname:row.className};
495
496       // set eventhandlers to table row
497       row.onmousedown = function(e) { rcube_webmail_client.in_selection_before=this.cid; return false; };  // fake for drag handler
498       row.onmouseup = function(e){ return rcube_webmail_client.click_row(e, this.cid); };
499       }
500     };
501
502
503   // get all contact rows from HTML table and init each row
504   this.init_identitieslist = function(identities_list)
505     {
506     if (identities_list && identities_list.tBodies[0])
507       {
508       this.identity_rows = new Array();
509
510       var row;
511       for(var r=0; r<identities_list.tBodies[0].childNodes.length; r++)
512         {
513         row = identities_list.tBodies[0].childNodes[r];
514         this.init_table_row(row, 'identity_rows');
515         }
516       }
517
518     // alias to common rows array
519     this.list_rows = this.identity_rows;
520     
521     if (this.env.iid)
2a1e18 522       this.highlight_row(this.env.iid);    
4e17e6 523     };
T 524     
525
526
527   /*********************************************************/
528   /*********       client command interface        *********/
529   /*********************************************************/
530
531
532   // execute a specific command on the web client
533   this.command = function(command, props, obj)
534     {
535     if (obj && obj.blur)
536       obj.blur();
537
538     if (this.busy)
539       return false;
540
541     // command not supported or allowed
542     if (!this.commands[command])
543       {
544       // pass command to parent window
545       if (this.env.framed && parent.rcmail && parent.rcmail.command)
546         parent.rcmail.command(command, props);
547
548       return false;
549       }
15a9d1 550       
T 551       
552    // check input before leaving compose step
553    if (this.task=='mail' && this.env.action=='compose' && (command=='list' || command=='mail' || command=='addressbook' || command=='settings'))
554      {
555      if (this.cmp_hash != this.compose_field_hash() && !confirm(this.get_label('notsentwarning')))
556         return false;
557      }
558
4e17e6 559
T 560     // process command
561     switch (command)
562       {
563       case 'login':
564         if (this.gui_objects.loginform)
565           this.gui_objects.loginform.submit();
566         break;
567
568       case 'logout':
569         location.href = this.env.comm_path+'&_action=logout';
570         break;      
571
572       // commands to switch task
573       case 'mail':
574       case 'addressbook':
575       case 'settings':
576         this.switch_task(command);
577         break;
578
579
580       // misc list commands
581       case 'list':
582         if (this.task=='mail')
4647e1 583           {
ac6b87 584           if (this.env.search_request<0 || (this.env.search_request && props != this.env.mailbox))
4647e1 585             this.reset_qsearch();
4e17e6 586           this.list_mailbox(props);
4647e1 587           }
4e17e6 588         else if (this.task=='addressbook')
T 589           this.list_contacts();
f3b659 590         break;
T 591
592       case 'sort':
593         // get the type of sorting
b076a4 594         var a_sort = props.split('_');
T 595         var sort_col = a_sort[0];
1cded8 596         var sort_order = a_sort[1] ? a_sort[1].toUpperCase() : null;
b076a4 597         var header;
1cded8 598         
T 599         // no sort order specified: toggle
600         if (sort_order==null)
601           {
602           if (this.env.sort_col==sort_col)
603             sort_order = this.env.sort_order=='ASC' ? 'DESC' : 'ASC';
604           else
605             sort_order = this.env.sort_order;
606           }
b076a4 607         
T 608         if (this.env.sort_col==sort_col && this.env.sort_order==sort_order)
609           break;
610
611         // set table header class
612         if (header = document.getElementById('rcmHead'+this.env.sort_col))
613           this.set_classname(header, 'sorted'+(this.env.sort_order.toUpperCase()), false);
614         if (header = document.getElementById('rcmHead'+sort_col))
615           this.set_classname(header, 'sorted'+sort_order, true);
616
617         // save new sort properties
618         this.env.sort_col = sort_col;
619         this.env.sort_order = sort_order;
620
621         // reload message list
1cded8 622         this.list_mailbox('', '', sort_col+'_'+sort_order);
4e17e6 623         break;
T 624
625       case 'nextpage':
626         this.list_page('next');
627         break;
628
629       case 'previouspage':
630         this.list_page('prev');
15a9d1 631         break;
T 632
633       case 'expunge':
634         if (this.env.messagecount)
635           this.expunge_mailbox(this.env.mailbox);
636         break;
637
5e3512 638       case 'purge':
T 639       case 'empty-mailbox':
640         if (this.env.messagecount)
641           this.purge_mailbox(this.env.mailbox);
4e17e6 642         break;
T 643
644
645       // common commands used in multiple tasks
646       case 'show':
647         if (this.task=='mail')
648           {
649           var uid = this.get_single_uid();
650           if (uid && (!this.env.uid || uid != this.env.uid))
651             this.show_message(uid);
652           }
653         else if (this.task=='addressbook')
654           {
655           var cid = props ? props : this.get_single_cid();
656           if (cid && !(this.env.action=='show' && cid==this.env.cid))
657             this.load_contact(cid, 'show');
658           }
659         break;
660
661       case 'add':
662         if (this.task=='addressbook')
d1d2c4 663           if (!window.frames[this.env.contentframe].rcmail)
S 664             this.load_contact(0, 'add');
665           else
666             {
667             if (window.frames[this.env.contentframe].rcmail.selection.length)
668               this.add_ldap_contacts();
669             else
670               this.load_contact(0, 'add');
671             }
4e17e6 672         else if (this.task=='settings')
T 673           {
674           this.clear_selection();
675           this.load_identity(0, 'add-identity');
676           }
677         break;
678
679       case 'edit':
680         var cid;
681         if (this.task=='addressbook' && (cid = this.get_single_cid()))
682           this.load_contact(cid, 'edit');
683         else if (this.task=='settings' && props)
684           this.load_identity(props, 'edit-identity');
685         break;
686
687       case 'save-identity':
688       case 'save':
689         if (this.gui_objects.editform)
10a699 690           {
T 691           var input_pagesize = rcube_find_object('_pagesize');
692           var input_name  = rcube_find_object('_name');
693           var input_email = rcube_find_object('_email');
694
695           // user prefs
e66f5b 696           if (input_pagesize && isNaN(input_pagesize.value))
10a699 697             {
T 698             alert(this.get_label('nopagesizewarning'));
699             input_pagesize.focus();
700             break;
701             }
702           // contacts/identities
703           else
704             {
705             if (input_name && input_name.value == '')
706               {
707               alert(this.get_label('nonamewarning'));
708               input_name.focus();
709               break;
710               }
711             else if (input_email && !rcube_check_email(input_email.value))
712               {
713               alert(this.get_label('noemailwarning'));
714               input_email.focus();
715               break;
716               }
717             }
718
4e17e6 719           this.gui_objects.editform.submit();
10a699 720           }
4e17e6 721         break;
T 722
723       case 'delete':
724         // mail task
725         if (this.task=='mail' && this.env.trash_mailbox && String(this.env.mailbox).toLowerCase()!=String(this.env.trash_mailbox).toLowerCase())
726           this.move_messages(this.env.trash_mailbox);
727         else if (this.task=='mail')
728           this.delete_messages();
729         // addressbook task
730         else if (this.task=='addressbook')
731           this.delete_contacts();
732         // user settings task
733         else if (this.task=='settings')
734           this.delete_identity();
735         break;
736
737
738       // mail task commands
739       case 'move':
740       case 'moveto':
741         this.move_messages(props);
742         break;
743         
744       case 'markread':
745         if (props && !props._row)
746           break;
747         
748         var uid;
749         var flag = 'read';
750         
751         if (props._row.uid)
752           {
753           uid = props._row.uid;
754           this.dont_select = true;
755           
756           // toggle read/unread
757           if (!this.message_rows[uid].unread)
758             flag = 'unread';
759           }
760           
761         this.mark_message(flag, uid);
762         break;
763         
764       case 'load-images':
765         if (this.env.uid)
766           this.show_message(this.env.uid, true);
767         break;
768
769       case 'load-attachment':
770         var url = this.env.comm_path+'&_action=get&_mbox='+this.env.mailbox+'&_uid='+this.env.uid+'&_part='+props.part;
771         
772         // open attachment in frame if it's of a supported mimetype
773         if (this.env.uid && props.mimetype && find_in_array(props.mimetype, this.mimetypes)>=0)
774           {
775           this.attachment_win = window.open(url+'&_frame=1', 'rcubemailattachment');
776           if (this.attachment_win)
777             {
778             setTimeout(this.ref+'.attachment_win.focus()', 10);
779             break;
780             }
781           }
782
783         location.href = url;
784         break;
785         
786       case 'select-all':
787         this.select_all(props);
788         break;
789
790       case 'select-none':
791         this.clear_selection();
792         break;
793
794       case 'nextmessage':
795         if (this.env.next_uid)
09941e 796           this.show_message(this.env.next_uid);
T 797           //location.href = this.env.comm_path+'&_action=show&_uid='+this.env.next_uid+'&_mbox='+this.env.mailbox;
4e17e6 798         break;
T 799
800       case 'previousmessage':
801         if (this.env.prev_uid)
09941e 802           this.show_message(this.env.prev_uid);
T 803           //location.href = this.env.comm_path+'&_action=show&_uid='+this.env.prev_uid+'&_mbox='+this.env.mailbox;
4e17e6 804         break;
d1d2c4 805       
S 806       
4e17e6 807       case 'compose':
T 808         var url = this.env.comm_path+'&_action=compose';
809         
810         // modify url if we're in addressbook
811         if (this.task=='addressbook')
812           {
813           url = this.get_task_url('mail', url);            
814           var a_cids = new Array();
815           
816           // use contact_id passed as command parameter
817           if (props)
818             a_cids[a_cids.length] = props;
819             
820           // get selected contacts
821           else
822             {
d1d2c4 823             if (!window.frames[this.env.contentframe].rcmail.selection.length)
S 824               {
825               for (var n=0; n<this.selection.length; n++)
826                 a_cids[a_cids.length] = this.selection[n];
827               }
828             else
829               {
830               var frameRcmail = window.frames[this.env.contentframe].rcmail;
831               // get the email address(es)
832               for (var n=0; n<frameRcmail.selection.length; n++)
833                 a_cids[a_cids.length] = frameRcmail.ldap_contact_rows[frameRcmail.selection[n]].obj.cells[1].innerHTML;
834               }
4e17e6 835             }
T 836           if (a_cids.length)
837             url += '&_to='+a_cids.join(',');
838           else
839             break;
d1d2c4 840             
4e17e6 841           }
T 842         else if (props)
843            url += '&_to='+props;
bc8dc6 844
d1d2c4 845         // don't know if this is necessary...
S 846         url = url.replace(/&_framed=1/, "");
4e17e6 847
T 848         this.set_busy(true);
d1d2c4 849
S 850         // need parent in case we are coming from the contact frame
bc8dc6 851         if (this.env.framed)
T 852           parent.location.href = url;
853         else
854           location.href = url;
d1d2c4 855         break;    
4e17e6 856
T 857       case 'send':
858         if (!this.gui_objects.messageform)
859           break;
860           
977a29 861         if (!this.check_compose_input())
10a699 862           break;
T 863
864         // all checks passed, send message
865         this.set_busy(true, 'sendingmessage');
866         var form = this.gui_objects.messageform;
867         form.submit();
4e17e6 868         break;
T 869
870       case 'add-attachment':
871         this.show_attachment_form(true);
872         
873       case 'send-attachment':
874         this.upload_file(props)      
875         break;
876
583f1c 877       case 'reply-all':
4e17e6 878       case 'reply':
T 879         var uid;
880         if (uid = this.get_single_uid())
881           {
882           this.set_busy(true);
583f1c 883           location.href = this.env.comm_path+'&_action=compose&_reply_uid='+uid+'&_mbox='+escape(this.env.mailbox)+(command=='reply-all' ? '&_all=1' : '');
4e17e6 884           }
T 885         break;      
886
887       case 'forward':
888         var uid;
889         if (uid = this.get_single_uid())
890           {
891           this.set_busy(true);
892           location.href = this.env.comm_path+'&_action=compose&_forward_uid='+uid+'&_mbox='+escape(this.env.mailbox);
893           }
894         break;
895         
896       case 'print':
897         var uid;
898         if (uid = this.get_single_uid())
899           {
900           this.printwin = window.open(this.env.comm_path+'&_action=print&_uid='+uid+'&_mbox='+escape(this.env.mailbox)+(this.env.safemode ? '&_safe=1' : ''));
901           if (this.printwin)
902             setTimeout(this.ref+'.printwin.focus()', 20);
903           }
904         break;
905
906       case 'viewsource':
907         var uid;
908         if (uid = this.get_single_uid())
909           {          
910           this.sourcewin = window.open(this.env.comm_path+'&_action=viewsource&_uid='+this.env.uid+'&_mbox='+escape(this.env.mailbox));
911           if (this.sourcewin)
912             setTimeout(this.ref+'.sourcewin.focus()', 20);
913           }
914         break;
915
916       case 'add-contact':
917         this.add_contact(props);
918         break;
d1d2c4 919       
4647e1 920       // mail quicksearch
T 921       case 'search':
922         if (!props && this.gui_objects.qsearchbox)
923           props = this.gui_objects.qsearchbox.value;
924         if (props)
925           this.qsearch(escape(props), this.env.mailbox);
926         break;
927
928       // reset quicksearch        
929       case 'reset-search':
930         var s = this.env.search_request;
931         this.reset_qsearch();
932         
933         if (s)
934           this.list_mailbox(this.env.mailbox);
935         break;
d1d2c4 936
S 937       // ldap search
938       case 'ldappublicsearch':
939         if (this.gui_objects.ldappublicsearchform) 
940           this.gui_objects.ldappublicsearchform.submit();
941         else 
942           this.ldappublicsearch(command);
943         break; 
4e17e6 944
T 945
946       // user settings commands
947       case 'preferences':
948         location.href = this.env.comm_path;
949         break;
950
951       case 'identities':
952         location.href = this.env.comm_path+'&_action=identities';
953         break;
954           
955       case 'delete-identity':
956         this.delete_identity();
957         
958       case 'folders':
959         location.href = this.env.comm_path+'&_action=folders';
960         break;
961
962       case 'subscribe':
963         this.subscribe_folder(props);
964         break;
965
966       case 'unsubscribe':
967         this.unsubscribe_folder(props);
968         break;
969         
970       case 'create-folder':
971         this.create_folder(props);
972         break;
973
974       case 'delete-folder':
1cded8 975         if (confirm(this.get_label('deletefolderconfirm')))
4e17e6 976           this.delete_folder(props);
T 977         break;
978
979       }
980
981     return obj ? false : true;
982     };
983
984
985   // set command enabled or disabled
986   this.enable_command = function()
987     {
988     var args = this.enable_command.arguments;
989     if(!args.length) return -1;
990
991     var command;
992     var enable = args[args.length-1];
993     
994     for(var n=0; n<args.length-1; n++)
995       {
996       command = args[n];
997       this.commands[command] = enable;
998       this.set_button(command, (enable ? 'act' : 'pas'));
999       }
1000     };
1001
1002
a95e0e 1003   // lock/unlock interface
4e17e6 1004   this.set_busy = function(a, message)
T 1005     {
1006     if (a && message)
10a699 1007       {
T 1008       var msg = this.get_label(message);
1009       if (msg==message)        
1010         msg = 'Loading...';
1011
1012       this.display_message(msg, 'loading', true);
1013       }
4e17e6 1014     else if (!a && this.busy)
T 1015       this.hide_message();
1016
1017     this.busy = a;
1018     //document.body.style.cursor = a ? 'wait' : 'default';
1019     
1020     if (this.gui_objects.editform)
1021       this.lock_form(this.gui_objects.editform, a);
a95e0e 1022       
T 1023     // clear pending timer
1024     if (this.request_timer)
1025       clearTimeout(this.request_timer);
1026
1027     // set timer for requests
1028     if (a && this.request_timeout)
1029       this.request_timer = setTimeout(this.ref+'.request_timed_out()', this.request_timeout);
4e17e6 1030     };
T 1031
1032
10a699 1033   // return a localized string
T 1034   this.get_label = function(name)
1035     {
1036     if (this.labels[name])
1037       return this.labels[name];
1038     else
1039       return name;
1040     };
1041
1042
1043   // switch to another application task
4e17e6 1044   this.switch_task = function(task)
T 1045     {
01bb03 1046     if (this.task===task && task!='mail')
4e17e6 1047       return;
T 1048
01bb03 1049     var url = this.get_task_url(task);
T 1050     if (task=='mail')
1051       url += '&_mbox=INBOX';
1052
4e17e6 1053     this.set_busy(true);
01bb03 1054     location.href = url;
4e17e6 1055     };
T 1056
1057
1058   this.get_task_url = function(task, url)
1059     {
1060     if (!url)
1061       url = this.env.comm_path;
1062
1063     return url.replace(/_task=[a-z]+/, '_task='+task);
a95e0e 1064     };
T 1065     
1066   
1067   // called when a request timed out
1068   this.request_timed_out = function()
1069     {
1070     this.set_busy(false);
1071     this.display_message('Request timed out!', 'error');
4e17e6 1072     };
T 1073
1074
1075   /*********************************************************/
1076   /*********        event handling methods         *********/
1077   /*********************************************************/
1078
1079
1080   // onmouseup handler for mailboxlist item
1081   this.mbox_mouse_up = function(mbox)
1082     {
1083     if (this.drag_active)
1084       this.command('moveto', mbox);
1085     else
1086       this.command('list', mbox);
1087       
1088     return false;
1089     };
1090
1091
1092   // onmousedown-handler of message list row
1093   this.drag_row = function(e, id)
1094     {
1095     this.in_selection_before = this.in_selection(id) ? id : false;
1096
1097     // don't do anything (another action processed before)
1098     if (this.dont_select)
1099       return false;
1100
b11a00 1101     // selects currently unselected row
4e17e6 1102     if (!this.in_selection_before)
b11a00 1103     {
d58c69 1104       var mod_key = this.get_modifier(e);
e8adc7 1105       this.select_row(id,mod_key,false);
b11a00 1106     }
4e17e6 1107     
T 1108     if (this.selection.length)
1109       {
1110       this.drag_start = true;
1111       document.onmousemove = function(e){ return rcube_webmail_client.drag_mouse_move(e); };
1112       document.onmouseup = function(e){ return rcube_webmail_client.drag_mouse_up(e); };
1113       }
1114
1115     return false;
1116     };
1117
1118
1119   // onmouseup-handler of message list row
1120   this.click_row = function(e, id)
1121     {
8c2e58 1122     var mod_key = this.get_modifier(e);
d58c69 1123
4e17e6 1124     // don't do anything (another action processed before)
T 1125     if (this.dont_select)
1126       {
1127       this.dont_select = false;
1128       return false;
1129       }
1130     
b11a00 1131     // unselects currently selected row    
8c2e58 1132     if (!this.drag_active && this.in_selection_before==id)
e8adc7 1133       this.select_row(id,mod_key,false);
8c2e58 1134
4e17e6 1135     this.drag_start = false;
T 1136     this.in_selection_before = false;
1137         
1138     // row was double clicked
8c2e58 1139     if (this.task=='mail' && this.list_rows && this.list_rows[id].clicked && !mod_key)
4e17e6 1140       {
T 1141       this.show_message(id);
1142       return false;
1143       }
1144     else if (this.task=='addressbook')
1145       {
d1d2c4 1146       if (this.contact_rows && this.selection.length==1)
S 1147         {
4e17e6 1148         this.load_contact(this.selection[0], 'show', true);
d1d2c4 1149         // change the text for the add contact button
S 1150         var links = parent.document.getElementById('abooktoolbar').getElementsByTagName('A');
1151         for (i = 0; i < links.length; i++)
1152           {
1153           var onclickstring = new String(links[i].onclick);
1154           if (onclickstring.search('\"add\"') != -1)
1155             links[i].title = this.env.newcontact;
1156           }
1157         }
1158       else if (this.contact_rows && this.contact_rows[id].clicked)
4e17e6 1159         {
T 1160         this.load_contact(id, 'show');
1161         return false;
1162         }
d1d2c4 1163       else if (this.ldap_contact_rows && !this.ldap_contact_rows[id].clicked)
S 1164         {
1165         // clear selection
1166         parent.rcmail.clear_selection();
1167
1168         // disable delete
1169         parent.rcmail.set_button('delete', 'pas');
1170
1171         // change the text for the add contact button
1172         var links = parent.document.getElementById('abooktoolbar').getElementsByTagName('A');
1173         for (i = 0; i < links.length; i++)
1174           {
1175           var onclickstring = new String(links[i].onclick);
1176           if (onclickstring.search('\"add\"') != -1)
1177             links[i].title = this.env.addcontact;
1178           }
1179         }
1180       // handle double click event
1181       else if (this.ldap_contact_rows && this.selection.length==1 && this.ldap_contact_rows[id].clicked)
1182         this.command('compose', this.ldap_contact_rows[id].obj.cells[1].innerHTML);
4e17e6 1183       else if (this.env.contentframe)
T 1184         {
1185         var elm = document.getElementById(this.env.contentframe);
1186         elm.style.visibility = 'hidden';
1187         }
1188       }
1189     else if (this.task=='settings')
1190       {
1191       if (this.selection.length==1)
1192         this.command('edit', this.selection[0]);
1193       }
1194
1195     this.list_rows[id].clicked = true;
1196     setTimeout(this.ref+'.list_rows['+id+'].clicked=false;', this.dblclick_time);
1197       
1198     return false;
1199     };
1200
1201
1202
1203   /*********************************************************/
1204   /*********     (message) list functionality      *********/
1205   /*********************************************************/
1206
e8adc7 1207   // get next and previous rows that are not hidden
S 1208   this.get_next_row = function(){
1209     var last_selected_row = this.list_rows[this.last_selected];
1210     var new_row = last_selected_row.obj.nextSibling;
1211     while (new_row && (new_row.nodeType != 1 || new_row.style.display == 'none')) {
1212       new_row = new_row.nextSibling;
1213     }
1214     return new_row;
1215   }
1216   
1217   this.get_prev_row = function(){
1218     var last_selected_row = this.list_rows[this.last_selected];
1219     var new_row = last_selected_row.obj.previousSibling;
1220     while (new_row && (new_row.nodeType != 1 || new_row.style.display == 'none')) {
1221       new_row = new_row.previousSibling;
1222     }
1223     return new_row;
1224   }
1225   
d58c69 1226   // highlight/unhighlight a row
S 1227   this.highlight_row = function(id, multiple)
4e17e6 1228     {
T 1229     var selected = false
1230     
1231     if (this.list_rows[id] && !multiple)
1232       {
1233       this.clear_selection();
1234       this.selection[0] = id;
1235       this.list_rows[id].obj.className += ' selected';
1236       selected = true;
1237       }
1238     
1239     else if (this.list_rows[id])
1240       {
1241       if (!this.in_selection(id))  // select row
1242         {
1243         this.selection[this.selection.length] = id;
e8adc7 1244         this.set_classname(this.list_rows[id].obj, 'selected', true);    
4e17e6 1245         }
T 1246       else  // unselect row
1247         {
1248         var p = find_in_array(id, this.selection);
1249         var a_pre = this.selection.slice(0, p);
1250         var a_post = this.selection.slice(p+1, this.selection.length);
1251         this.selection = a_pre.concat(a_post);
1252         this.set_classname(this.list_rows[id].obj, 'selected', false);
1253         }
1254       selected = (this.selection.length==1);
1255       }
1256
1257     // enable/disable commands for message
1258     if (this.task=='mail')
1259       {
583f1c 1260       this.enable_command('show', 'reply', 'reply-all', 'forward', 'print', selected);
4e17e6 1261       this.enable_command('delete', 'moveto', this.selection.length>0 ? true : false);
T 1262       }
1263     else if (this.task=='addressbook')
1264       {
1265       this.enable_command('edit', /*'print',*/ selected);
1266       this.enable_command('delete', 'compose', this.selection.length>0 ? true : false);
1267       }
1268     };
1269
b11a00 1270
d58c69 1271 // selects or unselects the proper row depending on the modifier key pressed
e8adc7 1272   this.select_row = function(id,mod_key,with_arrow)  { 
d58c69 1273       if (!mod_key) {
S 1274       this.shift_start = id;
1275         this.highlight_row(id, false);
1276     } else {
1277       switch (mod_key) {
1278         case SHIFT_KEY: { 
1279           this.shift_select(id,false); 
1280           break; }
1281         case CONTROL_KEY: { 
1282           this.shift_start = id;
e8adc7 1283           if (!with_arrow)
S 1284             this.highlight_row(id, true); 
d58c69 1285           break; 
S 1286           }
1287         case CONTROL_SHIFT_KEY: { 
1288           this.shift_select(id,true);
1289           break;
1290           }
1291         default: {
1292           this.highlight_row(id, false); 
1293           break;
1294           }
1295       }
1296     }
cbd62d 1297     if (this.last_selected != 0) { this.set_classname(this.list_rows[this.last_selected].obj, 'focused', false);}
e8adc7 1298     this.last_selected = id;
cbd62d 1299     this.set_classname(this.list_rows[id].obj, 'focused', true);        
d58c69 1300   };
S 1301
1302   this.shift_select = function(id, control) {
1303     var from_rowIndex = this.list_rows[this.shift_start].obj.rowIndex;
b11a00 1304     var to_rowIndex = this.list_rows[id].obj.rowIndex;
T 1305         
1306     var i = ((from_rowIndex < to_rowIndex)? from_rowIndex : to_rowIndex);
1307     var j = ((from_rowIndex > to_rowIndex)? from_rowIndex : to_rowIndex);
1308     
1309     // iterate through the entire message list
1310     for (var n in this.list_rows) {
1311       if ((this.list_rows[n].obj.rowIndex >= i) && (this.list_rows[n].obj.rowIndex <= j)) {
1312         if (!this.in_selection(n))
d58c69 1313           this.highlight_row(n, true);
b11a00 1314       } else {
T 1315         if  (this.in_selection(n) && !control)
d58c69 1316           this.highlight_row(n, true);
b11a00 1317       }
T 1318     }
1319   };
1320   
4e17e6 1321
T 1322   this.clear_selection = function()
1323     {
1324     for(var n=0; n<this.selection.length; n++)
1325       if (this.list_rows[this.selection[n]])
1326         this.set_classname(this.list_rows[this.selection[n]].obj, 'selected', false);
1327
1328     this.selection = new Array();    
1329     };
1330
1331
1332   // check if given id is part of the current selection
1333   this.in_selection = function(id)
1334     {
1335     for(var n in this.selection)
1336       if (this.selection[n]==id)
1337         return true;
1338
1339     return false;    
1340     };
1341
1342
1343   // select each row in list
1344   this.select_all = function(filter)
1345     {
1346     if (!this.list_rows || !this.list_rows.length)
1347       return false;
1348       
1349     // reset selection first
1350     this.clear_selection();
1351     
1352     for (var n in this.list_rows)
1353       if (!filter || this.list_rows[n][filter]==true)
2a1e18 1354       this.highlight_row(n, true);
4e17e6 1355     };
T 1356     
1357
1358   // when user doble-clicks on a row
1359   this.show_message = function(id, safe)
1360     {
1361     var add_url = '';
1362     var target = window;
1363     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
1364       {
1365       target = window.frames[this.env.contentframe];
1366       add_url = '&_framed=1';
1367       }
1368       
1369     if (safe)
1370       add_url = '&_safe=1';
1371
1372     if (id)
1373       {
09941e 1374       this.set_busy(true, 'loading');
4e17e6 1375       target.location.href = this.env.comm_path+'&_action=show&_uid='+id+'&_mbox='+escape(this.env.mailbox)+add_url;
T 1376       }
1377     };
1378
1379
1380
1381   // list a specific page
1382   this.list_page = function(page)
1383     {
1384     if (page=='next')
1385       page = this.env.current_page+1;
1386     if (page=='prev' && this.env.current_page>1)
1387       page = this.env.current_page-1;
1388       
1389     if (page > 0 && page <= this.env.pagecount)
1390       {
1391       this.env.current_page = page;
1392       
1393       if (this.task=='mail')
1394         this.list_mailbox(this.env.mailbox, page);
1395       else if (this.task=='addressbook')
1396         this.list_contacts(page);
1397       }
1398     };
1399
1400
1401   // list messages of a specific mailbox
f3b659 1402   this.list_mailbox = function(mbox, page, sort)
4e17e6 1403     {
cbd62d 1404     this.last_selected = 0;
4e17e6 1405     var add_url = '';
T 1406     var target = window;
1407
1408     if (!mbox)
1409       mbox = this.env.mailbox;
1410
f3b659 1411     // add sort to url if set
T 1412     if (sort)
1413       add_url += '&_sort=' + sort;
1414       
4e17e6 1415     // set page=1 if changeing to another mailbox
T 1416     if (!page && mbox != this.env.mailbox)
1417       {
1418       page = 1;
9fee0e 1419       add_url += '&_refresh=1';
4e17e6 1420       this.env.current_page = page;
T 1421       this.clear_selection();
1422       }
4647e1 1423     
T 1424     // also send search request to get the right messages
1425     if (this.env.search_request)
1426       add_url += '&_search='+this.env.search_request;
4e17e6 1427       
T 1428     if (this.env.mailbox!=mbox)
1429       this.select_mailbox(mbox);
1430
1431     // load message list remotely
1432     if (this.gui_objects.messagelist)
1433       {
9fee0e 1434       this.list_mailbox_remote(mbox, page, add_url);
4e17e6 1435       return;
T 1436       }
1437     
1438     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
1439       {
1440       target = window.frames[this.env.contentframe];
9fee0e 1441       add_url += '&_framed=1';
4e17e6 1442       }
T 1443
1444     // load message list to target frame/window
1445     if (mbox)
1446       {
1447       this.set_busy(true, 'loading');
1448       target.location.href = this.env.comm_path+'&_mbox='+escape(mbox)+(page ? '&_page='+page : '')+add_url;
1449       }
1450     };
1451
1452
1453   // send remote request to load message list
9fee0e 1454   this.list_mailbox_remote = function(mbox, page, add_url)
4e17e6 1455     {
15a9d1 1456     // clear message list first
T 1457     this.clear_message_list();
1458
1459     // send request to server
1460     var url = '_mbox='+escape(mbox)+(page ? '&_page='+page : '');
1461     this.set_busy(true, 'loading');
1462     this.http_request('list', url+add_url, true);
1463     };
1464
1465
1466   this.clear_message_list = function()
1467     {
4e17e6 1468     var table = this.gui_objects.messagelist;
T 1469     var tbody = document.createElement('TBODY');
1470     table.insertBefore(tbody, table.tBodies[0]);
1471     table.removeChild(table.tBodies[1]);
1472     
1473     this.message_rows = new Array();
1474     this.list_rows = this.message_rows;
15a9d1 1475     
T 1476     };
1477
1478
1479   this.expunge_mailbox = function(mbox)
1480     {
1481     var lock = false;
1482     var add_url = '';
1483     
1484     // lock interface if it's the active mailbox
1485     if (mbox == this.env.mailbox)
1486        {
1487        lock = true;
1488        this.set_busy(true, 'loading');
1489        add_url = '&_reload=1';
1490        }
4e17e6 1491
T 1492     // send request to server
15a9d1 1493     var url = '_mbox='+escape(mbox);
T 1494     this.http_request('expunge', url+add_url, lock);
4e17e6 1495     };
T 1496
1497
5e3512 1498   this.purge_mailbox = function(mbox)
T 1499     {
1500     var lock = false;
1501     var add_url = '';
1502     
1503     if (!confirm(this.get_label('purgefolderconfirm')))
1504       return false;
1505     
1506     // lock interface if it's the active mailbox
1507     if (mbox == this.env.mailbox)
1508        {
1509        lock = true;
1510        this.set_busy(true, 'loading');
1511        add_url = '&_reload=1';
1512        }
1513
1514     // send request to server
1515     var url = '_mbox='+escape(mbox);
1516     this.http_request('purge', url+add_url, lock);
1517     };
1518     
1519
4e17e6 1520   // move selected messages to the specified mailbox
T 1521   this.move_messages = function(mbox)
1522     {
1523     // exit if no mailbox specified or if selection is empty
1524     if (!mbox || !(this.selection.length || this.env.uid) || mbox==this.env.mailbox)
1525       return;
1526     
1527     var a_uids = new Array();
1528
1529     if (this.env.uid)
1530       a_uids[a_uids.length] = this.env.uid;
1531     else
1532       {
1533       var id;
1534       for (var n=0; n<this.selection.length; n++)
1535         {
1536         id = this.selection[n];
1537         a_uids[a_uids.length] = id;
1538       
1539         // 'remove' message row from list (just hide it)
1540         if (this.message_rows[id].obj)
1541           this.message_rows[id].obj.style.display = 'none';
1542         }
cbd62d 1543       next_row = this.get_next_row();
S 1544       prev_row = this.get_prev_row();
1545       new_row = (next_row) ? next_row : prev_row;
1546       this.select_row(new_row.uid,false,false);
4e17e6 1547       }
ecf759 1548       
T 1549     var lock = false;
4e17e6 1550
T 1551     // show wait message
1552     if (this.env.action=='show')
ecf759 1553       {
T 1554       lock = true;
4e17e6 1555       this.set_busy(true, 'movingmessage');
ecf759 1556       }
4e17e6 1557
T 1558     // send request to server
ecf759 1559     this.http_request('moveto', '_uid='+a_uids.join(',')+'&_mbox='+escape(this.env.mailbox)+'&_target_mbox='+escape(mbox)+'&_from='+(this.env.action ? this.env.action : ''), lock);
4e17e6 1560     };
T 1561
1562
1563   // delete selected messages from the current mailbox
1564   this.delete_messages = function()
1565     {
1566     // exit if no mailbox specified or if selection is empty
1567     if (!(this.selection.length || this.env.uid))
1568       return;
1569     
1570     var a_uids = new Array();
1571
1572     if (this.env.uid)
1573       a_uids[a_uids.length] = this.env.uid;
1574     else
1575       {
1576       var id;
1577       for (var n=0; n<this.selection.length; n++)
1578         {
1579         id = this.selection[n];
1580         a_uids[a_uids.length] = id;
1581       
1582         // 'remove' message row from list (just hide it)
1583         if (this.message_rows[id].obj)
1584           this.message_rows[id].obj.style.display = 'none';
1585         }
1586       }
cbd62d 1587       next_row = this.get_next_row();
S 1588       prev_row = this.get_prev_row();
1589       new_row = (next_row) ? next_row : prev_row;
1590       this.select_row(new_row.uid,false,false);
4e17e6 1591
T 1592     // send request to server
1593     this.http_request('delete', '_uid='+a_uids.join(',')+'&_mbox='+escape(this.env.mailbox)+'&_from='+(this.env.action ? this.env.action : ''));
1594     };
1595
1596
1597   // set a specific flag to one or more messages
1598   this.mark_message = function(flag, uid)
1599     {
1600     var a_uids = new Array();
1601     
1602     if (uid)
1603       a_uids[0] = uid;
1604     else if (this.env.uid)
1605       a_uids[0] = this.env.uid;
1606     else
1607       {
1608       var id;
1609       for (var n=0; n<this.selection.length; n++)
1610         {
1611         id = this.selection[n];
1612         a_uids[a_uids.length] = id;
1613       
1614         // 'remove' message row from list (just hide it)
1615         if (this.message_rows[id].obj)
1616           this.message_rows[id].obj.style.display = 'none';
1617         }
1618       }
1619
1620     // mark all message rows as read/unread
1621     var icn_src;
1622     for (var i=0; i<a_uids.length; i++)
1623       {
1624       uid = a_uids[i];
1625       if (this.message_rows[uid])
1626         {
1627         this.message_rows[uid].unread = (flag=='unread' ? true : false);
1628         
1629         if (this.message_rows[uid].classname.indexOf('unread')<0 && this.message_rows[uid].unread)
1630           {
1631           this.message_rows[uid].classname += ' unread';
fd660a 1632           this.set_classname(this.message_rows[uid].obj, 'unread', true);
T 1633
4e17e6 1634           if (this.env.unreadicon)
T 1635             icn_src = this.env.unreadicon;
1636           }
1637         else if (!this.message_rows[uid].unread)
1638           {
1639           this.message_rows[uid].classname = this.message_rows[uid].classname.replace(/\s*unread/, '');
fd660a 1640           this.set_classname(this.message_rows[uid].obj, 'unread', false);
4e17e6 1641
T 1642           if (this.message_rows[uid].replied && this.env.repliedicon)
1643             icn_src = this.env.repliedicon;
1644           else if (this.env.messageicon)
1645             icn_src = this.env.messageicon;
1646           }
1647
1648         if (this.message_rows[uid].icon && icn_src)
1649           this.message_rows[uid].icon.src = icn_src;
1650         }
1651       }
1652
1653     // send request to server
1654     this.http_request('mark', '_uid='+a_uids.join(',')+'&_flag='+flag);
1655     };
1656
1657
1658
1659   /*********************************************************/
1660   /*********        message compose methods        *********/
1661   /*********************************************************/
1cded8 1662   
T 1663   
977a29 1664   // checks the input fields before sending a message
T 1665   this.check_compose_input = function()
1666     {
1667     // check input fields
1668     var input_to = rcube_find_object('_to');
1669     var input_subject = rcube_find_object('_subject');
1670     var input_message = rcube_find_object('_message');
1671
1672     // check for empty recipient
1673     if (input_to && !rcube_check_email(input_to.value, true))
1674       {
1675       alert(this.get_label('norecipientwarning'));
1676       input_to.focus();
1677       return false;
1678       }
1679
1680     // display localized warning for missing subject
1681     if (input_subject && input_subject.value == '')
1682       {
1683       var subject = prompt(this.get_label('nosubjectwarning'), this.get_label('nosubject'));
1684
1685       // user hit cancel, so don't send
1686       if (!subject && subject !== '')
1687         {
1688         input_subject.focus();
1689         return false;
1690         }
1691       else
1692         {
1693         input_subject.value = subject ? subject : this.get_label('nosubject');            
1694         }
1695       }
1696
1697     // check for empty body
1698     if (input_message.value=='')
1699       {
1700       if (!confirm(this.get_label('nobodywarning')))
1701         {
1702         input_message.focus();
1703         return false;
1704         }
1705       }
1706
1707     return true;
1708     };
1709     
1710     
1711   this.compose_field_hash = function()
1712     {
1713     // check input fields
1714     var input_to = rcube_find_object('_to');
1715     var input_cc = rcube_find_object('_to');
1716     var input_bcc = rcube_find_object('_to');
1717     var input_subject = rcube_find_object('_subject');
1718     var input_message = rcube_find_object('_message');
1719     
1720     var str = '';
1721     if (input_to && input_to.value)
1722       str += input_to.value+':';
1723     if (input_cc && input_cc.value)
1724       str += input_cc.value+':';
1725     if (input_bcc && input_bcc.value)
1726       str += input_bcc.value+':';
1727     if (input_subject && input_subject.value)
1728       str += input_subject.value+':';
1729     if (input_message && input_message.value)
1730       str += input_message.value;
1731
1732     return str;
1733     };
1734     
1735   
1cded8 1736   this.change_identity = function(obj)
T 1737     {
1738     if (!obj || !obj.options)
1739       return false;
1740
1741     var id = obj.options[obj.selectedIndex].value;
1742     var input_message = rcube_find_object('_message');
1743     var message = input_message ? input_message.value : '';
749b07 1744     var sig, p;
1cded8 1745
T 1746     // remove the 'old' signature
1747     if (this.env.identity && this.env.signatures && this.env.signatures[this.env.identity])
1748       {
749b07 1749       sig = this.env.signatures[this.env.identity];
T 1750       if (sig.indexOf('-- ')!=0)
1751         sig = '-- \n'+sig;
1752
1753       p = message.lastIndexOf(sig);
1754       if (p>=0)
1cded8 1755         message = message.substring(0, p-1) + message.substring(p+sig.length, message.length);
T 1756       }
1757
1758     // add the new signature string
1759     if (this.env.signatures && this.env.signatures[id])
1760       {
749b07 1761       sig = this.env.signatures[id];
T 1762       if (sig.indexOf('-- ')!=0)
1763         sig = '-- \n'+sig;
1cded8 1764       message += '\n'+sig;
T 1765       }
1766
749b07 1767     if (input_message)
1cded8 1768       input_message.value = message;
T 1769       
1770     this.env.identity = id;
1771     };
4e17e6 1772
T 1773
1774   this.show_attachment_form = function(a)
1775     {
1776     if (!this.gui_objects.uploadbox)
1777       return false;
1778       
1779     var elm, list;
1780     if (elm = this.gui_objects.uploadbox)
1781       {
1782       if (a &&  (list = this.gui_objects.attachmentlist))
1783         {
1784         var pos = rcube_get_object_pos(list);
1785         var left = pos.x;
1786         var top = pos.y + list.offsetHeight + 10;
1787       
1788         elm.style.top = top+'px';
1789         elm.style.left = left+'px';
1790         }
1791       
1792       elm.style.visibility = a ? 'visible' : 'hidden';
1793       }
1794       
1795     // clear upload form
1796     if (!a && this.gui_objects.attachmentform && this.gui_objects.attachmentform!=this.gui_objects.messageform)
1797       this.gui_objects.attachmentform.reset();
1798     };
1799
1800
1801   // upload attachment file
1802   this.upload_file = function(form)
1803     {
1804     if (!form)
1805       return false;
1806       
1807     // get file input fields
1808     var send = false;
1809     for (var n=0; n<form.elements.length; n++)
1810       if (form.elements[n].type=='file' && form.elements[n].value)
1811         {
1812         send = true;
1813         break;
1814         }
1815     
1816     // create hidden iframe and post upload form
1817     if (send)
1818       {
1819       var ts = new Date().getTime();
1820       var frame_name = 'rcmupload'+ts;
1821
1822       // have to do it this way for IE
1823       // otherwise the form will be posted to a new window
1824       if(document.all && !window.opera)
1825         {
1826         var html = '<iframe name="'+frame_name+'" src="program/blank.gif" style="width:0;height:0;visibility:hidden;"></iframe>';
1827         document.body.insertAdjacentHTML('BeforeEnd',html);
1828         }
1829       else  // for standards-compilant browsers
1830         {
1831         var frame = document.createElement('IFRAME');
1832         frame.name = frame_name;
1833         frame.width = 10;
1834         frame.height = 10;
1835         frame.style.visibility = 'hidden';
1836         document.body.appendChild(frame);
1837         }
1838
1839       form.target = frame_name;
1840       form.action = this.env.comm_path+'&_action=upload';
1841       form.setAttribute('enctype', 'multipart/form-data');
1842       form.submit();
1843       }
1844     
1845     // set reference to the form object
1846     this.gui_objects.attachmentform = form;
1847     };
1848
1849
1850   // add file name to attachment list
1851   // called from upload page
1852   this.add2attachment_list = function(name)
1853     {
1854     if (!this.gui_objects.attachmentlist)
1855       return false;
1856       
1857     var li = document.createElement('LI');
1858     li.innerHTML = name;
1859     this.gui_objects.attachmentlist.appendChild(li);
1860     };
1861
1862
1863   // send remote request to add a new contact
1864   this.add_contact = function(value)
1865     {
1866     if (value)
1867       this.http_request('addcontact', '_address='+value);
1868     };
1869
4647e1 1870   // send remote request to search mail
T 1871   this.qsearch = function(value, mbox)
1872     {
1873     if (value && mbox)
1874       {
1875       this.clear_message_list();
1876       this.set_busy(true, 'searching');
1877       this.http_request('search', '_search='+value+'&_mbox='+mbox, true);
1878       }
1879     };
1880
1881   // reset quick-search form
1882   this.reset_qsearch = function()
1883     {
1884     if (this.gui_objects.qsearchbox)
1885       this.gui_objects.qsearchbox.value = '';
1886       
1887     this.env.search_request = null;
1888     };
1889     
4e17e6 1890
T 1891   /*********************************************************/
1892   /*********     keyboard live-search methods      *********/
1893   /*********************************************************/
1894
1895
1896   // handler for keyboard events on address-fields
1897   this.ksearch_keypress = function(e, obj)
1898     {
1899     if (typeof(this.env.contacts)!='object' || !this.env.contacts.length)
1900       return true;
1901
1902     if (this.ksearch_timer)
1903       clearTimeout(this.ksearch_timer);
1904
1905     if (!e)
1906       e = window.event;
1907       
1908     var highlight;
1909     var key = e.keyCode ? e.keyCode : e.which;
1910
1911     switch (key)
1912       {
1913       case 38:  // key up
1914       case 40:  // key down
1915         if (!this.ksearch_pane)
1916           break;
1917           
1918         var dir = key==38 ? 1 : 0;
1919         var next;
1920         
1921         highlight = document.getElementById('rcmksearchSelected');
1922         if (!highlight)
1923           highlight = this.ksearch_pane.ul.firstChild;
1924         
1925         if (highlight && (next = dir ? highlight.previousSibling : highlight.nextSibling))
1926           {
1927           highlight.removeAttribute('id');
1928           //highlight.removeAttribute('class');
1929           this.set_classname(highlight, 'selected', false);
1930           }
1931
1932         if (next)
1933           {
1934           next.setAttribute('id', 'rcmksearchSelected');
1935           this.set_classname(next, 'selected', true);
1936           this.ksearch_selected = next._rcm_id;
1937           }
1938
1939         if (e.preventDefault)
1940           e.preventDefault();
1941         return false;
1942
1943       case 9:  // tab
1944         if(e.shiftKey)
1945           break;
1946
1947       case 13:  // enter     
1948         if (this.ksearch_selected===null || !this.ksearch_input || !this.ksearch_value)
1949           break;
1950
1951         // get cursor pos
1952         var inp_value = this.ksearch_input.value.toLowerCase();
1953         var cpos = this.get_caret_pos(this.ksearch_input);
1954         var p = inp_value.lastIndexOf(this.ksearch_value, cpos);
1955         
1956         // replace search string with full address
1957         var pre = this.ksearch_input.value.substring(0, p);
1958         var end = this.ksearch_input.value.substring(p+this.ksearch_value.length, this.ksearch_input.value.length);
1959         var insert = this.env.contacts[this.ksearch_selected]+', ';
1960         this.ksearch_input.value = pre + insert + end;
1961         
1962         //this.ksearch_input.value = this.ksearch_input.value.substring(0, p)+insert;
1963         
1964         // set caret to insert pos
1965         cpos = p+insert.length;
1966         if (this.ksearch_input.setSelectionRange)
1967           this.ksearch_input.setSelectionRange(cpos, cpos);
1968         
1969         // hide ksearch pane
1970         this.ksearch_hide();
1971       
1972         if (e.preventDefault)
1973           e.preventDefault();
1974         return false;
1975
1976       case 27:  // escape
1977         this.ksearch_hide();
1978         break;
1979
1980       }
1981
1982     // start timer
1983     this.ksearch_timer = setTimeout(this.ref+'.ksearch_get_results()', 200);      
1984     this.ksearch_input = obj;
1985     
1986     return true;
1987     };
1988
1989
1990   // address search processor
1991   this.ksearch_get_results = function()
1992     {
1993     var inp_value = this.ksearch_input ? this.ksearch_input.value : null;
1994     if (inp_value===null)
1995       return;
1996
1997     // get string from current cursor pos to last comma
1998     var cpos = this.get_caret_pos(this.ksearch_input);
1999     var p = inp_value.lastIndexOf(',', cpos-1);
2000     var q = inp_value.substring(p+1, cpos);
2001
2002     // trim query string
2003     q = q.replace(/(^\s+|\s+$)/g, '').toLowerCase();
2004
2005     if (!q.length || q==this.ksearch_value)
2006       {
2007       if (!q.length && this.ksearch_pane && this.ksearch_pane.visible)
2008         this.ksearch_pane.show(0);
2009
2010       return;
2011       }
2012
2013     this.ksearch_value = q;
2014     
2015     // start searching the contact list
2016     var a_results = new Array();
2017     var a_result_ids = new Array();
2018     var c=0;
2019     for (var i=0; i<this.env.contacts.length; i++)
2020       {
2021       if (this.env.contacts[i].toLowerCase().indexOf(q)>=0)
2022         {
2023         a_results[c] = this.env.contacts[i];
2024         a_result_ids[c++] = i;
2025         
2026         if (c==15)  // limit search results
2027           break;
2028         }
2029       }
2030
2031     // display search results
2032     if (c && a_results.length)
2033       {
2034       var p, ul, li;
2035       
2036       // create results pane if not present
2037       if (!this.ksearch_pane)
2038         {
2039         ul = document.createElement('UL');
2040         this.ksearch_pane = new rcube_layer('rcmKSearchpane', {vis:0, zindex:30000});
2041         this.ksearch_pane.elm.appendChild(ul);
2042         this.ksearch_pane.ul = ul;
2043         }
2044       else
2045         ul = this.ksearch_pane.ul;
2046
2047       // remove all search results
2048       ul.innerHTML = '';
2049             
2050       // add each result line to list
2051       for (i=0; i<a_results.length; i++)
2052         {
2053         li = document.createElement('LI');
2054         li.innerHTML = a_results[i].replace(/</, '&lt;').replace(/>/, '&gt;');
2055         li._rcm_id = a_result_ids[i];
2056         ul.appendChild(li);
2057         }
2058
2059       // check if last selected item is still in result list
2060       if (this.ksearch_selected!==null)
2061         {
2062         p = find_in_array(this.ksearch_selected, a_result_ids);
2063         if (p>=0 && ul.childNodes)
2064           {
2065           ul.childNodes[p].setAttribute('id', 'rcmksearchSelected');
2066           this.set_classname(ul.childNodes[p], 'selected', true);
2067           }
2068         else
2069           this.ksearch_selected = null;
2070         }
2071       
2072       // if no item selected, select the first one
2073       if (this.ksearch_selected===null)
2074         {
2075         ul.firstChild.setAttribute('id', 'rcmksearchSelected');
2076         this.set_classname(ul.firstChild, 'selected', true);
2077         this.ksearch_selected = a_result_ids[0];
2078         }
2079
2080       // resize the containing layer to fit the list
2081       //this.ksearch_pane.resize(ul.offsetWidth, ul.offsetHeight);
2082     
2083       // move the results pane right under the input box and make it visible
2084       var pos = rcube_get_object_pos(this.ksearch_input);
2085       this.ksearch_pane.move(pos.x, pos.y+this.ksearch_input.offsetHeight);
2086       this.ksearch_pane.show(1); 
2087       }
2088     // hide results pane
2089     else
2090       this.ksearch_hide();
2091     };
2092
2093
2094   this.ksearch_blur = function(e, obj)
2095     {
2096     if (this.ksearch_timer)
2097       clearTimeout(this.ksearch_timer);
2098
2099     this.ksearch_value = '';      
2100     this.ksearch_input = null;
2101     
2102     this.ksearch_hide();
2103     };
2104
2105
2106   this.ksearch_hide = function()
2107     {
2108     this.ksearch_selected = null;
2109     
2110     if (this.ksearch_pane)
2111       this.ksearch_pane.show(0);    
2112     };
2113
2114
2115
2116   /*********************************************************/
2117   /*********         address book methods          *********/
2118   /*********************************************************/
2119
2120
2121   this.list_contacts = function(page)
2122     {
2123     var add_url = '';
2124     var target = window;
2125     
2126     if (page && this.current_page==page)
2127       return false;
2128
2129     // load contacts remotely
2130     if (this.gui_objects.contactslist)
2131       {
2132       this.list_contacts_remote(page);
2133       return;
2134       }
2135
2136     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
2137       {
2138       target = window.frames[this.env.contentframe];
2139       add_url = '&_framed=1';
2140       }
2141
2142     this.set_busy(true, 'loading');
2143     location.href = this.env.comm_path+(page ? '&_page='+page : '')+add_url;
2144     };
2145
2146
2147   // send remote request to load contacts list
2148   this.list_contacts_remote = function(page)
2149     {
2150     // clear list
2151     var table = this.gui_objects.contactslist;
2152     var tbody = document.createElement('TBODY');
2153     table.insertBefore(tbody, table.tBodies[0]);
2154     table.tBodies[1].style.display = 'none';
2155     
2156     this.contact_rows = new Array();
2157     this.list_rows = this.contact_rows;
2158
2159     // send request to server
2160     var url = page ? '&_page='+page : '';
2161     this.set_busy(true, 'loading');
ecf759 2162     this.http_request('list', url, true);
4e17e6 2163     };
T 2164
2165
2166   // load contact record
2167   this.load_contact = function(cid, action, framed)
2168     {
2169     var add_url = '';
2170     var target = window;
2171     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
2172       {
2173       add_url = '&_framed=1';
2174       target = window.frames[this.env.contentframe];
2175       document.getElementById(this.env.contentframe).style.visibility = 'inherit';
2176       }
2177     else if (framed)
2178       return false;
2179       
2180     //if (this.env.framed && add_url=='')
5e3512 2181     
4e17e6 2182     //  add_url = '&_framed=1';
T 2183     
2184     if (action && (cid || action=='add'))
2185       {
2186       this.set_busy(true);
2187       target.location.href = this.env.comm_path+'&_action='+action+'&_cid='+cid+add_url;
2188       }
2189     };
2190
2191
2192   this.delete_contacts = function()
2193     {
2194     // exit if no mailbox specified or if selection is empty
5e3512 2195     if (!(this.selection.length || this.env.cid) || !confirm(this.get_label('deletecontactconfirm')))
4e17e6 2196       return;
5e3512 2197       
4e17e6 2198     var a_cids = new Array();
T 2199
2200     if (this.env.cid)
2201       a_cids[a_cids.length] = this.env.cid;
2202     else
2203       {
2204       var id;
2205       for (var n=0; n<this.selection.length; n++)
2206         {
2207         id = this.selection[n];
2208         a_cids[a_cids.length] = id;
2209       
2210         // 'remove' row from list (just hide it)
2211         if (this.contact_rows[id].obj)
2212           this.contact_rows[id].obj.style.display = 'none';
2213         }
2214
2215       // hide content frame if we delete the currently displayed contact
2216       if (this.selection.length==1 && this.env.contentframe)
2217         {
2218         var elm = document.getElementById(this.env.contentframe);
2219         elm.style.visibility = 'hidden';
2220         }
2221       }
2222
2223     // send request to server
2224     this.http_request('delete', '_cid='+a_cids.join(',')+'&_from='+(this.env.action ? this.env.action : ''));
2225     };
2226
2227
2228   // update a contact record in the list
2229   this.update_contact_row = function(cid, cols_arr)
2230     {
2231     if (!this.contact_rows[cid] || !this.contact_rows[cid].obj)
2232       return false;
2233       
2234     var row = this.contact_rows[cid].obj;
2235     for (var c=0; c<cols_arr.length; c++)
2236       if (row.cells[c])
2237         row.cells[c].innerHTML = cols_arr[c];
2238
2239     };
2240   
d1d2c4 2241   
S 2242   // load ldap search form
2243   this.ldappublicsearch = function(action)
2244     {
2245     var add_url = '';
2246     var target = window;
2247     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
2248       {
2249       add_url = '&_framed=1';
2250       target = window.frames[this.env.contentframe];
2251       document.getElementById(this.env.contentframe).style.visibility = 'inherit';
2252       }
2253     else
2254       return false; 
2255
2256
2257     if (action == 'ldappublicsearch')
2258       target.location.href = this.env.comm_path+'&_action='+action+add_url;
2259     };
2260  
2261   // add ldap contacts to address book
2262   this.add_ldap_contacts = function()
2263     {
2264     if (window.frames[this.env.contentframe].rcmail)
2265       {
2266       var frame = window.frames[this.env.contentframe];
2267
2268       // build the url
2269       var url    = '&_framed=1';
2270       var emails = '&_emails=';
2271       var names  = '&_names=';
2272       var end    = '';
2273       for (var n=0; n<frame.rcmail.selection.length; n++)
2274         {
2275         end = n < frame.rcmail.selection.length - 1 ? ',' : '';
2276         emails += frame.rcmail.ldap_contact_rows[frame.rcmail.selection[n]].obj.cells[1].innerHTML + end;
2277         names  += frame.rcmail.ldap_contact_rows[frame.rcmail.selection[n]].obj.cells[0].innerHTML + end;
2278         }
2279        
2280       frame.location.href = this.env.comm_path + '&_action=save&_framed=1' + emails + names;
2281       }
2282     return false;
2283     }
2284   
4e17e6 2285
T 2286
2287   /*********************************************************/
2288   /*********        user settings methods          *********/
2289   /*********************************************************/
2290
2291
2292   // load contact record
2293   this.load_identity = function(id, action)
2294     {
2295     if (action=='edit-identity' && (!id || id==this.env.iid))
2296       return;
2297
2298     var add_url = '';
2299     var target = window;
2300     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
2301       {
2302       add_url = '&_framed=1';
2303       target = window.frames[this.env.contentframe];
2304       document.getElementById(this.env.contentframe).style.visibility = 'inherit';
2305       }
2306
2307     if (action && (id || action=='add-identity'))
2308       {
2309       this.set_busy(true);
2310       target.location.href = this.env.comm_path+'&_action='+action+'&_iid='+id+add_url;
2311       }
2312     };
2313
2314
2315
2316   this.delete_identity = function(id)
2317     {
2318     // exit if no mailbox specified or if selection is empty
2319     if (!(this.selection.length || this.env.iid))
2320       return;
2321     
2322     if (!id)
2323       id = this.env.iid ? this.env.iid : this.selection[0];
2324
2325 /*
2326     // 'remove' row from list (just hide it)
2327     if (this.identity_rows && this.identity_rows[id].obj)
2328       {
2329       this.clear_selection();
2330       this.identity_rows[id].obj.style.display = 'none';
2331       }
2332 */
2333
2334     // if (this.env.framed && id)
2335       this.set_busy(true);
2336       location.href = this.env.comm_path+'&_action=delete-identity&_iid='+id;
2337     // else if (id)
2338     //  this.http_request('delete-identity', '_iid='+id);
2339     };
2340
2341
2342   this.create_folder = function(name)
2343     {
2344     var form;
2345     if ((form = this.gui_objects.editform) && form.elements['_folder_name'])
2346       name = form.elements['_folder_name'].value;
2347
2348     if (name)
ecf759 2349       this.http_request('create-folder', '_name='+escape(name), true);
4e17e6 2350     else if (form.elements['_folder_name'])
T 2351       form.elements['_folder_name'].focus();
2352     };
2353
2354
2355   this.delete_folder = function(folder)
2356     {
2357     if (folder)
2358       {
2359       this.http_request('delete-folder', '_mboxes='+escape(folder));
2360       }
1cded8 2361     };
T 2362
2363
2364   this.remove_folder_row = function(folder)
2365     {
2366     for (var id in this.env.subscriptionrows)
2367       if (this.env.subscriptionrows[id]==folder)
2368         break;
2369
2370     var row;
2371     if (id && (row = document.getElementById(id)))
2372       row.style.display = 'none';    
4e17e6 2373     };
T 2374
2375
2376   this.subscribe_folder = function(folder)
2377     {
2378     var form;
2379     if ((form = this.gui_objects.editform) && form.elements['_unsubscribed'])
2380       this.change_subscription('_unsubscribed', '_subscribed', 'subscribe');
2381     else if (folder)
2382       this.http_request('subscribe', '_mboxes='+escape(folder));
2383     };
2384
2385
2386   this.unsubscribe_folder = function(folder)
2387     {
2388     var form;
2389     if ((form = this.gui_objects.editform) && form.elements['_subscribed'])
2390       this.change_subscription('_subscribed', '_unsubscribed', 'unsubscribe');
2391     else if (folder)
2392       this.http_request('unsubscribe', '_mboxes='+escape(folder));
2393     };
2394     
2395
2396   this.change_subscription = function(from, to, action)
2397     {
2398     var form;
2399     if (form = this.gui_objects.editform)
2400       {
2401       var a_folders = new Array();
2402       var list_from = form.elements[from];
2403
2404       for (var i=0; list_from && i<list_from.options.length; i++)
2405         {
2406         if (list_from.options[i] && list_from.options[i].selected)
2407           {
2408           a_folders[a_folders.length] = list_from.options[i].value;
2409           list_from[i] = null;
2410           i--;
2411           }
2412         }
2413
2414       // yes, we have some folders selected
2415       if (a_folders.length)
2416         {
2417         var list_to = form.elements[to];
2418         var index;
2419         
2420         for (var n=0; n<a_folders.length; n++)
2421           {
2422           index = list_to.options.length;
2423           list_to[index] = new Option(a_folders[n]);
2424           }
2425           
2426         this.http_request(action, '_mboxes='+escape(a_folders.join(',')));
2427         }
2428       }
2429       
2430     };
2431
2432
2433    // add a new folder to the subscription list by cloning a folder row
2434    this.add_folder_row = function(name)
2435      {
2436      if (!this.gui_objects.subscriptionlist)
2437        return false;
2438
2439      var tbody = this.gui_objects.subscriptionlist.tBodies[0];
2440      var id = tbody.childNodes.length+1;
2441      
2442      // clone a table row
2443      var row = this.clone_table_row(tbody.rows[0]);
2444      row.id = 'rcmrow'+id;
2445      tbody.appendChild(row);
2446
2447      // add to folder/row-ID map
2448      this.env.subscriptionrows[row.id] = name;
2449
2450      // set folder name
2451      row.cells[0].innerHTML = name;
2452      if (row.cells[1].firstChild.tagName=='INPUT')
2453        {
2454        row.cells[1].firstChild.value = name;
2455        row.cells[1].firstChild.checked = true;
2456        }
2457      if (row.cells[2].firstChild.tagName=='A')
2458        row.cells[2].firstChild.onclick = new Function(this.ref+".command('delete-folder','"+name+"')");
14eafe 2459
T 2460     var form;
2461     if ((form = this.gui_objects.editform) && form.elements['_folder_name'])
2462       form.elements['_folder_name'].value = '';
4e17e6 2463      };
T 2464
2465
2466   // duplicate a specific table row
2467   this.clone_table_row = function(row)
2468     {
2469     var cell, td;
2470     var new_row = document.createElement('TR');
2471     for(var n=0; n<row.childNodes.length; n++)
2472       {
2473       cell = row.childNodes[n];
2474       td = document.createElement('TD');
2475
2476       if (cell.className)
2477         td.className = cell.className;
2478       if (cell.align)
2479         td.setAttribute('align', cell.align);
2480         
2481       td.innerHTML = cell.innerHTML;
2482       new_row.appendChild(td);
2483       }
2484     
2485     return new_row;
2486     };
2487
2488
2489   /*********************************************************/
2490   /*********           GUI functionality           *********/
2491   /*********************************************************/
2492
2493
2494   // eable/disable buttons for page shifting
2495   this.set_page_buttons = function()
2496     {
2497     this.enable_command('nextpage', (this.env.pagecount > this.env.current_page));
2498     this.enable_command('previouspage', (this.env.current_page > 1));
2499     }
2500
2501
2502   // set button to a specific state
2503   this.set_button = function(command, state)
2504     {
2505     var a_buttons = this.buttons[command];
2506     var button, obj;
2507
2508     if(!a_buttons || !a_buttons.length)
2509       return;
2510
2511     for(var n=0; n<a_buttons.length; n++)
2512       {
2513       button = a_buttons[n];
2514       obj = document.getElementById(button.id);
2515
2516       // get default/passive setting of the button
2517       if (obj && button.type=='image' && !button.status)
2518         button.pas = obj._original_src ? obj._original_src : obj.src;
2519       else if (obj && !button.status)
2520         button.pas = String(obj.className);
2521
2522       // set image according to button state
2523       if (obj && button.type=='image' && button[state])
2524         {
2525         button.status = state;        
2526         obj.src = button[state];
2527         }
2528       // set class name according to button state
2529       else if (obj && typeof(button[state])!='undefined')
2530         {
2531         button.status = state;        
2532         obj.className = button[state];        
2533         }
2534       // disable/enable input buttons
2535       if (obj && button.type=='input')
2536         {
2537         button.status = state;
2538         obj.disabled = !state;
2539         }
2540       }
2541     };
2542
2543
2544   // mouse over button
2545   this.button_over = function(command, id)
2546     {
2547     var a_buttons = this.buttons[command];
2548     var button, img;
2549
2550     if(!a_buttons || !a_buttons.length)
2551       return;
2552
2553     for(var n=0; n<a_buttons.length; n++)
2554       {
2555       button = a_buttons[n];
2556       if(button.id==id && button.status=='act')
2557         {
2558         img = document.getElementById(button.id);
2559         if (img && button.over)
2560           img.src = button.over;
2561         }
2562       }
2563     };
2564
2565
2566   // mouse out of button
2567   this.button_out = function(command, id)
2568     {
2569     var a_buttons = this.buttons[command];
2570     var button, img;
2571
2572     if(!a_buttons || !a_buttons.length)
2573       return;
2574
2575     for(var n=0; n<a_buttons.length; n++)
2576       {
2577       button = a_buttons[n];
2578       if(button.id==id && button.status=='act')
2579         {
2580         img = document.getElementById(button.id);
2581         if (img && button.act)
2582           img.src = button.act;
2583         }
2584       }
2585     };
2586
2587
2588   // set/unset a specific class name
2589   this.set_classname = function(obj, classname, set)
2590     {
2591     var reg = new RegExp('\s*'+classname, 'i');
2592     if (!set && obj.className.match(reg))
2593       obj.className = obj.className.replace(reg, '');
2594     else if (set && !obj.className.match(reg))
2595       obj.className += ' '+classname;
2596     };
2597
2598
2599   // display a specific alttext
2600   this.alttext = function(text)
2601     {
2602     
2603     };
2604
2605
2606   // display a system message
2607   this.display_message = function(msg, type, hold)
2608     {
2609     if (!this.loaded)  // save message in order to display after page loaded
2610       {
2611       this.pending_message = new Array(msg, type);
2612       return true;
2613       }
2614     
2615     if (!this.gui_objects.message)
2616       return false;
2617       
2618     if (this.message_timer)
2619       clearTimeout(this.message_timer);
2620     
2621     var cont = msg;
2622     if (type)
2623       cont = '<div class="'+type+'">'+cont+'</div>';
a95e0e 2624
T 2625     this.gui_objects.message._rcube = this;
4e17e6 2626     this.gui_objects.message.innerHTML = cont;
T 2627     this.gui_objects.message.style.display = 'block';
a95e0e 2628     
T 2629     if (type!='loading')
2630       this.gui_objects.message.onmousedown = function(){ this._rcube.hide_message(); return true; };
4e17e6 2631     
T 2632     if (!hold)
2633       this.message_timer = setTimeout(this.ref+'.hide_message()', this.message_time);
2634     };
2635
2636
2637   // make a message row disapear
2638   this.hide_message = function()
2639     {
2640     if (this.gui_objects.message)
a95e0e 2641       {
4e17e6 2642       this.gui_objects.message.style.display = 'none';
a95e0e 2643       this.gui_objects.message.onmousedown = null;
T 2644       }
4e17e6 2645     };
T 2646
2647
2648   // mark a mailbox as selected and set environment variable
2649   this.select_mailbox = function(mbox)
2650     {
2651     if (this.gui_objects.mailboxlist)
2652       {
2653       var item, reg, text_obj;
6a35c8 2654       var s_current = this.env.mailbox.toLowerCase().replace(this.mbox_expression, '');
4e17e6 2655       var s_mbox = String(mbox).toLowerCase().replace(this.mbox_expression, '');
T 2656       var s_current = this.env.mailbox.toLowerCase().replace(this.mbox_expression, '');
597170 2657       
6a35c8 2658       var current_li = document.getElementById('rcmbx'+s_current);
T 2659       var mbox_li = document.getElementById('rcmbx'+s_mbox);
2660       
2661       if (current_li)
2662         this.set_classname(current_li, 'selected', false);
2663       if (mbox_li)
2664         this.set_classname(mbox_li, 'selected', true);
4e17e6 2665       }
T 2666     
2667     this.env.mailbox = mbox;
2668     };
2669
2670
2671   // create a table row in the message list
15a9d1 2672   this.add_message_row = function(uid, cols, flags, attachment, attop)
4e17e6 2673     {
T 2674     if (!this.gui_objects.messagelist || !this.gui_objects.messagelist.tBodies[0])
2675       return false;
2676     
2677     var tbody = this.gui_objects.messagelist.tBodies[0];
2678     var rowcount = tbody.rows.length;
2679     var even = rowcount%2;
2680     
2681     this.env.messages[uid] = {replied:flags.replied?1:0,
2682                               unread:flags.unread?1:0};
2683     
2684     var row = document.createElement('TR');
2685     row.id = 'rcmrow'+uid;
15a9d1 2686     row.className = 'message '+(even ? 'even' : 'odd')+(flags.unread ? ' unread' : '')+(flags.deleted ? ' deleted' : '');
T 2687     
4e17e6 2688     if (this.in_selection(uid))
T 2689       row.className += ' selected';
2690
2691     var icon = flags.unread && this.env.unreadicon ? this.env.unreadicon :
2692                (flags.replied && this.env.repliedicon ? this.env.repliedicon : this.env.messageicon);
2693
2694     var col = document.createElement('TD');
2695     col.className = 'icon';
2696     col.innerHTML = icon ? '<img src="'+icon+'" alt="" border="0" />' : '';
2697     row.appendChild(col);
2698
2699     // add each submitted col
2700     for (var c in cols)
2701       {
2702       col = document.createElement('TD');
2703       col.className = String(c).toLowerCase();
2704       col.innerHTML = cols[c];
2705       row.appendChild(col);
2706       }
2707
2708     col = document.createElement('TD');
2709     col.className = 'icon';
2710     col.innerHTML = attachment && this.env.attachmenticon ? '<img src="'+this.env.attachmenticon+'" alt="" border="0" />' : '';
2711     row.appendChild(col);
2712     
15a9d1 2713     if (attop && tbody.rows.length)
T 2714       tbody.insertBefore(row, tbody.firstChild);
2715     else
2716       tbody.appendChild(row);
2717       
4e17e6 2718     this.init_message_row(row);
T 2719     };
2720
2721
2722   // replace content of row count display
2723   this.set_rowcount = function(text)
2724     {
2725     if (this.gui_objects.countdisplay)
2726       this.gui_objects.countdisplay.innerHTML = text;
2727
2728     // update page navigation buttons
2729     this.set_page_buttons();
2730     };
2731
58e360 2732   // replace content of quota display
T 2733    this.set_quota = function(text)
2734      {
2735      if (this.gui_objects.quotadisplay)
2736        this.gui_objects.quotadisplay.innerHTML = text;
2737      };
2738                  
4e17e6 2739
T 2740   // update the mailboxlist
15a9d1 2741   this.set_unread_count = function(mbox, count, set_title)
4e17e6 2742     {
T 2743     if (!this.gui_objects.mailboxlist)
2744       return false;
f108b9 2745       
4e17e6 2746     var item, reg, text_obj;
15a9d1 2747     mbox = String(mbox).toLowerCase().replace(this.mbox_expression, '');
T 2748     item = document.getElementById('rcmbx'+mbox);
2749
2750     if (item && item.className && item.className.indexOf('mailbox '+mbox)>=0)
4e17e6 2751       {
15a9d1 2752       // set new text
T 2753       text_obj = item.firstChild;
2754       reg = /\s+\([0-9]+\)$/i;
4e17e6 2755
15a9d1 2756       if (count && text_obj.innerHTML.match(reg))
T 2757         text_obj.innerHTML = text_obj.innerHTML.replace(reg, ' ('+count+')');
2758       else if (count)
2759         text_obj.innerHTML += ' ('+count+')';
2760       else
2761         text_obj.innerHTML = text_obj.innerHTML.replace(reg, '');
4e17e6 2762           
15a9d1 2763       // set the right classes
T 2764       this.set_classname(item, 'unread', count>0 ? true : false);
2765       }
2766
2767     // set unread count to window title
01c86f 2768     reg = /^\([0-9]+\)\s+/i;
T 2769     if (set_title && count && document.title)    
15a9d1 2770       {
T 2771       var doc_title = String(document.title);
2772
2773       if (count && doc_title.match(reg))
2774         document.title = doc_title.replace(reg, '('+count+') ');
2775       else if (count)
2776         document.title = '('+count+') '+doc_title;
2777       else
2778         document.title = doc_title.replace(reg, '');
4e17e6 2779       }
01c86f 2780     // remove unread count from window title
T 2781     else if (document.title)
2782       {
2783       document.title = document.title.replace(reg, '');
2784       }
4e17e6 2785     };
T 2786
2787
2788   // add row to contacts list
2789   this.add_contact_row = function(cid, cols)
2790     {
2791     if (!this.gui_objects.contactslist || !this.gui_objects.contactslist.tBodies[0])
2792       return false;
2793     
2794     var tbody = this.gui_objects.contactslist.tBodies[0];
2795     var rowcount = tbody.rows.length;
2796     var even = rowcount%2;
2797     
2798     var row = document.createElement('TR');
2799     row.id = 'rcmrow'+cid;
2800     row.className = 'contact '+(even ? 'even' : 'odd');
2801     
2802     if (this.in_selection(cid))
2803       row.className += ' selected';
2804
2805     // add each submitted col
2806     for (var c in cols)
2807       {
2808       col = document.createElement('TD');
2809       col.className = String(c).toLowerCase();
2810       col.innerHTML = cols[c];
2811       row.appendChild(col);
2812       }
2813     
2814     tbody.appendChild(row);
2815     this.init_table_row(row, 'contact_rows');
2816     };
2817
2818
2819
2820   /********************************************************/
2821   /*********          drag & drop methods         *********/
2822   /********************************************************/
2823
2824
2825   this.drag_mouse_move = function(e)
2826     {
2827     if (this.drag_start)
2828       {
2829       if (!this.draglayer)
2830         this.draglayer = new rcube_layer('rcmdraglayer', {x:0, y:0, width:300, vis:0, zindex:2000});
2831       
2832       // get subjects of selectedd messages
2833       var names = '';
2834       var c, subject, obj;
2835       for(var n=0; n<this.selection.length; n++)
2836         {
2837         if (n>12)  // only show 12 lines
2838           {
2839           names += '...';
2840           break;
2841           }
2842
2843         if (this.message_rows[this.selection[n]].obj)
2844           {
2845           obj = this.message_rows[this.selection[n]].obj;
2846           subject = '';
2847
2848           for(c=0; c<obj.childNodes.length; c++)
2849             if (!subject && obj.childNodes[c].nodeName=='TD' && obj.childNodes[c].firstChild && obj.childNodes[c].firstChild.nodeType==3)
2850               {
2851               subject = obj.childNodes[c].firstChild.data;
2852               names += (subject.length > 50 ? subject.substring(0, 50)+'...' : subject) + '<br />';
2853               }
2854           }
2855         }
2856         
2857       this.draglayer.write(names);
2858       this.draglayer.show(1);
2859       }
2860
2861     var pos = this.get_mouse_pos(e);
2862     this.draglayer.move(pos.x+20, pos.y-5);
2863     
2864     this.drag_start = false;
2865     this.drag_active = true;
2866     
2867     return false;
2868     };
2869
2870
2871   this.drag_mouse_up = function()
2872     {
2873     document.onmousemove = null;
2874     
2875     if (this.draglayer && this.draglayer.visible)
2876       this.draglayer.show(0);
2877       
2878     this.drag_active = false;
2879     
2880     return false;
2881     };
2882
2883
2884
2885   /********************************************************/
2886   /*********        remote request methods        *********/
2887   /********************************************************/
2888
2889
ecf759 2890   this.http_sockets = new Array();
T 2891   
2892   // find a non-busy socket or create a new one
2893   this.get_request_obj = function()
4e17e6 2894     {
ecf759 2895     for (var n=0; n<this.http_sockets.length; n++)
4e17e6 2896       {
ecf759 2897       if (!this.http_sockets[n].busy)
T 2898         return this.http_sockets[n];
4e17e6 2899       }
ecf759 2900     
T 2901     // create a new XMLHTTP object
2902     var i = this.http_sockets.length;
2903     this.http_sockets[i] = new rcube_http_request();
4e17e6 2904
ecf759 2905     return this.http_sockets[i];
T 2906     };
2907   
2908
2909   // send a http request to the server
2910   this.http_request = function(action, querystring, lock)
2911     {
2912     var request_obj = this.get_request_obj();
4e17e6 2913     querystring += '&_remote=1';
T 2914     
2915     // add timestamp to request url to avoid cacheing problems in Safari
2916     if (bw.safari)
2917       querystring += '&_ts='+(new Date().getTime());
2918
2919     // send request
ecf759 2920     if (request_obj)
4e17e6 2921       {
T 2922       // prompt('request', this.env.comm_path+'&_action='+escape(action)+'&'+querystring);
2923       console('HTTP request: '+this.env.comm_path+'&_action='+escape(action)+'&'+querystring);
ecf759 2924
T 2925       if (lock)
2926         this.set_busy(true);
2927
2928       request_obj.__lock = lock ? true : false;
2929       request_obj.__action = action;
2930       request_obj.onerror = function(o){ rcube_webmail_client.http_error(o); };
2931       request_obj.oncomplete = function(o){ rcube_webmail_client.http_response(o); };
2932       request_obj.GET(this.env.comm_path+'&_action='+escape(action)+'&'+querystring);
4e17e6 2933       }
T 2934     };
2935
2936
ecf759 2937   // handle HTTP response
T 2938   this.http_response = function(request_obj)
4e17e6 2939     {
ecf759 2940     var ctype = request_obj.get_header('Content-Type');
T 2941     if (ctype)
2942       ctype = String(ctype).toLowerCase();
4e17e6 2943
ecf759 2944     if (request_obj.__lock)
4e17e6 2945       this.set_busy(false);
T 2946
5e3512 2947   console(request_obj.get_text());
4e17e6 2948
ecf759 2949     // if we get javascript code from server -> execute it
c03095 2950     if (request_obj.get_text() && (ctype=='text/javascript' || ctype=='application/x-javascript'))
T 2951       eval(request_obj.get_text());
4e17e6 2952
ecf759 2953     // process the response data according to the sent action
T 2954     switch (request_obj.__action)
2955       {
2956       case 'delete':
2957       case 'moveto':
2958         if (this.env.action=='show')
2959           this.command('list');
2960         break;
15a9d1 2961
ecf759 2962       case 'list':
5e3512 2963         if (this.env.messagecount)
T 2964           this.enable_command('purge', (this.env.mailbox==this.env.trash_mailbox));
2965
15a9d1 2966       case 'expunge':
T 2967         this.enable_command('select-all', 'select-none', 'expunge', this.env.messagecount ? true : false);
5e3512 2968         break;      
4e17e6 2969       }
ecf759 2970
T 2971     request_obj.reset();
4e17e6 2972     };
ecf759 2973
T 2974
2975   // handle HTTP request errors
2976   this.http_error = function(request_obj)
2977     {
2978     alert('Error sending request: '+request_obj.url);
2979
2980     if (request_obj.__lock)
2981       this.set_busy(false);
2982
2983     request_obj.reset();
2984     request_obj.__lock = false;
2985     };
2986
2987
2988   // use an image to send a keep-alive siganl to the server
2989   this.send_keep_alive = function()
2990     {
2991     var d = new Date();
2992     this.http_request('keep-alive', '_t='+d.getTime());
2993     };
15a9d1 2994
ecf759 2995     
15a9d1 2996   // send periodic request to check for recent messages
T 2997   this.check_for_recent = function()
2998     {
2999     var d = new Date();
3000     this.http_request('check-recent', '_t='+d.getTime());
3001     };
4e17e6 3002
T 3003
3004   /********************************************************/
3005   /*********            helper methods            *********/
3006   /********************************************************/
3007   
3008   // check if we're in show mode or if we have a unique selection
3009   // and return the message uid
3010   this.get_single_uid = function()
3011     {
3012     return this.env.uid ? this.env.uid : (this.selection.length==1 ? this.selection[0] : null);
3013     };
3014
3015   // same as above but for contacts
3016   this.get_single_cid = function()
3017     {
3018     return this.env.cid ? this.env.cid : (this.selection.length==1 ? this.selection[0] : null);
3019     };
3020
3021
8c2e58 3022 /* deprecated methods
T 3023
4e17e6 3024   // check if Shift-key is pressed on event
T 3025   this.check_shiftkey = function(e)
3026     {
3027     if(!e && window.event)
3028       e = window.event;
3029
3030     if(bw.linux && bw.ns4 && e.modifiers)
3031       return true;
3032     else if((bw.ns4 && e.modifiers & Event.SHIFT_MASK) || (e && e.shiftKey))
3033       return true;
3034     else
3035       return false;
3036     }
3037
1cded8 3038   // check if Shift-key is pressed on event
T 3039   this.check_ctrlkey = function(e)
3040     {
3041     if(!e && window.event)
3042       e = window.event;
3043
3044     if(bw.linux && bw.ns4 && e.modifiers)
3045       return true;
3046    else if (bw.mac)
3047        return this.check_shiftkey(e);
3048     else if((bw.ns4 && e.modifiers & Event.CTRL_MASK) || (e && e.ctrlKey))
3049       return true;
3050     else
3051       return false;
3052     }
8c2e58 3053 */
4e17e6 3054
8c2e58 3055   // returns modifier key (constants defined at top of file)
b11a00 3056   this.get_modifier = function(e)
T 3057     {
3058     var opcode = 0;
8c2e58 3059     e = e || window.event;
T 3060
3061     if (bw.mac && e)
3062       {
3063       opcode += (e.metaKey && CONTROL_KEY) + (e.shiftKey && SHIFT_KEY);
3064       return opcode;    
3065       }
3066     if (e)
3067       {
b11a00 3068       opcode += (e.ctrlKey && CONTROL_KEY) + (e.shiftKey && SHIFT_KEY);
8c2e58 3069       return opcode;
T 3070       }
b11a00 3071     if (e.cancelBubble)
8c2e58 3072       {
b11a00 3073       e.cancelBubble = true;
T 3074       e.returnValue = false;
8c2e58 3075       }
b11a00 3076     else if (e.preventDefault)
T 3077       e.preventDefault();
3078   }
3079
3080
4e17e6 3081   this.get_mouse_pos = function(e)
T 3082     {
3083     if(!e) e = window.event;
3084     var mX = (e.pageX) ? e.pageX : e.clientX;
3085     var mY = (e.pageY) ? e.pageY : e.clientY;
3086
3087     if(document.body && document.all)
3088       {
3089       mX += document.body.scrollLeft;
3090       mY += document.body.scrollTop;
3091       }
3092
3093     return { x:mX, y:mY };
3094     };
3095     
3096   
3097   this.get_caret_pos = function(obj)
3098     {
3099     if (typeof(obj.selectionEnd)!='undefined')
3100       return obj.selectionEnd;
3101
3102     else if (document.selection && document.selection.createRange)
3103       {
3104       var range = document.selection.createRange();
3105       if (range.parentElement()!=obj)
3106         return 0;
3107
3108       var gm = range.duplicate();
3109       if (obj.tagName=='TEXTAREA')
3110         gm.moveToElementText(obj);
3111       else
3112         gm.expand('textedit');
3113       
3114       gm.setEndPoint('EndToStart', range);
3115       var p = gm.text.length;
3116
3117       return p<=obj.value.length ? p : -1;
3118       }
3119
3120     else
3121       return obj.value.length;
3122     };
3123
3124
3125   this.set_caret2start = function(obj)
3126     {
3127     if (obj.createTextRange)
3128       {
3129       var range = obj.createTextRange();
3130       range.collapse(true);
3131       range.select();
3132       }
3133     else if (obj.setSelectionRange)
3134       obj.setSelectionRange(0,0);
3135
3136     obj.focus();
3137     };
3138
3139
3140   // set all fields of a form disabled
3141   this.lock_form = function(form, lock)
3142     {
3143     if (!form || !form.elements)
3144       return;
3145     
3146     var type;
3147     for (var n=0; n<form.elements.length; n++)
3148       {
3149       type = form.elements[n];
3150       if (type=='hidden')
3151         continue;
3152         
3153       form.elements[n].disabled = lock;
3154       }
3155     };
3156     
3157   }  // end object rcube_webmail
3158
3159
3160
ecf759 3161 // class for HTTP requests
T 3162 function rcube_http_request()
3163   {
3164   this.url = '';
3165   this.busy = false;
3166   this.xmlhttp = null;
3167
3168
3169   // reset object properties
3170   this.reset = function()
3171     {
3172     // set unassigned event handlers
3173     this.onloading = function(){ };
3174     this.onloaded = function(){ };
3175     this.oninteractive = function(){ };
3176     this.oncomplete = function(){ };
3177     this.onabort = function(){ };
3178     this.onerror = function(){ };
3179     
3180     this.url = '';
3181     this.busy = false;
3182     this.xmlhttp = null;
3183     }
3184
3185
3186   // create HTMLHTTP object
3187   this.build = function()
3188     {
3189     if (window.XMLHttpRequest)
3190       this.xmlhttp = new XMLHttpRequest();
3191     else if (window.ActiveXObject)
3192       this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
3193     else
3194       {
3195       
3196       }
3197     }
3198
3199   // sedn GET request
3200   this.GET = function(url)
3201     {
3202     this.build();
3203
3204     if (!this.xmlhttp)
3205       {
3206       this.onerror(this);
3207       return false;
3208       }
3209
3210     var ref = this;
3211     this.url = url;
3212     this.busy = true;
3213
3214     this.xmlhttp.onreadystatechange = function(){ ref.xmlhttp_onreadystatechange(); };
3215     this.xmlhttp.open('GET', url);
3216     this.xmlhttp.send(null);
3217     };
3218
3219
3220   this.POST = function(url, a_param)
3221     {
3222     // not implemented yet
3223     };
3224
3225
3226   // handle onreadystatechange event
3227   this.xmlhttp_onreadystatechange = function()
3228     {
3229     if(this.xmlhttp.readyState == 1)
3230       this.onloading(this);
3231
3232     else if(this.xmlhttp.readyState == 2)
3233       this.onloaded(this);
3234
3235     else if(this.xmlhttp.readyState == 3)
3236       this.oninteractive(this);
3237
3238     else if(this.xmlhttp.readyState == 4)
3239       {
3240       if(this.xmlhttp.status == 0)
3241         this.onabort(this);
3242       else if(this.xmlhttp.status == 200)
3243         this.oncomplete(this);
3244       else
3245         this.onerror(this);
3246         
3247       this.busy = false;
3248       }
3249     }
3250
3251   // getter method for HTTP headers
3252   this.get_header = function(name)
3253     {
3254     return this.xmlhttp.getResponseHeader(name);
3255     };
3256
c03095 3257   this.get_text = function()
T 3258     {
3259     return this.xmlhttp.responseText;
3260     };
3261
3262   this.get_xml = function()
3263     {
3264     return this.xmlhttp.responseXML;
3265     };
ecf759 3266
T 3267   this.reset();
3268   
3269   }  // end class rcube_http_request
3270
3271
4e17e6 3272
T 3273 function console(str)
3274   {
3275   if (document.debugform && document.debugform.console)
3276     document.debugform.console.value += str+'\n--------------------------------------\n';
3277   }
3278
3279
3280 // set onload handler
3281 window.onload = function(e)
3282   {
3283   if (window.rcube_webmail_client)
3284     rcube_webmail_client.init();
3285   };