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