
function installTableDefaultListener(Main_Tab){
	//
	Main_Tab.CURRENT_ROW_CHANGE="CURRENT_ROW_CHANGE";
	Main_Tab.ON_CLICK="ON_CLICK";
	Main_Tab.ON_COLUMN_CLICK="ON_COLUMN_CLICK";
	Main_Tab.ON_DB_CLICK="ON_DB_CLICK";
	Main_Tab.PARTIAL_LOCATE="0";
    Main_Tab.FULL_LOCATE="1";
	
	//
	Main_Tab.dragElement = document.createElement("DIV");
	
	//
	Main_Tab.dragElement.innerHTML= "";
	Main_Tab.dragElement.style.textAlign 	= "center";
	Main_Tab.dragElement.style.position 	= "absolute";
	Main_Tab.dragElement.style.cursor 	= "hand";
	Main_Tab.dragElement.style.border 	= "1 solid black";
	Main_Tab.dragElement.style.display 	= "none";
	Main_Tab.dragElement.style.zIndex 	= "999";
	document.body.insertBefore(Main_Tab.dragElement);
	
	//
	Main_Tab.splitLine = window.document.createElement("DIV");
	Main_Tab.splitLine.style.position = "absolute";
	Main_Tab.splitLine.style.backgroundColor="#000000";
	Main_Tab.splitLine.style.width=1;
	Main_Tab.splitLine.style.display="none";
	window.document.body.appendChild(Main_Tab.splitLine);
	/*if (!Main_Tab.width || Main_Tab.width.indexOf("%")>0) {
		alert("Please Set "+Main_Tab.id+".width");
		return;
	}*/
	
	//
	Main_Tab.currentRow=null;
	
	//
	Main_Tab.listeners=new EventListenerList();
	
	//
	Main_Tab.currentCell={};
	Main_Tab.currentCell.rowIndex=-1;
	Main_Tab.currentCell.colIndex=0;
	
	if (Main_Tab.dataTableName && Main_Tab.dataTableName!="") {
		var dt=eval(Main_Tab.dataTableName);
		Main_Tab.dataTable=null;
		Main_Tab.setDataTable(dt);
	}
	
	Main_Tab.setDataTable=function(ds) {
		if (Main_Tab.dataTable==ds) return;
	    if (Main_Tab.dataTable) {
			Main_Tab.removeDataTableDefaultListener(Main_Tab.dataTable);
		}
		Main_Tab.dataTable=ds;
		if (ds) {
			var dt=ds;
			Main_Tab.repaintDataTable(dt);
		    Main_Tab.addDataTableDefaultListener(dt);
		} else Main_Tab.repaintDataTable(null);
    }
	
	Main_Tab.removeDataTableDefaultListener=function(dt) {
		dt.removeDeleteRecordListener(Main_Tab.doDefaultDeleteRecord);
		dt.removeInsertRecordListener(Main_Tab.doDefaultInsertRecord);
		dt.removeAfterScrollListener(Main_Tab.doDefaultAfterScroll);
		dt.removePropertyChangeListener(Main_Tab.doDefaultPropertyChange);
	}
	
	Main_Tab.addDataTableDefaultListener=function(dt) {
		dt.addDeleteRecordListener(Main_Tab.doDefaultDeleteRecord);
		dt.addInsertRecordListener(Main_Tab.doDefaultInsertRecord);
		dt.addAfterScrollListener(Main_Tab.doDefaultAfterScroll);
		dt.addPropertyChangeListener(Main_Tab.doDefaultPropertyChange);
	}

    Main_Tab.doDefaultAfterScroll=function(e) {
		Main_Tab.changeCurrentRow(e.toRowIndex);
	}
	
	Main_Tab.doDefaultDeleteRecord=function(e) {
		Main_Tab.removeRow(e.index);
	}
	
	Main_Tab.doDefaultInsertRecord=function(e) {
		Main_Tab.insertRowAtInternal(e.index);
		for (var j=0;j<Main_Tab.getColumnCount();j++) {
		   var colName=Main_Tab.getColumnByIndex(j).COLUMN_NAME;
		   
		   Main_Tab.setValueByIndexAtInternal(e.index,j,e.source.getValueByName(e.index,colName),
												 								e.source.getFieldSet().getFieldByName(colName).getType());
		}
	}
	
	Main_Tab.doDefaultPropertyChange=function(e) {
		if (e.propertyName=="VALUE_CHANGE") {
	        var column=Main_Tab.getColumnByName(e.source.getFieldSet().getFieldByIndex(e.index).getName());
			if (column) {
				Main_Tab.setValueByIndexAtInternal(e.rowIndex,column.cellIndex-1,e.source.getValueByIndex(e.index),
																										  e.source.getFieldSet().getFieldByIndex(e.index).getType());
				
			}
		} else if (e.propertyName=="ALL_CHANGE") {
			Main_Tab.repaintDataTable(e.source);
		}
	}
	
	Main_Tab.repaintDataTable=function(dt) {
		Main_Tab.removeAllRow();
		if (!dt) return;
		for (var i=0;i<dt.getRecordCount();i++) {
			Main_Tab.insertRowAtInternal(Main_Tab.getRowCount(),false);
			for (var j=0;j<Main_Tab.getColumnCount();j++) {
				var colName=Main_Tab.getColumnByIndex(j).COLUMN_NAME;
				Main_Tab.setValueByIndexAtInternal(i,j,dt.getValueByName(i,colName),
																		 dt.getFieldSet().getFieldByIndex(j).getType());
			}
		}
		Main_Tab.resetRowNum();
		Main_Tab.changeCurrentRow(dt.getCurrentIndex());
	}
	
	/**
	 *
	 */
	Main_Tab.addCurrentRowChangeListener=function(ls) {
        Main_Tab.listeners.add(Main_Tab.CURRENT_ROW_CHANGE,ls);
    }
	Main_Tab.removeCurrentRowChangeListener=function(ls) {
        Main_Tab.listeners.remove(Main_Tab.CURRENT_ROW_CHANGE,ls);
    }
	
	/**
	 *
	 */
	Main_Tab.addColumnClickListener=function(ls) {
        Main_Tab.listeners.add(Main_Tab.ON_COLUMN_CLICK,ls);
    }
	Main_Tab.removeColumnClickListener=function(ls) {
        Main_Tab.listeners.remove(Main_Tab.ON_COLUMN_CLICK,ls);
    }
	
	/**
	 *
	 */
	Main_Tab.addClickListener=function(ls) {
        Main_Tab.listeners.add(Main_Tab.ON_CLICK,ls);
    }
	Main_Tab.removeClickListener=function(ls) {
        Main_Tab.listeners.remove(Main_Tab.ON_CLICK,ls);
    }
	
	/**
	 *
	 */
	Main_Tab.addDbClickListener=function(ls) {
        Main_Tab.listeners.add(Main_Tab.ON_DB_CLICK,ls);
    }
	Main_Tab.removeDbClickListener=function(ls) {
        Main_Tab.listeners.remove(Main_Tab.ON_DB_CLICK,ls);
    }
	
	/**
	 *
	 */
	Main_Tab.getValueByIndex=function(rowIndex,colIndex) {
		return Main_Tab.rows[rowIndex+1].cells[colIndex+1].childNodes[0].innerHTML;
	}
	
	/**
	 *
	 */
	Main_Tab.getValueByName=function(rowIndex,colName) {
		var column=Main_Tab.getColumnByName(colName);
		if (column) {
		   return Main_Tab.getValueByIndex(rowIndex,column.cellIndex-1);
		}
		return null;
	}
	
	Main_Tab.getCurrentValueByIndex=function(colIndex) {
		if (!Main_Tab.currentRow) return null;
		return Main_Tab.getValueByIndex(Main_Tab.currentRow.rowIndex-1,colIndex);
	}
	
	Main_Tab.getCurrentValueByName=function(colName) {
		if (!Main_Tab.currentRow) return null;
		return Main_Tab.getValueByName(Main_Tab.currentRow.rowIndex-1,colName);
	}
	
	Main_Tab.setValueByIndex=function(rowIndex,colIndex,value,dataType) {
		if (Main_Tab.dataTable) {
		   var columnName=Main_Tab.getColumnByIndex(colIndex).COLUMN_NAME;
		   if (dataType==null) dataType=Main_Tab.dataTable.getFieldSet().getFieldByName(columnName).getType();
		   Main_Tab.dataTable.setValueByName(rowIndex,columnName,value,dataType);
		   return;
		}
		Main_Tab.setValueByIndexAtInternal(rowIndex,colIndex,value,dataType);
	}
	
	Main_Tab.setValueByIndexAtInternal=function(rowIndex,colIndex,value,dataType) {
		if (value==null) {
			value="";
		}
		if (dataType!=4) value=formatString(value);
		Main_Tab.rows[rowIndex+1].cells[colIndex+1].childNodes[0].innerHTML=value;
	}
	
	Main_Tab.setValueByName=function(rowIndex,colName,value,dataType) {
		var column=Main_Tab.getColumnByName(colName);
		if (column) {
		   Main_Tab.setValueByIndex(rowIndex,column.cellIndex-1,value,dataType);
		}
	}
	
	Main_Tab.setCurrentValueByIndex=function(colIndex,value,dataType) {
		if (!Main_Tab.currentRow) return;
		Main_Tab.setValueByIndex(Main_Tab.currentRow.rowIndex-1,colIndex,value,dataType);
	}
	
	Main_Tab.setCurrentValueByName=function(colName,value,dataType) {
		if (!Main_Tab.currentRow) return null;
		Main_Tab.setValueByName(Main_Tab.currentRow.rowIndex-1,colName,value,dataType);
	}
	
	Main_Tab.insertRow=function(rowIndex) {
		if (Main_Tab.dataTable) {
			Main_Tab.insert(rowIndex);
			return;
		}
		Main_Tab.insertRowAtInternal(rowIndex);
	}
	
	Main_Tab.insertRowAtInternal=function(rowIndex,repaintRowNo) {
		var rowElement=window.document.createElement("TR");
		rowElement.type="row";
		rowElement.className=Main_Tab.rowClass;
		
		var display="block";
        var width=Main_Tab.fixWidth;
        if (!Main_Tab.fixVisible) {
           display = "none";
           width="1";
        }
		var rowNumElement=window.document.createElement("TD");
		rowNumElement.className=Main_Tab.rows[0].className;
		rowNumElement.height=Main_Tab.rowHeight;
		rowNumElement.align="right";
		rowNumElement.style.display=display;
		rowNumElement.innerHTML='<div class='+Main_Tab.columnFontClass+"></div>";
        rowElement.insertAdjacentElement("beforeEnd",rowNumElement);
		
		for (var i=1;i<Main_Tab.rows[0].cells.length;i++) {
			var cell=Main_Tab.rows[0].cells[i];
			var tempElement=window.document.createElement("TD");
			tempElement.align=cell.align;
			tempElement.innerHTML="<span class="+Main_Tab.rowFontClass+"></span>";
			rowElement.insertAdjacentElement("beforeEnd",tempElement);
		}
        

		if (rowIndex>=Main_Tab.childNodes[1].childNodes.length) {
		   Main_Tab.childNodes[1].insertAdjacentElement("beforeEnd",rowElement);
		} else if (rowIndex==0) {
		   Main_Tab.rows[1].insertAdjacentElement("beforeBegin",rowElement); 	
		} else
		   Main_Tab.rows[rowIndex].insertAdjacentElement("afterEnd",rowElement);
		if (repaintRowNo!=false) Main_Tab.resetRowNum();
	}
	
	Main_Tab.appendRow=function() {
		Main_Tab.insertRow(Main_Tab.rows.length-1);
	}
	
	Main_Tab.deleteCurrentRow=function() {
		if (!Main_Tab.currentRow) return;
		Main_Tab.deleteRow(Main_Tab.currentRow.rowIndex-1);
	}
	
	Main_Tab.deleteRow=function(rowIndex) {
		if (Main_Tab.dataTable) {
			Main_Tab.dataTable.deleteByIndex(rowIndex);
			return;
		}
		var selectRow;
		if (rowIndex+1==Main_Tab.currentRow.rowIndex) {
			if (Main_Tab.currentRow.rowIndex==Main_Tab.rows.length-1)
			   selectRow=rowIndex-1;
			else selectRow=rowIndex+1;
			Main_Tab.setCurrentRow(selectRow);
		}
		Main_Tab.removeRow(rowIndex);
	}
	
	Main_Tab.removeRow=function(rowIndex) {
		Main_Tab.childNodes[1].removeChild(Main_Tab.rows[rowIndex+1]);
		Main_Tab.resetRowNum();
	}
	
	Main_Tab.removeAllRow=function(rowIndex) {
		Main_Tab.removeChild(Main_Tab.childNodes[1]);
		var bodyElement=document.createElement("TBODY");
		Main_Tab.insertAdjacentElement("beforeEnd",bodyElement);
	}
	
	Main_Tab.resetRowNum=function() {
		for (var i=1;i<Main_Tab.rows.length;i++)
		   if (Main_Tab.rows[i]==Main_Tab.currentRow) Main_Tab.rows[i].cells[0].childNodes[0].innerHTML=">"+i;
		   else Main_Tab.rows[i].cells[0].childNodes[0].innerHTML=i;
	}
	
	Main_Tab.resetRowNum();
	
	Main_Tab.setFixVisible=function(visible) {
		if (Main_Tab.fixVisible==visible) return;
		Main_Tab.fixVisible=visible;
		var display="block";
        if (visible==false) {
           display = "none";
        }
		for (var i=0;i<Main_Tab.rows.length;i++) {
			Main_Tab.rows[i].cells[0].style.display=display;
		}
	}
	
	Main_Tab.setCurrentRow=function(rowIndex) {
		if (Main_Tab.dataTable) {
			Main_Tab.dataTable.setCurrentIndex(rowIndex);
			return;
		}
		Main_Tab.changeCurrentRow(rowIndex);
		var e={};
		e.source=Main_Tab;
		Main_Tab.processEvent(Main_Tab.CURRENT_ROW_CHANGE,e);
	}
	
	Main_Tab.changeCurrentRow=function(rowIndex) {
		if (Main_Tab.currentRow) {
			if (Main_Tab.currentRow.rowIndex>=0) {
			  Main_Tab.currentRow.cells[0].childNodes[0].innerHTML=Main_Tab.currentRow.rowIndex;
			  Main_Tab.currentRow.className=Main_Tab.rowClass;
			}
		}
		if (rowIndex>=0 && rowIndex<Main_Tab.getRowCount()) {
		   Main_Tab.currentRow=Main_Tab.rows[rowIndex+1];
		   Main_Tab.currentRow.cells[0].childNodes[0].innerHTML=">"+Main_Tab.currentRow.rowIndex;
		   if (Main_Tab.selectClass && Main_Tab.selectClass!="null")
		      Main_Tab.currentRow.className=Main_Tab.selectClass;
		   Main_Tab.currentCell.rowIndex=rowIndex;
		   Main_Tab.scrollCellToView(Main_Tab.currentRow.rowIndex-1,0);
		} else {
		   Main_Tab.currentRow=null;
		   Main_Tab.currentCell.rowIndex=-1;
		}
	}
	
	Main_Tab.changeCurrentColumn=function(colIndex) {
		if (Main_Tab.currentCell.colIndex==colIndex) return;
		Main_Tab.currentCell.colIndex=colIndex;
		Main_Tab.scrollCellToView(Main_Tab.currentCell.rowIndex,Main_Tab.currentCell.colIndex);
	}
	
	Main_Tab.scrollCellToView=function(rowIndex,colIndex) {
		var rowElement=Main_Tab.getRow(rowIndex);
		//rowElement.scrollIntoView();
	}
	
	Main_Tab.getColumnByElement=function(element) {
		var props=new Array();
		props[0]=new Object();
		props[0].name="type";
		props[0].value="column";
		var the_td	= getElement(element,"td",props);
 	    return the_td;
	}
	
	Main_Tab.getColumnByIndex=function(index) {
 	    return Main_Tab.rows[0].cells[index+1];
	}
	
	Main_Tab.getColumnByName=function(colName) {
		for (var i=1;i<Main_Tab.rows[0].cells.length;i++) {
		    if (Main_Tab.rows[0].cells[i].COLUMN_NAME==colName) {
				return Main_Tab.rows[0].cells[i];
			}
		}
		return null;
	}
	
	Main_Tab.getCaptionOfColumn=function(column) {
		return column.children[0].innerHTML;
	}
	
	Main_Tab.getColumnCount=function() {
		return Main_Tab.rows[0].cells.length-1;
	}
	
	Main_Tab.getFixColumnByElement=function(element) {
		if (Main_Tab.fixVisible==false || Main_Tab.fixVisible=="false") {
			return null;
		}
		var props=new Array();
		props[0]=new Object();
		props[0].name="type";
		props[0].value="fixColumn";
		var the_td	= getElement(element,"td",props);
 	    return the_td;
	}
	
	Main_Tab.getColumnByAbsX=function(x) {
		for (var i=1;i<Main_Tab.rows[0].cells.length;i++) {
		    var pos=getElementOffsetPosition(Main_Tab.rows[0].cells[i]);
			var dec=x-pos.x;
			if (0<=dec && dec<=Main_Tab.rows[0].cells[i].clientWidth) return Main_Tab.rows[0].cells[i];
		}
		return null;
	}
	
	Main_Tab.getRowByAbsY=function(y) {
		for (var i=1;i<Main_Tab.rows.length;i++) {
		    var pos=getElementOffsetPosition(Main_Tab.rows[0]);
			var dec=x-pos.x;
			if (0<=dec && dec<=Main_Tab.rows[i].clientHeight) return Main_Tab.rows[i];
		}
		return null;
	}
	
	Main_Tab.setColumnWidth=function(column,width) {
		if (column.clientWidth==width || width<=2) return;
		var dec=width-column.clientWidth;
		Main_Tab.width=Main_Tab.clientWidth+dec;
		column.width=width;
	}
	
	Main_Tab.setFixColumnWidth=function(width) {
		if (Main_Tab.fixWidth==width || width<=2) return;
		Main_Tab.fixWidth=width;
		if (Main_Tab.fixVisible==false) {
			return;
		}
		var column=Main_Tab.rows[0].cells[0];
		var dec=width-column.clientWidth;
		Main_Tab.width=Main_Tab.clientWidth+dec;
		column.width=width;
	}
	
	Main_Tab.sort=function(index,order) {
		if (Main_Tab.dataTable) {
			var columnName=Main_Tab.getColumnByIndex(index).COLUMN_NAME;
			Main_Tab.dataTable.sort(columnName,order);
			return;
		}
		function sortFunction(one,two) {
			var result=0;
			var valueOne=Main_Tab.getValueByIndex(one.rowIndex-1,index);
			var valueTwo=Main_Tab.getValueByIndex(two.rowIndex-1,index);
			if (valueOne<valueTwo) result=-1;
			else if (valueOne>valueTwo) result=1;
            if (order==false) result=-result;
			return result;
		}
		var tempArray=new Array();
		for (var i=1;i<Main_Tab.rows.length;i++)
		    tempArray[i-1]=Main_Tab.rows[i];
		tempArray.sort(sortFunction);
		while (Main_Tab.childNodes[1].childNodes.length>0) Main_Tab.childNodes[1].removeChild(Main_Tab.childNodes[1].childNodes[0]);
		for (var i=0;i<tempArray.length;i++)
		    Main_Tab.childNodes[1].insertAdjacentElement("beforeEnd",tempArray[i]);
		Main_Tab.resetRowNum();
	}
	
	Main_Tab.getRowCount=function() {
		return Main_Tab.rows.length-1;
	}
	
	Main_Tab.getRow=function(rowIndex) {
		return Main_Tab.rows[rowIndex+1];
	}
	
	Main_Tab.locate=function(fieldNames,values,type) {
		if (Main_Tab.dataTable) {
 		   Main_Tab.dataTable.locate(fieldNames,values,type);
		   return;
		}
		
	    function equal(index) {
		   for (var i=0;i<fieldNames.length;i++) {
		      if (!values[i]) continue;
		      var v=Main_Tab.getValueByName(index,fieldNames[i]);
		      if (!v) return false;
		      var vStr=v.toString();
		      var cStr=values[i].toString();
		   
		      if (type==Main_Tab.PARTIAL_LOCATE) {
		         if (vStr.indexOf(cStr)<0) return false;
		      } else if (type==Main_Tab.FULL_LOCATE) {
			     if (vStr!=cStr) return false;
		      } else return false;
		   }
		   return true;
	    }
	    var index=-1;
	    for (var i=0;i<Main_Tab.getRowCount();i++)
	       if (equal(i)==true) {
		      index=i;
		      break;
	       }
	  
	    if (index>=0) {
	       Main_Tab.setCurrentRow(index);
	    }
	}
	
	Main_Tab.doDefaultMouseMove=function() {
		var column=Main_Tab.getColumnByElement(event.srcElement);
		if (!column) column=Main_Tab.getFixColumnByElement(event.srcElement);
		if (!column) {
			Main_Tab.style.cursor='default';
			return;
		}
		if (Main_Tab.status=="drag") {
			Main_Tab.status="draging";
		    Main_Tab.startDrag(column);
		} else if (Main_Tab.status=="resize") {
			Main_Tab.status="resizing";
		} else if (Main_Tab.status=="draging") {
		} else if (Main_Tab.status=="resizing") {
		} else {
			var pos=getMousePostitionInDocument(event);
			pos=getOffsetPositionInElement(pos,column);
			var dec=Math.abs(pos.x- column.clientWidth);
			if ((dec>1 && dec<=3 && column.cellIndex==Main_Tab.rows[0].cells.length-1) || (dec<=2 && column.cellIndex!=Main_Tab.rows[0].cells.length-1)) {
			   if (Main_Tab.resizeEnabled) Main_Tab.style.cursor='col-resize';
		    } else {
			   Main_Tab.style.cursor='default';
			}
		}
	}
	
	Main_Tab.doDefaultMouseDown=function() {
		var column=Main_Tab.getColumnByElement(event.srcElement);
		if (!column) column=Main_Tab.getFixColumnByElement(event.srcElement);
		if (!column) return;
		if (Main_Tab.status=="drag") {
		} else if (Main_Tab.status=="resize") {
		} else if (Main_Tab.status=="draging") {
		} else if (Main_Tab.status=="resizing") {
		} else {
			var pos=getMousePostitionInDocument(event);
			pos=getOffsetPositionInElement(pos,column);
			var dec=Math.abs(pos.x- column.clientWidth);
			if ((dec>1 && dec<=3 && column.cellIndex==Main_Tab.rows[0].cells.length-1) || (dec<=2 && column.cellIndex!=Main_Tab.rows[0].cells.length-1)) {
			   if (Main_Tab.resizeEnabled) {
			      Main_Tab.status="resize";
			      Main_Tab.startResize(column,getMousePostitionInDocument(event).x);
			   }
		    } else if (Main_Tab.dragEnabled && column.type!="fixColumn") {
			   Main_Tab.status="drag";
			}
		}
	}
	
	Main_Tab.doDefaultMouseUp=function() {
		if (Main_Tab.sortEnabled=="false" || Main_Tab.sortEnabled==false) return;
		var column=Main_Tab.getColumnByElement(event.srcElement);
		if (!column) return;
		if (Main_Tab.status && Main_Tab.status!="draging" && Main_Tab.status!="resize" && Main_Tab.status!="resizing") {
		   var order=true;
		   if (Main_Tab.sortType=="desc") order=false;
		   Main_Tab.sort(column.cellIndex-1,order);
		}
	}
	
	Main_Tab.doDefaultMouseOver=function() {
		var props=new Array();
		props[0]=new Object();
		props[0].name="type";
		props[0].value="row";
		var rowElement=getElement(event.srcElement,"tr",props);
		if (!rowElement) return;
		rowElement.className=Main_Tab.overClass;
	}
	
	Main_Tab.doDefaultMouseOut=function() {
		var props=new Array();
		props[0]=new Object();
		props[0].name="type";
		props[0].value="row";
		var rowElement=getElement(event.srcElement,"tr",props);
		if (!rowElement) return;
		if (Main_Tab.currentRow==rowElement && Main_Tab.selectClass && Main_Tab.selectClass!="null")
		   rowElement.className=Main_Tab.selectClass;
		else
		   rowElement.className=Main_Tab.rowClass;
	}
	
	Main_Tab.doDefaultKeyDown=function() {
		if (event.keyCode==38) {
			Main_Tab.prior();
		} else if (event.keyCode==40) {
			Main_Tab.next();
		}
	}
	
	Main_Tab.doDefaultClick=function() {
		var props=new Array();
		props[0]=new Object();
		props[0].name="type";
		props[0].value="row";
		var rowElement=getElement(event.srcElement,"tr",props);
		if (!rowElement) return;
		props[0].value="cell";
		var colElement=getElement(event.srcElement,"td",props);
		if (!colElement) Main_Tab.changeCurrentColumn(0);
		else Main_Tab.changeCurrentColumn(colElement.cellIndex-1);
		Main_Tab.setCurrentRow(rowElement.rowIndex-1);
		var e={};
		e.source=Main_Tab;
		e.cell=Main_Tab.currentCell;
		Main_Tab.processEvent(Main_Tab.ON_CLICK,e);
	}
	
	function doDefaultColumnClick() {
		var column=Main_Tab.getColumnByElement(event.srcElement);
		if (column==null) return;
		var e={};
		e.source=Main_Tab;
		e.column=column;
		Main_Tab.processEvent(Main_Tab.ON_COLUMN_CLICK,e);
	}
	
	function doDefaultDbClick() {
		var props=new Array();
		props[0]=new Object();
		props[0].name="type";
		props[0].value="row";
		var rowElement=getElement(event.srcElement,"tr",props);
		if (!rowElement) return;
		props[0].value="cell";
		var colElement=getElement(event.srcElement,"td",props);
		if (!colElement) Main_Tab.changeCurrentColumn(0);
		else Main_Tab.changeCurrentColumn(colElement.cellIndex-1);
		Main_Tab.setCurrentRow(rowElement.rowIndex-1);
		var e={};
		e.source=Main_Tab;
		e.cell=Main_Tab.currentCell;
		Main_Tab.processEvent(Main_Tab.ON_DB_CLICK,e);
	}
	
	Main_Tab.attachEvent("onmousedown", Main_Tab.doDefaultMouseDown);
	Main_Tab.attachEvent("onclick", Main_Tab.doDefaultClick);
	Main_Tab.attachEvent("onclick", doDefaultColumnClick);
	Main_Tab.attachEvent("ondblclick", doDefaultDbClick);
	Main_Tab.attachEvent("onmouseup", Main_Tab.doDefaultMouseUp);
	Main_Tab.attachEvent("onmousemove", Main_Tab.doDefaultMouseMove);
	Main_Tab.attachEvent("onmouseover", Main_Tab.doDefaultMouseOver);
	Main_Tab.attachEvent("onmouseout", Main_Tab.doDefaultMouseOut);
	Main_Tab.attachEvent("onkeydown", Main_Tab.doDefaultKeyDown);
	
	Main_Tab.startDrag=function(column) {
	    //Main_Tab.currentColumn=column;
	    //Main_Tab.dragElement.style.width= column.offsetWidth;
	    //Main_Tab.dragElement.style.height= column.offsetHeight;
	    //Main_Tab.dragElement.innerHTML = column.innerHTML;
	    //Main_Tab.dragElement.style.backgroundColor = column.currentStyle.backgroundColor;
	    //Main_Tab.dragElement.style.color = column.currentStyle.color;
	}
	
	Main_Tab.endDrag=function(column) {
		//Main_Tab.moveColumnTo(Main_Tab.currentColumn,column.cellIndex-1);
	    //Main_Tab.swapColumn(column,Main_Tab.currentColumn);
	}
	
	Main_Tab.moveColumnTo=function(column,index) {
		var fromIndex=column.cellIndex-1;
		var toIndex=index;
		if (fromIndex==toIndex) return;
		if (fromIndex>toIndex) {
			for (var i=fromIndex;i>toIndex;i--) {
			   Main_Tab.swapColumn(i,i-1);
			}
		} else {
			for (var i=fromIndex;i<toIndex;i++) {
			   Main_Tab.swapColumn(i,i+1);
			}
		}
	}
	
	Main_Tab.swapColumn=function(index,another) {
		if (index==another) return;
		for(var i=0;i<Main_Tab.rows.length;i++)
            Main_Tab.rows[i].cells[index+1].swapNode(Main_Tab.rows[i].cells[another+1]);
	}
	
	Main_Tab.startResize=function(column,x) {
		Main_Tab.currentColumn=column;
		var pos=getElementOffsetPosition(Main_Tab);
		Main_Tab.splitLine.style.left = x;
	    Main_Tab.splitLine.style.top = pos.y;
	    var height=Main_Tab.clientHeight;
		if (height+Main_Tab.offsetTop>Main_Tab.offsetParent.clientHeight) 
		    height=Main_Tab.offsetParent.clientHeight-Main_Tab.offsetTop;
        Main_Tab.splitLine.style.height = height;
		Main_Tab.splitLine.style.display="";
	}
	
	Main_Tab.clearStatus=function() {
		Main_Tab.dragElement.style.display="none";
		Main_Tab.splitLine.style.display="none";
		Main_Tab.status=null;
		Main_Tab.currentColumn=null;
	}
	
	Main_Tab.first=function() {
		if (Main_Tab.rows.length<=1) return;
		Main_Tab.setCurrentRow(0);
	}
	
	Main_Tab.next=function() {
		var index=null;
		if (Main_Tab.currentRow) index=Main_Tab.currentRow.rowIndex;
		else index=0;
		if (index+1<Main_Tab.rows.length) Main_Tab.setCurrentRow(index);
	}
	
	Main_Tab.prior=function() {
		var index=null;
		if (Main_Tab.currentRow) index=Main_Tab.currentRow.rowIndex-2;
		else index=0;
		if (index+1<Main_Tab.rows.length && index>=0) Main_Tab.setCurrentRow(index);
	}
	
	Main_Tab.last=function() {
		if (Main_Tab.rows.length<=1) return;
		Main_Tab.setCurrentRow(Main_Tab.rows.length-2);
	}
	
	Main_Tab.processEvent=function(which,e) {
        var lses=Main_Tab.listeners.get(which);
        if (lses) {
           for (var i=0;i<lses.length;i++) lses[i](e);
        }
    }
	
	function doDefaultTableMouseMove() {
		if (Main_Tab.status=="draging") {
			Main_Tab.dragElement.style.display="";
		    Main_Tab.dragElement.style.top= getMousePostitionInDocument(event).y - Main_Tab.dragElement.offsetHeight/2;
		    Main_Tab.dragElement.style.left= getMousePostitionInDocument(event).x - Main_Tab.dragElement.offsetWidth/2;
		} else if (Main_Tab.status=="resizing") {
			var pos=getElementOffsetPosition(Main_Tab);
	        Main_Tab.splitLine.style.left = getMousePostitionInDocument(event).x;
	        Main_Tab.splitLine.style.top = pos.y;
			var height=Main_Tab.clientHeight;
			if (height+Main_Tab.offsetTop>Main_Tab.offsetParent.clientHeight) 
			    height=Main_Tab.offsetParent.clientHeight-Main_Tab.offsetTop;
	        Main_Tab.splitLine.style.height = height;
		}
	}
	
	function doDefaultTableMouseUp() {
		var pos=getMousePostitionInDocument(event);
		if (Main_Tab.status=="draging") {
			if (containPosition(pos,Main_Tab)) {
			   var column=Main_Tab.getColumnByAbsX(getMousePostitionInDocument(event).x);
			   if (column) Main_Tab.endDrag(column);
			}
		} else if (Main_Tab.status=="resizing") {
			var p=getOffsetPositionInElement(pos,Main_Tab);
			if (0<=p.y && p.y<=Main_Tab.clientHeight) {
			   pos=getOffsetPositionInElement(pos,Main_Tab.currentColumn);
			   if (pos.x>0) {
				   if (Main_Tab.currentColumn.type=="fixColumn")
				     Main_Tab.setFixColumnWidth(pos.x);
				   else
 				     Main_Tab.setColumnWidth(Main_Tab.currentColumn,pos.x);
			   }
			}
		}
		Main_Tab.clearStatus();
	}
	
	var defaultSelectStart=document.onselectstart;
	
	function doDefaultSelectstart() {
		if (defaultSelectStart) {
		  var result=defaultSelectStart();
		  if (result==false) return false;
		}
		return !(Main_Tab.status=="drag" || Main_Tab.status=="draging" || Main_Tab.status=="resize" || Main_Tab.status=="resizing")
	}
	
	document.attachEvent("onmouseup", doDefaultTableMouseUp);
	document.attachEvent("onmousemove", doDefaultTableMouseMove);
	document.onselectstart=doDefaultSelectstart;

}
