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