function getSelectedText(elementId) {
	var obj = document.getElementById( elementId );
	if (!obj) throw "element " + elementId + " not found";
	// Netscape, Mozilla, Firefox
	if (typeof(obj.selectionStart) != "undefined") 	{
		obj.focus();
		var startSelection = obj.selectionStart;
		var endSelection = obj.selectionEnd;
		if (endSelection < startSelection) {
			var temp = endSelection;
			endSelection = startSelection;
			startSelection = temp;
		}
		return (obj.value).substring(startSelection, endSelection);
	}
	// Internet Explorer
	if (obj.createTextRange) {
		obj.focus(obj.caretPos);
		obj.caretPos = document.selection.createRange().duplicate();
		return obj.caretPos.text;
	}
}

function setSelectedText(elementId, text) {
	var obj = document.getElementById(elementId);
	if (!obj) throw "element " + elementId + " not found";
	// Netscape, Mozilla, Firefox
	if (typeof(obj.selectionStart) != "undefined") {
		obj.focus();
		var startSelection = obj.selectionStart;
		var endSelection = obj.selectionEnd;
		if (endSelection < startSelection) {
			var temp = endSelection;
			endSelection = startSelection;
			startSelection = temp;
		}
		// Sad, but true: You actually have to replace *all* of the text in
		// the text input to replace the selection in Mozilla browsers!
		var startText = (obj.value).substring(0, startSelection);
		var endText = (obj.value).substring(endSelection, obj.textLength);
		obj.value = startText + text + endText;
		obj.selectionStart = startSelection;
		obj.selectionEnd = startSelection + text.length;
	}
	// Internet Explorer
	if (obj.createTextRange) {
		obj.focus(obj.caretPos);
		obj.caretPos = document.selection.createRange().duplicate();
		obj.caretPos.text = text;
		obj.caretPos.moveStart( "character", text.length * -1);
		obj.caretPos.select();
	}
	obj.focus();
}

function formatText(elementId, s) {
	var text = getSelectedText(elementId);
	var match;
	if ((s == "left") || (s == "center") || (s == "right")) {
		text = text.replace(/\[[\/]*left\]/gi, "");
		text = text.replace(/\[[\/]*center\]/gi, "");
		text = text.replace(/\[[\/]*right\]/gi, "");
		if (s != "left")
			match = "[" + s + "]" + text + "[/" + s + "]";
		else
			match = text;
		setSelectedText(elementId, match);
		return;
	}
	match = "[" + s;
	if (text.substring(0, match.length).toLowerCase() == match) {
		var r = null;
		switch (s) {
			case "quote":
				match = text.replace(/\[quote=([^\]]+)\]([^\]]+)\[\/quote\]/gi, "$2");
				break;
			case "url":
				if (text.charAt(4) == '=')
					match = text.replace(/\[url=([^\]]+)\]([^\]]+)\[\/url\]/gi, "$2");
				else
					match = text.replace(/\[url\]([^\]]+)\[\/url\]/gi, "$1");
				break;
			default:
				r = new RegExp("\\["+ s +"\\]([^\\]]+)\\[\\/"+ s +"\\]", "gi");
				match = text.replace(r, "$1");
		}
	} else {
		var a = "";
		var b = "";
		switch (s) {
			case "img":
				if (text == "")
					a = prompt("Enter the url of the image", "");
				else
					a = text;
				a = a.replace(/\b/gi, "");
				if (a == "") return;
				match = "[img]"+ a + "[/img]";
				break;
			case "quote":
				match = "[quote=someone]"+ text + "[/quote]";
				break;
			case "url":
				a = text.replace(/\b/gi, "");
				if (a == "")
					a = prompt("Enter the name of the link", text);
				else
					a = text;
				b = prompt("Enter the url of the link", "").replace(/\b/gi, "");
				if (b == "") return;
				if (a == "")
					match = "[url]"+ b + "[/url]";
				else
					match = "[url=" + b + "]"+ a + "[/url]";
				break;
			default:
				match = "[" + s + "]" + text + "[/" + s + "]";
		}
	}
	setSelectedText(elementId, match);
}
