thomascube
2008-02-11 6f9926357c8fdd71ae3ede4c3811198974c2df43
commit | author | age
e0ed97 1 /*
4e17e6 2  +-----------------------------------------------------------------------+
T 3  | RoundCube Webmail Client Script                                       |
4  |                                                                       |
5  | This file is part of the RoundCube Webmail client                     |
b85bf8 6  | Copyright (C) 2005-2008, 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  +-----------------------------------------------------------------------+
6b47de 13  | Requires: common.js, list.js                                          |
T 14  +-----------------------------------------------------------------------+
15
15a9d1 16   $Id$
4e17e6 17 */
24053e 18
4e17e6 19
T 20 var rcube_webmail_client;
21
22 function rcube_webmail()
23   {
24   this.env = new Object();
10a699 25   this.labels = new Object();
4e17e6 26   this.buttons = new Object();
T 27   this.gui_objects = new Object();
28   this.commands = new Object();
a7d5c6 29   this.onloads = new Array();
4e17e6 30
cfdf04 31   // create protected reference to myself
4e17e6 32   rcube_webmail_client = this;
T 33   this.ref = 'rcube_webmail_client';
cfdf04 34   var ref = this;
4e17e6 35  
T 36   // webmail client settings
b19097 37   this.dblclick_time = 500;
4b9efb 38   this.message_time = 3000;
9a5261 39   
f11541 40   this.identifier_expr = new RegExp('[^0-9a-z\-_]', 'gi');
4e17e6 41   
T 42   // mimetypes supported by the browser (default settings)
43   this.mimetypes = new Array('text/plain', 'text/html', 'text/xml',
44                              'image/jpeg', 'image/gif', 'image/png',
45                              'application/x-javascript', 'application/pdf',
46                              'application/x-shockwave-flash');
47
9a5261 48   // default environment vars
7139e3 49   this.env.keep_alive = 60;        // seconds
9a5261 50   this.env.request_timeout = 180;  // seconds
e170b4 51   this.env.draft_autosave = 0;     // seconds
f11541 52   this.env.comm_path = './';
T 53   this.env.bin_path = './bin/';
54   this.env.blankpage = 'program/blank.gif';
9a5261 55
4e17e6 56
f11541 57   // set environment variable(s)
T 58   this.set_env = function(p, value)
4e17e6 59     {
f11541 60     if (p != null && typeof(p) == 'object' && !value)
T 61       for (var n in p)
62         this.env[n] = p[n];
63     else
64       this.env[p] = value;
4e17e6 65     };
10a699 66
T 67
68   // add a localized label to the client environment
69   this.add_label = function(key, value)
70     {
71     this.labels[key] = value;
72     };
73
4e17e6 74
T 75   // add a button to the button list
76   this.register_button = function(command, id, type, act, sel, over)
77     {
78     if (!this.buttons[command])
79       this.buttons[command] = new Array();
80       
81     var button_prop = {id:id, type:type};
82     if (act) button_prop.act = act;
83     if (sel) button_prop.sel = sel;
84     if (over) button_prop.over = over;
85
86     this.buttons[command][this.buttons[command].length] = button_prop;    
87     };
88
89   // register a specific gui object
90   this.gui_object = function(name, id)
91     {
92     this.gui_objects[name] = id;
93     };
a7d5c6 94   
T 95   // execute the given script on load
96   this.add_onload = function(f)
97     {
98       this.onloads[this.onloads.length] = f;
99     };
4e17e6 100
T 101   // initialize webmail client
102   this.init = function()
103     {
6b47de 104     var p = this;
4e17e6 105     this.task = this.env.task;
T 106     
107     // check browser
a95e0e 108     if (!bw.dom || !bw.xmlhttp_test())
4e17e6 109       {
6b47de 110       this.goto_url('error', '_code=0x199');
4e17e6 111       return;
T 112       }
113     
114     // find all registered gui objects
115     for (var n in this.gui_objects)
116       this.gui_objects[n] = rcube_find_object(this.gui_objects[n]);
a7d5c6 117
4e17e6 118     // tell parent window that this frame is loaded
T 119     if (this.env.framed && parent.rcmail && parent.rcmail.set_busy)
120       parent.rcmail.set_busy(false);
121
122     // enable general commands
123     this.enable_command('logout', 'mail', 'addressbook', 'settings', true);
124     
125     switch (this.task)
126       {
127       case 'mail':
6b47de 128         if (this.gui_objects.messagelist)
4e17e6 129           {
6b47de 130           this.message_list = new rcube_list_widget(this.gui_objects.messagelist, {multiselect:true, draggable:true, keyboard:true, dblclick_time:this.dblclick_time});
T 131           this.message_list.row_init = function(o){ p.init_message_row(o); };
132           this.message_list.addEventListener('dblclick', function(o){ p.msglist_dbl_click(o); });
133           this.message_list.addEventListener('keypress', function(o){ p.msglist_keypress(o); });
134           this.message_list.addEventListener('select', function(o){ p.msglist_select(o); });
135           this.message_list.addEventListener('dragstart', function(o){ p.drag_active = true; });
136           this.message_list.addEventListener('dragend', function(o){ p.drag_active = false; });
137
138           this.message_list.init();
857a38 139           this.enable_command('toggle_status', true);
6b47de 140           
T 141           if (this.gui_objects.mailcontframe)
142             {
143             this.gui_objects.mailcontframe.onmousedown = function(e){ return p.click_on_list(e); };
144             document.onmouseup = function(e){ return p.doc_mouse_up(e); };
145             }
146           else
147             this.message_list.focus();
4e17e6 148           }
T 149
150         // enable mail commands
c8c1e0 151         this.enable_command('list', 'checkmail', 'compose', 'add-contact', 'search', 'reset-search', true);
1f020b 152
S 153         if (this.env.search_text != null && document.getElementById('quicksearchbox') != null)
154           document.getElementById('quicksearchbox').value = this.env.search_text;
4e17e6 155         
b19097 156         if (this.env.action=='show' || this.env.action=='preview')
4e17e6 157           {
b85bf8 158           this.enable_command('show', 'reply', 'reply-all', 'forward', 'moveto', 'delete', 'mark', 'viewsource', 'print', 'load-attachment', true);
4e17e6 159           if (this.env.next_uid)
d17008 160             {
4e17e6 161             this.enable_command('nextmessage', true);
d17008 162             this.enable_command('lastmessage', true);
S 163             }
4e17e6 164           if (this.env.prev_uid)
d17008 165             {
4e17e6 166             this.enable_command('previousmessage', true);
d17008 167             this.enable_command('firstmessage', true);
S 168             }
4e17e6 169           }
eb6842 170
T 171         if (this.env.trash_mailbox && this.env.mailbox != this.env.trash_mailbox)
172           this.set_alttext('delete', 'movemessagetotrash');
b19097 173         
T 174         // make preview/message frame visible
175         if (this.env.action == 'preview' && this.env.framed && parent.rcmail)
176           {
177           this.enable_command('compose', 'add-contact', false);
f11541 178           parent.rcmail.show_contentframe(true);
2e8a61 179           parent.rcmail.mark_message('read', this.env.uid);
b19097 180           }
4e17e6 181
b19097 182         if ((this.env.action=='show' || this.env.action=='preview') && this.env.blockedobjects)
4e17e6 183           {
T 184           if (this.gui_objects.remoteobjectsmsg)
185             this.gui_objects.remoteobjectsmsg.style.display = 'block';
186           this.enable_command('load-images', true);
a0109c 187           }
4e17e6 188
T 189         if (this.env.action=='compose')
ed5d29 190           {
a894ba 191           this.enable_command('add-attachment', 'send-attachment', 'remove-attachment', 'send', true);
ed5d29 192           if (this.env.spellcheck)
e170b4 193             {
86958f 194             this.env.spellcheck.spelling_state_observer = function(s){ ref.set_spellcheck_state(s); };
e170b4 195             this.set_spellcheck_state('ready');
T 196             }
41fa0b 197           if (this.env.drafts_mailbox)
T 198             this.enable_command('savedraft', true);
ed5d29 199           }
a0109c 200
4e17e6 201         if (this.env.messagecount)
15a9d1 202           this.enable_command('select-all', 'select-none', 'sort', 'expunge', true);
4e17e6 203
b4b081 204         if (this.env.messagecount && (this.env.mailbox==this.env.trash_mailbox || this.env.mailbox==this.env.junk_mailbox))
5e3512 205           this.enable_command('purge', true);
T 206
4e17e6 207         this.set_page_buttons();
T 208
181970 209         // focus main window
S 210         if (this.env.framed && window.parent)
211           window.parent.focus();
212         else
213           window.focus();
4e17e6 214
T 215         // init message compose form
216         if (this.env.action=='compose')
217           this.init_messageform();
218
219         // show printing dialog
220         if (this.env.action=='print')
221           window.print();
a0109c 222
15a9d1 223         // get unread count for each mailbox
T 224         if (this.gui_objects.mailboxlist)
f11541 225         {
T 226           this.gui_objects.folderlist = this.gui_objects.mailboxlist;
15a9d1 227           this.http_request('getunread', '');
f11541 228         }
fba1f5 229         
T 230         // ask user to send MDN
231         if (this.env.mdn_request && this.env.uid)
232         {
233           var mdnurl = '_uid='+this.env.uid+'&_mbox='+urlencode(this.env.mailbox);
234           if (confirm(this.get_label('mdnrequest')))
235             this.http_post('sendmdn', mdnurl);
236           else
237             this.http_post('mark', mdnurl+'&_flag=mdnsent');
238         }
4e17e6 239
T 240         break;
241
242
243       case 'addressbook':
6b47de 244         if (this.gui_objects.contactslist)
T 245           {
f11541 246           this.contact_list = new rcube_list_widget(this.gui_objects.contactslist, {multiselect:true, draggable:true, keyboard:true});
6b47de 247           this.contact_list.addEventListener('keypress', function(o){ p.contactlist_keypress(o); });
T 248           this.contact_list.addEventListener('select', function(o){ p.contactlist_select(o); });
f11541 249           this.contact_list.addEventListener('dragstart', function(o){ p.drag_active = true; });
T 250           this.contact_list.addEventListener('dragend', function(o){ p.drag_active = false; });
6b47de 251           this.contact_list.init();
d1d2c4 252
6b47de 253           if (this.env.cid)
T 254             this.contact_list.highlight_row(this.env.cid);
255
256           if (this.gui_objects.contactslist.parentNode)
257             {
258             this.gui_objects.contactslist.parentNode.onmousedown = function(e){ return p.click_on_list(e); };
259             document.onmouseup = function(e){ return p.doc_mouse_up(e); };
260             }
261           else
262             this.contact_list.focus();
263           }
d1d2c4 264
4e17e6 265         this.set_page_buttons();
f11541 266         
56fab1 267         if (this.env.address_sources && !this.env.address_sources[this.env.source].readonly)
f11541 268           this.enable_command('add', true);
T 269         
4e17e6 270         if (this.env.cid)
T 271           this.enable_command('show', 'edit', true);
272
56fab1 273         if ((this.env.action=='add' || this.env.action=='edit') && this.gui_objects.editform)
4e17e6 274           this.enable_command('save', true);
f11541 275         else
T 276           this.enable_command('search', 'reset-search', 'moveto', true);
6b47de 277
f11541 278         this.enable_command('list', true);
4e17e6 279         break;
T 280
281
282       case 'settings':
283         this.enable_command('preferences', 'identities', 'save', 'folders', true);
284         
285         if (this.env.action=='identities' || this.env.action=='edit-identity' || this.env.action=='add-identity')
286           this.enable_command('edit', 'add', 'delete', true);
287
288         if (this.env.action=='edit-identity' || this.env.action=='add-identity')
289           this.enable_command('save', true);
290           
291         if (this.env.action=='folders')
c8c1e0 292           this.enable_command('subscribe', 'unsubscribe', 'create-folder', 'rename-folder', 'delete-folder', true);
6b47de 293
T 294         if (this.gui_objects.identitieslist)
295           {
296           this.identity_list = new rcube_list_widget(this.gui_objects.identitieslist, {multiselect:false, draggable:false, keyboard:false});
297           this.identity_list.addEventListener('select', function(o){ p.identity_select(o); });
298           this.identity_list.init();
299           this.identity_list.focus();
300
301           if (this.env.iid)
302             this.identity_list.highlight_row(this.env.iid);
303           }
4e17e6 304
b0dbf3 305         if (this.gui_objects.subscriptionlist)
S 306           this.init_subscription_list();
307
4e17e6 308         break;
T 309
310       case 'login':
311         var input_user = rcube_find_object('_user');
312         var input_pass = rcube_find_object('_pass');
b566ff 313         if (input_user)
S 314           input_user.onkeypress = function(e){ return rcmail.login_user_keypress(e); };
4e17e6 315         if (input_user && input_user.value=='')
T 316           input_user.focus();
317         else if (input_pass)
318           input_pass.focus();
319           
320         this.enable_command('login', true);
321         break;
322       
323       default:
324         break;
325       }
326
327
328     // enable basic commands
329     this.enable_command('logout', true);
330
331     // flag object as complete
332     this.loaded = true;
9a5261 333
4e17e6 334     // show message
T 335     if (this.pending_message)
336       this.display_message(this.pending_message[0], this.pending_message[1]);
9a5261 337
T 338     // start keep-alive interval
339     this.start_keepalive();
a7d5c6 340     
T 341     
342     // execute all foreign onload scripts
343     for (var i=0; i<this.onloads.length; i++)
344       {
345       if (typeof(this.onloads[i]) == 'string')
346         eval(this.onloads[i]);
347       else if (typeof(this.onloads[i]) == 'function')
348         this.onloads[i]();
349       }
4e17e6 350     };
9a5261 351
T 352
353   // start interval for keep-alive/recent_check signal
354   this.start_keepalive = function()
355     {
1a98a6 356     if (this.env.keep_alive && !this.env.framed && this.task=='mail' && this.gui_objects.messagelist)
cfdf04 357       this._int = setInterval(function(){ ref.check_for_recent(); }, this.env.keep_alive * 1000);
1a98a6 358     else if (this.env.keep_alive && !this.env.framed && this.task!='login')
cfdf04 359       this._int = setInterval(function(){ ref.send_keep_alive(); }, this.env.keep_alive * 1000);    
9a5261 360     }
T 361
4e17e6 362
T 363   this.init_message_row = function(row)
6b47de 364   {
T 365     var uid = row.uid;
366     if (uid && this.env.messages[uid])
4e17e6 367       {
ae895a 368       row.deleted = this.env.messages[uid].deleted ? true : false;
T 369       row.unread = this.env.messages[uid].unread ? true : false;
370       row.replied = this.env.messages[uid].replied ? true : false;
4e17e6 371       }
6b47de 372
T 373     // set eventhandler to message icon
374     if ((row.icon = row.obj.cells[0].childNodes[0]) && row.icon.nodeName=='IMG')
375       {
376       var p = this;
377       row.icon.id = 'msgicn_'+row.uid;
378       row.icon._row = row.obj;
379       row.icon.onmousedown = function(e) { p.command('toggle_status', this); };
380       }
381   };
4e17e6 382
T 383
384   // init message compose form: set focus and eventhandlers
385   this.init_messageform = function()
386     {
387     if (!this.gui_objects.messageform)
388       return false;
389     
390     //this.messageform = this.gui_objects.messageform;
1cded8 391     var input_from = rcube_find_object('_from');
4e17e6 392     var input_to = rcube_find_object('_to');
T 393     var input_cc = rcube_find_object('_cc');
394     var input_bcc = rcube_find_object('_bcc');
395     var input_replyto = rcube_find_object('_replyto');
396     var input_subject = rcube_find_object('_subject');
397     var input_message = rcube_find_object('_message');
a0109c 398
4e17e6 399     // init live search events
T 400     if (input_to)
401       this.init_address_input_events(input_to);
402     if (input_cc)
403       this.init_address_input_events(input_cc);
404     if (input_bcc)
405       this.init_address_input_events(input_bcc);
1cded8 406       
T 407     // add signature according to selected identity
408     if (input_from && input_from.type=='select-one')
409       this.change_identity(input_from);
4e17e6 410
T 411     if (input_to && input_to.value=='')
412       input_to.focus();
413     else if (input_subject && input_subject.value=='')
414       input_subject.focus();
415     else if (input_message)
6b47de 416       this.set_caret2start(input_message);
a0109c 417
977a29 418     // get summary of all field values
f11541 419     this.compose_field_hash(true);
f0f98f 420  
S 421     // start the auto-save timer
422     this.auto_save_start();
4e17e6 423     };
T 424
425   this.init_address_input_events = function(obj)
426     {
86958f 427     var handler = function(e){ return ref.ksearch_keypress(e,this); };
T 428     var handler2 = function(e){ return ref.ksearch_blur(e,this); };
429     
430     if (obj.addEventListener)
431     {
432       obj.addEventListener(bw.safari ? 'keydown' : 'keypress', handler, false);
4e17e6 433       obj.addEventListener('blur', handler2, false);
86958f 434     }
T 435     else
436     {
4e17e6 437       obj.onkeydown = handler;
86958f 438       obj.onblur = handler2;
T 439     }
6b47de 440
4e17e6 441     obj.setAttribute('autocomplete', 'off');       
T 442     };
443
444
445
446   /*********************************************************/
447   /*********       client command interface        *********/
448   /*********************************************************/
449
450
451   // execute a specific command on the web client
452   this.command = function(command, props, obj)
453     {
454     if (obj && obj.blur)
455       obj.blur();
456
457     if (this.busy)
458       return false;
459
460     // command not supported or allowed
461     if (!this.commands[command])
462       {
463       // pass command to parent window
464       if (this.env.framed && parent.rcmail && parent.rcmail.command)
465         parent.rcmail.command(command, props);
466
467       return false;
468       }
15a9d1 469       
T 470       
471    // check input before leaving compose step
472    if (this.task=='mail' && this.env.action=='compose' && (command=='list' || command=='mail' || command=='addressbook' || command=='settings'))
473      {
474      if (this.cmp_hash != this.compose_field_hash() && !confirm(this.get_label('notsentwarning')))
475         return false;
476      }
477
4e17e6 478
T 479     // process command
480     switch (command)
481       {
482       case 'login':
483         if (this.gui_objects.loginform)
484           this.gui_objects.loginform.submit();
485         break;
486
487       case 'logout':
719a25 488         this.goto_url('logout', true);
4e17e6 489         break;      
T 490
491       // commands to switch task
492       case 'mail':
493       case 'addressbook':
494       case 'settings':
495         this.switch_task(command);
496         break;
497
498
499       // misc list commands
500       case 'list':
501         if (this.task=='mail')
4647e1 502           {
1f020b 503           if (this.env.search_request<0 || (props != '' && (this.env.search_request && props != this.env.mailbox)))
4647e1 504             this.reset_qsearch();
c8c1e0 505
4e17e6 506           this.list_mailbox(props);
eb6842 507
T 508           if (this.env.trash_mailbox)
509             this.set_alttext('delete', this.env.mailbox != this.env.trash_mailbox ? 'movemessagetotrash' : 'deletemessage');
4647e1 510           }
4e17e6 511         else if (this.task=='addressbook')
f11541 512           {
T 513           if (this.env.search_request<0 || (this.env.search_request && props != this.env.source))
514             this.reset_qsearch();
515
516           this.list_contacts(props);
517           this.enable_command('add', (this.env.address_sources && !this.env.address_sources[props].readonly));
518           }
f3b659 519         break;
c8c1e0 520
f3b659 521
T 522       case 'sort':
523         // get the type of sorting
b076a4 524         var a_sort = props.split('_');
T 525         var sort_col = a_sort[0];
1cded8 526         var sort_order = a_sort[1] ? a_sort[1].toUpperCase() : null;
b076a4 527         var header;
1cded8 528         
T 529         // no sort order specified: toggle
530         if (sort_order==null)
531           {
532           if (this.env.sort_col==sort_col)
533             sort_order = this.env.sort_order=='ASC' ? 'DESC' : 'ASC';
534           else
535             sort_order = this.env.sort_order;
536           }
24053e 537
b076a4 538         if (this.env.sort_col==sort_col && this.env.sort_order==sort_order)
T 539           break;
540
541         // set table header class
542         if (header = document.getElementById('rcmHead'+this.env.sort_col))
543           this.set_classname(header, 'sorted'+(this.env.sort_order.toUpperCase()), false);
544         if (header = document.getElementById('rcmHead'+sort_col))
545           this.set_classname(header, 'sorted'+sort_order, true);
546
547         // save new sort properties
548         this.env.sort_col = sort_col;
549         this.env.sort_order = sort_order;
550
551         // reload message list
1cded8 552         this.list_mailbox('', '', sort_col+'_'+sort_order);
4e17e6 553         break;
T 554
555       case 'nextpage':
556         this.list_page('next');
557         break;
558
d17008 559       case 'lastpage':
S 560         this.list_page('last');
561         break;
562
4e17e6 563       case 'previouspage':
T 564         this.list_page('prev');
d17008 565         break;
S 566
567       case 'firstpage':
568         this.list_page('first');
15a9d1 569         break;
T 570
571       case 'expunge':
572         if (this.env.messagecount)
573           this.expunge_mailbox(this.env.mailbox);
574         break;
575
5e3512 576       case 'purge':
T 577       case 'empty-mailbox':
578         if (this.env.messagecount)
579           this.purge_mailbox(this.env.mailbox);
4e17e6 580         break;
T 581
582
583       // common commands used in multiple tasks
584       case 'show':
585         if (this.task=='mail')
586           {
587           var uid = this.get_single_uid();
588           if (uid && (!this.env.uid || uid != this.env.uid))
41fa0b 589             {
6b47de 590             if (this.env.mailbox == this.env.drafts_mailbox)
T 591               this.goto_url('compose', '_draft_uid='+uid+'&_mbox='+urlencode(this.env.mailbox), true);
1966c5 592             else
S 593               this.show_message(uid);
41fa0b 594             }
4e17e6 595           }
T 596         else if (this.task=='addressbook')
597           {
598           var cid = props ? props : this.get_single_cid();
599           if (cid && !(this.env.action=='show' && cid==this.env.cid))
600             this.load_contact(cid, 'show');
601           }
602         break;
603
604       case 'add':
605         if (this.task=='addressbook')
6b47de 606           this.load_contact(0, 'add');
4e17e6 607         else if (this.task=='settings')
T 608           {
6b47de 609           this.identity_list.clear_selection();
4e17e6 610           this.load_identity(0, 'add-identity');
T 611           }
612         break;
613
614       case 'edit':
615         var cid;
616         if (this.task=='addressbook' && (cid = this.get_single_cid()))
617           this.load_contact(cid, 'edit');
618         else if (this.task=='settings' && props)
619           this.load_identity(props, 'edit-identity');
620         break;
621
622       case 'save-identity':
623       case 'save':
624         if (this.gui_objects.editform)
10a699 625           {
T 626           var input_pagesize = rcube_find_object('_pagesize');
627           var input_name  = rcube_find_object('_name');
628           var input_email = rcube_find_object('_email');
629
630           // user prefs
e66f5b 631           if (input_pagesize && isNaN(input_pagesize.value))
10a699 632             {
T 633             alert(this.get_label('nopagesizewarning'));
634             input_pagesize.focus();
635             break;
636             }
637           // contacts/identities
638           else
639             {
640             if (input_name && input_name.value == '')
641               {
642               alert(this.get_label('nonamewarning'));
643               input_name.focus();
644               break;
645               }
646             else if (input_email && !rcube_check_email(input_email.value))
647               {
648               alert(this.get_label('noemailwarning'));
649               input_email.focus();
650               break;
651               }
652             }
653
4e17e6 654           this.gui_objects.editform.submit();
10a699 655           }
4e17e6 656         break;
T 657
658       case 'delete':
659         // mail task
857a38 660         if (this.task=='mail')
4e17e6 661           this.delete_messages();
T 662         // addressbook task
663         else if (this.task=='addressbook')
664           this.delete_contacts();
665         // user settings task
666         else if (this.task=='settings')
667           this.delete_identity();
668         break;
669
670
671       // mail task commands
672       case 'move':
673       case 'moveto':
f11541 674         if (this.task == 'mail')
T 675           this.move_messages(props);
676         else if (this.task == 'addressbook' && this.drag_active)
677           this.copy_contact(null, props);
4e17e6 678         break;
b85bf8 679
T 680       case 'mark':
681         if (props)
682           this.mark_message(props);
683         break;
684       
857a38 685       case 'toggle_status':
4e17e6 686         if (props && !props._row)
T 687           break;
688         
689         var uid;
690         var flag = 'read';
691         
692         if (props._row.uid)
693           {
694           uid = props._row.uid;
6b47de 695           this.message_list.dont_select = true;
4e17e6 696           // toggle read/unread
6b47de 697           if (this.message_list.rows[uid].deleted) {
T 698             flag = 'undelete';
699           } else if (!this.message_list.rows[uid].unread)
4e17e6 700             flag = 'unread';
T 701           }
702           
703         this.mark_message(flag, uid);
704         break;
705         
706       case 'load-images':
707         if (this.env.uid)
b19097 708           this.show_message(this.env.uid, true, this.env.action=='preview');
4e17e6 709         break;
T 710
711       case 'load-attachment':
6b47de 712         var qstring = '_mbox='+this.env.mailbox+'&_uid='+this.env.uid+'&_part='+props.part;
4e17e6 713         
T 714         // open attachment in frame if it's of a supported mimetype
715         if (this.env.uid && props.mimetype && find_in_array(props.mimetype, this.mimetypes)>=0)
716           {
cfdf04 717           if (props.mimetype == 'text/html')
853b2e 718             qstring += '&_safe=1';
b19097 719           this.attachment_win = window.open(this.env.comm_path+'&_action=get&'+qstring+'&_frame=1', 'rcubemailattachment');
4e17e6 720           if (this.attachment_win)
T 721             {
cfdf04 722             setTimeout(function(){ ref.attachment_win.focus(); }, 10);
4e17e6 723             break;
T 724             }
725           }
726
4b9efb 727         this.goto_url('get', qstring+'&_download=1', false);
4e17e6 728         break;
T 729         
730       case 'select-all':
6b47de 731         this.message_list.select_all(props);
4e17e6 732         break;
T 733
734       case 'select-none':
6b47de 735         this.message_list.clear_selection();
4e17e6 736         break;
T 737
738       case 'nextmessage':
739         if (this.env.next_uid)
b19097 740           this.show_message(this.env.next_uid, false, this.env.action=='preview');
4e17e6 741         break;
T 742
a7d5c6 743       case 'lastmessage':
d17008 744         if (this.env.last_uid)
S 745           this.show_message(this.env.last_uid);
746         break;
747
4e17e6 748       case 'previousmessage':
T 749         if (this.env.prev_uid)
b19097 750           this.show_message(this.env.prev_uid, false, this.env.action=='preview');
d17008 751         break;
S 752
753       case 'firstmessage':
754         if (this.env.first_uid)
755           this.show_message(this.env.first_uid);
4e17e6 756         break;
d1d2c4 757       
c8c1e0 758       case 'checkmail':
S 759         this.check_for_recent();
760         break;
d1d2c4 761       
4e17e6 762       case 'compose':
T 763         var url = this.env.comm_path+'&_action=compose';
1966c5 764        
S 765         if (this.task=='mail' && this.env.mailbox==this.env.drafts_mailbox)
766           {
aade7b 767           var uid;
T 768           if (uid = this.get_single_uid())
4d4264 769             url += '&_draft_uid='+uid+'&_mbox='+urlencode(this.env.mailbox);
f11541 770           }
4e17e6 771         // modify url if we're in addressbook
1966c5 772         else if (this.task=='addressbook')
4e17e6 773           {
f11541 774           // switch to mail compose step directly
T 775           if (props && props.indexOf('@') > 0)
776           {
777             url = this.get_task_url('mail', url);
778             this.redirect(url + '&_to='+urlencode(props));
779             break;
780           }
4e17e6 781           
T 782           // use contact_id passed as command parameter
f11541 783           var a_cids = new Array();
4e17e6 784           if (props)
T 785             a_cids[a_cids.length] = props;
786           // get selected contacts
f11541 787           else if (this.contact_list)
4e17e6 788             {
6b47de 789             var selection = this.contact_list.get_selection();
T 790             for (var n=0; n<selection.length; n++)
791               a_cids[a_cids.length] = selection[n];
4e17e6 792             }
f11541 793             
4e17e6 794           if (a_cids.length)
f11541 795             this.http_request('mailto', '_cid='+urlencode(a_cids.join(','))+'&_source='+urlencode(this.env.source), true);
T 796
797           break;
4e17e6 798           }
T 799         else if (props)
6b47de 800            url += '&_to='+urlencode(props);
bc8dc6 801
d1d2c4 802         // don't know if this is necessary...
S 803         url = url.replace(/&_framed=1/, "");
804
f11541 805         this.redirect(url);
ed5d29 806         break;
T 807         
808       case 'spellcheck':
e170b4 809         if (this.env.spellcheck && this.env.spellcheck.spellCheck && this.spellcheck_ready)
T 810           {
ed5d29 811           this.env.spellcheck.spellCheck(this.env.spellcheck.check_link);
e170b4 812           this.set_spellcheck_state('checking');
T 813           }
ed5d29 814         break;
4e17e6 815
1966c5 816       case 'savedraft':
41fa0b 817         // Reset the auto-save timer
T 818         self.clearTimeout(this.save_timer);
f0f98f 819
1966c5 820         if (!this.gui_objects.messageform)
S 821           break;
f0f98f 822
41fa0b 823         // if saving Drafts is disabled in main.inc.php
e170b4 824         // or if compose form did not change
T 825         if (!this.env.drafts_mailbox || this.cmp_hash == this.compose_field_hash())
41fa0b 826           break;
f0f98f 827
1966c5 828         this.set_busy(true, 'savingmessage');
S 829         var form = this.gui_objects.messageform;
41fa0b 830         form.target = "savetarget";
4d0413 831         form._draft.value = '1';
1966c5 832         form.submit();
S 833         break;
834
4e17e6 835       case 'send':
T 836         if (!this.gui_objects.messageform)
837           break;
41fa0b 838
977a29 839         if (!this.check_compose_input())
10a699 840           break;
4315b0 841
9a5261 842         // Reset the auto-save timer
T 843         self.clearTimeout(this.save_timer);
10a699 844
T 845         // all checks passed, send message
846         this.set_busy(true, 'sendingmessage');
847         var form = this.gui_objects.messageform;
41fa0b 848         form.target = "savetarget";     
T 849         form._draft.value = '';
10a699 850         form.submit();
9a5261 851         
T 852         // clear timeout (sending could take longer)
853         clearTimeout(this.request_timer);
4e17e6 854         break;
T 855
856       case 'add-attachment':
857         this.show_attachment_form(true);
858         
859       case 'send-attachment':
f0f98f 860         // Reset the auto-save timer
41fa0b 861         self.clearTimeout(this.save_timer);
f0f98f 862
4e17e6 863         this.upload_file(props)      
T 864         break;
a894ba 865       
S 866       case 'remove-attachment':
867         this.remove_attachment(props);
868         break;
4e17e6 869
583f1c 870       case 'reply-all':
4e17e6 871       case 'reply':
T 872         var uid;
873         if (uid = this.get_single_uid())
6b47de 874           this.goto_url('compose', '_reply_uid='+uid+'&_mbox='+urlencode(this.env.mailbox)+(command=='reply-all' ? '&_all=1' : ''), true);
4e17e6 875         break;      
T 876
877       case 'forward':
878         var uid;
879         if (uid = this.get_single_uid())
6b47de 880           this.goto_url('compose', '_forward_uid='+uid+'&_mbox='+urlencode(this.env.mailbox), true);
4e17e6 881         break;
T 882         
883       case 'print':
884         var uid;
885         if (uid = this.get_single_uid())
886           {
cfdf04 887           ref.printwin = window.open(this.env.comm_path+'&_action=print&_uid='+uid+'&_mbox='+urlencode(this.env.mailbox)+(this.env.safemode ? '&_safe=1' : ''));
4e17e6 888           if (this.printwin)
cfdf04 889             setTimeout(function(){ ref.printwin.focus(); }, 20);
4e17e6 890           }
T 891         break;
892
893       case 'viewsource':
894         var uid;
895         if (uid = this.get_single_uid())
cfdf04 896           {
T 897           ref.sourcewin = window.open(this.env.comm_path+'&_action=viewsource&_uid='+this.env.uid+'&_mbox='+urlencode(this.env.mailbox));
4e17e6 898           if (this.sourcewin)
cfdf04 899             setTimeout(function(){ ref.sourcewin.focus(); }, 20);
4e17e6 900           }
T 901         break;
902
903       case 'add-contact':
904         this.add_contact(props);
905         break;
d1d2c4 906       
f11541 907       // quicksearch
4647e1 908       case 'search':
T 909         if (!props && this.gui_objects.qsearchbox)
910           props = this.gui_objects.qsearchbox.value;
911         if (props)
f11541 912         {
T 913           this.qsearch(props);
914           break;
915         }
4647e1 916
T 917       // reset quicksearch        
918       case 'reset-search':
919         var s = this.env.search_request;
920         this.reset_qsearch();
921         
f11541 922         if (s && this.env.mailbox)
4647e1 923           this.list_mailbox(this.env.mailbox);
f11541 924         else if (s && this.task == 'addressbook')
T 925           this.list_contacts(this.env.source);
4647e1 926         break;
4e17e6 927
T 928
929       // user settings commands
930       case 'preferences':
6b47de 931         this.goto_url('');
4e17e6 932         break;
T 933
934       case 'identities':
6b47de 935         this.goto_url('identities');
4e17e6 936         break;
T 937           
938       case 'delete-identity':
939         this.delete_identity();
940         
941       case 'folders':
6b47de 942         this.goto_url('folders');
4e17e6 943         break;
T 944
945       case 'subscribe':
946         this.subscribe_folder(props);
947         break;
948
949       case 'unsubscribe':
950         this.unsubscribe_folder(props);
951         break;
952         
953       case 'create-folder':
954         this.create_folder(props);
c8c1e0 955         break;
S 956
957       case 'rename-folder':
958         this.rename_folder(props);
4e17e6 959         break;
T 960
961       case 'delete-folder':
68b6a9 962         this.delete_folder(props);
4e17e6 963         break;
T 964
965       }
966
967     return obj ? false : true;
968     };
969
970
971   // set command enabled or disabled
972   this.enable_command = function()
973     {
1c5853 974     var args = arguments;
4e17e6 975     if(!args.length) return -1;
T 976
977     var command;
978     var enable = args[args.length-1];
979     
980     for(var n=0; n<args.length-1; n++)
981       {
982       command = args[n];
983       this.commands[command] = enable;
984       this.set_button(command, (enable ? 'act' : 'pas'));
985       }
1c5853 986       return true;
4e17e6 987     };
T 988
989
a95e0e 990   // lock/unlock interface
4e17e6 991   this.set_busy = function(a, message)
T 992     {
993     if (a && message)
10a699 994       {
T 995       var msg = this.get_label(message);
996       if (msg==message)        
997         msg = 'Loading...';
998
999       this.display_message(msg, 'loading', true);
1000       }
258f1e 1001     else if (!a)
4e17e6 1002       this.hide_message();
T 1003
1004     this.busy = a;
1005     //document.body.style.cursor = a ? 'wait' : 'default';
1006     
1007     if (this.gui_objects.editform)
1008       this.lock_form(this.gui_objects.editform, a);
a95e0e 1009       
T 1010     // clear pending timer
1011     if (this.request_timer)
1012       clearTimeout(this.request_timer);
1013
1014     // set timer for requests
9a5261 1015     if (a && this.env.request_timeout)
cfdf04 1016       this.request_timer = setTimeout(function(){ ref.request_timed_out(); }, this.env.request_timeout * 1000);
4e17e6 1017     };
T 1018
1019
10a699 1020   // return a localized string
T 1021   this.get_label = function(name)
1022     {
1023     if (this.labels[name])
1024       return this.labels[name];
1025     else
1026       return name;
1027     };
1028
1029
1030   // switch to another application task
4e17e6 1031   this.switch_task = function(task)
T 1032     {
01bb03 1033     if (this.task===task && task!='mail')
4e17e6 1034       return;
T 1035
01bb03 1036     var url = this.get_task_url(task);
T 1037     if (task=='mail')
1038       url += '&_mbox=INBOX';
1039
f11541 1040     this.redirect(url);
4e17e6 1041     };
T 1042
1043
1044   this.get_task_url = function(task, url)
1045     {
1046     if (!url)
1047       url = this.env.comm_path;
1048
1049     return url.replace(/_task=[a-z]+/, '_task='+task);
a95e0e 1050     };
T 1051     
1052   
1053   // called when a request timed out
1054   this.request_timed_out = function()
1055     {
1056     this.set_busy(false);
1057     this.display_message('Request timed out!', 'error');
4e17e6 1058     };
T 1059
1060
1061   /*********************************************************/
1062   /*********        event handling methods         *********/
1063   /*********************************************************/
1064
1065
6b47de 1066   this.doc_mouse_up = function(e)
T 1067     {
1068     if (this.message_list)
1069       this.message_list.blur();
1070     else if (this.contact_list)
1071       this.contact_list.blur();
1072     };
1073
f11541 1074   this.focus_folder = function(id)
T 1075     {
1076     var li;
1077     if (this.drag_active && this.check_droptarget(id) && (li = this.get_folder_li(id)))
1078       this.set_classname(li, 'droptarget', true);
1079     }
6b47de 1080
f11541 1081   this.unfocus_folder = function(id)
T 1082     {
1083     var li;
1084     if (this.drag_active && (li = this.get_folder_li(id)))
1085       this.set_classname(li, 'droptarget', false);
1086     }
1087
1088   // onmouseup handler for folder list item
1089   this.folder_mouse_up = function(id)
4e17e6 1090     {
T 1091     if (this.drag_active)
fe79b1 1092       {
f11541 1093       this.unfocus_folder(id);
T 1094       this.command('moveto', id);
fe79b1 1095       }
4b9efb 1096
S 1097     // Hide message command buttons until a message is selected 
b85bf8 1098     this.enable_command('reply', 'reply-all', 'forward', 'delete', 'mark', 'print', false); 
4e17e6 1099     return false;
T 1100     };
1101
6b47de 1102   this.click_on_list = function(e)
4e17e6 1103     {
6b47de 1104     if (this.message_list)
T 1105       this.message_list.focus();
1106     else if (this.contact_list)
1107         this.contact_list.focus();
4e17e6 1108
6b47de 1109     var mbox_li;
f11541 1110     if (mbox_li = this.get_folder_li())
6b47de 1111       this.set_classname(mbox_li, 'unfocused', true);
4e17e6 1112
6b47de 1113     rcube_event.cancel(e);
4e17e6 1114     };
T 1115
1116
6b47de 1117   this.msglist_select = function(list)
4e17e6 1118     {
b19097 1119     if (this.preview_timer)
T 1120       clearTimeout(this.preview_timer);
1121
6b47de 1122     var selected = list.selection.length==1;
4b9efb 1123
S 1124     // Hide certain command buttons when Drafts folder is selected
6b47de 1125     if (this.env.mailbox == this.env.drafts_mailbox)
4e17e6 1126       {
4b9efb 1127       this.enable_command('reply', 'reply-all', 'forward', false);
301454 1128       this.enable_command('show', selected);
b85bf8 1129       this.enable_command('delete', 'moveto', 'mark', (list.selection.length > 0 ? true : false));
4e17e6 1130       }
6b47de 1131     else
4e17e6 1132       {
301454 1133       this.enable_command('show', 'reply', 'reply-all', 'forward', 'print', selected);
b85bf8 1134       this.enable_command('delete', 'moveto', 'mark', (list.selection.length > 0 ? true : false));
4e17e6 1135       }
068f6a 1136
S 1137     // start timer for message preview (wait for double click)
1138     if (selected && this.env.contentframe)
1139       this.preview_timer = setTimeout(function(){ ref.msglist_get_preview(); }, this.dblclick_time + 10);
1140     else if (this.env.contentframe)
f11541 1141       this.show_contentframe(false);
b19097 1142     };
d1d2c4 1143
S 1144
6b47de 1145   this.msglist_dbl_click = function(list)
T 1146     {
b19097 1147       if (this.preview_timer)
T 1148         clearTimeout(this.preview_timer);
1149
6b47de 1150     var uid = list.get_single_selection();
T 1151     if (uid && this.env.mailbox == this.env.drafts_mailbox)
1152       this.goto_url('compose', '_draft_uid='+uid+'&_mbox='+urlencode(this.env.mailbox), true);
1153     else if (uid)
b19097 1154       this.show_message(uid, false, false);
6b47de 1155     };
4e17e6 1156
6b47de 1157
T 1158   this.msglist_keypress = function(list)
1159     {
1160     if (list.key_pressed == list.ENTER_KEY)
1161       this.command('show');
1162     else if (list.key_pressed == list.DELETE_KEY)
1163       this.command('delete');
110748 1164     else
T 1165       list.shiftkey = false;
4e17e6 1166     };
T 1167
1168
b19097 1169   this.msglist_get_preview = function()
T 1170   {
1171     var uid = this.get_single_uid();
f11541 1172     if (uid && this.env.contentframe && !this.drag_active)
b19097 1173       this.show_message(uid, false, true);
T 1174     else if (this.env.contentframe)
f11541 1175       this.show_contentframe(false);
T 1176   };
1177   
1178   
1179   this.check_droptarget = function(id)
1180   {
1181     if (this.task == 'mail')
1182       return (id != this.env.mailbox);
1183     else if (this.task == 'addressbook')
1184       return (id != this.env.source && this.env.address_sources[id] && !this.env.address_sources[id].readonly);
b0dbf3 1185     else if (this.task == 'settings')
S 1186       return (id != this.env.folder);
b19097 1187   };
T 1188
4e17e6 1189
T 1190   /*********************************************************/
1191   /*********     (message) list functionality      *********/
1192   /*********************************************************/
1193
1194
1195   // when user doble-clicks on a row
b19097 1196   this.show_message = function(id, safe, preview)
4e17e6 1197     {
T 1198     var add_url = '';
b19097 1199     var action = preview ? 'preview': 'show';
4e17e6 1200     var target = window;
b19097 1201     if (preview && this.env.contentframe && window.frames && window.frames[this.env.contentframe])
4e17e6 1202       {
T 1203       target = window.frames[this.env.contentframe];
1204       add_url = '&_framed=1';
1205       }
6b47de 1206
4e17e6 1207     if (safe)
T 1208       add_url = '&_safe=1';
1209
1f020b 1210     // also send search request to get the right messages
S 1211     if (this.env.search_request)
1212       add_url += '&_search='+this.env.search_request;
1213
4e17e6 1214     if (id)
T 1215       {
b19097 1216       var url = '&_action='+action+'&_uid='+id+'&_mbox='+urlencode(this.env.mailbox)+add_url;
T 1217       if (action == 'preview' && String(target.location.href).indexOf(url) >= 0)
f11541 1218         this.show_contentframe(true);
b19097 1219       else
T 1220         {
1221         this.set_busy(true, 'loading');
1222         target.location.href = this.env.comm_path+url;
1223         }
4e17e6 1224       }
T 1225     };
1226
b19097 1227
f11541 1228   this.show_contentframe = function(show)
b19097 1229     {
T 1230     var frm;
1231     if (this.env.contentframe && (frm = rcube_find_object(this.env.contentframe)))
1232       {
f11541 1233       if (!show && window.frames[this.env.contentframe] && frames[this.env.contentframe].location.href.indexOf(this.env.blankpage)<0)
T 1234         frames[this.env.contentframe].location.href = this.env.blankpage;
1235       if (!bw.safari)
1236         frm.style.display = show ? 'block' : 'none';
b19097 1237       }
T 1238       
1239     if (!show && this.busy)
1240       this.set_busy(false);
1241     };
4e17e6 1242
T 1243
1244   // list a specific page
1245   this.list_page = function(page)
1246     {
1247     if (page=='next')
1248       page = this.env.current_page+1;
d17008 1249     if (page=='last')
S 1250       page = this.env.pagecount;
4e17e6 1251     if (page=='prev' && this.env.current_page>1)
T 1252       page = this.env.current_page-1;
d17008 1253     if (page=='first' && this.env.current_page>1)
S 1254       page = 1;
4e17e6 1255       
T 1256     if (page > 0 && page <= this.env.pagecount)
1257       {
1258       this.env.current_page = page;
1259       
1260       if (this.task=='mail')
1261         this.list_mailbox(this.env.mailbox, page);
1262       else if (this.task=='addressbook')
f11541 1263         this.list_contacts(this.env.source, page);
4e17e6 1264       }
T 1265     };
1266
1267
1268   // list messages of a specific mailbox
f3b659 1269   this.list_mailbox = function(mbox, page, sort)
4e17e6 1270     {
cbd62d 1271     this.last_selected = 0;
4e17e6 1272     var add_url = '';
T 1273     var target = window;
1274
1275     if (!mbox)
1276       mbox = this.env.mailbox;
1277
f3b659 1278     // add sort to url if set
T 1279     if (sort)
1280       add_url += '&_sort=' + sort;
f11541 1281
T 1282     // also send search request to get the right messages
1283     if (this.env.search_request)
1284       add_url += '&_search='+this.env.search_request;
f3b659 1285       
4e17e6 1286     // set page=1 if changeing to another mailbox
T 1287     if (!page && mbox != this.env.mailbox)
1288       {
1289       page = 1;
1290       this.env.current_page = page;
bef5ca 1291       if (this.message_list)
T 1292         this.message_list.clear_selection();
f11541 1293       this.show_contentframe(false);
4e17e6 1294       }
4647e1 1295     
06895c 1296     if (mbox != this.env.mailbox || (mbox == this.env.mailbox && !page && !sort))
T 1297       add_url += '&_refresh=1';
1298     
f11541 1299     this.select_folder(mbox, this.env.mailbox);
T 1300     this.env.mailbox = mbox;
4e17e6 1301
T 1302     // load message list remotely
1303     if (this.gui_objects.messagelist)
1304       {
9fee0e 1305       this.list_mailbox_remote(mbox, page, add_url);
4e17e6 1306       return;
T 1307       }
1308     
1309     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
1310       {
1311       target = window.frames[this.env.contentframe];
9fee0e 1312       add_url += '&_framed=1';
4e17e6 1313       }
T 1314
1315     // load message list to target frame/window
1316     if (mbox)
1317       {
1318       this.set_busy(true, 'loading');
4d4264 1319       target.location.href = this.env.comm_path+'&_mbox='+urlencode(mbox)+(page ? '&_page='+page : '')+add_url;
4e17e6 1320       }
T 1321     };
1322
1323
1324   // send remote request to load message list
9fee0e 1325   this.list_mailbox_remote = function(mbox, page, add_url)
4e17e6 1326     {
15a9d1 1327     // clear message list first
6b47de 1328     this.message_list.clear();
15a9d1 1329
T 1330     // send request to server
4d4264 1331     var url = '_mbox='+urlencode(mbox)+(page ? '&_page='+page : '');
15a9d1 1332     this.set_busy(true, 'loading');
T 1333     this.http_request('list', url+add_url, true);
c8c1e0 1334     };
24053e 1335
15a9d1 1336
T 1337   this.expunge_mailbox = function(mbox)
1338     {
1339     var lock = false;
1340     var add_url = '';
1341     
1342     // lock interface if it's the active mailbox
1343     if (mbox == this.env.mailbox)
1344        {
1345        lock = true;
1346        this.set_busy(true, 'loading');
1347        add_url = '&_reload=1';
1348        }
4e17e6 1349
T 1350     // send request to server
4d4264 1351     var url = '_mbox='+urlencode(mbox);
8d0758 1352     this.http_post('expunge', url+add_url, lock);
4e17e6 1353     };
T 1354
1355
5e3512 1356   this.purge_mailbox = function(mbox)
T 1357     {
1358     var lock = false;
1359     var add_url = '';
1360     
1361     if (!confirm(this.get_label('purgefolderconfirm')))
1362       return false;
1363     
1364     // lock interface if it's the active mailbox
1365     if (mbox == this.env.mailbox)
1366        {
1367        lock = true;
1368        this.set_busy(true, 'loading');
1369        add_url = '&_reload=1';
1370        }
1371
1372     // send request to server
4d4264 1373     var url = '_mbox='+urlencode(mbox);
8d0758 1374     this.http_post('purge', url+add_url, lock);
1c5853 1375     return true;
5e3512 1376     };
f11541 1377
fe79b1 1378   
4e17e6 1379   // move selected messages to the specified mailbox
T 1380   this.move_messages = function(mbox)
1381     {
e4bbb2 1382     // exit if current or no mailbox specified or if selection is empty
S 1383     if (!mbox || !this.env.uid || mbox==this.env.mailbox)
1384       {
1385       if (!this.message_list || !this.message_list.get_selection().length)
1386         return;
1387       }
1388
ecf759 1389     var lock = false;
cfdf04 1390     var add_url = '&_target_mbox='+urlencode(mbox)+'&_from='+(this.env.action ? this.env.action : '');
4e17e6 1391
T 1392     // show wait message
1393     if (this.env.action=='show')
ecf759 1394       {
T 1395       lock = true;
4e17e6 1396       this.set_busy(true, 'movingmessage');
ecf759 1397       }
cfdf04 1398     else
f11541 1399       this.show_contentframe(false);
6b47de 1400
cfdf04 1401     this._with_selected_messages('moveto', lock, add_url);
4e17e6 1402     };
T 1403
857a38 1404   // delete selected messages from the current mailbox
S 1405   this.delete_messages = function()
1406     {
1c7b97 1407     var selection = this.message_list ? this.message_list.get_selection() : new Array();
T 1408     
857a38 1409     // exit if no mailbox specified or if selection is empty
1c7b97 1410     if (!this.env.uid && !selection.length)
e4bbb2 1411         return;
6b47de 1412
857a38 1413     // if there is a trash mailbox defined and we're not currently in it:
1c7b97 1414     if (this.env.trash_mailbox && String(this.env.mailbox).toLowerCase() != String(this.env.trash_mailbox).toLowerCase())
cfdf04 1415       {
31c171 1416       // if shift was pressed delete it immediately
086377 1417       if (this.message_list && this.message_list.shiftkey)
31c171 1418         {
S 1419         if (confirm(this.get_label('deletemessagesconfirm')))
1420           this.permanently_remove_messages();
1421         }
1422       else
1423         this.move_messages(this.env.trash_mailbox);
cfdf04 1424       }
857a38 1425     // if there is a trash mailbox defined but we *are* in it:
S 1426     else if (this.env.trash_mailbox && String(this.env.mailbox).toLowerCase() == String(this.env.trash_mailbox).toLowerCase())
1427       this.permanently_remove_messages();
1428     // if there isn't a defined trash mailbox and the config is set to flag for deletion
6b47de 1429     else if (!this.env.trash_mailbox && this.env.flag_for_deletion)
T 1430       {
1c7b97 1431       this.mark_message('delete');
6b47de 1432       if(this.env.action=="show")
dab065 1433         this.command('nextmessage','',this);
6b47de 1434       else if (selection.length == 1)
T 1435         this.message_list.select_next();
dab065 1436       }
857a38 1437     // if there isn't a defined trash mailbox and the config is set NOT to flag for deletion
6b47de 1438     else if (!this.env.trash_mailbox) 
857a38 1439       this.permanently_remove_messages();
S 1440   };
cfdf04 1441
T 1442
1443   // delete the selected messages permanently
1444   this.permanently_remove_messages = function()
1445     {
1446     // exit if no mailbox specified or if selection is empty
1447     if (!this.env.uid && (!this.message_list || !this.message_list.get_selection().length))
1448       return;
1c7b97 1449       
f11541 1450     this.show_contentframe(false);
cfdf04 1451     this._with_selected_messages('delete', false, '&_from='+(this.env.action ? this.env.action : ''));
T 1452     };
1453
1454   // Send a specifc request with UIDs of all selected messages
1455   // @private
1456   this._with_selected_messages = function(action, lock, add_url)
1457     {
1458     var a_uids = new Array();
1459     if (this.env.uid)
1460       a_uids[a_uids.length] = this.env.uid;
1461     else
1462       {
1463       var selection = this.message_list.get_selection();
1464       var id;
1465       for (var n=0; n<selection.length; n++)
1466         {
1467         id = selection[n];
1468         a_uids[a_uids.length] = id;
b62656 1469         this.message_list.remove_row(id, (n == selection.length-1));
cfdf04 1470         }
T 1471       }
f11541 1472       
T 1473     // also send search request to get the right messages 
1474     if (this.env.search_request) 
cfdf04 1475       add_url += '&_search='+this.env.search_request;
T 1476
1477     // send request to server
8d0758 1478     this.http_post(action, '_uid='+a_uids.join(',')+'&_mbox='+urlencode(this.env.mailbox)+add_url, lock);
cfdf04 1479     };
4e17e6 1480
T 1481
1482   // set a specific flag to one or more messages
1483   this.mark_message = function(flag, uid)
1484     {
1485     var a_uids = new Array();
b19097 1486     var selection = this.message_list ? this.message_list.get_selection() : new Array();
4e17e6 1487     
T 1488     if (uid)
1489       a_uids[0] = uid;
1490     else if (this.env.uid)
1491       a_uids[0] = this.env.uid;
1c7b97 1492     else if (this.message_list)
4e17e6 1493       {
1c7b97 1494       for (var id, n=0; n<selection.length; n++)
4e17e6 1495         {
6b47de 1496         id = selection[n];
1c7b97 1497         if ((flag=='read' && this.message_list.rows[id].unread) || (flag=='unread' && !this.message_list.rows[id].unread)
T 1498             || (flag=='delete' && !this.message_list.rows[id].deleted) || (flag=='undelete' && this.message_list.rows[id].deleted))
1a98a6 1499           a_uids[a_uids.length] = id;
4e17e6 1500         }
T 1501       }
1a98a6 1502     
T 1503     // nothing to do
1504     if (!a_uids.length)
1505       return;
6b47de 1506       
T 1507     switch (flag)
1508       {
857a38 1509         case 'read':
S 1510         case 'unread':
6b47de 1511           this.toggle_read_status(flag, a_uids);
857a38 1512           break;
S 1513         case 'delete':
1514         case 'undelete':
1c5853 1515           this.toggle_delete_status(a_uids);
857a38 1516           break;
S 1517       }
1518     };
4e17e6 1519
857a38 1520   // set class to read/unread
6b47de 1521   this.toggle_read_status = function(flag, a_uids)
T 1522   {
4e17e6 1523     // mark all message rows as read/unread
T 1524     var icn_src;
6b47de 1525     var rows = this.message_list.rows;
4e17e6 1526     for (var i=0; i<a_uids.length; i++)
T 1527       {
1528       uid = a_uids[i];
6b47de 1529       if (rows[uid])
4e17e6 1530         {
6b47de 1531         rows[uid].unread = (flag=='unread' ? true : false);
4e17e6 1532         
6b47de 1533         if (rows[uid].classname.indexOf('unread')<0 && rows[uid].unread)
4e17e6 1534           {
6b47de 1535           rows[uid].classname += ' unread';
T 1536           this.set_classname(rows[uid].obj, 'unread', true);
fd660a 1537
4e17e6 1538           if (this.env.unreadicon)
T 1539             icn_src = this.env.unreadicon;
1540           }
6b47de 1541         else if (!rows[uid].unread)
4e17e6 1542           {
6b47de 1543           rows[uid].classname = rows[uid].classname.replace(/\s*unread/, '');
T 1544           this.set_classname(rows[uid].obj, 'unread', false);
4e17e6 1545
6b47de 1546           if (rows[uid].replied && this.env.repliedicon)
4e17e6 1547             icn_src = this.env.repliedicon;
T 1548           else if (this.env.messageicon)
1549             icn_src = this.env.messageicon;
1550           }
1551
6b47de 1552         if (rows[uid].icon && icn_src)
T 1553           rows[uid].icon.src = icn_src;
4e17e6 1554         }
T 1555       }
6b47de 1556       
8d0758 1557     this.http_post('mark', '_uid='+a_uids.join(',')+'&_flag='+flag);
6b47de 1558   };
857a38 1559   
S 1560   // mark all message rows as deleted/undeleted
6b47de 1561   this.toggle_delete_status = function(a_uids)
T 1562   {
1563     if (this.env.read_when_deleted)
857a38 1564       this.toggle_read_status('read',a_uids);
6b47de 1565
dab065 1566     // if deleting message from "view message" don't bother with delete icon
S 1567     if (this.env.action == "show")
1568       return false;
4e17e6 1569
6b47de 1570     var rows = this.message_list.rows;
1c7b97 1571     if (a_uids.length==1)
T 1572     {
6b47de 1573       if (rows[a_uids[0]] && rows[a_uids[0]].classname.indexOf('deleted') < 0)
T 1574         this.flag_as_deleted(a_uids);
1575       else
1576         this.flag_as_undeleted(a_uids);
1577
1c5853 1578       return true;
S 1579     }
1580     
1581     var all_deleted = true;
1c7b97 1582     for (var i=0; i<a_uids.length; i++)
T 1583     {
857a38 1584       uid = a_uids[i];
6b47de 1585       if (rows[uid]) {
1c7b97 1586         if (rows[uid].classname.indexOf('deleted')<0)
T 1587         {
1c5853 1588           all_deleted = false;
S 1589           break;
857a38 1590         }
S 1591       }
1c5853 1592     }
S 1593     
1594     if (all_deleted)
1595       this.flag_as_undeleted(a_uids);
1596     else
1597       this.flag_as_deleted(a_uids);
1598     
1599     return true;
6b47de 1600   };
4e17e6 1601
6b47de 1602
T 1603   this.flag_as_undeleted = function(a_uids)
1604   {
1c5853 1605     // if deleting message from "view message" don't bother with delete icon
S 1606     if (this.env.action == "show")
1607       return false;
1608
1609     var icn_src;
6b47de 1610     var rows = this.message_list.rows;
1c5853 1611       
1c7b97 1612     for (var i=0; i<a_uids.length; i++)
T 1613     {
1c5853 1614       uid = a_uids[i];
6b47de 1615       if (rows[uid]) {
T 1616         rows[uid].deleted = false;
1c5853 1617         
1c7b97 1618         if (rows[uid].classname.indexOf('deleted') > 0)
T 1619         {
6b47de 1620           rows[uid].classname = rows[uid].classname.replace(/\s*deleted/, '');
T 1621           this.set_classname(rows[uid].obj, 'deleted', false);
1c5853 1622         }
6b47de 1623         if (rows[uid].unread && this.env.unreadicon)
1c5853 1624           icn_src = this.env.unreadicon;
6b47de 1625         else if (rows[uid].replied && this.env.repliedicon)
1c5853 1626           icn_src = this.env.repliedicon;
S 1627         else if (this.env.messageicon)
1628           icn_src = this.env.messageicon;
6b47de 1629         if (rows[uid].icon && icn_src)
T 1630           rows[uid].icon.src = icn_src;
1c5853 1631       }
S 1632     }
6b47de 1633
8d0758 1634     this.http_post('mark', '_uid='+a_uids.join(',')+'&_flag=undelete');
1c5853 1635     return true;
6b47de 1636   };
T 1637
1c5853 1638   
6b47de 1639   this.flag_as_deleted = function(a_uids)
T 1640   {
1c5853 1641     // if deleting message from "view message" don't bother with delete icon
S 1642     if (this.env.action == "show")
1643       return false;
1644
6b47de 1645     var rows = this.message_list.rows;
1c7b97 1646     for (var i=0; i<a_uids.length; i++)
T 1647     {
1c5853 1648       uid = a_uids[i];
6b47de 1649       if (rows[uid]) {
T 1650         rows[uid].deleted = true;
1c5853 1651         
6b47de 1652         if (rows[uid].classname.indexOf('deleted')<0) {
T 1653           rows[uid].classname += ' deleted';
1654           this.set_classname(rows[uid].obj, 'deleted', true);
1c5853 1655         }
6b47de 1656         if (rows[uid].icon && this.env.deletedicon)
T 1657           rows[uid].icon.src = this.env.deletedicon;
1c5853 1658       }
S 1659     }
6b47de 1660
8d0758 1661     this.http_post('mark', '_uid='+a_uids.join(',')+'&_flag=delete');
1c5853 1662     return true;  
6b47de 1663   };
fe79b1 1664
T 1665
b566ff 1666   /*********************************************************/
S 1667   /*********           login form methods          *********/
1668   /*********************************************************/
1669
1670   // handler for keyboard events on the _user field
1671   this.login_user_keypress = function(e)
1672   {
9bbb17 1673     var key = rcube_event.get_keycode(e);
T 1674     var elm;
b566ff 1675
S 1676     // enter
9bbb17 1677     if ((key==13) && (elm = rcube_find_object('_pass')))
b566ff 1678     {
9bbb17 1679       elm.focus();
b566ff 1680       return false;
S 1681     }
1682   };
f11541 1683
4e17e6 1684
T 1685   /*********************************************************/
1686   /*********        message compose methods        *********/
1687   /*********************************************************/
1cded8 1688   
T 1689   
977a29 1690   // checks the input fields before sending a message
T 1691   this.check_compose_input = function()
1692     {
1693     // check input fields
1694     var input_to = rcube_find_object('_to');
1695     var input_subject = rcube_find_object('_subject');
1696     var input_message = rcube_find_object('_message');
1697
1698     // check for empty recipient
f20cf0 1699     if (input_to && !rcube_check_email(input_to.value.replace(/^\s+/, '').replace(/[\s,;]+$/, ''), true))
977a29 1700       {
T 1701       alert(this.get_label('norecipientwarning'));
1702       input_to.focus();
1703       return false;
1704       }
1705
1706     // display localized warning for missing subject
1707     if (input_subject && input_subject.value == '')
1708       {
1709       var subject = prompt(this.get_label('nosubjectwarning'), this.get_label('nosubject'));
1710
1711       // user hit cancel, so don't send
1712       if (!subject && subject !== '')
1713         {
1714         input_subject.focus();
1715         return false;
1716         }
1717       else
1718         {
1719         input_subject.value = subject ? subject : this.get_label('nosubject');            
1720         }
1721       }
1722
1723     // check for empty body
67eda0 1724     if ((input_message.value == '' && (!window.tinyMCE || tinyMCE.getContent() == '')) && !confirm(this.get_label('nobodywarning')))
977a29 1725       {
31d9ef 1726       input_message.focus();
T 1727       return false;
977a29 1728       }
T 1729
1730     return true;
1731     };
41fa0b 1732
T 1733
e170b4 1734   this.set_spellcheck_state = function(s)
T 1735     {
86958f 1736     this.spellcheck_ready = (s=='check_spelling' || s=='ready');
e170b4 1737     this.enable_command('spellcheck', this.spellcheck_ready);
86958f 1738     };
e170b4 1739
T 1740
f11541 1741   this.set_draft_id = function(id)
T 1742     {
1743     var f;
1744     if (f = rcube_find_object('_draft_saveid'))
1745       f.value = id;
1746     };
1747
f0f98f 1748   this.auto_save_start = function()
S 1749     {
9a5261 1750     if (this.env.draft_autosave)
cfdf04 1751       this.save_timer = self.setTimeout(function(){ ref.command("savedraft"); }, this.env.draft_autosave * 1000);
4b9efb 1752
S 1753     // Unlock interface now that saving is complete
1754     this.busy = false;
41fa0b 1755     };
T 1756
1757
f11541 1758   this.compose_field_hash = function(save)
977a29 1759     {
T 1760     // check input fields
1761     var input_to = rcube_find_object('_to');
1762     var input_cc = rcube_find_object('_to');
1763     var input_bcc = rcube_find_object('_to');
1764     var input_subject = rcube_find_object('_subject');
1765     var input_message = rcube_find_object('_message');
1766     
1767     var str = '';
1768     if (input_to && input_to.value)
1769       str += input_to.value+':';
1770     if (input_cc && input_cc.value)
1771       str += input_cc.value+':';
1772     if (input_bcc && input_bcc.value)
1773       str += input_bcc.value+':';
1774     if (input_subject && input_subject.value)
1775       str += input_subject.value+':';
1776     if (input_message && input_message.value)
1777       str += input_message.value;
f11541 1778     
T 1779     if (save)
1780       this.cmp_hash = str;
1781     
977a29 1782     return str;
T 1783     };
1784     
1785   
1cded8 1786   this.change_identity = function(obj)
T 1787     {
1788     if (!obj || !obj.options)
1789       return false;
1790
1791     var id = obj.options[obj.selectedIndex].value;
1792     var input_message = rcube_find_object('_message');
1793     var message = input_message ? input_message.value : '';
a0109c 1794     var is_html = (rcube_find_object('_is_html').value == '1');
749b07 1795     var sig, p;
1cded8 1796
1966c5 1797     if (!this.env.identity)
S 1798       this.env.identity = id
a0109c 1799   
S 1800     if (!is_html)
1cded8 1801       {
a0109c 1802       // remove the 'old' signature
S 1803       if (this.env.identity && this.env.signatures && this.env.signatures[this.env.identity])
1804         {
1805         sig = this.env.signatures[this.env.identity]['text'];
dd792e 1806         if (sig.indexOf('-- ')!=0)
S 1807           sig = '-- \n'+sig;
1808
a0109c 1809         p = message.lastIndexOf(sig);
S 1810         if (p>=0)
1811           message = message.substring(0, p-1) + message.substring(p+sig.length, message.length);
1812         }
dd792e 1813
a0109c 1814       // add the new signature string
S 1815       if (this.env.signatures && this.env.signatures[id])
1816         {
1817         sig = this.env.signatures[id]['text'];
dd792e 1818         if (this.env.signatures[id]['is_html'])
S 1819           {
1820           sig = this.env.signatures[id]['plain_text'];
1821           }
1822         if (sig.indexOf('-- ')!=0)
1823           sig = '-- \n'+sig;
a0109c 1824         message += '\n'+sig;
S 1825         }
1cded8 1826       }
a0109c 1827     else
1cded8 1828       {
dd792e 1829       var eid = tinyMCE.getEditorId('_message');
S 1830       // editor is a TinyMCE_Control object
1831       var editor = tinyMCE.getInstanceById(eid);
610898 1832       // if this is null, we should exit
T 1833       if (editor == null) {
1834         return false;
1835       }
dd792e 1836       var msgDoc = editor.getDoc();
S 1837       var msgBody = msgDoc.body;
a0109c 1838
dd792e 1839       if (this.env.signatures && this.env.signatures[id])
S 1840         {
1841         // Append the signature as a span within the body
1842         var sigElem = msgDoc.getElementById("_rc_sig");
1843         if (!sigElem)
a0109c 1844           {
dd792e 1845           sigElem = msgDoc.createElement("span");
S 1846           sigElem.setAttribute("id", "_rc_sig");
1847           msgBody.appendChild(sigElem);
a0109c 1848           }
dd792e 1849         if (this.env.signatures[id]['is_html'])
S 1850           {
1851           sigElem.innerHTML = this.env.signatures[id]['text'];
1852           }
1853         else
1854           {
1855           sigElem.innerHTML = '<pre>' + this.env.signatures[id]['text'] + '</pre>';
1856           }
1857         }
1cded8 1858       }
T 1859
749b07 1860     if (input_message)
1cded8 1861       input_message.value = message;
a0109c 1862
1cded8 1863     this.env.identity = id;
1c5853 1864     return true;
1cded8 1865     };
4e17e6 1866
T 1867
1868   this.show_attachment_form = function(a)
1869     {
1870     if (!this.gui_objects.uploadbox)
1871       return false;
1872       
1873     var elm, list;
1874     if (elm = this.gui_objects.uploadbox)
1875       {
1876       if (a &&  (list = this.gui_objects.attachmentlist))
1877         {
1878         var pos = rcube_get_object_pos(list);
1879         var left = pos.x;
1880         var top = pos.y + list.offsetHeight + 10;
1881       
1882         elm.style.top = top+'px';
1883         elm.style.left = left+'px';
1884         }
1885       
1886       elm.style.visibility = a ? 'visible' : 'hidden';
1887       }
1888       
1889     // clear upload form
480f5d 1890     try {
T 1891       if (!a && this.gui_objects.attachmentform != this.gui_objects.messageform)
1892           this.gui_objects.attachmentform.reset();
1893     }
1894     catch(e){}  // ignore errors
1c5853 1895     
S 1896     return true;  
4e17e6 1897     };
T 1898
1899
1900   // upload attachment file
1901   this.upload_file = function(form)
1902     {
1903     if (!form)
1904       return false;
1905       
1906     // get file input fields
1907     var send = false;
1908     for (var n=0; n<form.elements.length; n++)
1909       if (form.elements[n].type=='file' && form.elements[n].value)
1910         {
1911         send = true;
1912         break;
1913         }
1914     
1915     // create hidden iframe and post upload form
1916     if (send)
1917       {
1918       var ts = new Date().getTime();
1919       var frame_name = 'rcmupload'+ts;
1920
1921       // have to do it this way for IE
1922       // otherwise the form will be posted to a new window
1923       if(document.all && !window.opera)
1924         {
1925         var html = '<iframe name="'+frame_name+'" src="program/blank.gif" style="width:0;height:0;visibility:hidden;"></iframe>';
1926         document.body.insertAdjacentHTML('BeforeEnd',html);
1927         }
1928       else  // for standards-compilant browsers
1929         {
1930         var frame = document.createElement('IFRAME');
1931         frame.name = frame_name;
1932         frame.width = 10;
1933         frame.height = 10;
1934         frame.style.visibility = 'hidden';
1935         document.body.appendChild(frame);
1936         }
1937
1938       form.target = frame_name;
1939       form.action = this.env.comm_path+'&_action=upload';
1940       form.setAttribute('enctype', 'multipart/form-data');
1941       form.submit();
1942       }
1943     
1944     // set reference to the form object
1945     this.gui_objects.attachmentform = form;
1c5853 1946     return true;
4e17e6 1947     };
T 1948
1949
1950   // add file name to attachment list
1951   // called from upload page
9a5261 1952   this.add2attachment_list = function(name, content)
4e17e6 1953     {
T 1954     if (!this.gui_objects.attachmentlist)
1955       return false;
aade7b 1956       
4e17e6 1957     var li = document.createElement('LI');
a894ba 1958     li.id = name;
S 1959     li.innerHTML = content;
4e17e6 1960     this.gui_objects.attachmentlist.appendChild(li);
1c5853 1961     return true;
4e17e6 1962     };
T 1963
a894ba 1964   this.remove_from_attachment_list = function(name)
S 1965     {
1966     if (!this.gui_objects.attachmentlist)
1967       return false;
1968
1969     var list = this.gui_objects.attachmentlist.getElementsByTagName("li");
1970     for (i=0;i<list.length;i++)
1971       if (list[i].id == name)
9a5261 1972         this.gui_objects.attachmentlist.removeChild(list[i]);
41fa0b 1973     };
a894ba 1974
S 1975   this.remove_attachment = function(name)
1976     {
1977     if (name)
8d0758 1978       this.http_post('remove-attachment', '_file='+urlencode(name));
a894ba 1979
S 1980     return true;
41fa0b 1981     };
4e17e6 1982
T 1983   // send remote request to add a new contact
1984   this.add_contact = function(value)
1985     {
1986     if (value)
f11541 1987       this.http_post('addcontact', '_address='+value);
1c5853 1988     
S 1989     return true;
4e17e6 1990     };
T 1991
f11541 1992   // send remote request to search mail or contacts
T 1993   this.qsearch = function(value)
4647e1 1994     {
f11541 1995     if (value != '')
4647e1 1996       {
f11541 1997       if (this.message_list)
T 1998         this.message_list.clear();
1999       else if (this.contact_list) {
2000         this.contact_list.clear(true);
2001         this.show_contentframe(false);
2002       }
2003
2004       // reset vars
2005       this.env.current_page = 1;
4647e1 2006       this.set_busy(true, 'searching');
f11541 2007       this.http_request('search', '_q='+urlencode(value)+(this.env.mailbox ? '&_mbox='+this.env.mailbox : '')+(this.env.source ? '&_source='+urlencode(this.env.source) : ''), true);
4647e1 2008       }
1c5853 2009     return true;
4647e1 2010     };
T 2011
2012   // reset quick-search form
2013   this.reset_qsearch = function()
2014     {
2015     if (this.gui_objects.qsearchbox)
2016       this.gui_objects.qsearchbox.value = '';
2017       
2018     this.env.search_request = null;
1c5853 2019     return true;
4647e1 2020     };
41fa0b 2021
T 2022
2023   this.sent_successfully = function(msg)
2024     {
2025     this.list_mailbox();
2026     this.display_message(msg, 'confirmation', true);
2027     }
2028
4e17e6 2029
T 2030   /*********************************************************/
2031   /*********     keyboard live-search methods      *********/
2032   /*********************************************************/
2033
2034
2035   // handler for keyboard events on address-fields
2036   this.ksearch_keypress = function(e, obj)
2037     {
2038     if (typeof(this.env.contacts)!='object' || !this.env.contacts.length)
2039       return true;
2040
2041     if (this.ksearch_timer)
2042       clearTimeout(this.ksearch_timer);
2043
2044     var highlight;
9bbb17 2045     var key = rcube_event.get_keycode(e);
T 2046     var mod = rcube_event.get_modifier(e);
4e17e6 2047
T 2048     switch (key)
2049       {
2050       case 38:  // key up
2051       case 40:  // key down
2052         if (!this.ksearch_pane)
2053           break;
2054           
2055         var dir = key==38 ? 1 : 0;
2056         var next;
2057         
2058         highlight = document.getElementById('rcmksearchSelected');
2059         if (!highlight)
2060           highlight = this.ksearch_pane.ul.firstChild;
2061         
2062         if (highlight && (next = dir ? highlight.previousSibling : highlight.nextSibling))
2063           {
2064           highlight.removeAttribute('id');
2065           this.set_classname(highlight, 'selected', false);
2066           }
2067
2068         if (next)
2069           {
2070           next.setAttribute('id', 'rcmksearchSelected');
2071           this.set_classname(next, 'selected', true);
2072           this.ksearch_selected = next._rcm_id;
2073           }
2074
86958f 2075         return rcube_event.cancel(e);
4e17e6 2076
T 2077       case 9:  // tab
9bbb17 2078         if(mod == SHIFT_KEY)
4e17e6 2079           break;
T 2080
2081       case 13:  // enter     
2082         if (this.ksearch_selected===null || !this.ksearch_input || !this.ksearch_value)
2083           break;
2084
86958f 2085         // insert selected address and hide ksearch pane
T 2086         this.insert_recipient(this.ksearch_selected);
4e17e6 2087         this.ksearch_hide();
86958f 2088
T 2089         return rcube_event.cancel(e);
4e17e6 2090
T 2091       case 27:  // escape
2092         this.ksearch_hide();
2093         break;
2094
2095       }
2096
2097     // start timer
cfdf04 2098     this.ksearch_timer = setTimeout(function(){ ref.ksearch_get_results(); }, 200);
4e17e6 2099     this.ksearch_input = obj;
T 2100     
2101     return true;
2102     };
86958f 2103
T 2104
2105   this.insert_recipient = function(id)
2106   {
2107     if (!this.env.contacts[id] || !this.ksearch_input)
2108       return;
2109     
2110     // get cursor pos
2111     var inp_value = this.ksearch_input.value.toLowerCase();
2112     var cpos = this.get_caret_pos(this.ksearch_input);
2113     var p = inp_value.lastIndexOf(this.ksearch_value, cpos);
2114     
2115     // replace search string with full address
2116     var pre = this.ksearch_input.value.substring(0, p);
2117     var end = this.ksearch_input.value.substring(p+this.ksearch_value.length, this.ksearch_input.value.length);
2118     var insert  = this.env.contacts[id]+', ';
2119     this.ksearch_input.value = pre + insert + end;
2120     
2121     // set caret to insert pos
2122     cpos = p+insert.length;
2123     if (this.ksearch_input.setSelectionRange)
2124       this.ksearch_input.setSelectionRange(cpos, cpos);
2125     
2126   };
4e17e6 2127
T 2128
2129   // address search processor
2130   this.ksearch_get_results = function()
2131     {
2132     var inp_value = this.ksearch_input ? this.ksearch_input.value : null;
2133     if (inp_value===null)
2134       return;
2135
2136     // get string from current cursor pos to last comma
2137     var cpos = this.get_caret_pos(this.ksearch_input);
2138     var p = inp_value.lastIndexOf(',', cpos-1);
2139     var q = inp_value.substring(p+1, cpos);
2140
2141     // trim query string
2142     q = q.replace(/(^\s+|\s+$)/g, '').toLowerCase();
2143
2144     if (!q.length || q==this.ksearch_value)
2145       {
2146       if (!q.length && this.ksearch_pane && this.ksearch_pane.visible)
2147         this.ksearch_pane.show(0);
2148
2149       return;
2150       }
2151
2152     this.ksearch_value = q;
2153     
2154     // start searching the contact list
2155     var a_results = new Array();
2156     var a_result_ids = new Array();
2157     var c=0;
2158     for (var i=0; i<this.env.contacts.length; i++)
2159       {
2160       if (this.env.contacts[i].toLowerCase().indexOf(q)>=0)
2161         {
2162         a_results[c] = this.env.contacts[i];
2163         a_result_ids[c++] = i;
2164         
2165         if (c==15)  // limit search results
2166           break;
2167         }
2168       }
2169
2170     // display search results
2171     if (c && a_results.length)
2172       {
2173       var p, ul, li;
2174       
2175       // create results pane if not present
2176       if (!this.ksearch_pane)
2177         {
2178         ul = document.createElement('UL');
2179         this.ksearch_pane = new rcube_layer('rcmKSearchpane', {vis:0, zindex:30000});
2180         this.ksearch_pane.elm.appendChild(ul);
2181         this.ksearch_pane.ul = ul;
2182         }
2183       else
2184         ul = this.ksearch_pane.ul;
2185
2186       // remove all search results
2187       ul.innerHTML = '';
2188             
2189       // add each result line to list
2190       for (i=0; i<a_results.length; i++)
2191         {
2192         li = document.createElement('LI');
2193         li.innerHTML = a_results[i].replace(/</, '&lt;').replace(/>/, '&gt;');
86958f 2194         li.onmousedown = function(e){ ref.insert_recipient(this._rcm_id); ref.ksearch_pane.show(0); return rcube_event.cancel(e); };
T 2195         li.style.cursor = 'pointer';
4e17e6 2196         li._rcm_id = a_result_ids[i];
T 2197         ul.appendChild(li);
2198         }
2199
2200       // check if last selected item is still in result list
2201       if (this.ksearch_selected!==null)
2202         {
2203         p = find_in_array(this.ksearch_selected, a_result_ids);
2204         if (p>=0 && ul.childNodes)
2205           {
2206           ul.childNodes[p].setAttribute('id', 'rcmksearchSelected');
2207           this.set_classname(ul.childNodes[p], 'selected', true);
2208           }
2209         else
2210           this.ksearch_selected = null;
2211         }
2212       
2213       // if no item selected, select the first one
2214       if (this.ksearch_selected===null)
2215         {
2216         ul.firstChild.setAttribute('id', 'rcmksearchSelected');
2217         this.set_classname(ul.firstChild, 'selected', true);
2218         this.ksearch_selected = a_result_ids[0];
2219         }
2220
2221       // move the results pane right under the input box and make it visible
2222       var pos = rcube_get_object_pos(this.ksearch_input);
2223       this.ksearch_pane.move(pos.x, pos.y+this.ksearch_input.offsetHeight);
2224       this.ksearch_pane.show(1); 
2225       }
2226     // hide results pane
2227     else
2228       this.ksearch_hide();
2229     };
2230
2231
2232   this.ksearch_blur = function(e, obj)
2233     {
2234     if (this.ksearch_timer)
2235       clearTimeout(this.ksearch_timer);
2236
2237     this.ksearch_value = '';      
2238     this.ksearch_input = null;
2239     
2240     this.ksearch_hide();
2241     };
2242
2243
2244   this.ksearch_hide = function()
2245     {
2246     this.ksearch_selected = null;
2247     
2248     if (this.ksearch_pane)
2249       this.ksearch_pane.show(0);    
2250     };
2251
2252
2253
2254   /*********************************************************/
2255   /*********         address book methods          *********/
2256   /*********************************************************/
2257
2258
6b47de 2259   this.contactlist_keypress = function(list)
T 2260     {
2261       if (list.key_pressed == list.DELETE_KEY)
2262         this.command('delete');
2263     };
2264
2265
2266   this.contactlist_select = function(list)
2267     {
f11541 2268       if (this.preview_timer)
T 2269         clearTimeout(this.preview_timer);
2270
2271       var id, frame, ref = this;
6b47de 2272       if (id = list.get_single_selection())
f11541 2273         this.preview_timer = setTimeout(function(){ ref.load_contact(id, 'show'); }, this.dblclick_time + 10);
T 2274       else if (this.env.contentframe)
2275         this.show_contentframe(false);
6b47de 2276
T 2277       this.enable_command('edit', id?true:false);
f11541 2278       this.enable_command('compose', list.selection.length > 0);
T 2279       this.enable_command('delete', list.selection.length && this.env.address_sources && !this.env.address_sources[this.env.source].readonly);
6b47de 2280
f11541 2281       return false;
6b47de 2282     };
T 2283
2284
f11541 2285   this.list_contacts = function(src, page)
4e17e6 2286     {
T 2287     var add_url = '';
2288     var target = window;
2289     
f11541 2290     if (!src)
T 2291       src = this.env.source;
2292     
2293     if (page && this.current_page==page && src == this.env.source)
4e17e6 2294       return false;
f11541 2295       
T 2296     if (src != this.env.source)
2297       {
2298       page = 1;
2299       this.env.current_page = page;
6b603d 2300       this.reset_qsearch();
f11541 2301       }
T 2302
2303     this.select_folder(src, this.env.source);
2304     this.env.source = src;
4e17e6 2305
T 2306     // load contacts remotely
2307     if (this.gui_objects.contactslist)
2308       {
f11541 2309       this.list_contacts_remote(src, page);
4e17e6 2310       return;
T 2311       }
2312
2313     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
2314       {
2315       target = window.frames[this.env.contentframe];
2316       add_url = '&_framed=1';
2317       }
2318
f11541 2319     // also send search request to get the correct listing
T 2320     if (this.env.search_request)
2321       add_url += '&_search='+this.env.search_request;
2322
4e17e6 2323     this.set_busy(true, 'loading');
f11541 2324     target.location.href = this.env.comm_path+(src ? '&_source='+urlencode(src) : '')+(page ? '&_page='+page : '')+add_url;
4e17e6 2325     };
T 2326
2327
2328   // send remote request to load contacts list
f11541 2329   this.list_contacts_remote = function(src, page)
4e17e6 2330     {
6b47de 2331     // clear message list first
f11541 2332     this.contact_list.clear(true);
T 2333     this.show_contentframe(false);
2334     this.enable_command('delete', 'compose', false);
4e17e6 2335
T 2336     // send request to server
fc7374 2337     var url = (src ? '_source='+urlencode(src) : '') + (page ? (src?'&':'') + '_page='+page : '');
f11541 2338     this.env.source = src;
T 2339     
2340     // also send search request to get the right messages 
2341     if (this.env.search_request) 
2342       url += '&_search='+this.env.search_request;
2343
4e17e6 2344     this.set_busy(true, 'loading');
ecf759 2345     this.http_request('list', url, true);
4e17e6 2346     };
T 2347
2348
2349   // load contact record
2350   this.load_contact = function(cid, action, framed)
2351     {
2352     var add_url = '';
2353     var target = window;
2354     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
2355       {
2356       add_url = '&_framed=1';
2357       target = window.frames[this.env.contentframe];
f11541 2358       this.show_contentframe(true);
4e17e6 2359       }
T 2360     else if (framed)
2361       return false;
f11541 2362       
T 2363     if (action && (cid || action=='add') && !this.drag_active)
4e17e6 2364       {
T 2365       this.set_busy(true);
f11541 2366       target.location.href = this.env.comm_path+'&_action='+action+'&_source='+urlencode(this.env.source)+'&_cid='+urlencode(cid) + add_url;
4e17e6 2367       }
1c5853 2368     return true;
4e17e6 2369     };
f11541 2370
T 2371   // copy a contact to the specified target (group or directory)
2372   this.copy_contact = function(cid, to)
2373   {
2374     if (!cid)
2375       cid = this.contact_list.get_selection().join(',');
2376
2377     if (to != this.env.source && cid && this.env.address_sources[to] && !this.env.address_sources[to].readonly)
2378       this.http_post('copy', '_cid='+urlencode(cid)+'&_source='+urlencode(this.env.source)+'&_to='+urlencode(to));
2379   };
4e17e6 2380
T 2381
2382   this.delete_contacts = function()
2383     {
2384     // exit if no mailbox specified or if selection is empty
6b47de 2385     var selection = this.contact_list.get_selection();
T 2386     if (!(selection.length || this.env.cid) || !confirm(this.get_label('deletecontactconfirm')))
4e17e6 2387       return;
5e3512 2388       
4e17e6 2389     var a_cids = new Array();
b15568 2390     var qs = '';
4e17e6 2391
T 2392     if (this.env.cid)
2393       a_cids[a_cids.length] = this.env.cid;
2394     else
2395       {
2396       var id;
6b47de 2397       for (var n=0; n<selection.length; n++)
4e17e6 2398         {
6b47de 2399         id = selection[n];
4e17e6 2400         a_cids[a_cids.length] = id;
f4f8c6 2401         this.contact_list.remove_row(id, (n == selection.length-1));
4e17e6 2402         }
T 2403
2404       // hide content frame if we delete the currently displayed contact
f11541 2405       if (selection.length == 1)
T 2406         this.show_contentframe(false);
4e17e6 2407       }
T 2408
b15568 2409     // also send search request to get the right records from the next page
T 2410     if (this.env.search_request) 
2411       qs += '&_search='+this.env.search_request;
2412
4e17e6 2413     // send request to server
b15568 2414     this.http_post('delete', '_cid='+urlencode(a_cids.join(','))+'&_from='+(this.env.action ? this.env.action : '')+qs);
1c5853 2415     return true;
4e17e6 2416     };
T 2417
2418
2419   // update a contact record in the list
2420   this.update_contact_row = function(cid, cols_arr)
2421     {
6b47de 2422     var row;
T 2423     if (this.contact_list.rows[cid] && (row = this.contact_list.rows[cid].obj))
2424       {
2425       for (var c=0; c<cols_arr.length; c++)
2426         if (row.cells[c])
2427           row.cells[c].innerHTML = cols_arr[c];
2428
2429       return true;
2430       }
2431
2432     return false;
4e17e6 2433     };
T 2434
2435
2436   /*********************************************************/
2437   /*********        user settings methods          *********/
2438   /*********************************************************/
2439
b0dbf3 2440   this.init_subscription_list = function()
S 2441     {
2442     var p = this;
68b6a9 2443     this.subscription_list = new rcube_list_widget(this.gui_objects.subscriptionlist, {multiselect:false, draggable:true, keyboard:false, toggleselect:true});
b0dbf3 2444     this.subscription_list.addEventListener('select', function(o){ p.subscription_select(o); });
S 2445     this.subscription_list.addEventListener('dragstart', function(o){ p.drag_active = true; });
2446     this.subscription_list.addEventListener('dragend', function(o){ p.subscription_move_folder(o); });
68b6a9 2447     this.subscription_list.row_init = function (row)
S 2448       {
2449       var anchors = row.obj.getElementsByTagName('A');
2450       if (anchors[0])
0aa430 2451         anchors[0].onclick = function() { p.rename_folder(row.id); return false; };
68b6a9 2452       if (anchors[1])
0aa430 2453         anchors[1].onclick = function() { p.delete_folder(row.id); return false; };
68b6a9 2454       row.obj.onmouseover = function() { p.focus_subscription(row.id); };
S 2455       row.obj.onmouseout = function() { p.unfocus_subscription(row.id); };
2456       }
b0dbf3 2457     this.subscription_list.init();
S 2458     }
2459
6b47de 2460   this.identity_select = function(list)
T 2461     {
2462     var id;
2463     if (id = list.get_single_selection())
2464       this.load_identity(id, 'edit-identity');
2465     };
4e17e6 2466
T 2467   // load contact record
2468   this.load_identity = function(id, action)
2469     {
2470     if (action=='edit-identity' && (!id || id==this.env.iid))
1c5853 2471       return false;
4e17e6 2472
T 2473     var add_url = '';
2474     var target = window;
2475     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
2476       {
2477       add_url = '&_framed=1';
2478       target = window.frames[this.env.contentframe];
2479       document.getElementById(this.env.contentframe).style.visibility = 'inherit';
2480       }
2481
2482     if (action && (id || action=='add-identity'))
2483       {
2484       this.set_busy(true);
2485       target.location.href = this.env.comm_path+'&_action='+action+'&_iid='+id+add_url;
2486       }
1c5853 2487     return true;
4e17e6 2488     };
T 2489
2490
2491   this.delete_identity = function(id)
2492     {
2493     // exit if no mailbox specified or if selection is empty
6b47de 2494     var selection = this.identity_list.get_selection();
T 2495     if (!(selection.length || this.env.iid))
4e17e6 2496       return;
T 2497     
2498     if (!id)
6b47de 2499       id = this.env.iid ? this.env.iid : selection[0];
4e17e6 2500
T 2501     // if (this.env.framed && id)
6b47de 2502     this.goto_url('delete-identity', '_iid='+id, true);
1c5853 2503     return true;
b0dbf3 2504     };
S 2505
2506
2507   this.focus_subscription = function(id)
2508     {
68b6a9 2509     var row, folder;
b0dbf3 2510     var reg = RegExp('['+RegExp.escape(this.env.delimiter)+']?[^'+RegExp.escape(this.env.delimiter)+']+$');
3e71ab 2511
S 2512     if (this.drag_active && (row = document.getElementById(id)))
2513       if (this.env.subscriptionrows[id] &&
2514           (folder = this.env.subscriptionrows[id][0]))
b0dbf3 2515         {
3e71ab 2516         if (this.check_droptarget(folder) &&
S 2517             (folder != this.env.folder.replace(reg, '')) &&
2518             (!folder.match(new RegExp('^'+RegExp.escape(this.env.folder+this.env.delimiter)))))
b0dbf3 2519           {
3e71ab 2520           this.set_env('dstfolder', folder);
S 2521           this.set_classname(row, 'droptarget', true);
b0dbf3 2522           }
S 2523         }
3e71ab 2524       else if (this.env.folder.match(new RegExp(RegExp.escape(this.env.delimiter))))
b0dbf3 2525         {
3e71ab 2526         this.set_env('dstfolder', this.env.delimiter);
S 2527         this.set_classname(this.subscription_list.frame, 'droptarget', true);
b0dbf3 2528         }
S 2529     }
2530
2531
2532   this.unfocus_subscription = function(id)
2533     {
3e71ab 2534       var row;
b0dbf3 2535       this.set_env('dstfolder', null);
3e71ab 2536       if (this.env.subscriptionrows[id] &&
S 2537           (row = document.getElementById(id)))
b0dbf3 2538         this.set_classname(row, 'droptarget', false);
3e71ab 2539       else
S 2540         this.set_classname(this.subscription_list.frame, 'droptarget', false);
b0dbf3 2541     }
S 2542
2543
2544   this.subscription_select = function(list)
2545     {
68b6a9 2546     var id, folder;
S 2547     if ((id = list.get_single_selection()) &&
2548         this.env.subscriptionrows['rcmrow'+id] &&
2549         (folder = this.env.subscriptionrows['rcmrow'+id][0]) &&
2550         (find_in_array(this.env.defaultfolders, folder)!=0))
2551       this.set_env('folder', folder);
2552     else
2553       this.set_env('folder', null);
b0dbf3 2554     };
S 2555
2556
2557   this.subscription_move_folder = function(list)
2558     {
2559     var reg = RegExp('['+RegExp.escape(this.env.delimiter)+']?[^'+RegExp.escape(this.env.delimiter)+']+$');
2560     if (this.env.folder && this.env.dstfolder && (this.env.dstfolder != this.env.folder) &&
2561         (this.env.dstfolder != this.env.folder.replace(reg, '')))
2562       {
2563       var reg = new RegExp('[^'+RegExp.escape(this.env.delimiter)+']*['+RegExp.escape(this.env.delimiter)+']', 'g');
2564       var basename = this.env.folder.replace(reg, '');
2565       var newname = this.env.dstfolder==this.env.delimiter ? basename : this.env.dstfolder+this.env.delimiter+basename;
2566       this.http_post('rename-folder', '_folder_oldname='+urlencode(this.env.folder)+'&_folder_newname='+urlencode(newname));
2567       }
2568     this.drag_active = false;
68b6a9 2569     this.unfocus_subscription(this.get_folder_row_id(this.env.dstfolder));
4e17e6 2570     };
T 2571
2572
24053e 2573   // tell server to create and subscribe a new mailbox
4e17e6 2574   this.create_folder = function(name)
T 2575     {
6b47de 2576   if (this.edit_folder)
T 2577     this.reset_folder_rename();
24053e 2578
4e17e6 2579     var form;
T 2580     if ((form = this.gui_objects.editform) && form.elements['_folder_name'])
2581       name = form.elements['_folder_name'].value;
f0db3d 2582     if (this.env.folder)
S 2583       name = this.env.folder+this.env.delimiter+name;
4e17e6 2584
T 2585     if (name)
8d0758 2586       this.http_post('create-folder', '_name='+urlencode(name), true);
4e17e6 2587     else if (form.elements['_folder_name'])
T 2588       form.elements['_folder_name'].focus();
2589     };
2590
24053e 2591
d19426 2592   // start renaming the mailbox name.
24053e 2593   // this will replace the name string with an input field
68b6a9 2594   this.rename_folder = function(id)
4e17e6 2595     {
24053e 2596     var temp, row, form;
T 2597
2598     // reset current renaming
6b47de 2599   if (temp = this.edit_folder)
T 2600     {
2601     this.reset_folder_rename();
2602     if (temp == id)
2603       return;
2604     }
24053e 2605
0aa430 2606     if (id && this.env.subscriptionrows[id] && (row = document.getElementById(id)))
4e17e6 2607       {
b0dbf3 2608       var reg = new RegExp('.*['+RegExp.escape(this.env.delimiter)+']');
24053e 2609       this.name_input = document.createElement('INPUT');
0aa430 2610       this.name_input.value = this.env.subscriptionrows[id][1].replace(reg, '');
24053e 2611       this.name_input.style.width = '100%';
0aa430 2612       
b0dbf3 2613       reg = new RegExp('['+RegExp.escape(this.env.delimiter)+']?[^'+RegExp.escape(this.env.delimiter)+']+$');
0aa430 2614       this.name_input.__parent = this.env.subscriptionrows[id][0].replace(reg, '');
24053e 2615       this.name_input.onkeypress = function(e){ rcmail.name_input_keypress(e); };
T 2616       
2617       row.cells[0].replaceChild(this.name_input, row.cells[0].firstChild);
2618       this.edit_folder = id;
2619       this.name_input.select();
2620       
2621       if (form = this.gui_objects.editform)
2622         form.onsubmit = function(){ return false; };
4e17e6 2623       }
1cded8 2624     };
T 2625
2626
24053e 2627   // remove the input field and write the current mailbox name to the table cell
T 2628   this.reset_folder_rename = function()
1cded8 2629     {
24053e 2630     var cell = this.name_input ? this.name_input.parentNode : null;
e170b4 2631     if (cell && this.edit_folder && this.env.subscriptionrows[this.edit_folder])
b0dbf3 2632       {
S 2633       var reg = new RegExp('[^'+RegExp.escape(this.env.delimiter)+']*['+RegExp.escape(this.env.delimiter)+']', 'g');
2634       cell.innerHTML = this.env.subscriptionrows[this.edit_folder][1].replace(reg, '&nbsp;&nbsp;&nbsp;&nbsp;');
2635       }
24053e 2636       
T 2637     this.edit_folder = null;
2638     };
2639
2640
2641   // handler for keyboard events on the input field
2642   this.name_input_keypress = function(e)
2643     {
9bbb17 2644     var key = rcube_event.get_keycode(e);
24053e 2645
T 2646     // enter
2647     if (key==13)
2648       {
2649       var newname = this.name_input ? this.name_input.value : null;
2650       if (this.edit_folder && newname)
b0dbf3 2651         {
0aa430 2652         if (this.name_input.__parent)
T 2653           newname = this.name_input.__parent + this.env.delimiter + newname;
2654         this.http_post('rename-folder', '_folder_oldname='+urlencode(this.env.subscriptionrows[this.edit_folder][0])+'&_folder_newname='+urlencode(newname));
b0dbf3 2655         }
24053e 2656       }
T 2657     // escape
2658     else if (key==27)
2659       this.reset_folder_rename();
2660     };
2661
2662
2663   // delete a specific mailbox with all its messages
68b6a9 2664   this.delete_folder = function(id)
24053e 2665     {
68b6a9 2666     var folder = this.env.subscriptionrows[id][0];
S 2667
41841b 2668     if (this.edit_folder)
S 2669       this.reset_folder_rename();
fdbb19 2670
68b6a9 2671     if (folder && confirm(this.get_label('deletefolderconfirm')))
S 2672       {
2673       this.http_post('delete-folder', '_mboxes='+urlencode(folder));
2674       this.set_env('folder', null);
2675       }
24053e 2676     };
T 2677
2678
2679   // add a new folder to the subscription list by cloning a folder row
4d4264 2680   this.add_folder_row = function(name, display_name, replace)
24053e 2681     {
T 2682     name = name.replace('\\',"");
2683     if (!this.gui_objects.subscriptionlist)
2684       return false;
2685
2686     for (var refid in this.env.subscriptionrows)
2687       if (this.env.subscriptionrows[refid]!=null)
1cded8 2688         break;
T 2689
24053e 2690     var refrow, form;
T 2691     var tbody = this.gui_objects.subscriptionlist.tBodies[0];
68b6a9 2692     var id = replace && replace.id ? replace.id : 'rcmrow'+(tbody.childNodes.length+1);
1f064b 2693     var selection = this.subscription_list.get_single_selection();
24053e 2694
T 2695     if (!id || !(refrow = document.getElementById(refid)))
2696       {
2697       // Refresh page if we don't have a table row to clone
6b47de 2698       this.goto_url('folders');
24053e 2699       }
T 2700     else
2701       {
2702       // clone a table row if there are existing rows
2703       var row = this.clone_table_row(refrow);
68b6a9 2704       row.id = id;
24053e 2705       if (replace)
T 2706         tbody.replaceChild(row, replace);
2707       else
2708         tbody.appendChild(row);
2709       }
2710
2711     // add to folder/row-ID map
4d4264 2712     this.env.subscriptionrows[row.id] = [name, display_name];
24053e 2713
T 2714     // set folder name
4d4264 2715     row.cells[0].innerHTML = display_name;
24053e 2716     if (row.cells[1] && row.cells[1].firstChild.tagName=='INPUT')
T 2717       {
2718       row.cells[1].firstChild.value = name;
2719       row.cells[1].firstChild.checked = true;
2720       }
2721        
2722     // add new folder to rename-folder list and clear input field
f9c107 2723     if (!replace && (form = this.gui_objects.editform))
24053e 2724       {
f9c107 2725       if (form.elements['_folder_oldname'])
T 2726         form.elements['_folder_oldname'].options[form.elements['_folder_oldname'].options.length] = new Option(name,name);
2727       if (form.elements['_folder_name'])
2728         form.elements['_folder_name'].value = ''; 
24053e 2729       }
T 2730
9bebdf 2731     this.sort_subscription_list();
b0dbf3 2732     this.init_subscription_list();
b119e2 2733     if (selection && document.getElementById('rcmrow'+selection))
1f064b 2734       this.subscription_list.select_row(selection);
b0dbf3 2735
68b6a9 2736     if (document.getElementById(id).scrollIntoView)
S 2737       document.getElementById(id).scrollIntoView();
24053e 2738     };
T 2739
2740
2741   // replace an existing table row with a new folder line
4d4264 2742   this.replace_folder_row = function(oldfolder, newfolder, display_name)
24053e 2743     {
T 2744     var id = this.get_folder_row_id(oldfolder);
2745     var row = document.getElementById(id);
2746     
2747     // replace an existing table row (if found)
4d4264 2748     this.add_folder_row(newfolder, display_name, row);
24053e 2749     
T 2750     // rename folder in rename-folder dropdown
2751     var form, elm;
2752     if ((form = this.gui_objects.editform) && (elm = form.elements['_folder_oldname']))
2753       {
2754       for (var i=0;i<elm.options.length;i++)
2755         {
2756         if (elm.options[i].value == oldfolder)
2757           {
4d4264 2758           elm.options[i].text = display_name;
24053e 2759           elm.options[i].value = newfolder;
T 2760           break;
2761           }
2762         }
2763
2764       form.elements['_folder_newname'].value = '';
2765       }
2766     };
2767     
2768
2769   // remove the table row of a specific mailbox from the table
2770   // (the row will not be removed, just hidden)
2771   this.remove_folder_row = function(folder)
2772     {
1cded8 2773     var row;
24053e 2774     var id = this.get_folder_row_id(folder);
1cded8 2775     if (id && (row = document.getElementById(id)))
T 2776       row.style.display = 'none';    
c8c1e0 2777
S 2778     // remove folder from rename-folder list
2779     var form;
2780     if ((form = this.gui_objects.editform) && form.elements['_folder_oldname'])
2781       {
2782       for (var i=0;i<form.elements['_folder_oldname'].options.length;i++)
2783         {
2784         if (form.elements['_folder_oldname'].options[i].value == folder) 
24053e 2785           {
c8c1e0 2786           form.elements['_folder_oldname'].options[i] = null;
24053e 2787           break;
c8c1e0 2788           }
S 2789         }
2790       }
24053e 2791     
f9c107 2792     if (form && form.elements['_folder_newname'])
T 2793       form.elements['_folder_newname'].value = '';
4e17e6 2794     };
T 2795
2796
2797   this.subscribe_folder = function(folder)
2798     {
2799     var form;
2800     if ((form = this.gui_objects.editform) && form.elements['_unsubscribed'])
2801       this.change_subscription('_unsubscribed', '_subscribed', 'subscribe');
2802     else if (folder)
8d0758 2803       this.http_post('subscribe', '_mboxes='+urlencode(folder));
4e17e6 2804     };
T 2805
2806
2807   this.unsubscribe_folder = function(folder)
2808     {
2809     var form;
2810     if ((form = this.gui_objects.editform) && form.elements['_subscribed'])
2811       this.change_subscription('_subscribed', '_unsubscribed', 'unsubscribe');
2812     else if (folder)
8d0758 2813       this.http_post('unsubscribe', '_mboxes='+urlencode(folder));
4e17e6 2814     };
T 2815     
2816
2817   this.change_subscription = function(from, to, action)
2818     {
2819     var form;
2820     if (form = this.gui_objects.editform)
2821       {
2822       var a_folders = new Array();
2823       var list_from = form.elements[from];
2824
2825       for (var i=0; list_from && i<list_from.options.length; i++)
2826         {
2827         if (list_from.options[i] && list_from.options[i].selected)
2828           {
2829           a_folders[a_folders.length] = list_from.options[i].value;
2830           list_from[i] = null;
2831           i--;
2832           }
2833         }
2834
2835       // yes, we have some folders selected
2836       if (a_folders.length)
2837         {
2838         var list_to = form.elements[to];
2839         var index;
2840         
2841         for (var n=0; n<a_folders.length; n++)
2842           {
2843           index = list_to.options.length;
2844           list_to[index] = new Option(a_folders[n]);
2845           }
2846           
8d0758 2847         this.http_post(action, '_mboxes='+urlencode(a_folders.join(',')));
4e17e6 2848         }
T 2849       }
2850       
2851     };
2852
24053e 2853   // helper method to find a specific mailbox row ID
T 2854   this.get_folder_row_id = function(folder)
2855     {
2856     for (var id in this.env.subscriptionrows)
4d4264 2857       if (this.env.subscriptionrows[id] && this.env.subscriptionrows[id][0] == folder)
24053e 2858         break;
T 2859         
2860     return id;
2861     };
4e17e6 2862
T 2863   // duplicate a specific table row
2864   this.clone_table_row = function(row)
2865     {
2866     var cell, td;
2867     var new_row = document.createElement('TR');
2868     for(var n=0; n<row.childNodes.length; n++)
2869       {
2870       cell = row.childNodes[n];
2871       td = document.createElement('TD');
2872
2873       if (cell.className)
2874         td.className = cell.className;
2875       if (cell.align)
2876         td.setAttribute('align', cell.align);
2877         
2878       td.innerHTML = cell.innerHTML;
2879       new_row.appendChild(td);
2880       }
2881     
2882     return new_row;
2883     };
2884
9bebdf 2885   // sort subscription folder list
S 2886   this.sort_subscription_list = function()
2887     {
6ea80c 2888     var index = new Array();
9bebdf 2889     var tbody = this.gui_objects.subscriptionlist.tBodies[0];
6ea80c 2890     var swapped = false;
b0dbf3 2891     for (var i = 0; i<tbody.childNodes.length; i++)
9bebdf 2892       if (this.env.subscriptionrows[tbody.childNodes[i].id]!=null)
6ea80c 2893         index.push(i);
S 2894     for (i = 0; i<(index.length-1); i++)
2895       {
b0dbf3 2896       var one = tbody.childNodes[index[i]];
S 2897       var two = tbody.childNodes[index[i+1]];
2898       if (this.env.subscriptionrows[one.id][0].toLowerCase()>
2899           this.env.subscriptionrows[two.id][0].toLowerCase())
9bebdf 2900         {
b0dbf3 2901         var swap = one.cloneNode(true);
S 2902         tbody.replaceChild(swap, two);
2903         tbody.replaceChild(two, one);
6ea80c 2904         swapped = true;
9bebdf 2905         }
S 2906       }
6ea80c 2907     if (swapped)
S 2908       this.sort_subscription_list();
9bebdf 2909     };
S 2910
4e17e6 2911
T 2912   /*********************************************************/
2913   /*********           GUI functionality           *********/
2914   /*********************************************************/
2915
2916
2917   // eable/disable buttons for page shifting
2918   this.set_page_buttons = function()
2919     {
2920     this.enable_command('nextpage', (this.env.pagecount > this.env.current_page));
d17008 2921     this.enable_command('lastpage', (this.env.pagecount > this.env.current_page));
4e17e6 2922     this.enable_command('previouspage', (this.env.current_page > 1));
d17008 2923     this.enable_command('firstpage', (this.env.current_page > 1));
4e17e6 2924     }
T 2925
2926
2927   // set button to a specific state
2928   this.set_button = function(command, state)
2929     {
2930     var a_buttons = this.buttons[command];
2931     var button, obj;
2932
2933     if(!a_buttons || !a_buttons.length)
2934       return;
2935
2936     for(var n=0; n<a_buttons.length; n++)
2937       {
2938       button = a_buttons[n];
2939       obj = document.getElementById(button.id);
2940
2941       // get default/passive setting of the button
104ee3 2942       if (obj && button.type=='image' && !button.status) {
4e17e6 2943         button.pas = obj._original_src ? obj._original_src : obj.src;
104ee3 2944         // respect PNG fix on IE browsers
T 2945         if (obj.runtimeStyle && obj.runtimeStyle.filter && obj.runtimeStyle.filter.match(/src=['"]([^'"]+)['"]/))
2946           button.pas = RegExp.$1;
2947       }
4e17e6 2948       else if (obj && !button.status)
T 2949         button.pas = String(obj.className);
2950
2951       // set image according to button state
2952       if (obj && button.type=='image' && button[state])
2953         {
2954         button.status = state;        
2955         obj.src = button[state];
2956         }
2957       // set class name according to button state
2958       else if (obj && typeof(button[state])!='undefined')
2959         {
2960         button.status = state;        
2961         obj.className = button[state];        
2962         }
2963       // disable/enable input buttons
2964       if (obj && button.type=='input')
2965         {
2966         button.status = state;
2967         obj.disabled = !state;
2968         }
2969       }
2970     };
2971
eb6842 2972   // display a specific alttext
T 2973   this.set_alttext = function(command, label)
2974     {
2975       if (!this.buttons[command] || !this.buttons[command].length)
2976         return;
2977       
2978       var button, obj, link;
2979       for (var n=0; n<this.buttons[command].length; n++)
2980       {
2981         button = this.buttons[command][n];
2982         obj = document.getElementById(button.id);
2983         
2984         if (button.type=='image' && obj)
2985         {
2986           obj.setAttribute('alt', this.get_label(label));
2987           if ((link = obj.parentNode) && link.tagName == 'A')
2988             link.setAttribute('title', this.get_label(label));
2989         }
2990         else if (obj)
2991           obj.setAttribute('title', this.get_label(label));
2992       }
2993     };
4e17e6 2994
T 2995   // mouse over button
2996   this.button_over = function(command, id)
2997     {
2998     var a_buttons = this.buttons[command];
2999     var button, img;
3000
3001     if(!a_buttons || !a_buttons.length)
3002       return;
3003
3004     for(var n=0; n<a_buttons.length; n++)
3005       {
3006       button = a_buttons[n];
3007       if(button.id==id && button.status=='act')
3008         {
3009         img = document.getElementById(button.id);
3010         if (img && button.over)
3011           img.src = button.over;
3012         }
3013       }
3014     };
3015
c8c1e0 3016   // mouse down on button
S 3017   this.button_sel = function(command, id)
3018     {
3019     var a_buttons = this.buttons[command];
3020     var button, img;
3021
3022     if(!a_buttons || !a_buttons.length)
3023       return;
3024
3025     for(var n=0; n<a_buttons.length; n++)
3026       {
3027       button = a_buttons[n];
3028       if(button.id==id && button.status=='act')
3029         {
3030         img = document.getElementById(button.id);
3031         if (img && button.sel)
3032           img.src = button.sel;
3033         }
3034       }
3035     };
4e17e6 3036
T 3037   // mouse out of button
3038   this.button_out = function(command, id)
3039     {
3040     var a_buttons = this.buttons[command];
3041     var button, img;
3042
3043     if(!a_buttons || !a_buttons.length)
3044       return;
3045
3046     for(var n=0; n<a_buttons.length; n++)
3047       {
3048       button = a_buttons[n];
3049       if(button.id==id && button.status=='act')
3050         {
3051         img = document.getElementById(button.id);
3052         if (img && button.act)
3053           img.src = button.act;
3054         }
3055       }
3056     };
3057
3058
3059   // set/unset a specific class name
3060   this.set_classname = function(obj, classname, set)
3061     {
3062     var reg = new RegExp('\s*'+classname, 'i');
3063     if (!set && obj.className.match(reg))
3064       obj.className = obj.className.replace(reg, '');
3065     else if (set && !obj.className.match(reg))
3066       obj.className += ' '+classname;
3067     };
3068
3069
5eee00 3070   // write to the document/window title
T 3071   this.set_pagetitle = function(title)
3072   {
3073     if (title && document.title)
3074       document.title = title;
3075   }
3076
3077
4e17e6 3078   // display a system message
T 3079   this.display_message = function(msg, type, hold)
3080     {
3081     if (!this.loaded)  // save message in order to display after page loaded
3082       {
3083       this.pending_message = new Array(msg, type);
3084       return true;
3085       }
b716bd 3086
T 3087     // pass command to parent window
3088     if (this.env.framed && parent.rcmail)
3089       return parent.rcmail.display_message(msg, type, hold);
3090
4e17e6 3091     if (!this.gui_objects.message)
T 3092       return false;
f9c107 3093
4e17e6 3094     if (this.message_timer)
T 3095       clearTimeout(this.message_timer);
3096     
3097     var cont = msg;
3098     if (type)
3099       cont = '<div class="'+type+'">'+cont+'</div>';
a95e0e 3100
b716bd 3101     var _rcube = this;
4e17e6 3102     this.gui_objects.message.innerHTML = cont;
T 3103     this.gui_objects.message.style.display = 'block';
b716bd 3104     
a95e0e 3105     if (type!='loading')
b716bd 3106       this.gui_objects.message.onmousedown = function(){ _rcube.hide_message(); return true; };
4e17e6 3107     
T 3108     if (!hold)
cfdf04 3109       this.message_timer = setTimeout(function(){ ref.hide_message(); }, this.message_time);
4e17e6 3110     };
T 3111
3112
3113   // make a message row disapear
3114   this.hide_message = function()
3115     {
3116     if (this.gui_objects.message)
a95e0e 3117       {
4e17e6 3118       this.gui_objects.message.style.display = 'none';
a95e0e 3119       this.gui_objects.message.onmousedown = null;
T 3120       }
4e17e6 3121     };
T 3122
3123
3124   // mark a mailbox as selected and set environment variable
f11541 3125   this.select_folder = function(name, old)
T 3126   {
3127     if (this.gui_objects.folderlist)
4e17e6 3128     {
f11541 3129       var current_li, target_li;
597170 3130       
f11541 3131       if ((current_li = this.get_folder_li(old)))
T 3132       {
6a35c8 3133         this.set_classname(current_li, 'selected', false);
952860 3134         this.set_classname(current_li, 'unfocused', false);
4e17e6 3135       }
6b47de 3136
f11541 3137       if ((target_li = this.get_folder_li(name)))
fa4cd2 3138       {
f11541 3139         this.set_classname(target_li, 'unfocused', false);
T 3140         this.set_classname(target_li, 'selected', true);
fa4cd2 3141       }
f11541 3142     }
T 3143   };
3144
3145   // helper method to find a folder list item
3146   this.get_folder_li = function(name)
3147   {
3148     if (this.gui_objects.folderlist)
3149     {
3150       name = String(name).replace(this.identifier_expr, '');
3151       return document.getElementById('rcmli'+name);
3152     }
3153
3154     return null;
3155   };
4e17e6 3156
24053e 3157
25d8ba 3158   // for reordering column array, Konqueror workaround
S 3159   this.set_message_coltypes = function(coltypes) 
3160   { 
24053e 3161     this.coltypes = coltypes;
T 3162     
3163     // set correct list titles
3164     var cell, col;
3165     var thead = this.gui_objects.messagelist ? this.gui_objects.messagelist.tHead : null;
3166     for (var n=0; thead && n<this.coltypes.length; n++) 
3167       {
3168       col = this.coltypes[n];
3169       if ((cell = thead.rows[0].cells[n+1]) && (col=='from' || col=='to'))
3170         {
3171         // if we have links for sorting, it's a bit more complicated...
3172         if (cell.firstChild && cell.firstChild.tagName=='A')
3173           {
3174           cell.firstChild.innerHTML = this.get_label(this.coltypes[n]);
3175           cell.firstChild.onclick = function(){ return rcmail.command('sort', this.__col, this); };
3176           cell.firstChild.__col = col;
3177           }
3178         else
3179           cell.innerHTML = this.get_label(this.coltypes[n]);
3180
3181         cell.id = 'rcmHead'+col;
3182         }
3183       }
3184
3185   };
4e17e6 3186
T 3187   // create a table row in the message list
15a9d1 3188   this.add_message_row = function(uid, cols, flags, attachment, attop)
4e17e6 3189     {
6b47de 3190     if (!this.gui_objects.messagelist || !this.message_list)
4e17e6 3191       return false;
6b47de 3192
4e17e6 3193     var tbody = this.gui_objects.messagelist.tBodies[0];
T 3194     var rowcount = tbody.rows.length;
3195     var even = rowcount%2;
3196     
857a38 3197     this.env.messages[uid] = {deleted:flags.deleted?1:0,
S 3198                               replied:flags.replied?1:0,
4e17e6 3199                               unread:flags.unread?1:0};
T 3200     
3201     var row = document.createElement('TR');
3202     row.id = 'rcmrow'+uid;
15a9d1 3203     row.className = 'message '+(even ? 'even' : 'odd')+(flags.unread ? ' unread' : '')+(flags.deleted ? ' deleted' : '');
6b47de 3204
T 3205     if (this.message_list.in_selection(uid))
4e17e6 3206       row.className += ' selected';
T 3207
857a38 3208     var icon = flags.deleted && this.env.deletedicon ? this.env.deletedicon:
S 3209                (flags.unread && this.env.unreadicon ? this.env.unreadicon :
3210                (flags.replied && this.env.repliedicon ? this.env.repliedicon : this.env.messageicon));
4e17e6 3211
T 3212     var col = document.createElement('TD');
3213     col.className = 'icon';
3214     col.innerHTML = icon ? '<img src="'+icon+'" alt="" border="0" />' : '';
3215     row.appendChild(col);
3216
3217     // add each submitted col
25d8ba 3218     for (var n = 0; n < this.coltypes.length; n++) 
S 3219       { 
3220       var c = this.coltypes[n];
4e17e6 3221       col = document.createElement('TD');
T 3222       col.className = String(c).toLowerCase();
3223       col.innerHTML = cols[c];
3224       row.appendChild(col);
3225       }
3226
3227     col = document.createElement('TD');
3228     col.className = 'icon';
3229     col.innerHTML = attachment && this.env.attachmenticon ? '<img src="'+this.env.attachmenticon+'" alt="" border="0" />' : '';
3230     row.appendChild(col);
6b47de 3231
T 3232     this.message_list.insert_row(row, attop);
4e17e6 3233     };
T 3234
3235
3236   // replace content of row count display
3237   this.set_rowcount = function(text)
3238     {
3239     if (this.gui_objects.countdisplay)
3240       this.gui_objects.countdisplay.innerHTML = text;
3241
3242     // update page navigation buttons
3243     this.set_page_buttons();
3244     };
3245
58e360 3246   // replace content of quota display
f11541 3247   this.set_quota = function()
T 3248     {
3249     if (this.gui_objects.quotadisplay &&
3250         this.gui_objects.quotadisplay.attributes.getNamedItem('display') &&
3251         this.gui_objects.quotadisplay.attributes.getNamedItem('id'))
3252       this.http_request('quotadisplay', '_display='+
3253       this.gui_objects.quotadisplay.attributes.getNamedItem('display').nodeValue+
3254       '&_id='+this.gui_objects.quotadisplay.attributes.getNamedItem('id').nodeValue, false);
58e360 3255      };
6b47de 3256
4e17e6 3257
T 3258   // update the mailboxlist
15a9d1 3259   this.set_unread_count = function(mbox, count, set_title)
4e17e6 3260     {
T 3261     if (!this.gui_objects.mailboxlist)
3262       return false;
25d8ba 3263
fe79b1 3264     var reg, text_obj;
f11541 3265     var item = this.get_folder_li(mbox);
T 3266     mbox = String(mbox).toLowerCase().replace(this.identifier_expr, '');
15a9d1 3267
T 3268     if (item && item.className && item.className.indexOf('mailbox '+mbox)>=0)
4e17e6 3269       {
15a9d1 3270       // set new text
T 3271       text_obj = item.firstChild;
3272       reg = /\s+\([0-9]+\)$/i;
4e17e6 3273
15a9d1 3274       if (count && text_obj.innerHTML.match(reg))
T 3275         text_obj.innerHTML = text_obj.innerHTML.replace(reg, ' ('+count+')');
3276       else if (count)
3277         text_obj.innerHTML += ' ('+count+')';
3278       else
3279         text_obj.innerHTML = text_obj.innerHTML.replace(reg, '');
25d8ba 3280
15a9d1 3281       // set the right classes
T 3282       this.set_classname(item, 'unread', count>0 ? true : false);
3283       }
3284
3285     // set unread count to window title
01c86f 3286     reg = /^\([0-9]+\)\s+/i;
25d8ba 3287     if (set_title && document.title)
15a9d1 3288       {
T 3289       var doc_title = String(document.title);
5eee00 3290       var new_title = "";
15a9d1 3291
T 3292       if (count && doc_title.match(reg))
5eee00 3293         new_title = doc_title.replace(reg, '('+count+') ');
15a9d1 3294       else if (count)
5eee00 3295         new_title = '('+count+') '+doc_title;
15a9d1 3296       else
5eee00 3297         new_title = doc_title.replace(reg, '');
T 3298         
3299       this.set_pagetitle(new_title);
01c86f 3300       }
4e17e6 3301     };
T 3302
3303
3304   // add row to contacts list
6b47de 3305   this.add_contact_row = function(cid, cols, select)
4e17e6 3306     {
T 3307     if (!this.gui_objects.contactslist || !this.gui_objects.contactslist.tBodies[0])
3308       return false;
3309     
3310     var tbody = this.gui_objects.contactslist.tBodies[0];
3311     var rowcount = tbody.rows.length;
3312     var even = rowcount%2;
3313     
3314     var row = document.createElement('TR');
3315     row.id = 'rcmrow'+cid;
3316     row.className = 'contact '+(even ? 'even' : 'odd');
3317     
6b47de 3318     if (this.contact_list.in_selection(cid))
4e17e6 3319       row.className += ' selected';
T 3320
3321     // add each submitted col
3322     for (var c in cols)
3323       {
3324       col = document.createElement('TD');
3325       col.className = String(c).toLowerCase();
3326       col.innerHTML = cols[c];
3327       row.appendChild(col);
3328       }
3329     
6b47de 3330     this.contact_list.insert_row(row);
4e17e6 3331     };
T 3332
3333
a0109c 3334   this.toggle_editor = function(checkbox, textElementName)
S 3335     {
3336     var ischecked = checkbox.checked;
3337     if (ischecked)
3338       {
3339         tinyMCE.execCommand('mceAddControl', true, textElementName);
3340       }
3341     else
3342       {
3343         tinyMCE.execCommand('mceRemoveControl', true, textElementName);
3344       }
4e17e6 3345     };
T 3346
3347
3348
3349   /********************************************************/
3350   /*********        remote request methods        *********/
3351   /********************************************************/
6b47de 3352
4b9efb 3353   this.redirect = function(url, lock)
f11541 3354     {
719a25 3355     if (lock || lock === null)
4b9efb 3356       this.set_busy(true);
S 3357
f11541 3358     if (this.env.framed && window.parent)
T 3359       parent.location.href = url;
3360     else  
3361       location.href = url;
3362     };
6b47de 3363
T 3364   this.goto_url = function(action, query, lock)
3365     {
3366     var querystring = query ? '&'+query : '';
4b9efb 3367     this.redirect(this.env.comm_path+'&_action='+action+querystring, lock);
6b47de 3368     };
4e17e6 3369
T 3370
ecf759 3371   this.http_sockets = new Array();
T 3372   
3373   // find a non-busy socket or create a new one
3374   this.get_request_obj = function()
4e17e6 3375     {
ecf759 3376     for (var n=0; n<this.http_sockets.length; n++)
4e17e6 3377       {
ecf759 3378       if (!this.http_sockets[n].busy)
T 3379         return this.http_sockets[n];
4e17e6 3380       }
ecf759 3381     
T 3382     // create a new XMLHTTP object
3383     var i = this.http_sockets.length;
3384     this.http_sockets[i] = new rcube_http_request();
4e17e6 3385
ecf759 3386     return this.http_sockets[i];
T 3387     };
3388   
3389
3390   // send a http request to the server
3391   this.http_request = function(action, querystring, lock)
3392     {
3393     var request_obj = this.get_request_obj();
b19097 3394     querystring += (querystring ? '&' : '') + '_remote=1';
4e17e6 3395     
T 3396     // add timestamp to request url to avoid cacheing problems in Safari
3397     if (bw.safari)
3398       querystring += '&_ts='+(new Date().getTime());
3399
3400     // send request
ecf759 3401     if (request_obj)
4e17e6 3402       {
f11541 3403       console.log('HTTP request: '+this.env.comm_path+'&_action='+action+'&'+querystring);
ecf759 3404
T 3405       if (lock)
3406         this.set_busy(true);
3407
f11541 3408       var rcm = this;
ecf759 3409       request_obj.__lock = lock ? true : false;
T 3410       request_obj.__action = action;
86958f 3411       request_obj.onerror = function(o){ ref.http_error(o); };
T 3412       request_obj.oncomplete = function(o){ ref.http_response(o); };
4d4264 3413       request_obj.GET(this.env.comm_path+'&_action='+action+'&'+querystring);
4e17e6 3414       }
T 3415     };
3416
f11541 3417     // send a http POST request to the server
T 3418     this.http_post = function(action, postdata, lock)
3419       {
3420       var request_obj;
3421       if (postdata && typeof(postdata) == 'object')
3422         postdata._remote = 1;
3423       else
3424         postdata += (postdata ? '&' : '') + '_remote=1';
3425
3426       // send request
3427       if (request_obj = this.get_request_obj())
3428         {
3429         console.log('HTTP POST: '+this.env.comm_path+'&_action='+action);
3430
3431         if (lock)
3432           this.set_busy(true);
3433
3434         var rcm = this;
3435         request_obj.__lock = lock ? true : false;
3436         request_obj.__action = action;
3437         request_obj.onerror = function(o){ rcm.http_error(o); };
3438         request_obj.oncomplete = function(o){ rcm.http_response(o); };
3439         request_obj.POST(this.env.comm_path+'&_action='+action, postdata);
3440         }
3441       };
4e17e6 3442
ecf759 3443   // handle HTTP response
T 3444   this.http_response = function(request_obj)
4e17e6 3445     {
ecf759 3446     var ctype = request_obj.get_header('Content-Type');
e1cf7c 3447     if (ctype){
ecf759 3448       ctype = String(ctype).toLowerCase();
e1cf7c 3449       var ctype_array=ctype.split(";");
S 3450       ctype = ctype_array[0];
3451     }
4e17e6 3452
5eee00 3453     if (request_obj.__lock)
T 3454         this.set_busy(false);
4e17e6 3455
f11541 3456     console.log(request_obj.get_text());
4e17e6 3457
ecf759 3458     // if we get javascript code from server -> execute it
c03095 3459     if (request_obj.get_text() && (ctype=='text/javascript' || ctype=='application/x-javascript'))
T 3460       eval(request_obj.get_text());
4e17e6 3461
ecf759 3462     // process the response data according to the sent action
T 3463     switch (request_obj.__action)
3464       {
3465       case 'delete':
3466       case 'moveto':
3467         if (this.env.action=='show')
3468           this.command('list');
b19097 3469         else if (this.message_list)
T 3470           this.message_list.init();
ecf759 3471         break;
15a9d1 3472
ecf759 3473       case 'list':
5e3512 3474         if (this.env.messagecount)
08c007 3475           this.enable_command('purge', (this.env.mailbox==this.env.trash_mailbox || this.env.mailbox==this.env.junk_mailbox));
ee18f5 3476         this.msglist_select(this.message_list);
5e3512 3477
15a9d1 3478       case 'expunge':
T 3479         this.enable_command('select-all', 'select-none', 'expunge', this.env.messagecount ? true : false);
e170b4 3480         break;
4e17e6 3481       }
ecf759 3482
T 3483     request_obj.reset();
4e17e6 3484     };
ecf759 3485
T 3486
3487   // handle HTTP request errors
3488   this.http_error = function(request_obj)
3489     {
9a5261 3490     //alert('Error sending request: '+request_obj.url);
ecf759 3491
T 3492     if (request_obj.__lock)
3493       this.set_busy(false);
3494
3495     request_obj.reset();
3496     request_obj.__lock = false;
3497     };
3498
3499
3500   // use an image to send a keep-alive siganl to the server
3501   this.send_keep_alive = function()
3502     {
3503     var d = new Date();
3504     this.http_request('keep-alive', '_t='+d.getTime());
3505     };
15a9d1 3506
ecf759 3507     
15a9d1 3508   // send periodic request to check for recent messages
T 3509   this.check_for_recent = function()
3510     {
aade7b 3511     if (this.busy)
T 3512       return;
3513
c8c1e0 3514     this.set_busy(true, 'checkingmail');
110748 3515     this.http_request('check-recent', (this.env.search_request ? '_search='+this.env.search_request+'&' : '') + '_t='+(new Date().getTime()), true);
15a9d1 3516     };
4e17e6 3517
T 3518
3519   /********************************************************/
3520   /*********            helper methods            *********/
3521   /********************************************************/
3522   
3523   // check if we're in show mode or if we have a unique selection
3524   // and return the message uid
3525   this.get_single_uid = function()
3526     {
6b47de 3527     return this.env.uid ? this.env.uid : (this.message_list ? this.message_list.get_single_selection() : null);
4e17e6 3528     };
T 3529
3530   // same as above but for contacts
3531   this.get_single_cid = function()
3532     {
6b47de 3533     return this.env.cid ? this.env.cid : (this.contact_list ? this.contact_list.get_single_selection() : null);
4e17e6 3534     };
T 3535
3536
3537   this.get_caret_pos = function(obj)
3538     {
3539     if (typeof(obj.selectionEnd)!='undefined')
3540       return obj.selectionEnd;
3541
3542     else if (document.selection && document.selection.createRange)
3543       {
3544       var range = document.selection.createRange();
3545       if (range.parentElement()!=obj)
3546         return 0;
3547
3548       var gm = range.duplicate();
3549       if (obj.tagName=='TEXTAREA')
3550         gm.moveToElementText(obj);
3551       else
3552         gm.expand('textedit');
3553       
3554       gm.setEndPoint('EndToStart', range);
3555       var p = gm.text.length;
3556
3557       return p<=obj.value.length ? p : -1;
3558       }
3559
3560     else
3561       return obj.value.length;
3562     };
3563
3564
3565   this.set_caret2start = function(obj)
3566     {
3567     if (obj.createTextRange)
3568       {
3569       var range = obj.createTextRange();
3570       range.collapse(true);
3571       range.select();
3572       }
3573     else if (obj.setSelectionRange)
3574       obj.setSelectionRange(0,0);
3575
3576     obj.focus();
3577     };
3578
3579
3580   // set all fields of a form disabled
3581   this.lock_form = function(form, lock)
3582     {
3583     if (!form || !form.elements)
3584       return;
3585     
3586     var type;
3587     for (var n=0; n<form.elements.length; n++)
3588       {
3589       type = form.elements[n];
3590       if (type=='hidden')
3591         continue;
3592         
3593       form.elements[n].disabled = lock;
3594       }
3595     };
3596     
3597   }  // end object rcube_webmail
3598
3599
3600
6b47de 3601 /**
T 3602  * Class for sending HTTP requests
3603  * @constructor
3604  */
ecf759 3605 function rcube_http_request()
T 3606   {
3607   this.url = '';
3608   this.busy = false;
3609   this.xmlhttp = null;
3610
3611
3612   // reset object properties
3613   this.reset = function()
3614     {
3615     // set unassigned event handlers
3616     this.onloading = function(){ };
3617     this.onloaded = function(){ };
3618     this.oninteractive = function(){ };
3619     this.oncomplete = function(){ };
3620     this.onabort = function(){ };
3621     this.onerror = function(){ };
3622     
3623     this.url = '';
3624     this.busy = false;
3625     this.xmlhttp = null;
3626     }
3627
3628
3629   // create HTMLHTTP object
3630   this.build = function()
3631     {
3632     if (window.XMLHttpRequest)
3633       this.xmlhttp = new XMLHttpRequest();
3634     else if (window.ActiveXObject)
f11541 3635       {
T 3636       try { this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
3637       catch(e) { this.xmlhttp = null; }
3638       }
ecf759 3639     else
T 3640       {
3641       
3642       }
3643     }
3644
a0109c 3645   // send GET request
ecf759 3646   this.GET = function(url)
T 3647     {
3648     this.build();
3649
3650     if (!this.xmlhttp)
3651       {
3652       this.onerror(this);
3653       return false;
3654       }
3655
719a25 3656     var _ref = this;
ecf759 3657     this.url = url;
T 3658     this.busy = true;
3659
719a25 3660     this.xmlhttp.onreadystatechange = function(){ _ref.xmlhttp_onreadystatechange(); };
ecf759 3661     this.xmlhttp.open('GET', url);
234c0d 3662     this.xmlhttp.setRequestHeader('X-RoundCube-Referer', bw.get_cookie('roundcube_sessid'));
ecf759 3663     this.xmlhttp.send(null);
T 3664     };
3665
3666
a0109c 3667   this.POST = function(url, body, contentType)
ecf759 3668     {
a0109c 3669     // default value for contentType if not provided
f11541 3670     if (typeof(contentType) == 'undefined')
T 3671       contentType = 'application/x-www-form-urlencoded';
a0109c 3672
S 3673     this.build();
3674     
3675     if (!this.xmlhttp)
3676     {
3677        this.onerror(this);
3678        return false;
3679     }
f11541 3680     
T 3681     var req_body = body;
3682     if (typeof(body) == 'object')
3683     {
3684       req_body = '';
3685       for (var p in body)
3686         req_body += (req_body ? '&' : '') + p+'='+urlencode(body[p]);
3687     }
a0109c 3688
f11541 3689     var ref = this;
a0109c 3690     this.url = url;
S 3691     this.busy = true;
3692     
3693     this.xmlhttp.onreadystatechange = function() { ref.xmlhttp_onreadystatechange(); };
3694     this.xmlhttp.open('POST', url, true);
3695     this.xmlhttp.setRequestHeader('Content-Type', contentType);
234c0d 3696     this.xmlhttp.setRequestHeader('X-RoundCube-Referer', bw.get_cookie('roundcube_sessid'));
f11541 3697     this.xmlhttp.send(req_body);
ecf759 3698     };
T 3699
3700
3701   // handle onreadystatechange event
3702   this.xmlhttp_onreadystatechange = function()
3703     {
3704     if(this.xmlhttp.readyState == 1)
3705       this.onloading(this);
3706
3707     else if(this.xmlhttp.readyState == 2)
3708       this.onloaded(this);
3709
3710     else if(this.xmlhttp.readyState == 3)
3711       this.oninteractive(this);
3712
3713     else if(this.xmlhttp.readyState == 4)
3714       {
9a5261 3715       try {
T 3716         if (this.xmlhttp.status == 0)
3717           this.onabort(this);
3718         else if(this.xmlhttp.status == 200)
3719           this.oncomplete(this);
3720         else
3721           this.onerror(this);
3722
3723         this.busy = false;
3724         }
3725       catch(err)
3726         {
ecf759 3727         this.onerror(this);
9a5261 3728         this.busy = false;
T 3729         }
ecf759 3730       }
T 3731     }
3732
3733   // getter method for HTTP headers
3734   this.get_header = function(name)
3735     {
3736     return this.xmlhttp.getResponseHeader(name);
3737     };
3738
c03095 3739   this.get_text = function()
T 3740     {
3741     return this.xmlhttp.responseText;
3742     };
3743
3744   this.get_xml = function()
3745     {
3746     return this.xmlhttp.responseXML;
3747     };
ecf759 3748
T 3749   this.reset();
3750   
3751   }  // end class rcube_http_request
3752
3753
e170b4 3754 // helper function to call the init method with a delay
T 3755 function call_init(o)
3756   {
3757   if (window[o] && window[o].init)
3758     setTimeout(o+'.init()', 200);
4e17e6 3759   }
T 3760