var OpenFriendBox = "";
var AddFriendMsgMax = 1000; //Changes must also be made on constants script.

function AddFriend(BoxObj, Id, Username, Photo)
{
	var ShowForm = false;
	if (OpenFriendBox == BoxObj)
		CloseFriendBox(BoxObj);
	else
	{
		if (OpenFriendBox != "")
			CloseFriendBox(OpenFriendBox);
		
		BoxObj.style.display = "inline";
		OpenFriendBox = BoxObj;
		
		var x = "";
		
		if (MyStats == "")
		{
			x += "<p>You must first login before adding " + Username + " as a friend.</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 a friend to your account, 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 (MyStats[0].toLowerCase() == Username.toLowerCase())
			x += "<p>You cannot be your own friend.</p>";
		else
		{
			BoxObj.innerHTML = "Loading...";
			var y = AjaxGet("http://www.debate.org/member/friends/add/?id=" + Id,false,true);
			y = y.split(",");
			
			if (y[0] == "0")
			{
				ShowForm = true;
				
				x += "<div class=\"confirm\" id=\"confirmTxt\">" + Username + " will have to confirm your request.</div>";
				x += "<form action=\"http://www.debate.org/member/friends/add/\" id=\"" + BoxObj.id + "Form\" method=\"get\" onSubmit=\"return SubmitAddFriend(this);\">";
				x += "<div class=\"label\">Optional Message:</div>";
				x += "<div><textarea id=\"message\" name=\"message\" onKeyUp=\"CharRemain(this, document.getElementById('charAddFriendMsg'), " + AddFriendMsgMax + ");\"></textarea></div>";
				x += "<div id=\"charAddFriendMsg\"></div>";
				
				if (MyStats[2] == 1)
				{
					BoxObj.style.height = "345px";
					x += "<div class=\"label\">Security Code:<span><a href=\"javascript: SecCodeReload('secImg', '" + y[1] + "', 'secCode');\">Can't see the security code?</a></span></div>";
					x += "<div class=\"secCode\">";
						x += "<div class=\"row\"><img id=\"secImg\" src=\"http://www.debate.org/code/?key=" + y[1] + "\" alt=\"Security Code\" /></div>";
						x += "<div class=\"row\"><input id=\"secCode\" name=\"secCode\" type=\"text\" value=\"Type code here...\" onBlur=\"SecCodeFocus(this);\" onFocus=\"SecCodeFocus(this);\" /><span>Tired of these? <a href=\"http://www.debate.org/member/settings/mobile/\" onClick=\"return confirm('You are about to leave this page. Do you want to continue?');\">Confirm your identity.</a></span></div>";
						x += "<input id=\"secKey\" name=\"secKey\" type=\"hidden\" value=\"" + y[1] + "\" />";
					x += "</div>";
				}
				else
					BoxObj.style.height = "235px";
				
				x += "<div class=\"btn\"><input name=\"submit\" type=\"submit\" value=\"Add Friend\" /></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 += "<input id=\"username\" name=\"username\" type=\"hidden\" value=\"" + Username + "\" />";
				x += "</form>";
			}
			else if (y[0] == "1")
			{
				x += "<p>You must first login before adding " + Username + " as a friend.</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 (y[0] == "2")
			{
				x += "<p>Before you can add a friend to your account, 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[0] == "3")
				x += "<p>You cannot request to be friends with yourself.</p>";
			else if (y[0] == "4")
				x += "<p>" + Username + AddPossessive(Username) + " account has been closed or is no longer active.</p>";
			else if (y[0] == "5")
				x += "<p>" + Username + " is not accepting new friends at this time.</p>";
			else if (y[0] == "6")
			{
				x += "<p>A request to be friends with " + Username + " is already pending.</p>";
				x += "<p><a href=\"http://www.debate.org/member/friends/requests/?sent=true\">Manage your friend requests.</a></p>";
			}
			else if (y[0] == "7")
			{
				x += "<p>You are already friends with " + Username + ".</p>";
				x += "<p><a href=\"http://www.debate.org/member/friends/\">View your friends.</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 = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td class=\"left\">" + Photo + "</td><td class=\"right\"><div class=\"close\"><a href=\"#\" onClick=\"CloseFriendBox(document.getElementById('" + BoxObj.id + "')); return false;\">Close Window</a></div><div class=\"title\">Add " + Username + " as a Friend</div>" + x + "</td></tr></table>";
		BoxObj.innerHTML = x;
	}
	
	if (ShowForm == true)
	{
		var o = document.getElementById(BoxObj.id + "Form");
		CharRemain(o.message, document.getElementById("charAddFriendMsg"), AddFriendMsgMax);
	}
}

function CloseFriendBox(o)
{
	OpenFriendBox = "";
	o.style.display = "none";
	o.innerHTML = "";
}

function SubmitAddFriend(o)
{
	if (MyStats[2] == 1 && o.secCode.value != null && (o.secCode.value == "" || o.secCode.value == "Type code here..."))
	{
		alert("Enter the security code.");
		o.secCode.focus();
	}
	else
	{
		if (MyStats[2] == 1)
			var x = AjaxGet("http://www.debate.org/member/friends/add/?add=true&id=" + o.id.value + "&secCode=" + o.secCode.value + "&secKey=" + o.secKey.value + "&message=" + escape(o.message.value),false,true);
		else
			var x = AjaxGet("http://www.debate.org/member/friends/add/?add=true&id=" + o.id.value + "&message=" + escape(o.message.value),false,true);
		
		x = x.split(",");
		
		if (x[0].toLowerCase() == "false")
		{
			alert("The security code you entered did not match the image. Please try again.");
			SecCodeReload(o.secImg.id, x[1], o.secCode.id);
			o.secCode.value = "Type code here...";
			o.secKey.value = x[1];
			o.secCode.focus();
		}
		else if (x[0].toLowerCase() == "true")
		{
			document.getElementById(o.boxId.value).style.height = "100px";
			o.style.display = "none";
			document.getElementById("confirmTxt").innerHTML = "We have sent your request to " + o.username.value + ".<br /><br /><a href=\"http://www.debate.org/member/friends/requests/?sent=true\">Manage your friend requests.</a>";
		}
		else
		{
			alert("An unexpected connection error occurred. Please try your request again. If you continue to encounter problems, please contact Customer Support.");
			SecCodeReload(o.secImg.id, x[1], o.secCode.id);
			o.secCode.value = "Type code here...";
			o.secKey.value = x[1];
		}
	}
	return false;
}
