// -------------------------------------------------------------
//
// FILENAME		: insert_code.js
// COPYRIGHT	: © 2003, 2004, 2005, 2006 Alex Newton
// WWW				: http://ebascripts.com/
//
// -------------------------------------------------------------

function getSel(field) {
	if (document.selection) {
		return document.selection.createRange().text;
	}

	if (field.selectionStart || field.selectionStart == 0) {
		return field.value.substring(field.selectionStart, field.selectionEnd);
	}

	return '';
}

function replaceSel(field, txt) {
	if (document.selection) {
		field.focus();
		document.selection.createRange().text = txt;
	} else if (field.selectionStart || field.selectionStart == 0) {
		field.value = field.value.substring(0, field.selectionStart) + txt
		+ field.value.substring(field.selectionEnd, field.value.length);
	} else {
		field.value += txt;
	}

	field.focus();
}

function attributeTag(fieldId, tag) {
	field = document.getElementById(fieldId);
	selection = getSel(field);
	replaceSel(field, '[' + tag + '=' + selection + ']' + selection + '[/' + tag + ']');
}

function addtag(fieldId, tag) {
	field = document.getElementById(fieldId);
	selection = getSel(field);
	replaceSel(field, '[' + tag + ']' + selection + '[/' + tag + ']');
}

function insert(fieldId, txt) {
	field = document.getElementById(fieldId);
	replaceSel(field, txt);
}
