function TableModel(id){

    this.model = document.getElementById(id);
    this.numColumns = this.model.getElementsByTagName('th').length;
    this.align = new Array();
    this.tHead = 1;

    if(this.model.tHead){
        this.model = this.model.tBodies[0];
        this.tHead = 0;
    }

    this.add = function(array){
    	
    	if(this.model.rows.length>7){
    		return;
    	}

        var row = this.model.insertRow(-1);

        row.onmouseover = function(){
            this.className = 'rowOver';
        };

        row.onmouseout = function(){
            this.className = 'row';
        };

        for(var i = 0; i < this.numColumns; i++){
            var col = row.insertCell(i);
            col.innerHTML = array[i];
            col.align = this.align[i];
        }

    };

    this.replace = function(input, index){
        var inputName = input.name;
        var l = inputName.indexOf('[') + 1;
        var r = inputName.indexOf(']');
        inputName = inputName.substr(l, (r - l));
        input.name = input.name.replace(inputName, (index - this.tHead));
    };

    this.organize = function(){
        var trs = this.model.rows;
        for(var i = 0; i < trs.length; i++){

            var inputs = trs[i].getElementsByTagName('input');
            for(var j = 0; j < inputs.length; j++){
                this.replace(inputs[j], i);
                
      		  var iLen = String(inputs[j].name).length;
    		  if(inputs[j].name.substring(iLen, iLen - 6)=="idxIte") {                
                	inputs[j].value = i+1;
                }
            }
            

            var textarea = trs[i].getElementsByTagName('textarea');
            for(var j = 0; j < textarea.length; j++){
                this.replace(textarea[j], i);
            }

        }
        
    };

    this.remove = function(row){
        if(this.model.rows.length > 0){
            this.model.deleteRow(this.tHead == 0 ? row.rowIndex - 1 : row.rowIndex);
            this.organize();
        }
    };

    this.removeLast = function(){
        if(this.model.rows.length > this.tHead){
            this.model.deleteRow((this.model.rows.length - 1));
        }
    };

    this.getNumRows = function(){
        return this.model.rows.length;
    };

    this.removeAll = function(){
        while(this.model.rows.length > this.tHead){
            for(var i = this.tHead; i < this.model.rows.length; i++){
                this.model.deleteRow(i);
            }
        }
    };
    
}
