function getBBTextSelection(textareaId) {
	var textarea = document.getElementById(textareaId);

	if (document.selection && !window.opera) {	// IE - from http://the-stickman.com/
		// The current selection
		var range = document.selection.createRange();
		// We'll use this as a 'dummy'
		var stored_range = range.duplicate();
		// Select all text
		stored_range.moveToElementText(textarea);
		// Now move 'dummy' end point to end point of original range
		stored_range.setEndPoint('EndToEnd', range);
		// Now we can calculate start and end points
		textarea.selectionStart = stored_range.text.length - range.text.length;
		textarea.selectionEnd = textarea.selectionStart + range.text.length;
	}

	var oldSelectionStart = textarea.selectionStart;
	var oldSelectionEnd = textarea.selectionEnd;
	var selectedText = textarea.value.substring(oldSelectionStart,oldSelectionEnd);

	// alert(oldSelectionStart);
	// alert(oldSelectionEnd);
	// alert(selectedText);
	return [selectedText,oldSelectionStart,oldSelectionEnd];
}
function bracketSelection(selection, start, end,textareaId) {
	var textarea = document.getElementById(textareaId);
	var newText = start + selection[0] + end;
	oldSelectionStart = selection[1];
	oldSelectionEnd = selection[2];

	textarea.focus();
	if (document.selection && !window.opera) {
		sel = document.selection.createRange();
		sel.text = newText;
		sel.collapse(false);
		sel.moveStart("character", newText.length);
		sel.moveEnd("character", newText.length);
		sel.select();
	} else {
		textarea.value = textarea.value.substring(0, oldSelectionStart) + newText + textarea.value.substring(oldSelectionEnd);
		textarea.setSelectionRange(oldSelectionStart + newText.length,oldSelectionStart + newText.length);
	}
	textarea.focus();
}

function makeStyle(textareaId,style1,style2) {
	var selection = getBBTextSelection(textareaId);
	if (selection[0] == '') {
		// insert start or end tag in text
		chooseTagToAdd(style1,style2,textareaId);
	} else {
		// replace selected text with the same enclosed with tags
		bracketSelection(selection,style1,style2,textareaId);
	}
}


function chooseTagToAdd(tag1,tag2,textareaId) {
	var selection = getBBTextSelection(textareaId);
	var textarea = document.getElementById(textareaId);
	var text = textarea.value.substring(0,selection[1]);
	var lastClose = text.lastIndexOf(tag2);
	var lastOpen = text.lastIndexOf(tag1);
	if (lastOpen < lastClose && lastOpen != -1 && lastClose != -1) {
		// add [b]
		addText(tag1,textareaId)
	} else if (lastOpen > lastClose || lastClose != -1) {
		// add [/b]
		addText(tag2,textareaId)
	} else {
		// add [b]
		addText(tag1,textareaId)
	}
}
function addText(text,textareaId) {
	var selection = getBBTextSelection(textareaId);
	var textarea = document.getElementById(textareaId);


	textarea.focus();
	if (document.selection && !window.opera) {
		sel = document.selection.createRange();
		sel.text = text;
		sel.collapse(false);
		sel.moveStart("character", 0);
		sel.moveEnd("character", 0);
		sel.select();
	} else {
		var newText = textarea.value.substr(0,selection[1]) + text + textarea.value.substr(selection[1]);
		textarea.value = newText;
		textarea.setSelectionRange(selection[1] + text.length,selection[1] + text.length);
	}
	// textarea.focus();
}
