function postComment()
{
	var object = document.getElementById('COMMENT_SELECTED');
	var submit = document.getElementById('BPostComment');
	var textarea = document.getElementById('TAComment');
	
	var message = document.getElementById('commentMessage');
	message.style.display = "none";
	message.style.visibility = "hidden";
	
	if(object.style.visibility == "visible")
	{
		submit.style.display = "none";
		submit.style.visibility = "hidden";
		textarea.disabled = true;
		object.style.display = "none";
		object.style.visibility = "hidden";
	}
	else
	{
		submit.style.display = "";
		submit.style.visibility = "visible";
		textarea.disabled = false;
		object.style.display = "inline";
		object.style.visibility = "visible";
	}
}

function getComments(path)
{
	function customHandler(node, myAjaxRequest)
	{
		try
		{
			comments = eval("(" + node.childNodes[0].childNodes[0].nodeValue + ")");
			alert(comments);
		}
		catch(e)
		{
			alert(e);
		}
	}
	
	var myAjaxRequest = new AjaxRequestObject();
	myAjaxRequest.url = "/data/DataRequest.aspx";
	myAjaxRequest.postParameters = "op=GetDocumentComments&Path=" + path;
	myAjaxRequest.requestType = "POST";
	myAjaxRequest.customHandler = customHandler;
	myAjaxRequest.startRequest();
}

function submitComment(path, commenter, source, _commentSelected, _commentCollapsed)
{	
	source.disabled = true;
	document.getElementById('BPostComment').disabled = true;
	document.getElementById('BPostComment').style.display = "none";
	document.getElementById('BPostComment').style.visibility = "hidden";
	
	//Convert newline characters into <br> breaks.
	output = source.value;
	output = output.replace(new RegExp(/\r\n/g), "<br/>");
	output = output.replace(new RegExp(/\n/g), "<br/>");
	output = output.replace(new RegExp(/\r/g), "<br/>");
	output = safeValue(output);
	output = encodeURIComponent(output);
	
	//Simple message to notify user that the comment is being saved in slow times...
	source.value = "Saving comment...";
	
	function customHandler(node, myAjaxRequest)
	{
		myAjaxRequest.targetID.value = "";
		if(document.getElementById(_commentSelected) != null && document.getElementById(_commentCollapsed) != null)
			ToggleObjectVisibility(_commentSelected, _commentCollapsed);
		else if(document.getElementById(_commentSelected) != null)
			ToggleObjectVisibility(_commentSelected);
			
		if(node.childNodes[0].childNodes[0].nodeValue == "OK")
		{
			myAjaxRequest.targetID.disabled = false;
			myAjaxRequest.messageObject.style.display = "";
			myAjaxRequest.messageObject.style.visibility = "visible";
			document.getElementById('BPostComment').disabled = false;
			document.getElementById('BPostComment').style.display = "";
			document.getElementById('BPostComment').style.visibility = "visible";			
		}
	}
	
	var url = "/data/DataRequest.aspx";
	
	var myAjaxRequest = new AjaxRequestObject();
	myAjaxRequest.url = url;
	myAjaxRequest.postParameters = "op=AddComment&Path=" + path + "&CommenterUserName=" + commenter + "&Comment=" + output;
	myAjaxRequest.requestType = "POST";
	myAjaxRequest.customHandler = customHandler;
	myAjaxRequest.targetID = source;
	myAjaxRequest.messageObject = document.getElementById('commentMessage');
	myAjaxRequest.startRequest();
}

function deleteComment(path, ownerUserName, commentID)
{
	if(commentID != null)
	{
		if(window.confirm("Are you sure you want to delete this comment?"))
		{
			function customHandler(node, myAjaxRequest)
			{
				if(node != null)
				{
					if(node.childNodes[0].childNodes[0].nodeValue == "OK")
					{
						var comment = document.getElementById('Comment_' + myAjaxRequest.targetID);
						comment.style.display = "none";
						comment.style.visibility = "hidden";
					}
				}
			}
			
			var url = "/data/DataRequest.aspx";
			var myAjaxRequest = new AjaxRequestObject();
			myAjaxRequest.url = url;
			myAjaxRequest.postParameters = "op=DeleteComment&Path=" + path + "&UserName=" + ownerUserName + "&CommentID=" + commentID;
			myAjaxRequest.requestType = "POST";
			myAjaxRequest.customHandler = customHandler;
			myAjaxRequest.targetID = commentID;
			myAjaxRequest.startRequest();
		}
	}
}

function approveComment(path, ownerUserName, commentID)
{
	if(commentID != null)
	{
		var comment = document.getElementById('Approve_' + commentID);
		var originalSource = comment.innerHTML;
		comment.innerHTML = "approve | ";
		
		function customHandler(node, myAjaxRequest)
		{
			if(node.childNodes[0].childNodes[0].nodeValue == "OK")
				comment.innerHTML = "approved | ";
			else
				comment.innerHTML = originalSource;	
		}
		
		var url = "/data/DataRequest.aspx";
		var myAjaxRequest = new AjaxRequestObject();
		myAjaxRequest.url = url;
		myAjaxRequest.postParameters = "op=ApproveComment&Path=" + path + "&UserName=" + ownerUserName + "&CommentID=" + commentID;
		myAjaxRequest.requestType = "POST";
		myAjaxRequest.customHandler = customHandler;
		myAjaxRequest.targetID = commentID;
		myAjaxRequest.startRequest();
	}
}

// RatingWidget class used for creating instances of the ratings widget.
// _id (The NAME of the variable created)
// _rating (The initial rating for this widget)
// _path (The path to the document being rated)
// _username (The username of the viewing user)
// _isOwner (Whether the viewing user is the owner or not (Owners cannot rate their own content))
// _displayStyle (Whether the display style of the widget is 'horizontal' or 'vertical')
function RatingWidget(_id, _rating, _path, _username, _isOwner, _displayStyle)
{
	this.rating				= _rating;					//The rating for this widget.
	this.id					= _id;						//Unique id of the widget object.
	this.path				= _path;					//Path to the document being rated.
	this.username			= _username;				//Username of user who is RATING this document.
	this.isOwner			= _isOwner.toLowerCase();	//Whether the widget is being viewed by the owner or not.
	this.displayStyle		= _displayStyle;			//The display style is either 'horizontal' or 'vertical'.
	this.cssstyle			= this.displayStyle == "horizontal" ? "float: left; width: 80px; text-align: right; padding-right: 6px; font-size: 12px; color: #000000; font-weight: bold;" : "font-size: 12px; color: #000000; font-weight: bold;";
		
	this.setRating			= _setRating;				//Function to set the rating of this widget.
	this.ratingHover		= _ratingHover;				//Function to handle rollovers.
	this.rateDocument		= _rateDocument;			//Function to rate the document.
	this.ratingLogin		= _ratingLogin;
	
	//This is called upon creation of a RatingWidget object to initialize it.
	this.Initialize = function()
	{
		if(this.rating < 0 || this.rating > 5)
			this.rating = 0.0;
		
		if(this.isOwner == "true" || this.isOwner == "anonymous")	//VIEW State
		{
			viewState(this.id, this.displayStyle, this.cssstyle, this.isOwner);
			this.setRating(this.rating);
		}
		else						//EDIT State
		{
			editState(this.id, this.displayStyle, this.cssstyle);
			this.setRating(this.rating);
		}
	}

	function viewState(_id, _displayStyle, _cssstyle, _isOwner)
	{
		if(_displayStyle == "horizontal")
		{
			var	temp = "<div id=\"" + _id + "ratingTitle\" style=\"" + _cssstyle + "\">Rating:</div>\n";
			temp += "<table id=\"" + _id + "star_\" cellspacing=\"0\" class=\"star_ratings\" style=\"position: relative; top: -3px;\">\n";
		}
		else
		{
			var	temp = "<div id=\"" + _id + "ratingTitle\" style=\"" + _cssstyle + "\">Rating:</div>\n";
			temp += "<table id=\"" + _id + "star_\" cellspacing=\"0\" class=\"star_ratings\">\n";
		}
		temp += "<tr>\n";
		if(_isOwner == "anonymous")
		{
			temp += "<td id=\"" + _id + "star_1\" class=\"star_empty\" onmouseover=\"javascript: " + _id + ".ratingLogin(this, '" + _id + "');\" onmouseout=\"javascript: ToggleObjectVisibility('" + _id + "_ratingLogin');\"><a href=\"/login/login.aspx?from=" + escape(document.URL) + "\"><img border=\"0\" src=\"/images/spacer.gif\" class=\"star_spacer\"></a></td>\n";
			temp += "<td id=\"" + _id + "star_2\" class=\"star_empty\" onmouseover=\"javascript: " + _id + ".ratingLogin(this, '" + _id + "');\" onmouseout=\"javascript: ToggleObjectVisibility('" + _id + "_ratingLogin');\"><a href=\"/login/login.aspx?from=" + escape(document.URL) + "\"><img border=\"0\" src=\"/images/spacer.gif\" class=\"star_spacer\"></a></td>\n";
			temp += "<td id=\"" + _id + "star_3\" class=\"star_empty\" onmouseover=\"javascript: " + _id + ".ratingLogin(this, '" + _id + "');\" onmouseout=\"javascript: ToggleObjectVisibility('" + _id + "_ratingLogin');\"><a href=\"/login/login.aspx?from=" + escape(document.URL) + "\"><img border=\"0\" src=\"/images/spacer.gif\" class=\"star_spacer\"></a></td>\n";
			temp += "<td id=\"" + _id + "star_4\" class=\"star_empty\" onmouseover=\"javascript: " + _id + ".ratingLogin(this, '" + _id + "');\" onmouseout=\"javascript: ToggleObjectVisibility('" + _id + "_ratingLogin');\"><a href=\"/login/login.aspx?from=" + escape(document.URL) + "\"><img border=\"0\" src=\"/images/spacer.gif\" class=\"star_spacer\"></a></td>\n";
			temp += "<td id=\"" + _id + "star_5\" class=\"star_empty\" onmouseover=\"javascript: " + _id + ".ratingLogin(this, '" + _id + "');\" onmouseout=\"javascript: ToggleObjectVisibility('" + _id + "_ratingLogin');\"><a href=\"/login/login.aspx?from=" + escape(document.URL) + "\"><img border=\"0\" src=\"/images/spacer.gif\" class=\"star_spacer\"></a></td>\n";
		}
		else
		{
			temp += "<td id=\"" + _id + "star_1\" class=\"star_empty\"><img src=\"/images/spacer.gif\" class=\"star_spacer\"></td>\n";
			temp += "<td id=\"" + _id + "star_2\" class=\"star_empty\"><img src=\"/images/spacer.gif\" class=\"star_spacer\"></td>\n";
			temp += "<td id=\"" + _id + "star_3\" class=\"star_empty\"><img src=\"/images/spacer.gif\" class=\"star_spacer\"></td>\n";
			temp += "<td id=\"" + _id + "star_4\" class=\"star_empty\"><img src=\"/images/spacer.gif\" class=\"star_spacer\"></td>\n";
			temp += "<td id=\"" + _id + "star_5\" class=\"star_empty\"><img src=\"/images/spacer.gif\" class=\"star_spacer\"></td>\n";
		}
		temp += "</tr>\n";
		temp += "</table>\n";
		if(_isOwner == "anonymous")
			temp += "<div id=\"" + _id + "_ratingLogin\" class=\"ACPWindow\" style=\"width: 255px; height: 32px; padding: 4px; display: none; visibility: hidden;\">You must be logged in to rate content. Click to login.</div>";		
		document.write(temp);
	}

	function editState(_id, _displayStyle, _cssstyle)
	{
		if(_displayStyle == "horizontal")
		{
			var	temp = "<div id=\"" + _id + "ratingTitle\" style=\"" + _cssstyle + "\">Rating:</div>\n";
			temp += "<table id=\"" + _id + "star_\" cellspacing=\"0\" class=\"star_ratings\" onmouseout=\"javascript: " + _id + ".setRating(" + _id + ".rating);\" style=\"position: relative; top: -3px;\">\n";
		}
		else
		{
			var	temp = "<div id=\"" + _id + "ratingTitle\" style=\"" + _cssstyle + "\">Rating:</div>\n";
			temp += "<table id=\"" + _id + "star_\" cellspacing=\"0\" class=\"star_ratings\" onmouseout=\"javascript: " + _id + ".setRating(" + _id + ".rating);\">\n";
		}
		temp += "<tr>\n";
		temp += "<td id=\"" + _id + "star_1\" class=\"star_empty\" onmouseover=\"javascript: " + _id + ".ratingHover(this, '1');\"><a id=\"" + _id + "star_a_1\" onclick=\"javascript: " + _id + ".rateDocument('1'); return false;\" href=\"#\"><img src=\"/images/spacer.gif\" alt=\"Click to rate\" class=\"star_spacer\"></a></td>\n";
		temp += "<td id=\"" + _id + "star_2\" class=\"star_empty\" onmouseover=\"javascript: " + _id + ".ratingHover(this, '2');\"><a id=\"" + _id + "star_a_2\" onclick=\"javascript: " + _id + ".rateDocument('2'); return false;\" href=\"#\"><img src=\"/images/spacer.gif\" alt=\"Click to rate\" class=\"star_spacer\"></a></td>\n";
		temp += "<td id=\"" + _id + "star_3\" class=\"star_empty\" onmouseover=\"javascript: " + _id + ".ratingHover(this, '3');\"><a id=\"" + _id + "star_a_3\" onclick=\"javascript: " + _id + ".rateDocument('3'); return false;\" href=\"#\"><img src=\"/images/spacer.gif\" alt=\"Click to rate\" class=\"star_spacer\"></a></td>\n";
		temp += "<td id=\"" + _id + "star_4\" class=\"star_empty\" onmouseover=\"javascript: " + _id + ".ratingHover(this, '4');\"><a id=\"" + _id + "star_a_4\" onclick=\"javascript: " + _id + ".rateDocument('4'); return false;\" href=\"#\"><img src=\"/images/spacer.gif\" alt=\"Click to rate\" class=\"star_spacer\"></a></td>\n";
		temp += "<td id=\"" + _id + "star_5\" class=\"star_empty\" onmouseover=\"javascript: " + _id + ".ratingHover(this, '5');\"><a id=\"" + _id + "star_a_5\" onclick=\"javascript: " + _id + ".rateDocument('5'); return false;\" href=\"#\"><img src=\"/images/spacer.gif\" alt=\"Click to rate\" class=\"star_spacer\"></a></td>\n";
		temp += "</tr>\n";
		temp += "</table>\n";
		document.write(temp);
	}
		
	//Rate a document.
	//rating (0-5 number indicating the desired rating)
	function _rateDocument(rating)
	{
		if(rating == null || (rating < 0 || rating > 5))
			return;
		function customHandler(node, myAjaxRequest)
		{
			try
			{
				myRequest.widget.rating = node.childNodes[0].childNodes[0].nodeValue;
				myRequest.widget.setRating(myRequest.widget.rating);
			}
			catch(e)
			{
				//Something broke, rating should not change.
			}	
		}					
		var myRequest = new AjaxRequestObject();
		myRequest.url = "/data/DataRequest.aspx";
		myRequest.postParameters = "op=RateDocument&Path=" + this.path + "&UserName=" + this.username + "&Rating=" + rating;
		myRequest.requestType = "POST";
		myRequest.widget = this;
		myRequest.customHandler = customHandler;
		myRequest.startRequest();
	}

	//Used to set the rating of the widget
	//rating (0-5 number indicating the desired rating)
	function _setRating(rating)
	{
		for(var i = 1; i <= 5; i++)
		{
			var star = document.getElementById(this.id + "star_" + i);
			if(i <= Math.floor(rating))
			{
				star.className = "star_red";
			}
			else if(i <= Math.ceil(rating))
			{
				var fraction = rating.split(".")[1];
				if(fraction <= 3)
					star.className = "star_1_fifth";
				if(fraction > 3 && fraction <= 5)
					star.className = "star_2_fifth";
				if(fraction > 5 && fraction <= 7)
					star.className = "star_3_fifth";
				if(fraction > 7)
					star.className = "star_4_fifth";
			}
			else if(i >= Math.ceil(rating))
			{
				star.className = "star_empty";
			}
		}		
		document.getElementById(this.id + "ratingTitle").innerHTML = "Rating:";
	}

	//Used when hovering over stars to update the color of the stars and the text.
	//star (star object we are hovering over)
	//starNumber (number of the star we are hovering over)
	function _ratingHover(star, starNumber)
	{
		for(var i = 1; i <= 5; i++)
		{
			var star = document.getElementById(star.id.substr(0, (star.id.length - 1)) + i);
			if(i <= starNumber)
				star.className = "star_yellow";
			else if(i > starNumber)
				star.className = "star_empty";
		}		
		var ratingTitle = document.getElementById(this.id + "ratingTitle");
		switch(starNumber)
		{
			case "1":
				ratingTitle.innerHTML = "I hate it"; break;
			case "2":
				ratingTitle.innerHTML = "I don't like it"; break;
			case "3":
				ratingTitle.innerHTML = "It's OK"; break;
			case "4":
				ratingTitle.innerHTML = "I like it"; break;
			case "5":
				ratingTitle.innerHTML = "I love it"; break;
		}	
	}
	
	function _ratingLogin(star, id)
	{
		var target = $(id + "_ratingLogin");
		target.style.top = (getTotalOffsetTop(star) + 25) + "px";
		target.style.left = (getTotalOffsetLeft(star) - 150) + "px";
		target.style.display = "";
		target.style.visibility = "visible";
	}
}

//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//OBSOLETE CODE BELOW!!!
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////

// Rate a document.
function rateDocument(widgetID, path, raterUserName, rating)
{
	if(rating == null || (rating < 0 || rating > 5))
	{
		return;
	}
	
	function customHandler(node, myAjaxRequest)
	{
		// set the new rating
		if(node != null)
		{
			_OBglobalrating = node.childNodes[0].childNodes[0].nodeValue;
			setRating(document.getElementById(myAjaxRequest.widgetID), node.childNodes[0].childNodes[0].nodeValue);
		}
	}
				
	var url = "/data/DataRequest.aspx";
	var myAjaxRequest = new AjaxRequestObject();
	myAjaxRequest.url = url;
	myAjaxRequest.postParameters = "op=RateDocument&Path=" + path + "&UserName=" + raterUserName + "&Rating=" + rating;
	myAjaxRequest.requestType = "POST";
	myAjaxRequest.customHandler = customHandler;
	myAjaxRequest.widgetID = widgetID;
	myAjaxRequest.startRequest();
}

// widget (the id of the table for tha rating)
// rating (0-5 number indicating the desired rating)
function setRating(widget, rating)
{
	for(var i = 1; i <= 5; i++)
	{
		var star = document.getElementById('star_' + i);
		if(i <= Math.floor(rating))
		{
			star.className = "star_red";
		}
		else if(i <= Math.ceil(rating))
		{
			var fraction = rating.split(".")[1];
			if(fraction <= 3)
				star.className = "star_1_fifth";
			if(fraction > 3 && fraction <= 5)
				star.className = "star_2_fifth";
			if(fraction > 5 && fraction <= 7)
				star.className = "star_3_fifth";
			if(fraction > 7)
				star.className = "star_4_fifth";
		}
		else if(i >= Math.ceil(rating))
		{
			star.className = "star_empty";
		}
	}
	
	document.getElementById('ratingTitle').innerHTML = "Rating:";
}

// star (star object we are hovering over)
// starNumber (number of the star we are hovering over)
function ratingHover(star, starNumber)
{
	for(var i = 1; i <= 5; i++)
	{
		var star = document.getElementById(star.id.substr(0, (star.id.length - 1)) + i);
		if(i <= starNumber)
			star.className = "star_yellow";
		else if(i > starNumber)
			star.className = "star_empty";
	}
	
	var ratingTitle = document.getElementById('ratingTitle');
	switch(starNumber)
	{
		case "1":
			ratingTitle.innerHTML = "I hate it";
			break;
		case "2":
			ratingTitle.innerHTML = "I don't like it";
			break;
		case "3":
			ratingTitle.innerHTML = "It's OK";
			break;
		case "4":
			ratingTitle.innerHTML = "I like it";
			break;
		case "5":
			ratingTitle.innerHTML = "I love it";
			break;
	}	
}

function getViewCount(path, target)
{	
	function customHandler(node, req)
	{
		var viewCount = "";
		
		try
		{
			viewCount = node.childNodes[0].childNodes[0].childNodes[0].childNodes[0].nodeValue;
		}
		catch(e)
		{
			viewCount = "";
		}
		
		req.target.innerHTML = viewCount;
	}
	
	var req = new AjaxRequestObject();
	req.url = "/data/DataRequest.aspx?op=GetDocumentViewCount&Path=" + path;
	req.customHandler = customHandler;
	req.target = $(target);
	req.randomizeURL = true;
	req.startRequest();
}







