﻿var Cms = {
    RegisterComments: function () {

        var pc = $('#postComment');
        pc.submit(function () {
            var nameInp = $('#Name');
            var commentInp = $('#Comment');
            var websiteInp = $('#Website');
            var urlInp = $('#Url');
            var validation = $('#Validation');
            validation.empty();
            var isValid = true;
            if (nameInp.val().length == 0) {
                isValid = false;
                validation.html('<li>Please add your name.</li>');
            } else if (commentInp.val().length == 0) {
                isValid = false;
                validation.html('<li>Please add a comment.</li>');
            }
            if (!isValid) {
                validation.show();
                return false;
            } else {
                validation.hide();
                $('#postComment :input').each(function () {
                    $(this).attr({ disabled: true });
                });
                $('#postComment input:submit').val("Please wait");

                $.ajax({
                    type: "POST",
                    url: '../comment',
                    data: { name: nameInp.val(),
                        url: urlInp.val(),
                        website: websiteInp.val(),
                        comment: commentInp.val()
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        $('#postComment :input').each(function () {
                            removeAttr('disabled');
                        });
                        $('#postComment button').val("Submit");
                        validation.html('<li>There was a problem contacting the server.</li>');
                        validation.show();
                    },
                    success: function (data) {
                        pc.animate({
                            opacity: 0,
                            height: '50px'
                        }, 500, function () {
                            pc.replaceWith("<p>Thanks! Comments are moderated, and will appear within a few hours.</p>");
                        });
                    }
                });
                return false;
            }
        });
    }
};
