]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Emails/javascript/EmailUICompose.js
Release 6.2.0beta4
[Github/sugarcrm.git] / modules / Emails / javascript / EmailUICompose.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  (function() {
37         var sw = YAHOO.SUGAR,
38                 Event = YAHOO.util.Event,
39                 Connect = YAHOO.util.Connect,
40             Dom = YAHOO.util.Dom
41             SE = SUGAR.email2;
42
43 ///////////////////////////////////////////////////////////////////////////////
44 ////    ADDRESS BOOK
45 SE.addressBook = {
46     _contactCache : new Array(), // cache of contacts
47     _dd : new Array(), // filtered list, same format as _contactCache
48     _ddLists : new Array(), // list of Lists
49     _dd_mlUsed : new Array(), // contacts in mailing list edit view column1
50     _dd_mlAvailable : new Array(), // contacts in mailing list edit view column2
51     clickBubble : true, // hack to get around onclick event bubbling
52         relatedBeanId : '',
53         relatedBeanType : '',
54         idx : 0,
55
56     itemSpacing : 'white-space:nowrap; padding:2px;',
57     reGUID : SE.reGUID,
58
59
60
61     /**
62     *  YUI bug fix 2527707.  Causes nested datatable's in <tables> to cause 404 errors whens earching.
63     */
64     initFixForDatatableSort: function () {
65         //Workaround for YUI bug 2527707: http://yuilibrary.com/projects/yui2/ticket/913efafad48ce433199f3e72e4847b18, should be removed when YUI 2.8+ is used
66         YAHOO.widget.DataTable.prototype.getColumn = function(column) {
67             var oColumn = this._oColumnSet.getColumn(column);
68
69             if(!oColumn) {
70                 // Validate TD element
71                 var elCell = column.nodeName.toLowerCase() != "th" ? this.getTdEl(column) : false;
72                 if(elCell) {
73                     oColumn = this._oColumnSet.getColumn(elCell.cellIndex);
74                 }
75                 // Validate TH element
76                 else {
77                     elCell = this.getThEl(column);
78                     if(elCell) {
79                         // Find by TH el ID
80                         var allColumns = this._oColumnSet.flat;
81                         for(var i=0, len=allColumns.length; i<len; i++) {
82                             if(allColumns[i].getThEl().id === elCell.id) {
83                                 oColumn = allColumns[i];
84                             }
85                         }
86                     }
87                 }
88             }
89
90             return oColumn;
91         };
92     },
93
94     cancelEdit : function() {
95         if(this.editContactDialog)
96             this.editContactDialog.hide();
97         if(this.editMailingListDialog)
98             this.editMailingListDialog.hide();
99     },
100
101     /**
102      * Clears filter form
103      */
104     clear : function() {
105         var t = document.getElementById('contactsFilter');
106         t.value = '';
107         this.filter(t);
108     },
109
110     /**
111      * handle context-menu Compose-to call
112      * @param string type 'contacts' or 'lists'
113      */
114     composeTo : function(type, waited) {
115         var activePanel = SUGAR.email2.innerLayout.get("activeTab").get("id")
116         if (activePanel.substring(0, 10) != "composeTab") {
117             SE.composeLayout.c0_composeNewEmail();
118             setTimeout("SE.addressBook.composeTo('" + type + "', true);");
119                 SE.contextMenus.contactsContextMenu.hide();
120             return;
121         }
122         var idx = activePanel.substring(10);
123         var rows = [ ];
124         var id = '';
125         // determine if we have a selection to work with
126         if(type == 'contacts') {
127             var ids = SE.contactView.getSelectedRows();
128             for (var i in ids) {
129                 rows[i] = SE.contactView.getRecord(ids[i]);
130             }
131             removeHiddenNodes(rows, SE.contactView);
132         } 
133                 else { return; }
134
135         if(rows.length > 0) {
136             SE.composeLayout.handleDrop(
137                 (type == 'contacts') ? SE.contactView : SE.emailListsView, 
138                 null, rows, 'addressTO' + idx );
139         } else {
140             alert(app_strings.LBL_EMAIL_MENU_MAKE_SELECTION);
141         }
142     },
143
144     editContact : function() {
145         SE.contextMenus.contactsContextMenu.hide();
146         var element = SE.contactView.getSelectedNodes()[0];
147         var elementId = "";
148         if (element.className.indexOf('address-contact') > -1) {
149             elementId = element.id;
150         } else if (element.className.indexOf('address-exp-contact') > -1) {
151             elementId = element.id.substring(2);
152         }
153     },
154     
155
156     /**
157      * Filters contact entries based on user input
158      */
159     filter : function(inputEl) {
160         var ret = new Object();
161         var re = new RegExp(inputEl.value, "gi");
162
163         for(var i in this._contactCache) {
164             if(this._contactCache[i].name.match(re)) {
165                 ret[i] = this._contactCache[i];
166             }
167         }
168
169         this.buildContactList(ret);
170     },
171
172     fullForm : function(id, module) {
173         document.location = "index.php?return_module=Emails&return_action=index&module=" + module + "&action=EditView&record=" + id;
174     },
175
176     /**
177      * returns a formatted email address from the addressBook cache
178      */
179     getFormattedAddress : function(id) {
180         var o = this._contactCache[id];
181         var primaryEmail = '';
182
183         for(var i=0; i<o.email.length; i++) {
184             var currentEmail = o.email[i].email_address;
185
186             if(o.email[i].primary_address == 1) {
187                 primaryEmail = o.email[i].email_address;
188             }
189         }
190
191         var finalEmail = (primaryEmail == "") ? currentEmail : primaryEmail;
192         var name = new String(o.name);
193         var finalName = name.replace(/(<([^>]+)>)/ig, "").replace(/&#039;/gi,'\'');
194         var ret = finalName + " <" + finalEmail.replace(/&#039;/gi,'\'') + ">";
195
196         return ret;
197     },
198     
199     /**
200      * Sets up async call to query for matching contacts, users, etc.
201      */
202     searchContacts : function() {
203         var fn = document.getElementById('input_searchField').value;
204         var pe = document.getElementById('input_searchPerson').value;
205         
206         var rb = document.getElementById('hasRelatedBean').checked;
207         if (rb) {
208                         var idx = this.idx;
209                 var relatedBeanId = document.getElementById('data_parent_id' + idx).value;
210                 var relatedBeanType = document.getElementById('data_parent_type' + idx).value;
211                 this.addressBookDataModel.params['related_bean_id'] = relatedBeanId;
212                 this.addressBookDataModel.params['related_bean_type'] = relatedBeanType;
213         } else {
214                 this.addressBookDataModel.params['related_bean_id'] = '';
215         }
216         
217         this.addressBookDataModel.params['search_field'] = fn;
218         this.addressBookDataModel.params['person'] = pe;
219         this.addressBookDataModel.params['emailUIAction'] = 'getAddressSearchResults';
220         this.grid._oDataSource = this.addressBookDataModel;
221         this.grid.getDataSource().sendRequest(SUGAR.util.paramsToUrl(this.addressBookDataModel.params),  this.grid.onDataReturnInitializeTable, this.grid);
222     },
223     
224     /**
225      * Clear Search Crieteria For Addressbook
226      */
227     clearAddressBookSearch : function() {
228         document.getElementById('input_searchField').value = "";
229         document.getElementById('input_searchPerson').selectedIndex = 0;
230     },
231     
232     /**
233      * Opens modal select window to add contacts to addressbook
234      */
235     selectContactsDialogue : function(destId) {
236         if(!this.contactsDialogue) {
237                 var dlg = this.contactsDialogue = new YAHOO.widget.Dialog("contactsDialogue", {
238                 modal:true,
239                 visible:false,
240                 draggable: false,
241                 constraintoviewport: true,
242                 width   : 980,
243                 buttons : [{text: app_strings.LBL_EMAIL_ADDRESS_BOOK_ADD, isDefault: true, handler: this.populateEmailAddressFieldsFromResultTable},
244                            {text: app_strings.LBL_EMAIL_ADDRESS_BOOK_CLEAR, isDefault: true, handler: this.clearAllEmailAddressFieldsFromResultTable} ]
245             });
246                 dlg.setHeader(app_strings.LBL_EMAIL_ADDRESS_BOOK_SELECT_TITLE);
247                 
248                 var body = SUGAR.util.getAndRemove("contactsDialogueHTML");
249                 dlg.setBody(body.innerHTML);
250                 dlg.renderEvent.subscribe(function() {
251                 var iev = YAHOO.util.Dom.get("contactsDialogueBody");
252                 if (iev && !SUGAR.isIE) {
253                         this.body.style.width = "950px";
254                 }
255             }, dlg);
256                      
257             
258                 dlg.beforeRenderEvent.subscribe(function() { 
259                         var dd = new YAHOO.util.DDProxy(dlg.element); 
260                         dd.setHandleElId(dlg.header); 
261                         dd.on('endDragEvent', function() { 
262                                 dlg.show(); 
263                         }); 
264                 }, dlg, true); 
265                 dlg.render();
266                 
267                 var tp = new YAHOO.widget.TabView("contactsSearchTabs");
268                         
269                 var tabContent = SUGAR.util.getAndRemove("searchForm");
270                 tp.addTab(new YAHOO.widget.Tab({
271                                 label: app_strings.LBL_EMAIL_ADDRESS_BOOK_TITLE,
272                                 scroll : true,
273                                 content : tabContent.innerHTML,
274                                 id : "addressSearchTab",
275                                 active : true
276                         }));
277                         
278                 var addListenerFields = ['input_searchPerson','input_searchField' ]
279                 YAHOO.util.Event.addListener(addListenerFields,"keydown", function(e){
280                         if (e.keyCode == 13) {
281                                 YAHOO.util.Event.stopEvent(e);
282                                 SUGAR.email2.addressBook.searchContacts();
283                         }
284                 });
285
286                 this.contactsDialogue.render();
287                 dlg.center();
288         }
289         //Quick Compose does not have an innerLayout component and will always be referenced with ix 0.
290         if (typeof(SUGAR.email2.innerLayout) == 'undefined')
291             var idx = 0;
292         else
293         {
294             var activePanel = SUGAR.email2.innerLayout.get("activeTab").get("id");
295             var idx = activePanel.substring(10);
296         }
297         SE.addressBook.idx = idx;
298         
299                 var relatedBeanId;
300         if ((hasRelatedBeanId = document.getElementById('data_parent_id' + idx).value) != '') {
301                 document.getElementById('relatedBeanColumn').style.display = '';
302                 var relatedBeanName = document.getElementById('data_parent_name' + idx).value;
303                         var relatedBeanType = document.getElementById('data_parent_type' + idx).value;
304                         relatedBeanId = document.getElementById('data_parent_id' + idx).value;
305                         document.getElementById('relatedBeanInfo').innerHTML = ' ' + relatedBeanType + ' <b>' + relatedBeanName + '</b>';
306                         SE.addressBook.relatedBeanType = relatedBeanType;
307             } else {
308                 document.getElementById('relatedBeanColumn').style.display = 'none';
309                 document.getElementById('hasRelatedBean').checked = false;
310             }
311             
312             if (!SE.addressBook.grid) 
313             {
314                 if (hasRelatedBeanId) {
315                         document.getElementById('hasRelatedBean').checked = true;
316                 }
317                 AddressSearchGridInit();
318                         SE.addressBook.relatedBeanId = relatedBeanId;
319             } 
320             else
321             {
322                 if (typeof(relatedBeanId) != 'undefined' && relatedBeanId != SE.addressBook.relatedBeanId)
323                 {
324                         SE.addressBook.relatedBeanId = relatedBeanId;
325                         document.getElementById('hasRelatedBean').checked = true;
326                 }
327                 if (document.getElementById('hasRelatedBean').checked == true)
328                 {
329                         SE.addressBook.addressBookDataModel.params['related_bean_id'] = relatedBeanId;
330                         SE.addressBook.addressBookDataModel.params['related_bean_type'] = relatedBeanType;
331                 } else {
332                         SE.addressBook.addressBookDataModel.params['related_bean_id'] = '';
333                         SE.addressBook.addressBookDataModel.params['related_bean_type'] = '';
334                 }
335                 SE.addressBook.addressBookDataModel.params['search_field'] = document.getElementById('input_searchField').value;;
336                         SE.addressBook.addressBookDataModel.params['person'] = document.getElementById('input_searchPerson').value;
337                 SE.addressBook.grid.getDataSource().sendRequest(SUGAR.util.paramsToUrl(SE.addressBook.addressBookDataModel.params),  SE.addressBook.grid.onDataReturnInitializeTable, SE.addressBook.grid);
338             }
339             
340             //Remove any lingering rows in the result set table if the module was closed.
341             SE.addressBook.gridResults.deleteRows(0, SUGAR.email2.addressBook.gridResults.getRecordSet().getLength());
342             //Repopulate
343             SE.addressBook.populateResulstTableEmailAddresses();
344             
345         this.contactsDialogue.show();
346     },
347     /**
348     *  Clear all email addresses from result table.
349     *
350     */
351     clearAllEmailAddressFieldsFromResultTable: function () {
352         SUGAR.email2.addressBook.gridResults.deleteRows(0, SUGAR.email2.addressBook.gridResults.getRecordSet().getLength());
353         //Unhighlight any rows currently selected if the emails were cleared.
354         SUGAR.email2.addressBook.grid.toggleSelectAll(false);
355         SUGAR.email2.addressBook.grid.reSelectRowsOnRender();
356     },
357     /**
358     *  Take all email address listed in the compose tab To|Cc|Bcc fields and re-populates the 
359     *  results table.  This function is called when the address book is displayed.
360     */
361     populateResulstTableEmailAddresses: function () {
362       
363         var idx = SE.addressBook.idx;
364         var emailFields = ['to','cc','bcc'];
365         
366         for(var k=0;k<emailFields.length;k++)
367         {
368             var elKey = 'address' + emailFields[k].toUpperCase() + idx;
369             var allEmails = document.getElementById(elKey).value;
370             if(allEmails == '')
371                 continue;
372             
373             var formatedEmails = SE.composeLayout._getEmailArrayFromString(allEmails);
374             
375                 for (var i=0; i<formatedEmails.length; i++)
376                 {
377                     var t_name = formatedEmails[i].name;
378                     var t_emailAddr = formatedEmails[i].email_address;
379                     var displayEmail = t_name + ' <' + t_emailAddr + '>';
380                     if(t_name == '')
381                         t_name = displayEmail = t_emailAddr;
382                 
383                     var addressType = SE.addressBook.translateAddresType(emailFields[k],true);
384                 SUGAR.email2.addressBook.gridResults.addRow({'type':addressType,'name':t_name,'email_address': t_emailAddr,
385                     'display_email_address': displayEmail,'bean_id': -1,'idx' : SE.addressBook.idx});
386                 }
387         }  
388     },
389  
390     /**
391     * Checks all entries in the result table against a particular email address, returning true
392     * if the email address is found, false otherwise.
393     */
394     doesEmailAdddressExistInResultTable: function(emailAddress)
395     {
396         if(trim(emailAddress) == '')
397             return false;   
398             
399         var emailAddressFound = false;
400         var contacts = SE.addressBook.gridResults.getRecordSet().getRecords();
401         for (var i=0; i < contacts.length; i++) 
402         {
403             var data = SE.addressBook.gridResults.getRecord(contacts[i]).getData();
404             //If we are adding to cc or bcc fields, make them visible.
405             if(data.email_address == emailAddress)    
406             {
407                 emailAddressFound = true;
408                 break;
409             }
410         }
411         
412         return emailAddressFound;
413     },
414     /**
415     *  Takes all email addresses that the users wishes to add from the address book and populates the To 
416     *  fields on the compose tab. 
417     */
418     populateEmailAddressFieldsFromResultTable: function()
419     {
420         //Clear the fields first, all email addresses are stored in the address book
421         var idx = SE.addressBook.idx;
422         var emailFields = ['to','cc','bcc'];    
423         for(var k=0;k<emailFields.length;k++)
424         {
425             var elKey = 'address' + emailFields[k].toUpperCase() + idx;
426             document.getElementById(elKey).value = "";
427         }
428         
429         var contacts = SE.addressBook.gridResults.getRecordSet().getRecords();
430         for (var i=0; i < contacts.length; i++) 
431         {
432             var data = SE.addressBook.gridResults.getRecord(contacts[i]).getData();
433             
434             var addressTypeKey = SE.addressBook.translateAddresType(data.type,false);
435             //If we are adding to cc or bcc fields, make them visible.
436             if(addressTypeKey =='cc' || addressTypeKey =='bcc')    
437                 SE.composeLayout.showHiddenAddress(addressTypeKey,data.idx);
438             //Construct the target id
439             var target_id = 'address' + addressTypeKey.toUpperCase() + data.idx
440            
441             var target = document.getElementById(target_id);
442             target.value = SE.addressBook.smartAddEmailAddressToComposeField(target.value, data.display_email_address);
443         }
444         
445         //Delete all rows from the result set table
446         SUGAR.email2.addressBook.gridResults.deleteRows(0, SUGAR.email2.addressBook.gridResults.getRecordSet().getLength());
447         
448         //Hide the dialogue
449         SE.addressBook.contactsDialogue.hide()
450     },
451     /**
452     *  Insert contacts into the result table.
453     */
454     insertContactToResultTable : function(event,address_type) {
455     
456         var contactsDialogue = SE.addressBook.contactsDialogue;
457         var contacts = SE.addressBook.grid.getSelectedRows();
458         
459         var rows = SUGAR.email2.addressBook.grid.getRecordSet().getRecords();
460         for (var i = 0; i < rows.length; i++) 
461         {
462                         if (typeof(rows[i]) != "undefined" && rows[i].getData().checked )
463                         {
464                             var recId = SE.addressBook.grid.getRecord(rows[i]).getId();
465                 SE.addressBook.insertContactRowToResultTable(recId,address_type);
466                 SUGAR.email2.addressBook.grid.selectRow(rows[i]);
467                 rows[i].setData("selected",true);
468                         }
469         }
470         var checkBoxes = SUGAR.email2.addressBook.grid.get("element").getElementsByTagName('input');
471         for (var i = 0; i < checkBoxes.length; i++) {
472             checkBoxes[i].checked = false;
473         }
474     },
475     /**
476     *
477     */
478     insertContactRowToResultTable : function(rowId, addressType) {
479         var data = SE.addressBook.grid.getRecord(rowId).getData();
480         if(SE.addressBook.doesGridResultsEntryExist(data.email) )
481                 return;
482         var name = data.name.replace(/&#039;/gi,'\'').replace(/&quot;/gi,'"');
483         var ea = name + ' <' + data.email.replace(/&#039;/gi,'\'') + '>';
484         if(addressType == null)
485             addressType = app_strings.LBL_EMAIL_ADDRESS_BOOK_ADD_TO.replace(/:$/,''); //Default to To when using the plus icon.
486         SUGAR.email2.addressBook.gridResults.addRow({'type':addressType,'name':name,'email_address': data.email,'display_email_address': ea,'bean_id': data.bean_id,'idx' : SE.addressBook.idx});
487     },
488     /**
489     * Remove a row from the gridsResult table.
490     */
491     removeRowFromGridResults : function(rowId,emailAddress)
492     {
493         var contacts = SE.addressBook.gridResults.getRecordSet().getRecords();
494         for (var i=0; i < contacts.length; i++) 
495         {
496             var rec = SE.addressBook.gridResults.getRecord(contacts[i]);
497             var data = rec.getData();
498             if(data.email_address == emailAddress)
499             {
500                 SUGAR.email2.addressBook.gridResults.deleteRow(rec.getId());
501                 break;
502             }
503         }
504         
505        SUGAR.email2.addressBook.toggleSearchRowIcon(rowId,true);
506     },
507     /**
508     * Translates between the addressType To|Cc|Bcc labels/keys.  
509     */
510     translateAddresType: function(addressType,fromKey)
511     {
512         var displayTo = app_strings.LBL_EMAIL_ADDRESS_BOOK_ADD_TO.replace(/:$/,'');
513         var displayCc = app_strings.LBL_EMAIL_ADDRESS_BOOK_ADD_CC.replace(/:$/,'');
514         var displayBcc = app_strings.LBL_EMAIL_ADDRESS_BOOK_ADD_BCC.replace(/:$/,''); 
515         var mappingObject = {};
516         
517         if(fromKey)
518             mappingObject = {'to':displayTo, 'cc':displayCc, 'bcc':displayBcc};
519         else
520         {
521             mappingObject[displayTo] = 'to'; //Cant use object literal with variable variable.
522             mappingObject[displayCc] = 'cc';
523             mappingObject[displayBcc] = 'bcc';
524         }
525             
526         return typeof(mappingObject[addressType]) != 'undefined' ? mappingObject[addressType] : '';
527           
528     },
529     /**
530     *
531     */
532     toggleSearchRowIcon : function(rowId,show) 
533     {
534         if(show)
535         {
536             var idToShow = rowId + '_add_img';
537             var idToHide = rowId + '_rm_img';
538         }
539         else
540         {
541             var idToShow = rowId + '_rm_img';
542             var idToHide = rowId + '_add_img';
543         }
544
545
546         Dom.addClass(idToHide, "yui-hidden");
547         Dom.removeClass(idToShow, "yui-hidden");
548     },
549     /**
550     * Determine if an entry has already been added to the grid results table to prevent duplicates.
551     */
552     doesGridResultsEntryExist: function(emailAddrs)
553     {
554         
555         var contactExists = false;
556         var contacts = SE.addressBook.gridResults.getRecordSet().getRecords();
557         for (var i=0; i < contacts.length; i++) 
558         {
559             var data = SE.addressBook.gridResults.getRecord(contacts[i]).getData();
560             if(data.email_address == emailAddrs)
561             {
562                 contactExists = true;
563                 break;
564             }
565         }
566         return contactExists;
567     },
568     
569     /**
570      * adds an email address to a string, but first checks if it exists
571      * @param string concat The string we are appending email addresses to
572      * @param string addr Email address to add
573      * @return string
574      */
575     smartAddEmailAddressToComposeField : function(concat, addr) {
576         var re = new RegExp(addr);
577
578         if(!concat.match(re)) {
579             if(concat != "") {
580                 concat += "; " + addr;
581             } else {
582                 concat = addr;
583             }
584         }
585
586         return concat;
587     }
588 };
589 ////    END ADDRESS BOOK
590 ///////////////////////////////////////////////////////////////////////////////
591
592
593
594 ///////////////////////////////////////////////////////////////////////////////
595 ////    AUTOCOMPLETE
596 /**
597  * Auto-complete object
598  */
599 SE.autoComplete = {
600     config : {
601         delimChar : [";", ","],
602         useShadow :    false,
603         useIFrame : false,
604         typeAhead : true,
605         prehighlightClassName : "yui-ac-prehighlight",
606         queryDelay : 0
607     },
608     instances : new Array(),
609
610     /**
611      * Parses an addressBook entry looking for primary address.  If not found, it will return the last found address.
612      * @param object Contact from AddressBook
613      * @return string
614      */
615     getPrimaryAddress : function(contact) {
616         var address = app_strings.LBL_EMAIL_ADDRESS_BOOK_NOT_FOUND;
617
618         for(var eIndex in contact.email) {
619             address = contact.email[eIndex].email_address;
620             if(contact.email[eIndex].primary_address == 1) {
621                 return contact.email[eIndex].email_address;
622             }
623         }
624         return address;
625     },
626     
627
628     /**
629      * initializes autocomplete widgets for a given compose view
630      * @param int idx
631      */
632     init : function(idx) {
633         var ds = new YAHOO.widget.DS_JSArray(this.returnDataSource(SE.addressBook._contactCache), {
634             "queryMatchContains" : false,
635             "queryMatchSubset" : true
636         });
637
638         this.instances[idx] = {
639             to : null,
640             cc : null,
641             bcc : null
642         };
643
644    
645         // instantiate the autoComplete widgets
646         this.instances[idx]['to'] = new YAHOO.widget.AutoComplete('addressTO'+idx, "addressToAC"+idx, ds, this.config);
647         this.instances[idx]['cc'] = new YAHOO.widget.AutoComplete('addressCC'+idx, "addressCcAC"+idx, ds, this.config);
648         this.instances[idx]['bcc'] = new YAHOO.widget.AutoComplete('addressBCC'+idx, "addressBccAC"+idx, ds, this.config);
649
650         // enable hiding of interfering textareas
651         this.instances[idx]['to'].containerExpandEvent.subscribe(SE.autoComplete.toggleTextareaHide);
652         this.instances[idx]['cc'].containerExpandEvent.subscribe(SE.autoComplete.toggleTextareaHide);
653         this.instances[idx]['bcc'].containerExpandEvent.subscribe(SE.autoComplete.toggleTextareaHide);
654
655         // enable reshowing of hidden textareas
656         this.instances[idx]['to'].containerCollapseEvent.subscribe(SE.autoComplete.toggleTextareaShow);
657         this.instances[idx]['cc'].containerCollapseEvent.subscribe(SE.autoComplete.toggleTextareaShow);
658         this.instances[idx]['bcc'].containerCollapseEvent.subscribe(SE.autoComplete.toggleTextareaShow);
659
660         // enable refreshes of contact lists
661         this.instances[idx]['to'].textboxFocusEvent.subscribe(SE.autoComplete.refreshDataSource);
662         this.instances[idx]['cc'].textboxFocusEvent.subscribe(SE.autoComplete.refreshDataSource);
663         this.instances[idx]['bcc'].textboxFocusEvent.subscribe(SE.autoComplete.refreshDataSource);
664     },
665
666     refreshDataSource : function(sType, aArgs) {
667         var textBoxId = aArgs[0].getInputEl().id; // "addressTo0"
668         var idx;
669         var refresh = SE.autoComplete.returnDataSource(SE.addressBook._contactCache);
670
671         if(textBoxId.indexOf("addressTO") > -1 || textBoxId.indexOf("addressCC") > -1) {
672             idx = textBoxId.substr(9);
673         } else {
674             idx = textBoxId.substr(10);
675         }
676
677         SE.autoComplete.instances[idx]['to'].dataSource.data = refresh;
678         SE.autoComplete.instances[idx]['cc'].dataSource.data = refresh;
679         SE.autoComplete.instances[idx]['bcc'].dataSource.data = refresh;
680     },
681
682     /**
683      * Parses AddressBook entries to return an appropriate DataSource array for YUI.autoComplete
684      */
685     returnDataSource : function(contacts) {
686         var ret = new Array();
687         for(var id in contacts) {
688             if (contacts[id].name) {
689                     var primary = this.getPrimaryAddress(contacts[id]);
690         
691                     ret[ret.length] = contacts[id].name.replace(/<[\/]*b>/gi, '') + " <" + primary + ">";
692                     //ret[ret.length] = contacts[id].name + " <" + primary + ">";
693         
694                     for(var emailIndex in contacts[id].email) {
695                         ret[ret.length] = contacts[id].email[emailIndex].email_address;
696                     }
697             }
698         }
699
700         return ret;
701     },
702
703     /**
704      * Hides address textareas to prevent autocomplete dropdown from being obscured
705      */
706     toggleTextareaHide : function(sType, aArgs) {
707         var textBoxId = aArgs[0]._oTextbox.id; // "addressTo0"
708         var type = "";
709         var idx = -1;
710
711         if(textBoxId.indexOf("addressTO") > -1) {
712             type = "to";
713         } else if(textBoxId.indexOf("addressCC") > -1) {
714             type = "cc";
715         }
716         idx = textBoxId.substr(9);
717
718         // follow through if not BCC
719         if(type != "") {
720             var cc = document.getElementById("addressCC" + idx);
721             var bcc = document.getElementById("addressBCC" + idx);
722
723             switch(type) {
724                 case "to":
725                     cc.style.visibility = 'hidden';
726                 case "cc":
727                     bcc.style.visibility = 'hidden';
728                 break;
729             }
730         }
731     },
732
733     /**
734      * Redisplays the textareas after an address is commited
735      */
736     toggleTextareaShow : function(sType, aArgs) {
737         var textBoxId = aArgs[0]._oTextbox.id; // "addressTo0"
738         var type = "";
739         var idx = -1;
740
741         if(textBoxId.indexOf("addressTO") > -1) {
742             type = "to";
743         } else if(textBoxId.indexOf("addressCC") > -1) {
744             type = "cc";
745         }
746         idx = textBoxId.substr(9);
747
748         // follow through if not BCC
749         if(type != "") {
750             document.getElementById("addressCC" + idx).style.visibility = 'visible';
751             document.getElementById("addressBCC" + idx).style.visibility = 'visible';
752         }
753     }
754 };
755
756 ////    END AUTOCOMPLETE
757 ///////////////////////////////////////////////////////////////////////////////
758
759 ///////////////////////////////////////////////////////////////////////////////
760 ////    COMPOSE & SEND
761 /**
762  * expands the options sidebar
763  */
764 SE.composeLayout = {
765     currentInstanceId : 0,
766     ccHidden : true,
767     bccHidden : true,
768     outboundAccountErrors : null,
769     loadedTinyInstances : {}, //Tracks which tinyMCE editors have initalized with html content.
770
771     showAddressDetails : function(e) {
772         var linkElement = document.getElementById("More"+e.id);
773         var spanElement = document.getElementById("Detail"+e.id);
774         var emailAddressList = e.value;
775         if(e.value.length > 96) 
776         {
777                 var resultArray = SE.composeLayout._getEmailArrayFromString(emailAddressList);
778             var displayArray = [];
779                 for (var i=0; i<resultArray.length; i++)
780                 {
781                     var t_name = resultArray[i].name;
782                     var t_emailAddr = resultArray[i].email_address;
783                     if(t_name == '')
784                        displayArray.push('<br/>&lt;' + t_emailAddr + '&gt;');
785                     else
786                        displayArray.push(t_name + '<br/>&lt;' + t_emailAddr + '&gt;');     
787                 }
788                         
789             var result = displayArray.join('<br/>');
790                 // Display
791             linkElement.style.display = "inline";
792             linkElement.style.height="10px";
793             linkElement.style.overflow="visible";
794             spanElement.innerHTML = result;
795         } 
796         else 
797                 linkElement.style.display = "none";
798         
799         },
800
801    /**
802     *  Given a string of email address, return an array containing the name portion (if available)
803     *  and email portion.
804     */
805     _getEmailArrayFromString : function (emailAddressList){
806       
807         var reg = /@.*?;/g;
808         while ((results = reg.exec(emailAddressList)) != null) 
809         {
810             orignial = results[0];
811             parsedResult = results[0].replace(';', ':::::');
812             emailAddressList = emailAddressList.replace (orignial, parsedResult);
813         }
814
815         reg = /@.*?,/g;
816         while ((results = reg.exec(emailAddressList)) != null) 
817         {
818             orignial = results[0];
819             parsedResult = results[0].replace(',', ':::::');
820             emailAddressList = emailAddressList.replace (orignial, parsedResult);
821         }
822         //Administrator <johndoe@som.com>  ;1@somwhe.com;2@somwherecomplex.com,3@somwherecomplex.com;4@somwherecomplex.com,5@somwherecomplex.com,
823         var emailArr = emailAddressList.split(":::::");
824         var resultsArray = [];
825         var newArr = [];
826         for (var i=0; i<emailArr.length; i++) 
827         {
828             var rposition = emailArr[i].indexOf('<');
829             var lposition = emailArr[i].indexOf('>');
830
831             if(trim(emailArr[i]) != '')
832             {
833                 if(rposition != -1 && lposition != -1)
834                 {
835                     var t_name = emailArr[i].substr(0, rposition-1);
836                     var t_emailAddr = emailArr[i].substr(rposition+1, (lposition-1 - rposition) );
837                     resultsArray.push({'name':t_name, 'email_address': t_emailAddr});
838                 }
839                 else
840                 {
841                     resultsArray.push({'name':'', 'email_address': emailArr[i]});
842                 }
843             }
844         }
845         return resultsArray;  
846     },
847     ///////////////////////////////////////////////////////////////////////////
848     ////    COMPOSE FLOW
849     /**
850      * Prepare bucket DIV and yui-ext tab panels
851      */
852     _0_yui : function() {
853         var idx = this.currentInstanceId;
854
855         var composeTab = new YAHOO.SUGAR.ClosableTab({
856                         label: mod_strings.LNK_NEW_SEND_EMAIL,
857                                 scroll : true,
858                                 content : "<div id='htmleditordiv" + idx + "'/>",
859                                 id : "composeTab" + idx,
860                                 closeMsg: app_strings.LBL_EMAIL_CONFIRM_CLOSE,
861                                 active : true
862         }, SE.innerLayout);
863         SE.innerLayout.addTab(composeTab);
864         
865         // get template engine with template
866         if (!SE.composeLayout.composeTemplate) {
867                 SE.composeLayout.composeTemplate = new YAHOO.SUGAR.Template(SE.templates['compose']);
868         }
869         
870         // create Tab inner layout
871         var composePanel =  this.getComposeLayout();
872         composePanel.getUnitByPosition("right").collapse();
873         composePanel.autoSize();
874        
875     },
876         /**
877      * Generate the quick compose layout
878          * @method getQuickComposeLayout
879          * @param {Pannel} parentPanel Parent pannel
880          * @param {Object} o Options
881          * @return {} none
882          **/
883     getQuickComposeLayout : function (parentPanel,o) {
884          var idx = SE.composeLayout.currentInstanceId;
885
886          //Before rendering the parent pannel we need to initalize the grid layout
887          parentPanel.beforeRenderEvent.subscribe(function() {
888                 
889                 YAHOO.util.Event.onAvailable('htmleditordiv' + idx, function() {
890                         SE.composeLayout._createComposeLayout(idx);
891                         SE.composeLayout[idx].set('height', 350);
892                         SE.composeLayout[idx].render();
893            });
894         });
895
896          //Wait until the Compose Layout has rendered, then add the 
897          //options tab and perform the tiny initialization.
898          parentPanel.renderEvent.subscribe(function() {
899                 
900                 YAHOO.util.Event.onAvailable('htmleditordiv' + idx, function() {
901                 SE.composeLayout._initComposeOptionTabs(idx);
902                 SE.composeLayout[idx].getUnitByPosition("right").collapse();
903                 //Initialize tinyMCE
904                 if (!SUGAR.util.isTouchScreen())
905                     SE.composeLayout._1_tiny();
906                 //Init templates and address book
907                 SE.composeLayout._2_final();
908
909             SE.composeLayout.quickCreateComposePackage(o);      
910
911                 });
912          });
913                 
914             //Check if we have the div override for the shortcut bar
915         if(typeof o.menu_id != 'undefined') {
916                    parentPanel.render(o.menu_id);    
917             } else {
918                    parentPanel.render(document.body); 
919             }
920         
921         return SE.composeLayout[idx];
922     },
923     /**
924      * Fill in all fields into the quick compose layout.
925          * @method quickCreateComposePackage
926          * @param {Object} o Options
927          * @return {} none
928          **/
929     quickCreateComposePackage: function(o)
930     {
931         //If we have a compose package fill in defaults.
932         if (typeof(o.composePackage) != 'undefined')
933         {
934             composePackage = o.composePackage; //Set the compose data object
935             //Hijack this method called by composePackage as it's not need for quick creates.
936             SE.composeLayout.c0_composeNewEmail = function(){};
937             SE.composeLayout.composePackage(); //Fill in defaults.
938         }
939     },
940     getComposeLayout : function() {
941         var idx = SE.composeLayout.currentInstanceId;
942       
943         this._createComposeLayout(idx); 
944         SE.composeLayout[idx].render();
945         this._initComposeOptionTabs(idx);
946         
947         return SE.composeLayout[idx];
948         },
949         
950         /**
951         *       Create the layout manager for the compose window.
952         */
953         _createComposeLayout : function(idx)
954         {
955                 SE.composeLayout[idx] = new YAHOO.widget.Layout('htmleditordiv' + idx, {
956                 parent: SE.complexLayout,
957                 border:true,
958             hideOnLayout: true,
959             height: 400,
960                         units: [{
961                                         position: "center",
962                         animate: false,
963                         scroll: false,
964                         split:true,
965                         body:   
966                                 SE.composeLayout.composeTemplate.exec({
967                                 'app_strings':app_strings,
968                                 'mod_strings':mod_strings,
969                                 'theme': theme,
970                                 'linkbeans_options' : linkBeans,
971                                 'idx' : SE.composeLayout.currentInstanceId
972                                 })
973                     },{
974                         position: "right",
975                                     scroll:true,
976                                     collapse: true,
977                                     collapsed: true,
978                                     resize: true,
979                                     border:true,
980                                     animate: false,
981                                     width:'230',
982                                     body: "<div class='composeRightTabs' id='composeRightTabs" + idx + "'/>",
983                                     titlebar: true,
984                                     split: true,
985                                     header: app_strings.LBL_EMAIL_OPTIONS
986                     }]
987                 });
988         },
989         
990         /**
991         *  Create compose tab which will populate the 'right' container in the compose window.
992         */
993         _initComposeOptionTabs : function(idx) 
994         {
995                 var cTabs = new YAHOO.widget.TabView("composeRightTabs" + idx);
996                 var tab = new YAHOO.widget.Tab({ 
997                                 label: app_strings.LBL_EMAIL_ATTACHMENT,
998                                 scroll : true,
999                                 content : SUGAR.util.getAndRemove("divAttachments" + idx).innerHTML,
1000                                 id : "divAttachments" + idx,
1001                                 active : true
1002                         });
1003         
1004                 tab.layout = SE.composeLayout[idx];
1005
1006            tab.on("activeChange", function(o){ 
1007                         if (o.newValue) {
1008                                 this.layout.getUnitByPosition("right").set("header", app_strings.LBL_EMAIL_ATTACHMENT);
1009                         }
1010                 });
1011
1012                 cTabs.addTab(tab);
1013
1014                 tab = new YAHOO.widget.Tab({ 
1015                                 label: app_strings.LBL_EMAIL_OPTIONS,
1016                                 scroll : true,
1017                                 content : SUGAR.util.getAndRemove("divOptions" + idx).innerHTML,
1018                                 id : "divOptions" + idx,
1019                                 active : false
1020                         });
1021                 
1022                 tab.layout = SE.composeLayout[idx];
1023                 tab.on("activeChange", function(o){ 
1024                         if (o.newValue) {
1025                                 this.layout.getUnitByPosition("right").set("header", app_strings.LBL_EMAIL_OPTIONS);
1026                         }
1027                 });
1028                 cTabs.addTab(tab);
1029         
1030                 SE.composeLayout[idx].autoSize = function() {
1031                         var pEl = this.get("element").parentNode.parentNode.parentNode;
1032                         this.set("height", pEl.clientHeight-30);
1033                         this.render();
1034                 }
1035         
1036                 SE.composeLayout[idx].rightTabs = cTabs;
1037     },
1038     isParentTypeValid : function(idx) {
1039                 var parentTypeValue = document.getElementById('data_parent_type' + idx).value;
1040                 var parentNameValue = document.getElementById('data_parent_name' + idx).value;
1041                 if (trim(parentTypeValue) == ""){
1042                         alert(mod_strings.LBL_ERROR_SELECT_MODULE);
1043                         return false;
1044                 } // if
1045                 return true;
1046     },
1047     
1048     isParentTypeAndNameValid : function(idx) {
1049                 var parentTypeValue = document.getElementById('data_parent_type' + idx).value;
1050                 var parentNameValue = document.getElementById('data_parent_name' + idx).value;
1051                 var parentIdValue = document.getElementById('data_parent_id' + idx).value;
1052                 if ((trim(parentTypeValue) != "" && trim(parentNameValue) == "") || 
1053                         (trim(parentTypeValue) != "" && trim(parentNameValue) != "" && parentIdValue == "")){
1054                                 alert(mod_strings.LBL_ERROR_SELECT_MODULE_SELECT);
1055                         return false;
1056                 } // if
1057                 return true;
1058     },
1059
1060     callopenpopupForEmail2 : function(idx,options) {
1061         
1062         var formName = 'emailCompose' + idx;
1063         
1064         if(typeof(options) != 'undefined' && typeof(options.form_name) != 'undefined')
1065             formName = options.form_name;   
1066             
1067                 var parentTypeValue = document.getElementById('data_parent_type' + idx).value;
1068                 var parentNameValue = document.getElementById('data_parent_name' + idx).value;
1069                 if (!SE.composeLayout.isParentTypeValid(idx)) {
1070                         return;
1071                 } // if
1072                 open_popup(document.getElementById('data_parent_type' + idx).value,600,400,'&tree=ProductsProd',true,false,
1073                 {
1074                         call_back_function:"SE.composeLayout.popupAddEmail",
1075                         form_name:formName,
1076                         field_to_name_array:{
1077                                 id:'data_parent_id' + idx,
1078                                 name:'data_parent_name' + idx,
1079                                 email1:'email1'}
1080                 });     
1081         },    
1082         
1083         popupAddEmail : function(o)
1084         {
1085                 var nameKey = "data_parent_name" + SE.composeLayout.currentInstanceId;
1086                 var data = o.name_to_value_array;
1087                 if (typeof (data[nameKey]) != "undefined" && data[nameKey] != "" 
1088                         && typeof (data["email1"]) != "undefined" && data["email1"] != "" && data["email1"] != "undefined")
1089         {
1090                 var target = Dom.get("addressTO" + SE.composeLayout.currentInstanceId);
1091                 target.value = SE.addressBook.smartAddEmailAddressToComposeField(target.value, data[nameKey] + "<" + data.email1 + ">");
1092         }
1093                 set_return(o);
1094         },
1095     /**
1096      * Prepare TinyMCE
1097      */
1098     _1_tiny : function() {
1099         var idx = SE.composeLayout.currentInstanceId;
1100         var elId = SE.tinyInstances.currentHtmleditor = 'htmleditor' + idx;
1101         SE.tinyInstances[elId] = { };
1102         SE.tinyInstances[elId].ready = false;
1103         var t = tinyMCE.getInstanceById(elId);
1104
1105         if(typeof(t) == 'undefined')  {
1106             tinyMCE.execCommand('mceAddControl', false, elId);
1107             YAHOO.util.Event.onAvailable(elId + "_parent", function() {
1108                 SE.composeLayout.resizeEditor(idx);
1109                 setTimeout("SUGAR.email2.composeLayout.setSignature('" + idx + "')", 1000);
1110             }, this);
1111         }
1112     },
1113     
1114     resizeEditor : function(idx)
1115     {
1116         var cof = Dom.get('composeOverFrame' + idx);
1117         var head = Dom.get('composeHeaderTable' + idx);
1118         var targetHeight = cof.clientHeight - head.clientHeight;
1119         var instance =  tinyMCE.get(SE.tinyInstances.currentHtmleditor);                
1120         
1121         var parentEl = Dom.get(instance.editorId + '_parent');
1122         var toolbar = Dom.getElementsByClassName("mceFirst", "tr", parentEl)[0];
1123         var contentEl  = instance.contentAreaContainer;
1124         var iFrame = contentEl.firstChild;
1125         var tinMceToolbarOffset = 18;
1126         iFrame.style.height = (targetHeight - toolbar.offsetHeight - tinMceToolbarOffset)  + "px";
1127     },
1128
1129     /**
1130      * Initializes d&d, auto-complete, email templates
1131      */
1132     _2_final : function() {
1133         var idx = SE.composeLayout.currentInstanceId;
1134
1135         if(this.emailTemplates) {
1136             this.setComposeOptions(idx);
1137         } else {
1138             //populate email template cache
1139             AjaxObject.target = '';
1140             AjaxObject.startRequest(callbackComposeCache, urlStandard + "&emailUIAction=fillComposeCache");
1141         }
1142
1143         // handle drop targets for addressBook
1144        var to =  new YAHOO.util.DDTarget('addressTO' +idx, 'addressBookDD', {notifyDrop:this.handleDrop});
1145        var cc =  new YAHOO.util.DDTarget('addressCC' +idx, 'addressBookDD', {notifyDrop:this.handleDrop});
1146        var bcc = new YAHOO.util.DDTarget('addressBCC'+idx, 'addressBookDD', {notifyDrop:this.handleDrop});
1147        to.notifyDrop = cc.notifyDrop = bcc.notifyDrop = this.handleDrop;
1148
1149         // auto-complete setup
1150         SE.autoComplete.init(idx);
1151
1152         // set focus on to:
1153         document.getElementById("addressTO" + idx).focus();
1154     },
1155
1156         /**
1157      * hide tinyMCE tool bar if send email as plaintext is checked
1158      */
1159     renderTinyMCEToolBar : function (idx, hide) {
1160         if (hide) {
1161                 document.getElementById('htmleditor' + idx + '_toolbar1').style.display = 'none';
1162         } else {
1163                 document.getElementById('htmleditor' + idx + '_toolbar1').style.display = '';
1164         }
1165     },
1166
1167     c1_composeEmail : function(isReplyForward, retry) {
1168         if (!retry) {
1169             this._0_yui();
1170         }
1171         if (typeof(tinyMCE) == 'undefined' || typeof(tinyMCE.settings) == 'undefined'){
1172             setTimeout("SE.composeLayout.c1_composeEmail(" + isReplyForward + ", true);", 500);
1173         } else {
1174                 this._1_tiny();
1175                 this._2_final();
1176         
1177                 if(isReplyForward) {
1178                     this.replyForwardEmailStage2();
1179                 }
1180         }
1181     },
1182
1183     /**
1184      * takes draft info and prepopulates
1185      */
1186     c0_composeDraft : function() {
1187         this.getNewInstanceId();
1188         inCompose = true;
1189         document.getElementById('_blank').innerHTML = '';
1190         var idx = SE.composeLayout.currentInstanceId;
1191                 SE.composeLayout.draftObject = new Object();
1192                 SE.composeLayout.draftObject.id = idx;
1193                 SE.composeLayout.draftObject.isDraft = true;
1194         SE.composeLayout.currentInstanceId = idx;
1195         SE.tinyInstances.currentHtmleditor = 'htmleditor' + SE.composeLayout.currentInstanceId;
1196         SE.tinyInstances[SE.tinyInstances.currentHtmleditor] = new Object();
1197         SE.tinyInstances[SE.tinyInstances.currentHtmleditor].ready = false;
1198
1199         SE.composeLayout._0_yui();
1200         SE.composeLayout._1_tiny();
1201
1202         // final touches
1203         SE.composeLayout._2_final();
1204
1205         /* Draft-specific final processing. Need a delay to allow Tiny to render before calling setText() */
1206         setTimeout("AjaxObject.handleReplyForwardForDraft(SE.o);", 1000);
1207     },
1208
1209     /**
1210      * Strip & Prep editor hidden fields
1211      */
1212     c0_composeNewEmail : function() {
1213         this.getNewInstanceId();
1214         this.c1_composeEmail(false);
1215     },
1216
1217     /**
1218      * Sends async request to get the compose view.
1219      * Requests come from "reply" or "forwards"
1220      */
1221     c0_replyForwardEmail : function(ieId, uid, mbox, type) {
1222         SE.composeLayout.replyForwardObj = new Object();
1223         SE.composeLayout.replyForwardObj.ieId = ieId;
1224         SE.composeLayout.replyForwardObj.uid = uid;
1225         SE.composeLayout.replyForwardObj.mbox = mbox;
1226         SE.composeLayout.replyForwardObj.type = type;
1227
1228         if(mbox == 'sugar::Emails') {
1229             SE.composeLayout.replyForwardObj.sugarEmail = true;
1230         }
1231
1232         SE.composeLayout.getNewInstanceId();
1233         SE.composeLayout.c1_composeEmail(true);
1234     },
1235     ////    END COMPOSE FLOW
1236     ///////////////////////////////////////////////////////////////////////////
1237
1238     /**
1239      * Called when a contact, email, or mailinglist is dropped
1240      * into one of the compose fields.
1241      */
1242     handleDrop : function (source, event, data, target) {
1243         var nodes;
1244         if (!target) {
1245             target = event.getTarget();
1246             if (data.single) {
1247                 data.nodes = [data.nodes];
1248             }
1249             nodes = data.nodes;
1250         } else {
1251             target = document.getElementById(target);
1252             nodes = data;
1253         }
1254         
1255         if (target.id.indexOf('address') > -1) {
1256             // dropped onto email to/cc/bcc field
1257             for(var i in nodes) {
1258                 var node = nodes[i].getData();
1259                 var email = "";
1260                 if (node[1].indexOf('contact') > -1) {
1261                     email = SE.addressBook.getFormattedAddress(node[0]);
1262                 } else if (node[1].indexOf('address-email') > -1){
1263                     email = node[3].replace(/&nbsp;/gi, '');
1264                     email = email.replace('&lt;', '<').replace('&gt;', '>');
1265                     var tr = source.getTrEl(nodes[i]);
1266                     while (tr && !Dom.hasClass(tr, "address-contact")) {
1267                         tr = source.getPreviousTrEl(tr);
1268                     }
1269                     var CID = source.getRecord(tr).getData()[0];
1270                     var o = SE.addressBook._contactCache[CID];
1271                     var name = new String(o.name);
1272                     var finalName = name.replace(/(<([^>]+)>)/ig, "");
1273                     email = finalName + email;
1274                 }
1275                 target.value = SE.addressBook.smartAddEmailAddressToComposeField(target.value, email);              
1276             }
1277         }
1278     },
1279
1280
1281     /*/////////////////////////////////////////////////////////////////////////////
1282     ///    EMAIL TEMPLATE CODE
1283      */
1284     applyEmailTemplate : function (idx, id) {
1285                 
1286         //bug #20680
1287         var box_title = mod_strings.LBL_EMAILTEMPLATE_MESSAGE_SHOW_TITLE;
1288                 var box_msg = mod_strings.LBL_EMAILTEMPLATE_MESSAGE_SHOW_MSG;
1289                 var box_none_msg = mod_strings.LBL_EMAILTEMPLATE_MESSAGE_CLEAR_MSG;
1290                 //Bug #6224
1291                 var to_addr = document.getElementById('addressTO'+idx);
1292                 if (to_addr.value.search(/[^;,]{6,}[;,][^;,]{6,}/) != -1) {
1293                         box_title = mod_strings.LBL_EMAILTEMPLATE_MESSAGE_WARNING_TITLE;
1294                         box_msg = mod_strings.LBL_EMAILTEMPLATE_MESSAGE_MULTIPLE_RECIPIENTS + '<br /><br />' + box_msg;
1295                 }
1296                 
1297                 // id is selected index of email template drop-down
1298                 if(id == '' || id == "0") {
1299                         YAHOO.SUGAR.MessageBox.show({
1300                    title:box_title,
1301                    msg: box_none_msg,
1302                    type: 'confirm',
1303                    fn: function(btn){
1304                                 if(btn=='no'){return;};                         
1305                                 SUGAR.email2.composeLayout.processNoneResult(idx, id);},
1306                    modal:true,
1307                    scope:this
1308                });
1309                return;
1310                 } 
1311         
1312                 YAHOO.SUGAR.MessageBox.show({
1313            title:box_title,
1314            msg: box_msg,
1315            type: 'confirm',
1316            fn: function(btn){
1317                         if(btn=='no'){return;};
1318                         SUGAR.email2.composeLayout.processResult(idx, id);},
1319            modal:true,
1320            scope:this
1321        });
1322     },
1323     
1324     processNoneResult : function(idx, id) {
1325         var tiny = SE.util.getTiny('htmleditor' + idx);
1326         var tinyHTML = tiny.getContent();
1327         var openTag = '<div><span><span>';
1328         var htmllow = tinyHTML.toLowerCase();
1329         var start = htmllow.indexOf(openTag);
1330                 if (start > -1) {
1331                 tinyHTML = tinyHTML.substr(start);
1332                 tiny.setContent(tinyHTML);
1333                 } else {
1334                 tiny.setContent('');
1335                 }       
1336     },
1337         
1338         processResult : function(idx , id){
1339         call_json_method('EmailTemplates','retrieve','record='+id,'email_template_object', this.appendEmailTemplateJSON);
1340
1341         // get attachments if any
1342         AjaxObject.target = '';
1343         AjaxObject.startRequest(callbackLoadAttachments, urlStandard + "&emailUIAction=getTemplateAttachments&parent_id=" + id);
1344     },
1345
1346     appendEmailTemplateJSON : function() {
1347         var idx = SE.composeLayout.currentInstanceId; // post increment
1348
1349         // query based on template, contact_id0,related_to
1350         //jchi 09/10/2008 refix #7743
1351         if(json_objects['email_template_object']['fields']['subject'] != '' ) { // cn: bug 7743, don't stomp populated Subject Line
1352             document.getElementById('emailSubject' + idx).value = decodeURI(encodeURI(json_objects['email_template_object']['fields']['subject']));
1353         }
1354
1355         var text = decodeURI(encodeURI(json_objects['email_template_object']['fields']['body_html'])).replace(/<BR>/ig, '\n').replace(/<br>/gi, "\n").replace(/&amp;/gi,'&').replace(/&lt;/gi,'<').replace(/&gt;/gi,'>').replace(/&#039;/gi,'\'').replace(/&quot;/gi,'"');
1356
1357         // cn: bug 14361 - text-only templates don't fill compose screen
1358         if(text == '') {
1359             text = decodeURI(encodeURI(json_objects['email_template_object']['fields']['body'])).replace(/<BR>/ig, '\n').replace(/<br>/gi, "\n").replace(/&amp;/gi,'&').replace(/&lt;/gi,'<').replace(/&gt;/gi,'>').replace(/&#039;/gi,'\'').replace(/&quot;/gi,'"').replace(/\r\n/gi,"<br/>");
1360         }
1361
1362         var tiny = SE.util.getTiny('htmleditor' + idx);
1363         var tinyHTML = tiny.getContent();
1364         var openTag = '<div><span><span>';
1365         var closeTag = '</span></span></div>';
1366         var htmllow = tinyHTML.toLowerCase();
1367         var start = htmllow.indexOf(openTag);
1368                 if (start > -1) {
1369                 var htmlPart2 = tinyHTML.substr(start);
1370                 tinyHTML = text + htmlPart2;
1371                 tiny.setContent(tinyHTML);
1372                 } else {
1373                 tiny.setContent(text);
1374                 }
1375     },
1376
1377     /**
1378      * Writes out the signature in the email editor
1379      */
1380     setSignature : function(idx) {
1381         if (!tinyMCE)
1382             return false;
1383         var hide = document.getElementById('setEditor' + idx).checked;
1384         SE.composeLayout.renderTinyMCEToolBar(idx,hide);
1385         //wait for signatures to load before trying to set them
1386         if (!SE.composeLayout.signatures) {
1387             setTimeout("SE.composeLayout.setSignature(" + idx + ");", 1000);
1388                         return;
1389         }
1390             
1391         if(idx) {
1392             var sel = document.getElementById('signatures' + idx);
1393         } else {
1394             var sel = document.getElementById('signature_id');
1395             idx = SE.tinyInstances.currentHtmleditor;
1396         }
1397         
1398         //Ensure that the tinyMCE html has been rendered.
1399         if(typeof(SE.composeLayout.loadedTinyInstances[idx]) != 'undefined' && SE.composeLayout.loadedTinyInstances[idx] == false) {
1400             setTimeout("SE.composeLayout.setSignature(" + idx + ");",1000);
1401                     return;
1402                 }
1403             
1404         var signature = '';
1405
1406         try {
1407             signature = sel.options[sel.selectedIndex].value;
1408         } catch(e) {
1409
1410         }
1411
1412         var openTag = '<div><span><span>';
1413         var closeTag = '</span></span></div>';
1414         var t = SE.util.getTiny('htmleditor' + idx);
1415         //IE 6 Hack
1416         if(typeof(t) != 'undefined')
1417         {
1418             t.contentDocument = t.contentWindow.document;
1419             var html = t.getContent();
1420         }
1421         else
1422             var html = '';
1423             
1424         var htmllow = html.toLowerCase();
1425         var start = htmllow.indexOf(openTag);
1426         var end = htmllow.indexOf(closeTag) + closeTag.length;
1427
1428         // selected "none" - remove signature from email
1429         if(signature == '') {
1430             if (start > -1) {
1431                 var htmlPart1 = html.substr(0, start);
1432                 var htmlPart2 = html.substr(end, html.length);
1433     
1434                 html = htmlPart1 + htmlPart2;
1435                 t.setContent(html);
1436             }
1437             SE.signatures.lastAttemptedLoad = '';
1438             return false;
1439         }
1440
1441         if(!SE.signatures.lastAttemptedLoad) // lazy load place holder
1442             SE.signatures.lastAttemptedLoad = '';
1443
1444         SE.signatures.lastAttemptedLoad = signature;
1445
1446         if(typeof(SE.signatures[signature]) == 'undefined') {
1447             //lazy load
1448             SE.signatures.lastAttemptedLoad = ''; // reset this flag for recursion
1449             SE.signatures.targetInstance = (idx) ? idx : "";
1450             AjaxObject.target = '';
1451             AjaxObject.startRequest(callbackLoadSignature, urlStandard + "&emailUIAction=getSignature&id="+signature);
1452         } else {
1453             var newSignature = this.prepareSignature(SE.signatures[signature]);
1454
1455             // clear out old signature
1456             if(SE.signatures.lastAttemptedLoad && start > -1) {
1457                 var htmlPart1 = html.substr(0, start);
1458                 var htmlPart2 = html.substr(end, html.length);
1459
1460                 html = htmlPart1 + htmlPart2;
1461             }
1462             
1463             // [pre|ap]pend
1464                         start = html.indexOf('<div><hr></div>');
1465             if(SE.userPrefs.signatures.signature_prepend == 'true' && start > -1) {
1466                                 var htmlPart1 = html.substr(0, start);
1467                                 var htmlPart2 = html.substr(start, html.length);
1468                 var newHtml = htmlPart1 + openTag + newSignature + closeTag + htmlPart2;
1469             } else if(SUGAR.email2.userPrefs.signatures.signature_prepend == 'true') {
1470                 var newHtml = '<br/>' + openTag + newSignature + closeTag + html;
1471             } else {
1472                 var newHtml = html + openTag + newSignature + closeTag;
1473             }
1474             //tinyMCE.setContent(newHtml);
1475             t.setContent(newHtml);
1476         }
1477     },
1478
1479     prepareSignature : function(str) {
1480         var signature = new String(str);
1481
1482         signature = signature.replace(/&lt;/gi, '<');
1483         signature = signature.replace(/&gt;/gi, '>');
1484
1485         return signature;
1486     },
1487
1488
1489     showAttachmentPanel : function(idx) {
1490         var east = SE.composeLayout[idx].getUnitByPosition("right");
1491         var tabs = SE.composeLayout[idx].rightTabs;
1492         east.expand();
1493         tabs.set("activeTab", tabs.getTab(0));
1494     },
1495
1496     /**
1497      * expands sidebar and displays options panel
1498      */
1499     showOptionsPanel : function(idx) {
1500         var east = SE.composeLayout[idx].getUnitByPosition("right");
1501         var tabs = SE.composeLayout[idx].rightTabs;
1502         east.expand();
1503         tabs.set("activeTab", tabs.getTab(1));
1504     },
1505
1506     /**
1507      * Selects the Contacts tab
1508      */
1509     showContactsPanel : function() {
1510         SE.complexLayout.regions.west.showPanel("contactsTab");
1511     },
1512
1513     /**
1514      * Generates fields for Select Document
1515      */
1516     addDocumentField : function(idx) {
1517         var basket = document.getElementById('addedDocuments' + idx);
1518         if(basket) {
1519             var index = (basket.childNodes.length / 7) - 1;
1520             if(index < 0)
1521                 index = 0;
1522         } else {
1523             index = 0;
1524         }
1525
1526         var test = document.getElementById('documentId' + idx + index);
1527
1528         while(test != null) {
1529             index++;
1530             test = document.getElementById('documentId' + idx + index);
1531         }
1532         
1533         var documentCup = document.createElement("div");
1534         documentCup.id = 'documentCup' + idx + index;
1535         documentCup.innerHTML = "<input type='hidden' name='document" + idx + index + "' id='document" + idx + index + "' />" +
1536                 // document id field
1537                 "<input type='hidden' name='documentId" + idx + index + "' id='documentId" + idx + index + "' />" +
1538                 // document name field
1539                 "<input value='' size='15' disabled='true' type='text' name='documentName" + idx + index + "' id='documentName" + idx + index + "' />" +
1540                 // select button
1541                 "<button class='button firstChild' type='button' name='documentSelect" + idx + index + "' id='documentSelect" + idx + index + "'" + 
1542                     "onclick='SE.composeLayout.selectDocument(\"" + index + "\");' value='" + app_strings.LBL_EMAIL_SELECT + "'>" +
1543                 "<img src='index.php?entryPoint=getImage&themeName=" + SUGAR.themes.theme_name + "&imageName=id-ff-select.png' ></button>" +
1544                 // remove button
1545                 "<button class='button lastChild' type='button' name='documentRemove" + idx + index + "' id='documentRemove" + idx + index + "'" + 
1546                     "onclick='SE.composeLayout.deleteDocumentField(\"documentCup" + idx + index + "\");' value='" + app_strings.LBL_EMAIL_REMOVE + "'>" + 
1547                  "<img src='index.php?entryPoint=getImage&themeName=" + SUGAR.themes.theme_name + "&imageName=id-ff-clear.png' ></button>" +   
1548                 "<br/>";
1549         
1550         basket.appendChild(documentCup);
1551         //basket.innerHTML += out;
1552         return index;
1553     },
1554
1555     /**
1556      * Makes async call to save a draft of the email
1557      * @param int Instance index
1558      */
1559     saveDraft : function(tinyInstance) {
1560         this.sendEmail(tinyInstance, true);
1561     },
1562
1563     selectDocument : function(target) {
1564         URL="index.php?module=Emails&action=PopupDocuments&to_pdf=true&target=" + target;
1565         windowName = 'selectDocument';
1566         windowFeatures = 'width=800' + ',height=600' + ',resizable=1,scrollbars=1';
1567
1568         win = window.open(URL, windowName, windowFeatures);
1569         if(window.focus) {
1570             // put the focus on the popup if the browser supports the focus() method
1571             win.focus();
1572         }
1573     },
1574
1575     /**
1576      * Modal popup for file attachment dialogue
1577      */
1578     addFileField : function() {
1579         if(!SE.addFileDialog){ // lazy initialize the dialog and only create it once
1580             SE.addFileDialog = new YAHOO.widget.Dialog("addFileDialog", {
1581                 modal:true,
1582                 visible:false,
1583                 fixedcenter:true,
1584                 constraintoviewport: true,
1585                 scroll: true,
1586                 keylisteners : new YAHOO.util.KeyListener(document, { keys:27 }, { 
1587                         fn:function(){SE.addFileDialog.hide();}
1588                 })
1589             });
1590             SE.addFileDialog.setHeader(app_strings.LBL_EMAIL_ATTACHMENTS);
1591             SE.addFileDialog.render();
1592            // SE.addFileDialog.addKeyListener(27, , SE.addFileDialog);
1593         }
1594         Dom.removeClass("addFileDialog", "yui-hidden");
1595         
1596         SE.addFileDialog.show();
1597     },
1598
1599     /**
1600      * Async upload of file to temp dir
1601      */
1602     uploadAttachment : function() {
1603         if(document.getElementById('email_attachment').value != "") {
1604             var formObject = document.getElementById('uploadAttachment');
1605             YAHOO.util.Connect.setForm(formObject, true, true);
1606             AjaxObject.target = '';
1607             AjaxObject.startRequest(callbackUploadAttachment, null);
1608         } else {
1609             alert(app_strings.LBL_EMAIL_ERROR_NO_FILE);
1610         }
1611     },
1612
1613     /**
1614      * Adds a SugarDocument to an outbound email.  Action occurs in a popup window displaying a ListView from the Documents module
1615      * @param string target in focus compose layout
1616      */
1617     setDocument : function(idx, target, documentId, documentName, docRevId) {
1618         // fields are named/id'd [fieldName][instanceId][index]
1619         var addedDocs = document.getElementById("addedDocuments" + idx);
1620         var docId = document.getElementById('documentId' + idx + target);
1621         var docName = document.getElementById('documentName' + idx + target);
1622         var docRevisionId = document.getElementById('document' + idx + target);
1623         docId.value = documentId;
1624         docName.value = documentName;
1625         docRevisionId.value = docRevId;
1626     },
1627
1628     /**
1629      * Removes the bucket div containing the document input fields
1630      */
1631     deleteDocumentField : function(documentCup) {
1632         var f0 = document.getElementById(documentCup);
1633         f0.parentNode.removeChild(f0);
1634     },
1635
1636     /**
1637      * Removes a Template Attachment field
1638      * @param int
1639      * @param int
1640      */
1641     deleteTemplateAttachmentField : function(idx, index) {
1642         // create not-in-array values for removal filtering
1643         var r = document.getElementById("templateAttachmentsRemove" + idx).value;
1644
1645         if(r != "") {
1646             r += "::";
1647         }
1648
1649         r += document.getElementById('templateAttachmentId' + idx + index).value;
1650         document.getElementById("templateAttachmentsRemove" + idx).value = r;
1651
1652         var target = 'templateAttachmentCup' + idx + index;
1653         d =  document.getElementById(target);
1654         d.parentNode.removeChild(d);
1655     },
1656
1657     /**
1658      * Async removal of uploaded temp file
1659      * @param string index Should be a concatenation of idx and index
1660      * @param string
1661      */
1662     deleteUploadAttachment : function(index, file) {
1663         var d = document.getElementById('email_attachment_bucket' + index);
1664         d.parentNode.removeChild(d);
1665
1666         // make async call to delete cached file
1667         AjaxObject.target = '';
1668         AjaxObject.startRequest(null, urlStandard + "&emailUIAction=removeUploadedAttachment&file="+file);
1669     },
1670
1671     /**
1672      * Attaches files coming from Email Templates
1673      */
1674     addTemplateAttachmentField : function(idx) {
1675         // expose title
1676         document.getElementById('templateAttachmentsTitle' + idx).style.display = 'block';
1677
1678         var basket = document.getElementById('addedTemplateAttachments' + idx);
1679
1680         if(basket) {
1681             var index = basket.childNodes.length;
1682             if(index < 0)
1683                 index = 0;
1684         } else {
1685             index = 0;
1686         }
1687
1688         var out = "<div id='templateAttachmentCup" + idx + index + "'>" +
1689                                 // remove button        
1690                                 "<img src='index.php?entryPoint=getImage&themeName=" + SUGAR.themes.theme_name + "&imageName=minus.gif' " +
1691                                         "style='cursor:pointer' align='absmiddle' onclick='SUGAR.email2.composeLayout.deleteTemplateAttachmentField(\"" + 
1692                                         idx + "\",\"" + index + "\");'/>" +
1693                                 // file icon
1694                                 "<img src='index.php?entryPoint=getImage&themeName=" + SUGAR.themes.theme_name + "&imageName=attachment.gif' " + "align='absmiddle' />" +
1695                                 // templateAttachment field
1696                                 "<input type='hidden' value='" + "' name='templateAttachment" + idx + index + "' id='templateAttachment" + idx + index + "' />" +
1697                                 // docId field
1698                                 "<input type='hidden' value='" + "' name='templateAttachmentId" + idx + index + "' id='templateAttachmentId" + idx + index + "' />" +
1699                                 // file name
1700                                 "<span id='templateAttachmentName"  + idx + index + "'" + ">&nbsp;</span>" + 
1701                                 "<br id='br" + index + "></br>" + 
1702                                 "<br id='brdoc" + index + "></br>" + 
1703                         "</div>";
1704                 basket.innerHTML = basket.innerHTML + out;
1705      
1706         return index;
1707     },
1708
1709     /**
1710      * Sends one email via async call
1711      * @param int idx Editor instance ID
1712      * @param bool isDraft
1713      */
1714     sendEmail : function(idx, isDraft) {
1715         
1716         //If the outbound account has an error message associate with it, alert the user and refuse to continue.
1717         var obAccountID = document.getElementById('addressFrom' + idx).value;
1718         
1719         if( typeof(SUGAR.email2.composeLayout.outboundAccountErrors[obAccountID]) != 'undefined' )
1720         {
1721             overlay(app_strings.LBL_EMAIL_ERROR_DESC, SUGAR.email2.composeLayout.outboundAccountErrors[obAccountID], 'alert');
1722             return false;
1723         }
1724         
1725         
1726         var form = document.getElementById('emailCompose' + idx);
1727         var composeOptionsFormName = "composeOptionsForm" + idx;
1728         var t = SE.util.getTiny(SE.tinyInstances.currentHtmleditor);
1729         var html = t.getContent();
1730         var subj = document.getElementById('emailSubject' + idx).value;
1731         var to = trim(document.getElementById('addressTO' + idx).value);
1732         var cc = trim(document.getElementById('addressCC' + idx).value);
1733         var bcc = trim(document.getElementById('addressBCC' + idx).value);
1734         var email_id = document.getElementById('email_id' + idx).value;
1735         var composeType = document.getElementById('composeType').value;
1736         var parent_type = document.getElementById("parent_type").value;
1737         var parent_id = document.getElementById("parent_id").value;
1738         
1739         var el_uid = document.getElementById("uid");
1740         var uid = (el_uid == null) ? '' : el_uid.value;
1741         
1742         var el_ieId = document.getElementById("ieId");
1743         var ieId = (el_ieId == null) ? '' : el_ieId.value;
1744         
1745         var el_mbox = document.getElementById("mbox");
1746         var mbox = (el_mbox == null) ? '' : el_mbox.value;
1747         
1748         if (!isValidEmail(to) || !isValidEmail(cc) || !isValidEmail(bcc)) {
1749                         alert(app_strings.LBL_EMAIL_COMPOSE_INVALID_ADDRESS);
1750                 return false;
1751         }
1752
1753         if (!SE.composeLayout.isParentTypeAndNameValid(idx)) {
1754                 return;
1755         } // if
1756                 var parentTypeValue = document.getElementById('data_parent_type' + idx).value;
1757                 var parentIdValue = document.getElementById('data_parent_id' + idx).value;
1758         parent_id = parentIdValue;
1759         parent_type = parentTypeValue;
1760
1761         var in_draft = (document.getElementById('type' + idx).value == 'draft') ? true : false;
1762         // baseline viability check
1763
1764         if(to == "" && cc == '' && bcc == '' && !isDraft) {
1765             alert(app_strings.LBL_EMAIL_COMPOSE_ERR_NO_RECIPIENTS);
1766             return false;
1767         } else if(subj == '' && !isDraft) {
1768             if(!confirm(app_strings.LBL_EMAIL_COMPOSE_NO_SUBJECT)) {
1769                 return false;
1770             } else {
1771                 subj = app_strings.LBL_EMAIL_COMPOSE_NO_SUBJECT_LITERAL;
1772             }
1773         } else if(html == '' && !isDraft) {
1774             if(!confirm(app_strings.LBL_EMAIL_COMPOSE_NO_BODY)) {
1775                 return false; 
1776             }
1777         }
1778
1779         SE.util.clearHiddenFieldValues('emailCompose' + idx);
1780                 document.getElementById('data_parent_id' + idx).value = parentIdValue;
1781                 var title = (isDraft) ? app_strings.LBL_EMAIL_SAVE_DRAFT : app_strings.LBL_EMAIL_SENDING_EMAIL;
1782         overlay(title, app_strings.LBL_EMAIL_ONE_MOMENT);
1783         html = html.replace(/&lt;/ig, "sugarLessThan");       
1784         html = html.replace(/&gt;/ig, "sugarGreaterThan");
1785         
1786         form.sendDescription.value = html;
1787         form.sendSubject.value = subj;
1788         form.sendTo.value = to;
1789         form.sendCc.value = cc;
1790         form.sendBcc.value = bcc;
1791         form.email_id.value = email_id;
1792         form.composeType.value = composeType;
1793         form.composeLayoutId.value = 'composeLayout' + idx;
1794         form.setEditor.value = (document.getElementById('setEditor' + idx).checked == false) ? 1 : 0;
1795         form.saveToSugar.value = 1;
1796         form.fromAccount.value = document.getElementById('addressFrom' + idx).value;
1797         form.parent_type.value = parent_type;
1798         form.parent_id.value = parent_id;
1799         form.uid.value = uid;
1800         form.ieId.value = ieId;
1801         form.mbox.value = mbox;
1802
1803         // email attachments
1804         var addedFiles = document.getElementById('addedFiles' + idx);
1805         if(addedFiles) {
1806             for(i=0; i<addedFiles.childNodes.length; i++) {
1807                 var bucket = addedFiles.childNodes[i];
1808
1809                 for(j=0; j<bucket.childNodes.length; j++) {
1810                     var node = bucket.childNodes[j];
1811                     var nName = new String(node.name);
1812
1813                     if(node.type == 'hidden' && nName.match(/email_attachment/)) {
1814                         if(form.attachments.value != '') {
1815                             form.attachments.value += "::";
1816                         }
1817                         form.attachments.value += node.value;
1818                     }
1819                 }
1820             }
1821         }
1822
1823         // sugar documents
1824         var addedDocs = document.getElementById('addedDocuments' + idx);
1825         if(addedDocs) {
1826             for(i=0; i<addedDocs.childNodes.length; i++) {
1827                 var cNode = addedDocs.childNodes[i];
1828                 for(j=0; j<cNode.childNodes.length; j++) {
1829                     var node = cNode.childNodes[j];
1830                     var nName = new String(node.name);
1831                     if(node.type == 'hidden' && nName.match(/documentId/)) {
1832                         if(form.documents.value != '') {
1833                             form.documents.value += "::";
1834                         }
1835                         form.documents.value += node.value;
1836                     }
1837                 }
1838             }
1839         }
1840
1841         // template attachments
1842         var addedTemplateAttachments = document.getElementById('addedTemplateAttachments' + idx);
1843         if(addedTemplateAttachments) {
1844             for(i=0; i<addedTemplateAttachments.childNodes.length; i++) {
1845                 var cNode = addedTemplateAttachments.childNodes[i];
1846                 for(j=0; j<cNode.childNodes.length; j++) {
1847                     var node = cNode.childNodes[j];
1848                     var nName = new String(node.name);
1849                     if(node.type == 'hidden' && nName.match(/templateAttachmentId/)) {
1850                         if(form.templateAttachments.value != "") {
1851                             form.templateAttachments.value += "::";
1852                         }
1853                         form.templateAttachments.value += node.value;
1854                     }
1855                 }
1856             }
1857         }
1858
1859         // remove attachments
1860         form.templateAttachmentsRemove.value = document.getElementById("templateAttachmentsRemove" + idx).value;
1861
1862         YAHOO.util.Connect.setForm(form);
1863
1864         AjaxObject.target = 'frameFlex';
1865
1866         // sending a draft email
1867         if(!isDraft && in_draft) {
1868             // remove row
1869             SE.listView.removeRowByUid(email_id);
1870         }
1871
1872         var sendCallback = (isDraft) ? AjaxObject.composeLayout.callback.saveDraft : callbackSendEmail;
1873         var emailUiAction = (isDraft) ? "&emailUIAction=sendEmail&saveDraft=true" : "&emailUIAction=sendEmail";
1874
1875         AjaxObject.startRequest(sendCallback, urlStandard + emailUiAction);
1876     },
1877
1878     /**
1879      * Handles clicking the email address link from a given view
1880      */
1881     composePackage : function() {
1882         if(composePackage != null) {
1883             SE.composeLayout.c0_composeNewEmail();
1884             
1885             
1886             if(composePackage.to_email_addrs) {
1887                 document.getElementById("addressTO" + SE.composeLayout.currentInstanceId).value = composePackage.to_email_addrs;
1888             } // if
1889             if (composePackage.subject != null && composePackage.subject.length > 0) {
1890                 document.getElementById("emailSubject" + SE.composeLayout.currentInstanceId).value = composePackage.subject;
1891             }
1892             
1893             //If no parent fields are set in the composePackage, ensure they are cleared.
1894             var parentFields = ['parent_type','parent_name','parent_id'];
1895             for(var i=0;i<parentFields.length;i++)
1896             {
1897                 if ( typeof(composePackage[parentFields[i]]) == 'undefined' )
1898                     composePackage[parentFields[i]] = "";
1899             }
1900                  
1901             document.getElementById("parent_type").value = composePackage.parent_type;
1902             document.getElementById('data_parent_type' + SE.composeLayout.currentInstanceId).value = composePackage.parent_type;
1903             document.getElementById("parent_id").value = composePackage.parent_id;
1904             document.getElementById('data_parent_id' + SE.composeLayout.currentInstanceId).value = composePackage.parent_id;
1905             document.getElementById('data_parent_name' + SE.composeLayout.currentInstanceId).value = composePackage.parent_name;  
1906                 
1907             if(composePackage.email_id != null && composePackage.email_id.length > 0) {
1908                 document.getElementById("email_id" + SE.composeLayout.currentInstanceId).value = composePackage.email_id;
1909             } // if
1910             if (composePackage.body != null && composePackage.body.length > 0) {
1911                         var tiny = SE.util.getTiny('htmleditor' + SE.composeLayout.currentInstanceId);
1912                         SE.composeLayout.loadedTinyInstances[SE.composeLayout.currentInstanceId] = false;
1913                         setTimeout("SE.composeLayout.setContentOnThisTiny();", 3000);
1914             } // if
1915             if (composePackage.attachments != null) {
1916                                 SE.composeLayout.loadAttachments(composePackage.attachments);                   
1917             } // if
1918             
1919             if (composePackage.fromAccounts != null && composePackage.fromAccounts.status) {
1920                                 var addressFrom = document.getElementById('addressFrom' + SE.composeLayout.currentInstanceId);
1921                         SE.util.emptySelectOptions(addressFrom);
1922                         var fromAccountOpts = composePackage.fromAccounts.data;
1923                         for(i=0; i<fromAccountOpts.length; i++) {
1924                               var key = fromAccountOpts[i].value;
1925                               var display = fromAccountOpts[i].text;
1926                               var opt = new Option(display, key);
1927                               if (fromAccountOpts[i].selected) {
1928                                 opt.selected = true;
1929                               }
1930                               addressFrom.options.add(opt);
1931                         }                       
1932                 
1933             } // if
1934         } // if
1935     },
1936
1937     setContentOnThisTiny : function() {
1938         var tiny = SE.util.getTiny('htmleditor' + SE.composeLayout.currentInstanceId);
1939         var tinyHTML = tiny.getContent();
1940         composePackage.body = decodeURI(encodeURI(composePackage.body));
1941         // cn: bug 14361 - text-only templates don't fill compose screen
1942         if(composePackage.body == '') {
1943             composePackage.body = decodeURI(encodeURI(composePackage.body)).replace(/<BR>/ig, '\n').replace(/<br>/gi, "\n").replace(/&amp;/gi,'&').replace(/&lt;/gi,'<').replace(/&gt;/gi,'>').replace(/&#039;/gi,'\'').replace(/&quot;/gi,'"');
1944         } // if
1945         //Flag determines if we should clear the tiny contents or just append
1946         if (typeof(composePackage.clearBody) != 'undefined' && composePackage.clearBody)
1947             SE.composeLayout.tinyHTML = '';
1948         else
1949             SE.composeLayout.tinyHTML = tinyHTML + composePackage.body;                 
1950         
1951          tiny.setContent(SE.composeLayout.tinyHTML);
1952          //Indicate that the contents has been loaded successfully.
1953          SE.composeLayout.loadedTinyInstances[SE.composeLayout.currentInstanceId] = true;
1954     },
1955     /**
1956      * Confirms closure of a compose screen if "x" is clicked
1957      */
1958     confirmClose : function(panel) {
1959         if(confirm(app_strings.LBL_EMAIL_CONFIRM_CLOSE)) {
1960             SE.composeLayout.closeCompose(panel.id);
1961             return true;
1962         } else {
1963             return false;
1964         }
1965     },
1966
1967     /**
1968      * forces close of a compose screen
1969      */
1970     forceCloseCompose : function(id) {
1971         SE.composeLayout.closeCompose(id);
1972         
1973         // handle flow back to originating view
1974         if(composePackage) {
1975             // check if it's a module we need to return to
1976             if(composePackage.return_module && composePackage.return_action && composePackage.return_id) {
1977                 if(confirm(app_strings.LBL_EMAIL_RETURN_TO_VIEW)) {
1978                     var url = "index.php?module=" + composePackage.return_module + "&action=" + composePackage.return_action + "&record=" + composePackage.return_id;
1979                     window.location = url;
1980                 }
1981             }
1982         }
1983     },
1984
1985     /**
1986      * closes the editor that just sent email
1987      * @param string id ID of composeLayout tab
1988      */
1989     closeCompose : function(id) {
1990         // destroy tinyMCE instance
1991         var idx = id.substr(13, id.length);
1992         var instanceId = "htmleditor" + idx;
1993         tinyMCE.execCommand('mceRemoveControl', false, instanceId);
1994
1995         // nullify DOM and namespace values.
1996         inCompose = false;
1997         SE.composeLayout[idx] = null;
1998         SE.tinyInstances[instanceId] = null;
1999         var tabsArray = SE.innerLayout.get("tabs");
2000         for (i = 0 ; i < tabsArray.length ; i++) {
2001                 if (tabsArray[i].get("id") == ('composeTab' + idx)) {
2002                         tabsArray[i].close();
2003                         break;
2004                 }
2005         }
2006         //SE.innerLayout.getTab(idx).close();
2007     },
2008
2009     /**
2010     *  Enable the quick search for the compose relate field or search tab
2011     */
2012     enableQuickSearchRelate: function(idx,overides){
2013         
2014         if(typeof overides != 'undefined')
2015         {
2016             var newModuleID = overides['moduleSelectField']; //data_parent_type_search
2017             var newModule = document.getElementById(newModuleID).value; 
2018             var formName = overides['formName'];
2019             var fieldName = overides['fieldName'];
2020             var fieldId = overides['fieldId'];
2021             var fullName = formName + "_" + fieldName;
2022             var postBlurFunction = null;
2023         }
2024         else
2025         {
2026             var newModule = document.getElementById('data_parent_type'+idx).value;
2027             var formName = 'emailCompose'+idx;
2028             var fieldName = 'data_parent_name'+idx;
2029             var fieldId = 'data_parent_id'+idx;
2030             var fullName = formName + "_" + fieldName;
2031             var postBlurFunction = "SE.composeLayout.qsAddAddress";
2032         }
2033         
2034         if(typeof sqs_objects == 'undefined')
2035             window['sqs_objects'] = new Array;
2036             
2037         window['sqs_objects'][fullName] = {
2038             form:formName,
2039                         method:"query",
2040                         modules:[newModule],
2041                         group:"or",
2042             field_list:["name","id", "email1"],populate_list:[fieldName,fieldId],required_list:[fieldId],
2043             conditions:[{name:"name",op:"like_custom",end:"%",value:""}],
2044                         post_onblur_function: postBlurFunction,
2045             order:"name","limit":"30","no_match_text":"No Match"};
2046                                 
2047         
2048         if(typeof QSProcessedFieldsArray != 'undefined')
2049                 QSProcessedFieldsArray[fullName] = false;
2050         if (typeof(QSFieldsArray) != 'undefined' && typeof(QSFieldsArray[fullName]) != 'undefined') {
2051                 QSFieldsArray[fullName].destroy();
2052                 delete QSFieldsArray[fullName];
2053         }
2054         if (Dom.get(fullName + "_results")) {
2055                 Dom.get(fullName + "_results").parentNode.removeChild(Dom.get(fullName + "_results"));
2056         }
2057             
2058         enableQS(false);
2059     },
2060     
2061         qsAddAddress : function(o) {
2062         if (o.name != "" && o.email1 != "")
2063         {
2064                 var target = Dom.get("addressTO" + SE.composeLayout.currentInstanceId);
2065                 target.value = SE.addressBook.smartAddEmailAddressToComposeField(target.value, o.name + "<" + o.email1 + ">");
2066         }
2067     },
2068     /**
2069      * Returns a new instance ID, 0-index
2070      */
2071     getNewInstanceId : function() {
2072         this.currentInstanceId = this.currentInstanceId + 1;
2073         return this.currentInstanceId;
2074     },
2075
2076     /**
2077      * Takes an array of objects that contain the filename and GUID of a Note (attachment or Sugar Document) and applies the values to the compose screen.  Valid use-cases are applying an EmailTemplate or resuming a Draft Email.
2078      */
2079     loadAttachments : function(result) {
2080         var idx = SE.composeLayout.currentInstanceId;
2081
2082         if(typeof(result) == 'object') {
2083                 //jchi #20680. Clean the former template attachments;
2084                 var basket = document.getElementById('addedTemplateAttachments' + idx);
2085                         if(basket.innerHTML != ''){
2086                                 confirm(mod_strings.LBL_CHECK_ATTACHMENTS, mod_strings.LBL_HAS_ATTACHMENTS, function(btn){
2087                                         if (btn != 'yes'){
2088                                                 basket.innerHTML = '';
2089                                         }
2090                                 });
2091                         }
2092             for(i in result) {
2093                 if(typeof result[i] == 'object') {
2094                     var index = SE.composeLayout.addTemplateAttachmentField(idx);
2095                     var bean = result[i];
2096                     document.getElementById('templateAttachmentId' + idx + index).value = bean['id'];
2097                     document.getElementById('templateAttachmentName' + idx + index).innerHTML += bean['filename'];
2098                 }
2099             }
2100         }
2101     },
2102
2103     /**
2104      * fills drop-down values for email templates and signatures
2105      */
2106     setComposeOptions : function(idx) {
2107         // send from accounts
2108         var addressFrom = document.getElementById('addressFrom' + idx);
2109         
2110         if (addressFrom.options.length <= 0) {
2111                 SE.util.emptySelectOptions(addressFrom);
2112                 var fromAccountOpts = SE.composeLayout.fromAccounts;
2113                 for (id = 0 ; id < fromAccountOpts.length ; id++) {
2114                       var key = fromAccountOpts[id].value;
2115                       var display = fromAccountOpts[id].text;
2116                       var is_default = false;
2117                       if(key == SUGAR.default_inbound_accnt_id)
2118                         is_default = true;
2119                       var opt = new Option(display, key);
2120                       addressFrom.options.add(opt);
2121                       addressFrom.options[id].selected = is_default; //Safari bug new Option(x,y,true) does not work.
2122                 }
2123         }
2124
2125         // email templates
2126         var et = document.getElementById('email_template' + idx);
2127         SE.util.emptySelectOptions(et);
2128
2129         for(var key in this.emailTemplates) { // iterate through assoc array
2130             var display = this.emailTemplates[key];
2131             var opt = new Option(display, key);
2132             et.options.add(opt);
2133         }
2134
2135         // signatures
2136         var sigs = document.getElementById('signatures' + idx);
2137         SE.util.emptySelectOptions(sigs);
2138
2139         for(var key in this.signatures) { // iterate through assoc array
2140             var display = this.signatures[key];
2141             var opt = new Option(display, key);
2142
2143             if(key == SE.userPrefs.signatures.signature_default) {
2144                 opt.selected = true;
2145             }
2146
2147             sigs.options.add(opt);
2148         }
2149
2150         // html/plain email?
2151         var htmlEmail = document.getElementById('setEditor' + idx);
2152         if(SE.userPrefs.emailSettings.sendPlainText == 1) {
2153             htmlEmail.checked = true;
2154         } else {
2155                 htmlEmail.checked = false;
2156         }
2157
2158         SE.tinyInstances[SE.tinyInstances.currentHtmleditor].ready = true;
2159     },
2160
2161     /**
2162      * After compose screen is rendered, async call to get email body from Sugar
2163      */
2164     replyForwardEmailStage2 : function() {
2165         SE.util.clearHiddenFieldValues('emailUIForm');
2166         overlay(app_strings.LBL_EMAIL_RETRIEVING_MESSAGE, app_strings.LBL_EMAIL_ONE_MOMENT);
2167
2168         var ieId = SE.composeLayout.replyForwardObj.ieId;
2169         var uid = SE.composeLayout.replyForwardObj.uid;
2170         var mbox = SE.composeLayout.replyForwardObj.mbox;
2171         var type = SE.composeLayout.replyForwardObj.type;
2172         var idx = SE.composeLayout.currentInstanceId;
2173
2174         var sugarEmail = (SE.composeLayout.replyForwardObj.sugarEmail) ? '&sugarEmail=true' : "";
2175
2176         document.getElementById('emailSubject' + idx).value = type;
2177         document.getElementById('emailUIAction').value = 'composeEmail';
2178         document.getElementById('composeType').value = type;
2179         document.getElementById('ieId').value = ieId;
2180         document.getElementById('uid').value = uid;
2181         document.getElementById('mbox').value = mbox;
2182                 document.getElementById('setEditor' + idx).checked = SE.userPrefs.emailSettings.sendPlainText == 1 ? true : false;
2183         var formObject = document.getElementById('emailUIForm');
2184         YAHOO.util.Connect.setForm(formObject);
2185
2186         var sendType = type;
2187         AjaxObject.startRequest(callbackReplyForward, urlStandard + "&composeType=" + type + sugarEmail);
2188     },
2189     
2190     /**
2191     *  Show the hidden cc or bcc fields
2192     */
2193     showHiddenAddress: function(addrType,idx){
2194
2195         Dom.removeClass(addrType+"_tr"+idx, "yui-hidden");
2196         Dom.addClass(addrType+"_span"+idx, "yui-hidden");
2197                 Dom.addClass("bcc_cc_sep"+idx, "yui-hidden");
2198                 this[addrType+'Hidden'+idx] = false;
2199                 
2200                 //After bcc or cc is added, move options below last addr field
2201                 Dom.insertAfter("add_addr_options_tr"+idx, 'bcc_tr'+idx);
2202                 
2203                 //If both cc and bcc hidden, remove the empty row containing text.
2204                 if( ( typeof(this['ccHidden'+idx]) != 'undefined' && typeof(this['bccHidden'+idx]) != 'undefined')
2205                            && ( this['ccHidden'+idx]  == false && this['bccHidden'+idx] == false) )
2206                         Dom.addClass("add_addr_options_tr"+idx, "yui-hidden");  
2207                 
2208                 SE.composeLayout.resizeEditor(idx);
2209     },
2210     /**
2211     *  Hide the cc and bcc fields if they were shown.
2212     */
2213     hideHiddenAddresses: function(idx){
2214         
2215         var addrTypes = ['cc','bcc'];
2216         for(var i = 0;i<addrTypes.length;i++)
2217         {
2218             Dom.addClass(addrTypes[i] + "_tr"+idx, "yui-hidden");
2219             Dom.removeClass(addrTypes[i] + "_span"+idx, "yui-hidden");
2220             this[addrTypes[i] + 'Hidden'+idx] = true
2221         }
2222         
2223         Dom.removeClass("bcc_cc_sep"+idx, "yui-hidden");
2224         Dom.removeClass("add_addr_options_tr"+idx, "yui-hidden");
2225         Dom.insertBefore("add_addr_options_tr"+idx, 'bcc_tr'+idx);
2226     }
2227 };
2228
2229 ////    END SE.composeLayout
2230 ///////////////////////////////////////////////////////////////////////////////
2231 ///////////////////////////////////////////////////////////////////////////////
2232 ////    SE.util
2233 SE.util = {
2234     /**
2235      * Cleans serialized UID lists of duplicates
2236      * @param string
2237      * @return string
2238      */
2239     cleanUids : function(str) {
2240         var seen = new Object();
2241         var clean = "";
2242         var arr = new String(str).split(",");
2243
2244         for(var i=0; i<arr.length; i++) {
2245             if(seen[arr[i]]) {
2246                 continue;
2247             }
2248
2249             clean += (clean != "") ? "," : "";
2250             clean += arr[i];
2251             seen[arr[i]] = true;
2252         }
2253
2254         return clean;
2255     },
2256
2257     /**
2258      * Clears hidden field values
2259      * @param string id ID of form element to clear
2260      */
2261     clearHiddenFieldValues : function(id) {
2262         var form = document.getElementById(id);
2263
2264         for(i=0; i<form.elements.length; i++) {
2265             if(form.elements[i].type == 'hidden') {
2266                 var e = form.elements[i];
2267                 if(e.name != 'action' && e.name != 'module' && e.name != 'to_pdf') {
2268                     e.value = '';
2269                 }
2270             }
2271         }
2272     },
2273
2274     /**
2275      * Reduces a SELECT drop-down to 0 items to prepare for new ones
2276      */
2277     emptySelectOptions : function(el) {
2278         if(el) {
2279             for(i=el.childNodes.length - 1; i >= 0; i--) {
2280                 if(el.childNodes[i]) {
2281                     el.removeChild(el.childNodes[i]);
2282                 }
2283             }
2284         }
2285     },
2286
2287     /**
2288      * Returns the MBOX path in the manner php_imap expects:
2289      * ie: INBOX.DEBUG.test
2290      * @param string str Current serialized value, Home.personal.test.INBOX.DEBUG.test
2291      */
2292     generateMboxPath : function(str) {
2293         var ex = str.split("::");
2294
2295         /* we have a serialized MBOX path */
2296         if(ex.length > 1) {
2297             var start = false;
2298             var ret = '';
2299             for(var i=0; i<ex.length; i++) {
2300                 if(ex[i] == 'INBOX') {
2301                     start = true;
2302                 }
2303
2304                 if(start == true) {
2305                     if(ret != "") {
2306                         ret += ".";
2307                     }
2308                     ret += ex[i];
2309                 }
2310             }
2311         } else {
2312             /* we have a Sugar folder GUID - do nothing */
2313             return str;
2314         }
2315
2316         return ret;
2317     },
2318
2319     /**
2320      * returns a SUGAR GUID by navigating the DOM tree a few moves backwards
2321      * @param HTMLElement el
2322      * @return string GUID of found element or empty on failure
2323      */
2324     getGuidFromElement : function(el) {
2325         var GUID = '';
2326         var iterations = 4;
2327         var passedEl = el;
2328
2329         // upwards
2330         for(var i=0; i<iterations; i++) {
2331             if(el) {
2332                 if(el.id.match(SE.reGUID)) {
2333                     return el.id;
2334                 } else {
2335                     el = el.parentNode;
2336                 }
2337             }
2338         }
2339
2340         return GUID;
2341     },
2342
2343     /**
2344      * Returns the ID value for the current in-focus, active panel (in the innerLayout, not complexLayout)
2345      * @return string
2346      */
2347     getPanelId : function() {
2348         return SE.innerLayout.get("activeTab").id ? SE.innerLayout.get("activeTab").id : "Preview";
2349     },
2350     
2351     /**
2352      * wrapper to handle weirdness with IE
2353      * @param string instanceId
2354      * @return tinyMCE Controller object
2355      */
2356     getTiny : function(instanceId) {
2357         if(instanceId == '') {
2358             return null;
2359         }
2360
2361         var t = tinyMCE.getInstanceById(instanceId);
2362
2363         if(this.isIe()) {
2364             this.sleep(200);
2365             YAHOO.util.Event.onContentReady(instanceId, function(t) { return t; });
2366         }
2367         return t;
2368     },
2369
2370     /**
2371      * Simple check for MSIE browser
2372      * @return bool
2373      */
2374     isIe : function() {
2375         var nav = new String(navigator.appVersion);
2376         if(nav.match(/MSIE/)) {
2377             return true;
2378         }
2379         return false;
2380     },
2381
2382     /**
2383      * Recursively removes an element from the DOM
2384      * @param HTMLElement
2385      */
2386     removeElementRecursive : function(el) {
2387         this.emptySelectOptions(el);
2388     },
2389     
2390     /**
2391      * Fakes a sleep
2392      * @param int
2393      */
2394     sleep : function(secs) {
2395         setTimeout("void(0);", secs);
2396     },
2397     
2398     /**
2399      * Converts a <select> element to an Ext.form.combobox
2400      */
2401      convertSelect : function(select) {
2402        alert('in convertSelect');
2403        if (typeof(select) == "string") {
2404            select = document.getElementById(select);
2405        }
2406      },
2407      
2408      findChildNode : function (parent, property, value) {
2409          for (i in parent.children) {
2410                  var child = parent.children[i];
2411                  if (child.data[property] && child.data[property] == value || child[property] && child[property] == value)
2412                          return child;
2413                  var searchChild = SE.util.findChildNode(child, property, value);
2414                  if (searchChild) 
2415                          return searchChild;
2416          }
2417          return false;
2418      },
2419      
2420      cascadeNodes : function (parent, fn, scope, args) {
2421          for (i in parent.children) {
2422                  var child = parent.children[i];
2423                  var s = scope ? scope : child;
2424                  var a = args ? args : child;
2425                  fn.call(s, a);
2426                  SE.util.cascadeNodes(child, fn, scope, args);
2427          }
2428      }
2429 };
2430
2431
2432 ////    END UTIL
2433 ///////////////////////////////////////////////////////////////////////////////
2434
2435
2436 })();//End namespace