﻿function deletePostComment(id) {
    if (confirm('Are you sure you wish to delete this comment?')) {
        $.getJSON("/Blog/DeletePostComment", { commentId: id }, function(data) {
            if (data == true) {
                $('#comment-' + id).fadeOut('slow');
            }
        });
    }

    return false;
}

function addPostComment(id) {
    var mailFilter = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i;
    var urlFilter = /^(?:https?|s?ftp|telnet|ssh|scp):\/\/(?:(?:[\w]+:)?\w+@)?(?:(?:(?:[\w-]+\.)*\w[\w-]{0,66}\.(?:[a-z]{2,6})(?:\.[a-z]{2})?)|(?:(?:25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.)(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})))(?:\:\d{1,5})?(?:\/(~[\w-_.])?)?(?:(?:\/[\w-_.]*)*)?\??(?:(?:[\w-_.]+\=[\w-_.]+&?)*)?$/i;
    
    if ($('#author').val().length < 2) {
        alert('Please, specify your name.');
        $('#author').focus();
        return false;
    }
    else if ($('#email').val() == '') {
        alert('Please, specify your e-mail.');
        $('#email').focus();
        return false;
    }
    else if (!mailFilter.test($('#email').val())) {
        alert('Specified e-mail is invalid.');
        $('#email').focus();
        return false;
    }
    else if ($('#website').val() != '' && !urlFilter.test($('#website').val())) {
        alert('Specified web site url is invalid.');
        $('#website').focus();
        return false;
    }
    else if ($('#text').val() == '') {
        alert('Please, input your comment.');
        $('#text').focus();
        return false;
    }

    $.getJSON("/Blog/AddComment", { postId: id, author: $('#author').val(), email: $('#email').val(), website: $('#website').val(), text: $('#text').val() }, function(data) {
        var postComment = document.createElement('div');
        $(postComment).addClass('post-comment');
        $(postComment).hide();
        postComment.id = 'comment-' + data.commentId;

        var postCommentAuthor = document.createElement('span');
        $(postCommentAuthor).addClass('post-comment-author');
        $(postCommentAuthor).html($('#author').val());
        $(postCommentAuthor).appendTo(postComment);

        var postCommentDate = document.createElement('span');
        $(postCommentDate).addClass('post-comment-date');
        $(postCommentDate).html(data.createdDate);
        $(postCommentDate).appendTo(postComment);

        var postCommentText = document.createElement('div');
        $(postCommentText).addClass('post-comment-text');
        $(postCommentText).html($('#text').val());
        $(postCommentText).appendTo(postComment);

        $(postComment).appendTo('#comments');
        $(postComment).fadeIn('slow');

        $('#author').val('');
        $('#email').val('');
        $('#website').val('');
        $('#text').val('');
    });

    return false;
}