//GENERAL TAG FUNCTION:
function dotag(code) {
	var tagtext = prompt('Enter the text for the tag below:', '');

	if (tagtext != '' && tagtext != null) {
		tagtext = '[' + code + ']' + tagtext + '[/' + code + ']';
		insert(document.forms(0).body, tagtext);
	}
}

//ADVANCED TAG FUNCTION:
function doadvanced(code, param) {
	if (param != '') {
		var tagtext = prompt('Enter the text for the tag below:', '');

		if (tagtext != '' && tagtext != null) {
			var tag = '[' + code + '=' + param + ']' + tagtext + '[/' + code + ']';

			insert(document.forms(0).body, tag);
		}
	}
}

//IMAGE TAG FUNCTION:
function doimg() {
	var tagtext = prompt('Enter the location of the image', 'http://');

	if (tagtext != '' && tagtext != null) {
		tagtext = '[img]' + tagtext + '[/img]';
		insert(document.forms(0).body, tagtext);
	}
}

//LINK TAGS FUNCTION:
function dolink() {
	var linktext = prompt('Enter the text for the link below: (optional)', '');
	var linkurl = prompt('Enter the web address for the link: (required)', '');

	if (linkurl != '' && linkurl != null) {
		if (linktext != '' && linktext != null) {
			var link = '[url=' + linkurl + ']' + linktext + '[/url]';
		} else {
			var link = '[url]' + linkurl + '[/url]';
		}

		insert(document.forms(0).body, link);
	}
}

//LIST TAG FUNCTION:
function dolist() {
	var listtype = prompt("Which type of list?\n1 - Numbered, 2 - Alphabetical, or 3 - Bullet", '');

	if (listtype != '' && listtype != null && listtype < 4 && listtype > 0) {
		var listitem = '';
		var listtag = '';

		do {
			listitem = prompt('Enter next item: (or leave blank to stop)', '');
			 if (listitem != '' && listitem != null) {
				 listtag = listtag + '\n[*]' + listitem + '[/*]';
			 }
		} while (listitem != '' && listitem != null);

		if (listtag != '' && listtag != null) {
			listtag = '[list=' + listtype + ']' + listtag + '[/list]';
		}

		insert(document.forms(0).body, listtag);
	}
}

//TEXTAREA FUNCTIONS:
function insert(box, txt) {
	insertAtCaret(box, txt);
}

function storeCaret (textEl) {
	if (textEl.createTextRange) {
		textEl.caretPos = document.selection.createRange().duplicate();
	}
}

function insertAtCaret (textEl, text) {
	if (textEl.createTextRange && textEl.caretPos) {
		var caretPos = textEl.caretPos;

		caretPos.text =
		caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?
		text + ' ' : text;
	} else {
		textEl.value = textEl.value + text;
	}
}