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