/*
* dynamic table create
*
* ver : 1.0
* create date : 2010-01-22
* last update : 2010-01-22
* create by Bestlee
*/

var arrDyTb = new Array();

function DyTable(oName)
{
	//constant data;
	this.selectedIdx	= -1;
	this.maxRowLimit	= 10;
	this.tbObj			= null;
	this.trbj			= null;
	this.tdObj			= null;
	this.ObjName			= null;

	// dynamic data
	//color data
	this.bgColor		= "white";
	this.ovColor		= "#EFF3FF";
	//cell style data
	this.tdAlign		= "left";

	// methods
	this.initialize = DyTb_initialize;
	this.insertRow = DyTb_insertRow;
	this.insertCell = DyTb_insertCell;
	this.deleteRow = DyTb_deleteRow;
	this.EventInit = DyTb_EventInit;
	this.MouseOver = DyTb_MouseOver;
	this.MouseOut = DyTb_MouseOut;

	//initialize
	this.initialize(oName);

		

}

//initialize
function DyTb_initialize(oName)
{
	var obj = document.getElementById(oName);
	if ( obj == null )
	{
		alert("Object Not Found");
		this.tbObj = null
	}
	else
	{
		this.tbObj = obj;
		arrDyTb[oName] = this;
		this.ObjName = oName;
		this.EventInit();
	}
}

//Mouse Over
function DyTb_MouseOver(tblRow,oName)
{
	var obj = arrDyTb[oName];
	tblRow.style.backgroundColor = obj.ovColor;
	obj.selectedIdx = tblRow.rowIndex;
}

//Mouse Out
function DyTb_MouseOut(tblRow,oName)
{
	var obj = arrDyTb[oName];
	tblRow.style.backgroundColor = obj.bgColor;
}

//set Event
function DyTb_EventInit()
{
	//cell event reset
	var Obj = this.ObjName;
	for(var i = 0 ; i < this.tbObj.rows.length ; i++){
		this.tbObj.rows[i].onmouseover = function(){DyTb_MouseOver(this,Obj)}
		this.tbObj.rows[i].onmouseout = function(){DyTb_MouseOut(this,Obj)}
	}
}

//insert row
function DyTb_insertRow()
{
	if (this.tbObj != null)
	{
		this.trObj = this.tbObj.insertRow(this.tbObj.rows.length)
		this.EventInit();
	}
}

//insert cell
function DyTb_insertCell(txt)
{
	if (this.tbObj != null)
	{
		this.tdObj = this.trObj.insertCell(this.trObj.cells.length);
		this.tdObj.align = this.tdAlign;
		this.tdObj.innerHTML = txt;
	}
}

//delete row
function DyTb_deleteRow(oName)
{
	var obj = arrDyTb[oName];
	if (obj != null)
	{
		if (obj.selectedIdx > -1)
		{
			obj.tbObj.deleteRow(obj.selectedIdx);
			obj.selectedIdx = -1;
		}
	}
}


/*
* sample code
**************************************
	function tadd()
	{
		var tb = new DyTable("mtb");
		tb.ovColor	= red;
		tb.bgColor	= white;

		//insert row
		tb.insertRow();

		//insert cell
		tb.insertCell("11111");
		//delete button cell insert
		tb.insertCell("<input type=\"button\" value=\"del\" onclick=\"DyTb_deleteRow('mtb')\">");

	}

	//html
	<input type="button" value="aaaaa" onclick="tadd()">
	<table border=1 width=500 id="mtb">
		<tr>
			<td>11</td>
			<td>22</td>
		</tr>
		<tr>
			<td>111</td>
			<td>221</td>
		</tr>
		<tr>
			<td>1122</td>
			<td><input type="button" value="del" onclick="DyTb_deleteRow('mtb')"></td>
		</tr>
		<tr>
			<td>113</td>
			<td><a href="javascript:void(DyTb_deleteRow('mtb'))">del</a></td>
		</tr>
	</table>
**********************************/

