function toggleImageSrc(imageSrcRef, onIcon, offIcon) {
	try {
		imageSrcRef.src = -1 != imageSrcRef.src.indexOf(onIcon) ? offIcon : onIcon;
	} catch(e) {
		alert('Error in `toggleImageSrc`: ' + e.description);
	}
}
function showSubforumsInfo(elemRef, forumId, subForumAreaId, lastMPostsOfSubForumsAreaId, subForumsInfoAreaId, subForumsDivAreaId) {
	try {
	var subForumsDivAreaRef = $('#' + subForumsDivAreaId), closeToOpenState = -1 != elemRef.src.indexOf('plus_icon.gif');
		$('#' + subForumAreaId).toggle();
		$('#' + lastMPostsOfSubForumsAreaId).toggle();
		$('#' + subForumsInfoAreaId).toggle();
		toggleImageSrc(elemRef, 'img/icons/plus_icon.gif', 'img/icons/ofo.gif');
		if(true === closeToOpenState) {
			if('' == $.trim(subForumsDivAreaRef.html())) {
				subForumsDivAreaRef.html('<div align="center"><img src="images/load_please_wait.gif" height="42" width="42" /></div>');
				$.ajax({
					type: 'POST',
					url: 'mavara-list_forums.php',
					data: {
						parentForumId: forumId
					},
					success: function(httpResponse, status) {
						subForumsDivAreaRef.after(httpResponse).remove();
					}
				});
			}
			return true;
		}
	} catch(e) {
		alert('Error in `showSubforumsInfo`: ' + e.description);
	}
}
function addOptionToSelect(isChecked, selectId, optionValue, optionText) {
	try {
		if(true === isChecked) {
			$('#' + selectId).append($('<option></option>').attr('value', optionValue).text(optionText));
		} else {
			$('#' + selectId + ' > option[value="' + optionValue + '"]').remove();
	    }
	} catch(e) {
		alert('Error in `addOptionToSelect`: ' + e.description);
	}
}
function handleViewForumTopicRequest(find, findInText, postFormAreaId, postFormActionTypeIdStr, postFormThreadIdStr, postFormParentIdStr, postFormAncestorIdStr, postContentAreaIdStr, forumId, threadId, hitCountAreaId, hiddenRowAreaId, hiddenCellAreaId, refreshIconId, viewAllTitlesOnlyIconId, pleaseWaitMessage, refetchFromServer, requestType, withData, dataParsed, listOfThreadIdsFollows, listOfThreadIds, inQueueOperation, rootsInQueue, theNature) {
	try {
	var hiddenRowAreaRef = $('#' + hiddenRowAreaId), hiddenCellAreaRef = $('#' + hiddenCellAreaId), refreshIconRef = $('#' + refreshIconId), viewAllTitlesOnlyIconRef = $('#' + viewAllTitlesOnlyIconId);
		refetchFromServer = undefined == refetchFromServer ? false : refetchFromServer;
		withData = undefined == withData ? false : withData;
		dataParsed = undefined == dataParsed ? false : dataParsed;
		theNature = undefined == theNature ? 'APPROVED' : theNature;
		rootsInQueue = undefined == rootsInQueue ? false : rootsInQueue;
		inQueueOperation = undefined == inQueueOperation ? false : inQueueOperation;
		listOfThreadIds = undefined == listOfThreadIds ? new Array() : listOfThreadIds;
		listOfThreadIdsFollows = undefined == listOfThreadIdsFollows ? false : listOfThreadIdsFollows;
		requestType = undefined == requestType ? 'thread' : requestType;//Request is either for THREAD or POST
		if(true === hiddenRowAreaRef.is(':hidden') || true === refetchFromServer) {
			refreshIconRef.show();
			hiddenRowAreaRef.show();
			viewAllTitlesOnlyIconRef.show();
			if('' === $.trim(hiddenCellAreaRef.html()) || true === refetchFromServer) {
				hiddenCellAreaRef.html('<div align="center"><img src="images/load_please_wait.gif" alt="' + pleaseWaitMessage + '" title="' + pleaseWaitMessage + '" height="42" width="42" /></div>');
				$.ajax({
					type: 'POST',
					dataType: 'json',/*'xml', 'html', 'script', 'json'*/
					url: 'mavara-view_forum_threads.php',
					data: {
						find: find,
						forumId: forumId,
						threadId: threadId,
						withData: withData,
						theNature: theNature,
						dataParsed: dataParsed,
						find_in_text: findInText,
						requestType: requestType,
						rootsInQueue: rootsInQueue,
						postFormAreaId: postFormAreaId,
						inQueueOperation: inQueueOperation,
						postFormThreadIdStr: postFormThreadIdStr, 
						postFormParentIdStr: postFormParentIdStr, 
						postContentAreaIdStr: postContentAreaIdStr,
						postFormAncestorIdStr: postFormAncestorIdStr,
						postFormActionTypeIdStr: postFormActionTypeIdStr,
						listOfThreadIds: JSONstring.make(listOfThreadIds),
						listOfThreadIdsFollows: listOfThreadIdsFollows
					},
					success: function(httpResponse, status) {
						if(httpResponse.retCode < 0) {
							refreshIconRef.hide();
							viewAllTitlesOnlyIconRef.hide();
							hiddenRowAreaRef.hide();
							hiddenCellAreaRef.html('');
							alert(httpResponse.retCodeDesc);
							return;
						}
						hiddenCellAreaRef.html(httpResponse.retCodeDesc);
						$('#' + hitCountAreaId).html(httpResponse.numThreadHits);
						//goToBookmark('#topic_top' + threadId);
					}
				});
			}
		} else {
			refreshIconRef.hide();
			hiddenRowAreaRef.hide();
			viewAllTitlesOnlyIconRef.hide();
		}
	} catch(e) {
		alert('Error in `handleViewForumTopicRequest`: ' + e.description);
	}
}
function handlePulloutRequest(forumId, selectedThreads, classSelector, postSelectorGeneralStr, selectIcon) {
	try {
	var classSelectorRef = $('.' + classSelector);
		if(true === classSelectorRef.is(':hidden')) {
			selectedThreads.length = 0;//Empty Array
			classSelectorRef.show();
		} else {
			if(selectedThreads.length > 0) {
				for(var tempRef, stc = 0; stc < selectedThreads.length; ++stc) {
					tempRef = $('#' + postSelectorGeneralStr + selectedThreads[stc]);
					tempRef.attr('src', selectIcon);
					tempRef.attr('modeValue', 'select');
				}
			}
			classSelectorRef.hide();
			if(selectedThreads.length > 0) {
				$.ajax({
					type: 'POST',
					dataType: 'json',
					url: 'mavara-view_forum.php',
					data: {
						forumId: forumId,
						actionType: 'extract',
						selectedThreads: JSONstring.make(selectedThreads)
					},
					success: function(httpResponse, status) {
						if(httpResponse.retCode < 0) {
							alert(httpResponse.retCodeDesc);
							return;
						}
						goToBookmark(httpResponse.retCodeDesc);
					}
				});
			}
		}
	} catch(e) {
		alert('Error in `handlePulloutRequest`: ' + e.description);
	}
}
function handlePostSelectRequest(selectedThreads, threadId, selectImgAreaId, selectIcon, deselectIcon) {
	try {
	var modeVal, selectImgAreaRef = $('#' + selectImgAreaId), theIndex = $.inArray(threadId, selectedThreads);
		if('select' == (modeVal = selectImgAreaRef.attr('modeValue'))) {
			-1 == theIndex && selectedThreads.push(threadId);
			selectImgAreaRef.attr('modeValue', 'deselect');
			selectImgAreaRef.attr('src', deselectIcon);
		} else if('deselect' == modeVal) {
			selectedThreads.splice(theIndex, 1);
			selectImgAreaRef.attr('modeValue', 'select');
			selectImgAreaRef.attr('src', selectIcon);
		}
	} catch(e) {
		alert('Error in `handlePostSelectRequest`: ' + e.description);
	}
}
function handlePostButton(buttonAreaId, postFormAreaId, postContentAreaIdStr, generalFormVars) {
	/*generalFormVars = [
		postFormActionInfo: [
			postFormActionIdStr: 'postFormActionTypeIdStr',
			postFormActionValue: postFormActionValue
		],
		threadIdInfo: [
			threadIdStr: 'threadIdStr',
			threadIdValue: threadIdValue
		],
		parentIdInfo: [
			parentIdStr: 'parentIdStr',
			parentIdValue: parentIdValue
		],
		ancestorIdInfo: [
			ancestorIdStr: 'ancestorIdStr',
			ancestorIdValue: ancestorIdValue
		],
		postTitleInfo: [
			postTitleStr: 'postTitleStr',
			postTitleValue: postTitleValue
		]
	]*/
	try {
	var buttonAreaRef = $('#' + buttonAreaId), postFormRef = $('#' + postFormAreaId), postFormAction = generalFormVars['postFormActionInfo']['postFormActionValue'];
		$('#' + generalFormVars['threadIdInfo']['threadIdStr']).val(generalFormVars['threadIdInfo']['threadIdValue']);
		$('#' + generalFormVars['parentIdInfo']['parentIdStr']).val(generalFormVars['parentIdInfo']['parentIdValue']);
		$('#' + generalFormVars['ancestorIdInfo']['ancestorIdStr']).val(generalFormVars['ancestorIdInfo']['ancestorIdValue']);
		$('#' + generalFormVars['postFormActionInfo']['postFormActionIdStr']).val(postFormAction);
		switch(postFormAction) {
			case 'newTopic':
				buttonAreaRef.hide();
				postFormRef.appendTo('#newPostAreaId');
				$('#postFormTypeId').val('n');
				$('#postFormTitleId').val(undefined != generalFormVars['postTitleInfo'] ? generalFormVars['postTitleInfo']['postTitleValue'] : '');
				$('#postFormSmileyId').val('');
				$('#postFormSummaryId').val('');
				$('#postFormLockStateId').setValue('');
				$('#postFormExemptStateId').setValue('');
				$('#' + postContentAreaIdStr).val('');
				postFormRef.attr('disabled', false);
				postFormRef.show();
				break;
			case 'replyToMainPost':
				break;
			case 'replyToThisPost':
				break;
			case 'editPost':
				break;
		}
	} catch(e) {
		alert('Error in `handlePostButton`: ' + e.description);
	}
}
function closePostForm(postFormAreaId, postButtonAreaId) {
	try {
	var postFormRef = $('#' + postFormAreaId);
		postFormRef.attr('disabled', true);
		postFormRef.hide(); 
		$('#' + postButtonAreaId).show();
	} catch(e) {
		alert('Error in `closePostForm`: ' + e.description);
	}
}
function postFormSubmit(formRef, forumIdAreaId, postFormThreadIdStr) {
	try {
		$(formRef).ajaxSubmit({
			async: false,
			dataType: 'json',
			success: function(httpResponse, statusText) {
				switch(httpResponse.operation) {
					case 'newTopic':
					case 'replyToThis':
					case 'replyToMain':
						if(httpResponse.retCode < 0) {
							alert(httpResponse.retCodeDesc);
							return false;
						}
						goToBookmark($(formRef).attr('action') + '?forumId=' + $('#' + forumIdAreaId).val() + (undefined != httpResponse.threadId && 0 != httpResponse.threadId ? '&threadId=' + httpResponse.threadId : ''));
						break;
					case 'editPost':
						if(httpResponse.retCode < 0) {
							alert(httpResponse.retCodeDesc);
							return false;
						}
						goToBookmark($(formRef).attr('action') + '?forumId=' + $('#' + forumIdAreaId).val() + '&threadId=' + $('#' + postFormThreadIdStr).val());
						break;
					case 'deletePost':
						if(httpResponse.retCode < 0) {
							alert(httpResponse.retCodeDesc);
							return false;
						}
						goToBookmark($(formRef).attr('action') + '?forumId=' + $('#' + forumIdAreaId).val() + (undefined != httpResponse.threadId && 0 != httpResponse.threadId ? '&threadId=' + httpResponse.threadId : ''));
						break;
				}
			}
		});
	} catch(e) {
		alert('Error in `postFormSubmit`: ' + e.description);
	}
	return false;
}
function postFormLoad(actionType, postFormAreaId, postFormActionTypeIdStr, postFormThreadIdStr, postFormParentIdStr, postFormAncestorIdStr, postContentAreaIdStr, forumId, threadId, rootsInQueue, theNature) {
	try {
		theNature = undefined == theNature ? 'APPROVED' : theNature;
		$.ajax({
			type: 'POST',
			dataType: 'json',
			url: 'mavara-view_forum_threads.php',
			data: {
				forumId: forumId,
				threadId: threadId,
				withData: true,
				dataParsed: false,
				requestType: 'post',
				theNature: theNature,
				postFormAreaId: postFormAreaId,
				postFormThreadIdStr: postFormThreadIdStr, 
				postFormParentIdStr: postFormParentIdStr, 
				postContentAreaIdStr: postContentAreaIdStr, 
				postFormAncestorIdStr: postFormAncestorIdStr,
				postFormActionTypeIdStr: postFormActionTypeIdStr,
				rootsInQueue: undefined == rootsInQueue ? false : rootsInQueue
			},
			success: function(httpResponse, status) {
				if(httpResponse.retCode < 0) {
					alert(httpResponse.retCodeDesc);
					return;
				}
				if(undefined != httpResponse.threadInfo) {
				var postFormAreaRef = $('#' + postFormAreaId);
					postFormAreaRef.appendTo('#postTopCol' + theNature + threadId);
					postFormAreaRef.attr('disabled', false);
					postFormAreaRef.show();
					$('#postTopRow' + theNature + threadId).show();
					switch(actionType) {
						case 'editPost':
							$('#' + postFormThreadIdStr).val(threadId);
							$('#' + postFormActionTypeIdStr).val(actionType);
							$('#' + postFormParentIdStr).val(httpResponse.threadInfo['parentId']);
							$('#' + postFormAncestorIdStr).val(httpResponse.threadInfo['ancestorId']);
							$('#postFormTypeId').val(httpResponse.threadInfo['type']);
							$('#postFormTitleId').val(httpResponse.threadInfo['title']);
							$('#postFormSmileyId').val(httpResponse.threadInfo['smiley']);
							$('#postFormSummaryId').val(httpResponse.threadInfo['summary']);
							$('#' + postContentAreaIdStr).val(httpResponse.threadInfo['data']);
							$('#postFormLockStateId').setValue(httpResponse.threadInfo['lock_state']);
							$('#postFormExemptStateId').setValue(httpResponse.threadInfo['exempt_state']);
							break;
					}
					goToBookmark('#postTopRow' + theNature + threadId);
				}
			}
		});
	} catch(e) {
		alert('Error in `postFormLoad`: ' + e.description);
	}
	return;
}
function postFormPrepare(actionType, postFormAreaId, postFormActionTypeIdStr, postFormThreadIdStr, postFormParentIdStr, postFormAncestorIdStr, postContentAreaIdStr, forumId, threadId, parentId, ancestorId, deleteReason, deleteWarningMessage, theNature) {
	try {
	var postFormAreaRef = $('#' + postFormAreaId);
		theNature = undefined == theNature ? 'APPROVED' : theNature;
		deleteReason = undefined == deleteReason ? '' : deleteReason;
		deleteWarningMessage = undefined == deleteWarningMessage ? '' : deleteWarningMessage;
		$('#' + postFormActionTypeIdStr).val(actionType);
		if('replyToThis' == actionType || 'replyToMain' == actionType) {
			postFormAreaRef.appendTo('#postTopCol' + theNature + threadId);
			postFormAreaRef.attr('disabled', false);
			postFormAreaRef.show();
			$('#postTopRow' + theNature + threadId).show();
			$('#' + postFormThreadIdStr).val(0);
			$('#' + postFormAncestorIdStr).val(ancestorId);
			$('#postFormTypeId').val('n');
			$('#postFormTitleId').val('');
			$('#postFormSmileyId').val('');
			$('#postFormSummaryId').val('');
			$('#postFormLockStateId').setValue('');
			$('#postFormExemptStateId').setValue('');
			$('#' + postContentAreaIdStr).val('');
			switch(actionType) {
				case 'replyToThis':
					$('#' + postFormParentIdStr).val(threadId);
					break;
				case 'replyToMain':
					$('#' + postFormParentIdStr).val(ancestorId);
					break;
			}
		} else if('deletePost' == actionType) {
			if((theDeleteReason = window.prompt(deleteReason, deleteReason)) && true === window.confirm(deleteWarningMessage)) {
				$('#' + postFormThreadIdStr).val(threadId);
				$('#deleteReasonAreaId').val(theDeleteReason);
				$(postFormAreaRef).ajaxSubmit({
					async: false,
					dataType: 'json',
					success: function(httpResponse, statusText) {
						if(httpResponse.retCode < 0) {
							alert(httpResponse.retCodeDesc);
							return false;
						}
						goToBookmark($(postFormAreaRef).attr('action') + '?forumId=' + forumId + (undefined != httpResponse.threadId && 0 != httpResponse.threadId ? '&threadId=' + httpResponse.threadId : ''));
					}
				});
			}
		}
	} catch(e) {
		alert('Error in `postFormPrepare`: ' + e.description);
	}
	return;
}
function formInit(formId, fieldNamesValuesArray) {
	try {
	var formRef = $('#' + formId);
		formRef.show();
		formRef.formHash(fieldNamesValuesArray);
	} catch(e) {
		alert('Error in `formInit`: ' + e.description);
	}
}
function queuedPostHandler(postFormActionType, inQueueToolbarAreaId, forumId, queueId, newForumAreaId, confirmationMessage) {
	try {
		newForumId = -1;
		needConfirmation = undefined != confirmationMessage;
		if('MOVE_TO_FORUM' == postFormActionType) {
		var newForumRef = $('#' + newForumAreaId);
			newForumRef.show();
			newForumId = newForumRef.val();
		}
		if('MOVE_TO_FORUM' != postFormActionType || newForumId > 0) {
			if(false === needConfirmation || window.confirm(confirmationMessage)) {
				$.ajax({
					type: 'POST',
					dataType: 'json',
					url: 'mavara-forum_queue.php',
					data: {
						forumId: forumId,
						queueId: queueId,
						newForumId: newForumId,
						postFormActionType: postFormActionType
					},
					success: function(httpResponse, status) {
					var toolbarAreaRef = $('#' + inQueueToolbarAreaId);
						if(httpResponse.retCode < 0) {
							alert(httpResponse.retCodeDesc);
							return;
						}
						toolbarAreaRef.hide();
					}
				});
			}
		}
	} catch(e) {
		alert('Error in `queuedPostHandler`: ' + e.description);
	}
	return;
}