$(document).ready(function() {
	$("#frmBlogComment").validate({
		errorClass		: 'errorMessage',
		errorElement	: 'span',
		onfocusout		: false,
		onkeyup			: false,
		rules: {
			commentContent:{
				validateBlogComment 	: true,
				validateBlogComment2	: true
			},
			txtCaptCha: {
				required			: true,
				validateCaptcha		: true
			}
		},
		messages: {
			commentContent:{
				validateBlogComment	: com_msg_requiredcomment,
				validateBlogComment2: com_msg_invalidcomment
			},
			txtCaptCha: {
				required		: com_msg_requiredcaptcha,
				validateCaptcha	: com_msg_invalidcaptcha
			}
		},
	
		submitHandler: function(form) {			
			var username = $('#username').val();	
			var page = $('#page').val();	
			var pcomid = $('#comid').val();	
			var pcontent = $('#commentContent').val();
			var purl = HOST_BLOG+'blog/addcomment?id='+pcomid;
			var pdata = {
				id: pcomid,				
				commentContent: pcontent
			}
			$.ajax({
				cache	: false,
				async	: false,
				data	: pdata,
				url		: purl,
				dataType: 'jsonp',
				jsonp	: 'callback',
				success	: function(response) {						
					if (response['error'] == 0){
						$('#commentContent').val('');												
						showUIMessage(response['msg']);
						showcomment(username, pcomid, page);						
					} else {
						alert(response['msg']);
					}
				}
			});
			return false;
		}
		
	});
	$.validator.addMethod('validateBlogComment', function() {
		var strComment = $('#commentContent').val();
		if(strComment == '') {
			$('#commentContent').val('');
			$('#boxshow').css('display', 'none');
			$('#boxhidden').css('display', 'block');
			document.getElementById('commentContent').focus();
			return false;
		}
		return true;
	});
	$.validator.addMethod('validateBlogComment2', function() {
		return validateComment($('#commentContent').val(), 'blog');
	});	
	$.validator.addMethod('validateCaptcha', function() {
		return validateCaptcha($('#txtCaptCha').val(), 'blog');
	});
});

showcomment = function(username, id, page) {
	$.get(
		HOST_BLOG + 'ajax/pagingcomment?isajax=1&type=comment&username='+username+'&id='+id+'&page='+page,{}, 
		function(response) {
			$("#spanComment").html(response); 
			refreshCaptcha('binhluanBlog','blog','');
			$('#txtCaptCha').val('');
			return true; 
		}
	);
}

uisendneighbour = function(pid) {
	$.getJSON(
		HOST_BLOG + "ajax/uisendneighbour",{id:pid},
		function (response) {
			if (response['error']==0) {
				$.blockUI({
					message: response['msg'],
					css: {
						background: 'none',
						border: 'none',
						top:  ($(window).height() - 550)/2 + 'px',
						left: ($(window).width() - 600)/2 + 'px',
						cursor: 'pointer'
					}
				});
			} else {
				alert(response['msg']);
			}
		}
	)
}

uiactflags = function(blogid, flag) {
	if(flag==1){
		var msg = 'Bạn muốn dừng bình luận!';
	}else{
		var msg = 'Bạn muốn kích hoạt chức năng bình luận!';
	}
	if(confirm(msg)==true){
		$.getJSON(
			HOST_BLOG + "ajax/actflags",{id:blogid, flag:flag},
			function (response) {
				if (response['error']==0) {
					location.reload();
				}
			}
		)
	}else{
		return false;
	}
}

showbox = function(){
	$('#boxshow').css('display', 'none');
	$('#boxhidden').css('display', 'block');
	document.getElementById('commentContent').focus();
}

function nolike(){
	alert('Bạn không có quyền bình chọn!');
}
