
$(document).ready(function(){ 
	$(".comment_submit").hide();
 }); 


function processSubmitComment(recordID) 
{	
	if(validateCaptcha(recordID)) {
	
		commentId = $("#comment-id-" + recordID).val();
		commentTxt = $("#comment-value-" + recordID).val();
		$("#comment-block-" + recordID).load("comment.php", {'action' : 'update', 'record_id' : recordID, 'comment_value': commentTxt, 'comment_id': commentId}); 
	} 
	
	return false;	
}

function deleteComment(recordID, commentID) 
{
	if(confirm('Are you sure want to delete?')) {		
		$("#comment-block-" + recordID).load("comment.php", {'action' : 'delete', 'record_id' : recordID, 'comment_id' : commentID});
	} else {
		return false;
	}
}

function editComment(recordID, commentID)
{
	$("#comment-form-" + recordID).load("comment.php", {'action' : 'edit', 'record_id' : recordID, 'comment_id' : commentID});
}

function restrictComment(recordId) 
{
	textBoxId = 'comment-value-' + recordId;
	limitInfoId = 'comment-limitinfo-' + recordId;
	limitChars(textBoxId, 250, limitInfoId);
}

function limitChars(textid, limit, infodiv) {
	
	var text = $('#'+textid).val();	
	var textlength = text.length;
	if(textlength > limit) {
		$('#' + infodiv).html('You cannot write more then '+limit+' characters!');
		$('#'+textid).val(text.substr(0,limit));
		return false;
	} else {
		$('#' + infodiv).html('You have '+ (limit - textlength) +' characters left.');
		return true;
	}
}

function showCaptcha(recordID, publicKey)
{
	Recaptcha.destroy();
	Recaptcha.create(publicKey, "recaptcha-div-"+recordID, {
		theme: 'white',
		tabindex: 0,
		callback: Recaptcha.focus_response_field
	});
	  
	$(".comment_submit").hide();
	$(".recaptcha_required").show();
	$("#recaptcha_required_"+recordID).hide();
	$("#comment_submit_"+recordID).show();
}

function validateCaptcha(recordID)
{
    challengeField = $("input#recaptcha_challenge_field").val();
    responseField = $("input#recaptcha_response_field").val();
    var html = $.ajax({
    type: "POST",
    url: "ajax.recaptcha.php",
    data: "recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField,
    async: false
    }).responseText;

    if(html == "success")
    {
        return true;
    }
    else
    {
        $("#recaptcha-info-"+recordID).html("Your captcha is incorrect. Please try again");
        Recaptcha.reload();
        return false;
    }
}

/* Function for admin edit and delete */


function processAdminEdit(commentID)
{
	commentTxt = $("#comment-value-" + commentID).val();
	$("#pending_comments").load("ajax.approveComments.php", {'action' : 'update', 'comment_value': commentTxt, 'comment_id': commentID});
}

function adminEditComment(commentID)
{
	$("#comment_edit_form_" + commentID).load("ajax.approveComments.php", {'action' : 'edit', 'comment_id' : commentID});
}

function adminDeleteComment(commentID)
{
	if(confirm('Are you sure want to delete?')) {		
		$("#pending_comments").load("ajax.approveComments.php", {'action' : 'delete', 'comment_id' : commentID});
	} else {
		return false;
	}	
}

function checkAll(flag)
{
	if(flag) {
		$(".approve_checkbox").attr('checked', true);
	} else {
		$(".approve_checkbox").attr('checked', false);
	}
}
