//------------------------------------------------------------------------------
// Class:  Comment
//------------------------------------------------------------------------------
// Author:  CL
// Date:  2007/06/27
// Description:  This class defines the functions used by the Comment section.
//------------------------------------------------------------------------------

YAHOO.namespace("commentPreview.container");

function init() {
	YAHOO.commentPreview.container.previewComment = new YAHOO.widget.ResizePanel("previewComment", { width:"500px", visible:false, constraintoviewport:false } );
	YAHOO.commentPreview.container.previewComment.render(document.getElementById('addCommentSection'));
	YAHOO.util.Event.addListener("preview", "click", YAHOO.commentPreview.container.previewComment.show, YAHOO.commentPreview.container.previewComment, true);
}

YAHOO.util.Event.addListener(window, "load", init);

//--------------------------------------------------------------------------

function Comment()
{
	// Variables:
	var maxMsgLength = 4000;
	
	//--------------------------------------------------------------------------

	// Methods:
	this.updateCharCount = updateCharCount;
	this.preview = preview;
	this.addComment = addComment;
	this.regenerate = regenerate;
	this.showEditCommentForm = showEditCommentForm;
	this.editComment = editComment;
	this.deleteComment = deleteComment;
	this.deleteCommentConfirm = deleteCommentConfirm;
	this.deleteCommentConfirmClose = deleteCommentConfirmClose;
	this.escapeHTML = escapeHTML;

	//--------------------------------------------------------------------------
	function escapeHTML(str)
	{
		var div = document.createElement('div');
		var text = document.createTextNode(str);
		div.appendChild(text);
		return div.innerHTML;
	}
	//--------------------------------------------------------------------------
		
	function updateCharCount(textarea, alertField)
	{
		if (textarea.value.length > maxMsgLength)
		{
			textarea.value = textarea.value.substr(0, maxMsgLength);
			alertField.value = maxMsgLength - textarea.value.length;
			//document.getElementById('frmErr').style.display = "block";
			//document.getElementById('frmErr').innerHTML = '* Your message must have ' + maxMsgLength + ' chars or less.';
		}
		else
		{
			alertField.value = maxMsgLength - textarea.value.length;
		}
	}
	
	//--------------------------------------------------------------------------

	function preview(idString)
	{
		theElement = document.getElementById(idString);
			
		if (theElement.style.display == "none")
		{
			theElement.style.display = "block";
		}
		var sName = "<a href='" 
			+ document.frmAddComment.visitorURL.value 
			+ "' target='_new'>" 
			+ this.escapeHTML(document.frmAddComment.visitorName.value)
			+ "</a>";
			
		document.getElementById('previewCommentMessage').innerHTML = this.escapeHTML(document.frmAddComment.visitorComment.value);
		document.getElementById('previewCommentName').innerHTML = sName;
		//document.getElementById('previewCommentMessage').innerHTML = document.frmAddComment.visitorComment.value;
	}
	
	//--------------------------------------------------------------------------

	function addComment()
	{
		document.frmAddComment.isSaveComment.value = 1;
		document.frmAddComment.submit();
	}
	
	//--------------------------------------------------------------------------
	
	function regenerate()
	{
		document.frmAddComment.submit();
	}
	
	//--------------------------------------------------------------------------
	
	function showEditCommentForm(iCommentID)
	{
		document.frmEditComment.isShowEditCommentForm.value = 1;
		document.frmEditComment.editedCommentID.value = iCommentID;
		document.frmEditComment.submit();
	}
	
	//--------------------------------------------------------------------------
	
	function editComment(iCommentID)
	{
		document.frmEditComment.isEdit.value = 1;
		document.frmEditComment.isShowEditCommentForm.value = 0;
		document.frmEditComment.editedCommentID.value = iCommentID;
		document.frmEditComment.submit();
	}
	
	//--------------------------------------------------------------------------
	
	function deleteComment()
	{
		document.frmEditComment.isDelete.value = 1;
		document.frmEditComment.isShowEditCommentForm.value = 0;
		//document.frmEditComment.deleteCommentID.value = iCommentID;
		document.frmEditComment.submit();
	}
	
	//--------------------------------------------------------------------------

	function deleteCommentConfirm(idString,iCommentID)
	{
		theElement = document.getElementById(idString);
		if (theElement.style.display == "none")
		{
			theElement.style.display = "block";
		}
		
		document.getElementById(iCommentID + '_1').style.backgroundColor = "#EEEEEE";
		document.getElementById(iCommentID + '_2').style.backgroundColor = "#EEEEEE";
		document.getElementById(iCommentID + '_3').style.backgroundColor = "#EEEEEE";
		document.getElementById(iCommentID + '_4').style.backgroundColor = "#EEEEEE";
		document.getElementById(iCommentID + '_5').style.backgroundColor = "#EEEEEE";
		
		YAHOO.commentPreview.container.deleteComment = new YAHOO.widget.ResizePanel('deleteComment', { width:'300px', close:false, draggable:false, fixedcenter:true, visible:true, constraintoviewport:false } );
		YAHOO.commentPreview.container.deleteComment.render();
		YAHOO.util.Event.addListener("closeDeleteComment", "click", YAHOO.commentPreview.container.deleteComment.hide, YAHOO.commentPreview.container.deleteComment, true);
		document.frmEditComment.deleteCommentID.value = iCommentID;
				
		/*
		if(confirm("Are you sure you want to delete this comment?"))
		{
			document.frmEditComment.isDelete.value = 1;
			document.frmEditComment.isShowEditCommentForm.value = 0;
			document.frmEditComment.deleteCommentID.value = iCommentID;
			document.frmEditComment.submit();
		}
		*/
	}
	
	//--------------------------------------------------------------------------
	
	function deleteCommentConfirmClose(idString)
	{
		if(document.frmEditComment.editedCommentID)
		{
			if(document.frmEditComment.editedCommentID.value == document.frmEditComment.deleteCommentID.value)
			{
				document.frmEditComment.deleteCommentID.value = 0;
			}
			else
			{
				var iCommentID = document.frmEditComment.deleteCommentID.value;
				document.getElementById(iCommentID + '_1').style.backgroundColor = "#FFFFFF";
				document.getElementById(iCommentID + '_2').style.backgroundColor = "#FFFFFF";
				document.getElementById(iCommentID + '_3').style.backgroundColor = "#FFFFFF";
				document.getElementById(iCommentID + '_4').style.backgroundColor = "#FFFFFF";
				document.getElementById(iCommentID + '_5').style.backgroundColor = "#FFFFFF";
				document.frmEditComment.deleteCommentID.value = 0;
			}
		}
		else
		{
			var iCommentID = document.frmEditComment.deleteCommentID.value;
			document.getElementById(iCommentID + '_1').style.backgroundColor = "#FFFFFF";
			document.getElementById(iCommentID + '_2').style.backgroundColor = "#FFFFFF";
			document.getElementById(iCommentID + '_3').style.backgroundColor = "#FFFFFF";
			document.getElementById(iCommentID + '_4').style.backgroundColor = "#FFFFFF";
			document.getElementById(iCommentID + '_5').style.backgroundColor = "#FFFFFF";
			document.frmEditComment.deleteCommentID.value = 0;
		}
	}

}

// Create Object
oComment = new Comment();

//--------------------------------------------------------------------------

