
// global request and XML document objects
var commentsIsIE=false;
var commentsxmlhttp=false;
var commentscommentxmlhttp=false;
var commentsflagxmlhttp=false;
var commentsremovexmlhttp=false;

var commentsCount = '0';
var allComments;
var userCanMod = false;

if (window.ActiveXObject) {
 commentsIsIE = true;
}

function trim(str)
{
	return str.replace(/^\s*/, "").replace(/\s*$/, "");
}

function newCommentsXMLHttpRequest() {
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
    try {
        commentsxmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            commentsxmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            commentsxmlhttp = false;
        }
    }
@end @*/
    if (!commentsxmlhttp && typeof XMLHttpRequest!='undefined') {
        commentsxmlhttp = new XMLHttpRequest();
    }
    if (commentsxmlhttp) {
        commentsxmlhttp.onreadystatechange = processCommentsInitStateChange;
    }
}

function newSubmitCommentXMLHttpRequest() {
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
    try {
        commentscommentxmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            commentscommentxmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            commentscommentxmlhttp = false;
        }
    }
@end @*/
    if (!commentscommentxmlhttp && typeof XMLHttpRequest!='undefined') {
        commentscommentxmlhttp = new XMLHttpRequest();
    }
    if (commentscommentxmlhttp) {
        commentscommentxmlhttp.onreadystatechange = processCommentStateChange;
    }
}

function newFlagCommentXMLHttpRequest() {
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
    try {
        commentsflagxmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            commentsflagxmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            commentsflagxmlhttp = false;
        }
    }
@end @*/
    if (!commentsflagxmlhttp && typeof XMLHttpRequest!='undefined') {
        commentsflagxmlhttp = new XMLHttpRequest();
    }
    if (commentsflagxmlhttp) {
        commentsflagxmlhttp.onreadystatechange = processFlagStateChange;
    }
}

function newRemoveCommentXMLHttpRequest() {
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
    try {
        commentsremovexmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            commentsremovexmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            commentsremovexmlhttp = false;
        }
    }
@end @*/
    if (!commentsremovexmlhttp && typeof XMLHttpRequest!='undefined') {
        commentsremovexmlhttp = new XMLHttpRequest();
    }
    if (commentsremovexmlhttp) {
        commentsremovexmlhttp.onreadystatechange = processRemoveStateChange;
    }
}

function processCommentsInitStateChange() {
    switch (commentsxmlhttp.readyState) {
        case 2:
            break;
        case 3:
            break;
        case 4:
            // only if "OK"
            if (commentsxmlhttp.status == 0 || commentsxmlhttp.status == 200) {
                var doc = commentsxmlhttp.responseXML;
                if (doc) {
                    var commentsElement = doc.getElementsByTagName("comments")[0];
		            commentsCount = commentsElement.getAttribute("count");

			        document.getElementById("commentcount").innerHTML = commentsCount;

			        allComments = doc.getElementsByTagName("comment");
		        }
            }
            break;
    }
}

function processCommentStateChange() {
    switch (commentscommentxmlhttp.readyState) {
        case 2:
            break;
        case 3:
            break;
        case 4:
            // only if "OK"
            if (commentscommentxmlhttp.status == 0 || commentscommentxmlhttp.status == 200) {
				var doc = commentscommentxmlhttp.responseXML;
				if (doc) {
					var commentsElement = doc.getElementsByTagName("comments")[0];
					commentsCount = commentsElement.getAttribute("count");
					captchaCorrect = commentsElement.getAttribute("new-post-success");

					if (captchaCorrect == 'true')
					{
						document.getElementById("commentBody").value = '';
					}
					else
					{
						document.getElementById("commenterror").innerHTML = 'Your security code entry was incorrect';
					}

					document.getElementById("captchaGuess").value = '';
					document.getElementById("captchaImage").src = '/webapps/captcha?' + (new Date()).getTime();

					document.getElementById("commentcount").innerHTML = commentsCount;
					allComments = doc.getElementsByTagName("comment");
					showComments(commentsElement.getAttribute("content"));
				}
            }
            break;
    }
}

function processFlagStateChange() {
    switch (commentsflagxmlhttp.readyState) {
        case 2:
            break;
        case 3:
            break;
        case 4:
            // only if "OK"
            if (commentsflagxmlhttp.status == 0 || commentsflagxmlhttp.status == 200) {
		        var doc = commentsflagxmlhttp.responseXML;
		        if (doc) {
                    var commentsElement = doc.getElementsByTagName("comments")[0];
		            commentsCount = commentsElement.getAttribute("count");

			        document.getElementById("commentcount").innerHTML = commentsCount;

			        allComments = doc.getElementsByTagName("comment");
			        showComments(commentsElement.getAttribute("content"));
		        }
            }
            break;
    }
}

function processRemoveStateChange() {
    switch (commentsremovexmlhttp.readyState) {
        case 2:
            break;
        case 3:
            break;
        case 4:
            // only if "OK"
            if (commentsremovexmlhttp.status == 0 || commentsremovexmlhttp.status == 200) {
		        var doc = commentsremovexmlhttp.responseXML;
		        if (doc) {
                    var commentsElement = doc.getElementsByTagName("comments")[0];
		            commentsCount = commentsElement.getAttribute("count");

			        document.getElementById("commentcount").innerHTML = commentsCount;

			        allComments = doc.getElementsByTagName("comment");
			        showComments(commentsElement.getAttribute("content"));
		        }
            }
            break;
    }
}

function initComments(contentID, userStatus) {
    newCommentsXMLHttpRequest();
    var path = "/webapps/comments.action?contentID=" + contentID;
    commentsxmlhttp.open("GET", path, true);
    if (commentsIsIE) {
        commentsxmlhttp.send();
    } else {
        commentsxmlhttp.send(null);
    }

    userCanMod = userStatus;
}

function submitComment(contentID, captchaGuess, comment) {

	// clear the error, in case there was already one
	document.getElementById("commenterror").innerHTML = '';

	if (trim(comment) == '')
	{
		document.getElementById("commenterror").innerHTML = 'Enter a comment';
	}
	else
	{
		if (trim(captchaGuess) == '')
		{
			document.getElementById("commenterror").innerHTML = 'Please enter the security code on the right into the form below it';
		}
		else
		{
			newSubmitCommentXMLHttpRequest();
			var path = "/webapps/comments-comment.action?contentID=" + contentID + "&captchaGuess=" + encodeURI(captchaGuess) + "&comment=" + encodeURIComponent(comment);
			commentscommentxmlhttp.open("GET", path, true);
			if (commentsIsIE) {
				commentscommentxmlhttp.send();
			} else {
				commentscommentxmlhttp.send(null);
			}
		}
	}
}

function flagComment(contentID, commentId) {

	newFlagCommentXMLHttpRequest();
	var path = "/webapps/comments-flagcomment.action?contentID=" + contentID + "&commentId=" + commentId;
	commentsflagxmlhttp.open("GET", path, true);
	if (commentsIsIE) {
		commentsflagxmlhttp.send();
	} else {
		commentsflagxmlhttp.send(null);
	}
}

function removeComment(contentID, commentId) {

	newRemoveCommentXMLHttpRequest();
	var path = "/webapps/comments-removecomment.action?contentID=" + contentID + "&commentId=" + commentId;
	commentsremovexmlhttp.open("GET", path, true);
	if (commentsIsIE) {
		commentsremovexmlhttp.send();
	} else {
		commentsremovexmlhttp.send(null);
	}
}

function showComments(contentID) {

	var commentsBlock = "";
	for (var i=0; i<allComments.length; i++)
	{
		var comment = allComments[i];

		var flagImage = '<a href="javascript:flagComment(\'' + contentID + '\', ' + comment.getAttribute("id") + ');"><img src="/img/comments/flag.jpg" border="0" /></a>';
		if (comment.getAttribute("user-flagged") == 'true')
		{
			flagImage = '<img src="/img/comments/flagged.jpg" border="0" />';
		}

		var delImage = '';
		var flagText = '&nbsp;'
		if (userCanMod)
		{
			delImage = '<a href="javascript:removeComment(\'' + contentID + '\', ' + comment.getAttribute("id") + ');"><img src="/img/comments/delete.jpg" border="0" /></a>';
			flagText = '' + comment.getAttribute("flags") + ' flag';
			if (comment.getAttribute("flags") != '1') flagText += 's';
		}

		if (i%2 == 0)
		{
			commentsBlock += '<div class="commentRow commentShaded">';
		} else {
			commentsBlock += '<div class="commentRow">';
		}
		commentsBlock += '	<div class="commentTools"><div align="right"><table border="0" cellpadding="0" cellspacing="0"><tr><td valign="top">' + flagText + '</td><td valign="top" class="commentToolsBtns"> ' + flagImage + delImage + '</td></tr></table></div></div>';
		commentsBlock += '	<div class="commentContent">';
		commentsBlock += '		<div class="commentHeader"><span class="commentLabel">Posted by:&nbsp;&nbsp;</span><span class="commentDetail">' + comment.getAttribute("screenName") + '</span><span class="commentLabel">&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;</span><span class="commentDetail">' + comment.getAttribute("postDate") + '</span></div>';
		if (comment.childNodes.length > 1) {
			commentsBlock += comment.childNodes[1].nodeValue;
		} else {
			commentsBlock += comment.firstChild.nodeValue;
		}
		commentsBlock += '	</div>';
		commentsBlock += '</div>';
	}

	document.getElementById("commentsBlock").innerHTML = commentsBlock;
	document.getElementById("commentsForm").style.display = "block";
}
