function trim(value)
{
	if (typeof(value) != "string")
		return;
	
	value = value.replace(/^\s+/,'');
	value = value.replace(/\s+$/,'');
	return value;
}

function openWindow(url, width, height)
{
	window.open(url, "_blank", "width="+width+",height="+height+",scrollbars=1");
}

function ToggleDisplay(id)
{
	var element = document.getElementById(id);
	if (element == null || typeof(element) == "undefined")
		return;
	
	if (element.style.display == "none")
		element.style.display = "";
	else
		element.style.display = "none";
}

var chars = new Array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9");
function GetRandomString(length)
{
	length = parseInt(length);
	if (isNaN(length))
		length = 10;
	
	var string = "";
	for (var i = 0; i < length; i++)
	{
		var random = Math.floor(Math.random() * chars.length);
		string += chars[random];
	}
	return string;
}

var periods			= new Array("w",    "d",   "h",  "m", "s");
var periodSeconds	= new Array(604800, 86400, 3600, 60,   1);
function ReadableDuration(duration)
{
	if (duration == null)
		return;
	
	duration = parseInt(duration);
	if (isNaN(duration))
		return;
	
	var readable = "";
	for (var i = 0; i < periods.length; i++)
	{
		var remaining = duration % periodSeconds[i];
		var secondsForThisPeriod = duration - remaining;
		duration = remaining;
		
		// Must be larger than zero or we must already have something
		if (secondsForThisPeriod > 0)
			readable += (secondsForThisPeriod / periodSeconds[i]) + periods[i] + " ";
	}
	
	return trim(readable);
}

var units		= new Array("GB",       "MB",    "KB", "B");
var unitBytes	= new Array(1073741824, 1048576, 1024,  1 );
function ReadableSize(bytes)
{
	if (bytes == null)
		return;
	
	bytes = parseInt(bytes);
	if (isNaN(bytes))
		return;
	
	if (bytes <= 0)
		return "0 B";
	
	for (var i = 0; i < units.length; i++)
	{
		var remaining = bytes % unitBytes[i];
		if (remaining == bytes)
			continue;
		
		var size = Math.round((bytes / unitBytes[i]) * 10);
		return size / 10 + " " + units[i];
	}
}

function AlterComment(id, typeID, typeGUID, deleteComment)
{
	if (typeof(id) != "number" || typeof(typeID) != "number" || typeof(typeGUID) == "undefined")
		return;
	
	if (deleteComment !== true)
		deleteComment = false;
	
	// The updated text
	var text = document.getElementById("EditCommentText" + id).value;
	if (trim(text).length < 2 && !deleteComment)
	{
		alert("Please type some text...");
	}
	else
	{
		// Send update request to the server
		var url = BASE_PATH + "comments/" + (deleteComment ? "delete" : "edit") + "/";
		jQuery.post(
			url,
			{ id: id, message: text, typeGUID: typeGUID, typeID: typeID, sbe: "" },
			AlterCommentCompleted,
			"json"
		);
	}
}

function AlterCommentCompleted(json)
{
	if (json.error > 0)
	{
		if (json.error == 6)
		{
			alert("The contents of your message were invalid. Please keep it simple.");
		}
		else
		{
			alert("Failed to edit the coment! (error " + json.error + ", " + json.action + ")");
		}
	}
	else
	{
		if (json.action == "edit")
		{
			// Update the website
			document.getElementById("CommentText" + json.id).innerHTML = json.message;
			ToggleDisplay("CommentText" + json.id);
			ToggleDisplay("EditComment" + json.id);
		}
		else if (json.action == "delete")
		{
			// Remove the comment
			var e = document.getElementById("Comment" + json.id);
			e.parentNode.removeChild(e);
		}
		else
		{
			alert("Someone set us up the bomb!");
		}
	}
}

function QuoteComment(text)
{
	var e = document.getElementById("PostCommentText");
	if (e == null || typeof(e) == "undefined")
		return;
	
	e.value += "[quote]" + text.replace(/&#039;/, "'") + "[/quote]\r\n";
	document.location = "#PostComment";
	e.focus();
}

function EditForumPost(id)
{
	try
	{
		var text = document.getElementById("editposttext" + id).value;
		var token = document.getElementById("forumPostSecurityToken");
		jQuery.post(
			BASE_PATH + "forums/editpost/",
			{ id: id, message: text, securityToken: token.value },
			function (json)
			{
				if (json.error == 0)
				{
					document.getElementById("post"+id).innerHTML = json.message;
					ToggleDisplay("post"+id);
					ToggleDisplay("editpost"+id);

					// Update security token
					token.value = json.securityToken;
				}
				else
				{
					alert("The post could not be edited. Make sure you've typed something. (error " + json.error + ")");
				}
			},
			"json"
		);
	}
	catch (e)
	{
		alert("Failed to edit forum post\n\n" + e);
	}
}