var TypeConst = "Type your comments here...";

function AddComments(o)
{
	if (o.newComment.value == "" || o.newComment.value == TypeConst)
	{
		alert("Type your comments.");
		o.newComment.select();
	}
	else
		AjaxPost(o.action, "comments=" + encodeURI(o.newComment.value), AddCommentsResults);
	
	return false;
}

function AddCommentsResults()
{
	if (HttpPost.readyState == 4)
	{
		if (HttpPost.status == 200)
		{
			var x = HttpPost.responseText;
			
			if(x.substring(0,6) == "ERROR=")
			{
				alert("The following error occurred:\n\n" + x.substring(6,x.length));
			}
			else
			{
				// Reset the form.
				var o = document.getElementById("formNewComment").newComment;
				var p = document.getElementById("charNewComment");
				o.value = "";
				NewCommentBlur(o,p);
				
				if (CommentsToShow == 0)
					alert("Your comments have been sent to '" + Username + "'.");
				else
				{
					// Add the new comments.
					CommentsCount++;
					var cNew = new Array();
					cNew[0] = x;
					for (var i = 0; i < c.length; i++)
					{
						if (i < CommentsToShow)
							cNew[i+1] = c[i];
					}
					c = cNew;
					LoadComments();
					
					alert("Your comments have been added.");
				}
			}
		}
		else
			alert("An unexpected connection error occurred. Please try your request again. If you continue to encounter problems, please contact Customer Support.");
	}
}

function CloseAllExcept(i, j)
{
	if (j != 0 && OpenFriendBox != "")
		CloseFriendBox(document.getElementById("addFriend" + i));
	
	if (j != 1 && OpenFavoriteMemberBox != "")
		CloseFavoriteMemberBox(document.getElementById("addFavoriteMember" + i));
	
	if (j != 2 && OpenReportObjBox != "")
		CloseFavoriteMemberBox(document.getElementById("reportProfile" + i));
}

function DeleteAll()
{
	if (MyId == MemberId)
	{
		var c = confirm('Are you sure you want delete all of these rows?');
		
		if (c == true)
		{
			var x = AjaxGet("http://www.debate.org/people/member/remove_2.asp",false,true);
			
			if (x.toLowerCase() != "true")
				alert("An unexpected connection error occurred. Please try your request again. If you continue to encounter problems, please contact Customer Support.")
			
			h.length = 0
			
			LoadHistory();
		}
	}
}

function DelRow(i)
{
	if (MyId == MemberId)
	{
		var x = AjaxGet("http://www.debate.org/people/member/remove_1.asp?id=" + i,false,true);
		
		if (x.toLowerCase() != "true")
			alert("An unexpected connection error occurred. Please try your request again. If you continue to encounter problems, please contact Customer Support.")
		else
		{
			var b = new Array();
			var k = 0;
			var o = document.getElementById("memberHistory")
			
			for (var m = 0; m < h.length; m++)
			{
				var c = h[m].split(";");
				if (i != c[0])
				{
					b[k] = h[m];
					k++;
				}
			}
			
			h = b;
			LoadHistory();
		}
	}
}

function LoadComments()
{
	LoadCommentsAll();
	
	var o = document.getElementById("profileComments");
	
	if (CommentsCount == 0)
	{
		o.innerHTML = "No comments have been posted on <strong>" + Username + AddPossessive(Username) + "</strong> profile.";
		o.style.paddingBottom = "10px";
	}
	else
	{
		var x = "";
		for (var i = 0; i < c.length; i++)
		{
			var j = c[i].search(",")
			var Id = c[i].substring(0,j)
			var Comment = c[i].substring(j+1,c[i].length)
			
			if (MyId == MemberId)
				x += "<div><div class=\"del\"><a href=\"http://www.debate.org/" + Username + "/comments/delete/" + Id + "/\" onClick=\"return confirm('Are you sure you want to permanently delete this comment?');\">Delete</a></div>" + Comment + "<div class=\"clear\">&nbsp;</div></div>";
			else
				x += "<div>" + Comment + "<div class=\"clear\">&nbsp;</div></div>";
			
			if (i < c.length-1)
				x += "<div class=\"hr\"></div>";
		}
		o.innerHTML = x;
		
		o.style.paddingBottom = "0px";
	}
}

function LoadCommentsAll()
{
	var p = document.getElementById("allComments");
	
	if (CommentsCount <= CommentsToShow)
		p.innerHTML = FormatNumber(CommentsCount,0,0) + " Comment" + Plural(CommentsCount);
	else
		p.innerHTML = "<a href=\"http://www.debate.org/" + Username + "/comments/\">" + FormatNumber(CommentsCount,0,0) + " Comment" + Plural(CommentsCount);
}

function LoadHistory()
{
	var x = "";
	var o = document.getElementById("memberHistory");
	
	if (h.length == 0)
	{
		document.getElementById("delAll").style.display = "none";
		x = "<div class=\"none\">There are no records right now.</div>";
	}
	else
	{
		for (var i = 0; i < h.length; i++)
		{
			var b = h[i].split(";");
			x += "<div class=\"row\" onMouseOver=\"Over(this);\" onMouseOut=\"Out(this);\">";
			if (MyId == MemberId)
				x += "<div class=\"del\"><a href=\"#\" onClick=\"DelRow(" + b[0] + "); return false;\">X</a></div>";
			x += b[1] + "</div>";
			
			if (i < h.length-1)
				x += "<div class=\"break\"></div>";
		}
	}
	o.innerHTML = x;
	
	// Resize the history object.
	var BaseHgt = 23;
	var RowHgt = 24;
	var MaxRows = 5;
	
	for (var i = 0; i < h.length-1; i++)
	{
		if (i <= MaxRows-2)
			BaseHgt += RowHgt;
	}
	
	o.style.height = BaseHgt + "px";
}

function NewCommentBlur(o,p)
{
	if (o.value == "" || o.value == TypeConst)
	{
		o.value = TypeConst;
		o.style.color = "#808080";
		p.innerHTML = FormatNumber(CommentsLenMax,0,0) + " Characters Remaining";
	}
}

function NewCommentFocus(o,p)
{
	if (o.value == TypeConst)
	{
		o.value = "";
		o.style.color = "#000000";
		p.innerHTML = FormatNumber(CommentsLenMax,0,0) + " Characters Remaining";
	}
}

function Out(o)
{
	o.style.backgroundColor = "#ffffff";
}

function Over(o)
{
	o.style.backgroundColor = "#f5f5f5";
}
