function showCaptcha(id)
{
	var captcha = $('#'+id);
	if (captcha.css('display') == 'none') captcha.css('display', '');
}
var inedit = false;
function editcomment(rid)
{
	if (/^\d+?$/.test(rid) && inedit == false)
	{
		var comment = $('#comment_content_'+rid);
		inedit = true;
		editAjax(comment, rid);
	}
}
function editAjax(obj, rid)
{
	var formercomment = obj.html();
	obj.html(edit_comment_loading);
	$.ajax({
		type: 'GET',
		url: pb_url+'ajax.php',
		dataType: 'json',
		data: 'type=editcomment' + '&rid=' + rid  + '&verifyhash=' + verifyhash + '&random=' + Math.random(),
		success: function(data)
		{
			if (data[0]=='1')
			{
				formercomment = encodeURIComponent(formercomment);
	            obj.html('<textarea id="editcomment_'+rid+'" name="editcomment_'+rid+'" style="width:300px;height:100px;">'+data[1]+'</textarea>&nbsp;<input type="button" name="editconfig_'+rid+'" id="editconfig_'+rid+'" onclick="sendcommentedit(\''+formercomment+'\','+rid+')" value="'+comment_submit+'"/>&nbsp;<input type="button" name="caceledit_'+rid+'" id="caceledit_'+rid+'" onclick="cancleedit(\''+formercomment+'\','+rid+')" value="'+comment_cancel+'" />');
			}
			else
			{
				alert(data[1]);
				obj.html(formercomment);
				inedit = false;
			}
		}
	});
	return;
}
function cancleedit(obj,rid)
{
	$('#comment_content_'+rid).html(decodeURIComponent(obj));
	inedit = false;
}
function sendcommentedit(obj,rid)
{
	var editcomment = $('#editcomment_'+rid).val();
	$('#editconfig_'+rid).attr('disabled', 'disabled');
	if (editcomment.length < comment_minc || editcomment.length > comment_maxc)
	{
		alert(comment_length_error);
		$('#editconfig_'+rid).attr('disabled', '');
		$('#editcomment_'+rid).focus();
		return;
	}
	$.ajax({
		type: 'POST',
		url: pb_url+'ajax.php',
		dataType: 'json',
		data: 'type=sendeditcomment&comment='+encodeURIComponent(editcomment)+'&rid=' + rid + '&verifyhash=' + verifyhash + '&random=' + Math.random(),
		error: function(var1,var2,var3)
		{
			$('#comment_content_'+rid).hide().html(decodeURIComponent(obj)).show('slow');
		},
		success: function(data)
		{
			alert(data[1]);
			if (data[0] == '1')
			{
	            $('#comment_content_'+rid).hide().html(data[2]).show('slow');
			}
			else
			{
				$('#comment_content_'+rid).hide().html(decodeURIComponent(obj)).show('slow');
			}
		}
	});
	inedit = false;
	return;
}
function recomment(rid, author)
{
	var reply_body = $('#comment_reply_body');
	var quote = $.trim($('#comment_content_' + rid).text());
	var add = '';
	if (quote.length > 50)
	{
		add = "...";
	}
	quote = quote.substr(0, 50) + add;
	reply_body.val('[quote=' + author + ']'+quote+'[/quote]');
	reply_body[0].focus();
	return;
}
function comment(tid)
{
	var sendcomment = $('#send_comment');
	var comment = $('#comment_reply_body').val();
	var ctip = $('#c_tip');
	var captcha = $('#captcha').val();
	sendcomment.attr('disabled', 'disabled');
	if (comment.length < comment_minc || comment.length > comment_maxc)
	{
		ctip.css('display', 'block').html(comment_length_error);
		sendcomment.attr('disabled', '');
		return;
	}
	commentAjax(ctip, 'type=comment&comment='+encodeURIComponent(comment)+'&tid='+tid+'&verifyhash=' + verifyhash + '&captcha=' + captcha);
}
function commentAjax(obj, param)
{
	obj.css('display', 'block').html(add_comment_loading);
	$.ajax({
		type: 'POST',
		url: pb_url+'ajax.php',
		dataType: 'json',
		data: param + '&random=' + Math.random(),
		error: function(var1,var2,var3)
		{
			obj.html(ajax_response_failed);
		},
		success: function(data)
		{
			if (data[0] == '1-1')
			{
				obj.html(data[1]);
	            $('#nocomment').remove();
	            $('#coment_reply').append(data[2]);
	            $('#comment_reply_body').val('');
	            obj.fadeOut(2000,function(){obj.css({display:'none'})});
			}
			else if (data[0]=='1-0')
			{
				obj.html(data[1]);
				$('#comment_reply_body').val('');
				obj.fadeOut(2000,function(){obj.css({display:'none'})});
			}
			else
			{
				obj.html(data[1]);
			}
			$('#captcha').val('');
			$('#checkcode').attr('src', pb_url+'checkcode.php?' + '&random=' + Math.random());
			$('#send_comment').attr('disabled', '');
		}
	});
	return;
}
function emoticon(text) {
	var txtarea = document.getElementById('comment_reply_body');
	text = '[emote]' + text + '[/emote]';
	if (txtarea.createTextRange && txtarea.caretPos) {
		var caretPos = txtarea.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == '[emote]' ? text + '[/emote]' : text;
		txtarea.focus();
	} else {
		txtarea.value  += text;
		txtarea.focus();
	}
}
function storeCaret(textEl) {
	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}