﻿//ArticleViewer.js

$(function()
{
    SetDownloadLinksBehavior();
    SetNewCommentContainerBehavior();
    InitializeSyntaxHighlighter();
});

function SetDownloadLinksBehavior()
{
    $('a.ArticleFileHref').click(function()
    {
        var articleFileUid = parseInt($(this).attr('uid'));
        var articleFilePath = $(this).attr('path');

        if (__currentUserName == '')
        {
            ShowMessageBox('You need to login before downloading files.\r\nClick <a href="/Login.aspx">here</a> to login now.', 'Download');
            return;
        }

        //save download-count
        var jsonArguments = { 'ArticleFileUid': articleFileUid, 'ArticleFilePath': articleFilePath };
        var json = BuildCallbackJson('Home', 'NewDownloadEntry', jsonArguments);
        var jsonText = JSON.stringify(json);
        CallServer(jsonText);
    });
}

function SetNewCommentContainerBehavior()
{
    $('.NewCommentTable tr td:first').css({ 'width': '80px' });

    $('#PostCommentHref').click(function()
    {
        var isAnonymous = false;
        var anonymousName = '';
        var anonymousEmail = '';
        var commentContent = $('#CommentContentTextBox').val();

        if (__currentUserName == '')
        {
            isAnonymous = true;
            anonymousName = $('#CommentAnonymousNameTextBox').val();
            anonymousEmail = $('#CommentAnonymousEmailTextBox').val();

            //check anonymous-name
            if (anonymousName.length == 0)
            {
                ShowMessageBox('Enter your Name.', 'Comment');
                $('#CommentAnonymousNameTextBox').focus();
                return;
            }

            //check anonymous-email
            if (anonymousEmail.length == 0)
            {
                ShowMessageBox('Enter your Email.', 'Comment');
                $('#CommentAnonymousEmailTextBox').focus();
                return;
            }

            if (!IsValidEmail(anonymousEmail))
            {
                ShowMessageBox('Invalid Email Format.', 'Comment');
                $('#CommentAnonymousEmailTextBox').focus();
                return;
            }
        }

        //check comment-content
        if (commentContent.length == 0)
        {
            ShowMessageBox('Enter the Comment.', 'Comment');
            $('#CommentContentTextBox').focus();
            return;
        }

        var jsonArguments = { 'ArticleUid': __currentArticleUid, 'CurrentUserName': __currentUserName, 'IsAnonymousComment': isAnonymous, 'AnonymousName': anonymousName, 'AnonymousEmail': anonymousEmail, 'CommentContent': commentContent };
        var json = BuildCallbackJson('Home', 'NewComment', jsonArguments);
        var jsonText = JSON.stringify(json);
        CallServer(jsonText);
    });
}

