/**
 * Table
 */
var _TOTAL_PATTERN = "|TOTAL|";

var _TABLE_ROW_STATUS = "web_edit_status";
var _TABLE_ROW_STATUS_ADD = "A";
var _TABLE_ROW_STATUS_MODIFY = "M";
var _TABLE_ROW_STATUS_DELETE = "D";
var _TABLE_SCROLL_BAR_WIDTH = 18;

function IpacsTable(inTable, inData, inColumnConfig){
    this.needMappingColumn = true;
    this.editable = !(inTable.editable == "false");
    this.needCheckNewRow = false;
    this.canSwapColumn = (inTable.canSwapColumn == "true");

    this.currCell = null;
    this.currRow = null;
    this.currEditor = null;

    this.htmlTable = inTable;
    this.htmlTable.handler = this;

    this.headTable = window.document.getElementById(inTable.id + "_H");
    this.headTable.handler = this;

    this.footTable = window.document.getElementById(inTable.id + "_F");
    this.footTable.handler = this;

    this.dataBody = this.htmlTable.tBodies[0];
    this.tableHead = this.headTable.tHead;
    this.tableFoot = this.footTable.tFoot;

    this.colGroup = this.htmlTable.getElementsByTagName("COLGROUP");
    this.hcolGroup = this.headTable.getElementsByTagName("COLGROUP");
    this.fcolGroup = this.footTable.getElementsByTagName("COLGROUP");

    if (this.tableHead == null){
        this.tableHead = this.headTable.createTHead();
    }
    if (this.tableFoot == null){
        this.tableFoot = this.footTable.createTFoot();
    }

    if (this.colGroup.length == 0){
        this.colGroup = window.document.createElement("COLGROUP");
        this.dataBody.insertAdjacentElement("BeforeBegin",this.colGroup);
    }

    if (this.hcolGroup.length == 0){
        this.hcolGroup = window.document.createElement("COLGROUP");
        this.tableHead.insertAdjacentElement("BeforeBegin",this.hcolGroup);
    }

    if (this.fcolGroup.length == 0){
        this.fcolGroup = window.document.createElement("COLGROUP");
        this.tableFoot.insertAdjacentElement("BeforeBegin",this.fcolGroup);
    }

    this.drag = document.createElement("DIV");
    this.drag.style.position = "absolute";
    this.drag.style.cursor = "hand";
    this.drag.style.border = "1 solid black";
    this.drag.style.display    = "none";
    this.drag.style.zIndex = "999";

    this.model = null;
    this.columnModel = null;

    this.columnModel = new IpacsTableColumnModel(inColumnConfig);
    this.setModel(new IpacsTableModel(inData));

    this.columnRenderers = new ActiveXObject("Scripting.Dictionary");
    this.colorRenderers = new Array();

    this.cellEditableFilter = new Array();
    this.messages = new ActiveXObject("Scripting.Dictionary");
    this.cellTotalPatterns = new ActiveXObject("Scripting.Dictionary");

    this.htmlTable.onclick = this.handleClick;
    this.headTable.onclick = this.handleClick;
    this.htmlTable.onkeydown = TableKeydownHandler;
}

IpacsTable.prototype.setModel = function (inModel){
    this.model = inModel;
    this.model.addTableModelListener(this);
}

IpacsTable.prototype.setModelStatus = function(inModel){
    switch (inModel.toUpperCase()){
    case "COPY":
        for (var i=0; i < this.model.getRowCount(); i++){
            this.model.insertBuffer[i] = this.model.data[i];
        }
        break;
    }
}

IpacsTable.prototype.init = function(){
    var debug = new Debug();
    debug.start();

    //Create Table Header
    var newHead = this.tableHead.insertRow();
    for (var j=0; j<this.columnModel.getColumnCount(); j++){
        var newCell = newHead.insertCell();
        newCell.innerHTML = this.columnModel.getColumnDesc(j);
        if (this.canSwapColumn){
            newCell.onmousedown = this.dragStart;
        }
        newCell.noWrap = true;
    }

    var newCell = newHead.insertCell();
    newCell.innerHTML = "&nbsp;";
    newCell.noWrap = true;

    this.htmlTable.cols = this.columnModel.getColumnCount();
    this.headTable.cols = this.columnModel.getColumnCount();
    this.footTable.cols = this.columnModel.getColumnCount();

    if (this.model.getRowCount() > 0){
        // Create Template Row
        var newRow = this.dataBody.insertRow();

        if (!this.editable){
            newRow.style.color = _DISABLED_COLOR;
        }

        for (var j=0; j<this.columnModel.getColumnCount(); j++){
            var newCell = newRow.insertCell();
            newCell.noWrap = true;
        }

        // Insert Rows
        for (var i=1; i<this.model.getRowCount(); i++){
            var tempRow = this.dataBody.insertAdjacentElement("beforeEnd", newRow.cloneNode(true));

            if (!this.editable){
                tempRow.style.color = _DISABLED_COLOR;
            }

            for (var j=0; j<tempRow.cells.length; j++){
                var newCell = tempRow.cells[j];
                newCell.noWrap = true;
            }
        }

        if (this.defaultColorRenderer != null){
            this.defaultColorRenderer();
        }
    }

    // Create Table Foot
    for (var j=0; j<this.columnModel.getColumnCount(); j++){
        var totalPattern = this.columnModel.getColumnTotalPattern(j);
        if (totalPattern != null){
            this.cellTotalPatterns.Add(this.columnModel.getColumnName(j), totalPattern);
        }
    }

    var newFoot = this.tableFoot.insertRow();    

    if (this.cellTotalPatterns.Count == 0){
        newFoot.style.height = "0px";        
    } else {
        for (var j=0; j<this.columnModel.getColumnCount(); j++){
            var newCell = newFoot.insertCell();
            newCell.noWrap = true;
            newCell.innerText = " ";
        }
    }

    this.refreshData(0, this.model.getRowCount() - 1, 0, this.columnModel.getColumnCount() - 1);

    this.calculateAllColumn();

    // Def Columns
    var tableWidth = 0;
    for (var j=0; j<this.columnModel.getColumnCount(); j++){
        var col = null;

        col = document.createElement("COL");
        col.width = this.columnModel.getColumnWidth(j);
        col.align = this.columnModel.getColumnAlign(j)        
        this.colGroup.appendChild(col);
                
        col = document.createElement("COL");
        col.width = this.columnModel.getColumnWidth(j);
        col.align = this.columnModel.getColumnAlign(j)        
        this.hcolGroup.appendChild(col);

        col = document.createElement("COL");
        col.width = this.columnModel.getColumnWidth(j);      
        col.align = this.columnModel.getColumnAlign(j)        
        this.fcolGroup.appendChild(col);

        if (j == this.columnModel.getColumnCount() - 1){ 
            col = document.createElement("COL");
            col.width = _TABLE_SCROLL_BAR_WIDTH;
            this.hcolGroup.appendChild(col);

            col = document.createElement("COL");
            col.width = _TABLE_SCROLL_BAR_WIDTH;
            this.fcolGroup.appendChild(col);
        }

        tableWidth += this.columnModel.getColumnWidth(j);
    }
    this.htmlTable.width = tableWidth;
    debug.end();

    document.body.insertBefore(this.drag);
}

IpacsTable.prototype.refresh = function(){
    this.clear();
    this.init();
}

IpacsTable.prototype.clear = function(){
    this.clearData();

    var rowCount = this.tableHead.rows.length;

    for (var i=rowCount - 1; i >=0; i--){
        this.tableHead.deleteRow(i);
    }

    rowCount = this.tableFoot.rows.length;

    for (var i=rowCount - 1; i >=0; i--){
        this.tableFoot.deleteRow(i);
    }

    while(this.colGroup.children.length > 0){
        this.colGroup.removeChild(this.colGroup.children[0]);
    }
}

IpacsTable.prototype.clearData = function(){
    this.currCell = null;
    this.currRow = null;
    this.currEditor = null;

    var rowCount = this.dataBody.rows.length;

    for (var i=rowCount - 1; i >=0; i--){
        this.dataBody.deleteRow(i);
    }
}

IpacsTable.prototype.refreshData = function(inStartRow, inEndRow, inStartCol, inEndCol){
    for (var i=inStartRow; i<=inEndRow; i++){
        for (var j=inStartCol; j<=inEndCol; j++){
            this.rendererCell(this.dataBody.rows[i].cells[j], i, j, this.getValueAt(i, j));
        }
    }
}

IpacsTable.prototype.calculateAllColumn = function(){
    var columnNames = (new VBArray(this.cellTotalPatterns.Keys())).toArray();

    for (var i=0; i < this.cellTotalPatterns.Count; i++){
        var columnName = columnNames[i];
        var pattern = this.cellTotalPatterns.Item(columnName);

        this.calculateColumn(this.columnModel.getColumnIndex(columnName), pattern);
    }
}

IpacsTable.prototype.calculateColumn = function (columnIndex, pattern){
    if (pattern.indexOf(_TOTAL_PATTERN) == -1){
        this.tableFoot.rows[0].cells[columnIndex].innerText = pattern;
    } else {
        var total = 0;
        for (var i=0; i < this.model.getRowCount(); i++){
            var value = this.getValueAt(i, columnIndex);
            if (value != null && value != ""){
                total += parseFloat(value);
            }
        }

        var integerDigits = 9;
        var fractionDigits = 0;
        var params = this.columnModel.getColumnEditorParams(columnIndex);

        if (params != null){
            params = params.split(",");
            integerDigits = parseInt(params[0]);

            if(params.length > 1){
                fractionDigits = parseInt(params[1]);
            }
        }

        total = NumberHandler(appendZero(roundNumber(total, fractionDigits).toString(), fractionDigits), 100, fractionDigits);
        this.tableFoot.rows[0].cells[columnIndex].innerText = pattern.replace(_TOTAL_PATTERN, total);
    }
}

IpacsTable.prototype.getModelColumnIndex = function(index){
    return this.model.getColumnIndex(this.columnModel.getColumnName(index));
}

IpacsTable.prototype.getColumnIndex = function(index){
    return this.columnModel.getColumnIndex(this.model.getColumnName(index));
}

IpacsTable.prototype.getValueAt = function(rowIndex, columnIndex){
    if (this.needMappingColumn){
        return this.model.getValueAt(rowIndex, this.getModelColumnIndex(columnIndex));
    } else {
        return this.model.getValueAt(rowIndex, columnIndex);
    }
}

IpacsTable.prototype.setValueAt = function(rowIndex, columnIndex, data){
    if (this.needMappingColumn){
        return this.model.setValueAt(rowIndex, this.getModelColumnIndex(columnIndex), data);
    } else {
        return this.model.setValueAt(rowIndex, columnIndex, data);
    }
}

IpacsTable.prototype.appendRow = function(){
    if (this.editable){
        this.model.appendRow();
    }
}

IpacsTable.prototype.copyRow = function(){
    if (this.editable){
        this.model.copyRow();
    }
}

IpacsTable.prototype.deleteRow = function(){
    if (this.editable){
        if (this.currRow != null){
            if (this.messages.Item("DEL_MSG") == null || window.confirm(this.messages.Item("DEL_MSG"))){
                var rowIndex = findIndexFormArray(this.currRow, this.dataBody.rows);
                var columnIndex = findIndexFormArray(this.currCell, this.currRow.cells);
                var result = this.model.deleteRow(rowIndex);
                if (result && this.model.getRowCount() > 0){
                    this.currEditor = null;
                    if (rowIndex > this.model.getRowCount() - 1){
                        rowIndex--;
                    }
                    this.editCell(rowIndex, columnIndex);
                }
            }
        }
    }
}

IpacsTable.prototype.addColorRenderer = function(inColorRenderer){
    this.colorRenderers[this.colorRenderers.length] = inColorRenderer;
}

IpacsTable.prototype.clearColorRenderer = function(){
    this.colorRenderers = new Array();
}

IpacsTable.prototype.addCellEditableFilter = function(inFilter){
    this.cellEditableFilter[this.cellEditableFilter.length] = inFilter;
}

IpacsTable.prototype.modelChanged = function(inEvent){
    var type = inEvent.type;
    var inStartRow = inEvent.data[0];
    var inEndRow = inEvent.data[1];
    var inStartCol = inEvent.data[2];
    var inEndCol = inEvent.data[3];

    switch (type) {
        case "TABLE_MODEL_INSERT":
            var oldRows = this.dataBody.rows;
            if (oldRows.length > 0){
                this.dataBody.insertAdjacentElement("beforeEnd", this.dataBody.rows[0].cloneNode(true));
            } else {
                var newRow = this.dataBody.insertRow();
                for (var j=0; j<this.columnModel.getColumnCount(); j++){
                    var newCell = newRow.insertCell();
                    newCell.noWrap = true;
                }
            }
            this.calculateAllColumn();
            this.refreshData(inStartRow, inEndRow, 0, this.columnModel.getColumnCount() - 1);
            this.editCell(inStartRow, 0);
            break;
        case "TABLE_MODEL_DELETE":
            for (var i=0; i < inEndRow - inStartRow; i++){
                this.dataBody.deleteRow(inStartRow);
            }
            this.currCell = null;
            this.currRow = null;
            this.currEditor = null;
            break;
        case "TABLE_MODEL_MODIFY":
            this.refreshData(inStartRow, inEndRow, this.getColumnIndex(inStartCol), this.getColumnIndex(inEndCol));

            if (typeof this.getColumnIndex(inStartCol) != "undefined" &&
                this.cellTotalPatterns.Exists(this.columnModel.getColumnName(this.getColumnIndex(inStartCol)))){
                this.calculateColumn(this.getColumnIndex(inStartCol), this.columnModel.getColumnTotalPattern(this.getColumnIndex(inStartCol)));
            }
            break;
        case "TABLE_VALUE_INVALID":
            var errorCell = this.dataBody.rows[inStartRow].cells[this.getColumnIndex(inStartCol)];
            errorCell.className = "error";
            break;
    }
}

IpacsTable.prototype.defaultColorRenderer = function(){
}

IpacsTable.prototype.rendererCell = function(cell, rowIndex, columnIndex, data){
    var rendererName = "";
    var columnClass = this.columnModel.getColumnClass(columnIndex);
    if (this.columnRenderers.Exists(columnClass)){
        rendererName = this.columnRenderers.Item(columnClass);
    } else {
        rendererName = this.columnModel.rendererType.Item(this.columnModel.getColumnClass(columnIndex));
        this.columnRenderers.Add(columnClass, rendererName);
    }

    if (data == null){
        data = _DEFAULT_NULL;
    }

    eval(rendererName + "(cell, rowIndex, columnIndex, data);");
    this.rendererCellColor(cell, rowIndex, columnIndex, data);
}

IpacsTable.prototype.rendererCellColor = function(cell, rowIndex, columnIndex, data, isInit){
    if (cell.sColor == null){
        cell.sColor = cell.style.backgroundColor;
    }

    if (this.dataBody.rows[rowIndex] == this.currRow){
        cell.style.backgroundColor = _SELECT_COLOR;
    } else {
        cell.style.backgroundColor = cell.sColor;
    }

    if (this.colorRenderers != null){
        for (var i=0; i < this.colorRenderers.length; i++){
            eval(this.colorRenderers[i] + "(cell, this.dataBody.rows[rowIndex] == this.currRow, rowIndex, columnIndex, data, this);");
        }
    }
}

IpacsTable.prototype.rendererString = function(cell, rowIndex, columnIndex, data){
    if (data != ""){
        cell.innerText = data;
    } else {
        cell.innerText = " ";
    }

    cell.title = data;
}

IpacsTable.prototype.rendererDate = function(cell, rowIndex, columnIndex, data){
    if (data != ""){
        cell.innerText = data;
    } else {
        cell.innerText = " ";
    }
    cell.title = data;
}

IpacsTable.prototype.rendererTime = function(cell, rowIndex, columnIndex, data){
    if (data != "" && data.length == 4){
        cell.innerText = data.substring(0, 2) + ":" + data.substring(2, 4);
    } else {
        cell.innerText = " ";
    }
    cell.title = data;
}

IpacsTable.prototype.rendererNumber = function(cell, rowIndex, columnIndex, data){
    if (data == ""){
        cell.innerText = " ";
    } else {
        if (data != _DEFAULT_NULL){
            var params = this.columnModel.getColumnEditorParams(columnIndex);
            if (params != null){
                var fractionDigits = parseInt(params.split(",")[1]);
                data = appendZero(data, fractionDigits);
            }
        }
        cell.innerText = data;
    }
    cell.title = data;
}

IpacsTable.prototype.rendererBoolean = function(cell, rowIndex, columnIndex, data){
    var displayData = "";
    if (data == "true"){
        displayData = "<div style='font-family:Marlett;font-size:14px'>a<div>";
    } else {
        displayData = "<div style='font-family:Webdings;font-size:12px'>c<div>";
    }
    cell.innerHTML = displayData;
}

IpacsTable.prototype.rendererId = function(cell, rowIndex, columnIndex, data){
    var displayData = "";

    if (data != null && data.split("^")[1] == "1"){
        displayData = "<div style='font-family:Marlett;font-size:14px'>a<div>";
    } else {
        displayData = "<div style='font-family:Webdings;font-size:12px'>c<div>";
    }
    cell.innerHTML = displayData;
    cell.title = data;
}

IpacsTable.prototype.handleClick = function(){
    var srcObj = event.srcElement;
    if (srcObj.tagName != "TABLE" && srcObj.tagName != "TBODY" && srcObj.tagName != "THEAD"
        && srcObj.tagName != "TFOOT" && srcObj.tagName != "CAPTION" && srcObj.tagName != "TR"){
        var srcCell = getParentByTagName(srcObj, "TD");
        var srcBody = getTBodyByCell(srcCell);

        if(srcBody.tagName == "TBODY" && srcCell.status != "EDIT"){
            var cellLocation = findLocationByCell(srcCell);
            this.handler.editCell(cellLocation[0], cellLocation[1]);
        }

        if(srcBody.tagName == "THEAD"){
            if (srcObj.tagName == "INPUT" && srcObj.type == "checkbox"){
                columnIndex = findIndexFormArray(srcCell, getRowByCell(srcCell).cells);
                if (this.handler.columnModel.getColumnClass(columnIndex) == "boolean"){
                    for (var i=0; i < this.handler.model.getRowCount(); i++){
                        this.handler.setValueAt(i, columnIndex, srcObj.checked + "");
                    }
                }

                if (this.handler.columnModel.getColumnClass(columnIndex) == "id"){
                    for (var i=0; i < this.handler.model.getRowCount(); i++){
                        this.handler.setValueAt(i, columnIndex, setIdSelected(this.handler.getValueAt(i, columnIndex), srcObj.checked));
                    }
                }

                this.handler.refreshData(0, this.handler.model.getRowCount() - 1, columnIndex, columnIndex);
            }
        }
    }
}

IpacsTable.prototype.isCellEditable = function(rowIndex, columnIndex){
    var result = false;
    if (this.editable
        && this.model.getModelMeta().indexOf("M") != -1
        && this.model.isRowEditable(rowIndex)
        && this.model.getRowMeta(rowIndex).indexOf("|" + this.columnModel.getColumnName(columnIndex) + "_R|") == -1
        && (this.columnModel.isColumnEditable(columnIndex)
        || (!this.needCheckNewRow && this.model.isNewRow(rowIndex)))){
        if (this.cellEditableFilter.length > 0){
            for (var i=0; i < this.cellEditableFilter.length; i++){
                eval("result = " + this.cellEditableFilter[i] + "(rowIndex, columnIndex, this.getValueAt(rowIndex, columnIndex), this)");
                if (!result){
                    break;
                }
            }
        } else {
            result = true;
        }
    }

    return result;
}

IpacsTable.prototype.editCell = function(rowIndex, columnIndex){
    this.stoppingEdit();
    this.setCurrCell(rowIndex, columnIndex);

    if (this.isCellEditable(rowIndex, columnIndex)){
        if (this.columnModel.getColumnEditor(columnIndex) == null){
            var columnClass = this.columnModel.getColumnClass(columnIndex);
            var editorName = this.columnModel.editorType.Item(this.columnModel.getColumnClass(columnIndex));
            eval("this.currEditor = new " + editorName + "(this.columnModel.getColumnEditorParams(columnIndex))");
            this.columnModel.setColumnEditor(columnIndex, this.currEditor);
        } else {
            this.currEditor = this.columnModel.getColumnEditor(columnIndex)
        }
        this.currEditor.getEditor().boundCell = this.currCell;

        this.currCell.innerHTML = "";
        this.currCell.status = "EDIT";
        this.currCell.style.padding = "0px";
        this.currCell.className = "normal";
        this.currCell.appendChild(this.currEditor.getEditor());
        this.currEditor.setValue(this.getValueAt(rowIndex, columnIndex));
        window.setTimeout("document.all('" + this.htmlTable.id + "').handler.currEditor.focus()", 10);
    } else {
        window.setTimeout("document.all('" + this.htmlTable.id + "').handler.currCell.focus()", 10);
    }
}

IpacsTable.prototype.stoppingEdit = function (inCancelChange){
    if (this.currEditor != null){
        var cellLocation = findLocationByCell(this.currCell);
        var value = this.currEditor.getValue();
        if (!inCancelChange){
            if (value != null && typeof value != "undefined" && typeof value.Count != "undefined"){
                var fields = (new VBArray(value.Keys())).toArray();
                for (var i=0; i < fields.length; i++){
                    this.model.setValueAt(cellLocation[0], this.model.getColumnIndex(fields[i]), value.Item(fields[i]));
                }

                this.model.setValueValid(cellLocation[0], this.getModelColumnIndex(cellLocation[1]));
            } else {
                if (_INVALID_VALUE != value){
                    this.setValueAt(cellLocation[0], cellLocation[1], value);
                    this.model.setValueValid(cellLocation[0], this.getModelColumnIndex(cellLocation[1]));
                } else {
                    this.model.setValueInvalid(cellLocation[0], this.getModelColumnIndex(cellLocation[1]));
                }
            }
        }

        this.refreshData(cellLocation[0], cellLocation[0], cellLocation[1], cellLocation[1]);

        try{
            if (_INVALID_VALUE == value){
                this.currCell.title = _DATA_INVALID_MSG;
            } else {
                if (this.getValueAt(cellLocation[0], cellLocation[1]) == null && !this.columnModel.isColumnAllowEmpty(cellLocation[1])){
                    this.currCell.className = "error";
                    this.currCell.title = _DATA_EMPTY_MSG;
                } else {
                    this.currCell.className = "ok";
                }
            }
        } catch(e){
            if (this.getValueAt(cellLocation[0], cellLocation[1]) == null && !this.columnModel.isColumnAllowEmpty(cellLocation[1])){
                this.currCell.className = "error";
                this.currCell.title = _DATA_EMPTY_MSG;
            } else {
                this.currCell.className = "ok";
            }
        }

        this.currEditor = null;
        this.currCell.status = null;
    }

    if (typeof window.popup != "undefined"){
        window.popup.handler.hidePopup();
    }
}

IpacsTable.prototype.setCurrCell = function(rowIndex, columnIndex){
    var newCell = this.dataBody.rows[rowIndex].cells[columnIndex];
    if (this.currCell != newCell){
        this.currCell = newCell;
        this.setCurrRow(rowIndex);
        return true;
    } else {
        return false;
    }
}

IpacsTable.prototype.setCurrRow = function(rowIndex){
    if (this.dataBody.rows[rowIndex] != this.currRow){
        var oldRow = null;
        if (this.currRow != null){
            oldRow = this.currRow;
        }

        this.currRow = this.dataBody.rows[rowIndex];

        for (var i=0; i < this.currRow.cells.length; i++){
            this.currRow.cells[i].style.backgroundColor = _SELECT_COLOR;
        }

        if (oldRow != null){
            var currRowIndex = findIndexFormArray(oldRow, this.dataBody.rows);
            for (var i=0; i < oldRow.cells.length; i++){
                oldRow.cells[i].style.backgroundColor = oldRow.cells[i].sColor;
                this.rendererCellColor(oldRow.cells[i], currRowIndex, i, this.getValueAt(currRowIndex, i));
            }
        }

        this.model.setSelectedRow(rowIndex);
    }
}

IpacsTable.prototype.swapColumn = function(aColumnIndex, bColumnIndex){
    this.columnModel.swapColumn(aColumnIndex, bColumnIndex);

    this.refreshData(0, this.model.getRowCount() - 1, aColumnIndex, aColumnIndex);
    this.refreshData(0, this.model.getRowCount() - 1, bColumnIndex, bColumnIndex);

    this.colGroup.children(aColumnIndex).swapNode(this.colGroup.children(bColumnIndex));

    this.tableHead.rows[0].cells[aColumnIndex].innerHTML = this.columnModel.getColumnDesc(aColumnIndex);
    this.tableHead.rows[0].cells[bColumnIndex].innerHTML = this.columnModel.getColumnDesc(bColumnIndex);

    if (this.tableFoot.rows.length > 0){
        var tempHTML = this.tableFoot.rows[0].cells[bColumnIndex].innerHTML;
        this.tableFoot.rows[0].cells[bColumnIndex].innerHTML = this.tableFoot.rows[0].cells[aColumnIndex].innerHTML;
        this.tableFoot.rows[0].cells[aColumnIndex].innerHTML = tempHTML;
    }
}

IpacsTable.prototype.setMessages = function(inMsgKey, inMessage){
    this.messages.Add(inMsgKey, inMessage);
}

IpacsTable.prototype.validate = function(){
    var result = !this.model.hasInvalidValue();

    if (result){
        for (var i=0; i < this.columnModel.getColumnCount(); i++){
            if (!this.columnModel.isColumnAllowEmpty(i)){
                for (var j=0; j < this.model.getRowCount(); j++){
                    var data = this.getValueAt(j, i);
                    if (data == null || data == ""){
                        result = false;
                        var errorCell = this.dataBody.rows[j].cells[i];
                        errorCell.className = "error";
                        errorCell.title = _DATA_EMPTY_MSG;
                        this.rendererCellColor(errorCell, j, j+1, i, i+1);
                    }
                }
            }
        }
    }

    return result;
}

IpacsTable.prototype.prepareSubmit = function(){
    this.stoppingEdit();

    var result = this.validate();
    if (result){
        var submitHTML= "";
        for (var i=0; i < this.model.insertBuffer.length; i++){
            submitHTML += "<input type='hidden' name='" + this.htmlTable.name + "." + _TABLE_ROW_STATUS + "' value='" + _TABLE_ROW_STATUS_ADD + "'>";
            for (var j=0; j < this.model.getColumnCount(); j++){
                var data = this.model.insertBuffer[i][j];
                if (data != null){
                    submitHTML += "<input type='hidden' name='" + this.model.getColumnName(j) + "' value='" + data + "'>";
                } else {
                    submitHTML += "<input type='hidden' name='" + this.model.getColumnName(j) + "' value=''>";
                }
            }
        }

        for (var i=0; i < this.model.modifyBuffer.length; i++){
            submitHTML += "<input type='hidden' name='" + this.htmlTable.name + "." + _TABLE_ROW_STATUS + "' value='" + _TABLE_ROW_STATUS_MODIFY + "'>";
            for (var j=0; j < this.model.getColumnCount(); j++){
                var data = this.model.modifyBuffer[i][j];
                if (data != null){
                    submitHTML += "<input type='hidden' name='" + this.model.getColumnName(j) + "' value='" + data + "'>";
                } else {
                    submitHTML += "<input type='hidden' name='" + this.model.getColumnName(j) + "' value=''>";
                }
            }
        }

        for (var i=0; i < this.model.deleteBuffer.length; i++){
            submitHTML += "<input type='hidden' name='" + this.htmlTable.name + "." + _TABLE_ROW_STATUS + "' value='" + _TABLE_ROW_STATUS_DELETE + "'>";
            for (var j=0; j < this.model.getColumnCount(); j++){
                var data = this.model.deleteBuffer[i][j];
                if (data != null){
                    submitHTML += "<input type='hidden' name='" + this.model.getColumnName(j) + "' value='" + data + "'>";
                } else {
                    submitHTML += "<input type='hidden' name='" + this.model.getColumnName(j) + "' value=''>";
                }
            }
        }

        if (document.all(this.htmlTable.id + "Data") == null){
            this.htmlTable.insertAdjacentHTML("afterEnd", "<div id='" + this.htmlTable.id + "Data'>" + submitHTML + "</div>");
        } else {
            document.all(this.htmlTable.id + "Data").innerHTML = submitHTML;
        }
    }

    return result;
}

IpacsTable.prototype.prepareRefresh = function(){
    var submitHTML= "";
    for (var i=0; i < this.model.getRowCount(); i++){
        for (var j=0; j < this.model.getColumnCount(); j++){
            var data = this.model.getValueAt(i, j);
            if (data != null){
                submitHTML += "<input type='hidden' name='" + this.model.getColumnName(j) + "' value='" + data + "'>";
            } else {
                submitHTML += "<input type='hidden' name='" + this.model.getColumnName(j) + "' value=''>";
            }
        }
    }

    if (document.all(this.htmlTable.id + "Data") == null){
        this.htmlTable.insertAdjacentHTML("afterEnd", "<div id='" + this.htmlTable.id + "Data'>" + submitHTML + "</div>");
    } else {
        document.all(this.htmlTable.id + "Data").innerHTML = submitHTML;
    }
}

IpacsTable.prototype.dragStart = function(){
    var cell = getParentByTagName(event.srcElement,"TD");
    var sTable = getTableByCell(cell);
    var handler = sTable.handler;
    handler.stoppingEdit();

    if (cell == null){
        return;
    }

    window.dragColStart = cell.cellIndex;
    handler.drag.style.width = cell.offsetWidth;
    handler.drag.style.height = cell.offsetHeight;
       handler.drag.innerHTML = cell.innerHTML;
    handler.drag.style.textAlign = handler.columnModel.getColumnAlign(cell.cellIndex);
    handler.drag.style.padding = "2px";
    handler.drag.style.verticalAlign = "middle";
    handler.drag.style.fontWeight = "bold";
    window.drag = handler.drag;
    window.currTable = sTable;

    document.onmouseup = handler.dragEnd;
    document.onselectstart = function(){
        return false;
    }

    function document.onmousemove(){
        drag.style.display    = "";
        drag.style.top    = event.y - drag.offsetHeight/2 + document.body.scrollTop;
        drag.style.left = event.x - drag.offsetWidth/2 + document.body.scrollLeft;

        var columnModel = window.currTable.handler.columnModel;

        var width = window.currTable.clientLeft;
        for (var i=0; i < columnModel.getColumnCount(); i++){
            width += columnModel.getColumnWidth(i);
            if (event.x + document.body.scrollLeft < width){
                window.dragColEnd = i;
                break;
            }
        }
    }
}

IpacsTable.prototype.dragEnd = function(){
    var sTable = window.currTable;

    if (sTable != null){
        var handler = sTable.handler;

        handler.drag.style.display="none";
        handler.drag.innerHTML = "";
        handler.drag.style.width = 0;
        handler.drag.style.height = 0;

           document.onmousemove = null;
        document.onmouseup = null;
        document.onselectstart = null;

        if(window.dragColStart != null && window.dragColEnd!=null && window.dragColStart!=window.dragColEnd){
            handler.swapColumn(window.dragColStart, window.dragColEnd);
        }
    }

    window.drag = null;
    window.currTable = null;
    window.dragColStart = null;
    window.dragColEnd = null;
}

IpacsTable.prototype.release = function(){
    this.htmlTable = undefined;
    this.dataBody = undefined;
    this.tableHead = undefined;
    this.tableFoot = undefined;
    this.colGroup = undefined;
    this.drag = undefined;

    this.model.release();
    this.columnModel.release();
    this.model = undefined;
    this.columnModel = undefined;

    this.columnRenderers = undefined;
    this.colorRenderers = undefined;

    this.cellEditableFilte = undefined;

    this.currCell = undefined;
    this.currRow = undefined;
    this.currEditor = undefined;
}