// Start of control topic part. This part is needed to control topics - move and delete messages.

// This function activates the glow of a forum message field. This occurs as the mouse moves over the field.
function activateForumMessageGlow(messageId){
	document.body.style.cursor = 'pointer';
	topField = document.getElementById('forumLeftTop'+messageId);
	middleField = document.getElementById('forumLeftMiddle'+messageId);

	if (isForumMessageSelected(messageId)) {
		topField.style.backgroundColor = '#2d88b5';
		middleField.style.backgroundColor = '#2d88b5';
	} else {
		if (isForumMessagePostedByUser(messageId)) {
			topField.style.backgroundColor = '#cccccc';
			middleField.style.backgroundColor = '#cccccc';
		} else {
			topField.style.backgroundColor = '#dddddd';
			middleField.style.backgroundColor = '#dddddd';
		}
	}
}

// This function deactivates the glow of a forum message field. This occurs as the mouse moves away from the field.
function deactivateForumMessageGlow(messageId){
	document.body.style.cursor = 'auto';
	topField = document.getElementById('forumLeftTop'+messageId);
	middleField = document.getElementById('forumLeftMiddle'+messageId);

	if (isForumMessageSelected(messageId)) {
		topField.style.backgroundColor = '#3399cc';
		middleField.style.backgroundColor = '#3399cc';
	} else {
		if (isForumMessagePostedByUser(messageId)) {
			topField.style.backgroundColor = '#eeeeee';
			middleField.style.backgroundColor = '#eeeeee';
		} else {
			topField.style.backgroundColor = '#ffffff';
			middleField.style.backgroundColor = '#ffffff';
		}
	}
}

// This function selects/unselects a forum message.
function changeForumMessageSelect(messageId){
	isSelectedField = document.getElementById('isSelected'+messageId);
	topField = document.getElementById('forumLeftTop'+messageId);
	middleField = document.getElementById('forumLeftMiddle'+messageId);

	if (isSelectedField.value == '1') {
		isSelectedField.value = '0';
		if (isForumMessagePostedByUser(messageId)) {
			topField.style.backgroundColor = '#cccccc';
			middleField.style.backgroundColor = '#cccccc';
		} else {
			topField.style.backgroundColor = '#dddddd';
			middleField.style.backgroundColor = '#dddddd';
		}
	} else {
		isSelectedField.value = '1';
		topField.style.backgroundColor = '#2d88b5';
		middleField.style.backgroundColor = '#2d88b5';
	}
}

// This function updates all the forum messages on color.
function updateForumMessages(){
	messageIds = document.getElementById("controlTopicMessageIds").value;
	messageIdsSplit = messageIds.split(/;/);

	for (i = 0; i < messageIdsSplit.length; i++) {
		updateForumMessage(messageIdsSplit[i]);
	}
}

// This function updates a single forum message on color.
function updateForumMessage(messageId){
	isSelectedField = document.getElementById('isSelected'+messageId);
	topField = document.getElementById('forumLeftTop'+messageId);
	middleField = document.getElementById('forumLeftMiddle'+messageId);

	if (isSelectedField.value == '1') {
		topField.style.backgroundColor = '#3399cc';
		middleField.style.backgroundColor = '#3399cc';
	} else {
		if (isForumMessagePostedByUser(messageId)) {
			topField.style.backgroundColor = '#eeeeee';
			middleField.style.backgroundColor = '#eeeeee';
		} else {
			topField.style.backgroundColor = '#ffffff';
			middleField.style.backgroundColor = '#ffffff';
		}
	}
}

// This function checks whether a forum field is selected.
function isForumMessageSelected(messageId){
	isSelectedField = document.getElementById('isSelected'+messageId);
	if (isSelectedField.value == '1')
		return true;
	return false;
}

// This function checks whether a certain forum message is posted by the current user.
function isForumMessagePostedByUser(messageId){
	isOwnPostField = document.getElementById('isOwnPost'+messageId);
	if (isOwnPostField.value == '1')
		return true;
	return false;
}

// This function updates the control topic form. This is necessary at page load and when changes are made.
function updateControlTopicForm(){
	// First we retrieve the radio button objects, so we can see which one is selected.
	actionDeleteTopic = document.getElementById("controlTopicActionDeleteTopic");
	actionRenameTopic = document.getElementById("controlTopicActionRenameTopic");
	actionDelete = document.getElementById("controlTopicActionDelete");
	actionMove = document.getElementById("controlTopicActionMove");

	// Then we retrieve the objects that might have to switch their visibility.
	renameTopicContainer = document.getElementById("controlTopicRenameTopicContainer");
	actionMoveLabelUnactive = document.getElementById("controlTopicActionMoveLabelUnactive");
	actionMoveLabelActive = document.getElementById("controlTopicActionMoveLabelActive");
	movePostsContainer = document.getElementById("controlTopicMovePostsContainer");

	// Do we need to show the topic selection form? This is the case if the "move" action is selected.
	if (actionMove.checked) {
		// First we make everything (in)visible that should be (in)visible.
		actionMoveLabelUnactive.style.display = "none";
		actionMoveLabelActive.style.display = "block";
		movePostsContainer.style.display = "block";

		// Now we make sure that also the right topic dropdown list is being showed. We first check which subject is selected. Then we get all the present subjects, and make sure all are invisible, except for the selected one.
		forumSubject = document.getElementById("controlTopicForumSubject");
		forumSubjectValue = forumSubject.options[forumSubject.selectedIndex].value;

		forumSubjectIds = document.getElementById("controlTopicForumSubjectIds").value;
		forumSubjectIdsSplit = forumSubjectIds.split(/;/);

		for (i = 0; i < forumSubjectIdsSplit.length; i++) {
			if (forumSubjectIdsSplit[i] == forumSubjectValue) {
				document.getElementById("controlTopicForumTopic" + forumSubjectIdsSplit[i]).style.display = 'block';
				document.getElementById("controlTopicForumTopicLabel" + forumSubjectIdsSplit[i]).style.display = 'block';
			} else {
				document.getElementById("controlTopicForumTopic" + forumSubjectIdsSplit[i]).style.display = 'none';
				document.getElementById("controlTopicForumTopicLabel" + forumSubjectIdsSplit[i]).style.display = 'none';
			}
		}

		// If the New Topic option is selected, we display the New Topic field.
		if (document.getElementById("controlTopicForumTopic" + forumSubjectValue).value == 0) {
			document.getElementById("controlTopicNewTopicContainer").style.display = "block";
		} else {
			document.getElementById("controlTopicNewTopicContainer").style.display = "none";
		}
	} else {
		// If the action isn't equal to move, then we simply display nothing.
		actionMoveLabelUnactive.style.display = "block";
		actionMoveLabelActive.style.display = "none";
		movePostsContainer.style.display = "none";
	}

	// Now let's see if we need to show a field with which the topic can be renamed.
	if (actionRenameTopic.checked) {
		renameTopicContainer.style.display = "block";
	} else {
		renameTopicContainer.style.display = "none";
	}
}

// End of control topic block.

// Start of control subject block. This part is present to move and delete topics.

// This function updates the control subject form - it makes sure everything that needs to be displayed is displayed.
function updateControlSubjectForm(){
	actionDelete = document.getElementById("controlSubjectActionDelete");
	actionMove = document.getElementById("controlSubjectActionMove");

	actionMoveLabelUnactive = document.getElementById("controlSubjectActionMoveLabelUnactive");
	actionMoveLabelActive = document.getElementById("controlSubjectActionMoveLabelActive");
	movePostsContainer = document.getElementById("controlSubjectMovePostsContainer");

	// Do we need to show the subject selection form? This is the case if the "move" action is selected.
	if (actionMove.checked) {
		// We make everything (in)visible that should be (in)visible.
		actionMoveLabelUnactive.style.display = "none";
		actionMoveLabelActive.style.display = "block";
		movePostsContainer.style.display = "block";
	} else {
		// If the action isn't equal to move, then we simply display nothing.
		actionMoveLabelUnactive.style.display = "block";
		actionMoveLabelActive.style.display = "none";
		movePostsContainer.style.display = "none";
	}

}
