﻿$(function () {

    addLoginStateFunction(setupComments);

    /* Handle the sign-in WS call */
    $('#createComment #blogCommentSignIn a[href="#signin_screen"]').click(doBlogCommentSignIn);

    $('#createComment a[id$=SubmitBlogComment]').click(doCreateBlogComment);

});

function doBlogCommentSignIn()
{
    //initLoginDialog(); //this is called by global #sign_in handler
    postLoginFunction = blogCommentPostLoginFunction;
    return false;
}

function blogCommentPostLoginFunction()
{
    $('#createComment .anonymousFields').css('display', 'none');
}

function doCreateBlogComment() {
    clearFormErrors($('#createComment'));
    var requestData = {
            blogItemId: blogItemId,
            anonymousUserName: $('#createComment #anonymousUserName').val(),
            anonymousUserEmail: $('#createComment #anonymousUserEmail').val(),
            ageVerificationCheckbox: $('#createComment #ageVerificationCheckbox:checked').val(),
            commentText: $('#createComment #CommentText').val(),
            recaptcha_response_field: $('#createComment #recaptcha_response_field').val(),
            recaptcha_challenge_field: $('#createComment #recaptcha_challenge_field').val()
        };

    onSuccess = CreateBlogCommentSuccess;
    CreateBlogComment(requestData);

    return false;
}

function CreateBlogComment(requestData) {
    AjaxCall(requestData);
}

function CreateBlogCommentReply(reply, requestData, callingFunction)
{
    if (reply.Success != "true") {

        if ( CaptchaError(reply.Errors) )
            Recaptcha.reload();

        if (IsAuthError(reply)) {
            $('a#login').trigger('click');
            postLoginFunction = partial(callingFunction, requestData);
        } else {
            displayErrors(reply.Errors, $('#createComment'));
        }

    } else {
        googleReportEvent('blog', 'comment', this.location.pathname);
        
        if (onSuccess != null) {
            onSuccess();
            onSuccess = null;
        }
    }
}

function CreateBlogCommentSuccess() {
    $('#createComment #commentForm').css('display', 'none');
    $('#blogCommentSubmitted').css('display', 'block');
}
