function getSelection(ta) {
	ta = document.getElementById(ta);
	var bits = [ta.value,'','','']; 
	if(document.selection) {
		var vs = '#$%^%$#';
		var tr=document.selection.createRange()
		if(tr.parentElement()!=ta) return null;
		bits[2] = tr.text;
		tr.text = vs;
		fb = ta.value.split(vs);
		tr.moveStart('character',-vs.length);
		tr.text = bits[2];
		bits[1] = fb[0];
		bits[3] = fb[1];
	}else{ 
		// if(ta.selectionStart == ta.selectionEnd) return null; 
		bits=(new RegExp('([\x00-\xff]{'+ta.selectionStart+'})([\x00-\xff]{'+(ta.selectionEnd - ta.selectionStart)+'})([\x00-\xff]*)')).exec(ta.value);
	}
	bits[0] = '';
	return bits;
}

function makeBold() {
	formatText("supertext", "[B]", "[/B]");
}

function makeItalic() {
	formatText("supertext", "[I]", "[/I]");
}

function makeUnderline() {
	formatText("supertext", "[U]", "[/U]");
}

function makeImg() {
	bits = getSelection("supertext"); 
	imgSrc = window.prompt("Image Location", bits[2]);
	formatText("supertext", "[IMG]", "[/IMG]", imgSrc);
}

function makeURL() {
	bits = getSelection("supertext");
	
	loc = window.prompt("URL Location? (http://...)");
	text = window.prompt("Text to display? (Leave blank to just use the URL)", bits[2]);
	
	formatText("supertext", "[URL=" + loc + "]", "[/URL]", text);	
}

function makeColor(col) {
	if (col != "null") {
		formatText("supertext", "[COLOR=" + col + "]", "[/COLOR]");
		document.getElementById("cselect").selectedIndex = 0;
	}
}

function makeFloat(col) {
	if (col != "null") {
		formatText("supertext", "[FLOAT=" + col + "]", "[/FLOAT]");
		document.getElementById("fselect").selectedIndex = 0;
	}
}

function makeAlign(col) {
	if (col != "null") {
		formatText("supertext", "[ALIGN=" + col + "]", "[/ALIGN]");
		document.getElementById("aselect").selectedIndex = 0;
	}
}

function formatText(ta, stTag, edTag, midRep) {
	box = document.getElementById(ta);
	bits = getSelection(ta);
	
	box.value = bits[1] + stTag + ((typeof midRep == "undefined") ? bits[2] : midRep) + edTag + bits[3];
}