// JavaScript Document
// BY: Cameron McGregor
// CREATED: October 05, 2006
// ABSTRACT: Contains javascript functions used with the comment forms in EIS Platform

//Comment action being committed
var commentAction = "";

//comment ID acted upon for alerts, etc.
var actCommentID = -1;

//timer object for auto-switching menus
var tmr;

//tcm uri for currently submitted request (comments)
var currentTcmUri = "";

//Whether or not the textarea for post comments has been focused
var focused = false;

//Update controls classes based on the base control set name.
//  Each form has a corresponding tab set (or span) that must be updated
function updateCtrlsClass(ctrlName, newClass)
{
	var ctrl = document.getElementById(ctrlName);
	if (ctrl)
	{
//		alert("ctrl found:" + ctrlName);
		ctrl.className = newClass;
	}
/*	//DEBUG
	else
		alert("ctrl NOT FOUND!!! " + ctrlName);
*/
}

//Show the "showForm" and hide all forms in the "hideForms" array
//  giving the appearance that the forms are overlayed
function showCommentForm(showForm, hideForms)
{
	//show the form
	updateCtrlsClass(showForm, "");

	//Set the selected tag (inactive)
	updateCtrlsClass(showForm + "_tab", "active");


	for (i = 0; i < hideForms.length; i++)
	{
		//Hide the form
		updateCtrlsClass(hideForms[i], "hidden");
		
		//Set the tabs as active
		updateCtrlsClass(hideForms[i] + "_tab", "");
		
	}
		
	return false;
}

//Show the existing comments form
function showExistingComments()
{
	clearTimeout(tmr);

	showCommentForm('view_comment', new Array('post_comment', 'alert_moderator'));

	//Hide the character counter message
	updateCtrlsClass("character_limit_tab", "hidden");

}

//Show the post comment form
function showPostComments()
{
	clearTimeout(tmr);

	updateCtrlsClass("comment_submit", "");
	updateCtrlsClass("comment_thanks", "hidden");
	
	showCommentForm('post_comment', new Array('view_comment', 'alert_moderator'));

	var el = document.getElementById('new_comment');
	if ((el)  && (el.value == ''))
	{
		focused = false;
		el.value = 'Enter comments...';
	}
	
	//Show the character counter message
	updateCtrlsClass("character_limit_tab", "");
}

//Show the confirm alert form
function showConfirmAlert()
{
	clearTimeout(tmr);
	
	updateCtrlsClass("alert_confirm", "");
	updateCtrlsClass("alert_thanks", "hidden");
	
	showCommentForm('alert_moderator', new Array('view_comment', 'post_comment'));
	
	//Hide the character counter message
	updateCtrlsClass("character_limit_tab", "hidden");
}

//Get the comments string
function doCommentString(uri)
{
	var result = "";
	var count = Number(doGetCommentCount(uri));
	if (count > 0)
	{
		
		if (count == 1)
			result = "comment"
		else
			result = "comments"

		result = "(" + String(count) + "&nbsp;" + result + ")";
	}
	

	return result;
}

//Get more comments from the server
function doMoreComments(tcmuri)
{
	commentAction = "more";
	
	return xmlCommentPost("/Comment", false, tcmuri);
}

//Get comment count from the server
function doGetCommentCount(tcmuri)
{
	commentAction = "count";
	
	return xmlCommentPost("/Comment", false, tcmuri);
}

//Post a user's comment
function doPostComment(tcmuri)
{
	//Do webtrends capture of post comment event
	dcsMultiTrack("DCS.dcsuri","/Post A Comment.html","DCSext.activity","Post a Comment","DCSext.CommentedArticle",
		getArticleTitle(),"WT.ti","Post A Comment","WT.AJAX","1","DCSqry","","DCSext.articleAuthor","");

	DCS.dcsuri=DCS.dcsqry=WT.ti=WT.AJAX=DCSext.CommentedArticle=DCSext.activity="";

	commentAction = "post";
	
	xmlCommentPost("/Comment", true, tcmuri);
}

//Alert the moderator
function doAlertModerator(commentID)
{
	actCommentID = commentID;
	showConfirmAlert();
}

//Commit the moderator alert
function commitAlert(tcmuri)
{
	commentAction = "alert";
	xmlCommentPost("/Comment", true, tcmuri);
}

//Update the comment counter div contents
function updateCommentCounter(tcmuri)
{
	var countCtrl = document.getElementById("counter_" + tcmuri);
	if (countCtrl)
	{
		var newCount = document.getElementById("newcount_" + tcmuri);
		if (newCount)
			countCtrl.innerHTML = newCount.innerHTML;
	}
}

//Setup the Comment query to be sent to the server
function setupCommentQuery(tcmuri)
{
	//We will always have the action to be performed
	var result = "commentAction=" + commentAction + "&uri=" + tcmuri;
	
	if (commentAction == "post")
	{
		ctrl = document.getElementById("new_comment");
	
		if (ctrl)
			result = result + "&comment=" + escape(ctrl.value);
	}
	else if (commentAction == "alert")
	{
		result = result + "&commentID=" + actCommentID;
	}
	
	return result;
}

//Post an XML HTTP Request for AJAX
// This code was taken from common.css originally written by Will Price
function xmlCommentPost(strURL, doAsynchronous, tcmuri) 
{
    var xmlHttpReq = false;

    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlHttpReq.open('POST', strURL, doAsynchronous);
    xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

    //Do asynchronous request
    if (doAsynchronous)
    {
        xmlHttpReq.onreadystatechange = function() {
            if (xmlHttpReq.readyState == 4) {
                updatePage(xmlHttpReq.responseText);
            }
        }

        //Set the current uri for setting comments when returned
		currentTcmUri = tcmuri;

        //Compose and send the request string
        xmlHttpReq.send(setupCommentQuery(tcmuri));

    }
    //Return results on synchronous requests
    else
    {
        //Compose and send the request string
        xmlHttpReq.send(setupCommentQuery(tcmuri));

        return xmlHttpReq.responseText;
    }	
    
}

//Update the page according to results returned from the AJAX request
// This code was taken from common.css originally written by Will Price
function updatePage(strResult)
{
    var comments = document.getElementById("existing_comments");
	
	if (comments)
		comments.innerHTML = strResult;
	
	//Update the comment counter
	updateCommentCounter(currentTcmUri);
	currentTcmUri = "";
	
	//Show appropriate success message depending on action
	if (commentAction == "post")
	{
		updateCtrlsClass("comment_submit", "hidden");
		updateCtrlsClass("comment_thanks", "");
		tmr = setTimeout("showExistingComments();", 5000);
		ctrl = document.getElementById("new_comment");
	
		if (ctrl)
		{
			ctrl.value = "Enter comments...";
			focused = false;
		}
	}
	else if (commentAction == "alert")
	{
		updateCtrlsClass("alert_confirm", "hidden");
		updateCtrlsClass("alert_thanks", "");
		tmr = setTimeout("showExistingComments();", 5000);
	} 
}
