]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Emails/javascript/grid.js
Release 6.4.0
[Github/sugarcrm.git] / modules / Emails / javascript / grid.js
1 /*********************************************************************************
2  * SugarCRM Community Edition is a customer relationship management program developed by
3  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
4  * 
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Affero General Public License version 3 as published by the
7  * Free Software Foundation with the addition of the following permission added
8  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
9  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
10  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
11  * 
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
15  * details.
16  * 
17  * You should have received a copy of the GNU Affero General Public License along with
18  * this program; if not, see http://www.gnu.org/licenses or write to the Free
19  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301 USA.
21  * 
22  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
23  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
24  * 
25  * The interactive user interfaces in modified source and object code versions
26  * of this program must display Appropriate Legal Notices, as required under
27  * Section 5 of the GNU Affero General Public License version 3.
28  * 
29  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
30  * these Appropriate Legal Notices must retain the display of the "Powered by
31  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
32  * technical reasons, the Appropriate Legal Notices must display the words
33  * "Powered by SugarCRM".
34  ********************************************************************************/
35
36 function gridInit() {
37         if(SUGAR.email2.grid) {
38                 SUGAR.email2.grid.destroy();
39         }
40         
41         e2Grid = {
42                 init : function() {
43                 
44                         var Ck = YAHOO.util.Cookie;
45                         var widths = [ 10, 10, 150, 250, 175, 125 ];
46
47                         if (Ck.get("EmailGridWidths")) {
48                                 for (var i=0; i < widths.length; i++) {
49                                         widths[i] = Ck.getSub("EmailGridWidths", i+ "", Number);
50                                 }
51                         } else {
52                                 for (var i=0; i < widths.length; i++) {
53                                         Ck.setSub("EmailGridWidths", i + "", widths[i], {expires: SUGAR.email2.nextYear});
54                                 }
55                         }
56                         
57                         // changes "F" to an icon
58                         function flaggedIcon(cell, record, column, value) {
59                                 if(value != "") {
60                                         cell.innerHTML = "<span style='color: #f00; font-weight:bold;'>!</span>";
61                                 }
62                         }
63                         // changes "A" to replied icon
64                         function repliedIcon(cell, record, column, value) {
65                                 if(value != "") {
66                                         cell.innerHTML = "<img src='index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=export.gif' class='image' border='0' width='10' align='absmiddle'>";
67                                 }
68                         }
69                 function attachIcon(cell, record, column, value) {
70                                 if(value == "1") {
71                                         cell.innerHTML = "<img src='index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=attachment.gif' class='image' border='0' width='10' align='absmiddle'>";
72                                 }
73                         }
74
75                         var colModel = 
76                                 [
77                                         {
78                                                 label: "<h2><img src='index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=attachment.gif' class='image' border='0' width='10' align='absmiddle'></h2>",
79                                                 width: 10, 
80                                                 sortable: false, 
81                                                 fixed: true,
82                                                 resizeable: true,
83                                                 formatter: attachIcon,
84                                                 key: 'hasAttach'
85                                         }, 
86                                     {
87                                                 label: "<span style='color: #f00; font-weight:bold;'>!</span>", 
88                                                 width: widths[0], 
89                                                 sortable: true, 
90                                                 fixed: true,
91                                                 resizeable: true,
92                                                 formatter: flaggedIcon,
93                                                 key: 'flagged'
94                                         }, 
95                                         {
96                                                 label: "<img src='index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=export.gif' class='image' border='0' width='10' align='absmiddle'>", 
97                                                 width: widths[1], 
98                                                 sortable: true, 
99                                                 fixed: true,
100                                                 resizeable: true,
101                                                 formatter: repliedIcon,
102                                                 key: 'status'
103                                         },
104                                         {
105                                                 label: app_strings.LBL_EMAIL_FROM, 
106                                                 width: widths[2],
107                                                 sortable: true,
108                                                 resizeable: true,
109                                                 key: 'from'
110                                         }, 
111                                         {
112                                                 label: app_strings.LBL_EMAIL_SUBJECT,
113                                                 width: widths[3], 
114                                                 sortable: true,
115                                                 resizeable: true,
116                                                 key: 'subject'
117                                         }, 
118                                         {
119                                                 label: mod_strings.LBL_LIST_DATE,
120                                                 width: widths[4], 
121                                                 sortable: true,
122                                                 resizeable: true,
123                         key: 'date'
124                                         }, 
125                                         {
126                                                 label: app_strings.LBL_EMAIL_TO,
127                                                 width: widths[5], 
128                                                 sortable: false,
129                                                 resizeable: true,
130                         key: 'to_addrs'
131                                         }, 
132                                         {
133                                                 label: 'uid',
134                                                 hidden: true,
135                         key: 'uid'
136                                         }, 
137                                         {
138                                                 label: 'mbox',
139                                                 hidden: true,
140                         key: 'mbox'
141                                         }, 
142                                         {
143                                                 label: 'ieId',
144                                                 hidden: true,
145                         key: 'ieId'
146                                         }, 
147                                         {       
148                                                 label: 'site_url',
149                                                 hidden: true,
150                         key: 'site_url'
151                                         },
152                                         {       label: 'seen',
153                                                 hidden: true,
154                         key: 'seen'
155                                         },
156                                         {       label: 'type',
157                                                 hidden: true,
158                         key: 'type'
159                                         }
160                                 ];
161                         
162                         var dataModel = new YAHOO.util.DataSource(urlBase + "?", {
163                                 responseType: YAHOO.util.DataSource.TYPE_JSON,
164                                 responseSchema: {
165                                     resultsList: 'Email',
166                             fields: ['flagged', 'status', 'from', 'subject', 'date','to_addrs', 'uid', 'mbox', 'ieId', 'site_url', 'seen', 'type', 'AssignedTo','hasAttach'],
167                             metaFields: {total: 'TotalCount', unread:"UnreadCount", fromCache: "FromCache"}
168                                 }
169                     });
170                         var params = {
171                                         to_pdf : "true",
172                                         module : "Emails",
173                                         action : "EmailUIAjax",
174                                         emailUIAction : "getMessageList",
175                                         mbox : "INBOX",
176                                         ieId : "",
177                                         forceRefresh : "false"
178                         };
179                         if(lazyLoadFolder != null) {
180                                 params['mbox'] = lazyLoadFolder.folder;
181                                 params['ieId'] = lazyLoadFolder.ieId;
182                                 //Check if the folder is a Sugar Folder
183                                 var test = new String(lazyLoadFolder.folder);
184                                 if(test.match(/SUGAR\./)) {
185                                         params['emailUIAction'] = 'getMessageListSugarFolders';
186                                         params['mbox'] = test.substr(6);
187                                 }
188                         }
189                         //dataModel.initPaging(urlBase, SUGAR.email2.userPrefs.emailSettings.showNumInList);
190         
191                         // create the Grid
192                         var grid = SUGAR.email2.grid = new YAHOO.SUGAR.SelectionGrid('emailGrid', colModel, dataModel, {
193                                 MSG_EMPTY: SUGAR.language.get("Emails", "LBL_EMPTY_FOLDER"),
194                                 dynamicData: true,
195                                 paginator: new YAHOO.widget.Paginator({ 
196                                         rowsPerPage:parseInt(SUGAR.email2.userPrefs.emailSettings.showNumInList),  
197                                         containers : ["dt-pag-nav"],
198                                         template: "<div class='pagination'>{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}</div>",
199                                         firstPageLinkLabel:     "<button class='button'><div class='paginator-start'/></button>",
200                                         previousPageLinkLabel:  "<button class='button'><div class='paginator-previous'/></button>",
201                                         nextPageLinkLabel:              "<button class='button'><div class='paginator-next'/></button>",
202                                         lastPageLinkLabel:              "<button class='button'><div class='paginator-end'/></button>"
203                                 }),
204                                 initialRequest:SUGAR.util.paramsToUrl(params),
205                                 width:  "800px",
206                                 height: "400px"
207                         });
208
209                         initRowDD();
210
211                         //Override Paging request construction
212                         grid.set("generateRequest", function(oState, oSelf) {
213                     oState = oState || {pagination:null, sortedBy:null};
214                     var sort = (oState.sortedBy) ? oState.sortedBy.key : oSelf.getColumnSet().keys[5].getKey();
215                     var dir = (oState.sortedBy && oState.sortedBy.dir === YAHOO.widget.DataTable.CLASS_ASC) ? "asc" : "desc";
216                     var startIndex = (oState.pagination) ? oState.pagination.recordOffset : 0;
217                     var results = (oState.pagination) ? oState.pagination.rowsPerPage : null;
218                     // Build the request 
219                     var ret = 
220                             SUGAR.util.paramsToUrl(oSelf.params) + 
221                             "&sort=" + sort +
222                         "&dir=" + dir +
223                         "&start=" + startIndex +
224                         ((results !== null) ? "&limit=" + results : "");
225                     return  ret;
226                 });
227                         
228                         
229                         grid.handleDataReturnPayload = function(oRequest, oResponse, oPayload) { 
230                                 oPayload = oPayload || { };
231                                 
232                                 oPayload.totalRecords = oResponse.meta.total;
233                                 oPayload.unreadRecords = oResponse.meta.unread;
234                                 
235                         var tabObject = SE.innerLayout.get("tabs")[0];
236                         var mboxTitle = "";
237                         if (this.params.mbox != null) {
238                                 mboxTitle = this.params.mbox;
239                         }
240                         var tabtext = mboxTitle + " (" + oResponse.meta.total + " " + app_strings.LBL_EMAIL_MESSAGES + " )";
241                         tabObject.get("labelEl").firstChild.data = tabtext;
242                         
243                         if (SE.tree) {
244                                 var node = SE.tree.getNodeByProperty('id', this.params.ieId) || SE.tree.getNodeByProperty('origText', this.params.mbox);
245                                 if (node) {
246                                         node.data.unseen = oResponse.meta.unread;
247                                         SE.accounts.renderTree();
248                                 }
249                         }
250                                 return oPayload; 
251                         }
252                         
253                         var resize = grid.resizeGrid = function () {
254                                 SUGAR.email2.grid.set("width",  SUGAR.email2.grid.get("element").parentNode.clientWidth + "px");
255                                 SUGAR.email2.grid.set("height", (SUGAR.email2.grid.get("element").parentNode.clientHeight - 47) + "px");
256                         }
257                         grid.convertDDRows = function() {
258                                 var rowEl = this.getFirstTrEl();
259                                 while (rowEl != null) {
260                                         new this.DDRow(this, this.getRecord(rowEl), rowEl);
261                                         rowEl = this.getNextTrEl(rowEl);
262                                 }
263                         }
264                         
265                         
266                         grid.on("columnResizeEvent", function(o) {
267                                 //Find the index of the column
268                                 var colSet = SUGAR.email2.grid.getColumnSet().flat;
269                                 for (var i=0; i < colSet.length; i++) {
270                                         if (o.column == colSet[i]) {
271                                                 //Store it in the cookie
272                                                 Ck.setSub("EmailGridWidths", i + "", o.width, {expires: SUGAR.email2.nextYear});
273                                         }
274                                 }
275                                 //this.resizeGrid();
276                         }, null, grid); 
277                         
278                         grid.on("postRenderEvent", function() {this.convertDDRows()}, null, grid);
279                         grid.on("rowClickEvent", SUGAR.email2.listView.handleClick);  
280                         grid.on("rowDblclickEvent", SUGAR.email2.listView.getEmail);  
281                         grid.render();
282                         SUGAR.email2.listViewLayout.on("render", resize);
283                         resize();
284                         
285                         //Setup the default load parameters
286                         SUGAR.email2.grid.params = params;
287                         
288                         grid.on('postRenderEvent', SUGAR.email2.listView.setEmailListStyles);
289                         dataModel.subscribe("requestEvent", grid.disable, grid, true);
290                         dataModel.subscribe("responseParseEvent", grid.undisable, grid, true);
291                 }
292         };
293         e2Grid.init();
294 };
295
296
297 function initRowDD() {
298         var sg = SUGAR.email2.grid,
299         Dom = YAHOO.util.Dom;
300         sg.DDRow = function(oDataTable, oRecord, elTr) {
301                 if(oDataTable && oRecord && elTr) {
302                         this.ddtable = oDataTable;
303                 this.table = oDataTable.getTableEl();
304                 this.row = oRecord;
305                 this.rowEl = elTr;
306                 this.newIndex = null;
307                 this.init(elTr);
308                 this.initFrame(); // Needed for DDProxy
309                 this.invalidHandleTypes = {};
310             }   
311         };
312         
313         YAHOO.extend(sg.DDRow, YAHOO.util.DDProxy, {
314             _resizeProxy: function() {
315                 this.constructor.superclass._resizeProxy.apply(this, arguments);
316                 var dragEl = this.getDragEl(),
317                     el = this.getEl();
318                 var xy = Dom.getXY(el);
319                 
320                 Dom.setStyle(dragEl, 'height', this.rowEl.offsetHeight + "px");
321                 Dom.setStyle(dragEl, 'width', (parseInt(Dom.getStyle(dragEl, 'width'),10) + 4) + 'px');
322                 Dom.setXY(dragEl, [xy[0] - 100, xy[1] - 20] );
323                 Dom.setStyle(dragEl, 'display', "");
324             },
325             
326             startDrag: function(x, y) { 
327                 //Check if we should be dragging a set of rows rather than just the one.
328                 var selectedRows = this.ddtable.getSelectedRows();
329                 var iSelected = false;
330                 for (var i in selectedRows) {
331                         if (this.rowEl.id == selectedRows[i]) {
332                                 iSelected = true;
333                                 break
334                         }
335                 }
336                 if (iSelected) {
337                         this.rows = [];
338                         for (var i in selectedRows) {
339                                 this.rows[i] = this.ddtable.getRecord(selectedRows[i]);
340                         }
341                 } else {
342                         this.rows = [this.row];
343                         this.ddtable.unselectAllRows();
344                         this.ddtable.selectRow(this.row);
345                 }
346                 
347                 //Initialize the dragable proxy
348                 var dragEl = this.getDragEl(); 
349                 var clickEl = this.getEl(); 
350                 Dom.setStyle(clickEl, "opacity", "0.25"); 
351                 dragEl.innerHTML = "<table><tr>" + clickEl.innerHTML + "</tr></table>"; 
352                 Dom.addClass(dragEl, "yui-dt-liner");
353                 Dom.setStyle(dragEl, "opacity", "0.5"); 
354                 Dom.setStyle(dragEl, "height", (clickEl.clientHeight - 2) + "px");
355                 Dom.setStyle(dragEl, "backgroundColor", Dom.getStyle(clickEl, "backgroundColor")); 
356                     Dom.setStyle(dragEl, "border", "2px solid gray"); 
357             },
358             
359             clickValidator: function(e) {
360                 if (this.row.getData()[0] == " ")
361                         return false;
362                 var target = YAHOO.util.Event.getTarget(e);
363                 return ( this.isValidHandleChild(target) && 
364                                 (this.id == this.handleElId || this.DDM.handleWasClicked(target, this.id)) );
365             },
366             /**
367              * This funciton checks that the target of the drag is a table row in this
368              * DDGroup and simply moves the sourceEL to that location as a preview.
369              */
370             onDragOver: function(ev, id) {
371                 var node = SUGAR.email2.tree.getNodeByElement(Dom.get(id));
372                 if (node && node != this.targetNode) {
373                         this.targetNode = node;
374                         SUGAR.email2.folders.unhighliteAll();
375                         node.highlight();
376                 }
377             },
378             
379             onDragOut: function(e, id) {
380                 if (this.targetNode) {
381                         SUGAR.email2.folders.unhighliteAll();
382                         this.targetNode = false;
383                 }
384             },
385             endDrag: function() {
386                 Dom.setStyle(this.getEl(), "opacity", "");
387                 Dom.setStyle(this.getDragEl(), "display", "none"); 
388                 if (this.targetNode) {
389                         SUGAR.email2.folders.handleDrop(this.rows, this.targetNode);
390                 }
391                 SUGAR.email2.folders.unhighliteAll();
392                 this.rows = null;
393             }
394         });
395 }
396
397 function AddressSearchGridInit() {
398     function moduleIcon(elCell, oRecord, oColumn, oData) {
399         elCell.innerHTML = "<img src='index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=" + oData + ".gif' class='image' border='0' width='16' align='absmiddle'>";
400     };
401     function selectionCheckBox(elCell, oRecord, oColumn, oData) {
402         elCell.innerHTML =  '<input type="checkbox" onclick="SUGAR.email2.addressBook.grid.toggleSelectCheckbox(\'' + oRecord.getId() + '\', this.checked);">';
403     };
404     var checkHeader = '<input type="checkbox" ';
405     if (SUGAR.email2.util.isIe()) {
406         checkHeader += 'style="top:-5px" ';
407     }
408     checkHeader += 'onclick="SUGAR.email2.addressBook.grid.toggleSelectAll(this.checked);">';
409     var colModel = 
410             [{
411                 label: checkHeader,
412             width: 30,
413             formatter: selectionCheckBox,
414             key: 'bean_id'
415         },
416             {
417                 label: mod_strings.LBL_LIST_TYPE,
418                 width: 25,
419                 formatter: moduleIcon,
420                 key: 'bean_module'
421         },
422             {
423                 label: app_strings.LBL_EMAIL_ADDRESS_BOOK_NAME, 
424                 width: 180,
425                 sortable: true,
426                 key: 'name'
427             }, 
428             {
429                 label: app_strings.LBL_EMAIL_ADDRESS_BOOK_EMAIL_ADDR,
430                 width: 300, 
431                 sortable: true,
432                 key: 'email'
433             }];
434     
435     var dataModel = new YAHOO.util.DataSource(urlBase + "?", {
436                 responseType: YAHOO.util.XHRDataSource.TYPE_JSON,
437         responseSchema: {
438             resultsList: 'Person',
439             fields: ['name', 'email', 'bean_id', 'bean_module'],
440                     metaFields: {total: 'TotalCount'}
441         },
442         //enable sorting on the server accross all data
443         remoteSort: true
444     });
445     dataModel.params = {
446                 to_pdf          : true,
447                 module          : "Emails",
448                 action          : "EmailUIAjax",
449                 emailUIAction:"getAddressSearchResults"
450     }
451     var rb = document.getElementById('hasRelatedBean').checked;
452         if (rb) {
453                 var idx = SUGAR.email2.composeLayout.currentInstanceId;
454                 var relatedBeanId = document.getElementById('data_parent_id' + idx).value;
455                 var relatedBeanType = document.getElementById('data_parent_type' + idx).value;
456                 dataModel.params['related_bean_id'] = relatedBeanId;
457                 dataModel.params['related_bean_type'] = relatedBeanType;
458                 dataModel.params['person'] = document.getElementById('input_searchPerson').value;
459         }
460     SUGAR.email2.addressBook.addressBookDataModel = dataModel;
461     
462     var grid = SUGAR.email2.addressBook.grid = new YAHOO.widget.ScrollingDataTable("addrSearchGrid", colModel, dataModel, {
463         MSG_EMPTY: "&nbsp;", //SUGAR.language.get("Emails", "LBL_EMPTY_FOLDER"),
464                 dynamicData: true,
465                 paginator: new YAHOO.widget.Paginator({ 
466                         rowsPerPage: 25,  
467                         containers : ["dt-pag-nav-addressbook"],
468                         template: "<div class='pagination'>{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}</div>",
469                                         firstPageLinkLabel:     "<button class='button'><div class='paginator-start'/></button>",
470                                         previousPageLinkLabel:  "<button class='button'><div class='paginator-previous'/></button>",
471                                         nextPageLinkLabel:              "<button class='button'><div class='paginator-next'/></button>",
472                                         lastPageLinkLabel:              "<button class='button'><div class='paginator-end'/></button>"
473                 }),
474                 initialRequest:SUGAR.util.paramsToUrl(dataModel.params),
475                 width:  "560px",
476                 height: "250px"
477     });
478         //Override Paging request construction
479         grid.set("generateRequest", function(oState, oSelf) {
480         oState = oState || {pagination:null, sortedBy:null};
481         var sort = (oState.sortedBy) ? oState.sortedBy.key : oSelf.getColumnSet().keys[0].getKey();
482         var dir = (oState.sortedBy && oState.sortedBy.dir === YAHOO.widget.DataTable.CLASS_DESC) ? "desc" : "asc";
483         var startIndex = (oState.pagination) ? oState.pagination.recordOffset : 0;
484         var results = (oState.pagination) ? oState.pagination.rowsPerPage : null;
485         // Build the request 
486         var ret = 
487             SUGAR.util.paramsToUrl(oSelf.getDataSource().params) + 
488             "&sort=" + sort + "&dir=" + dir + "&start=" + startIndex +
489             ((results !== null) ? "&limit=" + results : "");
490         return  ret;
491     });
492     
493         grid.handleDataReturnPayload = function(oRequest, oResponse, oPayload) { 
494                 oPayload = oPayload || { };
495                 oPayload.totalRecords = oResponse.meta.total;
496                 return oPayload; 
497         }
498         
499         grid.clickToggleSelect= function(args) {
500                 var isIE = (args.event.target == null);
501                 var targetElement = isIE ? args.event.srcElement : args.event.target;
502                 if(targetElement.type == null || targetElement.type != 'checkbox') {
503                         SUGAR.email2.addressBook.grid.toggleSelect(args.target.id);
504                 }
505         }
506         
507         grid.reSelectRowsOnRender = function (){
508             var rows = SUGAR.email2.addressBook.grid.getRecordSet().getRecords();
509         for (var i = 0; i < rows.length; i++) 
510         {
511                 var emailAddress = rows[i].getData("email");
512             var alreadyAdded = SUGAR.email2.addressBook.doesEmailAdddressExistInResultTable(emailAddress);
513             if(alreadyAdded)
514             {
515                 rows[i].setData("selected",  true);
516                         SUGAR.email2.addressBook.grid.selectRow(rows[i]);
517             }
518             else
519             {
520                 rows[i].setData("selected",  false);
521                 SUGAR.email2.addressBook.grid.unselectRow(rows[i]);
522             }
523         }
524         }
525         grid.subscribe("rowMouseoverEvent", grid.onEventHighlightRow); 
526         grid.subscribe("rowMouseoutEvent", grid.onEventUnhighlightRow); 
527         grid.subscribe("rowClickEvent", grid.clickToggleSelect);
528     grid.subscribe("postRenderEvent", grid.reSelectRowsOnRender);
529     
530     grid.render();
531     dataModel.subscribe("requestEvent", grid.disable, grid, true);
532     dataModel.subscribe("responseParseEvent", grid.undisable, grid, true);
533     
534     grid.toggleSelectCheckbox = function(id,checked){
535         var row = SUGAR.email2.addressBook.grid.getRecord(id);
536         row.setData("checked",checked);
537     };
538     grid.toggleSelect = function(id, checked) {
539         var row = SUGAR.email2.addressBook.grid.getRecord(id);
540         checked = row.getData("selected");
541         if (!checked)
542         {
543             SUGAR.email2.addressBook.grid.selectRow(row);
544             SE.addressBook.insertContactRowToResultTable(id,null)
545         } else 
546         {
547             SUGAR.email2.addressBook.grid.unselectRow(row);
548             SE.addressBook.removeRowFromGridResults(id,row.getData("email"));
549         }
550         row.setData("selected", !checked);
551     };
552     
553     grid.toggleSelectAll = function(checked) {
554         rows = SUGAR.email2.addressBook.grid.getRecordSet().getRecords();
555         for (var i = 0; i < rows.length; i++) {
556                         if (typeof(rows[i]) != "undefined")
557                                 rows[i].setData("checked",  checked);
558         }
559         var checkBoxes = SUGAR.email2.addressBook.grid.get("element").getElementsByTagName('input');
560         for (var i = 0; i < checkBoxes.length; i++) {
561             checkBoxes[i].checked = checked;
562         }
563     };
564     
565     //Initialize the grid result table.
566     AddressSearchResultsGridInit();
567 }
568
569
570
571 /**
572 *  Initalize the results table for the address book selection.
573 *
574 */
575 function AddressSearchResultsGridInit()
576 {
577     
578     /* Full name sort funciton to compare by last name if available */
579     var fullNameSort = function(a, b, desc) {
580         // Deal with empty values
581         if(!YAHOO.lang.isValue(a))
582             return (!YAHOO.lang.isValue(b)) ? 0 : 1;
583         else if(!YAHOO.lang.isValue(b))
584             return -1;
585
586         var aNames = a.getData("name").split(' ');
587         var bNames = b.getData("name").split(' ');
588
589         var aSortField = (aNames.length == 2) ? aNames[1] : a.getData("name");
590         var bSortField = (bNames.length == 2) ? bNames[1] : b.getData("name");
591
592         return YAHOO.util.Sort.compare(aSortField,bSortField, desc);
593
594     };
595     
596     var typeDdOptions = [app_strings.LBL_EMAIL_ADDRESS_BOOK_ADD_TO.replace(/:$/,'') ,
597                          app_strings.LBL_EMAIL_ADDRESS_BOOK_ADD_CC.replace(/:$/,''),
598                          app_strings.LBL_EMAIL_ADDRESS_BOOK_ADD_BCC.replace(/:$/,'')]; 
599               
600     var ColumnDefs = [{key:'type',label:app_strings.LBL_EMAIL_ADDRESS_BOOK_ADRRESS_TYPE, width: 60, sortable: true, editor: new YAHOO.widget.RadioCellEditor({radioOptions:typeDdOptions,disableBtns:true})},
601                      {key:'name',label:app_strings.LBL_EMAIL_ACCOUNTS_NAME,width: 280,sortable: true, sortOptions:{sortFunction:fullNameSort}}];
602      
603      var myDataSource = new YAHOO.util.DataSource([]); 
604          myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY; 
605          myDataSource.responseSchema = { 
606                     fields: ["name","type","email_address","display_email_address","bean_id","idx"] 
607                 };                                                                 
608     
609          var gridResults = SUGAR.email2.addressBook.gridResults = new YAHOO.widget.ScrollingDataTable("addrSearchResultGrid", ColumnDefs, myDataSource, {
610                         width:  "350px",height: "250px", MSG_EMPTY: "&nbsp;"});
611     
612      var highlightEditableCell = function(oArgs) {
613             var elCell = oArgs.target;
614             if(YAHOO.util.Dom.hasClass(elCell, "yui-dt-editable")) {
615                 this.highlightCell(elCell);
616             }
617         };
618       
619      gridResults.subscribe("cellMouseoverEvent", highlightEditableCell);
620      gridResults.subscribe("cellMouseoutEvent", gridResults.onEventUnhighlightCell);
621      gridResults.subscribe("cellClickEvent", gridResults.onEventShowCellEditor);
622      gridResults.subscribe("rowMouseoverEvent", gridResults.onEventHighlightRow); 
623          gridResults.subscribe("rowMouseoutEvent", gridResults.onEventUnhighlightRow); 
624      
625      //Setup the context menus
626      var onContextMenuClick = function(p_sType, p_aArgs, p_myDataTable) { 
627              var task = p_aArgs[1]; 
628              if(task) 
629              { 
630                  var elRow = this.contextEventTarget; 
631                  elRow = p_myDataTable.getTrEl(elRow); 
632          
633                  if(elRow) 
634                  { 
635                      switch(task.index) 
636                      { 
637                          case 0:     
638                              var oRecord = p_myDataTable.getRecord(elRow); 
639                              p_myDataTable.deleteRow(elRow);  
640                              SUGAR.email2.addressBook.grid.reSelectRowsOnRender();    
641                      } 
642                  } 
643              } 
644          };
645      var contextMenu = new YAHOO.widget.ContextMenu("contextmenu", 
646                         {trigger:gridResults.getTbodyEl()}); 
647          contextMenu.addItem(app_strings.LBL_EMAIL_DELETE); 
648          contextMenu.render("addrSearchResultGrid"); 
649          contextMenu.clickEvent.subscribe(onContextMenuClick, gridResults); 
650 }