]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Emails/javascript/ajax.js
Release 6.4.0
[Github/sugarcrm.git] / modules / Emails / javascript / ajax.js
1 /*********************************************************************************
2  * SugarCRM Community Edition is a customer relationship management program developed by
3  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
4  * 
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Affero General Public License version 3 as published by the
7  * Free Software Foundation with the addition of the following permission added
8  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
9  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
10  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
11  * 
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
15  * details.
16  * 
17  * You should have received a copy of the GNU Affero General Public License along with
18  * this program; if not, see http://www.gnu.org/licenses or write to the Free
19  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301 USA.
21  * 
22  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
23  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
24  * 
25  * The interactive user interfaces in modified source and object code versions
26  * of this program must display Appropriate Legal Notices, as required under
27  * Section 5 of the GNU Affero General Public License version 3.
28  * 
29  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
30  * these Appropriate Legal Notices must retain the display of the "Powered by
31  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
32  * technical reasons, the Appropriate Legal Notices must display the words
33  * "Powered by SugarCRM".
34  ********************************************************************************/
35
36
37 if (typeof console == "undefined")
38         console = { log: function(o) {alert(o)} };
39
40 var AjaxObject = {
41         ret : '',
42         currentRequestObject : null,
43         //timeout : 30000, // 30 second timeout default
44         timeout : 9999999999, // 30 second timeout default
45         forceAbort : false,
46         trail : new Array(),
47
48         /**
49          */
50         _reset : function() {
51                 this.timeout = 30000;
52                 this.forceAbort = false;
53         },
54
55         folderRenameCleanup : function() {
56                 SUGAR.email2.folders.setSugarFolders();
57         },
58
59         fullSyncCleanup : function(o) {
60                 this.folders.checkMailCleanup(o);
61                 SUGAR.email2.settings.settingsDialog.hide();
62         },
63
64         /**
65          */
66         composeCache : function(o) {
67                 var idx = SUGAR.email2.composeLayout.currentInstanceId; // post instance increment
68                 // get email templates and user signatures
69                 var ret = YAHOO.lang.JSON.parse(o.responseText);
70
71                 SUGAR.email2.composeLayout.emailTemplates = ret.emailTemplates;
72                 SUGAR.email2.composeLayout.signatures = ret.signatures;
73                 SUGAR.email2.composeLayout.fromAccounts = ret.fromAccounts;
74
75                 SUGAR.email2.composeLayout.setComposeOptions(idx);
76
77                 //Set the error array so we can notify the user when they try to hit send if any errors
78                 //are present.  We will also notify them now (after hitting compose button).
79                 SUGAR.email2.composeLayout.outboundAccountErrors = ret.errorArray;
80
81
82                 //if error element is returning an array, then check the length to make sure we have error messages
83                 if (typeof(ret.errorArray)=='object' && ret.errorArray instanceof Array && ret.errorArray.length > 0){
84                         //add error messages for display
85                         for(i in ret.errorArray)
86                                 SUGAR.showMessageBox(app_strings.LBL_EMAIL_ERROR_DESC, ret.errorArray[i], 'alert');
87                 }else if (typeof(ret.errorArray)=='object' && ret.errorArray!=null && ret.errorArray!='' ) {
88                         //if error element is returning an object, and the object value is not empty or null, then display error message
89                         for(i in ret.errorArray)
90                                 SUGAR.showMessageBox(app_strings.LBL_EMAIL_ERROR_DESC, ret.errorArray[i], 'alert');
91                 }
92
93                 //YUI bug with IE6 - Wont restore visibility property for nested select elements.
94                 if(SUGAR.isIE) {
95                         var overlayPanel = YAHOO.SUGAR.MessageBox.panel;
96                         if(overlayPanel) {
97                           overlayPanel.subscribe('hide',function() { YAHOO.util.Dom.setStyle('addressFrom' + idx,'visibility','');});
98                         }
99                 }
100         },
101
102
103         handleDeleteSignature : function(o) {
104                 SUGAR.hideMessageBox();
105         var ret = YAHOO.lang.JSON.parse(o.responseText);
106                 SUGAR.email2.composeLayout.signatures = ret.signatures;
107         var field = document.getElementById('signature_id');
108                 SUGAR.email2.util.emptySelectOptions(field);
109
110                 for(var i in ret.signatures) { // iterate through assoc array
111                         var opt = new Option(ret.signatures[i], i);
112                         field.options.add(opt);
113                 }
114                 setSigEditButtonVisibility();
115         },
116
117         /**
118          */
119         handleDeleteReturn : function(o) {
120                 // force refresh ListView
121                 SUGAR.hideMessageBox();
122                 if(document.getElementById('focusEmailMbox')) {
123                         YAHOO.namespace('frameFolders').selectednode = SUGAR.email2.folders.getNodeFromMboxPath(document.getElementById('focusEmailMbox').innerHTML);
124                 }
125
126                 // need to display success message before calling next async call?
127                 document.getElementById(this.target).innerHTML = o.responseText;
128         },
129
130         /**
131          */
132     handleFailure : function(o) {
133                 // Failure handler
134                 SUGAR.showMessageBox('Exception occurred...', o.statusText, 'alert');
135                 if(document.getElementById('saveButton')) {
136                         document.getElementById('saveButton').disabled = false;
137                 }
138         },
139
140         handleReplyForward : function(o) {
141                 var a = YAHOO.lang.JSON.parse(o.responseText);
142                 globalA = a;
143                 var idx = SUGAR.email2.composeLayout.currentInstanceId;
144
145                 document.getElementById('email_id' + idx).value = a.uid;
146                 document.getElementById('emailSubject' + idx).value = a.name;
147                 document.getElementById('addressTO' + idx).value = a.from;
148                 document.getElementById('uid' + idx).value = a.uid;
149                 if(a.cc) {
150                         document.getElementById('addressCC' + idx).value = a.cc;
151                         SE.composeLayout.showHiddenAddress('cc', idx);
152                 }
153
154                 if(a.type) {
155                         document.getElementById('type' + idx).value = a.type;
156                 }
157
158                 // apply attachment values
159                 SUGAR.email2.composeLayout.loadAttachments(a.attachments);
160
161                 setTimeout("callbackReplyForward.finish(globalA);", 500);
162         },
163
164         handleReplyForwardForDraft : function(o) {
165                 var a = YAHOO.lang.JSON.parse(o.responseText);
166                 globalA = a;
167                 var idx = SUGAR.email2.composeLayout.currentInstanceId;
168
169                 document.getElementById('email_id' + idx).value = a.uid;
170                 document.getElementById('emailSubject' + idx).value = a.name;
171                 document.getElementById('addressTO' + idx).value = a.to;
172
173                 if(a.cc) {
174                         document.getElementById('addressCC' + idx).value = a.cc;
175                         SUGAR.email2.composeLayout.showHiddenAddress('cc',idx);
176                 }
177
178                 if(a.bcc) {
179                         document.getElementById('addressBCC' + idx).value = a.bcc;
180                         SUGAR.email2.composeLayout.showHiddenAddress('bcc',idx);
181                 }
182
183
184                 if(a.type) {
185                         document.getElementById('type' + idx).value = a.type;
186                 }
187
188
189                 // apply attachment values
190                 SUGAR.email2.composeLayout.loadAttachments(a.attachments);
191
192                 setTimeout("callbackReplyForward.finish(globalA,0,1);", 500);
193         },
194
195         /**
196          */
197         handleSuccess : function(o) {
198                 document.getElementById(this.target).innerHTML = o.responseText;
199                 SUGAR.hideMessageBox();
200         },
201
202         /**
203          */
204         ieDeleteSuccess : function(o) {
205                 SUGAR.hideMessageBox();
206                 SUGAR.email2.accounts.refreshInboundAccountTable();
207                 alert(app_strings.LBL_EMAIL_IE_DELETE_SUCCESSFUL);
208                 SUGAR.email2.accounts.rebuildFolderList();
209         },
210
211         /**
212          */
213         ieSaveSuccess : function(o) {
214                 document.getElementById('saveButton').disabled = false;
215                 var a = YAHOO.lang.JSON.parse(o.responseText);
216                 if (a) {
217                         if(a.error) {
218                                 SUGAR.showMessageBox(app_strings.LBL_EMAIL_ERROR_DESC, app_strings.LBL_EMAIL_ERROR_CHECK_IE_SETTINGS, 'alert');
219                                 SUGAR.email2.accounts.ieAccountError(SUGAR.email2.accounts.errorStyle);
220                         } else {
221                                 resp = YAHOO.lang.JSON.parse(o.responseText);
222                                 SUGAR.email2.accounts.refreshInboundAccountTable();
223                                 SUGAR.email2.accounts.refreshOuboundAccountTable();
224                                 SUGAR.email2.folders.startEmailCheckOneAccount(resp.id, true);
225                                 SUGAR.email2.accounts.inboundAccountEditDialog.hide();
226                         }
227                 } else {
228                      SUGAR.hideMessageBox();
229                      SUGAR.showMessageBox(app_strings.LBL_EMAIL_ERROR_DESC, app_strings.LBL_EMAIL_ERROR_SAVE_ACCOUNT, 'alert');
230                 }
231
232         },
233
234         /**
235          */
236         loadAttachments : function(o) {
237                 var result = YAHOO.lang.JSON.parse(o.responseText);
238
239                 SUGAR.email2.composeLayout.loadAttachments(result);
240         },
241
242         /**
243          */
244         loadSignature : function(o) {
245                 var ret = YAHOO.lang.JSON.parse(o.responseText);
246                 SUGAR.email2.signatures[ret.id] = ret.signature_html;
247                 SUGAR.email2.composeLayout.setSignature(SUGAR.email2.signatures.targetInstance);
248         },
249
250         /**
251          * Follow up to mark email read|unread|flagged
252          */
253         markEmailCleanup : function(o) {
254                 var ret = YAHOO.lang.JSON.parse(o.responseText);
255                 if (!ret['status']) {
256                 SUGAR.hideMessageBox();
257                         SUGAR.showMessageBox(app_strings.LBL_EMAIL_ERROR_DESC, ret['message'], 'alert');
258                 } else {
259                         SUGAR.email2.contextMenus.markEmailCleanup();
260                 } // else
261         },
262
263         /**
264          */
265         rebuildShowFolders : function(o) {
266                 var t = YAHOO.lang.JSON.parse(o.responseText);
267                 var show = document.getElementById('ieAccountListShow');
268
269                 SUGAR.email2.util.emptySelectOptions(show);
270
271                 for(i=0; i<t.length; i++) { // iterate through assoc array
272                         var opt = new Option(t[i].text, t[i].value, t[i].selected);
273                         opt.selected = t[i].selected;
274                         show.options.add(opt);
275                 }
276
277                 SUGAR.email2.accounts.renderTree();
278         },
279         /**
280          */
281         saveListViewSortOrderPart2 : function() {
282                 // create the JSON string the func expects
283                 focusFolderPath = '[ "Home", "' + ieName + '"';
284
285                 var f = new String(focusFolder);
286                 var fEx = f.split('.');
287
288                 for(i=0; i<fEx.length; i++) {
289                         focusFolderPath += ', "' + fEx[i] +'"'
290                 }
291
292                 focusFolderPath += ']';
293
294                 YAHOO.namespace('frameFolders').selectednode = SUGAR.email2.folders.getNodeFromMboxPath(focusFolderPath);
295                 SUGAR.email2.listView.populateListFrame(YAHOO.namespace('frameFolders').selectednode, ieId, 'true');
296         },
297
298         /**
299          *
300          */
301         sendEmailCleanUp : function(o) {
302         var ret;
303         SUGAR.hideMessageBox();
304
305         try {
306             ret = YAHOO.lang.JSON.parse(o.responseText);
307             SUGAR.email2.composeLayout.forceCloseCompose(ret.composeLayoutId);
308             //SUGAR.email2.addressBook.showContactMatches(ret.possibleMatches);
309         } catch(err) {
310             if (o.responseText) {
311                 SUGAR.showMessageBox(mod_strings.LBL_SEND_EMAIL_FAIL_TITLE, o.responseText, 'alert');
312                     }
313             // Else we have an error here.
314         }        
315
316                 if (typeof(SE.grid) != 'undefined')
317                         SE.listView.refreshGrid();
318                 //Disabled while address book is disabled
319
320                 //If this call back was initiated by quick compose from a Detail View page, refresh the
321                 //history subpanel.  If it was initiated by quickcreate from shortcut bar, then
322                 //close the shortcut bar menu
323                 if ( (typeof(action_sugar_grp1) != 'undefined')) {
324                         if(action_sugar_grp1 == 'DetailView') {
325                                 showSubPanel('history',null,true);
326                         } else if(action_sugar_grp1 == 'quickcreate') {
327                                 closeEmailOverlay();
328                         }
329                 }
330
331         },
332
333         ieSendSuccess : function(o) {
334                 SUGAR.hideMessageBox();
335                 SUGAR.showMessageBox(app_strings.LBL_EMAIL_TEST_OUTBOUND_SETTINGS_SENT, app_strings.LBL_EMAIL_TEST_NOTIFICATION_SENT, 'plain');
336         },
337
338         /**
339          */
340         settingsFolderRefresh : function(o) {
341                 //SUGAR.email2.accounts.rebuildFolderList(); // refresh frameFolder
342                 var ret = YAHOO.lang.JSON.parse(o.responseText);
343                 var user = document.getElementById('userFolders');
344
345                 SUGAR.email2.util.emptySelectOptions(user);
346
347                 for(i=0; i<ret.userFolders.length; i++) {
348                         var display = ret.userFolders[i].name;
349                         var value = ret.userFolders[i].id;
350                         var selected = (ret.userFolders[i].selected != "") ? true : false;
351                         var opt = new Option(display, value, selected);
352                         opt.selected = selected;
353                         user.options.add(opt);
354                 }
355         },
356
357         /**
358          */
359         startRequest : function(callback, args, forceAbort) {
360                 if(this.currentRequestObject != null) {
361                         if(this.forceAbort == true) {
362                                 YAHOO.util.Connect.abort(this.currentRequestObject, null, false);
363                         }
364                 }
365                 this.currentRequestObject = YAHOO.util.Connect.asyncRequest('POST', "./index.php", callback, args);
366                 this._reset();
367         },
368
369         requestInProgress : function() {
370                 return (YAHOO.util.Connect.isCallInProgress(this.currentRequestObject));
371         },
372
373         /**
374          */
375         updateFolderSubscriptions : function() {
376                 SUGAR.email2.folders.lazyLoadSettings(); // refresh view in Settings overlay
377                 SUGAR.email2.folders.setSugarFolders(1000);// refresh view in TreeView
378                 SUGAR.hideMessageBox();
379         },
380
381         /**
382          */
383         updateFrameFolder : function() {
384                 SUGAR.email2.folders.checkEmailAccounts();
385         },
386
387         /**
388          */
389         updateUserPrefs : function(o) {
390                 SUGAR.email2.userPrefs = YAHOO.lang.JSON.parse(o.responseText);
391                 SUGAR.email2.folders.startCheckTimer(); // starts the auto-check interval
392         },
393
394         /**
395          */
396         uploadAttachmentSuccessful : function(o) {
397                 // clear out field
398                 document.getElementById('email_attachment').value = '';
399                 
400                 var ret = YAHOO.lang.JSON.parse(o.responseText);
401                 var idx = SUGAR.email2.composeLayout.currentInstanceId;
402                 var overall = document.getElementById('addedFiles' + idx);
403                 var index = overall.childNodes.length;
404                 var out =
405                         "<div id='email_attachment_bucket" + idx + index + "'>" +
406                                 // remove button
407                                 "<img src='index.php?entryPoint=getImage&themeName=" + SUGAR.themes.theme_name + "&imageName=minus.gif' " +
408                                         "style='cursor:pointer' align='absmiddle' onclick='SUGAR.email2.composeLayout.deleteUploadAttachment(\"" +
409                                         idx + index + "\",\"" + ret.guid + ret.name + "\");'/>" +
410                                 // file icon
411                                 "<img src='index.php?entryPoint=getImage&themeName=" + SUGAR.themes.theme_name + "&imageName=attachment.gif' " +
412                                         "id='email_attachmentImage'" + idx + index + "align='absmiddle' />" +
413                                 // hidden id field
414                                 "<input type='hidden' value='" + ret.guid + ret.name + "' name='email_attachment" + index + "' id='email_attachment" + idx + index + "' />" +
415                                 // file name
416                                 ((ret.nameForDisplay != null) ? ret.nameForDisplay + "&nbsp;" : ret.name + "&nbsp;") +
417                                 "<br/>" +
418                         "</div>";
419                 overall.innerHTML += out;
420                 if(SUGAR.email2.util.isIe()) {
421                         document.getElementById('addedFiles' + idx).innerHTML = document.getElementById('addedFiles' + idx).innerHTML;
422                 }
423
424                 // hide popup
425                 SUGAR.email2.addFileDialog.hide();
426                 // focus attachments
427                 SUGAR.email2.composeLayout.showAttachmentPanel(idx);
428         }
429 };
430
431
432 ///////////////////////////////////////////////////////////////////////////
433 ////    PER MODULE CALLBACK OBJECTS
434 AjaxObject.accounts = {
435         saveOutboundCleanup : function(o) {
436                 SUGAR.email2.accounts.refreshOuboundAccountTable();
437                 SUGAR.email2.accounts.outboundDialog.hide();
438                 var id = o.responseText;
439                 SUGAR.email2.accounts.newAddedOutboundId = id;
440                 },
441         saveDefaultOutboundCleanup: function(o){
442
443         },
444         callbackEditOutbound : {
445                 success : function(o)
446                 {
447                         var ret = YAHOO.lang.JSON.parse(o.responseText);
448                         // show overlay
449                         SUGAR.email2.accounts.showAddSmtp();
450
451                         // fill values
452                         document.getElementById("mail_id").value = ret.id;
453                         document.getElementById("type").value = ret.type;
454                         document.getElementById("mail_sendtype").value = ret.mail_sendtype;
455                         document.getElementById("mail_name").value = ret.name;
456                         document.getElementById("mail_smtpserver").value = ret.mail_smtpserver;
457                         document.getElementById("outboundEmailForm").mail_smtptype.value = ret.mail_smtptype;
458                         document.getElementById("mail_smtpport").value = ret.mail_smtpport;
459                         document.getElementById("mail_smtpuser").value = ret.mail_smtpuser;
460                         document.getElementById("mail_smtpauth_req").checked = (ret.mail_smtpauth_req == 1) ? true : false;
461                         SUGAR.email2.accounts.smtp_authenticate_field_display();
462                         document.getElementById("mail_smtpssl").options[ret.mail_smtpssl].selected = true;
463
464             if(ret.type == 'system-override') {
465                              SUGAR.email2.accounts.toggleOutboundAccountDisabledFields(true);
466                              SUGAR.email2.accounts.changeEmailScreenDisplay(ret.mail_smtptype,true);
467             }
468             else {
469                              SUGAR.email2.accounts.toggleOutboundAccountDisabledFields(false);
470                              SUGAR.email2.accounts.changeEmailScreenDisplay(ret.mail_smtptype,false);
471             }
472             SUGAR.util.setEmailPasswordDisplay('mail_smtppass', ret.has_password);
473
474                 },
475                 failure : AjaxObject.handleFailure,
476                 timeout : AjaxObject.timeout,
477                 scope   : AjaxObject
478         },
479         callbackDeleteOutbound : {
480                 success : function(o) {
481                     var ret = YAHOO.lang.JSON.parse(o.responseText);
482                     if(ret.is_error)
483                     {
484                         if(confirm(ret.error_message))
485                 {
486                     SUGAR.showMessageBox(app_strings.LBL_EMAIL_IE_DELETE, app_strings.LBL_EMAIL_ONE_MOMENT);
487                     AjaxObject.startRequest(AjaxObject.accounts.callbackDeleteOutbound, urlStandard + "&emailUIAction=deleteOutbound&confirm=true&outbound_email=" + ret.outbound_email);
488                 }
489                 else
490                     SUGAR.hideMessageBox();
491                     }
492                     else
493                     {
494                              SUGAR.hideMessageBox();
495                              SUGAR.email2.accounts.refreshOuboundAccountTable();
496                     }
497                 },
498
499                 failure : AjaxObject.handleFailure,
500                 timeout : AjaxObject.timeout,
501                 scope   : AjaxObject
502         },
503
504         callbackCheckMailProgress : {
505            success : function(o) {
506                if (typeof(SUGAR.email2.accounts.totalMsgCount) == "undefined") {
507                    SUGAR.email2.accounts.totalMsgCount = -1;
508                }
509
510                //Check for server timeout / errors
511                var ret = YAHOO.lang.JSON.parse(o.responseText);
512                var done = false;
513
514                if (typeof(o.responseText) == 'undefined' || o.responseText == "" || ret == false) {
515                    SUGAR.hideMessageBox();
516                    SUGAR.showMessageBox(app_strings.LBL_EMAIL_ERROR_DESC, app_strings.LBL_EMAIL_ERROR_TIMEOUT, 'alert');
517                    SUGAR.email2.accounts.totalMsgCount = -1;
518                //SUGAR.email2.folders.rebuildFolders();
519                done = true;
520                }
521
522                var currIeId = ret['ieid'];
523
524
525                var serverCount = ret.count;
526
527                if (ret['status'] == 'done') {
528                    for(i=0; i < SUGAR.email2.accounts.ieIds.length; i++) {
529                        if (i == SUGAR.email2.accounts.ieIds.length - 1) {
530                            //We are all done
531                            done = true;
532                            break;
533                        } else if (SUGAR.email2.accounts.ieIds[i] == currIeId) {
534                            //Go to next account
535                            currIeId = SUGAR.email2.accounts.ieIds[i+1];
536                            ret.count = 0;
537                            SUGAR.email2.accounts.totalMsgCount = -1;
538                            break;
539                        }
540                    }
541                }
542                else if (ret.mbox && ret.totalcount && ret.count) {
543                    SUGAR.email2.accounts.totalMsgCount = ret.totalcount;
544                    if (ret.count >= ret.totalcount) {
545                        serverCount = 0;
546                    }
547                } else if (SUGAR.email2.accounts.totalMsgCount < 0 && ret.totalcount) {
548                    SUGAR.email2.accounts.totalMsgCount = ret.totalcount;
549                } else {
550                        SUGAR.hideMessageBox();
551                SUGAR.showMessageBox(app_strings.LBL_EMAIL_ERROR_DESC, app_strings.LBL_EMAIL_ERROR_TIMEOUT, 'alert');
552                SUGAR.email2.accounts.totalMsgCount = -1;
553                done = true;
554                    }
555
556                if (done) {
557                    SUGAR.email2.accounts.totalMsgCount = -1;
558                    SUGAR.hideMessageBox();
559                    SUGAR.email2.folders.rebuildFolders();
560                    SE.listView.refreshGrid();
561                } else if (SUGAR.email2.accounts.totalMsgCount < 0) {
562                YAHOO.SUGAR.MessageBox.updateProgress(0, mod_strings.LBL_CHECKING_ACCOUNT + ' '+ (i + 2) + ' '+ mod_strings.LBL_OF + ' ' + SUGAR.email2.accounts.ieIds.length);
563                AjaxObject.startRequest(AjaxObject.accounts.callbackCheckMailProgress, urlStandard +
564                                 '&emailUIAction=checkEmailProgress&ieId=' + currIeId + "&currentCount=0&synch=" + ret.synch);
565            } else {
566                YAHOO.SUGAR.MessageBox.updateProgress((ret.count / SUGAR.email2.accounts.totalMsgCount) * 100,
567                    app_strings.LBL_EMAIL_DOWNLOAD_STATUS.replace(/\[\[count\]\]/, ret.count).replace(/\[\[total\]\]/, SUGAR.email2.accounts.totalMsgCount));
568                    AjaxObject.startRequest(AjaxObject.accounts.callbackCheckMailProgress, urlStandard +
569                    '&emailUIAction=checkEmailProgress&ieId=' + currIeId + "&currentCount=" + serverCount +
570                    '&mbox=' + ret.mbox + '&synch=' + ret.synch + '&totalcount=' + SUGAR.email2.accounts.totalMsgCount);
571                }
572            },
573            failure : AjaxObject.handleFailure,
574        timeout : AjaxObject.timeout,
575        scope   : AjaxObject
576         }
577 };
578
579 ///////////////////////////////////////////////////////////////////////////////
580 ////    COMPOSE LAYOUT
581 AjaxObject.composeLayout = {
582         /**
583          * Populates the record id
584          */
585     saveDraftCleanup : function(o) {
586         var ret;
587         SUGAR.hideMessageBox();
588
589         try {
590             ret = YAHOO.lang.JSON.parse(o.responseText);
591             SUGAR.email2.composeLayout.forceCloseCompose(ret.composeLayoutId);
592         } catch(err) {
593             if (o.responseText) {
594                 SUGAR.showMessageBox(mod_strings.LBL_ERROR_SAVING_DRAFT, o.responseText, 'alert');
595             }
596         }
597     }
598 };
599
600 AjaxObject.composeLayout.callback = {
601         saveDraft : {
602                 success : AjaxObject.composeLayout.saveDraftCleanup,
603                 failure : AjaxObject.handleFailure,
604                 timeout : AjaxObject.timeout,
605                 scope   : AjaxObject
606         }
607 };
608
609 AjaxObject.detailView = {
610         /**
611          * Pops-up a printable view of an email
612          */
613         displayPrintable : function(o) {
614                 var ret = YAHOO.lang.JSON.parse(o.responseText);
615                 var displayTemplate = new YAHOO.SUGAR.Template(SUGAR.email2.templates['viewPrintable']);
616                 // 2 below must be in global context
617                 meta = ret.meta;
618                 meta['panelId'] = SUGAR.email2.util.getPanelId();
619                 email = ret.meta.email;
620                 if (typeof(email.cc) == 'undefined') {
621                   email.cc = "";
622                 }
623
624                 var out = displayTemplate.exec({
625                         'app_strings'   : app_strings,
626                         'theme'                 : theme,
627                         'idx'                   : 'Preview',
628                         'meta'                  : meta,
629                         'email'                 : meta.email
630                 });
631
632                 // open popup window
633                 var popup = window.open('modules/Emails/templates/_blank.html', 'printwin' ,
634                     'scrollbars=yes,menubar=no,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no');
635
636                 popup.document.write(out);
637                 popup.document.close();
638         },
639
640         /**
641          * Takes formatted response and creates a modal pop-over containing a title and content
642          */
643         displayView : function(o) {
644                 var SED = SUGAR.email2.detailView;
645                 var ret = YAHOO.lang.JSON.parse(o.responseText);
646
647                 if(!SED.viewDialog) {
648                         SED.viewDialog = new YAHOO.widget.Dialog("viewDialog", {
649                                 modal:true,
650                 visible:true,
651                 fixedcenter:true,
652                 constraintoviewport: true,
653                                 shadow  : true
654                         });
655             SED.viewDialog.renderEvent.subscribe(function() {
656                 var content = this.body.firstChild;
657                 var viewH = YAHOO.util.Dom.getViewportHeight();
658                 if (content) {
659                         this.body.style.overflow = "auto";
660                         this.body.style.width = "800px";
661                         this.body.style.height = (viewH - 75 > content.clientHeight ? (content.clientHeight) : (viewH - 75)) + "px";
662                 }
663             }, SED.viewDialog);
664                 } // end lazy load
665                 SED.viewDialog.setHeader(ret.title);
666                 SED.viewDialog.setBody(ret.html);
667                 SED.viewDialog.render();
668                 SED.viewDialog.show();
669         },
670
671         /**
672          * Generates a modal popup to populate with the contents of bean's full EditView
673          */
674         showQuickCreateForm : function(o) {
675                 var SED = SUGAR.email2.detailView;
676                 var ret = YAHOO.lang.JSON.parse(o.responseText);
677
678                 if(!SED.quickCreateDialog) {
679                         SED.quickCreateDialog = new YAHOO.widget.Dialog("quickCreate", {
680                                 modal:true,
681                                 visible:true,
682                 fixedcenter:true,
683                 constraintoviewport: true,
684                                 shadow  : true
685                         });
686
687             SED.quickCreateDialog.renderEvent.subscribe(function() {
688                 var viewH = YAHOO.util.Dom.getViewportHeight();
689                 var contH = 0;
690                 for (var i in this.body.childNodes) {
691                         if (this.body.childNodes[i].clientHeight) {
692                                 contH += this.body.childNodes[i].clientHeight;
693                         } else if (this.body.childNodes[i].offsetHeight) {
694                                 contH += this.body.childNodes[i].offsetHeight;
695                         } // if
696                 }
697                         this.body.style.width = "800px";
698                         this.body.style.height = (viewH - 75 > contH ? (contH + 10) : (viewH - 75)) + "px";
699                         this.body.style.overflow = "auto";
700             }, SED.quickCreateDialog);
701
702             SED.quickCreateDialog.hideEvent.subscribe(function(){
703                                 var qsFields = YAHOO.util.Dom.getElementsByClassName('.sqsEnabled', null, this.body);
704                                 /*for(var qsField in qsFields){
705                                         if (typeof QSFieldsArray[qsFields[qsField].id] != 'undefined')
706                                         Ext.getCmp('combobox_'+qsFields[qsField].id).destroy();
707                                 }*/
708                         });
709             SED.quickCreateDialog.setHeader(app_strings.LBL_EMAIL_QUICK_CREATE);
710                 } // end lazy load
711                 if (ret.html) {
712                         ret.html = ret.html.replace('<script type="text/javascript" src="include/SugarEmailAddress/SugarEmailAddress.js"></script>', "");
713                 }
714                 SED.quickCreateDialog.setBody(ret.html ? ret.html : "&nbsp;");
715                 SED.quickCreateDialog.render();
716                 SUGAR.util.evalScript(ret.html + '<script language="javascript">enableQS(true);</script>');
717
718                 SED.quickCreateDialog.ieId = ret.ieId;
719                 SED.quickCreateDialog.uid = ret.uid;
720         SED.quickCreateDialog.mbox = ret.mbox;
721         SED.quickCreateDialog.qcmodule = ret.module;
722
723         SED.quickCreateDialog.show();
724
725                 var editForm = document.getElementById('form_EmailQCView_' + ret.module);
726                 if (editForm) {
727                   editForm.module.value = 'Emails';
728                   var count = SUGAR.EmailAddressWidget.count[ret.module] ? SUGAR.EmailAddressWidget.count[ret.module] : 0;
729                   var tableId = YAHOO.util.Dom.getElementsByClassName('emailaddresses', 'table', editForm)[0].id;
730                   var instId = ret.module + count;
731                   SED.quickCreateEmailsToAdd = ret.emailAddress;
732                   SED.quickCreateEmailCallback = function(instId, tableId) {
733                           var eaw = SUGAR.EmailAddressWidget.instances[instId];
734                           if (typeof(eaw) == "undefined")
735                                   window.setTimeout("SUGAR.email2.detailView.quickCreateEmailCallback('"
736                                                 + instId + "','" + tableId + "');", 100);
737                           eaw.prefillEmailAddresses(tableId, SUGAR.email2.detailView.quickCreateEmailsToAdd);
738                   }
739                   window.setTimeout("SUGAR.email2.detailView.quickCreateEmailCallback('"
740                                         + instId + "','" + tableId + "');", 100);
741                 }
742         },
743
744         saveQuickCreateForm : function(o) {
745             SUGAR.hideMessageBox();
746                 SUGAR.email2.detailView.quickCreateDialog.hide();
747                 validate['EditView'] = [ ];
748         },
749
750         saveQuickCreateFormAndReply : function(o) {
751         SUGAR.hideMessageBox();
752         var ret = YAHOO.lang.JSON.parse(o.responseText);
753         SUGAR.email2.detailView.quickCreateDialog.hide();
754         var qcd = SUGAR.email2.detailView.quickCreateDialog;
755         var type = (qcd.qcmodule == 'Cases') ? 'replyCase' : 'reply';
756         if (ret) {
757             var emailID = ret.id;
758             SUGAR.email2.composeLayout.c0_replyForwardEmail(null, ret.id, 'sugar::Emails', type);
759         } else {
760             SUGAR.email2.composeLayout.c0_replyForwardEmail(qcd.ieId, qcd.uid, qcd.mbox, type);
761         }
762         //Cean the validate cache to prevent errors on the next call
763         validate['EditView'] = [ ];
764     },
765
766         saveQuickCreateFormAndAddToAddressBook : function(o) {
767            SUGAR.hideMessageBox();
768                 SUGAR.email2.detailView.quickCreateDialog.hide();
769                 SUGAR.email2.complexLayout.findPanel('contactsTab').show();
770                 validate['EditView'] = [ ];
771         },
772
773         handleAssignmentDialogAssignAction : function() {
774
775
776             var assign_user_id = window.document.forms['Distribute'].elements['assigned_user_id'].value;
777
778             var dist = 'direct';
779             var users = false;
780             var rules = false;
781             var get = "";
782             var found_teams = false;
783             var warning_message = mod_strings.LBL_WARN_NO_USERS;
784             if(!found_teams && assign_user_id == '' )
785             {
786                 alert(warning_message);
787                 return;
788             }
789
790             var emailUids = SUGAR.email2.listView.getUidsFromSelection();
791             var uids = "";
792             for(i=0; i<emailUids.length; i++) {
793                 if(uids != '') {
794                     uids += app_strings.LBL_EMAIL_DELIMITER;
795                 }
796                 uids += emailUids[i];
797             }
798
799             var row = SUGAR.email2.grid.getSelectedRows()[0];
800             var data = SUGAR.email2.grid.getRecord(row).getData();
801             var ieid = data.ieId;
802             var mbox = data.mbox;
803             AjaxObject.startRequest(callbackAssignmentAction, urlStandard + '&emailUIAction=' + "doAssignmentAssign&uids=" + uids + "&ieId=" + ieid + "&folder=" + mbox + "&distribute_method=" + dist + "&users=" +assign_user_id + get);
804             SUGAR.email2.contextMenus.assignToDialogue.hide();
805             SUGAR.showMessageBox('Assignment', app_strings.LBL_EMAIL_ONE_MOMENT);
806
807         },
808
809         handleAssignmentDialogDeleteAction : function() {
810                 // TO pass list of UIDS/emailIds
811                 var uids = SUGAR.email2.listView.getUidsFromSelection();
812                 var row = SUGAR.email2.grid.getSelections()[0];
813                 var ieid = row.data.ieId;
814             var mbox = row.data.mbox;
815         AjaxObject.startRequest(callbackAssignmentAction, urlStandard + '&emailUIAction=' + "doAssignmentDelete&uids=" + uids + "&ieId=" + ieId + "&folder=" + mbox);
816         SUGAR.email2.contextMenus.assignmentDialog.hide();
817                 SUGAR.showMessageBox(app_strings.LBL_EMAIL_PERFORMING_TASK, app_strings.LBL_EMAIL_ONE_MOMENT);
818
819                 // AJAX Call
820
821         },
822
823         showEmailDetailView : function(o) {
824         SUGAR.hideMessageBox();
825         var SED = SUGAR.email2.detailView;
826                 var ret = YAHOO.lang.JSON.parse(o.responseText);
827
828                 if(!SED.quickCreateDialog) {
829                         SED.quickCreateDialog = new YAHOO.widget.Dialog("emailDetailDialog", {
830                                 modal:true,
831                                 visible:true,
832                 //fixedcenter:true,
833                 constraintoviewport: true,
834                 draggable: true,
835                                 autofillheight: "body",
836                                 shadow  : true
837                         });
838                         SED.quickCreateDialog.renderEvent.subscribe(function() {
839                 var viewHeight = YAHOO.util.Dom.getViewportHeight();
840                 var contH = 0;
841                 for (var i in this.body.childNodes) {
842                         if (this.body.childNodes[i].offsetHeight)
843                                 contH += this.body.childNodes[i].offsetHeight;
844                 }
845                         this.body.style.overflow = "auto";
846                         this.body.style.width = "800px";
847                         this.body.style.height = (viewHeight - 75 > contH ? (contH + 10) : (viewHeight - 75)) + "px";
848                         this.center();
849             }, SED.quickCreateDialog);
850                 }
851                 SED.quickCreateDialog.setHeader(app_strings.LBL_EMAIL_RECORD);
852                 SED.quickCreateDialog.setBody(ret.html);
853                 SED.quickCreateDialog.render();
854         SUGAR.util.evalScript(ret.html);
855         SED.quickCreateDialog.show();
856         },
857
858         showAssignmentDialogWithData : function(o) {
859         var SEC = SUGAR.email2.contextMenus;
860                 SUGAR.hideMessageBox();
861         var ret = YAHOO.lang.JSON.parse(o.responseText);
862         if (!SEC.assignmentDialog) {
863                 SEC.assignmentDialog = new YAHOO.widget.Dialog("assignmentDialog", {
864                         visible:false,
865                 fixedcenter:true,
866                 constraintoviewport: true,
867                         modal   : true
868                 });
869                 SEC.assignmentDialog.setBody("");
870                 SEC.assignmentDialog.setHeader(app_strings.LBL_EMAIL_ASSIGNMENT);
871                 SEC.assignmentDialog.renderEvent.subscribe(function() {
872                 var iev = YAHOO.util.Dom.get("Distribute");
873                 if (iev) {
874                         this.body.style.width = "700px";
875                 }
876             },  SEC.assignmentDialog);
877                 SEC.assignmentDialog.render();
878         }
879         SEC.assignmentDialog.setBody(ret);
880         SEC.assignmentDialog.render();
881         validate = [];
882         SEC.assignmentDialog.show();
883         SUGAR.util.evalScript(ret);
884         },
885
886         showImportForm : function(o) {
887                 var SED = SUGAR.email2.detailView;
888                 var ret = YAHOO.lang.JSON.parse(o.responseText);
889         
890         document.getElementById('quickCreateContent').innerHTML = "";
891         SUGAR.hideMessageBox();
892         if (!ret) {
893             return false;
894         }
895
896         if(!SED.importDialog) {
897             SED.importDialog = new YAHOO.widget.Dialog("importDialog", {
898                 modal:true,
899                 visible:false,
900                 fixedcenter:true,
901                 constraintoviewport: true,
902                 buttons : [{
903                         text: app_strings.LBL_EMAIL_ARCHIVE_TO_SUGAR, isDefault: true, handler: function(){
904                                 AjaxObject.detailView.getImportAction(SED.importDialog.ret); }
905                 }]//,
906                 //scroll : true
907             });
908             SED.importDialog.setHeader(app_strings.LBL_EMAIL_IMPORT_SETTINGS);
909             SED.importDialog.setBody("");
910             SED.importDialog.hideEvent.subscribe(function(){
911                 for(var i in QSFieldsArray) {
912                         if (QSFieldsArray[i] != null && typeof(QSFieldsArray[i]) == "object") {
913                                 QSFieldsArray[i].destroy();
914                                 delete QSFieldsArray[i];
915                         }
916                         if (QSProcessedFieldsArray[i]) {
917                                 QSProcessedFieldsArray[i] = false;
918                         } // if
919                                 }
920             });
921             SED.importDialog.renderEvent.subscribe(function() {
922                 var iev = YAHOO.util.Dom.get("ImportEditView");
923                 if (iev) {
924                         //this.body.style.height = (iev.clientHeight + 10) + "px";
925                         this.body.style.width = "600px";
926                 }
927             }, SED.importDialog);
928             SED.importDialog.render();
929         } // end lazy load
930         SED.importDialog.setBody(ret.html);
931         SED.importDialog.ret = ret;
932         SUGAR.util.evalScript(ret.html);
933         SED.importDialog.render();
934         validate = [];
935         SED.importDialog.show();
936         SED.importDialog.focusFirstButton();
937     },
938     getImportAction : function(ret) {
939         if (!check_form('ImportEditView')) return false;
940         if (!SUGAR.collection.prototype.validateTemSet('ImportEditView', 'team_name')) {
941                 alert(mod_strings.LBL_EMAILS_NO_PRIMARY_TEAM_SPECIFIED);
942                 return false;
943         } // if
944                 var get = "";
945         var editView = document.getElementById('ImportEditView');
946         if (editView.assigned_user_id != null) {
947             get = get + "&user_id=" + editView.assigned_user_id.value
948             //var user_id = editView.assigned_user_id.value;
949         }
950         var parent_id = editView.parent_id.value;
951         var parent_type = editView.parent_type.value;
952         var row = SUGAR.email2.grid.getSelectedRows()[0];
953         row = SUGAR.email2.grid.getRecord(row);
954         var data = row.getData();
955         var ieId = data.ieId;
956         var mbox = data.mbox;
957         var serverDelete = editView.serverDelete.checked;
958         var emailUids = SUGAR.email2.listView.getUidsFromSelection();
959         var uids = "";
960         for(i=0; i<emailUids.length; i++) {
961             if(uids != '') {
962                 uids += app_strings.LBL_EMAIL_DELIMITER;
963             }
964             uids += emailUids[i];
965         }
966
967         var action = 'importEmail&uid=';
968         if (ret.move) {
969             action = 'moveEmails';
970             action = action + '&sourceFolder=' + ret['srcFolder'];
971             action = action + '&sourceIeId=' + ret['srcIeId'];
972             action = action + '&destinationFolder=' + ret['dstFolder'];
973             action = action + '&destinationIeId=' + ret['dstIeId'];
974             action = action + '&emailUids=';
975         }
976         if (action.search(/importEmail/) != -1) {
977             SUGAR.showMessageBox(app_strings.LBL_EMAIL_IMPORTING_EMAIL, app_strings.LBL_EMAIL_ONE_MOMENT);
978         } else {
979             SUGAR.showMessageBox("Moving Email(s)", app_strings.LBL_EMAIL_ONE_MOMENT);
980         }
981
982         AjaxObject.startRequest(callbackStatusForImport, urlStandard + '&emailUIAction=' + action + uids + "&ieId=" + ieId + "&mbox=" + mbox +
983         get + "&parent_id=" + parent_id + "&parent_type=" + parent_type + '&delete=' + serverDelete);
984         SUGAR.email2.detailView.importDialog.hide();
985         document.getElementById('importDialogContent').innerHTML = "";
986
987     },
988     showRelateForm : function(o) {
989         var SED = SUGAR.email2.detailView;
990         var ret = YAHOO.lang.JSON.parse(o.responseText);
991         document.getElementById('quickCreateContent').innerHTML = "";
992         SUGAR.hideMessageBox();
993         if (!ret) {
994             return false;
995         }
996         dialog_loaded = true;
997
998         if(!SED.relateDialog) {
999             SED.relateDialog = new YAHOO.widget.Dialog('relateDialog', {
1000                                 modal:true,
1001                                 visible:true,
1002                 fixedcenter:true,
1003                 width: '800px',
1004                 constraintoviewport: true,
1005                                 buttons : [{
1006                         text: app_strings.LBL_EMAIL_RELATE_TO, isDefault: true, handler: function(){
1007                                         if (!check_form('RelateEditView')) return false;
1008                                         var get = "";
1009                         var editView = document.getElementById('RelateEditView');
1010                         var parent_id = editView.parent_id.value;
1011                         var parent_type = editView.parent_type.value;
1012                         var row = SUGAR.email2.grid.getSelectedRows()[0];
1013                         row  = SUGAR.email2.grid.getRecord(row);
1014                         var ieId = row.getData().ieId;
1015                         var mbox = row.getData().mbox;
1016                         var emailUids = SUGAR.email2.listView.getUidsFromSelection();
1017                         var uids = "";
1018                         for(i=0; i<emailUids.length; i++) {
1019                             if(uids != '') {
1020                                 uids += app_strings.LBL_EMAIL_DELIMITER;
1021                             }
1022                             uids += emailUids[i];
1023                         }
1024                         SUGAR.showMessageBox(app_strings.LBL_EMAIL_PERFORMING_TASK, app_strings.LBL_EMAIL_ONE_MOMENT);
1025                         AjaxObject.startRequest(callbackStatusForImport, urlStandard + '&emailUIAction=relateEmails&uid=' + uids
1026                             + "&ieId=" + ieId + "&mbox=" + mbox + "&parent_id=" + parent_id + "&parent_type=" + parent_type);
1027                         SED.relateDialog.hide();
1028                         document.getElementById('relateDialogContent').innerHTML = "";
1029                         }
1030                 }]
1031                         });
1032
1033             SED.relateDialog.hideEvent.subscribe(function(){
1034                 if (QSFieldsArray['ImportEditView_parent_name'] != null) {
1035                         QSFieldsArray['ImportEditView_parent_name'].destroy();
1036                         delete QSFieldsArray['ImportEditView_parent_name'];
1037                 } // if
1038                                 if (QSProcessedFieldsArray['ImportEditView_parent_name']) {
1039                                         QSProcessedFieldsArray['ImportEditView_parent_name'] = false;
1040                                 } // if
1041             });
1042
1043             SED.relateDialog.renderEvent.subscribe(function() {
1044                 var viewPortHeight = YAHOO.util.Dom.getViewportHeight();
1045                 var contH = 0;
1046                 for (var i in this.body.childNodes) {
1047                         if (this.body.childNodes[i].clientHeight)
1048                                 contH += this.body.childNodes[i].clientHeight;
1049                 }
1050             }, SED.relateDialog);
1051             SED.relateDialog.setHeader(app_strings.LBL_EMAIL_RELATE_EMAIL);
1052                 } // end lazy load
1053
1054         SED.relateDialog.setBody(ret.html);
1055         SED.relateDialog.render();
1056         SUGAR.util.evalScript(ret.html);
1057         SED.relateDialog.show();
1058     }
1059 };
1060 /**
1061  * DetailView callbacks
1062  */
1063 AjaxObject.detailView.callback = {
1064         emailDetail : {
1065                 success : function(o) {
1066                         SUGAR.email2.o = o;
1067                         var ret = YAHOO.lang.JSON.parse(o.responseText);
1068                         SUGAR.email2.detailView.consumeMetaDetail(ret);
1069                 },
1070                 argument: [targetDiv],
1071                 failure : AjaxObject.handleFailure,
1072                 timeout : 0,
1073                 scope   : AjaxObject
1074         },
1075         emailPreview : {
1076                 success : function(o) {
1077                         SUGAR.email2.o = o;
1078                         var ret = YAHOO.lang.JSON.parse(o.responseText);
1079                         SUGAR.email2.detailView.consumeMetaPreview(ret);
1080                 },
1081                 failure : AjaxObject.handleFailure,
1082                 timeout : 0,
1083                 scope   : AjaxObject
1084         },
1085         viewPrint : {
1086                 success : AjaxObject.detailView.displayPrintable,
1087                 failure : AjaxObject.handleFailure,
1088                 timeout : AjaxObject.timeout,
1089                 scope   : AjaxObject
1090         },
1091         viewRaw : {
1092                 success : AjaxObject.detailView.displayView,
1093                 failure : AjaxObject.handleFailure,
1094                 timeout : AjaxObject.timeout,
1095                 scope   : AjaxObject
1096         }
1097 };
1098
1099
1100
1101
1102
1103 AjaxObject.folders = {
1104         /**
1105          * check-mail post actions
1106          */
1107         checkMailCleanup : function(o) {
1108                 SUGAR.hideMessageBox();
1109                 AjaxObject.folders.rebuildFolders(o); // rebuild TreeView
1110
1111                 // refresh focus ListView
1112                 SE.listView.refreshGrid();
1113                 SUGAR.email2.folders.startCheckTimer(); // resets the timer
1114         },
1115
1116         /**
1117          */
1118         rebuildFolders : function(o) {
1119                 SUGAR.hideMessageBox();
1120
1121                 var data = YAHOO.lang.JSON.parse(o.responseText);
1122
1123                 email2treeinit(SUGAR.email2.tree, data.tree_data, 'frameFolders', data.param);
1124                 SUGAR.email2.folders.setSugarFolders();
1125         }
1126 };
1127 AjaxObject.folders.callback = {
1128         checkMail : {
1129                 success : AjaxObject.folders.checkMailCleanup,
1130                 failure : AjaxObject.handleFailure,
1131                 timeout : 600000, // 5 mins
1132                 scope   : AjaxObject
1133         }
1134 }
1135
1136 AjaxObject.rules = {
1137         loadRulesForSettings : function(o) {
1138                 document.getElementById("rulesListCell").innerHTML = o.responseText;
1139                 // assume we have the class we need
1140                 SUGAR.routing.getStrings();
1141                 SUGAR.routing.getDependentDropdowns();
1142         }
1143 };
1144 ////    END PER MODULE CALLBACK OBJECTS
1145 ///////////////////////////////////////////////////////////////////////////
1146
1147
1148 var callback = {
1149         success : AjaxObject.handleSuccess,
1150         failure : AjaxObject.handleFailure,
1151         timeout : AjaxObject.timeout,
1152         scope   : AjaxObject
1153 };
1154 var callbackAccount = {
1155         success : AjaxObject.ieSaveSuccess,
1156         failure : AjaxObject.handleFailure,
1157         timeout : AjaxObject.timeout,
1158         scope   : AjaxObject
1159 };
1160 var callbackAccountDelete = {
1161         success : AjaxObject.ieDeleteSuccess,
1162         failure : AjaxObject.handleFailure,
1163         timeout : AjaxObject.timeout,
1164         scope   : AjaxObject
1165 };
1166 var callbackOutboundTest = {
1167         success : AjaxObject.ieSendSuccess,
1168         failure : AjaxObject.handleFailure,
1169         timeout : AjaxObject.timeout,
1170         scope   : AjaxObject
1171 };
1172
1173
1174
1175 var callbackTeamInfoForSettings = {
1176 success : function (o) {
1177         var data = YAHOO.lang.JSON.parse(o.responseText);
1178         document.getElementById('EditViewGroupFolderTeamTD').innerHTML = data.defaultgroupfolder;
1179 },
1180 failure : AjaxObject.handleFailure,
1181 timeout : AjaxObject.timeout,
1182 scope   : AjaxObject
1183
1184 };
1185
1186 var callbackStatusForImport = {
1187 success : function (o) {
1188         SUGAR.hideMessageBox();
1189         if (o.responseText != "")  {
1190                 var statusString = "";
1191                 var data = YAHOO.lang.JSON.parse(o.responseText);
1192                 for(i=0; i<data.length; i++) {
1193                         statusString = statusString + data[i] + '<br/>';
1194                 }
1195                 SUGAR.showMessageBox(SUGAR.language.get('Emails','LBL_IMPORT_STATUS_TITLE'), statusString, 'alert');
1196         }
1197         SE.listView.refreshGrid();
1198
1199 },
1200 failure : AjaxObject.handleFailure,
1201 timeout : AjaxObject.timeout,
1202 scope   : AjaxObject
1203
1204 };
1205 var callbackComposeCache = {
1206         success : AjaxObject.composeCache,
1207         failure : AjaxObject.handleFailure,
1208         timeout : AjaxObject.timeout,
1209         scope   : AjaxObject
1210 };
1211 var callbackDelete = {
1212         success : AjaxObject.handleDeleteReturn,
1213         failure : AjaxObject.handleFailure,
1214         timeout : AjaxObject.timeout,
1215         scope   : AjaxObject
1216 };
1217 var callbackEmailDetailMultiple = {
1218         success : function(o) {
1219                 SUGAR.hideMessageBox();
1220         var retMulti = YAHOO.lang.JSON.parse(o.responseText);
1221                 var ret = new Object();
1222
1223                 for(var i=0; i<retMulti.length; i++) {
1224                         ret = retMulti[i];
1225
1226                         SUGAR.email2._setDetailCache(ret);
1227                         SUGAR.email2.detailView.populateDetailView(ret.meta.uid, ret.meta.mbox, ret.meta.ieId, true, SUGAR.email2.innerLayout);
1228                 }
1229         },
1230         failure : AjaxObject.handleFailure,
1231         timeout : 0,
1232         scope   : AjaxObject
1233 };
1234 var callbackListViewSortOrderChange = {
1235         success : AjaxObject.saveListViewSortOrderPart2,
1236         failure : AjaxObject.handleFailure,
1237         timeout : AjaxObject.timeout,
1238         scope   : AjaxObject,
1239         argument        : [ieId, ieName, focusFolder]
1240 };
1241 var callbackEmptyTrash = {
1242         success : function(o) {
1243                 SUGAR.hideMessageBox();
1244                 AjaxObject.folderRenameCleanup;
1245         },
1246         failure : AjaxObject.handleFailure,
1247         timeout : AjaxObject.timeout,
1248         scope   : AjaxObject
1249 };
1250 var callbackClearCacheFiles = {
1251         success : function(o) {
1252                 SUGAR.hideMessageBox();
1253         },
1254         failure : AjaxObject.handleFailure,
1255         timeout : AjaxObject.timeout,
1256         scope   : AjaxObject
1257 };
1258 var callbackFolderRename = {
1259         success : function(o) {SUGAR.hideMessageBox();SUGAR.email2.folders.rebuildFolders();},
1260         failure : AjaxObject.handleFailure,
1261         timeout : AjaxObject.timeout,
1262         scope   : AjaxObject
1263 };
1264 var callbackFolderDelete = {
1265         success : function(o) {
1266                 var ret = YAHOO.lang.JSON.parse(o.responseText);
1267                 if (ret.status) {
1268                     if (ret.folder_id) {
1269                         var node = SUGAR.email2.folders.getNodeFromId(ret.folder_id);
1270                         if(node)
1271                                 SUGAR.email2.tree.removeNode(node, true);
1272                     } else if (ret.ieId && ret.mbox) {
1273                         var node = SUGAR.email2.folders.getNodeFromIeIdAndMailbox(ret.ieId, ret.mbox);
1274                         if(node)
1275                                 SUGAR.email2.tree.removeNode(node, true);
1276                     }
1277                         SUGAR.hideMessageBox();
1278                         //SUGAR.email2.folders.loadSettingFolder();
1279                 } else {
1280                         SUGAR.hideMessageBox();
1281                         SUGAR.showMessageBox(app_strings.LBL_EMAIL_ERROR_DESC, ret.errorMessage, 'alert');
1282                 } // else
1283         },
1284         failure : AjaxObject.handleFailure,
1285         timeout : AjaxObject.timeout,
1286         scope   : AjaxObject
1287 };
1288 var callbackFolderSave = {
1289         success : function(o) {
1290                 var ret = YAHOO.lang.JSON.parse(o.responseText);
1291
1292                 switch(ret.action) {
1293                         case 'newFolderSave':
1294                                 SUGAR.email2.folders.rebuildFolders();
1295                         break;
1296                 }
1297         },
1298         failure : AjaxObject.handleFailure,
1299         timeout : AjaxObject.timeout,
1300         scope   : AjaxObject
1301 };
1302 var callbackFolderSubscriptions = {
1303         success : AjaxObject.updateFolderSubscriptions,
1304         failure : AjaxObject.handleFailure,
1305         timeout : AjaxObject.timeout,
1306         scope   : AjaxObject
1307 };
1308 var callbackFolderUpdate = {
1309         success : AjaxObject.updateFrameFolder,
1310         failure : AjaxObject.handleFailure,
1311         timeout : AjaxObject.timeout,
1312         scope   : AjaxObject
1313 };
1314 var callbackFolders = {
1315         success : AjaxObject.folders.rebuildFolders,
1316         //success : void(true),
1317         failure : AjaxObject.handleFailure,
1318         timeout : AjaxObject.timeout,
1319         scope   : AjaxObject
1320 };
1321 var callbackFullSync = {
1322         success : AjaxObject.fullSyncCleanup,
1323         failure : AjaxObject.handleFailure,
1324         timeout : 9999999999999,
1325         scope   : AjaxObject
1326 };
1327 var callbackGeneric = {
1328         success : function() {
1329                 SUGAR.hideMessageBox();
1330         },
1331         failure : AjaxObject.handleFailure,
1332         timeout : AjaxObject.timeout,
1333         scope   : AjaxObject
1334 };
1335 var callbackIeAccountRetrieve = {
1336         success : function(o) {
1337                 // return JSON encoding
1338                 SUGAR.hideMessageBox();
1339                 SUGAR.email2.accounts.fillIeAccount(o.responseText);
1340                 SUGAR.email2.accounts.showEditInboundAccountDialogue(false);
1341         },
1342         failure : AjaxObject.handleFailure,
1343         timeout : AjaxObject.timeout,
1344         scope   : AjaxObject
1345 };
1346 var callbackImportOneEmail = {
1347         success :  AjaxObject.detailView.showImportForm,
1348         failure : AjaxObject.handleFailure,
1349         timeout : AjaxObject.timeout,
1350         scope   : AjaxObject
1351 };
1352 var callbackRelateEmail = {
1353     success : AjaxObject.detailView.showRelateForm,
1354     failure : AjaxObject.handleFailure,
1355     timeout : AjaxObject.timeout,
1356     scope   : AjaxObject
1357 }
1358 var callbackEmailDetailView = {
1359     success : AjaxObject.detailView.showEmailDetailView,
1360     failure : AjaxObject.handleFailure,
1361     timeout : AjaxObject.timeout,
1362     scope   : AjaxObject
1363 }
1364 var callbackAssignmentDialog = {
1365         success :  AjaxObject.detailView.showAssignmentDialogWithData,
1366         failure : AjaxObject.handleFailure,
1367         timeout : AjaxObject.timeout,
1368         scope   : AjaxObject
1369 };
1370 var callbackAssignmentAction = {
1371         success :  function(o) {
1372                 SE.listView.refreshGrid();
1373                 SUGAR.hideMessageBox();
1374                 if(o.responseText != '') {
1375                SUGAR.showMessageBox('Assignment action result', o.responseText, 'alert');
1376             } // if
1377         } ,
1378         failure : AjaxObject.handleFailure,
1379         timeout : AjaxObject.timeout,
1380         scope   : AjaxObject
1381 };
1382 var callbackMoveEmails = {
1383         success :  function(o) {
1384             SE.listView.refreshGrid();
1385                 SUGAR.hideMessageBox();
1386                 if(o.responseText != '') {
1387                SUGAR.showMessageBox(app_strings.LBL_EMAIL_ERROR_DESC, o.responseText, 'alert');
1388             } // if
1389         } ,
1390         failure : AjaxObject.handleFailure,
1391         timeout : AjaxObject.timeout,
1392         scope   : AjaxObject
1393 };
1394 var callbackLoadAttachments = {
1395         success : AjaxObject.loadAttachments,
1396         failure : AjaxObject.handleFailure,
1397         timeout : AjaxObject.timeout,
1398         scope   : AjaxObject
1399 };
1400 var callbackLoadRules = {
1401         success : AjaxObject.rules.loadRulesForSettings,
1402         failure : AjaxObject.handleFailure,
1403         timeout : AjaxObject.timeout,
1404         scope   : AjaxObject
1405 };
1406 var callbackLoadSignature = {
1407         success : AjaxObject.loadSignature,
1408         failure : AjaxObject.handleFailure,
1409         timeout : AjaxObject.timeout,
1410         scope   : AjaxObject
1411 };
1412 var callbackDeleteSignature = {
1413         success : AjaxObject.handleDeleteSignature,
1414         failure : AjaxObject.handleFailure,
1415         timeout : AjaxObject.timeout,
1416         scope   : AjaxObject
1417 };
1418 /*var callbackMoveEmails = {
1419     success : function(o) { SUGAR.email2.listView.moveEmailsCleanup(o) },
1420     failure : AjaxObject.handleFailure,
1421     timeout : AjaxObject.timeout,
1422     scope   : AjaxObject
1423 }*/
1424 var callbackOutboundSave = {
1425         success : AjaxObject.accounts.saveOutboundCleanup,
1426         failure : AjaxObject.handleFailure,
1427         timeout : AjaxObject.timeout,
1428         scope   : AjaxObject
1429 };
1430 var callbackDefaultOutboundSave = {
1431         success : AjaxObject.accounts.saveDefaultOutboundCleanup,
1432         failure : AjaxObject.handleFailure,
1433         timeout : AjaxObject.timeout,
1434         scope   : AjaxObject
1435 };
1436 var callbackQuickCreate = {
1437         success : AjaxObject.detailView.showQuickCreateForm,
1438         failure : AjaxObject.handleFailure,
1439         timeout : AjaxObject.timeout,
1440         scope   : AjaxObject
1441 };
1442 var callbackQuickCreateSave = {
1443         success : AjaxObject.detailView.saveQuickCreateForm,
1444         failure : AjaxObject.handleFailure,
1445         timeout : AjaxObject.timeout,
1446         scope   : AjaxObject
1447 };
1448 var callbackQuickCreateSaveAndAddToAddressBook = {
1449         success : AjaxObject.detailView.saveQuickCreateFormAndAddToAddressBook,
1450         failure : AjaxObject.handleFailure,
1451         timeout : AjaxObject.timeout,
1452         scope   : AjaxObject
1453 };
1454 var callbackQuickCreateSaveAndReply = {
1455     success : AjaxObject.detailView.saveQuickCreateFormAndReply,
1456     failure : AjaxObject.handleFailure,
1457     timeout : AjaxObject.timeout,
1458     scope   : AjaxObject
1459 }
1460 var callbackQuickCreateSaveAndReplyCase = {
1461     success : AjaxObject.detailView.saveQuickCreateFormAndReplyCase,
1462     failure : AjaxObject.handleFailure,
1463     timeout : AjaxObject.timeout,
1464     scope   : AjaxObject
1465 }
1466 var callbackRebuildShowAccountList = {
1467         success : AjaxObject.rebuildShowFolders,
1468         failure : AjaxObject.handleFailure,
1469         timeout : AjaxObject.timeout,
1470         scope   : AjaxObject
1471 };
1472
1473 var callbackRefreshSugarFolders = {
1474         success : function(o) {
1475                 var t = YAHOO.lang.JSON.parse(o.responseText);
1476                 SUGAR.email2.folders.setSugarFoldersEnd(t);
1477         },
1478         failure : AjaxObject.handleFailure,
1479         timeout : AjaxObject.timeout,
1480         scope   : AjaxObject
1481 };
1482 var callbackReplyForward = {
1483         success : AjaxObject.handleReplyForward,
1484         finish : function(a, retryCount,isReOrFwDraft) {
1485                 if (typeof(retryCount) == 'undefined') {
1486                         retryCount = 0;
1487                 } else {
1488                         retryCount++;
1489                 }
1490                 if (typeof(isReOrFwDraft) == 'undefined') {
1491                         isReOrFwDraft = 0;
1492                 }
1493                 var idx = SUGAR.email2.composeLayout.currentInstanceId;
1494                 var t = tinyMCE.getInstanceById('htmleditor' + idx);
1495         try {
1496                         var html = t.getContent();
1497
1498             html = "&nbsp;";
1499             //add hr tag if this is not a reply draft or forward draft
1500             if(!isReOrFwDraft){
1501                 html += "<div><hr></div>";
1502             }
1503             html +=  a.description;
1504
1505                         t.setContent(html);//
1506
1507             if (a.type != 'draft') {
1508                 // Next step, attach signature
1509                 SUGAR.email2.composeLayout.resizeEditorSetSignature(idx,true);
1510             }
1511
1512                 } catch(e) {
1513                         if (retryCount < 5) {
1514                                 setTimeout("callbackReplyForward.finish(globalA, " + retryCount + ");", 500);
1515                                 return;
1516                         }
1517                 }
1518                 var tabArray = SUGAR.email2.innerLayout.get("tabs");
1519                 if (tabArray != null && tabArray.length > 0) {
1520                         for (i = 0 ; i < tabArray.length ; i++) {
1521                                 var tabObject = tabArray[i];
1522                                 if (tabObject.get("id") == ("composeTab" + idx)) {
1523                                         var tabLabel = a.name;
1524                                 if (tabLabel != null && tabLabel.length > 25) {
1525                                         tabLabel = tabLabel.substring(0, 25) + "...";
1526                                 } // if
1527                                         tabObject.get("labelEl").firstChild.data = tabLabel;
1528                                         break;
1529                                 }
1530                         }
1531                 }
1532
1533                 //SUGAR.email2.innerLayout.regions.center.getPanel('composeLayout' + idx).setTitle(a.name);
1534                 if (a.parent_name != null && a.parent_name != "") {
1535                         document.getElementById('data_parent_name' + idx).value = a.parent_name;
1536                 }
1537                 if (a.parent_type != null && a.parent_type != "") {
1538                         document.getElementById('data_parent_type' + idx).value = a.parent_type;
1539                 }
1540                 if (a.parent_id != null && a.parent_id != "") {
1541                         document.getElementById('data_parent_id' + idx).value = a.parent_id;
1542                 }
1543                 if (a.fromAccounts.status) {
1544                         var addressFrom = document.getElementById('addressFrom' + idx);
1545                 SUGAR.email2.util.emptySelectOptions(addressFrom);
1546                 var fromAccountOpts = a.fromAccounts.data;
1547                 for(i=0; i<fromAccountOpts.length; i++) {
1548                       var key = fromAccountOpts[i].value;
1549                       var display = fromAccountOpts[i].text;
1550                       var opt = new Option(display, key);
1551                       if (fromAccountOpts[i].selected) {
1552                         opt.selected = true;
1553                       }
1554                       addressFrom.options.add(opt);
1555                 }
1556                 } // if
1557                 SUGAR.hideMessageBox();
1558
1559         },
1560         failure : AjaxObject.handleFailure,
1561         timeout : AjaxObject.timeout,
1562         scope   : AjaxObject,
1563         argument        : [sendType]
1564 };
1565 var callbackSendEmail = {
1566         success : AjaxObject.sendEmailCleanUp,
1567         failure : AjaxObject.handleFailure,
1568         timeout : AjaxObject.timeout,
1569         scope   : AjaxObject
1570 };
1571 var callbackSettings = {
1572         success : AjaxObject.updateUserPrefs,
1573         failure : AjaxObject.handleFailure,
1574         timeout : AjaxObject.timeout,
1575         scope   : AjaxObject
1576 };
1577 var callbackSettingsFolderRefresh = {
1578         success : AjaxObject.settingsFolderRefresh,
1579         failure : AjaxObject.handleFailure,
1580         timeout : AjaxObject.timeout,
1581         scope   : AjaxObject
1582 };
1583 var callbackLoadSettingFolder = {
1584         success : function(o) {
1585                 AjaxObject.settingsFolderRefresh(o);
1586                 SUGAR.email2.accounts.rebuildFolderList(); // refresh frameFolder
1587         },
1588         failure : AjaxObject.handleFailure,
1589         timeout : AjaxObject.timeout,
1590         scope   : AjaxObject
1591
1592 };
1593 var callbackUploadAttachment = {
1594         success : AjaxObject.uploadAttachmentSuccessful,
1595         upload  : AjaxObject.uploadAttachmentSuccessful,
1596         failure : AjaxObject.handleFailure,
1597         timeout : AjaxObject.timeout,
1598         scope   : AjaxObject
1599 };
1600 var callbackUserPrefs = {
1601         success : function(o) {
1602                 SUGAR.email2.userPrefs = YAHOO.lang.JSON.parse(o.responseText);
1603         },
1604         failure : AjaxObject.handleFailure,
1605         timeout : AjaxObject.timeout,
1606         scope   : AjaxObject
1607 };
1608
1609 var callbackContextmenus = {
1610         markUnread : {
1611                 success : AjaxObject.markEmailCleanup,
1612                 failure : AjaxObject.handleFailure,
1613                 timeout : AjaxObject.timeout,
1614                 scope   : AjaxObject
1615         }
1616 };
1617
1618 var callbackCheckEmail2 = {
1619         success : function(o) {
1620         var ret = YAHOO.lang.JSON.parse(o.responseText);
1621                 SUGAR.showMessageBox(app_strings.LBL_EMAIL_CHECKING_NEW, ret.text);
1622
1623
1624         },
1625         failure : AjaxObject.handleFailure,
1626         timeout : AjaxObject.timeout,
1627         scope   : AjaxObject
1628 }