
function AddText(bbFirst, bbLast, text, el) {
	// replaces a selection / inserts at a specific point
	var len		= el.textLength;
	var start	= el.selectionStart;
	var end		= el.selectionEnd;

	var pre = el.value.substring(0, start);
	var post = el.value.substring(end, len);

	el.value = pre + bbFirst + text + bbLast + post;
	el.focus();
}

function wrapText(prePend, apPend, el) {
	var len		= el.textLength;
	var start	= el.selectionStart;
	var end		= el.selectionEnd;

	var pre = el.value.substring(0, start);
	var mid = el.value.substring(start, end);
	var post = el.value.substring(end, len);

	el.value = pre + prePend + mid + apPend + post;
}

function hasSelection(el) {
	if(el.selectionEnd-el.selectionStart > 0) {
		return true;
	} else {
		return false;
	}
}

function fetchSelection(el) {
	//return window.getSelection();
	return el.value.substring(el.selectionStart, el.selectionEnd);
}

function chsize(size) {
	if(hasSelection(messageElement)) {
		text = prompt(bbcode_prompt_size+size, fetchSelection(messageElement));
		if(text == fetchSelection(messageElement)) {
			wrapText('<font size='+size+'>', '</size>', messageElement);
		} else {
			AddText('<font size='+size+'>', '</size>', text, messageElement);
		}
	} else {
		text = prompt(bbcode_prompt_size+size, "Text");
		if(text.length > 0) {
			AddText('<font size='+size+'>', '</size>', text, messageElement);
		} else {
			AddText('<font size='+size+'>', '</size>', ' ', messageElement);
		}
	}
}

function chfont(font) {
	if(hasSelection(messageElement)) {
		text = prompt(bbcode_prompt_font+font, fetchSelection(messageElement));
		if(text == fetchSelection(messageElement)) {
			wrapText('<font face ="'+font+'">', '</font>', messageElement);
		} else {
			AddText('<font face ="'+font+'">', '</font>', text, messageElement);
		}
	} else {
		text = prompt(bbcode_prompt_font+font, "Text");
		if(text.length > 0) {
			AddText('<font face ="'+font+'">', '</font>', text, messageElement);
		} else {
			AddText('<font face ="'+font+'">', '</font>', ' ', messageElement);
		}
	}
}

function bold() {
	if(hasSelection(messageElement)) {
		text = prompt(bbcode_prompt_bold, fetchSelection(messageElement));
		if(text == fetchSelection(messageElement)) {
			wrapText('<b>', '</b>', messageElement);
		} else {
			AddText('<b>', '</b>', text, messageElement);
		}
	} else {
		text = prompt(bbcode_prompt_bold, 'Text');
		AddText('<b>', '</b>', text, messageElement);
	}
}

function italicize() {
	if(hasSelection(messageElement)) {
		text = prompt(bbcode_prompt_italic, fetchSelection(messageElement));
		if(text == fetchSelection(messageElement)) {
			wrapText('<i>', '</i>', messageElement);
		} else {
			AddText('<i>', '</i>', text, messageElement);
		}
	} else {
		text = prompt(bbcode_prompt_italic, 'Text');
		AddText('<i>', '</i>', text, messageElement);
	}
}

function underline() {
	if(hasSelection(messageElement)) {
		text = prompt(bbcode_prompt_underline, fetchSelection(messageElement));
		if(text == fetchSelection(messageElement)) {
			wrapText('<u>', '</u>', messageElement);
		} else {
			AddText('<u>', '</u>', text, messageElement);
		}
	} else {
		text = prompt(bbcode_prompt_underline, 'Text');
		AddText('<u>', '</u>', text, messageElement);
	}
}

function center() {
	if(hasSelection(messageElement)) {
		text = prompt(bbcode_prompt_center, fetchSelection(messageElement));
		if(text == fetchSelection(messageElement)) {
			wrapText('<center>', '</center>', messageElement);
		} else {
			AddText('<center>', '</center>', text, messageElement);
		}
	} else {
		text = prompt(bbcode_prompt_center, 'Text');
		AddText('<center>', '</center>', text, messageElement);
	}
}

function chcolor(color) {
	if(hasSelection(messageElement)) {
		text = prompt(bbcode_prompt_color+color, fetchSelection(messageElement));
		if(text == fetchSelection(messageElement)) {
			wrapText('<font color='+color+'>', '</font>', messageElement);
		} else {
			AddText('<font color='+color+'>', '</font>', text, messageElement);
		}
	} else {
		text = prompt(bbcode_prompt_color+color, "Text");
		if(text.length > 0) {
			AddText('<font color='+color+'>', '</font>', text, messageElement);
		} else {
			AddText('<font color='+color+'>', '</font>', ' ', messageElement);
		}
	}
}

// IE function
// useless in mozilla, but will throw errors if missing
function storeCaret() {
    return null;
}

function loadEls() {
	messageElement = document.getElementById("message");
}