Added 3grids asp & .net examples. ricoQuery.aspx removed, as all AJAX queries in...
[infodrom/rico3] / minsrc / ricoLiveGrid.js
1 /*
2  *  (c) 2005-2009 Richard Cowin (http://openrico.org)
3  *  (c) 2005-2009 Matt Brown (http://dowdybrown.com)
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
6  *  file except in compliance with the License. You may obtain a copy of the License at
7  *
8  *         http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software distributed under the
11  *  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
12  *  either express or implied. See the License for the specific language governing permissions
13  *  and limitations under the License.
14  */
15
16 if(typeof Rico=='undefined') throw("LiveGrid requires the Rico JavaScript framework");
17
18
19 /** @namespace */
20 if (!Rico.Buffer) Rico.Buffer = {};
21
22 Rico.Buffer.Base = function(dataTable, options) {
23   this.initialize(dataTable, options);
24 }
25 /** @lends Rico.Buffer.Base# */
26 Rico.Buffer.Base.prototype = {
27 /**
28  * @class Defines the static buffer class (no AJAX).
29  * Loads buffer with data that already exists in the document as an HTML table or passed via javascript.
30  * Also serves as a base class for AJAX-enabled buffers.
31  * @constructs
32  */
33   initialize: function(dataTable, options) {
34     this.clear();
35     this.updateInProgress = false;
36     this.lastOffset = 0;
37     this.rcvdRowCount = false;  // true if an eof element was included in the last xml response
38     this.foundRowCount = false; // true if an xml response is ever received with eof true
39     this.totalRows = 0;
40     this.rowcntContent = "";
41     this.rcvdOffset = -1;
42     this.options = {
43       fixedHdrRows     : 0,
44       canFilter        : true,  // does buffer object support filtering?
45       isEncoded        : true,  // is the data received via ajax html encoded?
46       acceptAttr       : []     // attributes that can be copied from original/ajax data (e.g. className, style, id)
47     };
48     Rico.extend(this.options, options || {});
49     if (dataTable) {
50       this.loadRowsFromTable(dataTable,this.options.fixedHdrRows);
51     } else {
52       this.clear();
53     }
54   },
55
56   registerGrid: function(liveGrid) {
57     this.liveGrid = liveGrid;
58   },
59
60   setTotalRows: function( newTotalRows ) {
61     if (typeof(newTotalRows)!='number') newTotalRows=this.size;
62     if (this.totalRows == newTotalRows) return;
63     this.totalRows = newTotalRows;
64     if (!this.liveGrid) return;
65     Rico.log("setTotalRows, newTotalRows="+newTotalRows);
66     switch (this.liveGrid.sizeTo) {
67       case 'data':
68         this.liveGrid.resizeWindow();
69         break;
70       case 'datamax':
71         this.liveGrid.setPageSize(newTotalRows);
72         break;
73       default:
74         this.liveGrid.updateHeightDiv();
75         break;
76     }
77   },
78
79   loadRowsFromTable: function(tableElement,firstRow) {
80     var newRows = new Array();
81     var trs = tableElement.getElementsByTagName("tr");
82     for ( var i=firstRow || 0; i < trs.length; i++ ) {
83       var row = new Array();
84       var cells = trs[i].getElementsByTagName("td");
85       for ( var j=0; j < cells.length ; j++ )
86         row[j]=cells[j].innerHTML;
87       newRows.push( row );
88     }
89     this.loadRows(newRows);
90   },
91
92   loadRowsFromArray: function(array2D) {
93     for ( var i=0; i < array2D.length; i++ ) {
94       for ( var j=0; j < array2D[i].length ; j++ ) {
95         array2D[i][j]=array2D[i][j].toString();
96       }
97     }
98     this.loadRows(array2D);
99   },
100
101   loadRows: function(jstable) {
102     this.baseRows = jstable;
103     this.startPos = 0;
104     this.size = this.baseRows.length;
105   },
106
107   dom2jstable: function(rowsElement) {
108     Rico.log('dom2jstable: encoded='+this.options.isEncoded);
109     var newRows = new Array();
110     var trs = rowsElement.getElementsByTagName("tr");
111     for ( var i=0; i < trs.length; i++ ) {
112       var row = new Array();
113       var cells = trs[i].getElementsByTagName("td");
114       for ( var j=0; j < cells.length ; j++ )
115         row[j]=Rico.getContentAsString(cells[j],this.options.isEncoded);
116       newRows.push( row );
117     }
118     return newRows;
119   },
120
121   dom2jstableAttr: function(rowsElement,firstRow) {
122     var acceptAttr=this.options.acceptAttr;
123     Rico.log("dom2jstableAttr start, # attr="+acceptAttr.length);
124     var newRows = new Array();
125     var trs = rowsElement.getElementsByTagName("tr");
126     for ( var i=firstRow || 0; i < trs.length; i++ ) {
127       var row = new Array();
128       var cells = trs[i].getElementsByTagName("td");
129       for ( var j=0; j < cells.length ; j++ ) {
130         row[j]={};
131         for (var k=0; k<acceptAttr.length; k++)
132           row[j]['_'+acceptAttr[k]]=cells[j].getAttribute(acceptAttr[k]);
133         if (Rico.isIE) row[j]._class=cells[j].getAttribute('className');
134       }
135       newRows.push( row );
136     }
137     Rico.log("dom2jstableAttr end");
138     return newRows;
139   },
140
141   _blankRow: function() {
142     var newRow=[];
143     for (var i=0; i<this.liveGrid.columns.length; i++) {
144       newRow[i]='';
145     }
146     return newRow;
147   },
148
149   deleteRows: function(rowIndex,cnt) {
150     this.baseRows.splice(rowIndex,typeof(cnt)=='number' ? cnt : 1);
151     this.liveGrid.isPartialBlank=true;
152     this.size=this.baseRows.length;
153   },
154
155   insertRow: function(beforeRowIndex) {
156     var r=this._blankRow();
157     this.baseRows.splice(beforeRowIndex,0,r);
158     this.size=this.baseRows.length;
159     this.liveGrid.isPartialBlank=true;
160     if (this.startPos < 0) this.startPos=0;
161     return r;
162   },
163
164   appendRows: function(cnt) {
165     var newRows=[];
166     for (var i=0; i<cnt; i++) {
167       var r=this._blankRow();
168       this.baseRows.push(r);
169       newRows.push(r);
170     }
171     this.size=this.baseRows.length;
172     this.liveGrid.isPartialBlank=true;
173     if (this.startPos < 0) this.startPos=0;
174     return newRows;
175   },
176   
177   sortFunc: function(coltype) {
178     var self=this;
179     switch (coltype) {
180       case 'number': return function(a,b) { return self._sortNumeric(a,b); };
181       case 'control':return function(a,b) { return self._sortControl(a,b); };
182       default:       return function(a,b) { return self._sortAlpha(a,b); };
183     }
184   },
185
186   sortBuffer: function(colnum) {
187     if (!this.baseRows) {
188       this.delayedSortCol=colnum;
189       return;
190     }
191     this.liveGrid.showMsg(Rico.getPhraseById("sorting"));
192     this.sortColumn=colnum;
193     var col=this.liveGrid.columns[colnum];
194     this.getValFunc=col._sortfunc;
195     this.baseRows.sort(this.sortFunc(col.format.type));
196     if (col.getSortDirection()=='DESC') this.baseRows.reverse();
197   },
198   
199   _sortAlpha: function(a,b) {
200     var aa = this.sortColumn<a.length ? Rico.getInnerText(a[this.sortColumn]) : '';
201     var bb = this.sortColumn<b.length ? Rico.getInnerText(b[this.sortColumn]) : '';
202     if (aa==bb) return 0;
203     if (aa<bb) return -1;
204     return 1;
205   },
206
207   _sortNumeric: function(a,b) {
208     var aa = this.sortColumn<a.length ? this.nan2zero(Rico.getInnerText(a[this.sortColumn])) : 0;
209     var bb = this.sortColumn<b.length ? this.nan2zero(Rico.getInnerText(b[this.sortColumn])) : 0;
210     return aa-bb;
211   },
212
213   nan2zero: function(n) {
214     if (typeof(n)=='string') n=parseFloat(n);
215     return isNaN(n) || typeof(n)=='undefined' ? 0 : n;
216   },
217   
218   _sortControl: function(a,b) {
219     var aa = this.sortColumn<a.length ? Rico.getInnerText(a[this.sortColumn]) : '';
220     var bb = this.sortColumn<b.length ? Rico.getInnerText(b[this.sortColumn]) : '';
221     if (this.getValFunc) {
222       aa=this.getValFunc(aa);
223       bb=this.getValFunc(bb);
224     }
225     if (aa==bb) return 0;
226     if (aa<bb) return -1;
227     return 1;
228   },
229
230   clear: function() {
231     this.baseRows = [];
232     this.rows = [];
233     this.startPos = -1;
234     this.size = 0;
235     this.windowPos = 0;
236   },
237
238   isInRange: function(position) {
239     var lastRow=Math.min(this.totalRows, position + this.liveGrid.pageSize);
240     return (position >= this.startPos) && (lastRow <= this.endPos()); // && (this.size != 0);
241   },
242
243   endPos: function() {
244     return this.startPos + this.rows.length;
245   },
246
247   fetch: function(offset) {
248     Rico.log('fetch '+this.liveGrid.tableId+': offset='+offset);
249     this.applyFilters();
250     this.setTotalRows();
251     this.rcvdRowCount = true;
252     this.foundRowCount = true;
253     if (offset < 0) offset=0;
254     this.liveGrid.refreshContents(offset);
255     return;
256   },
257
258 /**
259  * @return a 2D array of buffer data representing the rows that are currently visible on the grid
260  */
261   visibleRows: function() {
262     return this.rows.slice(this.windowStart,this.windowEnd);
263   },
264
265   setWindow: function(startrow, endrow) {
266     this.windowStart = startrow - this.startPos;  // position in the buffer of first visible row
267     Rico.log('setWindow '+this.liveGrid.tableId+': '+startrow+', '+endrow+', newstart='+this.windowStart);
268     this.windowEnd = Math.min(endrow,this.size);  // position in the buffer of last visible row containing data+1
269     this.windowPos = startrow;                    // position in the dataset of first visible row
270   },
271
272 /**
273  * @return true if bufRow is currently visible in the grid
274  */
275   isVisible: function(bufRow) {
276     return bufRow < this.rows.length && bufRow >= this.windowStart && bufRow < this.windowEnd;
277   },
278   
279 /**
280  * takes a window row index and returns the corresponding buffer row index
281  */
282   bufferRow: function(windowRow) {
283     return this.windowStart+windowRow;
284   },
285
286 /**
287  * @return buffer cell at the specified visible row/col index
288  */
289   getWindowCell: function(windowRow,col) {
290     var bufrow=this.bufferRow(windowRow);
291     return this.isVisible(bufrow) && col < this.rows[bufrow].length ? this.rows[bufrow][col] : null;
292   },
293
294   getWindowAttr: function(windowRow,col) {
295     var bufrow=this.bufferRow(windowRow);
296     return this.attr && this.isVisible(bufrow) && col < this.attr[bufrow].length ? this.attr[bufrow][col] : null;
297   },
298
299   getWindowValue: function(windowRow,col) {
300     return this.getWindowCell(windowRow,col);
301   },
302
303   setWindowValue: function(windowRow,col,newval) {
304     var bufrow=this.bufferRow(windowRow);
305     if (bufrow >= this.windowEnd) return false;
306     return this.setValue(bufrow,col,newval);
307   },
308
309   getCell: function(bufRow,col) {
310     return bufRow < this.size ? this.rows[bufRow][col] : null;
311   },
312
313   getValue: function(bufRow,col) {
314     return this.getCell(bufRow,col);
315   },
316
317   setValue: function(bufRow,col,newval,newstyle) {
318     if (bufRow>=this.size) return false;
319     if (!this.rows[bufRow][col]) this.rows[bufRow][col]={};
320     this.rows[bufRow][col]=newval;
321     if (typeof newstyle=='string') this.rows[bufRow][col]._style=newstyle;
322     this.rows[bufRow][col].modified=true;
323     return true;
324   },
325
326   getRows: function(start, count) {
327     var begPos = start - this.startPos;
328     var endPos = Math.min(begPos + count,this.size);
329     var results = new Array();
330     for ( var i=begPos; i < endPos; i++ ) {
331       results.push(this.rows[i]);
332     }
333     return results;
334   },
335
336   applyFilters: function() {
337     var newRows=[],re=[];
338     var r,c,n,i,showRow,filtercnt;
339     var cols=this.liveGrid.columns;
340     for (n=0,filtercnt=0; n<cols.length; n++) {
341       c=cols[n];
342       if (c.filterType == Rico.ColumnConst.UNFILTERED) continue;
343       filtercnt++;
344       if (c.filterOp=='LIKE') re[n]=new RegExp(c.filterValues[0],'i');
345     }
346     Rico.log('applyFilters: # of filters='+filtercnt);
347     if (filtercnt==0) {
348       this.rows = this.baseRows;
349     } else {
350       for (r=0; r<this.baseRows.length; r++) {
351         showRow=true;
352         for (n=0; n<cols.length && showRow; n++) {
353           c=cols[n];
354           if (c.filterType == Rico.ColumnConst.UNFILTERED) continue;
355           switch (c.filterOp) {
356             case 'LIKE':
357               showRow=re[n].test(this.baseRows[r][n]);
358               break;
359             case 'EQ':
360               showRow=this.baseRows[r][n]==c.filterValues[0];
361               break;
362             case 'NE':
363               for (i=0; i<c.filterValues.length && showRow; i++)
364                 showRow=this.baseRows[r][n]!=c.filterValues[i];
365               break;
366             case 'LE':
367               if (c.format.type=='number')
368                 showRow=this.nan2zero(this.baseRows[r][n])<=this.nan2zero(c.filterValues[0]);
369               else
370                 showRow=this.baseRows[r][n]<=c.filterValues[0];
371               break;
372             case 'GE':
373               if (c.format.type=='number')
374                 showRow=this.nan2zero(this.baseRows[r][n])>=this.nan2zero(c.filterValues[0]);
375               else
376                 showRow=this.baseRows[r][n]>=c.filterValues[0];
377               break;
378             case 'NULL':
379               showRow=this.baseRows[r][n]=='';
380               break;
381             case 'NOTNULL':
382               showRow=this.baseRows[r][n]!='';
383               break;
384           }
385         }
386         if (showRow) newRows.push(this.baseRows[r]);
387       }
388       this.rows = newRows;
389     }
390     this.rowcntContent = this.size = this.rows.length;
391   },
392
393   printAll: function() {
394     this.liveGrid.showMsg(Rico.getPhraseById('exportInProgress'));
395     Rico.runLater(10,this,'_printAll');  // allow message to paint
396   },
397
398 /**
399  * Support function for printAll()
400  */
401   _printAll: function() {
402     this.liveGrid.exportStart();
403     this.exportBuffer(this.getRows(0,this.totalRows));
404     this.liveGrid.exportFinish();
405   },
406
407 /**
408  * Copies visible rows to a new window as a simple html table.
409  */
410   printVisible: function() {
411     this.liveGrid.showMsg(Rico.getPhraseById('exportInProgress'));
412     Rico.runLater(10,this,'_printVisible');  // allow message to paint
413   },
414
415   _printVisible: function() {
416     this.liveGrid.exportStart();
417     this.exportBuffer(this.visibleRows());
418     this.liveGrid.exportFinish();
419   },
420
421 /**
422  * Send all rows to print/export window
423  */
424   exportBuffer: function(rows,startPos) {
425     var r,c,v,col,exportText;
426     Rico.log("exportBuffer: "+rows.length+" rows");
427     var exportStyles=this.liveGrid.getExportStyles(this.liveGrid.tbody[0]);
428     var tdstyle=[];
429     var totalcnt=startPos || 0;
430     var cols=this.liveGrid.columns;
431     for (c=0; c<cols.length; c++) {
432       if (cols[c].visible) tdstyle[c]=this.liveGrid.exportStyle(cols[c].cell(0),exportStyles);  // assumes row 0 style applies to all rows
433     }
434     for(r=0; r < rows.length; r++) {
435       exportText='';
436       for (c=0; c<cols.length; c++) {
437         if (!cols[c].visible) continue;
438         col=cols[c];
439         col.expStyle=tdstyle[c];
440         v=col._export(rows[r][c],rows[r]);
441         if (v=='') v='&nbsp;';
442         exportText+="<td style='"+col.expStyle+"'>"+v+"</td>";
443       }
444       this.liveGrid.exportRows.push(exportText);
445       totalcnt++;
446       if (totalcnt % 10 == 0) window.status=Rico.getPhraseById('exportStatus',totalcnt);
447     }
448   }
449
450 };
451
452
453 // Rico.LiveGrid -----------------------------------------------------
454
455 Rico.LiveGrid = function(tableId, buffer, options) {
456   this.initialize(tableId, buffer, options);
457 }
458
459 /** 
460  * @lends Rico.LiveGrid#
461  * @property tableId id string for this grid
462  * @property options the options object passed to the constructor extended with defaults
463  * @property buffer the buffer object containing the data for this grid
464  * @property columns array of {@link Rico.LiveGridColumn} objects
465  */
466 Rico.LiveGrid.prototype = {
467 /**
468  * @class Buffered LiveGrid component
469  * @extends Rico.GridCommon
470  * @constructs
471  */
472   initialize: function( tableId, buffer, options ) {
473     Rico.extend(this, Rico.GridCommon);
474     Rico.extend(this, Rico.LiveGridMethods);
475     this.baseInit();
476     this.tableId = tableId;
477     this.buffer = buffer;
478     this.actionId='_action_'+tableId;
479     Rico.setDebugArea(tableId+"_debugmsgs");    // if used, this should be a textarea
480
481     Rico.extend(this.options, {
482       visibleRows      : -3,    // -1 or 'window'=size grid to client window; -2 or 'data'=size grid to min(window,data); -3 or 'body'=size so body does not have a scrollbar; -4 or 'parent'=size to parent element (e.g. if grid is inside a div)
483       frozenColumns    : 0,
484       offset           : 0,     // first row to be displayed
485       prefetchBuffer   : true,  // load table on page load?
486       minPageRows      : 2,
487       maxPageRows      : 50,
488       canSortDefault   : true,  // can be overridden in the column specs
489       canFilterDefault : buffer.options.canFilter, // can be overridden in the column specs
490       canHideDefault   : true,  // can be overridden in the column specs
491
492       // highlight & selection parameters
493       highlightElem    : 'none',// what gets highlighted/selected (cursorRow, cursorCell, menuRow, menuCell, selection, or none)
494       highlightSection : 3,     // which section gets highlighted (frozen=1, scrolling=2, all=3, none=0)
495       highlightMethod  : 'class', // outline, class, both (outline is less CPU intensive on the client)
496       highlightClass   : Rico.theme.gridHighlightClass || 'ricoLG_selection',
497
498       // export/print parameters
499       maxPrint         : 5000,  // max # of rows that can be printed/exported, 0=disable print/export feature
500
501       // heading parameters
502       headingSort      : 'link', // link: make headings a link that will sort column, hover: make headings a hoverset, none: events on headings are disabled
503       hdrIconsFirst    : true    // true: put sort & filter icons before header text, false: after
504     });
505     // other options:
506     //   sortCol: initial sort column
507
508     var self=this;
509     this.options.sortHandler = function() { self.sortHandler(); };
510     this.options.filterHandler = function() { self.filterHandler(); };
511     this.options.onRefreshComplete = function(firstrow,lastrow) { self.bookmarkHandler(firstrow,lastrow); };
512     this.options.rowOverHandler = Rico.eventHandle(this,'rowMouseOver');
513     this.options.mouseDownHandler = Rico.eventHandle(this,'selectMouseDown');
514     this.options.mouseOverHandler = Rico.eventHandle(this,'selectMouseOver');
515     this.options.mouseUpHandler  = Rico.eventHandle(this,'selectMouseUp');
516     Rico.extend(this.options, options || {});
517
518     switch (typeof this.options.visibleRows) {
519       case 'string':
520         this.sizeTo=this.options.visibleRows;
521         switch (this.options.visibleRows) {
522           case 'data':   this.options.visibleRows=-2; break;
523           case 'body':   this.options.visibleRows=-3; break;
524           case 'parent': this.options.visibleRows=-4; break;
525           case 'datamax':this.options.visibleRows=-5; break;
526           default:       this.options.visibleRows=-1; break;
527         }
528         break;
529       case 'number':
530         switch (this.options.visibleRows) {
531           case -1: this.sizeTo='window'; break;
532           case -2: this.sizeTo='data'; break;
533           case -3: this.sizeTo='body'; break;
534           case -4: this.sizeTo='parent'; break;
535           case -5: this.sizeTo='datamax'; break;
536           default: this.sizeTo='fixed'; break;
537         }
538         break;
539       default:
540         this.sizeTo='body';
541         this.options.visibleRows=-3;
542         break;
543     }
544     this.highlightEnabled=this.options.highlightSection>0;
545     this.pageSize=0;
546     this.createTables();
547     if (this.headerColCnt==0) {
548       alert('ERROR: no columns found in "'+this.tableId+'"');
549       return;
550     }
551     this.createColumnArray('LiveGridColumn');
552     if (this.options.headingSort=='hover')
553       this.createHoverSet();
554
555     this.bookmark=document.getElementById(this.tableId+"_bookmark");
556     this.sizeDivs();
557     var filterUIrow=this.buffer.options.canFilter ? this.options.FilterLocation : false;
558     if (typeof(filterUIrow)=='number' && filterUIrow<0)
559       filterUIrow=this.addHeadingRow('ricoLG_FilterRow');
560     this.createDataCells(this.options.visibleRows);
561     if (this.pageSize == 0) return;
562     this.buffer.registerGrid(this);
563     if (this.buffer.setBufferSize) this.buffer.setBufferSize(this.pageSize);
564     this.scrollTimeout = null;
565     this.lastScrollPos = 0;
566     this.attachMenuEvents();
567
568     this.setSortUI( this.options.sortCol, this.options.sortDir );
569     this.setImages();
570     if (this.listInvisible().length==this.columns.length)
571       this.columns[0].showColumn();
572     this.sizeDivs();
573     this.scrollDiv.style.display="";
574     if (this.buffer.totalRows>0)
575       this.updateHeightDiv();
576     if (this.options.prefetchBuffer) {
577       if (this.bookmark) this.bookmark.innerHTML = Rico.getPhraseById('bookmarkLoading');
578       if (this.options.canFilterDefault && this.options.getQueryParms)
579         this.checkForFilterParms();
580       this.scrollToRow(this.options.offset);
581       this.buffer.fetch(this.options.offset);
582     }
583     if (typeof(filterUIrow)=='number')
584       this.createFilters(filterUIrow);
585     this.scrollEventFunc=Rico.eventHandle(this,'handleScroll');
586     this.wheelEventFunc=Rico.eventHandle(this,'handleWheel');
587     this.wheelEvent=(Rico.isIE || Rico.isOpera || Rico.isWebKit) ? 'mousewheel' : 'DOMMouseScroll';
588     if (this.options.offset && this.options.offset < this.buffer.totalRows)
589       Rico.runLater(50,this,'scrollToRow',this.options.offset);  // Safari requires a delay
590     this.pluginScroll();
591     this.setHorizontalScroll();
592     Rico.log("setHorizontalScroll done");
593     if (this.options.windowResize)
594       Rico.runLater(100,this,'pluginWindowResize');
595     Rico.log("initialize complete for "+this.tableId);
596   }
597 };
598
599
600 Rico.LiveGridMethods = {
601 /** @lends Rico.LiveGrid# */
602
603   createHoverSet: function() {
604     var hdrs=[];
605     for( var c=0; c < this.headerColCnt; c++ ) {
606       if (this.columns[c].sortable) {\r
607         hdrs.push(this.columns[c].hdrCellDiv);
608       }
609     }
610     this.hoverSet = new Rico.HoverSet(hdrs);
611   },
612
613   checkForFilterParms: function() {
614     var s=window.location.search;
615     if (s.charAt(0)=='?') s=s.substring(1);
616     var pairs = s.split('&');
617     for (var i=0; i<pairs.length; i++) {
618       if (pairs[i].match(/^f\[\d+\]/)) {
619         this.buffer.options.requestParameters.push(pairs[i]);
620       }
621     }
622   },
623
624 /**
625  * Refreshes a detail grid from a master grid
626  * @returns row index on master table on success, -1 otherwise
627  */
628   drillDown: function(e,masterColNum,detailColNum) {
629     var cell=Rico.eventElement(e || window.event);
630     cell=Rico.getParentByTagName(cell,'div','ricoLG_cell');
631     if (!cell) return -1;
632     var idx=this.winCellIndex(cell);
633     if (idx.row >= this.buffer.totalRows) return -1
634     this.unhighlight();
635     this.menuIdx=idx;  // ensures selection gets cleared when menu is displayed
636     this.highlight(idx);
637     var drillValue=this.buffer.getWindowCell(idx.row,masterColNum);
638     for (var i=3; i<arguments.length; i++)
639       arguments[i].setDetailFilter(detailColNum,drillValue);
640     return idx.row;
641   },
642
643 /**
644  * set filter on a detail grid that is in a master-detail relationship
645  */
646   setDetailFilter: function(colNumber,filterValue) {
647     var c=this.columns[colNumber];
648     c.format.ColData=filterValue;
649     c.setSystemFilter('EQ',filterValue);
650   },
651
652 /**
653  * Create one table for frozen columns and one for scrolling columns.
654  * Also create div's to contain them.
655  * @returns true on success
656  */
657   createTables: function() {
658     var insertloc,hdrSrc,i;
659     var table = document.getElementById(this.tableId) || document.getElementById(this.tableId+'_outerDiv');
660     if (!table) return false;
661     if (table.tagName.toLowerCase()=='table') {
662       var theads=table.getElementsByTagName("thead");
663       if (theads.length == 1) {
664         Rico.log("createTables: using thead section, id="+this.tableId);
665         if (this.options.PanelNamesOnTabHdr && this.options.panels) {
666           var r=theads[0].insertRow(0);
667           this.insertPanelNames(r, 0, this.options.frozenColumns, 'ricoFrozen');
668           this.insertPanelNames(r, this.options.frozenColumns, this.options.columnSpecs.length);
669         }
670         hdrSrc=theads[0].rows;
671       } else {
672         Rico.log("createTables: using tbody section, id="+this.tableId);
673         hdrSrc=new Array(table.rows[0]);
674       }
675       insertloc=table;
676     } else if (this.options.columnSpecs.length > 0) {
677       if (!table.id.match(/_outerDiv$/)) insertloc=table;
678       Rico.log("createTables: inserting at "+table.tagName+", id="+this.tableId);
679     } else {
680       alert("ERROR!\n\nUnable to initialize '"+this.tableId+"'\n\nLiveGrid terminated");
681       return false;
682     }
683
684     this.createDivs();
685     this.scrollContainer = this.createDiv("scrollContainer",this.structTabLR);
686     this.scrollContainer.appendChild(this.scrollDiv); // move scrollDiv
687     this.scrollTab = this.createDiv("scrollTab",this.scrollContainer);
688     this.shadowDiv  = this.createDiv("shadow",this.scrollDiv);
689     this.shadowDiv.style.direction='ltr';  // avoid FF bug
690     this.scrollDiv.style.display="none";
691     this.scrollDiv.scrollTop=0;
692     if (this.options.highlightMethod!='class') {
693       this.highlightDiv=[];
694       switch (this.options.highlightElem) {
695         case 'menuRow':
696         case 'cursorRow':
697           this.highlightDiv[0] = this.createDiv("highlight",this.outerDiv);
698           this.highlightDiv[0].style.display="none";
699           break;
700         case 'menuCell':
701         case 'cursorCell':
702           for (i=0; i<2; i++) {
703             this.highlightDiv[i] = this.createDiv("highlight",i==0 ? this.frozenTabs : this.scrollTab);
704             this.highlightDiv[i].style.display="none";
705             this.highlightDiv[i].id+=i;
706           }
707           break;
708         case 'selection':
709           // create one div for each side of the rectangle
710           var parentDiv=this.options.highlightSection==1 ? this.frozenTabs : this.scrollTab;
711           for (i=0; i<4; i++) {
712             this.highlightDiv[i] = this.createDiv("highlight",parentDiv);
713             this.highlightDiv[i].style.display="none";
714             this.highlightDiv[i].style.overflow="hidden";
715             this.highlightDiv[i].id+=i;
716             this.highlightDiv[i].style[i % 2==0 ? 'height' : 'width']="0px";
717           }
718           break;
719       }
720     }
721
722     // create new tables
723     for (i=0; i<3; i++) {
724       this.tabs[i] = document.createElement("table");
725       this.tabs[i].className = 'ricoLG_table';
726       this.tabs[i].border=0;
727       this.tabs[i].cellPadding=0;
728       this.tabs[i].cellSpacing=0;
729       this.tabs[i].id = this.tableId+"_tab"+i;
730     }
731     // set headings
732     for (i=0; i<2; i++) {
733       this.thead[i]=this.tabs[i].createTHead();
734       Rico.addClass(this.tabs[i],'ricoLG_top');
735       //this.thead[i].className='ricoLG_top';
736       if (Rico.theme.gridheader) Rico.addClass(this.thead[i],Rico.theme.gridheader);
737     }
738     // set bodies
739     for (i=0; i<2; i++) {
740       this.tbody[i]=Rico.getTBody(this.tabs[i==0?0:2]);
741       this.tbody[i].className='ricoLG_bottom';
742       if (Rico.theme.gridcontent) Rico.addClass(this.tbody[i],Rico.theme.gridcontent);
743       this.tbody[i].insertRow(-1);
744     }
745     this.frozenTabs.appendChild(this.tabs[0]);
746     this.innerDiv.appendChild(this.tabs[1]);
747     this.scrollTab.appendChild(this.tabs[2]);
748     if (insertloc) insertloc.parentNode.insertBefore(this.outerDiv,insertloc);
749     if (hdrSrc) {
750       this.headerColCnt = this.getColumnInfo(hdrSrc);
751       this.loadHdrSrc(hdrSrc);
752     } else {
753       this.createHdr(0,0,this.options.frozenColumns);
754       this.createHdr(1,this.options.frozenColumns,this.options.columnSpecs.length);
755       if (this.options.PanelNamesOnTabHdr && this.options.panels) {
756         this.insertPanelNames(this.thead[0].insertRow(0), 0, this.options.frozenColumns);
757         this.insertPanelNames(this.thead[1].insertRow(0), this.options.frozenColumns, this.options.columnSpecs.length);
758       }
759       for (i=0; i<2; i++)
760         this.headerColCnt = this.getColumnInfo(this.thead[i].rows);
761     }
762     for( var c=0; c < this.headerColCnt; c++ )
763       this.tbody[c<this.options.frozenColumns ? 0 : 1].rows[0].insertCell(-1);
764     if (insertloc) table.parentNode.removeChild(table);
765     Rico.log('createTables end');
766     return true;
767   },
768
769   createDataCells: function(visibleRows) {
770     if (visibleRows < 0) {
771       for (var i=0; i<this.options.minPageRows; i++)
772         this.appendBlankRow();
773       this.sizeDivs();
774       this.autoAppendRows(this.remainingHt());
775     } else {
776       for( var r=0; r < visibleRows; r++ )
777         this.appendBlankRow();
778     }
779     var s=this.options.highlightSection;
780     if (s & 1) this.attachHighlightEvents(this.tbody[0]);
781     if (s & 2) this.attachHighlightEvents(this.tbody[1]);
782   },
783
784 /**
785  * @param colnum column number
786  * @return id string for a filter element
787  */
788   filterId: function(colnum) {
789     return 'RicoFilter_'+this.tableId+'_'+colnum;
790   },
791
792 /**
793  * Create filter elements in heading
794  * Reads this.columns[].filterUI to determine type of filter element for each column (t=text box, s=select list, c=custom)
795  * @param r heading row where filter elements will be placed
796  */
797   createFilters: function(r) {
798     for( var c=0; c < this.headerColCnt; c++ ) {
799       var col=this.columns[c];
800       var fmt=col.format;
801       if (typeof fmt.filterUI!='string') continue;
802       var cell=this.hdrCells[r][c].cell;
803       var field,name=this.filterId(c);\r
804       var divs=cell.getElementsByTagName('div');
805       // copy text alignment from data cell
806       var align=Rico.getStyle(this.cell(0,c),'textAlign');
807       divs[1].style.textAlign=align;
808       switch (fmt.filterUI.charAt(0)) {
809         case 't':
810           // text field
811           field=Rico.createFormField(divs[1],'input','text',name,name);
812           var size=fmt.filterUI.match(/\d+/);
813           field.maxLength=fmt.Length || 50;\r
814           field.size=size ? parseInt(size,10) : 10;
815           divs[1].appendChild(Rico.clearButton(Rico.eventHandle(col,'filterClear')));
816           if (col.filterType==Rico.ColumnConst.USERFILTER && col.filterOp=='LIKE') {
817             var v=col.filterValues[0];
818             if (v.charAt(0)=='*') v=v.substr(1);
819             if (v.slice(-1)=='*') v=v.slice(0,-1);
820             field.value=v;
821             col.lastKeyFilter=v;
822           }
823           Rico.eventBind(field,'keyup',Rico.eventHandle(col,'filterKeypress'),false);
824           col.filterField=field;\r
825           break;\r
826         case 'm':
827           // multi-select
828         case 's':
829           // drop-down select
830           field=Rico.createFormField(divs[1],'select',null,name);\r
831           Rico.addSelectOption(field,this.options.FilterAllToken,Rico.getPhraseById("filterAll"));\r
832           col.filterField=field;
833           var options={};\r
834           Rico.extend(options, this.buffer.ajaxOptions);
835           var colnum=typeof(fmt.filterCol)=='number' ? fmt.filterCol : c;
836           options.parameters = {id: this.tableId, distinct:colnum};
837           options.parameters[this.actionId]="query";
838           options.onComplete = this.filterValuesUpdateFunc(c);
839           new Rico.ajaxRequest(this.buffer.dataSource, options);
840           break;\r
841         case 'c':
842           // custom
843           if (typeof col._createFilters == 'function')
844             col._createFilters(divs[1], name);
845           break;
846       }
847     }
848     this.initFilterImage(r);
849   },
850   
851   filterValuesUpdateFunc: function(colnum) {
852     var self=this;
853     return function (request) { self.filterValuesUpdate(colnum,request); };
854   },
855
856 /**
857  * update select list filter with values in AJAX response
858  * @returns true on success
859  */
860   filterValuesUpdate: function(colnum,request) {
861     var response = request.responseXML.getElementsByTagName("ajax-response");
862     Rico.log("filterValuesUpdate: "+request.status);
863     if (response == null || response.length != 1) return false;
864     response=response[0];
865     var error = response.getElementsByTagName('error');
866     if (error.length > 0) {
867       Rico.log("Data provider returned an error:\n"+Rico.getContentAsString(error[0],this.buffer.isEncoded));
868       alert(Rico.getPhraseById("requestError",Rico.getContentAsString(error[0],this.buffer.isEncoded)));
869       return false;
870     }\r
871     response=response.getElementsByTagName('response')[0];\r
872     var rowsElement = response.getElementsByTagName('rows')[0];\r
873     var col=this.columns[parseInt(colnum,10)];
874     var rows = this.buffer.dom2jstable(rowsElement);\r
875     var c,opt,v;
876     if (col.filterType==Rico.ColumnConst.USERFILTER && col.filterOp=='EQ') v=col.filterValues[0];
877     Rico.log('filterValuesUpdate: col='+colnum+' rows='+rows.length);
878     switch (col.format.filterUI.charAt(0)) {
879       case 'm':
880         // multi-select
881         col.mFilter = document.body.appendChild(document.createElement("div"));
882         col.mFilter.className = 'ricoLG_mFilter'
883         Rico.hide(col.mFilter);
884         var contentDiv = col.mFilter.appendChild(document.createElement("div"));
885         contentDiv.className = 'ricoLG_mFilter_content'
886         var buttonDiv = col.mFilter.appendChild(document.createElement("div"));
887         buttonDiv.className = 'ricoLG_mFilter_button'
888         col.mFilterButton=buttonDiv.appendChild(document.createElement("button"));
889         col.mFilterButton.innerHTML=Rico.getPhraseById("ok");
890         var eventName=Rico.isWebKit ? 'mousedown' : 'click';
891         Rico.eventBind(col.filterField,eventName,Rico.eventHandle(col,'mFilterSelectClick'));
892         Rico.eventBind(col.mFilterButton,'click',Rico.eventHandle(col,'mFilterFinish'));
893         //col.filterField.options[0].text=$('AllLabel').innerHTML;
894         tab = contentDiv.appendChild(document.createElement("table"));
895         tab.border=0;
896         tab.cellPadding=2;
897         tab.cellSpacing=0;
898         //tbody=(tab.tBodies.length==0) ? tab.appendChild(document.createElement("tbody")) : tab.tBodies[0];
899         var baseId=this.filterId(colnum)+'_';
900         this.createMFilterItem(tab,this.options.FilterAllToken,Rico.getPhraseById("filterAll"),baseId+'all',Rico.eventHandle(col,'mFilterAllClick'));
901         var handle=Rico.eventHandle(col,'mFilterOtherClick')
902         for (var i=0; i<rows.length; i++) {
903           if (rows[i].length>0) {
904             c=rows[i][0];
905             this.createMFilterItem(tab,c,c || Rico.getPhraseById("filterBlank"),baseId+i,handle);
906           }
907         }
908         col.mFilterInputs=contentDiv.getElementsByTagName('input');
909         col.mFilterLabels=contentDiv.getElementsByTagName('label');
910         col.mFilterFocus=col.mFilterInputs.length ? col.mFilterInputs[0] : col.mFilterButton;
911         break;
912
913       case 's':
914         // drop-down select
915         for (var i=0; i<rows.length; i++) {
916           if (rows[i].length>0) {
917             c=rows[i][0];
918             opt=Rico.addSelectOption(col.filterField,c,c || Rico.getPhraseById("filterBlank"));
919             if (col.filterType==Rico.ColumnConst.USERFILTER && c==v) opt.selected=true;
920           }
921         }
922         Rico.eventBind(col.filterField,'change',Rico.eventHandle(col,'filterChange'));
923         break;
924     }
925     return true;\r
926   },
927   
928   createMFilterItem: function(table,code,description,id,eventHandle) {
929     var tr=table.insertRow(-1);
930     tr.vAlign='top';
931     if (tr.rowIndex % 2 == 1) tr.className='ricoLG_mFilter_oddrow';
932     var td1=tr.insertCell(-1)
933     var td2=tr.insertCell(-1)
934     var field=Rico.createFormField(td1,'input','checkbox',id);
935     field.value=code;
936     field.checked=true;
937     var label = td2.appendChild(document.createElement("label"));
938     label.htmlFor = id;
939     label.innerHTML=description;
940     Rico.eventBind(field,'click',eventHandle);
941   },
942
943   unplugHighlightEvents: function() {
944     var s=this.options.highlightSection;
945     if (s & 1) this.detachHighlightEvents(this.tbody[0]);
946     if (s & 2) this.detachHighlightEvents(this.tbody[1]);
947   },
948
949 /**
950  * place panel names on first row of grid header (used by LiveGridForms)
951  */
952   insertPanelNames: function(r,start,limit,cellClass) {
953     Rico.log('insertPanelNames: start='+start+' limit='+limit);
954     r.className='ricoLG_hdg';
955     var lastIdx=-1, span, newCell=null, spanIdx=0;
956     for( var c=start; c < limit; c++ ) {
957       if (lastIdx == this.options.columnSpecs[c].panelIdx) {
958         span++;
959       } else {
960         if (newCell) newCell.colSpan=span;
961         newCell = r.insertCell(-1);
962         if (cellClass) newCell.className=cellClass;
963         span=1;
964         lastIdx=this.options.columnSpecs[c].panelIdx;
965         newCell.innerHTML=this.options.panels[lastIdx];
966       }
967     }
968     if (newCell) newCell.colSpan=span;
969   },
970
971 /**
972  * create grid header for table i (if none was provided)
973  */
974   createHdr: function(i,start,limit) {
975     Rico.log('createHdr: i='+i+' start='+start+' limit='+limit);
976     var mainRow = this.thead[i].insertRow(-1);
977     mainRow.id=this.tableId+'_tab'+i+'h_main';
978     mainRow.className='ricoLG_hdg';
979     for( var c=start; c < limit; c++ ) {
980       var newCell = mainRow.insertCell(-1);
981       newCell.innerHTML=this.options.columnSpecs[c].Hdg;
982     }
983   },
984
985 /**
986  * move header cells in original table to grid
987  */
988   loadHdrSrc: function(hdrSrc) {
989     var i,h,c,r,newrow,cells;
990     Rico.log('loadHdrSrc start');
991     for (i=0; i<2; i++) {
992       for (r=0; r<hdrSrc.length; r++) {
993         newrow = this.thead[i].insertRow(-1);
994         newrow.className='ricoLG_hdg '+this.tableId+'_hdg'+r;
995       }
996     }
997     if (hdrSrc.length==1) {
998       cells=hdrSrc[0].cells;
999       for (c=0; cells.length > 0; c++)
1000         this.thead[c<this.options.frozenColumns ? 0 : 1].rows[0].appendChild(cells[0]);
1001     } else {
1002       for (r=0; r<hdrSrc.length; r++) {
1003         cells=hdrSrc[r].cells;
1004         for (c=0,h=0; cells.length > 0; c++) {
1005           if (cells[0].className=='ricoFrozen') {
1006             if (r==this.headerRowIdx) this.options.frozenColumns=c+1;
1007           } else {
1008             h=1;
1009           }
1010           this.thead[h].rows[r].appendChild(cells[0]);
1011         }
1012       }
1013     }
1014     Rico.log('loadHdrSrc end');
1015   },
1016
1017 /**
1018  * Size div elements
1019  */
1020   sizeDivs: function() {
1021     Rico.log('sizeDivs: '+this.tableId);
1022     //this.cancelMenu();
1023     this.unhighlight();
1024     this.baseSizeDivs();
1025     var firstVisible=this.firstVisible();
1026     if (this.pageSize == 0 || firstVisible < 0) return;
1027     var totRowHt=this.columns[firstVisible].dataColDiv.offsetHeight;
1028     this.rowHeight = Math.round(totRowHt/this.pageSize);
1029     var scrHt=this.dataHt;
1030     if (this.scrTabWi0 == this.scrTabWi) {
1031       // no scrolling columns - horizontal scroll bar not needed
1032       this.innerDiv.style.height=(this.hdrHt+1)+'px';
1033       this.scrollDiv.style.overflowX='hidden';
1034     } else {
1035       this.scrollDiv.style.overflowX='scroll';
1036       scrHt+=this.options.scrollBarWidth;
1037     }
1038     this.scrollDiv.style.height=scrHt+'px';
1039     this.innerDiv.style.width=(this.scrWi)+'px';
1040     this.scrollTab.style.width=(this.scrWi-this.options.scrollBarWidth)+'px';
1041     //this.resizeDiv.style.height=this.frozenTabs.style.height=this.innerDiv.style.height=(this.hdrHt+this.dataHt+1)+'px';
1042     this.resizeDiv.style.height=(this.hdrHt+this.dataHt+1)+'px';
1043     Rico.log('sizeDivs scrHt='+scrHt+' innerHt='+this.innerDiv.style.height+' rowHt='+this.rowHeight+' pageSize='+this.pageSize);
1044     var pad=(this.scrWi-this.scrTabWi < this.options.scrollBarWidth) ? 2 : 0;
1045     this.shadowDiv.style.width=(this.scrTabWi+pad)+'px';
1046     this.outerDiv.style.height=(this.hdrHt+scrHt)+'px';
1047     this.setHorizontalScroll();
1048   },
1049
1050   setHorizontalScroll: function() {
1051     var newLeft=(-this.scrollDiv.scrollLeft)+'px';
1052     this.tabs[1].style.marginLeft=newLeft;
1053     this.tabs[2].style.marginLeft=newLeft;
1054   },
1055
1056   remainingHt: function() {
1057     var tabHt=this.outerDiv.offsetHeight;
1058     var winHt=Rico.windowHeight();
1059     var margin=Rico.isIE ? 15 : 10;
1060     // if there is a horizontal scrollbar take it into account
1061     if (!Rico.isIE && window.frameElement && window.frameElement.scrolling=='yes' && this.sizeTo!='parent') margin+=this.options.scrollBarWidth;
1062     switch (this.sizeTo) {
1063       case 'window':
1064         var divTop=Rico.cumulativeOffset(this.outerDiv).top;
1065         Rico.log("remainingHt/window, winHt="+winHt+' tabHt='+tabHt+' gridY='+divTop);
1066         return winHt-divTop-tabHt-margin;  // allow for scrollbar and some margin
1067       case 'parent':
1068         var offset=this.offsetFromParent(this.outerDiv);
1069         if (Rico.isIE) Rico.hide(this.outerDiv);
1070         var parentHt=this.outerDiv.parentNode.clientHeight;
1071         if (Rico.isIE) Rico.show(this.outerDiv);
1072         Rico.log("remainingHt/parent, parentHt="+parentHt+' offset='+offset+' tabHt='+tabHt);
1073         return parentHt-tabHt-offset-margin;
1074       case 'data':
1075       case 'body':
1076         var bodyHt=Rico.isIE ? document.body.scrollHeight : document.body.offsetHeight;
1077         //alert("remainingHt\n document.height="+document.height+"\n body.offsetHeight="+document.body.offsetHeight+"\n body.scrollHeight="+document.body.scrollHeight+"\n documentElement.scrollHeight="+document.documentElement.scrollHeight);
1078         var remHt=winHt-bodyHt-margin;
1079         if (!Rico.isWebKit) remHt-=this.options.scrollBarWidth;
1080         Rico.log("remainingHt, winHt="+winHt+' pageHt='+bodyHt+' remHt='+remHt);
1081         return remHt;
1082       default:
1083         Rico.log("remainingHt, winHt="+winHt+' tabHt='+tabHt);
1084         if (this.sizeTo.slice(-1)=='%') winHt*=parseFloat(this.sizeTo)/100.0;
1085         else if (this.sizeTo.slice(-2)=='px') winHt=parseInt(this.sizeTo,10);
1086         return winHt-tabHt-margin;  // allow for scrollbar and some margin
1087     }
1088   },
1089
1090   offsetFromParent: function(element) {
1091     var valueT = 0;
1092     var elParent=element.parentNode;
1093     do {
1094       //Rico.log("offsetFromParent: "+element.tagName+' id='+element.id+' otop='+element.offsetTop);
1095       valueT += element.offsetTop  || 0;
1096       element = element.offsetParent;
1097       if (!element || element==null) break;
1098       var p = Rico.getStyle(element, 'position');
1099       if (element.tagName=='BODY' || element.tagName=='HTML' || p=='absolute') return valueT-elParent.offsetTop;
1100     } while (element != elParent);
1101     return valueT;
1102   },
1103
1104   adjustPageSize: function() {
1105     var remHt=this.remainingHt();
1106     Rico.log('adjustPageSize remHt='+remHt+' lastRow='+this.lastRowPos);
1107     if (remHt > this.rowHeight)
1108       this.autoAppendRows(remHt);
1109     else if (remHt < 0 || this.sizeTo=='data')
1110       this.autoRemoveRows(-remHt);
1111   },
1112   
1113   setPageSize: function(newRowCount) {
1114     newRowCount=Math.min(newRowCount,this.options.maxPageRows);
1115     newRowCount=Math.max(newRowCount,this.options.minPageRows);
1116     this.sizeTo='fixed';
1117     var oldSize=this.pageSize;
1118     while (this.pageSize > newRowCount) {
1119       this.removeRow();
1120     }
1121     while (this.pageSize < newRowCount) {
1122       this.appendBlankRow();
1123     }
1124     this.finishResize(oldSize);
1125   },
1126
1127   pluginWindowResize: function() {
1128     Rico.log("pluginWindowResize");
1129     this.resizeWindowHandler=Rico.eventHandle(this,'resizeWindow');
1130     Rico.eventBind(window, "resize", this.resizeWindowHandler, false);
1131   },
1132
1133   unplugWindowResize: function() {
1134     if (!this.resizeWindowHandler) return;
1135     Rico.eventUnbind(window,"resize", this.resizeWindowHandler, false);
1136     this.resizeWindowHandler=null;
1137   },
1138
1139   resizeWindow: function() {
1140     Rico.log('resizeWindow '+this.tableId+' lastRow='+this.lastRowPos);
1141     if (this.resizeState=='finish') {
1142       Rico.log('resizeWindow postponed');
1143       this.resizeState='resize';
1144       return;
1145     }
1146     if (!this.sizeTo || this.sizeTo=='fixed') {
1147       this.sizeDivs();
1148       return;
1149     }
1150     if (this.sizeTo=='parent' && Rico.getStyle(this.outerDiv.parentNode,'display') == 'none') return;
1151     var oldSize=this.pageSize;
1152     this.adjustPageSize();
1153     this.finishResize(oldSize);
1154   },
1155
1156   finishResize: function(oldSize) {
1157     if (this.pageSize > oldSize && this.buffer.totalRows>0) {
1158       this.isPartialBlank=true;
1159       var adjStart=this.adjustRow(this.lastRowPos);
1160       this.buffer.fetch(adjStart);
1161     } else if (this.pageSize < oldSize) {
1162       if (this.options.onRefreshComplete) this.options.onRefreshComplete(this.contentStartPos,this.contentStartPos+this.pageSize-1);  // update bookmark
1163     }
1164     this.resizeState='finish';
1165     Rico.runLater(20,this,'finishResize2');
1166     Rico.log('Resize '+this.tableId+' complete. old size='+oldSize+' new size='+this.pageSize);
1167   },
1168
1169   finishResize2: function() {
1170     this.sizeDivs();
1171     this.updateHeightDiv();
1172     if (this.resizeState=='resize') {
1173       this.resizeWindow();
1174     } else {
1175       this.resizeState='';
1176     }
1177   },
1178
1179   topOfLastPage: function() {
1180     return Math.max(this.buffer.totalRows-this.pageSize,0);
1181   },
1182
1183   updateHeightDiv: function() {
1184     var notdisp=this.topOfLastPage();
1185     var ht = notdisp ? this.scrollDiv.clientHeight + Math.floor(this.rowHeight * (notdisp + 0.4)) : 1;
1186     Rico.log("updateHeightDiv, ht="+ht+' scrollDiv.clientHeight='+this.scrollDiv.clientHeight+' rowsNotDisplayed='+notdisp);
1187     this.shadowDiv.style.height=ht+'px';
1188   },
1189
1190   autoRemoveRows: function(overage) {
1191     if (!this.rowHeight) return;
1192     var removeCnt=Math.ceil(overage / this.rowHeight);
1193     if (this.sizeTo=='data')
1194       removeCnt=Math.max(removeCnt,this.pageSize-this.buffer.totalRows);
1195     Rico.log("autoRemoveRows overage="+overage+" removeCnt="+removeCnt);
1196     for (var i=0; i<removeCnt; i++)
1197       this.removeRow();
1198   },
1199
1200   removeRow: function() {
1201     if (this.pageSize <= this.options.minPageRows) return;
1202     this.pageSize--;
1203     for( var c=0; c < this.headerColCnt; c++ ) {
1204       var cell=this.columns[c].cell(this.pageSize);
1205       this.columns[c].dataColDiv.removeChild(cell);
1206     }
1207   },
1208
1209   autoAppendRows: function(overage) {
1210     if (!this.rowHeight) return;
1211     var addCnt=Math.floor(overage / this.rowHeight);
1212     Rico.log("autoAppendRows overage="+overage+" cnt="+addCnt+" rowHt="+this.rowHeight);
1213     for (var i=0; i<addCnt; i++) {
1214       if (this.sizeTo=='data' && this.pageSize>=this.buffer.totalRows) break;
1215       this.appendBlankRow();
1216     }
1217   },
1218
1219 /**
1220  * on older systems, this can be fairly slow
1221  */
1222   appendBlankRow: function() {
1223     if (this.pageSize >= this.options.maxPageRows) return;
1224     Rico.log("appendBlankRow #"+this.pageSize);
1225     var cls=this.defaultRowClass(this.pageSize);
1226     for( var c=0; c < this.headerColCnt; c++ ) {
1227       var newdiv = document.createElement("div");
1228       newdiv.className = 'ricoLG_cell '+cls;
1229       newdiv.id=this.tableId+'_'+this.pageSize+'_'+c;
1230       this.columns[c].dataColDiv.appendChild(newdiv);
1231       if (this.columns[c].format.canDrag && Rico.registerDraggable)
1232         Rico.registerDraggable( new Rico.LiveGridDraggable(this, this.pageSize, c), this.options.dndMgrIdx );
1233       newdiv.innerHTML='&nbsp;';   // this seems to be required by IE
1234       if (this.columns[c]._create) {
1235         this.columns[c]._create(newdiv,this.pageSize);
1236       }
1237     }
1238     this.pageSize++;
1239   },
1240
1241   defaultRowClass: function(rownum) {
1242     var cls
1243     if (rownum % 2==0) {
1244       cls='ricoLG_evenRow';
1245       //if (Rico.theme.primary) cls+=' '+Rico.theme.primary;
1246     } else {
1247       cls='ricoLG_oddRow';
1248       //if (Rico.theme.secondary) cls+=' '+Rico.theme.secondary;
1249     }
1250     return cls;
1251   },
1252
1253   handleMenuClick: function(e) {
1254     if (!this.menu) return;
1255     this.cancelMenu();
1256     this.unhighlight(); // in case highlighting was invoked externally
1257     var idx;
1258     var cell=Rico.eventElement(e);
1259     if (cell.className=='ricoLG_highlightDiv') {
1260       idx=this.highlightIdx;
1261     } else {
1262       cell=Rico.getParentByTagName(cell,'div','ricoLG_cell');
1263       if (!cell) return;
1264       idx=this.winCellIndex(cell);
1265       if ((this.options.highlightSection & (idx.tabIdx+1))==0) return;
1266     }
1267     this.highlight(idx);
1268     this.highlightEnabled=false;
1269     if (this.hideScroll) this.scrollDiv.style.overflow="hidden";
1270     this.menuIdx=idx;
1271     if (!this.menu.div) this.menu.createDiv();
1272     this.menu.liveGrid=this;
1273     if (this.menu.buildGridMenu) {
1274       var showMenu=this.menu.buildGridMenu(idx.row, idx.column, idx.tabIdx);
1275       if (!showMenu) return;
1276     }
1277     if (this.options.highlightElem=='selection' && !this.isSelected(idx.cell)) {
1278       this.selectCell(idx.cell);
1279     }
1280     var self=this;
1281     this.menu.showmenu(e,function() { self.closeMenu(); });
1282     return false;
1283   },
1284
1285   closeMenu: function() {
1286     if (!this.menuIdx) return;
1287     if (this.hideScroll) this.scrollDiv.style.overflow="";
1288     //this.unhighlight();
1289     this.highlightEnabled=true;
1290     this.menuIdx=null;
1291   },
1292
1293 /**
1294  * @return index of cell within the window
1295  */
1296   winCellIndex: function(cell) {
1297     var l=cell.id.lastIndexOf('_',cell.id.length);
1298     var l2=cell.id.lastIndexOf('_',l-1)+1;
1299     var c=parseInt(cell.id.substr(l+1));
1300     var r=parseInt(cell.id.substr(l2,l));
1301     return {row:r, column:c, tabIdx:this.columns[c].tabIdx, cell:cell};
1302   },
1303
1304 /**
1305  * @return index of cell within the dataset
1306  */
1307   datasetIndex: function(cell) {
1308     var idx=this.winCellIndex(cell);
1309     idx.row+=this.buffer.windowPos;
1310     idx.onBlankRow=(idx.row >= this.buffer.endPos());
1311     return idx;
1312   },
1313
1314   attachHighlightEvents: function(tBody) {
1315     switch (this.options.highlightElem) {
1316       case 'selection':
1317         Rico.eventBind(tBody,"mousedown", this.options.mouseDownHandler, false);
1318         /** @ignore */
1319         tBody.ondrag = function () { return false; };
1320         /** @ignore */
1321         tBody.onselectstart = function () { return false; };
1322         break;
1323       case 'cursorRow':
1324       case 'cursorCell':
1325         Rico.eventBind(tBody,"mouseover", this.options.rowOverHandler, false);
1326         break;
1327     }
1328   },
1329
1330   detachHighlightEvents: function(tBody) {
1331     switch (this.options.highlightElem) {
1332       case 'selection':
1333         Rico.eventUnbind(tBody,"mousedown", this.options.mouseDownHandler, false);
1334         tBody.ondrag = null;
1335         tBody.onselectstart = null;
1336         break;
1337       case 'cursorRow':
1338       case 'cursorCell':
1339         Rico.eventUnbind(tBody,"mouseover", this.options.rowOverHandler, false);
1340         break;
1341     }
1342   },
1343
1344 /**
1345  * @return array of objects containing row/col indexes (index values are relative to the start of the window)
1346  */
1347   getVisibleSelection: function() {
1348     var cellList=[];
1349     if (this.SelectIdxStart && this.SelectIdxEnd) {
1350       var r1=Math.max(Math.min(this.SelectIdxEnd.row,this.SelectIdxStart.row)-this.buffer.startPos,this.buffer.windowStart);
1351       var r2=Math.min(Math.max(this.SelectIdxEnd.row,this.SelectIdxStart.row)-this.buffer.startPos,this.buffer.windowEnd-1);
1352       var c1=Math.min(this.SelectIdxEnd.column,this.SelectIdxStart.column);
1353       var c2=Math.max(this.SelectIdxEnd.column,this.SelectIdxStart.column);
1354       //Rico.log("getVisibleSelection "+r1+','+c1+' to '+r2+','+c2+' ('+this.SelectIdxStart.row+',startPos='+this.buffer.startPos+',windowPos='+this.buffer.windowPos+',windowEnd='+this.buffer.windowEnd+')');
1355       for (var r=r1; r<=r2; r++) {
1356         for (var c=c1; c<=c2; c++)
1357           cellList.push({row:r-this.buffer.windowStart,column:c});
1358       }
1359     }
1360     if (this.SelectCtrl) {
1361       for (var i=0; i<this.SelectCtrl.length; i++) {
1362         if (this.SelectCtrl[i].row>=this.buffer.windowStart && this.SelectCtrl[i].row<this.buffer.windowEnd)
1363           cellList.push({row:this.SelectCtrl[i].row-this.buffer.windowStart,column:this.SelectCtrl[i].column});
1364       }
1365     }
1366     return cellList;
1367   },
1368
1369   updateSelectOutline: function() {
1370     if (!this.SelectIdxStart || !this.SelectIdxEnd) return;
1371     var r1=Math.max(Math.min(this.SelectIdxEnd.row,this.SelectIdxStart.row), this.buffer.windowStart);
1372     var r2=Math.min(Math.max(this.SelectIdxEnd.row,this.SelectIdxStart.row), this.buffer.windowEnd-1);
1373     if (r1 > r2) {
1374       this.HideSelection();
1375       return;
1376     }
1377     var c1=Math.min(this.SelectIdxEnd.column,this.SelectIdxStart.column);
1378     var c2=Math.max(this.SelectIdxEnd.column,this.SelectIdxStart.column);
1379     var top1=this.columns[c1].cell(r1-this.buffer.windowStart).offsetTop;
1380     var cell2=this.columns[c1].cell(r2-this.buffer.windowStart);
1381     var bottom2=cell2.offsetTop+cell2.offsetHeight;
1382     var left1=this.columns[c1].dataCell.offsetLeft;
1383     var left2=this.columns[c2].dataCell.offsetLeft;
1384     var right2=left2+this.columns[c2].dataCell.offsetWidth;
1385     //window.status='updateSelectOutline: '+r1+' '+r2+' top='+top1+' bot='+bottom2;
1386     this.highlightDiv[0].style.top=this.highlightDiv[3].style.top=this.highlightDiv[1].style.top=(this.hdrHt+top1-1) + 'px';
1387     this.highlightDiv[2].style.top=(this.hdrHt+bottom2-1)+'px';
1388     this.highlightDiv[3].style.left=(left1-2)+'px';
1389     this.highlightDiv[0].style.left=this.highlightDiv[2].style.left=(left1-1)+'px';
1390     this.highlightDiv[1].style.left=(right2-1)+'px';
1391     this.highlightDiv[0].style.width=this.highlightDiv[2].style.width=(right2-left1-1) + 'px';
1392     this.highlightDiv[1].style.height=this.highlightDiv[3].style.height=(bottom2-top1) + 'px';
1393     //this.highlightDiv[0].style.right=this.highlightDiv[2].style.right=this.highlightDiv[1].style.right=()+'px';
1394     //this.highlightDiv[2].style.bottom=this.highlightDiv[3].style.bottom=this.highlightDiv[1].style.bottom=(this.hdrHt+bottom2) + 'px';
1395     for (var i=0; i<4; i++)
1396       this.highlightDiv[i].style.display='';
1397   },
1398
1399   HideSelection: function() {
1400     var i;
1401     if (this.options.highlightMethod!='class') {
1402       for (i=0; i<this.highlightDiv.length; i++)
1403         this.highlightDiv[i].style.display='none';
1404     }
1405     if (this.options.highlightMethod!='outline') {
1406       var cellList=this.getVisibleSelection();
1407       Rico.log("HideSelection "+cellList.length);
1408       for (i=0; i<cellList.length; i++)
1409         this.unhighlightCell(this.columns[cellList[i].column].cell(cellList[i].row));
1410     }
1411   },
1412
1413   ShowSelection: function() {
1414     if (this.options.highlightMethod!='class')
1415       this.updateSelectOutline();
1416     if (this.options.highlightMethod!='outline') {
1417       var cellList=this.getVisibleSelection();
1418       for (var i=0; i<cellList.length; i++)
1419         this.highlightCell(this.columns[cellList[i].column].cell(cellList[i].row));
1420     }
1421   },
1422
1423   ClearSelection: function() {
1424     Rico.log("ClearSelection");
1425     this.HideSelection();
1426     this.SelectIdxStart=null;
1427     this.SelectIdxEnd=null;
1428     this.SelectCtrl=[];
1429   },
1430
1431   selectCell: function(cell) {
1432     this.ClearSelection();
1433     this.SelectIdxStart=this.SelectIdxEnd=this.datasetIndex(cell);
1434     this.ShowSelection();
1435   },
1436
1437   AdjustSelection: function(cell) {
1438     var newIdx=this.datasetIndex(cell);
1439     if (this.SelectIdxStart.tabIdx != newIdx.tabIdx) return;
1440     this.HideSelection();
1441     this.SelectIdxEnd=newIdx;
1442     this.ShowSelection();
1443   },
1444
1445   RefreshSelection: function() {
1446     var cellList=this.getVisibleSelection();
1447     for (var i=0; i<cellList.length; i++) {
1448       this.columns[cellList[i].column].displayValue(cellList[i].row);
1449     }
1450   },
1451
1452   FillSelection: function(newVal,newStyle) {
1453     if (this.SelectIdxStart && this.SelectIdxEnd) {
1454       var r1=Math.min(this.SelectIdxEnd.row,this.SelectIdxStart.row);
1455       var r2=Math.max(this.SelectIdxEnd.row,this.SelectIdxStart.row);
1456       var c1=Math.min(this.SelectIdxEnd.column,this.SelectIdxStart.column);
1457       var c2=Math.max(this.SelectIdxEnd.column,this.SelectIdxStart.column);
1458       for (var r=r1; r<=r2; r++) {
1459         for (var c=c1; c<=c2; c++) {
1460           this.buffer.setValue(r,c,newVal,newStyle);
1461         }
1462       }
1463     }
1464     if (this.SelectCtrl) {
1465       for (var i=0; i<this.SelectCtrl.length; i++) {
1466         this.buffer.setValue(this.SelectCtrl[i].row,this.SelectCtrl[i].column,newVal,newStyle);
1467       }
1468     }
1469     this.RefreshSelection();
1470   },
1471
1472 /**
1473  * Process mouse down event
1474  * @param e event object
1475  */
1476   selectMouseDown: function(e) {
1477     if (this.highlightEnabled==false) return true;
1478     this.cancelMenu();
1479     var cell=Rico.eventElement(e);
1480     if (!Rico.eventLeftClick(e)) return true;
1481     cell=Rico.getParentByTagName(cell,'div','ricoLG_cell');
1482     if (!cell) return true;
1483     Rico.eventStop(e);
1484     var newIdx=this.datasetIndex(cell);
1485     if (newIdx.onBlankRow) return true;
1486     Rico.log("selectMouseDown @"+newIdx.row+','+newIdx.column);
1487     if (e.ctrlKey) {
1488       if (!this.SelectIdxStart || this.options.highlightMethod!='class') return true;
1489       if (!this.isSelected(cell)) {
1490         this.highlightCell(cell);
1491         this.SelectCtrl.push(this.datasetIndex(cell));
1492       } else {
1493         for (var i=0; i<this.SelectCtrl.length; i++) {
1494           if (this.SelectCtrl[i].row==newIdx.row && this.SelectCtrl[i].column==newIdx.column) {
1495             this.unhighlightCell(cell);
1496             this.SelectCtrl.splice(i,1);
1497             break;
1498           }
1499         }
1500       }
1501     } else if (e.shiftKey) {
1502       if (!this.SelectIdxStart) return true;
1503       this.AdjustSelection(cell);
1504     } else {
1505       this.selectCell(cell);
1506       this.pluginSelect();
1507     }
1508     return false;
1509   },
1510
1511   pluginSelect: function() {
1512     if (this.selectPluggedIn) return;
1513     var tBody=this.tbody[this.SelectIdxStart.tabIdx];
1514     Rico.eventBind(tBody,"mouseover", this.options.mouseOverHandler, false);
1515     Rico.eventBind(this.outerDiv,"mouseup",  this.options.mouseUpHandler,  false);
1516     this.selectPluggedIn=true;
1517   },
1518
1519   unplugSelect: function() {
1520     if (!this.selectPluggedIn) return;
1521     var tBody=this.tbody[this.SelectIdxStart.tabIdx];
1522     Rico.eventUnbind(tBody,"mouseover", this.options.mouseOverHandler , false);
1523     Rico.eventUnbind(this.outerDiv,"mouseup", this.options.mouseUpHandler , false);
1524     this.selectPluggedIn=false;
1525   },
1526
1527   selectMouseUp: function(e) {
1528     this.unplugSelect();
1529     var cell=Rico.eventElement(e);
1530     cell=Rico.getParentByTagName(cell,'div','ricoLG_cell');
1531     if (!cell) return;
1532     if (this.SelectIdxStart && this.SelectIdxEnd)
1533       this.AdjustSelection(cell);
1534     else
1535       this.ClearSelection();
1536   },
1537
1538   selectMouseOver: function(e) {
1539     var cell=Rico.eventElement(e);
1540     cell=Rico.getParentByTagName(cell,'div','ricoLG_cell');
1541     if (!cell) return;
1542     this.AdjustSelection(cell);
1543     Rico.eventStop(e);
1544   },
1545
1546   isSelected: function(cell) {
1547     if (this.options.highlightMethod!='outline') return Rico.hasClass(cell,this.options.highlightClass);
1548     if (!this.SelectIdxStart || !this.SelectIdxEnd) return false;
1549     var r1=Math.max(Math.min(this.SelectIdxEnd.row,this.SelectIdxStart.row), this.buffer.windowStart);
1550     var r2=Math.min(Math.max(this.SelectIdxEnd.row,this.SelectIdxStart.row), this.buffer.windowEnd-1);
1551     if (r1 > r2) return false;
1552     var c1=Math.min(this.SelectIdxEnd.column,this.SelectIdxStart.column);
1553     var c2=Math.max(this.SelectIdxEnd.column,this.SelectIdxStart.column);
1554     var curIdx=this.datasetIndex(cell);
1555     return (r1<=curIdx.row && curIdx.row<=r2 && c1<=curIdx.column && curIdx.column<=c2);
1556   },
1557
1558   highlightCell: function(cell) {
1559     Rico.addClass(cell,this.options.highlightClass);
1560   },
1561
1562   unhighlightCell: function(cell) {
1563     if (cell) Rico.removeClass(cell,this.options.highlightClass);
1564   },
1565
1566   selectRow: function(r) {
1567     for (var c=0; c<this.columns.length; c++)
1568       this.highlightCell(this.columns[c].cell(r));
1569   },
1570
1571   unselectRow: function(r) {
1572     for (var c=0; c<this.columns.length; c++)
1573       this.unhighlightCell(this.columns[c].cell(r));
1574   },
1575
1576   rowMouseOver: function(e) {
1577     if (!this.highlightEnabled) return;
1578     var cell=Rico.eventElement(e);
1579     cell=Rico.getParentByTagName(cell,'div','ricoLG_cell');
1580     if (!cell) return;
1581     var newIdx=this.winCellIndex(cell);
1582     if ((this.options.highlightSection & (newIdx.tabIdx+1))==0) return;
1583     this.highlight(newIdx);
1584   },
1585
1586   highlight: function(newIdx) {
1587     if (this.options.highlightMethod!='outline') this.cursorSetClass(newIdx);
1588     if (this.options.highlightMethod!='class') this.cursorOutline(newIdx);
1589     this.highlightIdx=newIdx;
1590   },
1591
1592   cursorSetClass: function(newIdx) {
1593     switch (this.options.highlightElem) {
1594       case 'menuCell':
1595       case 'cursorCell':
1596         if (this.highlightIdx) this.unhighlightCell(this.highlightIdx.cell);
1597         this.highlightCell(newIdx.cell);
1598         break;
1599       case 'menuRow':
1600       case 'cursorRow':
1601         if (this.highlightIdx) this.unselectRow(this.highlightIdx.row);
1602         var s1=this.options.highlightSection & 1;
1603         var s2=this.options.highlightSection & 2;
1604         var c0=s1 ? 0 : this.options.frozenColumns;
1605         var c1=s2 ? this.columns.length : this.options.frozenColumns;
1606         for (var c=c0; c<c1; c++)
1607           this.highlightCell(this.columns[c].cell(newIdx.row));
1608         break;
1609       default: return;
1610     }
1611   },
1612
1613   cursorOutline: function(newIdx) {
1614     var div;
1615     switch (this.options.highlightElem) {
1616       case 'menuCell':
1617       case 'cursorCell':
1618         div=this.highlightDiv[newIdx.tabIdx];
1619         div.style.left=(this.columns[newIdx.column].dataCell.offsetLeft-1)+'px';
1620         div.style.width=this.columns[newIdx.column].colWidth;
1621         this.highlightDiv[1-newIdx.tabIdx].style.display='none';
1622         break;
1623       case 'menuRow':
1624       case 'cursorRow':
1625         div=this.highlightDiv[0];
1626         var s1=this.options.highlightSection & 1;
1627         var s2=this.options.highlightSection & 2;
1628         div.style.left=s1 ? '0px' : this.frozenTabs.style.width;
1629         div.style.width=((s1 ? this.frozenTabs.offsetWidth : 0) + (s2 ? this.innerDiv.offsetWidth : 0) - 4)+'px';
1630         break;
1631       default: return;
1632     }
1633     div.style.top=(this.hdrHt+newIdx.row*this.rowHeight-1)+'px';
1634     div.style.height=(this.rowHeight-1)+'px';
1635     div.style.display='';
1636   },
1637
1638   unhighlight: function() {
1639     switch (this.options.highlightElem) {
1640       case 'menuCell':
1641         //this.highlightIdx=this.menuIdx;
1642         /*jsl:fallthru*/
1643       case 'cursorCell':
1644         if (this.highlightIdx) this.unhighlightCell(this.highlightIdx.cell);
1645         if (!this.highlightDiv) return;
1646         for (var i=0; i<2; i++)
1647           this.highlightDiv[i].style.display='none';
1648         break;
1649       case 'menuRow':
1650         //this.highlightIdx=this.menuIdx;
1651         /*jsl:fallthru*/
1652       case 'cursorRow':
1653         if (this.highlightIdx) this.unselectRow(this.highlightIdx.row);
1654         if (this.highlightDiv) this.highlightDiv[0].style.display='none';
1655         break;
1656     }
1657   },
1658
1659   resetContents: function() {
1660     Rico.log("resetContents");
1661     this.ClearSelection();
1662     this.buffer.clear();
1663     this.clearRows();
1664     this.clearBookmark();
1665   },
1666
1667   setImages: function() {
1668     for (var n=0; n<this.columns.length; n++)
1669       this.columns[n].setImage();
1670   },
1671
1672 /**
1673  * @return column index, or -1 if there are no sorted columns
1674  */
1675   findSortedColumn: function() {
1676     for (var n=0; n<this.columns.length; n++) {
1677       if (this.columns[n].isSorted()) return n;
1678     }
1679     return -1;
1680   },
1681
1682   findColumnName: function(name) {
1683     for (var n=0; n<this.columns.length; n++) {
1684       if (this.columns[n].fieldName == name) return n;
1685     }
1686     return -1;
1687   },
1688   
1689 /**
1690  * Searches options.columnSpecs colAttr for matching colValue
1691  * @return array of matching column indexes
1692  */
1693   findColumnsBySpec: function(colAttr, colValue) {
1694     var result=[]
1695     for (var n=0; n<this.options.columnSpecs.length; n++) {
1696       if (this.options.columnSpecs[n][colAttr] == colValue) result.push(n);
1697     }
1698     return result;
1699   },
1700
1701 /**
1702  * Set initial sort
1703  */
1704   setSortUI: function( columnNameOrNum, sortDirection ) {
1705     Rico.log("setSortUI: "+columnNameOrNum+' '+sortDirection);
1706     var colnum=this.findSortedColumn();
1707     if (colnum >= 0) {
1708       sortDirection=this.columns[colnum].getSortDirection();
1709     } else {
1710       if (typeof sortDirection!='string') {
1711         sortDirection=Rico.ColumnConst.SORT_ASC;
1712       } else {
1713         sortDirection=sortDirection.toUpperCase();
1714         if (sortDirection != Rico.ColumnConst.SORT_DESC) sortDirection=Rico.ColumnConst.SORT_ASC;
1715       }
1716       switch (typeof columnNameOrNum) {
1717         case 'string':
1718           colnum=this.findColumnName(columnNameOrNum);
1719           break;
1720         case 'number':
1721           colnum=columnNameOrNum;
1722           break;
1723       }
1724     }
1725     if (typeof(colnum)!='number' || colnum < 0) return;
1726     this.clearSort();
1727     this.columns[colnum].setSorted(sortDirection);
1728     this.buffer.sortBuffer(colnum);
1729   },
1730
1731 /**
1732  * clear sort flag on all columns
1733  */
1734   clearSort: function() {
1735     for (var x=0;x<this.columns.length;x++)
1736       this.columns[x].setUnsorted();
1737   },
1738
1739 /**
1740  * clear filters on all columns
1741  */
1742   clearFilters: function() {
1743     for (var x=0;x<this.columns.length;x++) {
1744       this.columns[x].setUnfiltered(true);
1745     }
1746     if (this.options.filterHandler) {
1747       this.options.filterHandler();
1748     }
1749   },
1750
1751 /**
1752  * returns number of columns with a user filter set
1753  */
1754   filterCount: function() {
1755     for (var x=0,cnt=0;x<this.columns.length;x++) {
1756       if (this.columns[x].isFiltered()) cnt++;
1757     }
1758     return cnt;
1759   },
1760
1761   sortHandler: function() {
1762     this.cancelMenu();
1763     this.ClearSelection();
1764     this.setImages();
1765     var n=this.findSortedColumn();
1766     if (n < 0) return;
1767     Rico.log("sortHandler: sorting column "+n);
1768     this.buffer.sortBuffer(n);
1769     this.clearRows();
1770     this.scrollDiv.scrollTop = 0;
1771     this.buffer.fetch(0);
1772   },
1773
1774   filterHandler: function() {
1775     Rico.log("filterHandler");
1776     this.cancelMenu();
1777     if (this.buffer.processingRequest) {
1778       this.queueFilter=true;
1779       return;
1780     }
1781     this.unplugScroll();
1782     this.ClearSelection();
1783     this.setImages();
1784     this.clearBookmark();
1785     this.clearRows();
1786     this.buffer.fetch(-1);
1787     Rico.runLater(10,this,'pluginScroll'); // resetting ht div can cause a scroll event, triggering an extra fetch
1788   },
1789
1790   clearBookmark: function() {
1791     if (this.bookmark) this.bookmark.innerHTML="&nbsp;";
1792   },
1793
1794   bookmarkHandler: function(firstrow,lastrow) {
1795     var newhtml;
1796     if (isNaN(firstrow) || !this.bookmark) return;
1797     var totrows=this.buffer.totalRows;
1798     if (totrows < lastrow) lastrow=totrows;
1799     if (totrows<=0) {
1800       newhtml = Rico.getPhraseById('bookmarkNoMatch');
1801     } else if (lastrow<0) {
1802       newhtml = Rico.getPhraseById('bookmarkNoRec');
1803     } else if (this.buffer.foundRowCount) {
1804       newhtml = Rico.getPhraseById('bookmarkExact',firstrow,lastrow,totrows);
1805     } else {
1806       newhtml = Rico.getPhraseById('bookmarkAbout',firstrow,lastrow,totrows);
1807     }
1808     this.bookmark.innerHTML = newhtml;
1809   },
1810
1811   clearRows: function() {
1812     if (this.isBlank==true) return;
1813     for (var c=0; c < this.columns.length; c++)
1814       this.columns[c].clearColumn();
1815     this.isBlank = true;
1816   },
1817
1818   refreshContents: function(startPos) {
1819     Rico.log("refreshContents1 "+this.tableId+": startPos="+startPos+" lastRow="+this.lastRowPos+" PartBlank="+this.isPartialBlank+" pageSize="+this.pageSize);
1820     this.hideMsg();
1821     this.cancelMenu();
1822     this.unhighlight(); // in case highlighting was manually invoked
1823     if (this.queueFilter) {
1824       Rico.log("refreshContents: cancelling refresh because filter has changed");
1825       this.queueFilter=false;
1826       this.filterHandler();
1827       return;
1828     }
1829     this.highlightEnabled=this.options.highlightSection!='none';
1830     var viewPrecedesBuffer = this.buffer.startPos > startPos;
1831     var contentStartPos = viewPrecedesBuffer ? this.buffer.startPos: startPos;
1832     this.contentStartPos = contentStartPos+1;
1833     var contentEndPos = Math.min(this.buffer.startPos + this.buffer.size, startPos + this.pageSize);
1834     this.buffer.setWindow(contentStartPos, contentEndPos);
1835     Rico.log('refreshContents2 '+this.tableId+': cStartPos='+contentStartPos+' cEndPos='+contentEndPos+' vPrecedesBuf='+viewPrecedesBuffer+' b.startPos='+this.buffer.startPos);
1836     if (startPos == this.lastRowPos && !this.isPartialBlank && !this.isBlank) return;
1837     this.isBlank = false;
1838     var onRefreshComplete = this.options.onRefreshComplete;
1839
1840     if ((startPos + this.pageSize < this.buffer.startPos) ||
1841         (this.buffer.startPos + this.buffer.size < startPos) ||
1842         (this.buffer.size == 0)) {
1843       this.clearRows();
1844       if (onRefreshComplete) onRefreshComplete(this.contentStartPos,contentEndPos);  // update bookmark
1845       return;
1846     }
1847
1848     Rico.log('refreshContents: contentStartPos='+contentStartPos+' contentEndPos='+contentEndPos+' viewPrecedesBuffer='+viewPrecedesBuffer);
1849     var rowSize = contentEndPos - contentStartPos;
1850     var blankSize = this.pageSize - rowSize;
1851     var blankOffset = viewPrecedesBuffer ? 0: rowSize;
1852     var contentOffset = viewPrecedesBuffer ? blankSize: 0;
1853
1854     for (var r=0; r < rowSize; r++) { //initialize what we have
1855       for (var c=0; c < this.columns.length; c++)
1856         this.columns[c].displayValue(r + contentOffset);
1857     }
1858     for (var i=0; i < blankSize; i++)     // blank out the rest
1859       this.blankRow(i + blankOffset);
1860     if (this.options.highlightElem=='selection') this.ShowSelection();
1861     this.isPartialBlank = blankSize > 0;
1862     this.lastRowPos = startPos;
1863     Rico.log("refreshContents complete, startPos="+startPos);
1864     if (onRefreshComplete) onRefreshComplete(this.contentStartPos,contentEndPos);  // update bookmark
1865   },
1866
1867   scrollToRow: function(rowOffset) {
1868      var p=this.rowToPixel(rowOffset);
1869      Rico.log("scrollToRow, rowOffset="+rowOffset+" pixel="+p);
1870      this.scrollDiv.scrollTop = p; // this causes a scroll event
1871      if ( this.options.onscroll )
1872         this.options.onscroll( this, rowOffset );
1873   },
1874
1875   scrollUp: function() {
1876      this.moveRelative(-1);
1877   },
1878
1879   scrollDown: function() {
1880      this.moveRelative(1);
1881   },
1882
1883   pageUp: function() {
1884      this.moveRelative(-this.pageSize);
1885   },
1886
1887   pageDown: function() {
1888      this.moveRelative(this.pageSize);
1889   },
1890
1891   adjustRow: function(rowOffset) {
1892      var notdisp=this.topOfLastPage();
1893      if (notdisp == 0 || !rowOffset) return 0;
1894      return Math.min(notdisp,rowOffset);
1895   },
1896
1897   rowToPixel: function(rowOffset) {
1898      return this.adjustRow(rowOffset) * this.rowHeight;
1899   },
1900
1901 /**
1902  * @returns row to display at top of scroll div
1903  */
1904   pixeltorow: function(p) {
1905      var notdisp=this.topOfLastPage();
1906      if (notdisp == 0) return 0;
1907      var prow=parseInt(p/this.rowHeight,10);
1908      return Math.min(notdisp,prow);
1909   },
1910
1911   moveRelative: function(relOffset) {
1912      var newoffset=Math.max(this.scrollDiv.scrollTop+relOffset*this.rowHeight,0);
1913      newoffset=Math.min(newoffset,this.scrollDiv.scrollHeight);
1914      //Rico.log("moveRelative, newoffset="+newoffset);
1915      this.scrollDiv.scrollTop=newoffset;
1916   },
1917
1918   pluginScroll: function() {
1919      if (this.scrollPluggedIn) return;
1920      Rico.log("pluginScroll: wheelEvent="+this.wheelEvent);
1921      Rico.eventBind(this.scrollDiv,"scroll",this.scrollEventFunc, false);
1922      for (var t=0; t<2; t++)
1923        Rico.eventBind(this.tabs[t],this.wheelEvent,this.wheelEventFunc, false);
1924      this.scrollPluggedIn=true;
1925   },
1926
1927   unplugScroll: function() {
1928      if (!this.scrollPluggedIn) return;
1929      Rico.log("unplugScroll");
1930      Rico.eventUnbind(this.scrollDiv,"scroll", this.scrollEventFunc , false);
1931      for (var t=0; t<2; t++)
1932        Rico.eventUnbind(this.tabs[t],this.wheelEvent,this.wheelEventFunc, false);
1933      this.scrollPluggedIn=false;
1934   },
1935
1936   handleWheel: function(e) {
1937     var delta = 0;
1938     if (e.wheelDelta) {
1939       if (Rico.isOpera)
1940         delta = e.wheelDelta/120;
1941       else if (Rico.isWebKit)
1942         delta = -e.wheelDelta/12;
1943       else
1944         delta = -e.wheelDelta/120;
1945     } else if (e.detail) {
1946       delta = e.detail/3; /* Mozilla/Gecko */
1947     }
1948     if (delta) this.moveRelative(delta);
1949     Rico.eventStop(e);
1950     return false;
1951   },
1952
1953   handleScroll: function(e) {
1954      if ( this.scrollTimeout )
1955        clearTimeout( this.scrollTimeout );
1956      this.setHorizontalScroll();
1957      var scrtop=this.scrollDiv.scrollTop;
1958      var vscrollDiff = this.lastScrollPos-scrtop;
1959      if (vscrollDiff == 0.00) return;
1960      var newrow=this.pixeltorow(scrtop);
1961      if (newrow == this.lastRowPos && !this.isPartialBlank && !this.isBlank) return;
1962      var stamp1 = new Date();
1963      Rico.log("handleScroll, newrow="+newrow+" scrtop="+scrtop);
1964      if (this.options.highlightElem=='selection') this.HideSelection();
1965      this.buffer.fetch(newrow);
1966      if (this.options.onscroll) this.options.onscroll(this, newrow);
1967      this.scrollTimeout = Rico.runLater(1200,this,'scrollIdle');
1968      this.lastScrollPos = this.scrollDiv.scrollTop;
1969      var stamp2 = new Date();
1970      //Rico.log("handleScroll, time="+(stamp2.getTime()-stamp1.getTime()));
1971   },
1972
1973   scrollIdle: function() {
1974      if ( this.options.onscrollidle )
1975         this.options.onscrollidle();
1976   }
1977
1978 };
1979
1980
1981 Rico.LiveGridColumn = function(grid,colIdx,hdrInfo,tabIdx) {
1982   this.initialize(grid,colIdx,hdrInfo,tabIdx);
1983 };
1984
1985 Rico.LiveGridColumn.prototype = 
1986 /** @lends Rico.LiveGridColumn# */
1987 {
1988 /**
1989  * Implements a LiveGrid column. Also contains static properties used by SimpleGrid columns.
1990  * @extends Rico.TableColumnBase
1991  * @constructs
1992  */
1993 initialize: function(liveGrid,colIdx,hdrInfo,tabIdx) {
1994   Rico.extend(this, new Rico.TableColumnBase());
1995   this.baseInit(liveGrid,colIdx,hdrInfo,tabIdx);
1996   this.buffer=liveGrid.buffer;
1997   if (typeof(this.format.type)!='string' || this.format.EntryType=='tinyMCE') this.format.type='raw';
1998   if (typeof this.isNullable!='boolean') this.isNullable = /number|date/.test(this.format.type);
1999   this.isText = /raw|text|showTags/.test(this.format.type);
2000   Rico.log(" sortable="+this.sortable+" filterable="+this.filterable+" hideable="+this.hideable+" isNullable="+this.isNullable+' isText='+this.isText);
2001   this.fixHeaders(this.liveGrid.tableId, this.options.hdrIconsFirst);
2002   if (this['format_'+this.format.type]) {
2003     this._format=this['format_'+this.format.type];
2004   }
2005   if (this.format.control) {
2006     // copy all properties/methods that start with '_'
2007     if (typeof this.format.control=='string') {
2008       this.format.control=eval(this.format.control);
2009     }
2010     for (var property in this.format.control) {
2011       if (property.charAt(0)=='_') {
2012         Rico.log("Copying control property "+property+ ' to ' + this);
2013         this[property] = this.format.control[property];
2014       }
2015     }
2016   }
2017 },
2018
2019 /**
2020  * Sorts the column in ascending order
2021  */
2022 sortAsc: function() {
2023   this.setColumnSort(Rico.ColumnConst.SORT_ASC);
2024 },
2025
2026 /**
2027  * Sorts the column in descending order
2028  */
2029 sortDesc: function() {
2030   this.setColumnSort(Rico.ColumnConst.SORT_DESC);
2031 },
2032
2033 /**
2034  * Sorts the column in the specified direction
2035  * @param direction must be one of Rico.ColumnConst.UNSORTED, .SORT_ASC, or .SORT_DESC
2036  */
2037 setColumnSort: function(direction) {
2038   this.liveGrid.clearSort();
2039   this.setSorted(direction);
2040   if (this.liveGrid.options.saveColumnInfo.sort)
2041     this.liveGrid.setCookie();
2042   if (this.options.sortHandler)
2043     this.options.sortHandler();
2044 },
2045
2046 /**
2047  * @returns true if this column is allowed to be sorted
2048  */
2049 isSortable: function() {
2050   return this.sortable;
2051 },
2052
2053 /**
2054  * @returns true if this column is currently sorted
2055  */
2056 isSorted: function() {
2057   return this.currentSort != Rico.ColumnConst.UNSORTED;
2058 },
2059
2060 /**
2061  * @returns Rico.ColumnConst.UNSORTED, .SORT_ASC, or .SORT_DESC
2062  */
2063 getSortDirection: function() {
2064   return this.currentSort;
2065 },
2066
2067 /**
2068  * toggle the sort sequence for this column
2069  */
2070 toggleSort: function() {
2071   if (this.buffer && this.buffer.totalRows==0) return;
2072   if (this.currentSort == Rico.ColumnConst.SORT_ASC)
2073     this.sortDesc();
2074   else
2075     this.sortAsc();
2076 },
2077
2078 /**
2079  * Flags that this column is not sorted
2080  */
2081 setUnsorted: function() {
2082   this.setSorted(Rico.ColumnConst.UNSORTED);
2083 },
2084
2085 /**
2086  * Flags that this column is sorted, but doesn't actually carry out the sort
2087  * @param direction must be one of Rico.ColumnConst.UNSORTED, .SORT_ASC, or .SORT_DESC
2088  */
2089 setSorted: function(direction) {
2090   this.currentSort = direction;
2091 },
2092
2093 /**
2094  * @returns true if this column is allowed to be filtered
2095  */
2096 canFilter: function() {
2097   return this.filterable;
2098 },
2099
2100 /**
2101  * @returns a textual representation of how this column is filtered
2102  */
2103 getFilterText: function() {
2104   var vals=[];
2105   for (var i=0; i<this.filterValues.length; i++) {
2106     var v=this.filterValues[i];
2107     vals.push(v=='' ? Rico.getPhraseById('filterBlank') : v);
2108   }
2109   switch (this.filterOp) {
2110     case 'EQ':   return '= '+vals.join(', ');
2111     case 'NE':   return Rico.getPhraseById('filterNot',vals.join(', '));
2112     case 'LE':   return '<= '+vals[0];
2113     case 'GE':   return '>= '+vals[0];
2114     case 'LIKE': return Rico.getPhraseById('filterLike',vals[0]);
2115     case 'NULL': return Rico.getPhraseById('filterEmpty');
2116     case 'NOTNULL': return Rico.getPhraseById('filterNotEmpty');
2117   }
2118   return '?';
2119 },
2120
2121 /**
2122  * @returns returns the query string representation of the filter
2123  */
2124 getFilterQueryParm: function() {
2125   if (this.filterType == Rico.ColumnConst.UNFILTERED) return '';
2126   var retval='&f['+this.index+'][op]='+this.filterOp;
2127   retval+='&f['+this.index+'][len]='+this.filterValues.length;
2128   for (var i=0; i<this.filterValues.length; i++) {
2129     retval+='&f['+this.index+']['+i+']='+escape(this.filterValues[i]);
2130   }
2131   return retval;
2132 },
2133
2134 /**
2135  * removes the filter from this column
2136  */
2137 setUnfiltered: function(skipHandler) {
2138   this.filterType = Rico.ColumnConst.UNFILTERED;
2139   if (this.liveGrid.options.saveColumnInfo.filter)
2140     this.liveGrid.setCookie();
2141   if (this.removeFilterFunc)
2142     this.removeFilterFunc();
2143   if (this.options.filterHandler && !skipHandler)
2144     this.options.filterHandler();
2145 },
2146
2147 setFilterEQ: function() {
2148   this.setUserFilter('EQ');
2149 },
2150 setFilterNE: function() {
2151   this.setUserFilter('NE');
2152 },
2153 addFilterNE: function() {
2154   this.filterValues.push(this.userFilter);
2155   if (this.liveGrid.options.saveColumnInfo.filter)
2156     this.liveGrid.setCookie();
2157   if (this.options.filterHandler)
2158     this.options.filterHandler();
2159 },
2160 setFilterGE: function() { this.setUserFilter('GE'); },
2161 setFilterLE: function() { this.setUserFilter('LE'); },
2162 setFilterKW: function(keyword) {
2163   if (keyword!='' && keyword!=null) {
2164     this.setFilter('LIKE',keyword,Rico.ColumnConst.USERFILTER);
2165   } else {
2166     this.setUnfiltered(false);
2167   }
2168 },
2169
2170 setUserFilter: function(relop) {
2171   this.setFilter(relop,this.userFilter,Rico.ColumnConst.USERFILTER);
2172 },
2173
2174 setSystemFilter: function(relop,filter) {
2175   this.setFilter(relop,filter,Rico.ColumnConst.SYSTEMFILTER);
2176 },
2177
2178 setFilter: function(relop,filter,type,removeFilterFunc) {
2179   this.filterValues = typeof(filter)=='object' ? filter : [filter];
2180   this.filterType = type;
2181   this.filterOp = relop;
2182   if (type == Rico.ColumnConst.USERFILTER && this.liveGrid.options.saveColumnInfo.filter)
2183     this.liveGrid.setCookie();
2184   this.removeFilterFunc=removeFilterFunc;
2185   if (this.options.filterHandler)
2186     this.options.filterHandler();
2187 },
2188
2189 isFiltered: function() {
2190   return this.filterType == Rico.ColumnConst.USERFILTER;
2191 },
2192
2193 filterChange: function(e) {\r
2194   var selbox=Rico.eventElement(e);
2195   if (selbox.value==this.liveGrid.options.FilterAllToken)\r
2196     this.setUnfiltered();\r
2197   else
2198     this.setFilter('EQ',selbox.value,Rico.ColumnConst.USERFILTER,function() {selbox.selectedIndex=0;});\r
2199 },
2200
2201 filterClear: function(e) {\r
2202   this.filterField.value='';
2203   this.setUnfiltered();\r
2204 },
2205
2206 filterKeypress: function(e) {\r
2207   var txtbox=Rico.eventElement(e);
2208   if (typeof this.lastKeyFilter != 'string') this.lastKeyFilter='';\r
2209   if (this.lastKeyFilter==txtbox.value) return;\r
2210   var v=txtbox.value;\r
2211   Rico.log("filterKeypress: "+this.index+' '+v);\r
2212   this.lastKeyFilter=v;
2213   if (v=='' || v=='*')\r
2214     this.setUnfiltered();\r
2215   else {
2216     this.setFilter('LIKE', v, Rico.ColumnConst.USERFILTER, function() {txtbox.value='';});
2217   }\r
2218 },\r
2219
2220 mFilterSelectClick: function(e) {
2221   Rico.eventStop(e);
2222   if (this.mFilter.style.display!='none') {
2223     this.mFilterFinish(e);
2224     if (Rico.isIE && Rico.ieVersion <= 6) {
2225       this.filterField.focus();
2226     } else {
2227       this.filterField.blur();
2228     }
2229   } else {
2230     var offset=Rico.cumulativeOffset(this.filterField);
2231     this.mFilter.style.top=(offset.top+this.filterField.offsetHeight)+'px';
2232     this.mFilter.style.left=offset.left+'px';
2233     this.mFilter.style.width=Math.min(this.filterField.offsetWidth,parseInt(this.colWidth,10))+'px';
2234     Rico.show(this.mFilter);
2235     this.mFilterFocus.focus();
2236   }
2237 },
2238
2239 mFilterFinish: function(e) {
2240   if (!this.mFilterChange) {
2241     Rico.hide(this.mFilter);
2242     return;
2243   }
2244   if (this.mFilterInputs[0].checked) {
2245     this.mFilterReset();
2246     Rico.hide(this.mFilter);
2247     this.setUnfiltered();
2248     return;
2249   }
2250   var newValues=[];
2251   var newLabels=[];
2252   for (var i=1; i<this.mFilterInputs.length; i++) {
2253     if (this.mFilterInputs[i].checked) {
2254       newValues.push(this.mFilterInputs[i].value)
2255       newLabels.push(this.mFilterLabels[i].innerHTML)
2256     }
2257   }
2258   if (newValues.length > 0) {
2259     var newText=newLabels.join(', ');
2260     this.filterField.options[0].text=newText;
2261     this.filterField.title=newText;
2262     Rico.hide(this.mFilter);
2263     this.mFilterChange=false;
2264     var self=this;
2265     this.setFilter('EQ',newValues,Rico.ColumnConst.USERFILTER,function() { self.mFilterReset(); });
2266   } else {
2267     alert('Please select at least one value');
2268   }
2269 },
2270
2271 mFilterReset: function() {
2272   var newText=this.mFilterLabels[0].innerHTML;  // all
2273   this.filterField.options[0].text=newText;
2274   this.filterField.title=newText;
2275 },
2276
2277 mFilterAllClick: function(e) {
2278   var allChecked=this.mFilterInputs[0].checked;
2279   for (var i=1; i<this.mFilterInputs.length; i++) {
2280     this.mFilterInputs[i].checked=allChecked;
2281   }
2282   this.mFilterChange=true;
2283 },
2284
2285 mFilterOtherClick: function(e) {
2286   this.mFilterInputs[0].checked=false;
2287   this.mFilterChange=true;
2288 },
2289
2290 format_text: function(v) {
2291   if (typeof v!='string')
2292     return '&nbsp;';
2293   else
2294     return Rico.stripTags(v);
2295 },
2296
2297 format_showTags: function(v) {
2298   if (typeof v!='string')
2299     return '&nbsp;';
2300   else
2301     return v.replace(/&/g, '&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
2302 },
2303
2304 format_number: function(v) {
2305   if (typeof v=='undefined' || v=='' || v==null)
2306     return '&nbsp;';
2307   else
2308     return Rico.formatNumber(v,this.format);
2309 },
2310
2311 format_datetime: function(v) {
2312   if (typeof v=='undefined' || v=='' || v==null)
2313     return '&nbsp;';
2314   else {
2315     var d=Rico.setISO8601(v);
2316     if (!d) return v;
2317     return (this.format.prefix || '')+Rico.formatDate(d,this.format.dateFmt || 'translateDateTime')+(this.format.suffix || '');
2318   }
2319 },
2320
2321 // converts GMT/UTC to local time
2322 format_UTCasLocalTime: function(v) {
2323   if (typeof v=='undefined' || v=='' || v==null)
2324     return '&nbsp;';
2325   else {
2326     var tz=new Date();
2327     var d=Rico.setISO8601(v,-tz.getTimezoneOffset());
2328     if (!d) return v;
2329     return (this.format.prefix || '')+Rico.formatDate(d,this.format.dateFmt || 'translateDateTime')+(this.format.suffix || '');
2330   }
2331 },
2332
2333 format_date: function(v) {
2334   if (typeof v=='undefined' || v==null || v=='')
2335     return '&nbsp;';
2336   else {
2337     var d=Rico.setISO8601(v);
2338     if (!d) return v;
2339     return (this.format.prefix || '')+Rico.formatDate(d,this.format.dateFmt || 'translateDate')+(this.format.suffix || '');
2340   }
2341 },
2342
2343 fixHeaders: function(prefix, iconsfirst) {
2344   if (this.sortable) {
2345     var handler=Rico.eventHandle(this,'toggleSort');
2346     switch (this.options.headingSort) {
2347       case 'link':
2348         var a=Rico.wrapChildren(this.hdrCellDiv,'ricoSort',undefined,'a');
2349         a.href = "javascript:void(0)";
2350         Rico.eventBind(a,"click", handler);
2351         break;
2352       case 'hover':
2353         Rico.eventBind(this.hdrCellDiv,"click", handler);
2354         break;
2355     }
2356   }
2357   this.imgFilter = document.createElement('span');
2358   this.imgFilter.style.display='none';
2359   this.imgFilter.className='rico-icon ricoLG_filterCol';
2360   this.imgSort = document.createElement('span');
2361   this.imgSort.style.display='none';
2362   this.imgSort.style.verticalAlign='top';
2363   if (iconsfirst) {
2364     this.hdrCellDiv.insertBefore(this.imgSort,this.hdrCellDiv.firstChild);
2365     this.hdrCellDiv.insertBefore(this.imgFilter,this.hdrCellDiv.firstChild);
2366   } else {
2367     this.hdrCellDiv.appendChild(this.imgFilter);
2368     this.hdrCellDiv.appendChild(this.imgSort);
2369   }
2370   if (!this.format.filterUI) {
2371     Rico.eventBind(this.imgFilter, 'click', Rico.eventHandle(this,'filterClick'), false);
2372   }
2373 },
2374
2375 filterClick: function(e) {
2376   if (this.filterType==Rico.ColumnConst.USERFILTER && this.filterOp=='LIKE') {
2377     this.liveGrid.openKeyword(this.index);
2378   }
2379 },
2380
2381 getValue: function(windowRow) {
2382   return this.buffer.getWindowCell(windowRow,this.index);
2383 },
2384
2385 getBufferAttr: function(windowRow) {
2386   return this.buffer.getWindowAttr(windowRow,this.index);
2387 },
2388
2389 setValue: function(windowRow,newval) {
2390   this.buffer.setWindowValue(windowRow,this.index,newval);
2391 },
2392
2393 _format: function(v) {
2394   return v;
2395 },
2396
2397 _display: function(v,gridCell) {
2398   gridCell.innerHTML=this._format(v);
2399 },
2400
2401 _export: function(v) {
2402   return this._format(v);
2403 },
2404
2405 exportBuffer: function(bufRow) {
2406   return this._export(this.buffer.getValue(bufRow,this.index));
2407 },
2408
2409 displayValue: function(windowRow) {
2410   var bufval=this.getValue(windowRow);
2411   if (bufval==null) {
2412     this.clearCell(windowRow);
2413     return;
2414   }
2415   var gridCell=this.cell(windowRow);
2416   this._display(bufval,gridCell,windowRow);
2417   var acceptAttr=this.buffer.options.acceptAttr;
2418   if (acceptAttr.length==0) return;
2419   var bufAttr=this.getBufferAttr(windowRow);
2420   if (bufAttr==null) return;
2421   for (var k=0; k<acceptAttr.length; k++) {
2422     bufAttr=bufAttr['_'+acceptAttr[k]] || '';
2423     switch (acceptAttr[k]) {
2424       case 'style': gridCell.style.cssText=bufAttr; break;
2425       case 'class': gridCell.className=bufAttr; break;
2426       default:      gridCell['_'+acceptAttr[k]]=bufAttr; break;
2427     }
2428   }
2429 }
2430
2431 };