if ((top.DynamicTableSetInit == "undefined") || (top.DynamicTableSetInit != true))
{
	/**
	*
	* DynamicTableSet CLASS definition.
	*
	*/
	function DynamicTableSet(tableSetName)
	{
		this.tableName = tableSetName;
		this.nextId = 0;
		this.cellAsAnchor = false;
	}

	DynamicTableSet.prototype = new TableSet();
	DynamicTableSet.prototype.constructor = DynamicTableSet;

	DynamicTableSet.prototype.setCellAsAnchor = function(_cellAsAnchor)
	{
		this.cellAsAnchor = _cellAsAnchor;
	}

	DynamicTableSet.prototype.isCellAsAnchor = function()
	{
		return this.cellAsAnchor;
	}
	
	DynamicTableSet.prototype.addRow = function(rowId, cellList)
	{
		var htmlComponent = this.getHtmlComponent();
		var tableName = this.tableName;
		var table = this.getDocument().getElementById(this.tableName);
		var tr = table.insertRow(table.rows.length);
		
		tr.setAttribute('class', 'row');
		tr.setAttribute('id', rowId);
		
		tr.onclick = function() {
			TableSetActionManager.doRowHighlight(tableName,this);
		}
		tr.ondblclick = function () {
			TableSetActionManager.doRowDoubleClick(tableName,this);
		}
		
		var text,td;
		for(var i=0; i<cellList.length; i++){
			text = this.getDocument().createTextNode(cellList[i].getText());
			td = this.getDocument().createElement("td");
			td.appendChild(text);
			var attrs = cellList[i].getAttributes();
			if ( attrs != null ) {
				$H(attrs.items).each(function(item) {
						td.setAttribute(item.key, item.value);
 				});				
/*				for (var j in attrs.items) { 
					td.setAttribute(j, attrs.items[j]);
				}*/
			}
			tr.appendChild(td);
		}
		this.initializeRowData();
	}

	DynamicTableSet.prototype.insertRow = function(rowId, index, cellList)
	{
		var htmlComponent = this.getHtmlComponent();
		var tableName = this.tableName;
		var table = this.getDocument().getElementById(this.tableName);
		var tr = table.insertRow(index);
		
		tr.setAttribute('id', rowId);
		
		tr.onclick = function() {
			TableSetActionManager.doRowSelected(tableName,this);
		}
		tr.ondblclick = function () {
			TableSetActionManager.doRowSelected(tableName,this);
		}
		
		var text,td, anchor;
		for(var i=0; i<cellList.length; i++){
			text = this.getDocument().createTextNode(cellList[i].getText());
			td = this.getDocument().createElement("td");
			if ( this.isCellAsAnchor() ) {
				anchor = this.getDocument().createElement("a");
				anchor.appendChild(text);
				anchor.href = 'javascript:';
				td.appendChild(anchor);
			} 
			else {
				td.appendChild(text);
			}
			var attrs = cellList[i].getAttributes();
			if ( attrs != null ) {
				$H(attrs.items).each(function(item) {
						td.setAttribute(item.key, item.value);
 				});				
/*				for (var j in attrs.items) { 
					td.setAttribute(j, attrs.items[j]);
				}*/ 
			}
			tr.appendChild(td);
		}
		this.initializeRowData();
	}

	DynamicTableSet.prototype.deleteAllRows = function(){
		var table = this.getDocument().getElementById(this.tableName);
		var len = table.rows.length;
		for(var i=0;i<len;i++){
			table.deleteRow(0);
		}
	}

	DynamicTableSet.prototype.deleteRow = function(rowId){
		var table = this.getDocument().getElementById(this.tableName);
		for (i=0; i < this.rowsList.length; i++){
			if( this.rowsList[i].id == rowId ){
				table.deleteRow(i);
				break;
			}
		}
		this.highlightInitialRow();
	}

	DynamicTableSet.prototype.deleteSelectedRow = function(){
		var table = this.getDocument().getElementById(this.tableName);
		for (i=0; i < this.rowsList.length; i++){
			if(!(this.rowsList[i].className=="row")){
				table.deleteRow(i);
				break;
			}
		}
		this.highlightInitialRow();
	}

	DynamicTableSet.prototype.getNextRowId = function(){
		return this.nextId++;
	}

	/**
	*
	* DynamicTableSet CREATE definition.
	*
	*/

	TableSetActionManager.prototype.createDynamicTableSet = function (tableSetName, documentObj){
		var dynamicTableSet = new DynamicTableSet(tableSetName);
		dynamicTableSet.initializeRowDataInitial(documentObj);
		this.tableSetArray[tableSetName] = dynamicTableSet;
		TableSetActionManager.setManager(tableSetName, this);
	}

	/**
	*
	* DynamicTableSet EVENT MANAGER definition.
	*
	* Handles component updates
	*
	*/

	function DynamicTableSetEventManager(){
		this.xmlWrapper = new XmlWrapper();
	}

	DynamicTableSetEventManager.prototype.handle = function(publisher, eventName, eventId, payLoad) {
		payLoad = this.xmlWrapper.unWrapXml(payLoad);
		if(eventName == 'deleteallrows') {
			var tableSet = tableSetActionManager.getTableSet(eventId);
			tableSet.deleteAllRows();
		} else if(eventName == 'deleteselectedrow'){
			var tableSet = tableSetActionManager.getTableSet(eventId);
			tableSet.deleteSelectedRow();
		} else if(eventName == 'addRow'){
			var tableSet = tableSetActionManager.getTableSet(eventId);
			var rowId = tableSet.getNextRowId();
			tableSet.addRow(rowId, payLoad);
		}
	}

	top.dynamicTableSetEventManager = new DynamicTableSetEventManager();
	top.eventService.subscribe("DynamicTableSet","deleteallrows","-",top.dynamicTableSetEventManager);
	top.eventService.subscribe("DynamicTableSet","deleteselectedrow","-",top.dynamicTableSetEventManager);
	top.eventService.subscribe("DynamicTableSet","addRow","-",top.dynamicTableSetEventManager);

	//////////////////////////////////////

	top.dynamicTableSetInit = true;
}
