var OpenFavoriteDebateBox = "";

function AddFavoriteDebate(BoxObj, Id)
{
	var ShowForm = false;
	if (OpenFavoriteDebateBox == BoxObj)
		CloseFavoriteDebateBox(BoxObj);
	else
	{
		if (OpenFavoriteDebateBox != "")
			CloseFavoriteDebateBox(OpenFavoriteDebateBox);
		
		BoxObj.style.display = "inline";
		OpenFavoriteDebateBox = BoxObj;
		
		var x = "";
		
		if (MyStats == "")
		{
			x += "<p>You must first login before adding this debate to your list of favorites.</p>";
			x += "<p><a href=\"http://www.debate.org/login/\">Login to your account.</a></p>";
			x += "<p><a href=\"http://www.debate.org/join/\">Become a member.</a></p>";
		}
		else if (MyStats[3] == 1)
		{
			x += "<p>Before you can add this debate to your list of favorites, you will first need to verify your email address.</p>";
			x += "<p><a href=\"http://www.debate.org/verify/\">Verify your email address.</a></p>";
		}
		else
		{
			BoxObj.innerHTML = "Loading...";
			var y = AjaxGet("http://www.debate.org/member/favorites/debates/add/?id=" + Id,false,true);
			y = y.split(",");
			
			if (y == "0")
			{
				ShowForm = true;
				
				BoxObj.style.height = "175px";
				
				x += "<div id=\"confirmTxt\"></div>";
				x += "<form action=\"http://www.debate.org/member/favorites/debates/add/\" id=\"" + BoxObj + "Form\" method=\"get\" onSubmit=\"return SubmitAddFavoriteDebate(this);\">";
				x += "<div class=\"when\">Email me when...</div>";
				x += "<div class=\"check\"><input id=\"notify_0\" name=\"notify_0\" type=\"checkbox\" value=\"true\" checked=\"checked\" /><label for=\"notify_0\">a new argument is posted on this debate.</label></div>";
				x += "<div class=\"check\"><input id=\"notify_1\" name=\"notify_1\" type=\"checkbox\" value=\"true\" checked=\"checked\" /><label for=\"notify_1\">a new comment is posted on this debate.</label></div>";
				x += "<div class=\"check\"><input id=\"notify_2\" name=\"notify_2\" type=\"checkbox\" value=\"true\" checked=\"checked\" /><label for=\"notify_2\">the winner has been decided for this debate.</label></div>";
				x += "<div class=\"btn\"><input name=\"submit\" type=\"submit\" value=\"Add to My Favorites\" /></div>";
				x += "<input id=\"add\" name=\"add\" type=\"hidden\" value=\"true\" />";
				x += "<input id=\"boxId\" name=\"boxId\" type=\"hidden\" value=\"" + BoxObj.id + "\" />";
				x += "<input id=\"id\" name=\"id\" type=\"hidden\" value=\"" + Id + "\" />";
				x += "</form>";
			}
			else if (y == "1")
			{
				x += "<p>You must first login before adding this debate to your list of favorites.</p>";
				x += "<p><a href=\"http://www.debate.org/login/\">Login to your account.</a> Or, <a href=\"http://www.debate.org/join/\">become a member.</a></p>";
			}
			else if (y == "2")
			{
				x += "<p>Before you can add this debate to your list of favorites, you will first need to verify your email address.</p>";
				x += "<p><a href=\"http://www.debate.org/verify/\">Verify your email address.</a></p>";
			}
			else if (y == "3")
				x += "<p>This debate cannot be added to your list of favorites at this time.</p>";
			else if (y == "4")
			{
				x += "<p>This debate is already on your list of favorites.</p>";
				x += "<p><a href=\"http://www.debate.org/member/favorites/debates/\">View your favorite debates.</a></p>";
			}
			else
				x += "<p>An unexpected connection error occurred. Please try your request again. If you continue to encounter problems, please contact <a href=\"http://www.debate.org/help/contact/?subject=1\">Customer Support</a>.</p>";
		}
		x = "<div class=\"close\"><a href=\"#\" onClick=\"CloseFavoriteDebateBox(document.getElementById('" + BoxObj.id + "')); return false;\">Close Window</a></div><div class=\"title\">Add this Debate to My Favorites</div>" + x;
		BoxObj.innerHTML = x;
	}
	
	if (ShowForm == true)
		var o = document.getElementById(BoxObj + "Form");
}

function CloseFavoriteDebateBox(o)
{
	OpenFavoriteDebateBox = "";
	o.style.display = "none";
	o.innerHTML = "";
}

function SubmitAddFavoriteDebate(o)
{
	var x = AjaxGet("http://www.debate.org/member/favorites/debates/add/?add=true&id=" + o.id.value + "&notify_0=" + o.notify_0.checked + "&notify_1=" + o.notify_1.checked + "&notify_2=" + o.notify_2.checked,false,true);
	
	if (x.toLowerCase() == "true")
	{
		document.getElementById(o.boxId.value).style.height = "100px";
		o.style.display = "none";
		document.getElementById("confirmTxt").innerHTML = "This debate has been added to your list of favorites.<br /><br /><a href=\"http://www.debate.org/member/favorites/debates/\">Manage your list of favorite debates.</a>";
	}
	else
		alert("An unexpected connection error occurred. Please try your request again. If you continue to encounter problems, please contact Customer Support.");
	
	return false;
}
