﻿$(document).ready(function () {

    $('#btnSubmitComment').click(function (e) {
        e.preventDefault();

        var form, url, data, submission;

        form = $(this).parents('form:first');
        url = form.attr('action');
        data = form.serialize();

        $('#submitcommentstatus').hide();

        submission = new CommentSubmission();

        if (submission.isValid()) {

            var content = ['<div class="info"><p>', R.Common.BlogCommentSubmitting, '</p></div>'].join('');
            showSubmitCommentStatus(content, function () {

                $.post(
                    url,
                    form.serialize(),
                    function (d) {
                        finishedSubmitComment(d, submission);
                    }
                );

            });
        }
        else {
            showSubmitCommentInvalidForm(submission);
        }
    });

});

function finishedSubmitComment(arg, submission) {
    if (arg.Success) {
        showSubmitCommentProcessed();
    }
    else {
        showSubmitCommentFailed();
    }
}

function showSubmitCommentProcessed() {
    var content = ['<div class="success"><p>', R.Common.BlogCommentSubmitted, '</p></div>'].join('');

    $('#submitcommentform').fadeOut(500,
        showSubmitCommentStatus(content));
}

function showSubmitCommentFailed() {
    var content = ['<div class="error"><p>', R.Common.BlogCommentError, '</p></div>'].join('');
    showSubmitCommentStatus(content);
}

function showSubmitCommentInvalidForm(submission) {

    var html = [];

    html.push('<div class="warn"><p>');
    html.push(R.Common.RequiredFieldsError);
    html.push('</p><ul>');

    if (submission.name == '')
        html.push('<li>', R.Common.NameRequired, '</li>');

    if (submission.email == '')
        html.push('<li>', R.Common.EmailRequired, '</li>');
    else if (!submission.emailreg.test(submission.email))
        html.push('<li>', R.Common.EmailInvalid, '</li>');

    if (submission.body == '')
        html.push('<li>', R.Common.BlogCommentRequired, '</li>');

    html.push('</ul></div>');

    showSubmitCommentStatus(html.join(''));
}

function showSubmitCommentStatus(content, callback) {
    $('#submitcommentstatus').html(content);
    $('#submitcommentstatus').fadeIn(1000, callback);
}

function hideSubmitCommentStatus(callback) {
    if ($('#submitcommentstatus').css('display', 'none')) {
        $('#submitcommentstatus').fadeOut(1000, function () { $('#submitcommentstatus').html(''); callback() });
    }
    else {
        callback();
    }
}

function CommentSubmission() {
    this.emailreg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    this.postid = $('#hdnPostId').val().trim();
    this.name = $('#txtCommentName').val().trim();
    this.email = $('#txtCommentEmail').val().trim();
    this.body = $('#txtCommentBody').val().trim();
    this.homepage = $('#txtCommentHomepage').val().trim();

    this.isValid = function () {
        return (this.name != '' &&
        this.email != '' &&
        this.body != '' &&
        this.emailreg.test(this.email));
    }
}
