<PUBLIC:COMPONENT
>

<PUBLIC:DEFAULTS
/>

<PUBLIC:attach	 event="oncontentready" onevent="initElement()" />
<PUBLIC:attach   event="ondetach" onevent="cleanupElement()" />
<PUBLIC:attach	 event="onresize" onevent="elementOnResize()" />
<PUBLIC:attach	 event="onselectstart" onevent="elementOnSelect()" />
<PUBLIC:attach	 event="onpropertychange" onevent="elementOnPropertyChange()" />

<PUBLIC:property name="borderWidth" value=1 />
<PUBLIC:property name="altRowColor" value="" />
<PUBLIC:property name="contextMenu" value="" />
<PUBLIC:property name="rowCount" value=0 />
<PUBLIC:property name="colCount" value=0 />
<PUBLIC:property name="rightClickRowIndex" value=-1 />

<PUBLIC:method   name="getSelectedCount" />
<PUBLIC:method   name="getSelectedRow" />
<PUBLIC:method   name="sortAscending" />
<PUBLIC:method   name="sortDescending" />
<PUBLIC:method   name="showColumn" />
<PUBLIC:method   name="hideColumn" />
<PUBLIC:method   name="gridRowCount" />
<PUBLIC:method   name="addGridRow" />
<PUBLIC:method   name="deleteGridRow" />
<PUBLIC:method   name="setRowSelected" />
<PUBLIC:method   name="showFieldChooser" />
<PUBLIC:method   name="doAction" />
<PUBLIC:method   name="hideMenu" />

<PUBLIC:event    name="onRowSelect" id="rowselect" />
<PUBLIC:event    name="onRowDoubleClick" id="rowdblclick" />
<PUBLIC:event    name="onRowRightClick" id="rowrightclick" />
<PUBLIC:event    name="onRowCountChange" id="rowcountchange" />
<PUBLIC:event    name="onRequestAddDialog" id="requestdialogadd" />

<script src="popup.js"></script>
<script language="jscript">

// ----------------------------------------------------
// 2k3Widgets: 2k3Grid
// 
// Copyright ?002-2004 Stedy Software and Systems     
// Please see http://www.stedy.com for terms of use.   
// ----------------------------------------------------

var cancelClick = false ;
var selectToHilite = null ;

var menuState = false ;
var menuColumnIdx = -1 ;

var sortColumnIdx = -1 ;
var sortDirection = "ASC" ;

var inSize = false ;
var inSizeColumnIdx = -1 ;
var inSizeStartPos = -1 ;
var inSizeEndPos = -1 ;

var inDrag = false ;
var inDragColumnFromIdx = -1 ;
var inDragColumnToIdx = -1 ;
var inDragColumnCurrentIdx = -1 ;
var inDragCounter = 0 ;
var inDragArrowsOffset = 4 ;

var gridHeadHeight = 0 ;
var gridHeadWidth = 0 ;

var objSelectedRows = null ;

var gridHead = null ;
var gridHeadRow = null ;
var gridRowExtraSpan = null ;
var gridBody = null ;

var objMenu = null ;
var objWindow = null ;
var objSizeItem = null ;
var objDragItem = null ;
var objDragToItem1 = null ;
var objDragToItem2 = null ;
var objBodyMenu = null;


// ----------------------------------------------------
// initialize/terminate functions   
// ----------------------------------------------------

function initElement() {
	setGridBorder() ;

	gridHead = getElement(element, "gridHead") ;
	if (gridHead == null) {
		alert("A 2k3Grid element must have a gridHead!") ;
		return ;
	}
	gridHead.align = "left" ;
	gridHeadHeight = gridHead.offsetHeight ;

	gridHeadRow = getElement(gridHead, "gridRow") ;
	if (gridHeadRow == null) {
		alert("A 2k3Grid element gridHead must have a gridRow!") ;
		return ;
	}
	with (gridHeadRow) {
		element.colCount = children.length ;
		insertAdjacentElement("beforeEnd", document.createElement("SPAN")) ;
		innerHTML = innerHTML.replace(/(\> <)/g, "><") ;
	}
	
	gridBody = getElement(element, "gridBody") ;
	if (gridBody == null) {
		alert("A 2k3Grid element must have a gridBody!") ;
		return ;
	}
	if (getElement(gridBody, "gridRow") == null) {
		//return ;
	}
	with (gridBody) {
		element.rowCount = children.length ;
		style.left = "0px" ;
	}
	var gbChildLength = gridBody.children.length ;
	for (var i = 0; i < gbChildLength; i++) {
		with (gridBody.children[i]) {
			innerHTML = innerHTML.replace(/(\> <)/g, "><") ;
		}
	}
	
	createAdditionalElements() ;

	objSelectedRows = new selectedRows() ;

	setRowColors() ;
	
	with (gridHead) {
		attachEvent("onmouseover", gridHeadOnMouseOver) ;
		attachEvent("onmouseout", gridHeadOnMouseOut) ;
		attachEvent("onmousedown", gridHeadOnMouseDown) ;
		attachEvent("onmousemove", gridHeadOnMouseMove) ;
		attachEvent("onclick", gridHeadOnClick) ;
		attachEvent("oncontextmenu", gridHeadOnRightClick) ;
	}

	with (gridBody) {
		//attachEvent("onmouseover", gridBodyOnMouseOver) ;
		//attachEvent("onmouseout", gridBodyOnMouseOut) ;
		//attachEvent("onmousedown", gridBodyOnMouseDown) ;
		//attachEvent("oncontextmenu", gridBodyOnRightClick) ;
		attachEvent("onclick", gridBodyOnClick) ;
		attachEvent("ondblclick", gridBodyOnDblClick) ;
		attachEvent("onscroll", gridBodyOnScroll) ;
	}
	
	with (element) {
		attachEvent("onmousemove", elementOnMouseMove) ;
		attachEvent("onmouseup", elementOnMouseUp) ;
		attachEvent("oncontextmenu", elementOnRightClick) ;
	}
	
	window.attachEvent("onload", actionOnLoad) ;
}

function cleanupElement() {
	with (gridHead) {
		detachEvent("onmouseover", gridHeadOnMouseOver) ;
		detachEvent("onmouseout", gridHeadOnMouseOut) ;
		detachEvent("onmousedown", gridHeadOnMouseDown) ;
		detachEvent("onmousemove", gridHeadOnMouseMove) ;
		detachEvent("onclick", gridHeadOnClick) ;
		detachEvent("oncontextmenu", gridHeadOnRightClick) ;
	}

	with (gridBody) {
		//detachEvent("onmouseover", gridBodyOnMouseOver) ;
		//detachEvent("onmouseout", gridBodyOnMouseOut) ;
		//detachEvent("onmousedown", gridBodyOnMouseDown) ;
		//detachEvent("oncontextmenu", gridBodyOnRightClick) ;
		detachEvent("onclick", gridBodyOnClick) ;
		detachEvent("ondblclick", gridBodyOnDblClick) ;
		detachEvent("onscroll", gridBodyOnScroll) ;
	}
	
	with (element) {
		detachEvent("onmousemove", elementOnMouseMove) ;
		detachEvent("onmouseup", elementOnMouseUp) ;
		detachEvent("oncontextmenu", elementOnRightClick) ;
	}
	
	window.detachEvent("onload", actionOnLoad) ;
}

function actionOnLoad() {
	with (gridHeadRow) {
		var grChildLength = children.length ;
		gridRowExtraSpan = children[grChildLength - 1] ;
		gridRowExtraSpan.innerHTML = "<table class='clsHeadBack' width='100%' cellspacing='0' cellpadding='0'><tr><td class='leftCell'></td><td class='centerCell'></td><td class='leftCell'></td></tr></table>" ;
		gridRowExtraSpan.style.cursor = "default" ;
		for (var i = 0; i < grChildLength - 1; i++) {
			var elChild = children[i] ;
			var elCaption = elChild.innerText ;
			try {
				elChild.style.width = ((parseInt(elChild.width) / 100) * element.offsetWidth) ;
			}
			catch(e) {
				elChild.style.width = 100 ;
			}
			setColWidth(i, elChild.offsetWidth) ;
			gridHeadWidth += elChild.offsetWidth ;
			elChild.innerText = "" ;
			elChild.innerHTML = "<table class='clsHeadBack' width='100%' cellspacing='0' cellpadding='0'><tr><td class='leftCell'></td><td class='centerCell'>" + elCaption + "<font class='sortBit'>&nbsp;&nbsp;</font></td><td class='rightCell'></td></tr></table>" ;
		}
	}
	
	elementOnResize() ;
	
	gridHead.style.visibility = "visible" ;
	gridBody.style.visibility = "visible" ;
}


// ----------------------------------------------------
// element functions   
// ----------------------------------------------------
function documentOnMouseDown() {
	if (getRealMenuItem(window.event.srcElement).className != "2k3MenuItemOver") {
		hideMenu() ;
	}
}


// ----------------------------------------------------
// element functions   
// ----------------------------------------------------
function elementOnMouseUp() {
	with (window.event) {
		returnValue = false ;
		cancelBubble = true ;
	}
	if (inSize) {
		inSizeEndPos = objSizeItem.offsetLeft ;
		objSizeItem.style.visibility = "hidden" ;
		element.releaseCapture() ; 
		if (inSizeColumnIdx != -1) {
			gridHeadRow.children[inSizeColumnIdx].children[0].className = "clsHeadBack" ;
			sizeCol(inSizeColumnIdx, inSizeEndPos - inSizeStartPos) ;
		}
		inSizeColumnIdx = -1 ;
		inSize = false ;
	}
	if (inDrag) {
		objDragItem.style.visibility = "hidden" ;
		objDragToItem1.style.visibility = "hidden" ;
		objDragToItem2.style.visibility = "hidden" ;
		gridHeadRow.children[inDragColumnFromIdx].children[0].className = "clsHeadBack" ;
		hiliteHeader(-1) ;
		element.releaseCapture() ;
		element.style.cursor = "default" ; 
		if (inDragColumnToIdx != -1) {
			moveCols(inDragColumnFromIdx, inDragColumnToIdx) ;
		}
		inDragColumnFromIdx = -1 ;
		inDragColumnToIdx = -1 ;
		inDragColumnCurrentIdx = -1 ;
		inDrag = false ;
	}
	return false ;
}

function elementOnMouseMove() {
	with (window.event) {
		var evSrcElement = srcElement ;
		var cliX = clientX ;
		var cliY = clientY ;
		cancelBubble = true ;
		returnValue = false ;
		if (button == 2) {	return ; }
	}
	if (inSize && objSizeItem != null) {
		if (inSizeColumnIdx != -1) {
			with (gridHeadRow.children[inSizeColumnIdx]) {
				var minLeft = parseInt(offsetLeft) - gridBody.scrollLeft ;
				var colFont = children[0].all.tags("FONT") ;
				with (colFont[colFont.length - 1]) {
					minLeft += parseInt(offsetLeft) ;
					minLeft += parseInt(offsetWidth) + 10 ;
				}
			}
			minLeft += absoluteX ;
			if (cliX > minLeft) {
				objSizeItem.style.posLeft = cliX - 1 ;
			}
		}
		return ;
	}
	if (inDrag && objDragItem != null) {
		if (inDragCounter < 10) {
			inDragCounter++ ;
			return ;
		}
		with (objDragItem) {
			if (style.visibility == "hidden") {
				style.visibility = "visible" ;
			}
			style.posLeft = cliX - (offsetWidth / 2) ;
			style.posTop  = cliY - (offsetHeight - 5) ;
		}
		inDragColumnToIdx = hitTestXY(cliX, cliY) ;
	}
}

function elementOnResize() {
	with (element) {
		var elClientHeight = clientHeight ;
		var elClientWidth = clientWidth ;
	}
	if (elClientHeight < gridHeadHeight || elClientWidth < 1) { return ; }
	with (gridBody) {
		style.height = elClientHeight - 20 ;
		style.width = elClientWidth ;
		gridHeadRow.style.marginLeft = (-scrollLeft) ;
	}
	setGridHeadWidth(elClientHeight, elClientWidth, gridBody.clientHeight) ;
}

function elementOnSelect() {
	with (window.event) {
		cancelBubble = true ;
		returnValue = false ;
	}
	return false ;
}

function elementOnPropertyChange() {
}


var _currentElement = null;


function copyCell() {
	hideMenu();
	clipboardData.setData('text',getCellValue(_currentElement));
}

function copyRow() {
	hideMenu();
	var objRow = getBodySrcRow(_currentElement);
	var text = "";
	if (objRow) {
		for (var i=0; i<objRow.children.length; i++) {
			if(i>0) {
				text += "\t";
			}
			text += getCellValue(objRow.children[i]);
		}
	}
	clipboardData.setData('text',text);
}

function copyColumn() {
	hideMenu();
	var columnIdx = getElementIndex(_currentElement);
	var text = "";
	for (i = 0; i < gridBody.children.length; i++) {
		if(i>0) {
			text += "\r\n";
		}
     		text += getCellValue(gridBody.children[i].children[columnIdx]);
	}
	clipboardData.setData('text',text);
}

function getCellValue(el) {
	return el.innerText.replace(/(^\s*)|(\s*$)/g, "");
}

function elementOnRightClick() {
	_currentElement = window.event.srcElement;
	if (_currentElement.tagName.toUpperCase() == "SPAN") {
		showMenu(objBodyMenu, window.event.clientX, window.event.clientY) ;
	}
	return false ;
}

// ----------------------------------------------------
// gridHead functions
// ----------------------------------------------------
function gridHeadOnMouseOver() {
	if (inSize || inDrag || menuState || getHeadColSrcCell(window.event.srcElement) == gridRowExtraSpan) { return ; }
	var el = getHeadSrcCell(window.event.srcElement) ;
	el.className = "clsHeadBackOver" ;
}

function gridHeadOnMouseOut() {
	if (menuState || getHeadColSrcCell(window.event.srcElement) == gridRowExtraSpan) { return ; }
	var el = getHeadSrcCell(window.event.srcElement) ;
	el.className = "clsHeadBack" ;
}

function gridHeadOnMouseDown() {
	if (menuState) {
		hideMenu() ;
		return ; 
	}
	with (window.event) {
		var evSrcElement = srcElement ;
		var cliX = clientX ;
		var cliY = clientY ;
		cancelBubble = true ;
		returnValue = false ;
		if (button == 2 || getHeadColSrcCell(evSrcElement) == gridRowExtraSpan) { return ; }
	}
	if (evSrcElement.className == "rightCell") {
		inSize = true ;
		inSizeStartPos = cliX ;
		element.setCapture() ;
		initOffsets(element) ;
		with (objSizeItem.style) {
			top = absoluteY ;
			height = element.offsetHeight ;
			left = cliX ;
			if (visibility == "hidden") { visibility = "visible" ; }
		}
		cancelClick = true ;
	}
	else {
		var el = getHeadSrcCell(evSrcElement) ;
		if (getVisibleCount() < 2) { return ; }
		el.className = "clsHeadBackDown" ;
		inDrag = true ;
		inDragCounter = 0 ;
		initOffsets(element) ;
		inDragColumnFromIdx = getElementIndex(el.parentElement) ;
		element.setCapture() ;
		element.style.cursor = "hand" ;
		with (objDragItem) {
			innerText = el.parentElement.innerText ;
			with (style) {
				height = el.parentElement.offsetHeight ;
				width  = el.parentElement.offsetWidth ;
				padding = "3px 0px 0px 4px" ;
			}
		}
	}
}

function gridHeadOnMouseMove() {
	if (inSize || inDrag || menuState) { return ; }
	var cellElement = window.event.srcElement ;
	if (cellElement.className != "rightCell") { return ; }
	var hilightedCell = getHeadColSrcCell(cellElement) ;
	inSizeColumnIdx = getElementIndex(hilightedCell) ;
	try {
		cellElement.style.cursor = (inSizeColumnIdx != -1 ? "col-resize" : "hand") ;
	}
	catch(e) {
		cellElement.style.cursor = (inSizeColumnIdx != -1 ? "e-resize" : "hand") ;
	}
}

function gridHeadOnClick() {
	if (inSize || inDrag || cancelClick) {
		cancelClick = false ;
		return ;
	}
	if (window.event.button == 2 || getHeadColSrcCell(window.event.srcElement) == gridRowExtraSpan) { return ; }
	var el = getHeadSrcCell(window.event.srcElement) ;
	if (sortColumnIdx != -1) {	
		gridHeadRow.children[sortColumnIdx].all.tags("FONT")[0].innerText = "  " ;
	}
	var selIndex = getElementIndex(el.parentElement) ;
	if (sortColumnIdx == selIndex) {
		if (sortDirection == "DSC") {
			sortDirection = "ASC" ;
			el.all.tags("FONT")[0].innerText = "6" ;
		}
		else {
			sortDirection = "DSC" ;
			el.all.tags("FONT")[0].innerText = "5" ;
		}
	}
	else {
		sortDirection = "ASC" ;
		el.all.tags("FONT")[0].innerText = "6" ;
	}
	sortColumnIdx = selIndex ;
	sortTable(sortColumnIdx) ;
	el.className = "clsHeadBackOver" ;
}

function gridHeadOnRightClick() {
	var el = getHeadColSrcCell(window.event.srcElement) ;
	with (window.event) {
		cancelBubble = true ;
		returnValue = false ;
	}
	if (el == gridRowExtraSpan) { return false ; }
	menuColumnIdx = getElementIndex(el) ;
	if (menuColumnIdx != -1) {
		showMenu(objMenu, window.event.clientX, window.event.clientY) ;
	}
	return false ;
}


// ----------------------------------------------------
// gridBody functions
// ----------------------------------------------------
function gridBodyOnMouseOver() {
	if (menuState) { return ; }
	/*
	var rowElement = getBodySrcRow(window.event.srcElement) ;
	if (selectToHilite == rowElement) { return ; }
	selectToHilite = null ;
	if (rowElement != null) {
		setRowStyle(rowElement, "gridRowOver") ;
	}
	*/
}

function gridBodyOnMouseOut() {
	if (menuState) { return ; }
	/*
	var rowElement = getBodySrcRow(window.event.srcElement) ;
	if (rowElement != null) {
		if (objSelectedRows.isRowSelected(rowElement)) {
			setRowStyle(rowElement, "gridRowHasFocus") ;
		}
		else {
			setRowStyle(rowElement, "gridRow") ;
		}
	}
	*/
}

function gridBodyOnMouseDown() {
	if (menuState) { return ; }
	/*
	var rowElement = getBodySrcRow(window.event.srcElement) ;
	if (rowElement != null) {
		switch (window.event.button) {
			case 1:
				setRowStyle(rowElement, "gridRowDown") ;
				break ;
		}
	}
	*/
}

function gridBodyOnClick() {
	if (menuState) { return ; }
	var rowElement = getBodySrcRow(window.event.srcElement) ;
	//checkSingleRow(rowElement);
	selectToHilite = rowElement ;
	if (rowElement != null) {
		switch (true) {
			case window.event.shiftKey:
				var lastRow = objSelectedRows.lastRow() ;
				if (lastRow != null) {
					var sIndex = getElementIndex(lastRow) ;
					var lIndex = getElementIndex(rowElement) ;
					if (sIndex > lIndex) {
						for (i = lIndex + 1; i <= sIndex; i++) {
							setRowStyle(gridBody.children[i - 1], "gridRowHasFocus") ;
							if (!objSelectedRows.isRowSelected(gridBody.children[i - 1])) {
								objSelectedRows.addRow(gridBody.children[i - 1]) ;
							}
						}
					}
					else {
						for (i = lIndex + 1; i >= sIndex + 1; i--) {
							setRowStyle(gridBody.children[i - 1], "gridRowHasFocus") ;
							if (!objSelectedRows.isRowSelected(gridBody.children[i - 1])) {
								objSelectedRows.addRow(gridBody.children[i - 1]) ;
							}							
						}
					}
				}
				else {
					objSelectedRows.addSingleRow(rowElement) ;
					setRowStyle(rowElement, "gridRowHasFocus") ;
				}
				break;
			case window.event.ctrlKey:
				if (objSelectedRows.isRowSelected(rowElement)) {
					objSelectedRows.deleteRow(rowElement) ;
					setRowStyle(rowElement, "gridRow") ;
				}
				else {
					objSelectedRows.addRow(rowElement) ;
					setRowStyle(rowElement, "gridRowHasFocus") ;
				}
				break;
			default:	
				if (objSelectedRows.count() > 0) {
					for (i = 0; i < objSelectedRows.count(); i++) {
						var tElement = objSelectedRows.getSelectedRow(i) ;
						setRowStyle(tElement, "gridRow") ;
					}
				}
				objSelectedRows.addSingleRow(rowElement) ;
				setRowStyle(rowElement, "gridRowHasFocus") ;
				break;
		}
		rowselect.fire() ;
		}
}

function gridBodyOnRightClick() {
	if (menuState) { return ; }
	if (element.contextMenu != null) {
		var rowElement = getBodySrcRow(window.event.srcElement) ;
		if (rowElement != null) {
			element.rightClickRowIndex = getElementIndex(rowElement) ;
			rowrightclick.fire() ;
			if (objSelectedRows.isRowSelected(rowElement)) {
				setRowStyle(rowElement, "gridRowHasFocus") ;
			}
			else {
				setRowStyle(rowElement, "gridRow") ;
			}
			showMenu(window.document.all(element.contextMenu), window.event.clientX, window.event.clientY) ;
		}
	}
	with (window.event) {
		cancelBubble = true ;
		returnValue = false ;
	}
	return false ;
}

function gridBodyOnDblClick() {
	if (menuState) { return ; }
	gridBodyOnClick() ;
	rowdblclick.fire() ;
}

function gridBodyOnScroll() {
	gridHeadRow.style.marginLeft = (-window.event.srcElement.scrollLeft) ;
}


// ----------------------------------------------------
// internal functions   
// ----------------------------------------------------
function selectedRows(varData) {
	var arrRows = new Array() ;
	var objLastRowAdded = null ;
	
	this.count = function () {
					return arrRows.length ;
				 }

	this.getSelectedRow = function (varData) {
							  if (varData >= 0 && varData <= this.count()) {
								  return arrRows[varData] ;
							  }
							  else {
								  return null ;
							  }
						  }
	
	this.lastRow = function () {
					   return objLastRowAdded ;
				   }
	
	this.getRows = function () {
					   return arrRows ;
				   }
	
	this.isRowSelected = function (varData) {
							 return (this.getRowIndex(varData) != -1) ;
						 }
	
	this.addSingleRow = function (varData) {
						    arrRows = new Array() ;
							this.addRow(varData) ;
						}
	
	this.addRow = function (varData) {
					  arrRows[arrRows.length] = varData ;
					  objLastRowAdded = varData ;
				  }
	
	this.deleteRow = function (varData) {
						 var idx = this.getRowIndex(varData) ;
						 if (idx != -1) {
							 var c = -1;
							 var tmpArray = new Array() ;
						     for (var i = 0; i < arrRows.length; i++) {
							     if (i != idx) {
							  	     c++ ;
							  	     tmpArray[c] = arrRows[i] ;
							     }
							 }
							 arrRows = tmpArray ;
						 }
				     }

	this.getRowIndex = function (varData) {
						   if (arrRows.length == 0) {
							   return (-1) ;
						   }
						   else {
						       for (var i = 0; i < arrRows.length; i++) {
								   if (varData == arrRows[i]) {
									   return (i) ;
								   }
							   }
							   return (-1) ;
						   }
					   }
}

function stringBuilder(sString) {
	this.length = 0 ;
	
	this.append = function (sString) {
					this.length += (this._parts[this._current++] = String(sString)).length ;
					this._string = null ;
					return this ;
				  } ;
	
	this.toString = function () {
						if (this._string != null) {
							return this._string ;
						}
						var s = this._parts.join("") ;
						this._parts = [s] ;
						this._current = 1 ;
						this.length = s.length ;
						return this._string = s ;
					} ;

	this._current	= 0 ;
	this._parts		= [] ;
	this._string	= null ;
	if (sString != null) {
		this.append(sString) ;
	}
}

function setGridBorder() {
	element.style.borderWidth = element.borderWidth ;
}

function getElement(el, strClassName) {
	var colLength = el.children.length ; 
	for (var i = 0; i < colLength; i++) {
		if (el.children[i].className == strClassName) {
			return el.children[i] ;
		}
	}
	return null ;
}

function getElementIndex(el) {
	var colLength = el.parentElement.children.length ; 
	for (var i = 0; i < colLength; i++) {
		if (el.parentElement.children[i] == el) {
			return i ;
		}
	}
	return -1 ;
}

function initOffsets(el) {
	absoluteX = 0 ;
	absoluteY = 0 ;
	while (el != null) {
		absoluteY += el.offsetTop ;
		absoluteX += el.offsetLeft ;
		el = el.offsetParent ;
	}
}

function getFirstVisible() {
	var colLength = gridHeadRow.children.length ; 
	for (var i = 0; i < colLength; i++) {
		if (gridHeadRow.children[i].style.display != "none") {
			return i ;
		}
	}
	return 0 ;
}

function getVisibleCount() {
	var count = 0 ;
	var colLength = gridHeadRow.children.length ; 
	for (var i = 0; i < colLength; i++) {
		if (gridHeadRow.children[i].style.display != "none") {
			count++ ;
		}
	}
	return count ;
}

function hitTestXY(x, y) {
	var iHit = -1 ;
	with (gridHeadRow.children[getFirstVisible()]) {
		if ((y - absoluteY) > (offsetTop + ((offsetHeight - 3) * 2)) || (y - absoluteY) < (offsetTop - 3)) {
			hiliteHeader(iHit) ;
			return iHit ;
		}
	}
	var colLength = gridHeadRow.children.length - 1 ;
	for (var i = 0; i < colLength; i++) {
		with (gridHeadRow.children[i]) {
			var elOffsetLeft = offsetLeft ;
			var elOffsetWidth = offsetWidth ;
		}
		if ((x - absoluteX) > elOffsetLeft && (x - absoluteX) < (elOffsetLeft + elOffsetWidth)) {
			if ((x - absoluteX) <= (elOffsetLeft + (elOffsetWidth / 2))) {
				hiliteHeader(i) ;
				switch (true) {
					case (inDragColumnFromIdx > i):
						iHit = i ;
						break ;
					case (inDragColumnFromIdx == i):
						iHit = i ;
						break ;
					case (inDragColumnFromIdx < i):
						iHit = i - 1 ;
						break ;
				}
			}
			else {
				if ((x - absoluteX) >= (elOffsetLeft + (elOffsetWidth / 2))) {
					hiliteHeader(i + 1) ;
					switch (true) {
						case (inDragColumnFromIdx > i):
							iHit = i + 1 ;
							break ;
						case (inDragColumnFromIdx == i):
							iHit = i ;
							break ;
						case (inDragColumnFromIdx < i):
							iHit = i ;
							break ;
					}
				}
			}
			if (iHit < 0) {	iHit = 0 ; }
			if (iHit > colLength) {	iHit = colLength ; }
			return iHit ;
		}
	}
	return iHit ;
}

function hiliteHeader(headIndex) {
	if (headIndex == inDragColumnCurrentIdx) {
		return ;
	}
	var o1Style = objDragToItem1.style ;
	var o2Style = objDragToItem2.style ;
	if (headIndex == -1) {
		if (o1Style.visibility != "hidden") { o1Style.visibility = "hidden" ; }
		if (o2Style.visibility != "hidden") { o2Style.visibility = "hidden" ; }
		try {
			if (element.style.cursor != "no-drop") { element.style.cursor = "no-drop" ;	}
		}
		catch(e) {
		}
	}
	else {
		if (element.style.cursor != "hand") { element.style.cursor = "hand" ; }
		if (headIndex == gridHeadRow.children.length) {
			o1Style.posTop = absoluteY - o1Style.posHeight ;
			o1Style.posLeft = gridHeadRow.children[headIndex - 1].offsetLeft + gridHeadRow.children[headIndex - 1].offsetWidth - inDragArrowsOffset + absoluteX ;
		}
		else {
			if (gridHeadRow.children[headIndex].style.display == "none") {
				o1Style.posTop = absoluteY - o1Style.posHeight ;
				o1Style.posLeft = gridHeadRow.children[headIndex - 1].offsetLeft + gridHeadRow.children[headIndex - 1].offsetWidth - inDragArrowsOffset + absoluteX ;
			}
			else {
				o1Style.posTop = absoluteY - o1Style.posHeight ;
				o1Style.posLeft = gridHeadRow.children[headIndex].offsetLeft - inDragArrowsOffset + absoluteX ;
			}
		}
		o2Style.posTop = o1Style.posTop + o1Style.posHeight + 21 ;
		o2Style.posLeft = o1Style.posLeft
		if (o1Style.visibility != "visible") { o1Style.visibility = "visible" ;	}
		if (o2Style.visibility != "visible") { o2Style.visibility = "visible" }
	}
	inDragColumnCurrentIdx = headIndex ;
}

function createAdditionalElements() {
	var txtHTML ;
	objSizeItem = window.document.createElement("DIV") ;
	with (objSizeItem.style) {
		backgroundColor = "buttonshadow" ;
		try {
			cursor = "col-resize" ;
		}
		catch(e) {
			cursor = "e-resize" ;
		}
		position = "absolute" ;
		border = "outset 1px" ;
		width = "2px" ;
		zIndex = 3000 ;
		visibility = "hidden" ;
	}
	window.document.body.insertAdjacentElement("afterBegin", objSizeItem) ;
	
	objDragItem = window.document.createElement("DIV") ;
	with (objDragItem.style) {
		font = "menu" ;
		backgroundColor = "buttonshadow" ;
		cursor = "hand" ;
		position = "absolute" ;
		filter = "progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=45)" ;
		zIndex = 3001 ;
		visibility = "hidden" ;
	}
	window.document.body.insertAdjacentElement("afterBegin", objDragItem) ;
	
	objDragToItem1 = window.document.createElement("DIV") ;
	with (objDragToItem1) {
		innerHTML = "<img src='" + element.currentStyle.posDownImageUrl + "'>" ;
		with (style) {
			height = "9px" ;
			width  = "9px" ;
			backgroundColor = "transparent" ;
			position = "absolute" ;
			zIndex = 3000 ;
			visibility = "hidden" ;
		}
	}
	window.document.body.insertAdjacentElement("afterBegin", objDragToItem1) ;
	
	objDragToItem2 = window.document.createElement("DIV") ;
	with (objDragToItem2) {
		innerHTML = "<img src='" + element.currentStyle.posUpImageUrl + "'>" ;
		with (style) {
			height = "9px" ;
			width  = "9px" ;
			backgroundColor = "transparent" ;
			position = "absolute" ;
			zIndex = 3000 ;
			visibility = "hidden" ;
		}
	}
	window.document.body.insertAdjacentElement("afterBegin", objDragToItem2) ;
	
	objMenu = window.document.createElement("<table id='mnu" + element.id + "' cellspacing='0' cellpadding='0'>") ; 
	with (objMenu.createTHead()) {
		with (insertRow()) {
			with (insertCell()) {
				colSpan = "5" ;
			}
		}
	}
	
	with (objMenu.tBodies[0].insertRow()) {
		className = "2k3MenuItem" ;
		onclick = function () { sortAscending() } ;
		with (insertCell()) {
			className = "2k3MenuVerticalSpacerLeft" ;
		}
		with (insertCell()) {
			className = "2k3MenuImage" ;
			innerHTML = "<img src='" + element.currentStyle.sortAscImageUrl + "' height='16' width='16'>" ;
		}
		with (insertCell()) {
			className = "2k3MenuCaption" ;
			innerHTML = "宋体" ;
		}
		with (insertCell()) {
			className = "2k3MenuNoMore" ;
			innerHTML = "4" ;
		}
		with (insertCell()) {
			className = "2k3MenuVerticalSpacerRight" ;
		}
	}
	
	with (objMenu.tBodies[0].insertRow()) {
		className = "2k3MenuItem" ;
		onclick = function () { sortDescending() } ;
		with (insertCell()) {
			className = "2k3MenuVerticalSpacerLeft" ;
		}
		with (insertCell()) {
			className = "2k3MenuImage" ;
			innerHTML = "<img src='" + element.currentStyle.sortDesImageUrl + "' height='16' width='16'>" ;
		}
		with (insertCell()) {
			className = "2k3MenuCaption" ;
			innerHTML = "宋体" ;
		}
		with (insertCell()) {
			className = "2k3MenuNoMore" ;
			innerHTML = "4" ;
		}
		with (insertCell()) {
			className = "2k3MenuVerticalSpacerRight" ;
		}
	}
	
	with (objMenu.tBodies[0].insertRow()) {
		className = "2k3MenuSeperator" ;
		with (insertCell()) {
			className = "2k3MenuImage" ;
			colSpan = "2" ;
		}
		with (insertCell()) {
			className = "2k3MenuCaption" ;
			colSpan = "3" ;
			innerHTML = "<img>" ;
		}
	}

	with (objMenu.tBodies[0].insertRow()) {
		className = "2k3MenuItem" ;
		onclick = function () { hideColumn() } ;
		with (insertCell()) {
			className = "2k3MenuVerticalSpacerLeft" ;
		}
		with (insertCell()) {
			className = "2k3MenuImage" ;
			innerHTML = "<img src='" + element.currentStyle.transparentImageUrl + "' height='16' width='16'>" ;
		}
		with (insertCell()) {
			className = "2k3MenuCaption" ;
			innerHTML = "宋体" ;
		}
		with (insertCell()) {
			className = "2k3MenuNoMore" ;
			innerHTML = "4" ;
		}
		with (insertCell()) {
			className = "2k3MenuVerticalSpacerRight" ;
		}
	}
	
	with (objMenu.tBodies[0].insertRow()) {
		className = "2k3MenuItem" ;
		onclick = function () { showFieldChooser() } ;
		with (insertCell()) {
			className = "2k3MenuVerticalSpacerLeft" ;
		}
		with (insertCell()) {
			className = "2k3MenuImage" ;
			innerHTML = "<img src='" + element.currentStyle.fieldChooserImageUrl + "' height='16' width='16'>" ;
		}
		with (insertCell()) {
			className = "2k3MenuCaption" ;
			innerHTML = "宋体" ;
		}
		with (insertCell()) {
			className = "2k3MenuNoMore" ;
			innerHTML = "4" ;
		}
		with (insertCell()) {
			className = "2k3MenuVerticalSpacerRight" ;
		}
	}
	
	with (objMenu.createTFoot()) {
		with (insertRow()) {
			with (insertCell()) {
				colSpan = "5" ;
			}
		}
	}

	objMenu.className = "2k3Menu";
	window.document.body.insertAdjacentElement("afterBegin", objMenu) ;

/*------------------------------------------------*/
	objBodyMenu = window.document.createElement("<table id='bmnu" + element.id + "' cellspacing='0' cellpadding='0'>") ; 
	with (objBodyMenu.createTHead()) {
		with (insertRow()) {
			with (insertCell()) {
				colSpan = "5" ;
			}
		}
	}
	
	with (objBodyMenu.tBodies[0].insertRow()) {
		className = "2k3MenuItem" ;
		onclick = function () { copyCell() } ;
		with (insertCell()) {
			className = "2k3MenuVerticalSpacerLeft" ;
		}
		with (insertCell()) {
			className = "2k3MenuImage" ;
			innerHTML = "<img src='" + element.currentStyle.copyImageUrl + "' height='16' width='16'>" ;
		}
		with (insertCell()) {
			className = "2k3MenuCaption" ;
			innerHTML = "宋体" ;
		}
		with (insertCell()) {
			className = "2k3MenuNoMore" ;
			innerHTML = "4" ;
		}
		with (insertCell()) {
			className = "2k3MenuVerticalSpacerRight" ;
		}
	}
	
	with (objBodyMenu.tBodies[0].insertRow()) {
		className = "2k3MenuItem" ;
		onclick = function () { copyRow() } ;
		with (insertCell()) {
			className = "2k3MenuVerticalSpacerLeft" ;
		}
		with (insertCell()) {
			className = "2k3MenuImage" ;
			innerHTML = "<img src='" + element.currentStyle.copyImageUrl + "' height='16' width='16'>" ;
		}
		with (insertCell()) {
			className = "2k3MenuCaption" ;
			innerHTML = "宋体" ;
		}
		with (insertCell()) {
			className = "2k3MenuNoMore" ;
			innerHTML = "4" ;
		}
		with (insertCell()) {
			className = "2k3MenuVerticalSpacerRight" ;
		}
	}

	with (objBodyMenu.tBodies[0].insertRow()) {
		className = "2k3MenuItem" ;
		onclick = function () { copyColumn() } ;
		with (insertCell()) {
			className = "2k3MenuVerticalSpacerLeft" ;
		}
		with (insertCell()) {
			className = "2k3MenuImage" ;
			innerHTML = "<img src='" + element.currentStyle.copyImageUrl + "' height='16' width='16'>" ;
		}
		with (insertCell()) {
			className = "2k3MenuCaption" ;
			innerHTML = "宋体" ;
		}
		with (insertCell()) {
			className = "2k3MenuNoMore" ;
			innerHTML = "4" ;
		}
		with (insertCell()) {
			className = "2k3MenuVerticalSpacerRight" ;
		}
	}
	
	with (objBodyMenu.createTFoot()) {
		with (insertRow()) {
			with (insertCell()) {
				colSpan = "5" ;
			}
		}
	}

	objBodyMenu.className = "2k3Menu";
	window.document.body.insertAdjacentElement("afterBegin", objBodyMenu) ;
/*--------------------------------------------------------------------------------------*/

	objWindow = window.document.createElement("DIV") ;
	txtHTML =  "<div class='windowCaption' id='" + element.id + "_capText" + "'>Field Chooser</div><img class='" + "captionImage" + "' src='" + element.currentStyle.fieldChooserImageUrl + "' height='16' width='16'>" ;
	txtHTML += "<span class='windowButtonClose' id='" + element.id + "_btnClose" + "'><img></span>" ;
	txtHTML += "<iframe class='windowContent' id='" + element.id + "_winContent" + "'></iframe>" ;
	with (objWindow) {
		id = element.id & "_fcWindow" ;
		className = "2k3Dialog" ;
		innerHTML = txtHTML ;
		style.visibility = "hidden" ;
	}
	window.document.body.insertAdjacentElement("afterBegin", objWindow) ;

}

function setRowColors() {
	if (element.altRowColor != "") {
		var gbChildLength = gridBody.children.length ;
		for (var i = 0; i < gbChildLength; i++) {
			with (gridBody.children[i]) {
				if ((Math.round(i / 2) * 2) == i) {
					style.borderColor = element.altRowColor ;
					style.backgroundColor = element.altRowColor ;
				}
				else {
					style.borderColor = "red" ;
					style.backgroundColor = "window" ;
				}
			}
			setRowStyle(gridBody.children[i], "gridRow") ;
			if (objSelectedRows.isRowSelected(gridBody.children[i])) {
				setRowStyle(gridBody.children[i], "gridRowHasFocus") ;
			}
		}
	}
}

function setRowStyle(objUnknown, strClassName) {
	if (objUnknown != null) {
		var bColor = null ;
		switch (strClassName) {
			case "gridRow":
				bColor = objUnknown.style.backgroundColor ;
				break ;
			case "gridRowOver":
				bColor = element.currentStyle.hoverBorderColor ;
				break ;
			case "gridRowDown":
				bColor = element.currentStyle.activeBorderColor ;
				break ;
			case "gridRowHasFocus":
				bColor = element.currentStyle.selectedBorderColor ;
				break ;
		}
		with (objUnknown) {
			className = strClassName ;
			style.borderColor = bColor ;
		}
	}
}

function setGridHeadWidth(elClientHeight, elClientWidth, gridBodyClientHeight) {
	var horScrollHeight = (elClientHeight - gridBodyClientHeight) - 20 ;
	if (elClientWidth >= gridHeadWidth && horScrollHeight < 1) {
		gridHeadRow.style.marginLeft = 0 ;
		gridBody.scrollLeft = 0 ;
		gridRowExtraSpan.style.width = elClientWidth - gridHeadWidth ;
	}
	else {
		gridRowExtraSpan.style.width = horScrollHeight + element.borderWidth ;
	}
}

function getGridRowWidth() {
	gridHeadWidth = 0 ;
	with (gridHeadRow) {
		for (var i = 0; i < children.length - 1; i++) {
			with (children[i]) {
				if (style.display != "none") {
					gridHeadWidth += offsetWidth ;
				}
			}
		}
	}
}

function getBodySrcRow(el) {
	if (el.className.toUpperCase() == "GRIDBODY") {
		return null ;
	}
	if (el.tagName.toUpperCase() == "SPAN") {
		return el.parentElement ;
	}  
	if (el.tagName.toUpperCase() == "INPUT") {
		return el.parentElement.parentElement ;
	} 
	if (el.tagName.toUpperCase() == "A") {
		return el.parentElement.parentElement ;
	}  
	if (el.tagName.toUpperCase() == "IMG") {
		return el.parentElement.parentElement ;
	}
	if (el.tagName.toUpperCase() == "SELECT") {
		return null ;
	}	
	return el ;
}

function getHeadSrcCell(el) {
	var checkElement = el ;
	if (checkElement != null) {
		if (checkElement.className == "sortBit") {
			checkElement = checkElement.parentElement ;
		}
		if (checkElement.className == "leftCell" || checkElement.className == "centerCell" || checkElement.className == "rightCell") {
			checkElement = checkElement.offsetParent;
		}
	}
	return checkElement ;
}

function getHeadColSrcCell(el) {
	var checkElement = el ;
	if (checkElement != null) {
		if (checkElement.className == "sortBit") {
			checkElement = checkElement.parentElement ;
		}
		if (checkElement.className == "leftCell" || checkElement.className == "centerCell" || checkElement.className == "rightCell") {
			checkElement = checkElement.offsetParent.offsetParent;
		}
	}
	return checkElement ;
}


// ----------------------------------------------------
// sort methods   
// ----------------------------------------------------
function sortTable(sortColumn) {
	var bReverse = (sortDirection == "DSC") ;
	for (i = 0; i < gridBody.children.length; i++) {
     		var rowInsert = getRowValue(gridBody.children[i].children[sortColumn]) ;
		//var rowInsert = (gridBody.children[i].children[sortColumn].innerText).toLowerCase() ;
		for (s = 0; s <= i; s++) {
			var rowCurrent = getRowValue(gridBody.children[s].children[sortColumn]);
			//var rowCurrent = (gridBody.children[s].children[sortColumn].innerText).toLowerCase() ;
			if (isNaN(rowCurrent)){
				if (((!bReverse && rowInsert < rowCurrent) || (bReverse && rowInsert > rowCurrent)) && (i != s)) {
				    gridBody.insertBefore(gridBody.children[i], gridBody.children[s]) ;
				    break ;
				}
			}else{
				if (((!bReverse && parseFloat(rowInsert) < parseFloat(rowCurrent)) || (bReverse && parseFloat(rowInsert) > parseFloat(rowCurrent))) && (i != s)) {
				    gridBody.insertBefore(gridBody.children[i], gridBody.children[s]) ;
				    break ;
				}
			}
		}
	}
	setRowColors() ;
} 

function getRowValue(obj) {
	var child = obj.all.tags("INPUT");
	if (child.length > 0) {
		if (child[0].type.toLowerCase() == "text") {
			return child[0].value;
		}
	} else {
		return (obj.innerText).toLowerCase();
	}
}

// ----------------------------------------------------
// column methods   
// ----------------------------------------------------
function setColWidth(intCol, intWidth) {
	var gbChildLength = gridBody.children.length ;
	for (var i = 0; i < gbChildLength; i++) {
		var elChild = gridBody.children[i] ;
		with (elChild.children[intCol].style) {
			border = "none" ;
			if (intCol == elChild.children.length - 1) {
				width = intWidth - 2 ;
			}
			else {
				width = intWidth ;
			}
		}
	}
}

function sizeCol(intCol, intWidth) {
	with (gridHeadRow.children[intCol]) {
		var newWidth = offsetWidth + intWidth ;
		style.width = newWidth ;
		newWidth = offsetWidth ;
	}
	setColWidth(intCol, newWidth) ;
	getGridRowWidth() ;
	elementOnResize() ;
}

function moveCols(fCol, tCol) {
	var elTextSave = "" ;
	if (fCol == tCol) {	return ; }
	if (sortColumnIdx != -1) {
		elTextSave = gridHeadRow.children[sortColumnIdx].innerText ;
	}
	moveHeaderCols(fCol, tCol) ;
	for (var i = 0;  i < gridBody.children.length; i++) {
		moveBodyCols(i, fCol, tCol) ;
	}
	for (var i = 0;  i < gridHeadRow.children.length - 1; i++) {
		if (elTextSave != "") {
			if (elTextSave == gridHeadRow.children[i].innerText) {
				sortColumnIdx = i ;
				elTextSave = "" ;
			}
		}
	}
	getGridRowWidth() ;
	elementOnResize() ;
}

function moveHeaderCols(fCol, tCol) {
	if (fCol > tCol) {
		var dCol = -1 ;
		var nCol = fCol - tCol ;
	}
	else {
		var dCol = 1 ;
		var nCol = tCol - fCol ;
	}
	var saveHTML = gridHeadRow.children[fCol].innerHTML ;
	var saveWidth = gridHeadRow.children[fCol].offsetWidth ;
	for (var i = 0;  i < nCol; i++) {
		gridHeadRow.children[fCol].innerHTML = gridHeadRow.children[fCol + dCol].innerHTML ;
		gridHeadRow.children[fCol].style.width = gridHeadRow.children[fCol + dCol].offsetWidth ;
		fCol = fCol + dCol ;
	}
	gridHeadRow.children[tCol].innerHTML = saveHTML ;
	gridHeadRow.children[tCol].style.width = saveWidth ;
}

function moveBodyCols(iRow, fCol, tCol) {
	if (fCol > tCol) {
		var dCol = -1 ;
		var nCol = fCol - tCol ;
	}
	else {
		var dCol = 1 ;
		var nCol = tCol - fCol ;
	}
	var saveHTML = gridBody.children[iRow].children[fCol].innerHTML ;
	var saveWidth = gridBody.children[iRow].children[fCol].offsetWidth ;
	for (var i = 0;  i < nCol; i++) {
		gridBody.children[iRow].children[fCol].innerHTML = gridBody.children[iRow].children[fCol + dCol].innerHTML ;
		gridBody.children[iRow].children[fCol].style.width = gridBody.children[iRow].children[fCol + dCol].offsetWidth ;
		fCol = fCol + dCol ;
	}
	gridBody.children[iRow].children[tCol].innerHTML = saveHTML ;
	gridBody.children[iRow].children[tCol].style.width = saveWidth ;
}


// ----------------------------------------------------
// menu methods   
// ----------------------------------------------------
function showMenu(elMenu, x, y) {
	with (parentElement.document.body) {	
		var intRightEdge = clientWidth - x ;
		var intBottomEdge = clientHeight - y ;
		var intscrollLeft = scrollLeft + x ;
		var intscrollTop = scrollTop + y ;
	}
	with (elMenu) {
		if (intRightEdge < offsetWidth) {
			var mnuX = intscrollLeft - offsetWidth ;
		}
		else {
			var mnuX = intscrollLeft ;
		}
		if (intBottomEdge < offsetHeight) {
			var mnuY = intscrollTop - offsetHeight ;
		}
		else {
			var mnuY = intscrollTop ;
		}
		style.zIndex = element.zIndex + 1 ;
	}
	elMenu.showMenu(mnuX, mnuY, 0) ;
	window.document.attachEvent("onmousedown", documentOnMouseDown) ;
	menuState = true ;
}

function hideMenu() {
	window.document.detachEvent("onmousedown", documentOnMouseDown) ;
	objMenu.hideMenu() ;
	objBodyMenu.hideMenu() ;
	if (menuColumnIdx != -1) {
		gridHeadRow.children[menuColumnIdx].children[0].className = "clsHeadBack" ;
	}
	if (element.contextMenu != null) {
		window.document.all(element.contextMenu).hideMenu() ;
	}
	menuState = false ;
}

function getRealMenuItem(el) {
	if (el != null) {
		if (el.tagName == "IMG") {
			return el.parentElement.parentElement.parentElement ;
		}
		if (el.tagName == "B") {
			el = el.parentElement ;
		}
		switch (el.className) {
			case "2k3MenuVerticalSpacerLeft":
				return el.parentElement ;
				break ;
			case "2k3MenuImage":
				return el.parentElement ;
				break ;
			case "2k3MenuCaption":
				return el.parentElement ;
				break ;
			case "2k3MenuMore":
				return el.parentElement ;
				break ;
			case "2k3MenuNoMore":
				return el.parentElement ;
				break ;
			case "2k3MenuVerticalSpacerRight":
				return el.parentElement ;
				break ;
			default:
				return el ;
				break ;
		}
	}
	else {
		return el ;
	}
}


// ----------------------------------------------------
// field-chooser methods   
// ----------------------------------------------------
function showFieldChooserEx() {
	writeFieldChooserHTML() ;
	with (window.document.getElementById(element.id & "_fcWindow")) {
		left = objMenu.offsetLeft ;
		top = objMenu.offsetTop ;
		width = 150 ;
		height = 170 ;
		style.visibility = "visible" ;
		style.zIndex = element.style.zIndex + 1 ;
	}
}

function buildFieldChooserHTML() {
	var objString = new stringBuilder() ;
	objString.append("<html><head><title>Field Chooser</title>") ;
	objString.append("<style>body {padding: 0px; margin: 0px;} .small {font: menu; white-space: nowrap;}</style>") ;
	objString.append("</head><body>") ;
	for (i = 0; i < gridHeadRow.children.length - 1; i++) {
		objString.append("<input type='checkbox' class='small' id='check_" + i + "' ") ;
		objString.append("onclick='jscript: parent." + element.id + ".doAction(" + i + ");'") ;
		if (gridHeadRow.children[i].style.display != "none") {
			objString.append(" checked='true'") ;
		}
		objString.append("><label class='small' for='check_" + i + "'>" + gridHeadRow.children[i].innerText + "</label><br>") ;
	}
	return objString.toString() + "</body></html>" ;
}

function writeFieldChooserHTML() {
	var winContentDoc = window.document.getElementById(element.id + "_winContent") ;
	with (winContentDoc.contentWindow.document) {
		open() ;
		write(buildFieldChooserHTML()) ;
		close() ;
	}
}


// ----------------------------------------------------
// public methods   
// ----------------------------------------------------
function getSelectedCount() {
	return objSelectedRows.count() ;
}

function getSelectedRow(idx) {
	return objSelectedRows.getSelectedRow(idx) ;
}

function sortAscending() {
	hideMenu() ;
	if (menuColumnIdx != -1) {
		if (sortColumnIdx != -1) {
			gridHeadRow.children[sortColumnIdx].all.tags("FONT")[0].innerText = "  " ;
		}
		sortDirection = "ASC" ;
		gridHeadRow.children[menuColumnIdx].all.tags("FONT")[0].innerText = "6" ;
		sortTable(menuColumnIdx) ;
		sortColumnIdx = menuColumnIdx ;
	}
	menuColumnIdx = -1 ;
}

function sortDescending() {
	hideMenu() ;
	if (menuColumnIdx != -1) {
		if (sortColumnIdx != -1) {
			gridHeadRow.children[sortColumnIdx].all.tags("FONT")[0].innerText = "  " ;
		}
		sortDirection = "DSC" ;
		gridHeadRow.children[menuColumnIdx].all.tags("FONT")[0].innerText = "5" ;
		sortTable(menuColumnIdx) ;
		sortColumnIdx = menuColumnIdx ;
	}
	menuColumnIdx = -1 ;
}

function showColumn(iCol) {
	if (iCol != -1) {
		gridHeadRow.children[iCol].style.display = "" ;
		for (var iRow = 0; iRow < gridBody.children.length; iRow++) {
			gridBody.children[iRow].children[iCol].style.display = "" ;
		}
	}
	getGridRowWidth() ;
	elementOnResize() ;
}

function hideColumn(iCol) {
	hideMenu() ;
	if (iCol != null) {
		menuColumnIdx = iCol ;
	}
	if (menuColumnIdx != -1) {
		if (getVisibleCount() == 1) {
			alert("You must have at least one column visible!") ;
			return ;
		}
		gridHeadRow.children[menuColumnIdx].style.display = "none" ;
		for (var iRow = 0; iRow < gridBody.children.length; iRow++) {
			gridBody.children[iRow].children[menuColumnIdx].style.display = "none"
		}
	}
	getGridRowWidth() ;
	elementOnResize() ;
}

function gridRowCount() {
	return gridBody.children.length ;
}

function addGridRow(rowId, columnValues, beforePos) {
	var strHTML = "";
	var arrValues = columnValues.split("|") ;
	for (var i = 0; i < arrValues.length; i++) {
		strHTML += "<span>" + arrValues[i] + "</span>" ;
	}
	var objRowItem = document.createElement("DIV") ;
	with (objRowItem) {
		id = rowId ;
		className = "gridRow" ;
		innerHTML = strHTML ;
		style.visibility = "hidden" ;
	}
	gridBody.insertAdjacentElement("beforeEnd", objRowItem) ;
	switch (true) {
		case (beforePos == ""):
			gridBody.insertBefore(objRowItem, gridBody.children[gridBody.children.length - 2]) ;
			break ;
		case (beforePos == "atStart"):
			gridBody.insertBefore(objRowItem, gridBody.children[0]) ;
			break ;
		case (beforePos == "atEnd"):
			gridBody.insertBefore(objRowItem, gridBody.children[gridBody.children.length - 2]) ;
			break ;
		case (!parseInt(beforePos).NaN):
			gridBody.insertBefore(objRowItem, gridBody.children[parseInt(beforePos) + 1]) ;
			break ;
		default:
			gridBody.insertBefore(objRowItem, gridBody.children[gridBody.children.length - 2]) ;
			break ;
	}
	for (var i = 0; i < gridHeadRow.children.length - 1; i++) {
		setColWidth(i, parseInt(gridHeadRow.children[i].style.width)) ;
		objRowItem.children[i].style.display = gridHeadRow.children[i].style.display ;
	}
	setRowColors() ;
	objRowItem.style.visibility = "visible" ;
	element.rowCount = gridBody.children.length - 1 ;
	rowcountchange.fire() ;
}

function deleteGridRow(id) {
	el = element.document.all(id) ;
	if (el != null) {
		el.removeNode(true) ;
	}
	else {
		alert("Row '" + id + "' was not found!") ;
	}
	setRowColors() ;
	element.rowCount = gridBody.children.length - 1 ;
	rowcountchange.fire() ;
}

function setRowSelected(idx, state) {
	if (idx <= gridBody.children.length) {
		el = gridBody.children[idx] ;
		if (el != null) {
			if (state) {
				objSelectedRows.addRow(el) ;
				setRowStyle(el, "gridRowHasFocus") ;
			}
			else {
				objSelectedRows.deleteRow(el) ;
				setRowStyle(el, "gridRow") ;
			}
			el = null ;
		}
	}
}

function showFieldChooser() {
	hideMenu() ;
	showFieldChooserEx() ;
}

function doAction(iCol) {
	if (gridHeadRow.children[iCol].style.display == "none") {
		showColumn(iCol) ;
	}
	else {
		hideColumn(iCol) ;
	}
	writeFieldChooserHTML() ;
}


function checkSingleRow(row){
	if (row == null) {
		return;
	}
	var se;
	var tags;
	var se = window.event.srcElement;

	if (se.tagName.toUpperCase() == "INPUT") {	
		if (se.type.toUpperCase() == "CHECKBOX") {
			return;
		}
	}
	tags = row.all.tags("INPUT");
	for (var i=0; i<tags.length; i++) {
		if (tags[i].type.toUpperCase() == "CHECKBOX") { 
			tags[i].checked = !tags[i].checked;
		}
	}
}
</script>

</PUBLIC:COMPONENT>