// Ver 1.1.100202 mod 3dfc
// Required: ajax.js

var forum_ajax_mainprocessor = "forum.phtml";
var forum_ajax_container = "body1";
var forum_form_id = "forumform";

/* Basic AJAX constructors */

function forumGetAjaxContainer(action, actionclass) {

	var WorkArea = new ajaxContainer(forum_ajax_container, forum_ajax_mainprocessor, action, actionclass);

	if (document.getElementById(forum_form_id))
		WorkArea.setForm(document.getElementById(forum_form_id));

	WorkArea.setOnLoad(hideLoading);

	return WorkArea;

}

function forumGetAjaxInterface(action, actionclass) {

	var Ajax = new ajaxInterface(action, actionclass, forum_ajax_mainprocessor);

	if (document.getElementById(forum_form_id))
		Ajax.parseForm(document.getElementById(forum_form_id));

	return Ajax;

}

/* Login/Logoff operations */

function forumLogin(form) {

	if (!isCookieEnabled()) {

		showContextMessage("loginmsg", "Your browser does not support Cookies. Please enable Cookies in your browser properies.", 10);

		return false;

	}

	showLoading();

	var Ajax = new ajaxInterface("login", "user", forum_ajax_mainprocessor);

	Ajax.parseForm(form);
	Ajax.setErrorHandler(function(text) { showContextMessage("loginmsg", text, 5); });
	Ajax.setOnFinish(forumOnLoggedIn);
	Ajax.doRequest();

	return false;

}

function forumOnLoggedIn() {

	forumLoadMenu();
	forumRefresh();

}

function forumLoadMenu() {

	if (!document.getElementById("menu_top"))
		return;

	var Menu = new ajaxContainer("menu_top", forum_ajax_mainprocessor, "menu", "user");

	Menu.setOnLoad(hideLoading);
	Menu.loadData();

}

function forumLogoff(form) {

	showLoading();

	var Ajax = new ajaxInterface("logoff", "user", forum_ajax_mainprocessor);

	Ajax.parseForm(form);
	Ajax.setOnFinish(forumOnLoggedOff);
	Ajax.doRequest();

	return false;

}

function forumOnLoggedOff() {

	hideLoading();

	window.location.href = "/";

}

/* Forums operations */

function forumShowForums() {

	showLoading();

	var WorkArea = forumGetAjaxContainer("list", "forums");

	WorkArea.loadData();

}

function forumRefresh() {

	var form = document.getElementById(forum_form_id);

	if (!form)
		return;

	if (!form.mode)
		return;

	var mode = form.mode.value;

	if (mode == "posts")
		forumShowPosts(0, form.page_posts ? form.page_posts.value : 0);

	if (mode == "topics")
		forumShowTopics(0, form.page_topics ? form.page_topics.value : 0);

	forumRedraw();

}

function forumRedraw() {

	// IE8 redraw bug fix

	document.body.style.display = "none";
	document.body.style.display = "block";

}

/* Topics operations */

function forumShowTopics(forumid, page) {

	showLoading();

	var WorkArea = forumGetAjaxContainer("list", "topics");

	if (forumid)
		WorkArea.setParam("forumid", forumid);

	WorkArea.setParam("page_topics", page ? page : 0);
	WorkArea.loadData();

}

function forumNewTopic() {

	var WorkArea = forumGetAjaxContainer("add", "topics");

	WorkArea.setOnLoad(function() { forumShowEditor(true); });
	WorkArea.loadData();

}

function forumCloseTopicEditor() {

	var form = document.getElementById("forumform");

	if (!form)
		return;

	forumShowEditor(false);
	forumShowTopics(0, form.page_topics.value)

}

function forumPostTopic(src, ste) {

	forumDisableButton(true, "apply_topic_button");

	if (ste)
		ste.submit();

	showLoading();

	var el = document.getElementById(src);

	if (!el)
		return false;


	var Ajax = forumGetAjaxInterface("submit", "topics");

	Ajax.setErrorHandler(function(text) { forumDisableButton(false, "apply_topic_button"); showContextMessage("topicmsg", text, 6); });
	Ajax.setOnFinish(function(res, doc) { window.location.href = forum_ajax_mainprocessor + "?forumid=" + getNodeAttribute(doc, "forumid") + "&topicid=" + getNodeAttribute(doc, "topicid"); });
	Ajax.doRequest();

}

function forumCancelEditTopic() {

	forumRefresh();

}

function forumEditTopic(id) {

	showLoading();

	var Container = new ajaxContainer("topic_container" + id, forum_ajax_mainprocessor, "edit", "topics");

	if (document.getElementById(forum_form_id))
		Container.setForm(document.getElementById(forum_form_id));

	Container.setParam("topicid", id);
	Container.setOnLoad(hideLoading);
	Container.loadData();

}

function forumApplyEditTopic(id) {

	var form = document.getElementById(forum_form_id);

	if (!form)
		return;

	forumDisableButton(true, "apply_topic_button" + id);

	var Ajax = forumGetAjaxInterface("apply_edit", "topics");

	Ajax.setParam("topicid", id);
	Ajax.setParam("title", document.getElementById("topictitle" + id).value)
	Ajax.setParam("text", document.getElementById("topicedit" + id).value)
	Ajax.setErrorHandler(function(text) { forumDisableButton(false, "apply_topic_button" + id); showContextMessage("topicedit" + id + "msg", text, 6); });
	Ajax.setOnFinish(function() { forumRefresh(); });
	Ajax.doRequest();

}

function forumDeleteTopic(id) {

	showContextMessage("delete_topic_confirm" + id, "", 10);

}

function forumApplyDeleteTopic(id) {

	var form = document.getElementById(forum_form_id);

	if (!form)
		return;

	var Ajax = forumGetAjaxInterface("apply_delete", "topics");

	Ajax.setParam("topicid", id);
	Ajax.setOnFinish(function() { forumShowTopics(0, form.page_topics.value); });
	Ajax.doRequest();

}

/* Posts operations */

function forumShowPosts(topicid, page, containerName) {

	showLoading();

	var WorkArea = forumGetAjaxContainer("list", "posts");

	if (topicid)
		WorkArea.setParam("topicid", topicid);

	if (containerName) {

		WorkArea.setContainer(containerName);
		WorkArea.setAction("list_box");

	}

	WorkArea.setParam("page_posts", page ? page : 0);
	WorkArea.loadData();

}

function forumRefreshPosts() {

	var form = document.getElementById(forum_form_id);

	if (!form)
		return;

	forumShowPosts(0, form.page_posts.value, "list_box1");

}

function forumDisableButton(status, name) {

	var postbutton = document.getElementById(name);

	if (postbutton)
		postbutton.disabled = status;

}

function forumPostReply(src, ste) {

	forumDisableButton(true, "apply_reply_button");

	if (ste)
		ste.submit();

	showLoading();

	var el = document.getElementById(src);

	if (!el)
		return false;


	var Ajax = forumGetAjaxInterface("submit", "posts");

	Ajax.setErrorHandler(function(text) { forumDisableButton(false, "apply_reply_button"); showContextMessage("postmsg", text, 6); });
	Ajax.setOnFinish(function() { forumShowPosts(0, 'last'); });
	Ajax.doRequest();

}

function forumEditPost(id) {

	showLoading();

	var Container = new ajaxContainer("post_container" + id, forum_ajax_mainprocessor, "edit", "posts");

	if (document.getElementById(forum_form_id))
		Container.setForm(document.getElementById(forum_form_id));

	Container.setParam("postid", id);
	Container.setOnLoad(hideLoading);
	Container.loadData();

}

function forumApplyEditPost(id) {

	var form = document.getElementById(forum_form_id);

	if (!form)
		return;

	forumDisableButton(true, "apply_post_button" + id);

	var Ajax = forumGetAjaxInterface("apply_edit", "posts");

	Ajax.setParam("postid", id);
	Ajax.setParam("text", document.getElementById("postedit" + id).value)
	Ajax.setErrorHandler(function(text) { forumDisableButton(false, "apply_post_button" + id); showContextMessage("postedit" + id + "msg", text, 6); });
	Ajax.setOnFinish(function() { forumRefreshPosts(); });
	Ajax.doRequest();

}

function forumDeletePost(id) {

	showContextMessage("delete_post_confirm" + id, "", 10);

}

function forumApplyDeletePost(id) {

	var Ajax = forumGetAjaxInterface("apply_delete", "posts");

	Ajax.setParam("postid", id);
	Ajax.setOnFinish(function() { forumRefreshPosts(); });
	Ajax.doRequest();

}

function forumCancelEditPost() {

	forumRefreshPosts();

}

/* Editor */

function forumQuoteFrom(id, type, ste) {

	var text = document.getElementById(type + id);
	var author = document.getElementById(type + "author" + id);

	if (!text || !author)
		return;

	if (!ste)
		return;

	ste.execCommand("quote", "<i><b>" + getObjInnerText(author) + ":</b><br>" + getObjInnerText(text) + "</i>");
	scrollBottom();

}

function forumShowEditor(visible) {

	var editor = document.getElementById("flash_editor");
	var viewer = document.getElementById("flash_viewer");

	if (!editor)
		return;

	editor.style.display = visible ? "block" : "none";
	viewer.style.display = visible ? "none" : "block";

}

