function getScrollBarWidth() {
	var objDiv = document.createElement('DIV');
	objDiv.style.top = -500;
	objDiv.style.width = 100;
	objDiv.style.height = 100;
	objDiv.style.overflow = 'scroll';
	objDiv.style.position = 'absolute';
	
	document.body.appendChild(objDiv);
	
	var scrollWidth = objDiv.offsetWidth - objDiv.scrollWidth;
	
	document.body.removeChild(objDiv);
	
	return scrollWidth;
}

function headerMouseMove(obj, cursor) {
	obj.style.cursor = (event.offsetX >= obj.offsetWidth - 8) ? 'col-resize' : cursor;
}

function headerMouseDown(obj) {
	if (obj.style.cursor != 'col-resize') return;
	
	var objMouse = obj.parentNode.parentNode.parentNode.previousSibling;
	objMouse.currentTD = obj.uniqueID;
	objMouse.prevX = event.clientX;
	objMouse.controlName = obj.parentNode.parentNode.parentNode.id.split("_")[0];
	objMouse.setCapture();
}	

function mouseDivMouseMove(obj) {
	var objTD = eval(obj.currentTD);
	var prevX = obj.prevX;
	var offset = event.clientX - prevX;
	var newWidth = parseInt(objTD.offsetWidth) + offset;
	
	if (newWidth > 10)
		objTD.style.width = newWidth;
	
	synWidth(obj.controlName);
	obj.prevX = event.clientX;
}

function mouseDivMouseUp(obj) {
	obj.releaseCapture();
}

function synWidth(name, init) {
	var headerTable = document.getElementById(name + '_HeaderTable');
	var footerTable = document.getElementById(name + '_FooterTable');
  var itemTable = document.getElementById(name + '_ItemTable');

	if (init) {
		var table = document.getElementById(name);
		var mainDiv = document.getElementById(name + '_MainDiv');
    var itemDiv = document.getElementById(name + '_ItemDiv');
		var vScrollBar = document.getElementById(name + '_VScrollBar');
		var vScrollBarLength = document.getElementById(name + '_VScrollBarLength');
    
		var height = mainDiv.clientHeight;
		if (height != 0) {
			if (headerTable) height -= headerTable.offsetHeight;
			if (footerTable) height -= footerTable.offsetHeight;
			itemDiv.style.height = height;
		}
		
		if (vScrollBar) {
			vScrollBar.style.width = getScrollBarWidth();
			vScrollBar.style.height = mainDiv.clientHeight;
			vScrollBarLength.style.height = vScrollBar.offsetHeight + itemTable.offsetHeight - itemDiv.offsetHeight;
		}

		mainDiv.style.width = table.offsetWidth - 2;
		if (vScrollBar) mainDiv.style.width = mainDiv.offsetWidth - vScrollBar.offsetWidth;
	}


	if (headerTable)
		var rowHead = headerTable.firstChild.firstChild;
	else
		return;

	if (footerTable)
		var rowFoot = footerTable.firstChild.firstChild;

	var rowItem = itemTable.firstChild.firstChild;
	
	for (var i=0; i < rowHead.children.length; i++) {
		if (rowItem && rowItem.children.length == rowHead.children.length)
			rowItem.children(i).style.width = rowHead.children(i).offsetWidth;
		if (rowFoot) rowFoot.children(i).style.width = rowHead.children(i).offsetWidth;
	}
}

function getControlName(obj) {
	return obj.id.split("_")[0];
}

function headerDblClick(obj) {
	if (obj.style.cursor != 'col-resize') return;
	
	var objTR = obj.parentNode;
	var index;
	
	//get the index of the clicked column
	for (index = 0; index < objTR.children.length; index++)
		if (objTR.children(index) == obj) break;
		
	var headerTable = objTR.parentNode.parentNode;
	var controlName = getControlName(headerTable);
	var itemTable = eval(controlName + "_ItemTable");
	var itemRow = itemTable.firstChild.firstChild;
	
	if (itemRow.nextSibling.firstChild.colSpan > 1) return; //no record in the list

	itemRow.children(index).style.width = 1;
	
	//get the max length of the columns
	var max = 8;
	while (itemRow != null) {
		if (itemRow.firstChild.colSpan == 1 && itemRow.children(index).scrollWidth > max)
			max = itemRow.children(index).scrollWidth;

		itemRow = itemRow.nextSibling;
	}
	
	obj.style.width = max;
	synWidth(controlName);
}

function itemDivMouseWheel(obj) {
	var controlName = obj.id.split("_")[0];
	var action = (event.wheelDelta > 0) ? 'scrollbarUp' : 'scrollbarDown';
	eval(controlName + '_VScrollBar').doScroll(action);
}

function checkAllClick(obj) {
	var objTD = obj.parentNode;
	var objTR = objTD.parentNode;
	var index;

	for (index = 0; index < objTR.children.length; index++)
		if (objTR.children(index) == objTD) break;
		
	var headerTable = objTR.parentNode.parentNode;
	var itemTable = eval(getControlName(headerTable) + "_ItemTable");
	var itemRow = itemTable.firstChild.children(1);
	
	if (itemRow.children(index) == null) return;
	if (itemRow.children(index).firstChild.tagName != 'INPUT') return;
	
	while (itemRow != null) {
		itemRow.children(index).firstChild.checked = obj.checked;	
		itemRow = itemRow.nextSibling;
	}
}

function checkClick(obj) {
	var objTD = obj.parentNode;
	var objTR = objTD.parentNode;
	var index;
	
	for (index = 0; index < objTR.children.length; index++)
		if (objTR.children(index) == objTD) break;
		
	var itemTable = objTR.parentNode.parentNode;
	var headerTable = eval(getControlName(itemTable) + "_HeaderTable");
	var headerCheck = headerTable.firstChild.firstChild.children(index).firstChild;
	
	headerCheck.checked = false;

	if (obj.checked == false) return;

	var itemRow = itemTable.firstChild.children(1);
	while (itemRow != null) {
		if (itemRow.children(index).firstChild.checked == false) return;
		itemRow = itemRow.nextSibling;
	}
	
	headerCheck.checked = true;
}
