﻿// JScript File

/****************************** cookie code ***********************************************/

var articleCommentID;

function GetArticleComments(contextObj) 
{
	AjaxServices.GetArticleComments(articleCommentID, contextObj.count, 
	    GetArticlesComments_Success, ArticleComments_Failure, contextObj);
}

function GetArticlesComments_Success(result, contextObj) 
{
	if (result == null) {
		return;
	}
	
    var div = $get(contextObj.baseDivID);
	//TODO: set the proper innerHTML of the div
	var html = "";
    $get('commentsCount').innerHTML = '(Total ' + result.length + ' comments)';
	for (var i=0; i<result.length; i++) {
	    var cssclass = 'WhiteBox';
	    if (i % 2 == 0)
	    {
	        cssclass = "TransparentBox"
	    }
    	html = html + '<div class="'+ cssclass + '">';
    	html = html + '<img style="vertical-align:text-top;float:left;margin-right:10px" id="UserPicture' + i + '" />'
    	html = html + '<span class="TitleTextSmall" id="UserComment' + i + '"></span><br>';
    	html = html + 'posted ' + result[i].commentDate.format("h:MM tt, MMMM d, yyyy");
    	html = html + '<p>' + result[i].commentText + '</p>';
    	if (isAdmin == 'True')
    	{
    	    html = html + '<a href="javascript:RemoveArticleComment({commentID: ' + result[i].commentID + ', baseDivID: \'divArticleComments\', count: -1 });">Remove comment</a>';
    	}
    	html = html + '</div>';
    	AjaxServices.GetUserInfo(result[i].memberID, GetUserInfo_Success, ArticleComments_Failure, {idx: i});
	}
	div.innerHTML = html;
}

function GetUserInfo_Success(result, contectObj)
{
    if (result == null) 
    {
        return;
    }
    $get('UserComment' + contectObj.idx).innerHTML = result.firstName + ' ' + result.lastName; 
    $get('UserPicture' + contectObj.idx).src = DefaultUserPicture;
}

function ArticleComments_Failure(error) {
    alert("Stack Trace: " + error.get_stackTrace() + "/r/n" +
          "Error: " + error.get_message() + "/r/n" +
          "Status Code: " + error.get_statusCode() + "/r/n" +
          "Exception Type: " + error.get_exceptionType() + "/r/n" +
          "Timed Out: " + error.get_timedOut());
	//do nothing here.
}

var ARTICLE_COMMENTED_COOKIE = 'articleCommented';

function AddArticleComment(contextObj) 
{   
    var hasVoted = ed_getCookie(ARTICLE_COMMENTED_COOKIE + articleCommentID);
    if (hasVoted == '') {
	    AjaxServices.AddArticleComment(articleCommentID, $get('CommentToArticle').value, AddArticleComment_Success, ArticleComments_Failure, contextObj);
	} else {
	    alert('You have already commented this article.');
	}
}

function AddArticleComment_Success(result, contextObj)
{
    if (result == null)
    {
        return;
    }
	ed_setCookie(ARTICLE_COMMENTED_COOKIE + result.articleID, 'rated', null);
    $get('CommentToArticle').value = '';
	GetArticleComments(contextObj);
}

function RemoveArticleComment(contextObj) 
{   
    AjaxServices.RemoveArticleComment(contextObj.commentID, RemoveArticleComment_Success, ArticleComments_Failure, contextObj);
}

function RemoveArticleComment_Success(result, contextObj)
{
    $get('CommentToArticle').value = '';
	GetArticleComments(contextObj);
}
