/*
##############################
## Dinamik Tablo Oluşturucu ##
##############################
Bu kod ile ilgili bilgi için: http://cookingthecode.com/a15_Dinamik-Tablo-(Dynamic-Table)
*/

function dyntable(vn,t,cCont)
{
	this.varName=vn;	// bu sınıftan yaratılan nesnenin referans adı (değişken adı)
	this.tbl;	// tablo
	this.tby;	// tablo gövde kısmı
	if(t)
	{
		this.tbl=t;
		if(t.getElementsByTagName('tbody').length>0) this.tby=t.getElementsByTagName('tbody')[0];
		this.setTdEvents(this.tby.getElementsByTagName('td'));
	}
	this.selectionMode='row';
	this.selectedColumn;
	this.selectedRow;
	this.editingRow;

	this.codeContainer;	// tablo kaynak kodunun içinde bulunacağı element. örn: textbox,textarea
	if(cCont) this.codeContainer=cCont;
}
dyntable.prototype.create=function(cols,rows)
{
	cols=parseInt(0+cols); // sütun sayısı
	rows=parseInt(0+rows); // satır sayısı
	if(rows<1  || cols<1 ) {alert('Sütun ve satır sayısı en az bir(1) olmalıdır.'); return false;}
	
	// tablo ve tablo kısımları yaratılıyor.
	this.tbl=document.createElement('table');
	
	this.tby=document.createElement('tbody');	// içerik satırlarını içine alacak tablo gövdesi
	
	//ilk satır oluşuruluyor ve satır içine sütun sayısı kadar hücre oluşturuluyor.
	var trow=document.createElement('tr');
	for(var i=0;i<cols;i++)
	{
		var td=document.createElement('td');td.innerHTML='&nbsp;';
		trow.appendChild(td);
	}
	
	// oluşturulan satır, satır sayısı kez tablo gövdesine ekleniyor.
	for(var i=0;i<rows;i++) this.tby.appendChild(trow.cloneNode(true));
	
	// hücre metodları ayarlanıyor
	this.setTdEvents(this.tby.getElementsByTagName('td'));
	
	// tablo kısımları tabloya ekleniyor.
	this.tbl.appendChild(this.tby);
	return true;
}

dyntable.prototype.setTdEvents=function(tds)
{
	var vn=this;
	for(var i=0;i<tds.length;i++)
	{
		tds[i].onclick=function(){
			if(vn.editingRow==this.parentNode) return false;
			if(vn.selectionMode=='row') {vn.selectedRow=this.parentNode; vn.selectRow();} else {vn.selectedColumn=this; vn.selectColumn();}
		}
		tds[i].ondblclick=function(){
			if(vn.editingRow==this.parentNode) return false;
			vn.editingRow=this.parentNode;
			vn.toEditMode();
		}
		tds[i].onkeydown=function(e){
			var e=e || event;
			if(e.keyCode==13 || e.keyCode==27) vn.toTextMode();
			else if((e.keyCode==40 || (e.keyCode==9 && this.cellIndex==this.parentNode.getElementsByTagName('td').length-1)) && vn.editingRow.rowIndex<vn.editingRow.parentNode.getElementsByTagName('tr').length-1)
			{
				var eR=vn.editingRow.parentNode.getElementsByTagName('tr')[vn.editingRow.rowIndex+1];
				vn.clearSelectionClass();
				vn.editingRow=eR;
				vn.toEditMode();
				if(e.keyCode==40) vn.editingRow.getElementsByTagName('input')[0].focus();
			}
			else if(e.keyCode==38 && vn.editingRow.rowIndex>0)
			{
				var eR=vn.editingRow.parentNode.getElementsByTagName('tr')[vn.editingRow.rowIndex-1];
				vn.clearSelectionClass();
				vn.editingRow=eR;
				vn.toEditMode();
				vn.editingRow.getElementsByTagName('input')[0].focus();
			}
		}
	}
}
dyntable.prototype.addColumn=function()
{
	if(!this.tbl) return false;
	var rows=this.tby.getElementsByTagName('tr').length;	// satır sayısı
	for(var i=0;i<rows;i++)
	{
		var tds=this.tby.getElementsByTagName('tr')[i].getElementsByTagName('td');
		this.tby.getElementsByTagName('tr')[i].appendChild(tds[tds.length-1].cloneNode(false));
		this.setTdEvents(new Array(tds[tds.length-1]));
	}
	this.clearSelectionClass();
	return true;
}
dyntable.prototype.addRow=function()
{
	if(!this.tbl) return false;
	var trs=this.tby.getElementsByTagName('tr');
	this.tby.appendChild(trs[trs.length-1].cloneNode(true));
	var tds=this.tby.getElementsByTagName('tr')[trs.length-1].getElementsByTagName('td');
	tds[0].parentNode.className='';	//	eğer klonlanan son satır seçili olabilir, bu yüzden yeni satırın sitilleri temizleniyor
	for(var i=0;i<tds.length;i++) tds[i].innerHTML='&nbsp;';
	this.setTdEvents(tds);
	this.clearSelectionClass();
	return true;
}
dyntable.prototype.selectRow=function()
{
	this.clearSelectionClass();
	this.selectedRow.className=this.selectedRow.className+' selected ';
}
dyntable.prototype.selectColumn=function()
{
	this.clearSelectionClass();
	var colIndex=this.selectedColumn.cellIndex;
	var trs=this.tby.getElementsByTagName('tr')
	for(var i=0;i<trs.length;i++) trs[i].getElementsByTagName('td')[colIndex].className+=' selected';
}

dyntable.prototype.removeColumn=function()
{
	if(this.selectionMode!='column' || this.selectedColumn==null) {alert('Silinecek sütunu seçmemişsiniz.'); return false;}
	if(this.tby.getElementsByTagName('tr')[0].getElementsByTagName('td').length<2)  {alert('Tabloda en az bir sütun olmalıdır. Silme işlemi yapılamaz.'); return false;}
	if(!confirm('Seçili sütunu silinsin mi?')) return false;
	 
	var colIndex=this.selectedColumn.cellIndex;
	var trs=this.tby.getElementsByTagName('tr');
	for(var i=0;i<trs.length;i++) trs[i].removeChild(trs[i].getElementsByTagName('td')[colIndex]);
	if(trs.length>0) return trs[0].getElementsByTagName('td')[colIndex-1].onclick();
	this.clearSelectionClass();
	this.selectedColumn=null;
	
}
dyntable.prototype.removeRow=function()
{
	if(this.selectionMode!='row' || this.selectedRow==null) {alert('Silinecek satırı seçmemişsiniz.'); return false;}
	if(this.tby.getElementsByTagName('tr').length<2)  {alert('Tabloda en az bir satır olmalıdır. Silme işlemi yapılamaz.'); return false;}
	if(!confirm('Seçili satır silinsin mi?')) return false;
	
	var rIndex=this.selectedRow.rowIndex;
	this.selectedRow.parentNode.removeChild(this.selectedRow);
	if(this.tby.getElementsByTagName('tr')[rIndex-1]) return this.tby.getElementsByTagName('tr')[rIndex-1].getElementsByTagName('td')[0].onclick(); 
	this.clearSelectionClass();
	this.selectedRow=null;
}
dyntable.prototype.clearSelectionClass=function()
{
	// düzenleme işlemi varsa kapatılıyor.
	if(this.editingRow!=null) this.toTextMode();
	
	// satırların sınıfları temizleniyor.
	var trs=this.tby.getElementsByTagName('tr')
	for(var i=0;i<trs.length;i++) removeClassName(trs[i],'selected');
	
	// hücrelerin sınıfları temizleniyor.
	var tds=this.tby.getElementsByTagName('td')
	for(var i=0;i<tds.length;i++) removeClassName(tds[i],'selected');
	this.updateSCode();
	tbar_update(this);
}
dyntable.prototype.changeSelMode=function()
{
	if(this.selectionMode=='row') this.selectionMode='column'; else this.selectionMode='row'; 
	this.clearSelectionClass();
	this.selectedRow=null;
	this.selectedColumn=null;
}
dyntable.prototype.toEditMode=function()
{
	if(this.editingRow==null) {alert('Düzenlenecek satırı seçmemişsiniz.'); return false;}
	this.editingRow.className+=' editing';
	var tds=this.editingRow.getElementsByTagName('td');
	for(var i=0;i<tds.length;i++) 
	{
		tds[i].innerHTML='<input type="text" name="td'+i+'" value="'+tds[i].innerHTML+'"  />';
	}
	this.updateSCode();
}
dyntable.prototype.toTextMode=function()
{
	var tds=this.editingRow.getElementsByTagName('td');
	for(var i=0;i<tds.length;i++) 
	{
		tds[i].innerHTML=tds[i].getElementsByTagName('input')[0].value;
	}
	removeClassName(this.editingRow,'editing');
	this.editingRow=null;
}
dyntable.prototype.makeHeader=function()
{
	if(this.selectionMode=='row' && this.selectedRow)
	{
		if(this.selectedRow.className.indexOf('header')==-1) this.selectedRow.className+=' header'; else removeClassName(this.selectedRow,'header'); 
	}
	else
	{
		var colIndex=this.selectedColumn.cellIndex;
		var trs=this.tby.getElementsByTagName('tr');
		for(var i=0;i<trs.length;i++)
		{
			var t=trs[i].getElementsByTagName('td')[colIndex];
			if(t.className.indexOf('header')==-1) t.className+=' header'; else removeClassName(t,'header'); 		
		}
	}
	this.updateSCode();
}
dyntable.prototype.updateSCode=function()
{
	if(!this.codeContainer) return false;
	this.codeContainer.value=this.tbl.innerHTML;
}
