thomascube
2006-05-18 cead5c727147faac362e742aa7bcecf07f68cd99
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(){
dab065 1209       if (!this.list_rows) return false;
e8adc7 1210     var last_selected_row = this.list_rows[this.last_selected];
S 1211     var new_row = last_selected_row.obj.nextSibling;
1212     while (new_row && (new_row.nodeType != 1 || new_row.style.display == 'none')) {
1213       new_row = new_row.nextSibling;
1214     }
1215     return new_row;
1216   }
1217   
1218   this.get_prev_row = function(){
dab065 1219     if (!this.list_rows) return false;
e8adc7 1220     var last_selected_row = this.list_rows[this.last_selected];
S 1221     var new_row = last_selected_row.obj.previousSibling;
1222     while (new_row && (new_row.nodeType != 1 || new_row.style.display == 'none')) {
1223       new_row = new_row.previousSibling;
1224     }
1225     return new_row;
1226   }
1227   
d58c69 1228   // highlight/unhighlight a row
S 1229   this.highlight_row = function(id, multiple)
4e17e6 1230     {
T 1231     var selected = false
1232     
1233     if (this.list_rows[id] && !multiple)
1234       {
1235       this.clear_selection();
1236       this.selection[0] = id;
1237       this.list_rows[id].obj.className += ' selected';
1238       selected = true;
1239       }
1240     
1241     else if (this.list_rows[id])
1242       {
1243       if (!this.in_selection(id))  // select row
1244         {
1245         this.selection[this.selection.length] = id;
e8adc7 1246         this.set_classname(this.list_rows[id].obj, 'selected', true);    
4e17e6 1247         }
T 1248       else  // unselect row
1249         {
1250         var p = find_in_array(id, this.selection);
1251         var a_pre = this.selection.slice(0, p);
1252         var a_post = this.selection.slice(p+1, this.selection.length);
1253         this.selection = a_pre.concat(a_post);
1254         this.set_classname(this.list_rows[id].obj, 'selected', false);
1255         }
1256       selected = (this.selection.length==1);
1257       }
1258
1259     // enable/disable commands for message
1260     if (this.task=='mail')
1261       {
583f1c 1262       this.enable_command('show', 'reply', 'reply-all', 'forward', 'print', selected);
4e17e6 1263       this.enable_command('delete', 'moveto', this.selection.length>0 ? true : false);
T 1264       }
1265     else if (this.task=='addressbook')
1266       {
1267       this.enable_command('edit', /*'print',*/ selected);
1268       this.enable_command('delete', 'compose', this.selection.length>0 ? true : false);
1269       }
1270     };
1271
b11a00 1272
d58c69 1273 // selects or unselects the proper row depending on the modifier key pressed
857a38 1274   this.select_row = function(id,mod_key,with_mouse)  { 
d58c69 1275       if (!mod_key) {
S 1276       this.shift_start = id;
1277         this.highlight_row(id, false);
1278     } else {
1279       switch (mod_key) {
1280         case SHIFT_KEY: { 
1281           this.shift_select(id,false); 
1282           break; }
1283         case CONTROL_KEY: { 
1284           this.shift_start = id;
857a38 1285           if (!with_mouse)
e8adc7 1286             this.highlight_row(id, true); 
d58c69 1287           break; 
S 1288           }
1289         case CONTROL_SHIFT_KEY: { 
1290           this.shift_select(id,true);
1291           break;
1292           }
1293         default: {
1294           this.highlight_row(id, false); 
1295           break;
1296           }
1297       }
1298     }
cbd62d 1299     if (this.last_selected != 0) { this.set_classname(this.list_rows[this.last_selected].obj, 'focused', false);}
e8adc7 1300     this.last_selected = id;
cbd62d 1301     this.set_classname(this.list_rows[id].obj, 'focused', true);        
d58c69 1302   };
S 1303
1304   this.shift_select = function(id, control) {
1305     var from_rowIndex = this.list_rows[this.shift_start].obj.rowIndex;
b11a00 1306     var to_rowIndex = this.list_rows[id].obj.rowIndex;
T 1307         
1308     var i = ((from_rowIndex < to_rowIndex)? from_rowIndex : to_rowIndex);
1309     var j = ((from_rowIndex > to_rowIndex)? from_rowIndex : to_rowIndex);
1310     
1311     // iterate through the entire message list
1312     for (var n in this.list_rows) {
1313       if ((this.list_rows[n].obj.rowIndex >= i) && (this.list_rows[n].obj.rowIndex <= j)) {
1314         if (!this.in_selection(n))
d58c69 1315           this.highlight_row(n, true);
b11a00 1316       } else {
T 1317         if  (this.in_selection(n) && !control)
d58c69 1318           this.highlight_row(n, true);
b11a00 1319       }
T 1320     }
1321   };
1322   
4e17e6 1323
T 1324   this.clear_selection = function()
1325     {
1326     for(var n=0; n<this.selection.length; n++)
1327       if (this.list_rows[this.selection[n]])
1328         this.set_classname(this.list_rows[this.selection[n]].obj, 'selected', false);
1329
1330     this.selection = new Array();    
1331     };
1332
1333
1334   // check if given id is part of the current selection
1335   this.in_selection = function(id)
1336     {
1337     for(var n in this.selection)
1338       if (this.selection[n]==id)
1339         return true;
1340
1341     return false;    
1342     };
1343
1344
1345   // select each row in list
1346   this.select_all = function(filter)
1347     {
1348     if (!this.list_rows || !this.list_rows.length)
1349       return false;
1350       
1351     // reset selection first
1352     this.clear_selection();
1353     
1354     for (var n in this.list_rows)
1355       if (!filter || this.list_rows[n][filter]==true)
2a1e18 1356       this.highlight_row(n, true);
4e17e6 1357     };
T 1358     
1359
1360   // when user doble-clicks on a row
1361   this.show_message = function(id, safe)
1362     {
1363     var add_url = '';
1364     var target = window;
1365     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
1366       {
1367       target = window.frames[this.env.contentframe];
1368       add_url = '&_framed=1';
1369       }
1370       
1371     if (safe)
1372       add_url = '&_safe=1';
1373
1374     if (id)
1375       {
09941e 1376       this.set_busy(true, 'loading');
4e17e6 1377       target.location.href = this.env.comm_path+'&_action=show&_uid='+id+'&_mbox='+escape(this.env.mailbox)+add_url;
T 1378       }
1379     };
1380
1381
1382
1383   // list a specific page
1384   this.list_page = function(page)
1385     {
1386     if (page=='next')
1387       page = this.env.current_page+1;
1388     if (page=='prev' && this.env.current_page>1)
1389       page = this.env.current_page-1;
1390       
1391     if (page > 0 && page <= this.env.pagecount)
1392       {
1393       this.env.current_page = page;
1394       
1395       if (this.task=='mail')
1396         this.list_mailbox(this.env.mailbox, page);
1397       else if (this.task=='addressbook')
1398         this.list_contacts(page);
1399       }
1400     };
1401
1402
1403   // list messages of a specific mailbox
f3b659 1404   this.list_mailbox = function(mbox, page, sort)
4e17e6 1405     {
cbd62d 1406     this.last_selected = 0;
4e17e6 1407     var add_url = '';
T 1408     var target = window;
1409
1410     if (!mbox)
1411       mbox = this.env.mailbox;
1412
f3b659 1413     // add sort to url if set
T 1414     if (sort)
1415       add_url += '&_sort=' + sort;
1416       
4e17e6 1417     // set page=1 if changeing to another mailbox
T 1418     if (!page && mbox != this.env.mailbox)
1419       {
1420       page = 1;
9fee0e 1421       add_url += '&_refresh=1';
4e17e6 1422       this.env.current_page = page;
T 1423       this.clear_selection();
1424       }
4647e1 1425     
T 1426     // also send search request to get the right messages
1427     if (this.env.search_request)
1428       add_url += '&_search='+this.env.search_request;
4e17e6 1429       
T 1430     if (this.env.mailbox!=mbox)
1431       this.select_mailbox(mbox);
1432
1433     // load message list remotely
1434     if (this.gui_objects.messagelist)
1435       {
9fee0e 1436       this.list_mailbox_remote(mbox, page, add_url);
4e17e6 1437       return;
T 1438       }
1439     
1440     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
1441       {
1442       target = window.frames[this.env.contentframe];
9fee0e 1443       add_url += '&_framed=1';
4e17e6 1444       }
T 1445
1446     // load message list to target frame/window
1447     if (mbox)
1448       {
1449       this.set_busy(true, 'loading');
1450       target.location.href = this.env.comm_path+'&_mbox='+escape(mbox)+(page ? '&_page='+page : '')+add_url;
1451       }
1452     };
1453
1454
1455   // send remote request to load message list
9fee0e 1456   this.list_mailbox_remote = function(mbox, page, add_url)
4e17e6 1457     {
15a9d1 1458     // clear message list first
T 1459     this.clear_message_list();
1460
1461     // send request to server
1462     var url = '_mbox='+escape(mbox)+(page ? '&_page='+page : '');
1463     this.set_busy(true, 'loading');
1464     this.http_request('list', url+add_url, true);
1465     };
1466
1467
1468   this.clear_message_list = function()
1469     {
4e17e6 1470     var table = this.gui_objects.messagelist;
T 1471     var tbody = document.createElement('TBODY');
1472     table.insertBefore(tbody, table.tBodies[0]);
1473     table.removeChild(table.tBodies[1]);
1474     
1475     this.message_rows = new Array();
1476     this.list_rows = this.message_rows;
15a9d1 1477     
T 1478     };
1479
1480
1481   this.expunge_mailbox = function(mbox)
1482     {
1483     var lock = false;
1484     var add_url = '';
1485     
1486     // lock interface if it's the active mailbox
1487     if (mbox == this.env.mailbox)
1488        {
1489        lock = true;
1490        this.set_busy(true, 'loading');
1491        add_url = '&_reload=1';
1492        }
4e17e6 1493
T 1494     // send request to server
15a9d1 1495     var url = '_mbox='+escape(mbox);
T 1496     this.http_request('expunge', url+add_url, lock);
4e17e6 1497     };
T 1498
1499
5e3512 1500   this.purge_mailbox = function(mbox)
T 1501     {
1502     var lock = false;
1503     var add_url = '';
1504     
1505     if (!confirm(this.get_label('purgefolderconfirm')))
1506       return false;
1507     
1508     // lock interface if it's the active mailbox
1509     if (mbox == this.env.mailbox)
1510        {
1511        lock = true;
1512        this.set_busy(true, 'loading');
1513        add_url = '&_reload=1';
1514        }
1515
1516     // send request to server
1517     var url = '_mbox='+escape(mbox);
1518     this.http_request('purge', url+add_url, lock);
1519     };
1520     
1521
4e17e6 1522   // move selected messages to the specified mailbox
T 1523   this.move_messages = function(mbox)
1524     {
1525     // exit if no mailbox specified or if selection is empty
1526     if (!mbox || !(this.selection.length || this.env.uid) || mbox==this.env.mailbox)
1527       return;
1528     
1529     var a_uids = new Array();
1530
1531     if (this.env.uid)
1532       a_uids[a_uids.length] = this.env.uid;
1533     else
1534       {
1535       var id;
1536       for (var n=0; n<this.selection.length; n++)
1537         {
1538         id = this.selection[n];
1539         a_uids[a_uids.length] = id;
1540       
1541         // 'remove' message row from list (just hide it)
1542         if (this.message_rows[id].obj)
1543           this.message_rows[id].obj.style.display = 'none';
1544         }
cbd62d 1545       next_row = this.get_next_row();
S 1546       prev_row = this.get_prev_row();
1547       new_row = (next_row) ? next_row : prev_row;
dab065 1548       if (new_row) this.select_row(new_row.uid,false,false);
4e17e6 1549       }
ecf759 1550       
T 1551     var lock = false;
4e17e6 1552
T 1553     // show wait message
1554     if (this.env.action=='show')
ecf759 1555       {
T 1556       lock = true;
4e17e6 1557       this.set_busy(true, 'movingmessage');
ecf759 1558       }
4e17e6 1559
T 1560     // send request to server
ecf759 1561     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 1562     };
T 1563
857a38 1564   this.permanently_remove_messages = function() {
4e17e6 1565     // exit if no mailbox specified or if selection is empty
T 1566     if (!(this.selection.length || this.env.uid))
1567       return;
1568     
1569     var a_uids = new Array();
1570
1571     if (this.env.uid)
1572       a_uids[a_uids.length] = this.env.uid;
1573     else
1574       {
1575       var id;
1576       for (var n=0; n<this.selection.length; n++)
1577         {
1578         id = this.selection[n];
1579         a_uids[a_uids.length] = id;
1580       
1581         // 'remove' message row from list (just hide it)
1582         if (this.message_rows[id].obj)
1583           this.message_rows[id].obj.style.display = 'none';
1584         }
1585       }
cbd62d 1586       next_row = this.get_next_row();
S 1587       prev_row = this.get_prev_row();
1588       new_row = (next_row) ? next_row : prev_row;
dab065 1589       if (new_row) this.select_row(new_row.uid,false,false);
4e17e6 1590
T 1591     // send request to server
1592     this.http_request('delete', '_uid='+a_uids.join(',')+'&_mbox='+escape(this.env.mailbox)+'&_from='+(this.env.action ? this.env.action : ''));
857a38 1593   }
S 1594     
1595     
1596   // delete selected messages from the current mailbox
1597   this.delete_messages = function()
1598     {
1599     // exit if no mailbox specified or if selection is empty
1600     if (!(this.selection.length || this.env.uid))
1601       return;
1602     // if there is a trash mailbox defined and we're not currently in it:
1603     if (this.env.trash_mailbox && String(this.env.mailbox).toLowerCase()!=String(this.env.trash_mailbox).toLowerCase())
1604       this.move_messages(this.env.trash_mailbox);
1605     // if there is a trash mailbox defined but we *are* in it:
1606     else if (this.env.trash_mailbox && String(this.env.mailbox).toLowerCase() == String(this.env.trash_mailbox).toLowerCase())
1607       this.permanently_remove_messages();
1608     // if there isn't a defined trash mailbox and the config is set to flag for deletion
1609     else if (!this.env.trash_mailbox && this.env.flag_for_deletion) {
dab065 1610       this.mark_message('delete',this.env.uid);
S 1611       if(this.env.action=="show"){
1612         this.command('nextmessage','',this);
1613       } else {
1614         next_row = this.get_next_row();
1615         prev_row = this.get_prev_row();
1616         new_row = (next_row) ? next_row : prev_row;
1617         if (new_row) this.select_row(new_row.uid,false,false);
1618       }
857a38 1619     // if there isn't a defined trash mailbox and the config is set NOT to flag for deletion
S 1620     }else if (!this.env.trash_mailbox && !this.env.flag_for_deletion) {
1621       this.permanently_remove_messages();
1622     }
1623     return;
1624   };
4e17e6 1625
T 1626
1627   // set a specific flag to one or more messages
1628   this.mark_message = function(flag, uid)
1629     {
1630     var a_uids = new Array();
1631     
1632     if (uid)
1633       a_uids[0] = uid;
1634     else if (this.env.uid)
1635       a_uids[0] = this.env.uid;
1636     else
1637       {
1638       var id;
1639       for (var n=0; n<this.selection.length; n++)
1640         {
1641         id = this.selection[n];
1642         a_uids[a_uids.length] = id;
1643         }
1644       }
857a38 1645       switch (flag) {
S 1646         case 'read':
1647         case 'unread':
1648           this.toggle_read_status(flag,a_uids);
1649           break;
1650         case 'delete':
1651         case 'undelete':
1652           this.toggle_delete_status(flag,a_uids);
1653           break;
1654       }
1655     // send request to server
1656     this.http_request('mark', '_uid='+a_uids.join(',')+'&_flag='+flag);
1657     };
4e17e6 1658
857a38 1659   // set class to read/unread
S 1660   this.toggle_read_status = function(flag, a_uids) {
4e17e6 1661     // mark all message rows as read/unread
T 1662     var icn_src;
1663     for (var i=0; i<a_uids.length; i++)
1664       {
1665       uid = a_uids[i];
1666       if (this.message_rows[uid])
1667         {
1668         this.message_rows[uid].unread = (flag=='unread' ? true : false);
1669         
1670         if (this.message_rows[uid].classname.indexOf('unread')<0 && this.message_rows[uid].unread)
1671           {
1672           this.message_rows[uid].classname += ' unread';
fd660a 1673           this.set_classname(this.message_rows[uid].obj, 'unread', true);
T 1674
4e17e6 1675           if (this.env.unreadicon)
T 1676             icn_src = this.env.unreadicon;
1677           }
1678         else if (!this.message_rows[uid].unread)
1679           {
1680           this.message_rows[uid].classname = this.message_rows[uid].classname.replace(/\s*unread/, '');
fd660a 1681           this.set_classname(this.message_rows[uid].obj, 'unread', false);
4e17e6 1682
T 1683           if (this.message_rows[uid].replied && this.env.repliedicon)
1684             icn_src = this.env.repliedicon;
1685           else if (this.env.messageicon)
1686             icn_src = this.env.messageicon;
1687           }
1688
1689         if (this.message_rows[uid].icon && icn_src)
1690           this.message_rows[uid].icon.src = icn_src;
1691         }
1692       }
857a38 1693   }
S 1694   
1695   // mark all message rows as deleted/undeleted
1696   this.toggle_delete_status = function(flag, a_uids) {
1697     if (this.env.read_when_deleted) {
1698       this.toggle_read_status('read',a_uids);
1699     }
dab065 1700     // if deleting message from "view message" don't bother with delete icon
S 1701     if (this.env.action == "show")
1702       return false;
4e17e6 1703
857a38 1704     var icn_src;
S 1705     for (var i=0; i<a_uids.length; i++)
1706       {
1707       uid = a_uids[i];
1708       if (this.message_rows[uid])
1709         {
1710         this.message_rows[uid].deleted = (flag=='undelete' ? false : true);
1711         
1712         if (this.message_rows[uid].classname.indexOf('deleted')<0 && this.message_rows[uid].deleted)
1713           {
1714           this.message_rows[uid].classname += ' deleted';
1715           this.set_classname(this.message_rows[uid].obj, 'deleted', true);
4e17e6 1716
857a38 1717           if (this.env.deletedicon)
S 1718             icn_src = this.env.deletedicon;
1719           }
1720         else if (!this.message_rows[uid].deleted)
1721           {
1722           this.message_rows[uid].classname = this.message_rows[uid].classname.replace(/\s*deleted/, '');
1723           this.set_classname(this.message_rows[uid].obj, 'deleted', false);
1724
1725           if (this.message_rows[uid].unread && this.env.unreadicon)
1726             icn_src = this.env.unreadicon;
1727           else if (this.message_rows[uid].replied && this.env.repliedicon)
1728             icn_src = this.env.repliedicon;
1729           else if (this.env.messageicon)
1730             icn_src = this.env.messageicon;
1731           }
1732
1733         if (this.message_rows[uid].icon && icn_src)
1734           this.message_rows[uid].icon.src = icn_src;
1735         }
1736       }
1737   }
4e17e6 1738
T 1739
1740   /*********************************************************/
1741   /*********        message compose methods        *********/
1742   /*********************************************************/
1cded8 1743   
T 1744   
977a29 1745   // checks the input fields before sending a message
T 1746   this.check_compose_input = function()
1747     {
1748     // check input fields
1749     var input_to = rcube_find_object('_to');
1750     var input_subject = rcube_find_object('_subject');
1751     var input_message = rcube_find_object('_message');
1752
1753     // check for empty recipient
1754     if (input_to && !rcube_check_email(input_to.value, true))
1755       {
1756       alert(this.get_label('norecipientwarning'));
1757       input_to.focus();
1758       return false;
1759       }
1760
1761     // display localized warning for missing subject
1762     if (input_subject && input_subject.value == '')
1763       {
1764       var subject = prompt(this.get_label('nosubjectwarning'), this.get_label('nosubject'));
1765
1766       // user hit cancel, so don't send
1767       if (!subject && subject !== '')
1768         {
1769         input_subject.focus();
1770         return false;
1771         }
1772       else
1773         {
1774         input_subject.value = subject ? subject : this.get_label('nosubject');            
1775         }
1776       }
1777
1778     // check for empty body
1779     if (input_message.value=='')
1780       {
1781       if (!confirm(this.get_label('nobodywarning')))
1782         {
1783         input_message.focus();
1784         return false;
1785         }
1786       }
1787
1788     return true;
1789     };
1790     
1791     
1792   this.compose_field_hash = function()
1793     {
1794     // check input fields
1795     var input_to = rcube_find_object('_to');
1796     var input_cc = rcube_find_object('_to');
1797     var input_bcc = rcube_find_object('_to');
1798     var input_subject = rcube_find_object('_subject');
1799     var input_message = rcube_find_object('_message');
1800     
1801     var str = '';
1802     if (input_to && input_to.value)
1803       str += input_to.value+':';
1804     if (input_cc && input_cc.value)
1805       str += input_cc.value+':';
1806     if (input_bcc && input_bcc.value)
1807       str += input_bcc.value+':';
1808     if (input_subject && input_subject.value)
1809       str += input_subject.value+':';
1810     if (input_message && input_message.value)
1811       str += input_message.value;
1812
1813     return str;
1814     };
1815     
1816   
1cded8 1817   this.change_identity = function(obj)
T 1818     {
1819     if (!obj || !obj.options)
1820       return false;
1821
1822     var id = obj.options[obj.selectedIndex].value;
1823     var input_message = rcube_find_object('_message');
1824     var message = input_message ? input_message.value : '';
749b07 1825     var sig, p;
1cded8 1826
T 1827     // remove the 'old' signature
1828     if (this.env.identity && this.env.signatures && this.env.signatures[this.env.identity])
1829       {
749b07 1830       sig = this.env.signatures[this.env.identity];
T 1831       if (sig.indexOf('-- ')!=0)
1832         sig = '-- \n'+sig;
1833
1834       p = message.lastIndexOf(sig);
1835       if (p>=0)
1cded8 1836         message = message.substring(0, p-1) + message.substring(p+sig.length, message.length);
T 1837       }
1838
1839     // add the new signature string
1840     if (this.env.signatures && this.env.signatures[id])
1841       {
749b07 1842       sig = this.env.signatures[id];
T 1843       if (sig.indexOf('-- ')!=0)
1844         sig = '-- \n'+sig;
1cded8 1845       message += '\n'+sig;
T 1846       }
1847
749b07 1848     if (input_message)
1cded8 1849       input_message.value = message;
T 1850       
1851     this.env.identity = id;
1852     };
4e17e6 1853
T 1854
1855   this.show_attachment_form = function(a)
1856     {
1857     if (!this.gui_objects.uploadbox)
1858       return false;
1859       
1860     var elm, list;
1861     if (elm = this.gui_objects.uploadbox)
1862       {
1863       if (a &&  (list = this.gui_objects.attachmentlist))
1864         {
1865         var pos = rcube_get_object_pos(list);
1866         var left = pos.x;
1867         var top = pos.y + list.offsetHeight + 10;
1868       
1869         elm.style.top = top+'px';
1870         elm.style.left = left+'px';
1871         }
1872       
1873       elm.style.visibility = a ? 'visible' : 'hidden';
1874       }
1875       
1876     // clear upload form
1877     if (!a && this.gui_objects.attachmentform && this.gui_objects.attachmentform!=this.gui_objects.messageform)
1878       this.gui_objects.attachmentform.reset();
1879     };
1880
1881
1882   // upload attachment file
1883   this.upload_file = function(form)
1884     {
1885     if (!form)
1886       return false;
1887       
1888     // get file input fields
1889     var send = false;
1890     for (var n=0; n<form.elements.length; n++)
1891       if (form.elements[n].type=='file' && form.elements[n].value)
1892         {
1893         send = true;
1894         break;
1895         }
1896     
1897     // create hidden iframe and post upload form
1898     if (send)
1899       {
1900       var ts = new Date().getTime();
1901       var frame_name = 'rcmupload'+ts;
1902
1903       // have to do it this way for IE
1904       // otherwise the form will be posted to a new window
1905       if(document.all && !window.opera)
1906         {
1907         var html = '<iframe name="'+frame_name+'" src="program/blank.gif" style="width:0;height:0;visibility:hidden;"></iframe>';
1908         document.body.insertAdjacentHTML('BeforeEnd',html);
1909         }
1910       else  // for standards-compilant browsers
1911         {
1912         var frame = document.createElement('IFRAME');
1913         frame.name = frame_name;
1914         frame.width = 10;
1915         frame.height = 10;
1916         frame.style.visibility = 'hidden';
1917         document.body.appendChild(frame);
1918         }
1919
1920       form.target = frame_name;
1921       form.action = this.env.comm_path+'&_action=upload';
1922       form.setAttribute('enctype', 'multipart/form-data');
1923       form.submit();
1924       }
1925     
1926     // set reference to the form object
1927     this.gui_objects.attachmentform = form;
1928     };
1929
1930
1931   // add file name to attachment list
1932   // called from upload page
1933   this.add2attachment_list = function(name)
1934     {
1935     if (!this.gui_objects.attachmentlist)
1936       return false;
1937       
1938     var li = document.createElement('LI');
1939     li.innerHTML = name;
1940     this.gui_objects.attachmentlist.appendChild(li);
1941     };
1942
1943
1944   // send remote request to add a new contact
1945   this.add_contact = function(value)
1946     {
1947     if (value)
1948       this.http_request('addcontact', '_address='+value);
1949     };
1950
4647e1 1951   // send remote request to search mail
T 1952   this.qsearch = function(value, mbox)
1953     {
1954     if (value && mbox)
1955       {
1956       this.clear_message_list();
1957       this.set_busy(true, 'searching');
1958       this.http_request('search', '_search='+value+'&_mbox='+mbox, true);
1959       }
1960     };
1961
1962   // reset quick-search form
1963   this.reset_qsearch = function()
1964     {
1965     if (this.gui_objects.qsearchbox)
1966       this.gui_objects.qsearchbox.value = '';
1967       
1968     this.env.search_request = null;
1969     };
1970     
4e17e6 1971
T 1972   /*********************************************************/
1973   /*********     keyboard live-search methods      *********/
1974   /*********************************************************/
1975
1976
1977   // handler for keyboard events on address-fields
1978   this.ksearch_keypress = function(e, obj)
1979     {
1980     if (typeof(this.env.contacts)!='object' || !this.env.contacts.length)
1981       return true;
1982
1983     if (this.ksearch_timer)
1984       clearTimeout(this.ksearch_timer);
1985
1986     if (!e)
1987       e = window.event;
1988       
1989     var highlight;
1990     var key = e.keyCode ? e.keyCode : e.which;
1991
1992     switch (key)
1993       {
1994       case 38:  // key up
1995       case 40:  // key down
1996         if (!this.ksearch_pane)
1997           break;
1998           
1999         var dir = key==38 ? 1 : 0;
2000         var next;
2001         
2002         highlight = document.getElementById('rcmksearchSelected');
2003         if (!highlight)
2004           highlight = this.ksearch_pane.ul.firstChild;
2005         
2006         if (highlight && (next = dir ? highlight.previousSibling : highlight.nextSibling))
2007           {
2008           highlight.removeAttribute('id');
2009           //highlight.removeAttribute('class');
2010           this.set_classname(highlight, 'selected', false);
2011           }
2012
2013         if (next)
2014           {
2015           next.setAttribute('id', 'rcmksearchSelected');
2016           this.set_classname(next, 'selected', true);
2017           this.ksearch_selected = next._rcm_id;
2018           }
2019
2020         if (e.preventDefault)
2021           e.preventDefault();
2022         return false;
2023
2024       case 9:  // tab
2025         if(e.shiftKey)
2026           break;
2027
2028       case 13:  // enter     
2029         if (this.ksearch_selected===null || !this.ksearch_input || !this.ksearch_value)
2030           break;
2031
2032         // get cursor pos
2033         var inp_value = this.ksearch_input.value.toLowerCase();
2034         var cpos = this.get_caret_pos(this.ksearch_input);
2035         var p = inp_value.lastIndexOf(this.ksearch_value, cpos);
2036         
2037         // replace search string with full address
2038         var pre = this.ksearch_input.value.substring(0, p);
2039         var end = this.ksearch_input.value.substring(p+this.ksearch_value.length, this.ksearch_input.value.length);
2040         var insert = this.env.contacts[this.ksearch_selected]+', ';
2041         this.ksearch_input.value = pre + insert + end;
2042         
2043         //this.ksearch_input.value = this.ksearch_input.value.substring(0, p)+insert;
2044         
2045         // set caret to insert pos
2046         cpos = p+insert.length;
2047         if (this.ksearch_input.setSelectionRange)
2048           this.ksearch_input.setSelectionRange(cpos, cpos);
2049         
2050         // hide ksearch pane
2051         this.ksearch_hide();
2052       
2053         if (e.preventDefault)
2054           e.preventDefault();
2055         return false;
2056
2057       case 27:  // escape
2058         this.ksearch_hide();
2059         break;
2060
2061       }
2062
2063     // start timer
2064     this.ksearch_timer = setTimeout(this.ref+'.ksearch_get_results()', 200);      
2065     this.ksearch_input = obj;
2066     
2067     return true;
2068     };
2069
2070
2071   // address search processor
2072   this.ksearch_get_results = function()
2073     {
2074     var inp_value = this.ksearch_input ? this.ksearch_input.value : null;
2075     if (inp_value===null)
2076       return;
2077
2078     // get string from current cursor pos to last comma
2079     var cpos = this.get_caret_pos(this.ksearch_input);
2080     var p = inp_value.lastIndexOf(',', cpos-1);
2081     var q = inp_value.substring(p+1, cpos);
2082
2083     // trim query string
2084     q = q.replace(/(^\s+|\s+$)/g, '').toLowerCase();
2085
2086     if (!q.length || q==this.ksearch_value)
2087       {
2088       if (!q.length && this.ksearch_pane && this.ksearch_pane.visible)
2089         this.ksearch_pane.show(0);
2090
2091       return;
2092       }
2093
2094     this.ksearch_value = q;
2095     
2096     // start searching the contact list
2097     var a_results = new Array();
2098     var a_result_ids = new Array();
2099     var c=0;
2100     for (var i=0; i<this.env.contacts.length; i++)
2101       {
2102       if (this.env.contacts[i].toLowerCase().indexOf(q)>=0)
2103         {
2104         a_results[c] = this.env.contacts[i];
2105         a_result_ids[c++] = i;
2106         
2107         if (c==15)  // limit search results
2108           break;
2109         }
2110       }
2111
2112     // display search results
2113     if (c && a_results.length)
2114       {
2115       var p, ul, li;
2116       
2117       // create results pane if not present
2118       if (!this.ksearch_pane)
2119         {
2120         ul = document.createElement('UL');
2121         this.ksearch_pane = new rcube_layer('rcmKSearchpane', {vis:0, zindex:30000});
2122         this.ksearch_pane.elm.appendChild(ul);
2123         this.ksearch_pane.ul = ul;
2124         }
2125       else
2126         ul = this.ksearch_pane.ul;
2127
2128       // remove all search results
2129       ul.innerHTML = '';
2130             
2131       // add each result line to list
2132       for (i=0; i<a_results.length; i++)
2133         {
2134         li = document.createElement('LI');
2135         li.innerHTML = a_results[i].replace(/</, '&lt;').replace(/>/, '&gt;');
2136         li._rcm_id = a_result_ids[i];
2137         ul.appendChild(li);
2138         }
2139
2140       // check if last selected item is still in result list
2141       if (this.ksearch_selected!==null)
2142         {
2143         p = find_in_array(this.ksearch_selected, a_result_ids);
2144         if (p>=0 && ul.childNodes)
2145           {
2146           ul.childNodes[p].setAttribute('id', 'rcmksearchSelected');
2147           this.set_classname(ul.childNodes[p], 'selected', true);
2148           }
2149         else
2150           this.ksearch_selected = null;
2151         }
2152       
2153       // if no item selected, select the first one
2154       if (this.ksearch_selected===null)
2155         {
2156         ul.firstChild.setAttribute('id', 'rcmksearchSelected');
2157         this.set_classname(ul.firstChild, 'selected', true);
2158         this.ksearch_selected = a_result_ids[0];
2159         }
2160
2161       // resize the containing layer to fit the list
2162       //this.ksearch_pane.resize(ul.offsetWidth, ul.offsetHeight);
2163     
2164       // move the results pane right under the input box and make it visible
2165       var pos = rcube_get_object_pos(this.ksearch_input);
2166       this.ksearch_pane.move(pos.x, pos.y+this.ksearch_input.offsetHeight);
2167       this.ksearch_pane.show(1); 
2168       }
2169     // hide results pane
2170     else
2171       this.ksearch_hide();
2172     };
2173
2174
2175   this.ksearch_blur = function(e, obj)
2176     {
2177     if (this.ksearch_timer)
2178       clearTimeout(this.ksearch_timer);
2179
2180     this.ksearch_value = '';      
2181     this.ksearch_input = null;
2182     
2183     this.ksearch_hide();
2184     };
2185
2186
2187   this.ksearch_hide = function()
2188     {
2189     this.ksearch_selected = null;
2190     
2191     if (this.ksearch_pane)
2192       this.ksearch_pane.show(0);    
2193     };
2194
2195
2196
2197   /*********************************************************/
2198   /*********         address book methods          *********/
2199   /*********************************************************/
2200
2201
2202   this.list_contacts = function(page)
2203     {
2204     var add_url = '';
2205     var target = window;
2206     
2207     if (page && this.current_page==page)
2208       return false;
2209
2210     // load contacts remotely
2211     if (this.gui_objects.contactslist)
2212       {
2213       this.list_contacts_remote(page);
2214       return;
2215       }
2216
2217     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
2218       {
2219       target = window.frames[this.env.contentframe];
2220       add_url = '&_framed=1';
2221       }
2222
2223     this.set_busy(true, 'loading');
2224     location.href = this.env.comm_path+(page ? '&_page='+page : '')+add_url;
2225     };
2226
2227
2228   // send remote request to load contacts list
2229   this.list_contacts_remote = function(page)
2230     {
2231     // clear list
2232     var table = this.gui_objects.contactslist;
2233     var tbody = document.createElement('TBODY');
2234     table.insertBefore(tbody, table.tBodies[0]);
2235     table.tBodies[1].style.display = 'none';
2236     
2237     this.contact_rows = new Array();
2238     this.list_rows = this.contact_rows;
2239
2240     // send request to server
2241     var url = page ? '&_page='+page : '';
2242     this.set_busy(true, 'loading');
ecf759 2243     this.http_request('list', url, true);
4e17e6 2244     };
T 2245
2246
2247   // load contact record
2248   this.load_contact = function(cid, action, framed)
2249     {
2250     var add_url = '';
2251     var target = window;
2252     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
2253       {
2254       add_url = '&_framed=1';
2255       target = window.frames[this.env.contentframe];
2256       document.getElementById(this.env.contentframe).style.visibility = 'inherit';
2257       }
2258     else if (framed)
2259       return false;
2260       
2261     //if (this.env.framed && add_url=='')
5e3512 2262     
4e17e6 2263     //  add_url = '&_framed=1';
T 2264     
2265     if (action && (cid || action=='add'))
2266       {
2267       this.set_busy(true);
2268       target.location.href = this.env.comm_path+'&_action='+action+'&_cid='+cid+add_url;
2269       }
2270     };
2271
2272
2273   this.delete_contacts = function()
2274     {
2275     // exit if no mailbox specified or if selection is empty
5e3512 2276     if (!(this.selection.length || this.env.cid) || !confirm(this.get_label('deletecontactconfirm')))
4e17e6 2277       return;
5e3512 2278       
4e17e6 2279     var a_cids = new Array();
T 2280
2281     if (this.env.cid)
2282       a_cids[a_cids.length] = this.env.cid;
2283     else
2284       {
2285       var id;
2286       for (var n=0; n<this.selection.length; n++)
2287         {
2288         id = this.selection[n];
2289         a_cids[a_cids.length] = id;
2290       
2291         // 'remove' row from list (just hide it)
2292         if (this.contact_rows[id].obj)
2293           this.contact_rows[id].obj.style.display = 'none';
2294         }
2295
2296       // hide content frame if we delete the currently displayed contact
2297       if (this.selection.length==1 && this.env.contentframe)
2298         {
2299         var elm = document.getElementById(this.env.contentframe);
2300         elm.style.visibility = 'hidden';
2301         }
2302       }
2303
2304     // send request to server
2305     this.http_request('delete', '_cid='+a_cids.join(',')+'&_from='+(this.env.action ? this.env.action : ''));
2306     };
2307
2308
2309   // update a contact record in the list
2310   this.update_contact_row = function(cid, cols_arr)
2311     {
2312     if (!this.contact_rows[cid] || !this.contact_rows[cid].obj)
2313       return false;
2314       
2315     var row = this.contact_rows[cid].obj;
2316     for (var c=0; c<cols_arr.length; c++)
2317       if (row.cells[c])
2318         row.cells[c].innerHTML = cols_arr[c];
2319
2320     };
2321   
d1d2c4 2322   
S 2323   // load ldap search form
2324   this.ldappublicsearch = function(action)
2325     {
2326     var add_url = '';
2327     var target = window;
2328     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
2329       {
2330       add_url = '&_framed=1';
2331       target = window.frames[this.env.contentframe];
2332       document.getElementById(this.env.contentframe).style.visibility = 'inherit';
2333       }
2334     else
2335       return false; 
2336
2337
2338     if (action == 'ldappublicsearch')
2339       target.location.href = this.env.comm_path+'&_action='+action+add_url;
2340     };
2341  
2342   // add ldap contacts to address book
2343   this.add_ldap_contacts = function()
2344     {
2345     if (window.frames[this.env.contentframe].rcmail)
2346       {
2347       var frame = window.frames[this.env.contentframe];
2348
2349       // build the url
2350       var url    = '&_framed=1';
2351       var emails = '&_emails=';
2352       var names  = '&_names=';
2353       var end    = '';
2354       for (var n=0; n<frame.rcmail.selection.length; n++)
2355         {
2356         end = n < frame.rcmail.selection.length - 1 ? ',' : '';
2357         emails += frame.rcmail.ldap_contact_rows[frame.rcmail.selection[n]].obj.cells[1].innerHTML + end;
2358         names  += frame.rcmail.ldap_contact_rows[frame.rcmail.selection[n]].obj.cells[0].innerHTML + end;
2359         }
2360        
2361       frame.location.href = this.env.comm_path + '&_action=save&_framed=1' + emails + names;
2362       }
2363     return false;
2364     }
2365   
4e17e6 2366
T 2367
2368   /*********************************************************/
2369   /*********        user settings methods          *********/
2370   /*********************************************************/
2371
2372
2373   // load contact record
2374   this.load_identity = function(id, action)
2375     {
2376     if (action=='edit-identity' && (!id || id==this.env.iid))
2377       return;
2378
2379     var add_url = '';
2380     var target = window;
2381     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
2382       {
2383       add_url = '&_framed=1';
2384       target = window.frames[this.env.contentframe];
2385       document.getElementById(this.env.contentframe).style.visibility = 'inherit';
2386       }
2387
2388     if (action && (id || action=='add-identity'))
2389       {
2390       this.set_busy(true);
2391       target.location.href = this.env.comm_path+'&_action='+action+'&_iid='+id+add_url;
2392       }
2393     };
2394
2395
2396
2397   this.delete_identity = function(id)
2398     {
2399     // exit if no mailbox specified or if selection is empty
2400     if (!(this.selection.length || this.env.iid))
2401       return;
2402     
2403     if (!id)
2404       id = this.env.iid ? this.env.iid : this.selection[0];
2405
2406 /*
2407     // 'remove' row from list (just hide it)
2408     if (this.identity_rows && this.identity_rows[id].obj)
2409       {
2410       this.clear_selection();
2411       this.identity_rows[id].obj.style.display = 'none';
2412       }
2413 */
2414
2415     // if (this.env.framed && id)
2416       this.set_busy(true);
2417       location.href = this.env.comm_path+'&_action=delete-identity&_iid='+id;
2418     // else if (id)
2419     //  this.http_request('delete-identity', '_iid='+id);
2420     };
2421
2422
2423   this.create_folder = function(name)
2424     {
2425     var form;
2426     if ((form = this.gui_objects.editform) && form.elements['_folder_name'])
2427       name = form.elements['_folder_name'].value;
2428
2429     if (name)
ecf759 2430       this.http_request('create-folder', '_name='+escape(name), true);
4e17e6 2431     else if (form.elements['_folder_name'])
T 2432       form.elements['_folder_name'].focus();
2433     };
2434
2435
2436   this.delete_folder = function(folder)
2437     {
2438     if (folder)
2439       {
2440       this.http_request('delete-folder', '_mboxes='+escape(folder));
2441       }
1cded8 2442     };
T 2443
2444
2445   this.remove_folder_row = function(folder)
2446     {
2447     for (var id in this.env.subscriptionrows)
2448       if (this.env.subscriptionrows[id]==folder)
2449         break;
2450
2451     var row;
2452     if (id && (row = document.getElementById(id)))
2453       row.style.display = 'none';    
4e17e6 2454     };
T 2455
2456
2457   this.subscribe_folder = function(folder)
2458     {
2459     var form;
2460     if ((form = this.gui_objects.editform) && form.elements['_unsubscribed'])
2461       this.change_subscription('_unsubscribed', '_subscribed', 'subscribe');
2462     else if (folder)
2463       this.http_request('subscribe', '_mboxes='+escape(folder));
2464     };
2465
2466
2467   this.unsubscribe_folder = function(folder)
2468     {
2469     var form;
2470     if ((form = this.gui_objects.editform) && form.elements['_subscribed'])
2471       this.change_subscription('_subscribed', '_unsubscribed', 'unsubscribe');
2472     else if (folder)
2473       this.http_request('unsubscribe', '_mboxes='+escape(folder));
2474     };
2475     
2476
2477   this.change_subscription = function(from, to, action)
2478     {
2479     var form;
2480     if (form = this.gui_objects.editform)
2481       {
2482       var a_folders = new Array();
2483       var list_from = form.elements[from];
2484
2485       for (var i=0; list_from && i<list_from.options.length; i++)
2486         {
2487         if (list_from.options[i] && list_from.options[i].selected)
2488           {
2489           a_folders[a_folders.length] = list_from.options[i].value;
2490           list_from[i] = null;
2491           i--;
2492           }
2493         }
2494
2495       // yes, we have some folders selected
2496       if (a_folders.length)
2497         {
2498         var list_to = form.elements[to];
2499         var index;
2500         
2501         for (var n=0; n<a_folders.length; n++)
2502           {
2503           index = list_to.options.length;
2504           list_to[index] = new Option(a_folders[n]);
2505           }
2506           
2507         this.http_request(action, '_mboxes='+escape(a_folders.join(',')));
2508         }
2509       }
2510       
2511     };
2512
2513
2514    // add a new folder to the subscription list by cloning a folder row
2515    this.add_folder_row = function(name)
2516      {
2517      if (!this.gui_objects.subscriptionlist)
2518        return false;
2519
2520      var tbody = this.gui_objects.subscriptionlist.tBodies[0];
2521      var id = tbody.childNodes.length+1;
2522      
2523      // clone a table row
2524      var row = this.clone_table_row(tbody.rows[0]);
2525      row.id = 'rcmrow'+id;
2526      tbody.appendChild(row);
2527
2528      // add to folder/row-ID map
2529      this.env.subscriptionrows[row.id] = name;
2530
2531      // set folder name
2532      row.cells[0].innerHTML = name;
2533      if (row.cells[1].firstChild.tagName=='INPUT')
2534        {
2535        row.cells[1].firstChild.value = name;
2536        row.cells[1].firstChild.checked = true;
2537        }
2538      if (row.cells[2].firstChild.tagName=='A')
2539        row.cells[2].firstChild.onclick = new Function(this.ref+".command('delete-folder','"+name+"')");
14eafe 2540
T 2541     var form;
2542     if ((form = this.gui_objects.editform) && form.elements['_folder_name'])
2543       form.elements['_folder_name'].value = '';
4e17e6 2544      };
T 2545
2546
2547   // duplicate a specific table row
2548   this.clone_table_row = function(row)
2549     {
2550     var cell, td;
2551     var new_row = document.createElement('TR');
2552     for(var n=0; n<row.childNodes.length; n++)
2553       {
2554       cell = row.childNodes[n];
2555       td = document.createElement('TD');
2556
2557       if (cell.className)
2558         td.className = cell.className;
2559       if (cell.align)
2560         td.setAttribute('align', cell.align);
2561         
2562       td.innerHTML = cell.innerHTML;
2563       new_row.appendChild(td);
2564       }
2565     
2566     return new_row;
2567     };
2568
2569
2570   /*********************************************************/
2571   /*********           GUI functionality           *********/
2572   /*********************************************************/
2573
2574
2575   // eable/disable buttons for page shifting
2576   this.set_page_buttons = function()
2577     {
2578     this.enable_command('nextpage', (this.env.pagecount > this.env.current_page));
2579     this.enable_command('previouspage', (this.env.current_page > 1));
2580     }
2581
2582
2583   // set button to a specific state
2584   this.set_button = function(command, state)
2585     {
2586     var a_buttons = this.buttons[command];
2587     var button, obj;
2588
2589     if(!a_buttons || !a_buttons.length)
2590       return;
2591
2592     for(var n=0; n<a_buttons.length; n++)
2593       {
2594       button = a_buttons[n];
2595       obj = document.getElementById(button.id);
2596
2597       // get default/passive setting of the button
2598       if (obj && button.type=='image' && !button.status)
2599         button.pas = obj._original_src ? obj._original_src : obj.src;
2600       else if (obj && !button.status)
2601         button.pas = String(obj.className);
2602
2603       // set image according to button state
2604       if (obj && button.type=='image' && button[state])
2605         {
2606         button.status = state;        
2607         obj.src = button[state];
2608         }
2609       // set class name according to button state
2610       else if (obj && typeof(button[state])!='undefined')
2611         {
2612         button.status = state;        
2613         obj.className = button[state];        
2614         }
2615       // disable/enable input buttons
2616       if (obj && button.type=='input')
2617         {
2618         button.status = state;
2619         obj.disabled = !state;
2620         }
2621       }
2622     };
2623
2624
2625   // mouse over button
2626   this.button_over = function(command, id)
2627     {
2628     var a_buttons = this.buttons[command];
2629     var button, img;
2630
2631     if(!a_buttons || !a_buttons.length)
2632       return;
2633
2634     for(var n=0; n<a_buttons.length; n++)
2635       {
2636       button = a_buttons[n];
2637       if(button.id==id && button.status=='act')
2638         {
2639         img = document.getElementById(button.id);
2640         if (img && button.over)
2641           img.src = button.over;
2642         }
2643       }
2644     };
2645
2646
2647   // mouse out of button
2648   this.button_out = function(command, id)
2649     {
2650     var a_buttons = this.buttons[command];
2651     var button, img;
2652
2653     if(!a_buttons || !a_buttons.length)
2654       return;
2655
2656     for(var n=0; n<a_buttons.length; n++)
2657       {
2658       button = a_buttons[n];
2659       if(button.id==id && button.status=='act')
2660         {
2661         img = document.getElementById(button.id);
2662         if (img && button.act)
2663           img.src = button.act;
2664         }
2665       }
2666     };
2667
2668
2669   // set/unset a specific class name
2670   this.set_classname = function(obj, classname, set)
2671     {
2672     var reg = new RegExp('\s*'+classname, 'i');
2673     if (!set && obj.className.match(reg))
2674       obj.className = obj.className.replace(reg, '');
2675     else if (set && !obj.className.match(reg))
2676       obj.className += ' '+classname;
2677     };
2678
2679
2680   // display a specific alttext
2681   this.alttext = function(text)
2682     {
2683     
2684     };
2685
2686
2687   // display a system message
2688   this.display_message = function(msg, type, hold)
2689     {
2690     if (!this.loaded)  // save message in order to display after page loaded
2691       {
2692       this.pending_message = new Array(msg, type);
2693       return true;
2694       }
2695     
2696     if (!this.gui_objects.message)
2697       return false;
2698       
2699     if (this.message_timer)
2700       clearTimeout(this.message_timer);
2701     
2702     var cont = msg;
2703     if (type)
2704       cont = '<div class="'+type+'">'+cont+'</div>';
a95e0e 2705
T 2706     this.gui_objects.message._rcube = this;
4e17e6 2707     this.gui_objects.message.innerHTML = cont;
T 2708     this.gui_objects.message.style.display = 'block';
a95e0e 2709     
T 2710     if (type!='loading')
2711       this.gui_objects.message.onmousedown = function(){ this._rcube.hide_message(); return true; };
4e17e6 2712     
T 2713     if (!hold)
2714       this.message_timer = setTimeout(this.ref+'.hide_message()', this.message_time);
2715     };
2716
2717
2718   // make a message row disapear
2719   this.hide_message = function()
2720     {
2721     if (this.gui_objects.message)
a95e0e 2722       {
4e17e6 2723       this.gui_objects.message.style.display = 'none';
a95e0e 2724       this.gui_objects.message.onmousedown = null;
T 2725       }
4e17e6 2726     };
T 2727
2728
2729   // mark a mailbox as selected and set environment variable
2730   this.select_mailbox = function(mbox)
2731     {
2732     if (this.gui_objects.mailboxlist)
2733       {
2734       var item, reg, text_obj;
6a35c8 2735       var s_current = this.env.mailbox.toLowerCase().replace(this.mbox_expression, '');
4e17e6 2736       var s_mbox = String(mbox).toLowerCase().replace(this.mbox_expression, '');
T 2737       var s_current = this.env.mailbox.toLowerCase().replace(this.mbox_expression, '');
597170 2738       
6a35c8 2739       var current_li = document.getElementById('rcmbx'+s_current);
T 2740       var mbox_li = document.getElementById('rcmbx'+s_mbox);
2741       
2742       if (current_li)
2743         this.set_classname(current_li, 'selected', false);
2744       if (mbox_li)
2745         this.set_classname(mbox_li, 'selected', true);
4e17e6 2746       }
T 2747     
2748     this.env.mailbox = mbox;
2749     };
2750
2751
2752   // create a table row in the message list
15a9d1 2753   this.add_message_row = function(uid, cols, flags, attachment, attop)
4e17e6 2754     {
T 2755     if (!this.gui_objects.messagelist || !this.gui_objects.messagelist.tBodies[0])
2756       return false;
2757     
2758     var tbody = this.gui_objects.messagelist.tBodies[0];
2759     var rowcount = tbody.rows.length;
2760     var even = rowcount%2;
2761     
857a38 2762     this.env.messages[uid] = {deleted:flags.deleted?1:0,
S 2763                               replied:flags.replied?1:0,
4e17e6 2764                               unread:flags.unread?1:0};
T 2765     
2766     var row = document.createElement('TR');
2767     row.id = 'rcmrow'+uid;
15a9d1 2768     row.className = 'message '+(even ? 'even' : 'odd')+(flags.unread ? ' unread' : '')+(flags.deleted ? ' deleted' : '');
T 2769     
4e17e6 2770     if (this.in_selection(uid))
T 2771       row.className += ' selected';
2772
857a38 2773     var icon = flags.deleted && this.env.deletedicon ? this.env.deletedicon:
S 2774                (flags.unread && this.env.unreadicon ? this.env.unreadicon :
2775                (flags.replied && this.env.repliedicon ? this.env.repliedicon : this.env.messageicon));
4e17e6 2776
T 2777     var col = document.createElement('TD');
2778     col.className = 'icon';
2779     col.innerHTML = icon ? '<img src="'+icon+'" alt="" border="0" />' : '';
2780     row.appendChild(col);
2781
2782     // add each submitted col
2783     for (var c in cols)
2784       {
2785       col = document.createElement('TD');
2786       col.className = String(c).toLowerCase();
2787       col.innerHTML = cols[c];
2788       row.appendChild(col);
2789       }
2790
2791     col = document.createElement('TD');
2792     col.className = 'icon';
2793     col.innerHTML = attachment && this.env.attachmenticon ? '<img src="'+this.env.attachmenticon+'" alt="" border="0" />' : '';
2794     row.appendChild(col);
2795     
15a9d1 2796     if (attop && tbody.rows.length)
T 2797       tbody.insertBefore(row, tbody.firstChild);
2798     else
2799       tbody.appendChild(row);
2800       
4e17e6 2801     this.init_message_row(row);
T 2802     };
2803
2804
2805   // replace content of row count display
2806   this.set_rowcount = function(text)
2807     {
2808     if (this.gui_objects.countdisplay)
2809       this.gui_objects.countdisplay.innerHTML = text;
2810
2811     // update page navigation buttons
2812     this.set_page_buttons();
2813     };
2814
58e360 2815   // replace content of quota display
T 2816    this.set_quota = function(text)
2817      {
2818      if (this.gui_objects.quotadisplay)
2819        this.gui_objects.quotadisplay.innerHTML = text;
2820      };
2821                  
4e17e6 2822
T 2823   // update the mailboxlist
15a9d1 2824   this.set_unread_count = function(mbox, count, set_title)
4e17e6 2825     {
T 2826     if (!this.gui_objects.mailboxlist)
2827       return false;
f108b9 2828       
4e17e6 2829     var item, reg, text_obj;
15a9d1 2830     mbox = String(mbox).toLowerCase().replace(this.mbox_expression, '');
T 2831     item = document.getElementById('rcmbx'+mbox);
2832
2833     if (item && item.className && item.className.indexOf('mailbox '+mbox)>=0)
4e17e6 2834       {
15a9d1 2835       // set new text
T 2836       text_obj = item.firstChild;
2837       reg = /\s+\([0-9]+\)$/i;
4e17e6 2838
15a9d1 2839       if (count && text_obj.innerHTML.match(reg))
T 2840         text_obj.innerHTML = text_obj.innerHTML.replace(reg, ' ('+count+')');
2841       else if (count)
2842         text_obj.innerHTML += ' ('+count+')';
2843       else
2844         text_obj.innerHTML = text_obj.innerHTML.replace(reg, '');
4e17e6 2845           
15a9d1 2846       // set the right classes
T 2847       this.set_classname(item, 'unread', count>0 ? true : false);
2848       }
2849
2850     // set unread count to window title
01c86f 2851     reg = /^\([0-9]+\)\s+/i;
T 2852     if (set_title && count && document.title)    
15a9d1 2853       {
T 2854       var doc_title = String(document.title);
2855
2856       if (count && doc_title.match(reg))
2857         document.title = doc_title.replace(reg, '('+count+') ');
2858       else if (count)
2859         document.title = '('+count+') '+doc_title;
2860       else
2861         document.title = doc_title.replace(reg, '');
4e17e6 2862       }
01c86f 2863     // remove unread count from window title
T 2864     else if (document.title)
2865       {
2866       document.title = document.title.replace(reg, '');
2867       }
4e17e6 2868     };
T 2869
2870
2871   // add row to contacts list
2872   this.add_contact_row = function(cid, cols)
2873     {
2874     if (!this.gui_objects.contactslist || !this.gui_objects.contactslist.tBodies[0])
2875       return false;
2876     
2877     var tbody = this.gui_objects.contactslist.tBodies[0];
2878     var rowcount = tbody.rows.length;
2879     var even = rowcount%2;
2880     
2881     var row = document.createElement('TR');
2882     row.id = 'rcmrow'+cid;
2883     row.className = 'contact '+(even ? 'even' : 'odd');
2884     
2885     if (this.in_selection(cid))
2886       row.className += ' selected';
2887
2888     // add each submitted col
2889     for (var c in cols)
2890       {
2891       col = document.createElement('TD');
2892       col.className = String(c).toLowerCase();
2893       col.innerHTML = cols[c];
2894       row.appendChild(col);
2895       }
2896     
2897     tbody.appendChild(row);
2898     this.init_table_row(row, 'contact_rows');
2899     };
2900
2901
2902
2903   /********************************************************/
2904   /*********          drag & drop methods         *********/
2905   /********************************************************/
2906
2907
2908   this.drag_mouse_move = function(e)
2909     {
2910     if (this.drag_start)
2911       {
2912       if (!this.draglayer)
2913         this.draglayer = new rcube_layer('rcmdraglayer', {x:0, y:0, width:300, vis:0, zindex:2000});
2914       
2915       // get subjects of selectedd messages
2916       var names = '';
2917       var c, subject, obj;
2918       for(var n=0; n<this.selection.length; n++)
2919         {
2920         if (n>12)  // only show 12 lines
2921           {
2922           names += '...';
2923           break;
2924           }
2925
2926         if (this.message_rows[this.selection[n]].obj)
2927           {
2928           obj = this.message_rows[this.selection[n]].obj;
2929           subject = '';
2930
2931           for(c=0; c<obj.childNodes.length; c++)
2932             if (!subject && obj.childNodes[c].nodeName=='TD' && obj.childNodes[c].firstChild && obj.childNodes[c].firstChild.nodeType==3)
2933               {
2934               subject = obj.childNodes[c].firstChild.data;
2935               names += (subject.length > 50 ? subject.substring(0, 50)+'...' : subject) + '<br />';
2936               }
2937           }
2938         }
2939         
2940       this.draglayer.write(names);
2941       this.draglayer.show(1);
2942       }
2943
2944     var pos = this.get_mouse_pos(e);
2945     this.draglayer.move(pos.x+20, pos.y-5);
2946     
2947     this.drag_start = false;
2948     this.drag_active = true;
2949     
2950     return false;
2951     };
2952
2953
2954   this.drag_mouse_up = function()
2955     {
2956     document.onmousemove = null;
2957     
2958     if (this.draglayer && this.draglayer.visible)
2959       this.draglayer.show(0);
2960       
2961     this.drag_active = false;
2962     
2963     return false;
2964     };
2965
2966
2967
2968   /********************************************************/
2969   /*********        remote request methods        *********/
2970   /********************************************************/
2971
2972
ecf759 2973   this.http_sockets = new Array();
T 2974   
2975   // find a non-busy socket or create a new one
2976   this.get_request_obj = function()
4e17e6 2977     {
ecf759 2978     for (var n=0; n<this.http_sockets.length; n++)
4e17e6 2979       {
ecf759 2980       if (!this.http_sockets[n].busy)
T 2981         return this.http_sockets[n];
4e17e6 2982       }
ecf759 2983     
T 2984     // create a new XMLHTTP object
2985     var i = this.http_sockets.length;
2986     this.http_sockets[i] = new rcube_http_request();
4e17e6 2987
ecf759 2988     return this.http_sockets[i];
T 2989     };
2990   
2991
2992   // send a http request to the server
2993   this.http_request = function(action, querystring, lock)
2994     {
2995     var request_obj = this.get_request_obj();
4e17e6 2996     querystring += '&_remote=1';
T 2997     
2998     // add timestamp to request url to avoid cacheing problems in Safari
2999     if (bw.safari)
3000       querystring += '&_ts='+(new Date().getTime());
3001
3002     // send request
ecf759 3003     if (request_obj)
4e17e6 3004       {
T 3005       // prompt('request', this.env.comm_path+'&_action='+escape(action)+'&'+querystring);
3006       console('HTTP request: '+this.env.comm_path+'&_action='+escape(action)+'&'+querystring);
ecf759 3007
T 3008       if (lock)
3009         this.set_busy(true);
3010
3011       request_obj.__lock = lock ? true : false;
3012       request_obj.__action = action;
3013       request_obj.onerror = function(o){ rcube_webmail_client.http_error(o); };
3014       request_obj.oncomplete = function(o){ rcube_webmail_client.http_response(o); };
3015       request_obj.GET(this.env.comm_path+'&_action='+escape(action)+'&'+querystring);
4e17e6 3016       }
T 3017     };
3018
3019
ecf759 3020   // handle HTTP response
T 3021   this.http_response = function(request_obj)
4e17e6 3022     {
ecf759 3023     var ctype = request_obj.get_header('Content-Type');
e1cf7c 3024     if (ctype){
ecf759 3025       ctype = String(ctype).toLowerCase();
e1cf7c 3026       var ctype_array=ctype.split(";");
S 3027       ctype = ctype_array[0];
3028     }
4e17e6 3029
ecf759 3030     if (request_obj.__lock)
4e17e6 3031       this.set_busy(false);
T 3032
5e3512 3033   console(request_obj.get_text());
4e17e6 3034
ecf759 3035     // if we get javascript code from server -> execute it
c03095 3036     if (request_obj.get_text() && (ctype=='text/javascript' || ctype=='application/x-javascript'))
T 3037       eval(request_obj.get_text());
4e17e6 3038
ecf759 3039     // process the response data according to the sent action
T 3040     switch (request_obj.__action)
3041       {
3042       case 'delete':
3043       case 'moveto':
3044         if (this.env.action=='show')
3045           this.command('list');
3046         break;
15a9d1 3047
ecf759 3048       case 'list':
5e3512 3049         if (this.env.messagecount)
T 3050           this.enable_command('purge', (this.env.mailbox==this.env.trash_mailbox));
3051
15a9d1 3052       case 'expunge':
T 3053         this.enable_command('select-all', 'select-none', 'expunge', this.env.messagecount ? true : false);
5e3512 3054         break;      
4e17e6 3055       }
ecf759 3056
T 3057     request_obj.reset();
4e17e6 3058     };
ecf759 3059
T 3060
3061   // handle HTTP request errors
3062   this.http_error = function(request_obj)
3063     {
3064     alert('Error sending request: '+request_obj.url);
3065
3066     if (request_obj.__lock)
3067       this.set_busy(false);
3068
3069     request_obj.reset();
3070     request_obj.__lock = false;
3071     };
3072
3073
3074   // use an image to send a keep-alive siganl to the server
3075   this.send_keep_alive = function()
3076     {
3077     var d = new Date();
3078     this.http_request('keep-alive', '_t='+d.getTime());
3079     };
15a9d1 3080
ecf759 3081     
15a9d1 3082   // send periodic request to check for recent messages
T 3083   this.check_for_recent = function()
3084     {
3085     var d = new Date();
3086     this.http_request('check-recent', '_t='+d.getTime());
3087     };
4e17e6 3088
T 3089
3090   /********************************************************/
3091   /*********            helper methods            *********/
3092   /********************************************************/
3093   
3094   // check if we're in show mode or if we have a unique selection
3095   // and return the message uid
3096   this.get_single_uid = function()
3097     {
3098     return this.env.uid ? this.env.uid : (this.selection.length==1 ? this.selection[0] : null);
3099     };
3100
3101   // same as above but for contacts
3102   this.get_single_cid = function()
3103     {
3104     return this.env.cid ? this.env.cid : (this.selection.length==1 ? this.selection[0] : null);
3105     };
3106
3107
8c2e58 3108 /* deprecated methods
T 3109
4e17e6 3110   // check if Shift-key is pressed on event
T 3111   this.check_shiftkey = function(e)
3112     {
3113     if(!e && window.event)
3114       e = window.event;
3115
3116     if(bw.linux && bw.ns4 && e.modifiers)
3117       return true;
3118     else if((bw.ns4 && e.modifiers & Event.SHIFT_MASK) || (e && e.shiftKey))
3119       return true;
3120     else
3121       return false;
3122     }
3123
1cded8 3124   // check if Shift-key is pressed on event
T 3125   this.check_ctrlkey = function(e)
3126     {
3127     if(!e && window.event)
3128       e = window.event;
3129
3130     if(bw.linux && bw.ns4 && e.modifiers)
3131       return true;
3132    else if (bw.mac)
3133        return this.check_shiftkey(e);
3134     else if((bw.ns4 && e.modifiers & Event.CTRL_MASK) || (e && e.ctrlKey))
3135       return true;
3136     else
3137       return false;
3138     }
8c2e58 3139 */
4e17e6 3140
8c2e58 3141   // returns modifier key (constants defined at top of file)
b11a00 3142   this.get_modifier = function(e)
T 3143     {
3144     var opcode = 0;
8c2e58 3145     e = e || window.event;
T 3146
3147     if (bw.mac && e)
3148       {
3149       opcode += (e.metaKey && CONTROL_KEY) + (e.shiftKey && SHIFT_KEY);
3150       return opcode;    
3151       }
3152     if (e)
3153       {
b11a00 3154       opcode += (e.ctrlKey && CONTROL_KEY) + (e.shiftKey && SHIFT_KEY);
8c2e58 3155       return opcode;
T 3156       }
b11a00 3157     if (e.cancelBubble)
8c2e58 3158       {
b11a00 3159       e.cancelBubble = true;
T 3160       e.returnValue = false;
8c2e58 3161       }
b11a00 3162     else if (e.preventDefault)
T 3163       e.preventDefault();
3164   }
3165
3166
4e17e6 3167   this.get_mouse_pos = function(e)
T 3168     {
3169     if(!e) e = window.event;
3170     var mX = (e.pageX) ? e.pageX : e.clientX;
3171     var mY = (e.pageY) ? e.pageY : e.clientY;
3172
3173     if(document.body && document.all)
3174       {
3175       mX += document.body.scrollLeft;
3176       mY += document.body.scrollTop;
3177       }
3178
3179     return { x:mX, y:mY };
3180     };
3181     
3182   
3183   this.get_caret_pos = function(obj)
3184     {
3185     if (typeof(obj.selectionEnd)!='undefined')
3186       return obj.selectionEnd;
3187
3188     else if (document.selection && document.selection.createRange)
3189       {
3190       var range = document.selection.createRange();
3191       if (range.parentElement()!=obj)
3192         return 0;
3193
3194       var gm = range.duplicate();
3195       if (obj.tagName=='TEXTAREA')
3196         gm.moveToElementText(obj);
3197       else
3198         gm.expand('textedit');
3199       
3200       gm.setEndPoint('EndToStart', range);
3201       var p = gm.text.length;
3202
3203       return p<=obj.value.length ? p : -1;
3204       }
3205
3206     else
3207       return obj.value.length;
3208     };
3209
3210
3211   this.set_caret2start = function(obj)
3212     {
3213     if (obj.createTextRange)
3214       {
3215       var range = obj.createTextRange();
3216       range.collapse(true);
3217       range.select();
3218       }
3219     else if (obj.setSelectionRange)
3220       obj.setSelectionRange(0,0);
3221
3222     obj.focus();
3223     };
3224
3225
3226   // set all fields of a form disabled
3227   this.lock_form = function(form, lock)
3228     {
3229     if (!form || !form.elements)
3230       return;
3231     
3232     var type;
3233     for (var n=0; n<form.elements.length; n++)
3234       {
3235       type = form.elements[n];
3236       if (type=='hidden')
3237         continue;
3238         
3239       form.elements[n].disabled = lock;
3240       }
3241     };
3242     
3243   }  // end object rcube_webmail
3244
3245
3246
ecf759 3247 // class for HTTP requests
T 3248 function rcube_http_request()
3249   {
3250   this.url = '';
3251   this.busy = false;
3252   this.xmlhttp = null;
3253
3254
3255   // reset object properties
3256   this.reset = function()
3257     {
3258     // set unassigned event handlers
3259     this.onloading = function(){ };
3260     this.onloaded = function(){ };
3261     this.oninteractive = function(){ };
3262     this.oncomplete = function(){ };
3263     this.onabort = function(){ };
3264     this.onerror = function(){ };
3265     
3266     this.url = '';
3267     this.busy = false;
3268     this.xmlhttp = null;
3269     }
3270
3271
3272   // create HTMLHTTP object
3273   this.build = function()
3274     {
3275     if (window.XMLHttpRequest)
3276       this.xmlhttp = new XMLHttpRequest();
3277     else if (window.ActiveXObject)
3278       this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
3279     else
3280       {
3281       
3282       }
3283     }
3284
3285   // sedn GET request
3286   this.GET = function(url)
3287     {
3288     this.build();
3289
3290     if (!this.xmlhttp)
3291       {
3292       this.onerror(this);
3293       return false;
3294       }
3295
3296     var ref = this;
3297     this.url = url;
3298     this.busy = true;
3299
3300     this.xmlhttp.onreadystatechange = function(){ ref.xmlhttp_onreadystatechange(); };
3301     this.xmlhttp.open('GET', url);
3302     this.xmlhttp.send(null);
3303     };
3304
3305
3306   this.POST = function(url, a_param)
3307     {
3308     // not implemented yet
3309     };
3310
3311
3312   // handle onreadystatechange event
3313   this.xmlhttp_onreadystatechange = function()
3314     {
3315     if(this.xmlhttp.readyState == 1)
3316       this.onloading(this);
3317
3318     else if(this.xmlhttp.readyState == 2)
3319       this.onloaded(this);
3320
3321     else if(this.xmlhttp.readyState == 3)
3322       this.oninteractive(this);
3323
3324     else if(this.xmlhttp.readyState == 4)
3325       {
3326       if(this.xmlhttp.status == 0)
3327         this.onabort(this);
3328       else if(this.xmlhttp.status == 200)
3329         this.oncomplete(this);
3330       else
3331         this.onerror(this);
3332         
3333       this.busy = false;
3334       }
3335     }
3336
3337   // getter method for HTTP headers
3338   this.get_header = function(name)
3339     {
3340     return this.xmlhttp.getResponseHeader(name);
3341     };
3342
c03095 3343   this.get_text = function()
T 3344     {
3345     return this.xmlhttp.responseText;
3346     };
3347
3348   this.get_xml = function()
3349     {
3350     return this.xmlhttp.responseXML;
3351     };
ecf759 3352
T 3353   this.reset();
3354   
3355   }  // end class rcube_http_request
3356
3357
4e17e6 3358
T 3359 function console(str)
3360   {
3361   if (document.debugform && document.debugform.console)
3362     document.debugform.console.value += str+'\n--------------------------------------\n';
3363   }
3364
3365
3366 // set onload handler
3367 window.onload = function(e)
3368   {
3369   if (window.rcube_webmail_client)
3370     rcube_webmail_client.init();
3371   };