function $(aID) {
	return document.getElementById(aID);
}


function onTypeChanged(aSelect) {
	if (aSelect.value == "1") {
		var comment = document.getElementById("comment");
		if (comment.value.indexOf("Mozilla/") < 0 || 
		    comment.value.indexOf("Gecko/"  ) < 0 || 
		    comment.value.indexOf("Firefox/") < 0)
			comment.value += "\nUser-Agent: " + navigator.userAgent;
	}
}


function checkForm() {
	var elt = $("captha");
	if (!elt.value) {
		elt.style.backgroundColor = "yellow";
		elt.focus();
		return false;
	}
	return true;
}


function doSubmit(aMode, aPostID) {
	if (!checkForm())
		return;
	if (aMode == "delete") {
		if (!window.confirm("Are you sure to delete comment #" + aPostID + "?"))
			return;
	}
	$("mode").value = aMode;
	$("commentform").submit();
}


function doPreview() {
	if (!checkForm())
		return;
	var preMode = $("mode").value;
	$("mode").value = "preview";
	var reqText = [];
	var fields = ["author", "url", "comment", "captha", "topic", "refurl", "mode"];
	for (var i = 0; i < fields.length; i++) {
		reqText.push(fields[i] + "=" + encodeURIComponent($(fields[i]).value));
	}
	reqText = reqText.join("&");
	$("mode").value = preMode;	// restore |mode| value for input[type="submit"] button
	var callbacks = {
		onRequest: function() {
			$("comment-preview-button").disabled = true;
			$("comment-preview").innerHTML = '<img src="../common/ajax-loader.gif" width="16" height="16" alt="Now loading" />';
		},
		onSucceed: function(aResText) {
			$("comment-preview-button").disabled = false;
			$("comment-preview").innerHTML = '<h4>Preview</h4>'
			                               + '<div style="padding: 10px; border: 3px double #3333FF; opacity: 0.75;">'
			                               + aResText + '</div>';
			window.location.href = "#comment-preview";
		},
		onFailure: function() {
			$("comment-preview-button").disabled = false;
			$("comment-preview").innerHTML = '<p class="notice"><strong>ERROR</strong></p>';
		}
	};
	AjaxUpdater.submit("../modules/comment-post.php", "POST", reqText, callbacks);
}


////////////////////////////////////////////////////////////////////////////////
// Ajax Updater

var AjaxUpdater = {

	_req: null,

	_callbacks: {
		onRequest: null,
		onSucceed: null,
		onFailure: null
	},

	destroy: function() {
		if (this._req) {
			this._req.abort();
			delete this._req;
		}
	},

	submit: function(aURL, aMethod, aReqText, aCallbacks) {
		if (this._req)
			return;
		if (!aMethod)
			aMethod = "GET";
		if (aCallbacks)
			this._callbacks = aCallbacks;
		this._req = new XMLHttpRequest();
		this._req.open(aMethod, aURL, true);
		if (aMethod == "POST")
			this._req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		this._req.onreadystatechange = this;
		this._req.send(aReqText);
		if (this._callbacks.onRequest)
			this._callbacks.onRequest();
	},

	handleEvent: function(event) {
		if (this._req.readyState != 4)
			return;
		var status = 0;
		try { status = this._req.status; } catch (ex) {}
		if (status == 200) {
			if (this._callbacks.onSucceed)
				this._callbacks.onSucceed(this._req.responseText);
		}
		else {
			if (this._callbacks.onFailure)
				this._callbacks.onFailure();
		}
		delete this._req;
	}

};


////////////////////////////////////////////////////////////////////////////////
// BBCode Helper

var BBCodeHelper = {

	_textArea: null,

	init: function(aTextAreaID) {
		this._textArea = $(aTextAreaID);
	},

	destroy: function() {
		this._textArea = null;
	},

	insertTag: function(aTag) {
		var startPos  = this._textArea.selectionStart;
		var endPos    = this._textArea.selectionEnd;
		var scrollTop = this._textArea.scrollTop;
		var openTag;
		var closeTag;
		if (aTag == "url") {
			var url = window.prompt("Enter the URL", "http://");
			if (!url || url.match(/^\w+:\/\/$/))
				return;
			openTag  = "[url=" + url + "]";
			closeTag = "[/url]";
		}
		else if (aTag == "quote" && startPos == endPos) {
			var name = window.prompt("Enter the author name of the quoted comment", "");
			if (!name)
				return;
			openTag  = "[quote=" + name + "]";
			closeTag = "[/quote]";
		}
		else {
			openTag  = "[" + aTag + "]";
			closeTag = "[/" + aTag + "]";
		}
		this._textArea.value = this._textArea.value.substring(0, startPos) + openTag
		                     + this._textArea.value.substring(startPos, endPos) + closeTag
		                     + this._textArea.value.substring(endPos, this._textArea.value.length);
		var newPos = (startPos == endPos) ? startPos + openTag.length
		                                  : endPos + openTag.length + closeTag.length;
		this._textArea.focus();
		this._textArea.selectionStart = newPos;
		this._textArea.selectionEnd = newPos;
		this._textArea.scrollTop = scrollTop;
	}

};


////////////////////////////////////////////////////////////////////////////////
// entry point (does not support IE)

if ("addEventListener" in window) {
	window.addEventListener("load", function() {
		$("comment-toolbar").style.display = "";
		$("comment-preview-button").disabled = false;
		BBCodeHelper.init("comment");
	}, false);
	window.addEventListener("beforeunload", function() {
		AjaxUpdater.destroy();
		BBCodeHelper.destroy();
	}, false);
}


