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