var divInEdit = null;
var tdInEdit  = null;
var idInEdit  = -1;
var oldContent= null;


var cp = new cpaint();
cp.set_transfer_mode('post');
cp.set_response_type('text');

function commDblClicked(tdObj, id, ltype)
{
	if (idInEdit > 0 || idInEdit == id )
		return;
	else
		initEditation(tdObj, id, ltype);
}

function initEditation(tdObj, id, ltype)
{
	divInEdit = document.getElementById('comm' + id);
	tdInEdit  = tdObj;
	oldContent= divInEdit.innerHTML.replace(/\\n/ig, '<br>');
	idInEdit  = id;
	if(ltype == 'reply')
		clabel = 'Yanıt';
	else	
		clabel = 'Yorum';
		
	comSubject   = document.getElementById('commSubject' + idInEdit).innerHTML;
	comBody		 = divInEdit.getElementsByTagName('BLOCKQUOTE')[0].innerHTML.replace(/<br>/ig, "\n");
	comApproved  = parseInt(tdObj.getAttribute("approved"));

	if (divInEdit.style.display != 'block')
		hideOrShow(id);
	// Yeni içeriği yaz
	divInEdit.innerHTML =
		'<div style="width:400px;padding:10px;text-align:left" align=center>' +
		'<b>Konu : </b> <input class=text type=text name=commSbjEdit id=commSbjEdit size=60 value="' + comSubject + '"> <br>' +
		'<b>'+clabel+':</b><br><textarea class=text rows=10 cols=70 name=commBdyEdit id=commBdyEdit >' + comBody + '</textarea><br>' +
		'<input type=checkbox value="1" ' + (comApproved > 0 ? 'checked' : '') + ' id=commAppEdit> <label for=commAppEdit>'+clabel+' onaylansın</label><br>' +
		'<input type=button class=button value="Vazgeç" onClick="cancelEditation()">&nbsp;' +
		'<input type=button class=button value="Güncelle" onClick="finishEditation()">' +
		'</div>';
}

function cancelEditation()
{
	divInEdit.innerHTML = oldContent;
	// Yeni bir güncellemeye izin ver
	idInEdit = -1;
	divInEdit = null;
	tdInEdit  = null;
	oldContent = null;
}
function response(result)
{
	if (result != '1')
		alert(result);
}

function finishEditation()
{
	var subject = document.getElementById('commSbjEdit').value;
	var body    = document.getElementById('commBdyEdit').value;
	var approved= (document.getElementById('commAppEdit').checked ? 1 : 0 );

	// Veritabanına güncelleme bilgilerini gönder
	var fileToUse = 'ajax_tier.php';
	var method = 'updateComment';
	
	if (window.location.href.match(/action\/dosya/))
		method = 'updateFileComment';
	
	cp.call(fileToUse, method,	response, idInEdit, subject, body, approved );

	// Yeni halini göster
	tdInEdit.setAttribute("approved", "" + approved);
	if (approved > 0 )
		tdInEdit.style.backgroundColor = 'white';
	else
		tdInEdit.style.backgroundColor = 'bisque';

	document.getElementById('commSubject' + idInEdit).innerHTML = subject;
	divInEdit.innerHTML = '<blockquote>' + body.replace(/\n/ig, '<br>')	 + '</blockquote>';
	// Yeni bir güncellemeye izin ver
	idInEdit = -1;
	divInEdit = null;
	oldContent = null;
}
