var lastCheckedRadio = null;
function VoteSelected(img)
{
	if (lastCheckedRadio != null)	
		findParent(lastCheckedRadio, 'TD').style.backgroundColor = '';

	lastCheckedRadio = img;
	findParent(lastCheckedRadio, 'TD').style.backgroundColor = 'yellow';
}

function SubmitCommentForm(btn)
{
	var f = document.getElementById('comment_form');
	if (f.subject.value == '' || f.body.value == '')
	{
		alert('"Başlık" veya "Yorumunuz" bölümleri doldurulmamış');
		return;
	}	
	
	var v = document.getElementsByName('vote');
	var selV = false;
	for (var i = 0; i < v.length; i++)
		if (v[i].checked) 
			selV = true;
			
	if (selV == false)
	{
		alert('Lütfen içerik için puan veriniz');
		return;
	}

	f.submit();
}

function VoteComment(id, score)
{
	if (! isMember)
	{
		alert('Sadece üyelerimiz yorumları puanlandırabilir.\n\nÜyemizseniz, lütfen giriş yapınız');
		return;
	}
	
	document.getElementById('DIV_VoteComment' + id).innerHTML = 
		'<img src="images/loading.gif" align=absmiddle> Kaydediliyor...';
	cp.call('comments2.php?mode=ajax', 'VoteComment', VoteResponse, id, score);
}

function VoteResponse(response)
{
	try
	{
		eval(response);
		
		var div = document.getElementById('DIV_VoteComment' + result.id);
		div.innerHTML = result.message;
		if (result.status == 0)
		{
			div.style.color = 'red';
			alert(result.message);
		}
		else
			div.style.color = 'green';
	}
	catch(Ex)
	{
		alert('Bir hata oluştu\n\nİşlem yapılamadı');
	}
}

function UpdatePagingLinks(divId)
{
	var links = document.getElementById(divId).getElementsByTagName('A');
	for(var i=0; i<links.length; i++)
	{
		var t = links[i].getAttribute('type');
		var p = parseInt(links[i].getAttribute('curr_page'));
		var m = parseInt(links[i].getAttribute('max_page'));
		if (t == 'back' && p == 1)
			links[i].disabled = true;
		else if (t == 'back' && p > 1)
			links[i].disabled = false;
		else if (t == 'forward' && p == m)
			links[i].disabled = true;
		else
			links[i].disabled = false;
	}
}

function ChangePage(link, inc, divId, type)
{
	if (type == 'reply') {		
		var panel = document.getElementById('DIV_Replies');
		var commentId = document.getElementById('num').getAttribute('val');
	}
	else 
		var panel = document.getElementById('DIV_CommentListPanel');
			
	if (link.disabled || panel.getAttribute('is_loading') == '1')
		return;

	// Sayfayı artır veya azalt
	var p = parseInt(link.getAttribute('curr_page')) + inc;	
	
	// İşlemi hatırlamak için panele yaz	
	panel.setAttribute('is_loading', '1');
	panel.setAttribute('new_page',  p);
	panel.innerHTML = '<img src="images/loading.gif" align=absmiddle> Yükleniyor ...';	
		
	// Sorgula
	if(type == 'reply')
		cp.call('comments2.php?mode=ajax', 'GetRepliesByPageAjax', RepliesPagingResult, commentId, p);
	else
		cp.call('comments2.php?mode=ajax', 'GetCommentsByPageAjax', PagingResult, contentId, p);	
}

function PagingResult(result)
{
	var panel = document.getElementById('DIV_CommentListPanel');
	panel.innerHTML = result;
	panel.setAttribute('is_loading', '0');
	
	// Panelleri güncelle
	var panelIds = ['DIV_CommentPaging', 'DIV_CommentPaging2'];
	for(var i=0; i<panelIds.length; i++)
	{
		var links = document.getElementById(panelIds[i]).getElementsByTagName('A');
		links[0].setAttribute('curr_page', panel.getAttribute('new_page'));
		links[1].setAttribute('curr_page', panel.getAttribute('new_page'));		
		
		UpdatePagingLinks(panelIds[i]);
	}
	
	// Odaklan
	panel.focus();
}

function RepliesPagingResult(result)
{
	var panel = document.getElementById('DIV_Replies');
	panel.innerHTML = result;
	panel.setAttribute('is_loading', '0');
	
	// Panelleri güncelle
	var panelIds = ['DIV_ReplyPaging', 'DIV_ReplyPaging2'];
	for(var i=0; i<panelIds.length; i++)
	{
		var links = document.getElementById(panelIds[i]).getElementsByTagName('A');
		links[0].setAttribute('curr_page', panel.getAttribute('new_page'));
		links[1].setAttribute('curr_page', panel.getAttribute('new_page'));		
		
		UpdatePagingLinks(panelIds[i]);
	}
	
	// Odaklan
	panel.focus();
}

function AddCommentReply(id)
{
	var article_id = document.getElementById('article_id').value;
	var subject = document.getElementById('replySubject'+id).value;
	var body = document.getElementById('replyBody'+id).value;

	if (subject == '' || body == '')
	{
		alert('"Başlık" veya "Yanıt" bölümleri doldurulmamış');
		return;
	}	

	cp.call('comments2.php?mode=ajax', 'AddCommentReply', AddCommentReplyResult, id, article_id, subject, body);	
}

function AddCommentReplyResult(response)
{
    if(response!='')
    {
        alert(response);   
    }
	else
		window.location.reload();
}

function AddReplyForm(id)
{
	document.getElementById('commentReplyForm'+id).style.display='block';
	var commentReplies = document.getElementById('commentReplies'+id);
	if(commentReplies != null) 
		commentReplies.style.display='none';
}

function DeleteReply(id)
{
	var answer = confirm('Bu yanıtı silmek istediğinize emin misiniz?'); 
	if(answer)
		cp.call('comments2.php?mode=ajax', 'DeleteCommentReply', DeleteCommentReplyResult, id);
}

function DeleteCommentReplyResult(response)
{
	if(response!='1')
    {
        alert(response);   
    }
	else
		window.location.reload();
}

function GetRepliesByAjax(commentId)
{
	comm_id = commentId;
	var commentReplyForm = document.getElementById('commentReplyForm' + commentId);
	var replies = document.getElementById('commentReplies' + commentId);
	
	if(commentReplyForm != null) 
		commentReplyForm.style.display='none';
			
	replies.style.display = 'block';
	replies.innerHTML = '<img src="images/loading.gif" align=absmiddle> Yükleniyor ...';	
		
	cp.call('comments2.php?mode=ajax', 'GetRepliesByPageAjax', GetRepliesByAjaxResult, commentId, 1);
}

function GetRepliesByAjaxResult(result)
{			
	var panel = document.getElementById('commentReplies' + comm_id);
	panel.innerHTML = result;
	panel.focus();
}

