/**
 * Table Model
 */

function IpacsTableModel(inData){
    this.columnName = inData[0];
    this.columnClass = inData[1];
    this.data = inData.slice(2);
    this.data.modelMeta = inData.modelMeta;
    this.data.defaultValue = inData.defaultValue;

    if (typeof this.data.modelMeta == "undefined"){
        this.data.modelMeta = "AMD";
    }

    this.columnIndex = new ActiveXObject("Scripting.Dictionary");
    for (var i=0; i<this.columnName.length; i++){
        this.columnIndex.Add(this.columnName[i], i);
    }

    this.listeners = new Array();

    this.selectedRow = null;

    this.insertBuffer = new Array();
    this.deleteBuffer = new Array();
    this.modifyBuffer = new Array();

    this.beforeInsertRowHandler = new Array();
    this.afterInsertRowHandler = new Array();
    this.beforeDeleteRowHandler = new Array();
    this.afterDeleteRowHandler = new Array();
    this.beforeModifyValueHandler = new Array();
    this.afterModifyValueHandler = new Array();

    this.selectedRowHandler = new Array();
}

IpacsTableModel.prototype.getRowCount = function(){
    return this.data.length;
}

IpacsTableModel.prototype.getModelMeta = function(){
    return this.data.modelMeta;
}

IpacsTableModel.prototype.getRowMeta = function(rowIndex){
    if (typeof this.data[rowIndex].rowMeta != "undefined"){
        return this.data[rowIndex].rowMeta;
    } else {
        return "";
    }
}

IpacsTableModel.prototype.isRowEditable = function(rowIndex){
    var rowMeta = this.getRowMeta(rowIndex);
    if (rowMeta != null && rowMeta.split("|")[0] == "R"){
        return false;
    } else {
        return true;
    }
}

IpacsTableModel.prototype.getColumnCount = function (){
    return this.columnName.length;
}

IpacsTableModel.prototype.getColumnName = function (columnIndex){
    return this.columnName[columnIndex];
}

IpacsTableModel.prototype.getColumnClass = function (columnIndex){
    return this.columnClass[columnIndex];
}

IpacsTableModel.prototype.getColumnIndex = function (inColumnName){
    return this.columnIndex.Item(inColumnName);
}

IpacsTableModel.prototype.isCellEditable = function (rowIndex, columnIndex){
}

IpacsTableModel.prototype.isNewRow = function (rowIndex){
    return findIndexFormArray(this.data[rowIndex], this.insertBuffer) != -1;
}

IpacsTableModel.prototype.getValueAt = function (rowIndex, columnIndex){
    try{
        if (isNaN(parseInt(columnIndex))){
            columnIndex = this.getColumnIndex(columnIndex);
        }

        var result = this.data[rowIndex][columnIndex];
        return result;
    }catch (e){
        return null;
    }
}

IpacsTableModel.prototype.setValueAt = function (rowIndex, columnIndex, aValue){
    if (this.getModelMeta().indexOf("M") != -1){
        if (aValue != null){
            aValue = aValue.toString();
        }

        if (isNaN(parseInt(columnIndex))){
            columnIndex = this.getColumnIndex(columnIndex);
        }

        var event = new Event();

        if (this.beforeModifyValueHandler.length > 0){
            event = new Event(this, "TABLE_MODEL_VALUE_BEFORE_CHANGE", ([rowIndex, columnIndex, this.getValueAt(rowIndex, columnIndex), aValue]));
            for (var i=0; i<this.beforeModifyValueHandler.length; i++){
                eval(this.beforeModifyValueHandler[i] + "(event)");
            }
        }

        if (aValue != this.data[rowIndex][columnIndex] && event.returnValue){
            this.data[rowIndex][columnIndex] = aValue;
            if (findIndexFormArray(this.data[rowIndex], this.insertBuffer) == -1
                && findIndexFormArray(this.data[rowIndex], this.modifyBuffer) == -1){
                this.modifyBuffer[this.modifyBuffer.length] = this.data[rowIndex];
            }

            if (this.afterModifyValueHandler.length > 0){
                event = new Event(this, "TABLE_MODEL_VALUE_AFTER_CHANGE", ([rowIndex, columnIndex, aValue]));
                for (var i=0; i<this.afterModifyValueHandler.length; i++){
                    eval(this.afterModifyValueHandler[i] + "(event)");
                }
            }

            event = new Event(this, "TABLE_MODEL_MODIFY", ([rowIndex, rowIndex, columnIndex, columnIndex]));
            this.fireModelChanged(event);
        }
    }
}

IpacsTableModel.prototype.setSelectedRow = function(inRowIndex){
    this.selectedRow = this.data[inRowIndex];

    var event = new Event();
    event = new Event(this, "TABLE_MODEL_SELECTED_ROW_CHANGE", ([inRowIndex, inRowIndex]));
    for (var i=0; i<this.selectedRowHandler.length; i++){
        eval(this.selectedRowHandler[i] + "(event)");
    }

    this.fireModelChanged(event);
}

IpacsTableModel.prototype.setValueInvalid = function(rowIndex, columnIndex){
    if (this.data[rowIndex].invalidMark == null){
        this.data[rowIndex].invalidMark = new Array();
    }

    var columnName = this.getColumnName(columnIndex);
    if (findIndexFormArray(columnName, this.data[rowIndex].invalidMark) == -1){
        this.data[rowIndex].invalidMark[this.data[rowIndex].invalidMark.length] = columnName;
    }

    var event = new Event();
    event = new Event(this, "TABLE_VALUE_INVALID", ([rowIndex, rowIndex, columnIndex, columnIndex]));
    this.fireModelChanged(event);
}

IpacsTableModel.prototype.setValueValid = function(rowIndex, columnIndex){
    if (this.data[rowIndex].invalidMark != null){
        var columnName = this.getColumnName(columnIndex);
        var index = findIndexFormArray(columnName, this.data[rowIndex].invalidMark);
        if (index != -1){
            this.data[rowIndex].invalidMark.splice(index, 1);
        }
    }
}

IpacsTableModel.prototype.hasInvalidValue = function(){
    for (var i=0; i < this.getRowCount(); i++){
        if (this.data[i].invalidMark != null && this.data[i].invalidMark.length > 0){
            return true;
        }
    }

    return false;
}

IpacsTableModel.prototype.initRow = function (){
    var rowData = new Array();

    for (var i=0; i<this.columnName.length; i++){
        if (typeof this.data.defaultValue != "undefined"){
            rowData[i] = this.data.defaultValue[i];
        } else {
            rowData[i] = null;
        }
    }

    return rowData;
}

IpacsTableModel.prototype.appendRow = function (inData){
    if (this.getModelMeta().indexOf("A") != -1){
        var event = new Event();
        if (this.beforeInsertRowHandler.length > 0){
            event = new Event(this, "TABLE_MODEL_BEFORE_INSERT", ([inData]));
            for (var i=0; i<this.beforeInsertRowHandler.length; i++){
                eval(this.beforeInsertRowHandler[i] + "(event)");
            }
        }

        if (event.returnValue){
            if (inData != null){
                this.data[this.getRowCount()] = inData;
            } else {
                this.data[this.getRowCount()] = this.initRow();
            }

            this.insertBuffer[this.insertBuffer.length] = this.data[this.getRowCount() - 1];

            event = new Event(this, "TABLE_MODEL_INSERT", ([this.getRowCount() - 1, this.getRowCount() - 1]));
            this.fireModelChanged(event);

            if (this.afterInsertRowHandler.length > 0){
                event = new Event(this, "TABLE_MODEL_AFTER_INSERT", ([this.getRowCount() - 1]));
                for (var i=0; i<this.afterInsertRowHandler.length; i++){
                    eval(this.afterInsertRowHandler[i] + "(event)");
                }
            }
        }
    }
}

IpacsTableModel.prototype.copyRow = function (){
    var newRow = null;
    if (this.selectedRow != null){
        var length = this.selectedRow.length;
        newRow = this.selectedRow.slice(0, length);
    } else if (this.getRowCount() > 0){
        var length = this.data[this.getRowCount() - 1].length;
        newRow = this.data[this.getRowCount() - 1].slice(0, length);
    }

    this.appendRow(newRow);
}

IpacsTableModel.prototype.deleteRow = function (rowIndex){
    var result = false;

    if (this.getModelMeta().indexOf("D") != -1){
        var event = new Event();
        if (this.beforeDeleteRowHandler.length > 0){
            event = new Event(this, "TABLE_MODEL_BEFORE_DELETE", ([rowIndex]));
            for (var i=0; i<this.beforeDeleteRowHandler.length; i++){
                eval(this.beforeDeleteRowHandler[i] + "(event)");
            }
        }

        if (event.returnValue){
            var index = findIndexFormArray(this.data[rowIndex], this.insertBuffer);
            if (index == -1){
                this.deleteBuffer[this.deleteBuffer.length] = this.data[rowIndex];
            } else {
                this.insertBuffer.splice(index, 1);
            }

            index = findIndexFormArray(this.data[rowIndex], this.modifyBuffer);
            if (index != -1){
                this.modifyBuffer.splice(index, 1);
            }

            this.data.splice(rowIndex, 1);

            event = new Event(this, "TABLE_MODEL_DELETE", ([rowIndex, rowIndex + 1]));
            this.fireModelChanged(event);

            if (this.afterDeleteRowHandler.length > 0){
                event = new Event(this, "TABLE_MODEL_AFTER_DELETE", ([rowIndex]));
                for (var i=0; i<this.afterDeleteRowHandler.length; i++){
                    eval(this.afterDeleteRowHandler[i] + "(event)");
                }
            }

            result = true;
        }
    }

    return result;
}

IpacsTableModel.prototype.addTableModelListener = function (listener){
    this.listeners[this.listeners.length] = listener;
}

IpacsTableModel.prototype.removeTableModelListener = function (listener){
    for (var i=0; i<listeners.length; i++){
        if(listeners[i] == listener){
            listeners.splice(i, 1);
            break;
        }
    }
}

IpacsTableModel.prototype.fireModelChanged = function (event){
    for (var i=0; i<this.listeners.length; i++){
        this.listeners[i].modelChanged(event);
    }
}

IpacsTableModel.prototype.addBeforeModifyHandler = function(inHandler){
    this.beforeModifyValueHandler[this.beforeModifyValueHandler.length] = inHandler;
}

IpacsTableModel.prototype.addAfterModifyHandler = function(inHandler){
    this.afterModifyValueHandler[this.afterModifyValueHandler.length] = inHandler;
}

IpacsTableModel.prototype.addBeforeInsertRowHandler = function(inHandler){
    this.beforeInsertRowHandler[this.beforeInsertRowHandler.length] = inHandler;
}

IpacsTableModel.prototype.addAfterInsertRowHandler = function(inHandler){
    this.afterInsertRowHandler[this.afterInsertRowHandler.length] = inHandler;
}

IpacsTableModel.prototype.addBeforeDeleteRowHandler = function(inHandler){
    this.beforeDeleteRowHandler[this.beforeDeleteRowHandler.length] = inHandler;
}

IpacsTableModel.prototype.addAfterDeleteRowHandler = function(inHandler){
    this.afterDeleteRowHandler[this.afterDeleteRowHandler.length] = inHandler;
}

IpacsTableModel.prototype.addSelectedRowHandler = function(inHandler){
    this.selectedRowHandler[this.selectedRowHandler.length] = inHandler;
}

IpacsTableModel.prototype.release = function(){
    this.columnName = undefined;
    this.columnClass = undefined;
    this.data = undefined;

    this.columnIndex = undefined;
      this.listeners = undefined;

    this.selectedRow = undefined;

    this.insertBuffer = undefined;
    this.deleteBuffer = undefined;
    this.modifyBuffer = undefined;

    this.beforeInsertRowHandler = undefined;
    this.afterInsertRowHandler = undefined;
    this.beforeDeleteRowHandler = undefined;
    this.afterDeleteRowHandler = undefined;
    this.beforeModifyValueHandler = undefined;
    this.afterModifyValueHandler = undefined;

    this.selectedRowHandler = undefined;
}