//javascript for blog.cgi

var BlogHandler =
{
	userName : '',
	ValidateComment : function(name)
	{
		var postDiv = document.getElementById(name);
		
		if (!postDiv || postDiv.value.length === 0 || postDiv.value.length > 1000)
		{
			postDiv = null;
			return false;
		}
		
		postDiv = null;
		return true;
	},
	ChangeAnon : function()
	{
		var nameSpan = document.getElementById('name');
		if (!nameSpan)
		{
			return;
		}
		
		if (BlogHandler.userName === '')
		{
			BlogHandler.userName = nameSpan.firstChild.data;
			nameSpan.firstChild.data = "Anonymous";
		}
		else
		{
			nameSpan.firstChild.data = BlogHandler.userName;
			BlogHandler.userName = '';
		}
		
		nameSpan = null;
	},
	PostChanged : function(post)
	{
		if (post.value.length > 1000)
		{
			post.value = post.value.substring(0,1000);
		}
	},
	ToggleArchive : function(obj,name)
	{
		var archive = document.getElementById(name);
		if (!archive || !obj)
		{
			return;
		}
		
		if (archive.style.display === 'block')
		{
			archive.style.display = 'none';
			obj.className = 'archiveClosed';
		}
		else
		{
			archive.style.display = 'block';
			obj.className = 'archiveOpen';
		}

		archive = null;
		window.ReSize();
	},
	ValidatePost : function()
	{
		var postBox = document.getElementById('postBox');
		
		if (!postBox)
		{
			return false;
		}
		
		if (postBox.value.length === 0)
		{
			return false;
		}
		
		postBox = null;
		return true;
	},
	ToggleEditPost : function (postId)
	{
		var editForm = document.getElementById('p' + postId);
		var postBody = document.getElementById('postBody');
		
		if (!editForm || !postBody)
		{
			editForm = null;
			postBody = null;
			return;
		}
		
		if (editForm.style.display === 'block')
		{
			editForm.style.display = 'none';
			postBody.style.display = 'block';
		}
		else
		{
			postBody.style.display = 'none';
			editForm.style.display = 'block'
		}
		
		editForm = null;
		postBody = null;
		
		window.ReSize();
	},
	ToggleEditComment : function (commentId)
	{
		var editForm = document.getElementById('e' + commentId);
		var commentBody = document.getElementById('c' + commentId);
		
		if (!editForm || !commentBody)
		{
			editForm = null;
			commentBody = null;
			return;
		}
		
		var editButton = document.getElementById(commentId + '_edit');
		var deleteButton = document.getElementById(commentId + '_delete');
		var editType = document.getElementById(commentId + '_submit');
		
		if (!editButton || !deleteButton)
		{
			editForm = null;
			commentBody = null;
			editButton = null;
			deleteButton = null;
			editType = null;
			return;
		}
		
		if (editForm.style.display === 'none')
		{
			commentBody.style.display = 'none';
			editForm.style.display = 'block';
			editButton.firstChild.data = 'cancel';
			deleteButton.firstChild.data = 'save';
			editType.value = 'Save';
		}
		else
		{
			editForm.style.display = 'none';
			commentBody.style.display = 'block';
			editButton.firstChild.data = 'edit';
			deleteButton.firstChild.data = 'delete';
			editType.value = 'Delete';
		}
		
		editForm = null;
		commentBody = null;
		editButton = null;
		deleteButton = null;
		editType = null;
		
		window.ReSize();
	},
	EditComment : function(commentId)
	{
		var type = document.getElementById(commentId + '_submit');
		var form = document.getElementById('form_' + commentId);
		
		if (type.value === 'Save')
		{
			if (BlogHandler.ValidateComment('post_' + commentId) && !!form)
			{
				form.submit();
			}
		}
		else
		{
			var ok = confirm("Are you sure you want to delete this comment?");
			if (ok && !!form)
			{
				form.submit();
			}
		}
		
		type = null;
		form = null;
	}
};

