/**
	Start generic comment action methods
**/

	function performSelectboxCommentAction(selectboxObj, widgetName)
	{
		if (selectboxObj)
			performCommentAction(selectboxObj.value, 'comment', extractCommentIdFromSelectBoxId(selectboxObj), widgetName);
	}
	
	function performCommentAction(actionName, commentTargetType, targetId, widgetName, skipConfirmation)
	{
		if (actionName != '')
		{
			// first time through, pop a confirmation window if required
			if (!skipConfirmation)
			{
				var callbackCmd = "";
			
				if (widgetName)
					callbackCmd = "performCommentAction('"+actionName+"','"+commentTargetType+"',"+targetId+",'"+widgetName+"',true)";
				else 
					callbackCmd = "performCommentAction('"+actionName+"','"+commentTargetType+"',"+targetId+",false,true)";
					
				if (actionName == 'blockuser')
				{
					confirmCommentChoice('Are you sure you want to permanently Block this user from submitting comments? All this user\'s comments (including attached replies) will be deleted and this can not be undone.',callbackCmd);
					return false;
				}
				else if (actionName == 'trustuser')
				{
					confirmCommentChoice('Trust User will automatically approve all comments from this user. Do you want to continue? ',callbackCmd);
					return false;
				}
				else if (actionName == 'delete')
				{
					confirmCommentChoice('Are you sure you want to delete this comment? This action can not be undone.',callbackCmd);
					return false;
				}
			}
			else
			{
				killModal();
			}
		
			resetAjaxResponseArray();
			resetCommentFormVariables();

			setCommentCommand(actionName,widgetName);
			if (widgetName)
				setWidgetName(widgetName);
			
			// For security, the target user is derived from the existing comment
			setCommentId(targetId,widgetName);
			submitCommentForm(handleCommentActionResponse, widgetName);
			
			if (commentTargetType == 'comment')
				resetActionSelectBox(targetId);
		}
		var commentScroller = getMainCommentContainer(widgetName);
		if (commentScroller && commentScroller.scrollUpdate)
			commentScroller.scrollUpdate();
	}

/**
	Start specific comment action methods
**/
		
	function saveApprovalMode(approvalMode)
	{
		setCommentCommand('saveapprovalmode');
		setApprovalMode(document.getElementById('approvalModeSelectId').value);
		toggleSubmitButton(document.getElementById('approvalModeSaveButtonId'),1);
	
		submitCommentForm(handleApprovalModeResponse);
	}
	
	function cancelReply(commentId,widgetName)
	{
		var replyContainer = getReplyFormContainer(commentId,widgetName);
		if (replyContainer)
		{
			replyContainer.innerHTML = '';
			replyContainer.style.display = 'none';

			resetActionSelectBox(getCommentId(widgetName));
			
			var commentScroller = getMainCommentContainer(widgetName);
			refreshWidgetContainerScrollerForObjectId(commentScroller);	
		}
		
	}
	
	function submitReply(commentId,widgetName)
	{
		var commentTextArea = document.getElementById('commentReplyTextarea' + commentId);
		var commentCaptcha = document.getElementById('commentReplyVerificationCode' + commentId);
		
		if (commentId && commentTextArea)
		{
			setCommentCommand('reply',widgetName);
			setCommentId(commentId,widgetName);
			setCommentText(commentTextArea.value,widgetName);
			
			if (commentCaptcha)
				setCaptchaCode(commentCaptcha.value);
			
			submitCommentForm(handleReplyResponse,widgetName);
		}
	}
	
	function renderNewCommentForm(widgetName, optionalTargetObjectId)
	{
		resetAjaxResponseArray();
		resetCommentFormVariables();
		setWidgetName(widgetName);

		setCommentCommand('rendercomment',widgetName);
		submitCommentForm(handleNewCommentFormResponse,widgetName, optionalTargetObjectId);
	}
	
	function submitNewComment(widgetName, objectId)
	{
		var commentTextArea = document.getElementById('commentNewCommentTextarea_' + widgetName);
		var commentCaptcha = document.getElementById('commentNewCommentVerificationCode_' + widgetName);
		
		if (commentTextArea)
		{
			if (objectId)
				setObjectId(objectId, widgetName);
		
			setCommentCommand('newcomment',widgetName);
			setCommentText(commentTextArea.value, widgetName);
			
			if (commentCaptcha)
				setCaptchaCode(commentCaptcha.value);
			
			if (document.getElementById('commentFormId_blogs'))
			{
				// Handle the response differently for blog comments (i.e. refresh the expander instead of inserting the blog comment)
				submitCommentForm(handleNewBlogCommentResponse,widgetName);
			}
			else
			{
				submitCommentForm(handleNewCommentResponse,widgetName);
			}
		}
	}

	function cancelNewComment(widgetName)
	{
		var commentContainer = '';
		
		var tmpBlogId = ajaxResponseArray.values['blogId'];
	
		if (tmpBlogId)
			commentContainer = getNewCommentFormContainer(widgetName,tmpBlogId);
		else
			commentContainer = getNewCommentFormContainer(widgetName);
			
		if (commentContainer)
		{
			commentContainer.innerHTML = '';
			commentContainer.style.display = 'none';
			
			var mainContainer = getMainCommentContainer(widgetName);
			if (mainContainer)
				mainContainer.style.display = 'block';
		}
		
		refreshWidgetContainerScrollerForObjectId(mainContainer);
	}
	
	function submitCommentBack(commentId, widgetName, objectId)
	{
		
		if (widgetName)
		{
			var commentBackTextAreaName = 'commentBackTextArea_' + widgetName;
			var commentBackVerificationCodeName = 'commentBackVerificationCode_' + widgetName;
		}
		else
		{
			var commentBackTextAreaName = 'commentBackTextArea';
			var commentBackVerificationCodeName = 'commentBackVerificationCode';
		}

		var commentTextArea = document.getElementById(commentBackTextAreaName);
		var commentCaptcha = document.getElementById(commentBackVerificationCodeName);
		
		if (commentId && commentTextArea)
		{
			if (objectId)
				setObjectId(objectId, widgetName);
		
			setCommentCommand('newcommentback',widgetName);
			setCommentId(commentId,widgetName);
			setCommentText(commentTextArea.value,widgetName);
			
			if (commentCaptcha)
				setCaptchaCode(commentCaptcha.value);
			
			submitCommentForm(handleCommentBackResponse,widgetName);
		}
	}
	
	function cancelCommentBack(widgetName)
	{
		var commentBackContainer = getCommentBackFormContainer(widgetName);
		if (commentBackContainer)
		{
			commentBackContainer.innerHTML = '';
			commentBackContainer.style.display = 'none';
			
			var mainContainer = getMainCommentContainer(widgetName);
			if (mainContainer)
				mainContainer.style.display = 'block';
				
			resetActionSelectBox(getCommentId());
		}
	}

/**
	Start Ajax Response Handlers
**/
					
	function handleCommentActionResponse()
	{
	
		var widgetName = getWidgetNameFromResponse();
		var currentCommand = getCurrentCommand(widgetName);
		var commentId = getCommentId();
		
		if (currentCommand == 'trustuser' || currentCommand == 'blockuser')
		{
			// Reload the page
			handleTrustBlockUserResponse();
		}
		else if (currentCommand == 'untrustuser')
		{
			handleUntrustUserResponse();
		}
		else if (currentCommand == 'approve' || currentCommand == 'decline')
		{
			// Hide the approved/declined comment
			hideCommentEntry(getCommentIdFromResponse());
			
			// TODO: Hide parent entry if applicable
		}
		else if (currentCommand == 'renderreply')
		{
			// Render the reply form on the page below the last reply
			handleDisplayReplyFormResponse();
		}
		else if (currentCommand == 'rendercommentback')
		{
			handleDisplayCommentBackFormResponse();
		}
		else if (currentCommand == 'delete')
		{
			handleCommentDeletionResponse();
		}
		else
		{
			alert('command='+currentCommand + ', Success=' + ajaxResponseArray.success);
		}
		
		refreshWidgetContainerScrollerForObjectId(getMainCommentContainer(widgetName));
	}
				
	function handleApprovalModeResponse()
	{
		toggleSubmitButton(document.getElementById('approvalModeSaveButtonId'),1,true);
	}
				
	function handleUntrustUserResponse()
	{
		if (ajaxResponseArray.success)
		{
			refreshCommentPage()
		}
		else
		{
			notify('Untrust User Failed');
		}
	}		
	
	function handleCommentDeletionResponse()
	{
		if (ajaxResponseArray.success)
		{
			var tmpWidgetName = getAjaxResponseValue('widgetName');
		
			hideCommentEntry(getCommentIdFromResponse());
			/*
				TODO: 
					If on 'new' page
						If removed comment had a parent comment
							Get container of parent comment
							If no child comments exist in parent container
								Remove parent comment
			*/
			
			// Add 'be the first to post' if there are no posts left
			decrementCommentCount(tmpWidgetName);
			if (getExistingCommentCount(tmpWidgetName) == 0)
			{
				if (getFirstToCommentContainer(tmpWidgetName))
				{
					getFirstToCommentContainer(tmpWidgetName).style.display = 'block';
				}	
			}
			
			refreshWidgetContainerScrollerForObjectId(getMainCommentContainer(tmpWidgetName));
			
			if (typeof(flushCommentsWidgetClientCaches) == "function")
				flushCommentsWidgetClientCaches();
		}
		else
		{
			handleErrorResponseMessage('handleCommentDeletionResponse');
		}
	
	}

	function handleNewCommentFormResponse()
	{
		if (ajaxResponseArray.success)
		{
			var tmpWidgetName = getAjaxResponseValue('widgetName');
			var tmpBlogId = ajaxResponseArray.values['blogId'];
			// Show the new comment form
			
			var newCommentContainer = undefined;
			if (tmpBlogId)
				newCommentContainer = getNewCommentFormContainer(tmpWidgetName, tmpBlogId);
			else
				newCommentContainer = getNewCommentFormContainer(tmpWidgetName);
				
			if (newCommentContainer)
			{
				replaceActiveCommentFormContainer(newCommentContainer, tmpWidgetName);			
			
				if (!tmpBlogId)
				{
					var mainContainer = getMainCommentContainer(tmpWidgetName);
					if (mainContainer)
					{
						mainContainer.style.display = 'none';
					}
				}
			
				newCommentContainer.innerHTML = getAjaxResponseHtml(tmpWidgetName);
				clearAjaxResponseHtml(tmpWidgetName);
				newCommentContainer.style.display = 'block';
				
				refreshWidgetContainerScrollerForObjectId(newCommentContainer);
			}
		}
		else 
		{
			handleErrorResponseMessage('handleNewCommentFormResponse');
		}
	}
	
	function handleDisplayCommentBackFormResponse() 
	{
		if (ajaxResponseArray.success)
		{
			var tmpWidgetName = getAjaxResponseValue('widgetName');
		
			// Show the comment back form
			var commentBackContainer = getCommentBackFormContainer(tmpWidgetName);
			if (commentBackContainer)
			{
				replaceActiveCommentFormContainer(commentBackContainer, tmpWidgetName);			
			
				var mainContainer = getMainCommentContainer(tmpWidgetName);
				if (mainContainer)
					mainContainer.style.display = 'none';
			
				commentBackContainer.innerHTML = getAjaxResponseHtml(tmpWidgetName);
				clearAjaxResponseHtml(tmpWidgetName);
				commentBackContainer.style.display = 'block';
				
				refreshWidgetContainerScrollerForObjectId(commentBackContainer);
			}
		}
		else 
		{
			handleErrorResponseMessage('handleDisplayCommentBackFormResponse');
		}
	}
	
	function handleNewBlogCommentResponse()
	{
		if (ajaxResponseArray.success)
		{
			var tmpBlogId = getAjaxResponseValue('blogId');
			var tmpWidgetName = getAjaxResponseValue('widgetName'); 
		
			if (responseEquals('Your comment was successful and is currently awaiting approval'))
			{
				notify(ajaxResponseArray.message);
			}
			else
			{
				// Hide comment box
				var newCommentContainer = getNewCommentFormContainer(tmpWidgetName, tmpBlogId);
				if (newCommentContainer)
				{
					newCommentContainer.style.display = 'none';
				}
			
				var expander = document.getElementById('blogItem' + tmpBlogId + '_expanded');
				var expanderLink = document.getElementById('blogItem' + tmpBlogId + '_comments');
				var expanderRefreshUrl = document.getElementById('blogItem' + tmpBlogId + '_expanderUrl');
				
				if (expander && expanderLink && expanderRefreshUrl)
				{
					if (expander.innerHTML == '')
					{
						expandForContent(expanderRefreshUrl.value, expanderLink);
					}
					else
					{
						// Already expanded - Reload expander content
						getDataToExpand(expanderRefreshUrl.value,'blogItem' + tmpBlogId + '_expanded'); 
						slideit('blogItem' + tmpBlogId + '_expanded',dataContainerHeight,1,500);
						
						// Need to unhide 'commentListingContainer_blogs'
					}
				}
				else
				{
					alert('blogItem' + tmpBlogId + '_expanded not found');
				}
			
				flushCommentsWidgetClientCaches();
			}
		}
		else
		{
			handleErrorResponseMessage('handleNewCommentResponse');
		}
	}
	
	function handleNewCommentResponse()
	{
		if (ajaxResponseArray.success)
		{
			var tmpWidgetName = getAjaxResponseValue('widgetName');
			
			// Hide the new comment form
			var newCommentContainer = getNewCommentFormContainer(tmpWidgetName);
			if (newCommentContainer)
			{
				newCommentContainer.style.display = 'none';
			}

			// Re-display the main container
			var mainContainer = getMainCommentContainer(tmpWidgetName);
			if (mainContainer)
				mainContainer.style.display = 'block';
			
			// If already approved, render the new comment on the page
			if (responseEquals('Your comment was successful and is currently awaiting approval'))
			{
				notify(ajaxResponseArray.message);
			}
			else
			{
				/* TODO Leon: Detect whether we're at the correct location for inserting the comment
					1) Comment Widget: We need to be on the first page. Perhaps hide the target container if we're not on the first page
					2) News Widget: Always ok since there is no pagination
					3) Blogs Widget: To ensure we don't end up with duplicate target containers, use blog IDs in addition to widget name
				*/
				var tmpBlogId = ajaxResponseArray.values['blogId']
				var commentContainer;
				
				// Insert comment into page
				if (tmpBlogId)
					commentContainer = getNewCommentContainer(tmpWidgetName + '_' + tmpBlogId);
				else
					commentContainer = getNewCommentContainer(tmpWidgetName);
						
				if (commentContainer)
				{
					commentContainer.style.display = 'block';
					commentContainer.innerHTML = getAjaxResponseHtml(tmpWidgetName) + commentContainer.innerHTML;
					refreshWidgetContainerScrollerForObjectId(commentContainer);
				}
				
				// Remove 'be the first to post' if it is visible
				if (getExistingCommentCount(tmpWidgetName) == 0)
				{
					if (getFirstToCommentContainer(tmpWidgetName))
					{
						getFirstToCommentContainer(tmpWidgetName).style.display = 'none';
					}	
					incrementCommentCount(tmpWidgetName);
				}
				
				flushCommentsWidgetClientCaches();
			}
		}
		else
		{
			handleErrorResponseMessage('handleNewCommentResponse');
		}
	}
	
	function handleCommentBackResponse()
	{
		if (ajaxResponseArray.success)
		{
			var userLink = document.getElementById('commentBackUserLink').innerHTML;
			cancelCommentBack();
			
			confirmCommentChoice('View your comment on this user\'s profile?','navigateToUserLink()');
		}
		else
		{
			handleErrorResponseMessage('handleCommentBackResponse');
		}
	}		
	
	function navigateToUserLink()
	{
		var userLink = document.getElementById('commentBackUserLink').innerHTML;
		
		if (userLink)
			document.location.href=userLink;
		else
			alert('Error in handleCommentBackResponse. Could not locate the user\'s link');
	}

	function handleReplyResponse()
	{
		if (ajaxResponseArray.success)
		{
			var tmpWidgetName = getAjaxResponseValue('widgetName');
		
			// Hide the reply form
			var replyContainer = getReplyFormContainer(getCommentIdFromResponse());
			if (replyContainer)
			{
				replyContainer.innerHTML = getAjaxResponseHtml(tmpWidgetName);
				replyContainer.style.display = 'none';
			}
			
			// Render the new reply
			var newReplyContainer = document.getElementById('newCommentReplyContainer'+getCommentIdFromResponse())
			if (newReplyContainer)
			{
				newReplyContainer.innerHTML += getAjaxResponseHtml(tmpWidgetName);
				newReplyContainer.style.display = 'block';
				refreshWidgetContainerScrollerForObjectId(newReplyContainer);
			}
			
			if (responseEquals('Your comment was successful and is currently awaiting approval'))
			{
				notify(ajaxResponseArray.messages);
			}
			
			clearAjaxResponseHtml(tmpWidgetName);
			
			resetActionSelectBox(getCommentIdFromResponse());
			
			flushCommentsWidgetClientCaches();
			
		}
		else {
			// Write failure code
			handleDisplayReplyFormResponse();
		}
	}

	function handleDisplayReplyFormResponse()
	{
		if (ajaxResponseArray.success)
		{
			var tmpWidgetName = getAjaxResponseValue('widgetName');
		
			// Show the reply form
			var replyContainer = getReplyFormContainer(getCommentId());
			if (replyContainer)
			{
				replaceActiveCommentFormContainer(replyContainer,tmpWidgetName);			
			
				replyContainer.innerHTML = getAjaxResponseHtml(tmpWidgetName);
				clearAjaxResponseHtml(tmpWidgetName);
				replyContainer.style.display = 'block';
				var commentContainer = getExistingCommentContainer(getCommentId())
				var commentScroller = getMainCommentContainer();
				if (commentScroller.scrollUpdate)
					commentScroller.scrollUpdate();
				if(commentScroller.scrollToElement) 
					commentScroller.scrollToElement(commentContainer);
					
				refreshWidgetContainerScrollerForObjectId(replyContainer);
			}
		}
		else 
		{
			handleErrorResponseMessage('handleDisplayReplyFormResponse');
		}
	}
	
	function handleErrorResponseMessage(sourceFunction, callbackFunction)
	{
		if (callbackFunction)
			callbackFunction();
	
		if (ajaxResponseArray.responseCode == 'Login required')
		{
			login();
		}
		else if (ajaxResponseArray.message && ajaxResponseArray.message != '')
		{
			notify(ajaxResponseArray.message, ajaxResponseArray.title);
		}
		else
		{
			notify(ajaxResponseArray.responseCode);
		}
		
		if (getCommentId())
			resetActionSelectBox(getCommentId())
	}
	
	function handleTrustBlockUserResponse()
	{
		if (ajaxResponseArray.success)
			refreshCommentPage();
		else
		{
			notify('trust/block failed');
		}
	}
		
/**
	Start Comment utils methods
**/

	var activeFormContainer = null;

	/**
		Maintain comment submission forms by closing the previously active
		form container and storing the new form container as active
	**/
	function replaceActiveCommentFormContainer(formDivContainer,widgetName)
	{
		if (formDivContainer)
		{
			if (activeFormContainer != null)
			{
				if (activeFormContainer[widgetName] != null && activeFormContainer[widgetName].id != formDivContainer.id)
					activeFormContainer[widgetName].style.display = 'none';
			}
			else
				activeFormContainer = new Array();
				
			activeFormContainer[widgetName] = formDivContainer;
		}
	}

	function getNewCommentCaptcha(imageObject)
	{
		if (imageObject)
		{
			var tmprnd = Math.floor(Math.random()*10000);
			imageObject.setAttribute('src', '/srv/Captcha.do?rnd=' + tmprnd);
		}
	}
	
	/**
	TODO Leon: Remove when cleaning
	*/
	function responseEquals(comparedKey)
	{
		if (ajaxResponseArray.responseCode == comparedKey)
		{
			return true;
		}
		
		return false;
	}

	function resetActionSelectBox(commentId)
	{
		if (document.getElementById('commentActionSelectBox_'+commentId))
			document.getElementById('commentActionSelectBox_'+commentId)[0].selected = true;
	}
	
	function hideCommentEntry(commentId)
	{
		var commentContainer = document.getElementById('commentContainer'+commentId);
		if (commentContainer)
		{
			commentContainer.style.display = 'none';
		}
	}
	
	function replaceCommentActionDropdownItem(commentId, originalCommand, originalName, newCommand, newName)
	{
		var selectElement = document.getElementById('commentActionSelectBox_'+commentId);
		for(var i=0; i < selectElement.options.length; i++)
		{
			if (selectElement.options[i].value == originalCommand)
			{
				selectElement.options[i].value = newCommand;
				selectElement.options[i].innerHTML = newName;
			}
		}
	}
	
	function extractCommentIdFromSelectBoxId(selectBox)
	{
		var commentId = 0;
		var selectBoxId = selectBox.id;
		var selectBoxIdSplit = selectBoxId.split('_');
	
		if (selectBoxId && selectBoxIdSplit.length >= 2)
		{
			commentId = selectBoxIdSplit[selectBoxIdSplit.length - 1]
		}
		
		return commentId;
	}
	
	function resetAjaxResponseArray()
	{
		ajaxResponseArray = new Array();
	}
	
	function resetCommentFormVariables(widgetName)
	{
		if (!widgetName)
		{
			setCommentCommand('');
			setApprovalMode('');
			setCommentId('');
			setCommentText('');
			setCaptchaCode('');
		}
		else
		{
			setCommentCommand('',widgetName);
			setApprovalMode('',widgetName);
			setCommentId('',widgetName);
			setCommentText('',widgetName);
			setCaptchaCode('',widgetName);
		}
	}
	
	function getAjaxResponseHtml(widgetName) 
	{
		if (!widgetName)
			return document.getElementById('commentAjaxResponse').innerHTML;
		else
			return document.getElementById('commentAjaxResponse_' + widgetName).innerHTML;
	}
	
	function clearAjaxResponseHtml(widgetName)
	{
		if (!widgetName)
			document.getElementById('commentAjaxResponse').innerHTML = '';
		else
			document.getElementById('commentAjaxResponse_' + widgetName).innerHTML = '';
	}
	

	function confirmCommentChoice(question, execOnYes)
	{
		popModal(question,400,200,'Bandit.fm Comments','yesNo',execOnYes);
	}

	/**
		Receives either a single string or an array of strings to 
		communicate to the user
	**/
	function notify(message, title)
	{
		if (!message || (message.length && message.length == 0))
			return;
	
		if (message instanceof Array)
		{
			var messageText = '';
			for (key in message)
			{
				messageText += message[key] + '\n';
			}
			
			alert(messageText);
		}
		else if (title)
		{
			popModal(message,400,200,'Bandit.fm Comments - ' + title,'ok',undefined)
		}
		else
		{
			popModal(message,400,200,'Bandit.fm Comments','ok',undefined)
		}
	}
	
	function getFormForWidgetName(widgetName)
	{
		if (widgetName)
			return document.getElementById('commentFormId_'+widgetName);
		else
			return document.getElementById('commentFormId');
	}
	
	function getWidgetNameFromResponse()
	{
		return getAjaxResponseValue('widgetName')
	}
	
	function getAjaxResponseValue(valueName)
	{
		if (ajaxResponseArray && ajaxResponseArray.values && ajaxResponseArray.values[valueName])
			return ajaxResponseArray.values[valueName];
		else
			return undefined;
	}
	
/**
	Start form value getters and setters
**/
	
	function getCommentIdFromResponse()
	{
		return getAjaxResponseValue('commentId');
	}
	
	function setCommentCommand(commandName, widgetName)
	{
		if (!widgetName)
			document.getElementById('commandInputId').value = commandName;
		else
		{
			var widgetFrm = getFormForWidgetName(widgetName);
			widgetFrm.command.value = commandName;
		}
	}
	
	function setWidgetName(widgetName)
	{
		var widgetFrm = getFormForWidgetName(widgetName);
		widgetFrm.widgetName.value = widgetName;
		//document.getElementById('widgetNameId').value = widgetName;
	}
	
	function getCurrentCommand(widgetName)
	{
		var widgetFrm = getFormForWidgetName(widgetName);
		return widgetFrm.command.value;
		//return document.getElementById('commandInputId').value;
	}
	
	function setApprovalMode(mode)
	{
		if (document.getElementById('commentApprovalModeInputId'))
			document.getElementById('commentApprovalModeInputId').value = mode;
	}
	
	function setCommentId(commentId,widgetName)
	{
		document.getElementById('commentIdInputId').value = commentId;
		var widgetFrm = getFormForWidgetName(widgetName);
		if (!widgetFrm)
			document.getElementById('commentIdInputId').value = commentId;
		else
		{
			widgetFrm['comment.commentId'].value = commentId;
		}
	}
	
	function getCommentId(widgetName)
	{
		var widgetFrm = getFormForWidgetName(widgetName);
		if (widgetFrm)
			return widgetFrm['comment.commentId'].value;
		else
			return document.getElementById('commentIdInputId').value;
	}

	function setCommentText(commentText,widgetName)
	{
		var widgetFrm = getFormForWidgetName(widgetName);
		if (!widgetFrm)
			document.getElementById('commentTextInputId').value = commentText;
		else
		{
			widgetFrm['comment.comment'].value = commentText;
		}
	}
	
	function setObjectId(objectId, widgetName)
	{
		var widgetFrm = getFormForWidgetName(widgetName);
		if (!widgetFrm)
			document.getElementById('commentObjectId').value = objectId;
		else
		{
			widgetFrm['comment.objectId'].value = objectId;
		}
	}
		
	function setCaptchaCode(captchaCode, widgetName)
	{
		var widgetFrm = getFormForWidgetName(widgetName);
		if (!widgetFrm)
		{
			document.getElementById('commentCaptchaInputId').value = captchaCode;
		}
		else if (widgetFrm['verificationCode'])
		{
			widgetFrm['verificationCode'].value = captchaCode;
		}
	}

	function getCommentBackFormContainer(widgetName)
	{
		if (!widgetName)
			return document.getElementById('commentBackFormContainer');
		else
			return document.getElementById('commentBackFormContainer_' + widgetName);
	}
	
	function getNewCommentFormContainer(widgetName, optionalObjectId)
	{
		if (!widgetName)
			return document.getElementById('newCommentFormContainer');
		else if (optionalObjectId)
			return document.getElementById('newCommentFormContainer_' + widgetName + '_' + optionalObjectId);
		else
			return document.getElementById('newCommentFormContainer_' + widgetName);
	}		
	
	function getNewCommentContainer(containerNameSuffix)
	{
		return document.getElementById('newCommentContainer_' + containerNameSuffix);
	}
	
	function getExistingCommentContainer(commentId)
	{
		return document.getElementById('commentContainer'+commentId);
	}
	
	/**
		TODO Leon: Follow up james on scrollingArea fix across the board.
	**/
	function getMainCommentContainer(widgetName)
	{
		if (!widgetName)
			return document.getElementById('scrollingArea_commentsWidget');
		else 
		{
			return document.getElementById('commentListingContainer_'+ widgetName);
		}
	}

	function getReplyFormContainer(commentId)
	{
		return document.getElementById('commentReplyFormContainer' + commentId);
	}
	
	function getExistingCommentCount(widgetName)
	{
		if (document.getElementById('commentCount_'+widgetName))
			return document.getElementById('commentCount_'+widgetName).value
		
		return 0;
	}
	
	function incrementCommentCount(widgetName)
	{
		if (document.getElementById('commentCount_'+widgetName))
			(document.getElementById('commentCount_'+widgetName).value)++;
		/*else
			alert('commentCount_'+widgetName + ' not found');*/
	}
	
	function decrementCommentCount(widgetName)
	{
		if (document.getElementById('commentCount_'+widgetName))
			(document.getElementById('commentCount_'+widgetName).value)--;
		/*else
			alert('commentCount_'+widgetName + ' not found');*/
	}
	
	function getFirstToCommentContainer(widgetName)
	{
		if (document.getElementById('firstToComment_'+widgetName))
			return document.getElementById('firstToComment_'+widgetName);
		/*else
			alert('firstToComment_'+widgetName + ' not found');*/
	}


	// Block of code to bypass standard toggling of link used to expand / collapse comments section.
	// The on click event of the link must invoke the "pre" and "post" functions respectively before and after 
	// the "core" method it's triggered.
	// A tag named "spanWithExpandCollapseArrow_"+id must be defined, and its style class is toggled between
	// "arrowOnRight" and "arrowOnRightUp"

	var switchExpandArrowNeedCheck = true;

	function switchExpandArrowPre(id)
	{
		obj = document.getElementById("spanWithExpandCollapseArrow_"+id);
		s = obj.className;
		if (s.indexOf("Up") < 0)
		{
			obj.className = "arrowOnRightUp";
			switchExpandArrowNeedCheck = false;
		}
	}
	function switchExpandArrowPost(id)
	{
		if (switchExpandArrowNeedCheck)
		{
			obj = document.getElementById("spanWithExpandCollapseArrow_"+id);
			obj.className = "arrowOnRight";
		}
		switchExpandArrowNeedCheck = true;
	}
