// Class DataTable, DataRow, DataCell

// #define
var UI_DATATABLE_JS = 1;

//
// class DataCell
//
function DataCell(width,text,name) {
	this.text = ""; if(text) this.text = text;
	this.parentRow = null;
	this.width = 40; if(width != undefined) this.width = width;
	this.classCss = "dataCell";
	this.styleCss = "";
	this.div = null;
	this.name = ""; if(name) this.name = name;
	this.construct = function() {
		this.styleCss += ";width:" + this.width + "px;";
		if(this.width == 0)	this.styleCss += "display:none;";
		var newDiv = document.createElement("div");
		newDiv.setAttribute("id",'dataCell');
		newDiv.setAttribute("class",this.classCss);
		newDiv.className = this.classCss;
		newDiv.setAttribute("style",friend_parseStyle(newDiv,this.styleCss));
		newDiv.setAttribute("style",this.styleCss);
		newDiv.innerHTML = this.text;
		this.div = newDiv;
		this.parentRow.div.appendChild(this.div);
	}
	//
	// sets parent
	//
	this.setParent = function(parentRow) {
		this.parentRow = parentRow;
	}

}
//
// class DataCellCollection
//
function DataCellCollection(parentRow) {
	var aItem = new Array();
	this.parentRow = parentRow;
	//
	// adds new element to the Collection
	//
	this.add = function(Item) {
		aItem.push(Item);
		Item.setParent(this.parentRow);
	}
	//
	// returns current number of elements
	//
	this.count = function() {
		return aItem.length;
	}
	//
	// getItem(Index)
	//
	this.getItem = function(Index) {
		return aItem[Index];
	}
	//
	// getCellByName(sourceName)
	//
	this.getCellByName = function(sourceName) {
		var i;
		for(i = 0; i<aItem.length;i++) {
			if(sourceName == aItem[i].name) return aItem[i];	
		}
		return null;
	}
	
}
//
// class DataRow
//
function DataRow() {
	this.cells = new DataCellCollection(this);
	this.classCss = "dataRow";
	this.styleCss = "";
	this.rollOverBckColor = "#FEF3AB";
	this.bckColor = "";
	this.div = null;
	this.parentDataTable = null;
	this.enableRollOver = true;
	this.enableClick = true;
	//
	// returns cell's object and cell's div
	//
	this.getCell = function(col) {
		return this.cells.getItem(col);
	}
	this.getCellDiv = function(col) {
		return this.cells.getItem(col).div;
	}
	//
	// construct div
	//
	this.construct = function() {
		var newDiv = document.createElement("div");
		newDiv.setAttribute("id",'dataRow');
		newDiv.setAttribute("class",this.classCss);
		newDiv.className = this.classCss;
		newDiv.setAttribute("style",friend_parseStyle(newDiv,this.styleCss));
		newDiv.setAttribute("style",this.styleCss);
		if(this.parentDataTable.enableRollOver && this.enableRollOver) {
			newDiv.onmouseover = function() {
				friend_dataTable_onMouseOver(this,'#4C8FBA');
			}
			newDiv.onmouseout = function() {
				friend_dataTable_onMouseOut(this);
			}
		}
		if(this.parentDataTable.enableClick && this.enableClick) {
			var nRowNumber =  this.parentDataTable.getRowNumber(this);
			//newDiv.setAttribute("onclick",this.parentDataTable.onClickFunctionName + "(" + nRowNumber + ")");
			newDiv.d = this;
			var cFunction = this.parentDataTable.onClickFunctionName;
			newDiv.onclick = function() {
				eval(cFunction + "(" + nRowNumber + ")");
			}
		}
		this.div = newDiv;
		this.parentDataTable.div.appendChild(this.div);
		var i;
		for(i=0;i<this.cells.count();i++) {
			this.cells.getItem(i).construct();	
		}
	}
	//
	// sets parent
	//
	this.setParent = function(parentDataTable) {
		this.parentDataTable = 	parentDataTable;
	}
	//
	// returns cell's col number
	//
	this.getCellColNumber = function(objCell) {
		var i;
		for(i=0;i<this.cells.count();i++) {
			if(this.cells.getItem(i) == objCell) return i;	
		}
		return -1;
	}
}
//
// class DataRowCollection
//
function DataRowCollection(parentDataTable) {
	var aItem = new Array();
	this.parentDataTable = parentDataTable;
	//
	// adds new element to the Collection
	//
	this.add = function(Item) {
		aItem.push(Item);
		Item.setParent(this.parentDataTable);
	}
	//
	// returns current number of elements
	//
	this.count = function() {
		return aItem.length;
	}
	//
	// getItem(Index)
	//
	this.getItem = function(Index) {
		return aItem[Index];
	}
	//
	// emptys the Array
	//
	this.clear = function() {
		aItem = new Array();
	}
}
//
// class DataColumn
//
function DataColumn(width,title,sourceMember,classCss, styleCss) {
	this.width = 40; if(width != undefined) this.width = width;	
	this.title = ""; if(title) this.title = title;
	this.sourceMember = ""; if(sourceMember) this.sourceMember = sourceMember;
	this.classCss = null; if(classCss) this.classCss = classCss;
	this.styleCss = ""; if(styleCss) this.styleCss =styleCss;
}
//
// class DataColumnCollection
//
function DataColumnCollection() {
	var aItem = new Array();
	// adds new element to the Collection
	this.add = function(Item) {
		aItem.push(Item);
	}
	// returns current number of elements
	this.count = function() {
		return aItem.length;
	}
	// getItem(Index)
	this.getItem = function(Index) {
		return aItem[Index];
	}
}
//
// class DataTableCreator
//
function DataTableCreator(dataTable) {
	var _bEvents = new Broadcaster();
	if(! dataTable) throw "DataTableCreator dataTable is not defined";
	this.dataTable = dataTable;
	this.columns = new DataColumnCollection();
	this.xmlSource = null;
	this.xml = null
	
	this.getAttributeValue = function(attName) {
		if(this.xml == null) return null;
		for(i=0;i<this.xml.attributes.length;i++) {
			if(this.xml.attributes[i].name == attName) return this.xml.attributes[i].value;
		}
		return null;
	}
	
	this.createTable = function(xmlSource) {
		if(xmlSource) this.xmlSource = xmlSource;
		if(! this.xmlSource) throw ("DataTableCreator, xmlSource is not defined");
		if (window.ActiveXObject) { //IE
		  var doc=new ActiveXObject("Microsoft.XMLDOM");
		  doc.async="false";
		  doc.loadXML(this.xmlSource);
		} else {
		  var parser=new DOMParser();
		  var doc=parser.parseFromString(this.xmlSource,"text/xml");
		}
		this.xml=doc.documentElement;
		// creating a table
		var i,j,k;
		var nRowTitle = new DataRow();
		nRowTitle.enableRollOver = false;
		nRowTitle.enableClick = false;
		for(i=0;i<this.columns.count();i++) {
			var objColumn = this.columns.getItem(i);
			var nCell = new DataCell(objColumn.width, objColumn.title);
			nCell.classCss = "dataTableTitleCell";
			nCell.styleCss = objColumn.styleCss;
			nRowTitle.cells.add(nCell);
		}
		this.dataTable.rows.add(nRowTitle);
		for(i=0;i<this.xml.childNodes.length;i++) {
			var obNode = this.xml.childNodes[i];
			var nRow = new DataRow();
			this.dataTable.rows.add(nRow);
			var nCell = new DataCell(this.columns.getItem(0).width,obNode.attributes[0].value);	
			nCell.name = "ID";
			nRow.cells.add(nCell);
			for(j=0;j<obNode.childNodes.length;j++) {
				var nCell2 = new DataCell();
				for(k=0;k<this.columns.count();k++) {
					if(this.columns.getItem(k).sourceMember == obNode.childNodes[j].nodeName) {
						var oColumn = this.columns.getItem(k);
						var oNode = obNode.childNodes[j];
						nCell2.width = oColumn.width;
						nCell2.text = oNode.firstChild.nodeValue;
						nCell2.name = this.columns.getItem(k).sourceMember;
						if(oColumn.classCss) nCell2.classCss = oColumn.classCss;
						if(oColumn.styleCss) nCell2.styleCss = oColumn.styleCss;
						nRow.cells.add(nCell2);
						break;
					}
				}
			}
		}
		this.dataTable.show();
		
		//alert(this.xml.attributes[0].name);
		
	}
	this.refreshTable = function() {
		_bEvents.broadcastMessage("refresh");
	}
	this.broadcaster = function() {
		return _bEvents;
	}
	this.dataSaved = function(Row) {
		_bEvents.broadcastMessage("dataSaved",Row);
	}

}
//
// class DataTable
//
function DataTable(parentDiv) {
	this.rows = new DataRowCollection(this);
	this.parentDiv = parentDiv;
	this.classCss = "dataTable";
	this.styleCss = "";
	this.dataTableCreator = new DataTableCreator(this);
	this.div = null;
	this.enableRollOver = true;
	this.enableClick = true;
	this.specialRow = null;
	this.tag = "";
	this.selectedRowIndex = -1;
	this.onClickFunctionName = "dataTableClicked";
	this.construct = function (parentDiv) {
		if(parentDiv) this.parentDiv = parentDiv;
		var newDiv = document.createElement("div");
		newDiv.setAttribute("id",'dataTable');
		newDiv.setAttribute("class",this.classCss);
		newDiv.className = this.classCss;
		newDiv.setAttribute("style",friend_parseStyle(newDiv,this.styleCss));
		newDiv.setAttribute("style",this.styleCss);
		this.div = newDiv;
		this.parentDiv.appendChild(this.div);
		var i;
		for(i=0;i<this.rows.count();i++) {
			this.rows.getItem(i).construct();	
		}
		//var dN = document.createElement("div");
		//dN.appendChild(newDiv);
		//var sD = dN.innerHTML;
		//this.parentDiv.innerHTML += sD;
	}
	this.show = function(parentDiv) {
		if(parentDiv) this.parentDiv = parentDiv;
		this.construct(this.parentDiv);
	}
	this.getRowNumber = function(objRow) {
		var i;
		for(i=0;i<this.rows.count();i++) {
			if(this.rows.getItem(i) == objRow) return i;	
		}
		return -1;
	}
	this.getCellValue = function(row,col) {
		return this.rows.getItem(row).cells.getItem(col).div.innerHTML;	
	}
	this.getCell = function(row,col) {
		return this.rows.getItem(row).cells.getItem(col);	
	}
	this.getSelectedRow =function() {
		if(this.selectedIndex == -1) return null;
		return this.cells.getItem(this.selectedIndex);
	}

}
//
// debug
//
function dt_debug(sText,append) {
	ob = document.getElementById("debug");
	ob.style.display = "block";
	if(append) {
		ob.value += "\n" + sText;
	} else {
		ob.value = sText;
	}
}

// friendly functions
//
// handles on mouse over event
//
function friend_dataTable_onMouseOver(obj,color) {
	friend_menu_changeColor(obj,color);
}
//
// handles on mouse out event
//
function friend_dataTable_onMouseOut(obj) {
	friend_menu_changeColor(obj,'');
}

