function insertTextFormat(id, open, close){

	f_obj = document.getElementById(id);
	// IE
	if (document.selection && document.selection.createRange){
		f_obj.focus();
		sel = document.selection.createRange();
		sel.text = open + sel.text + close;
		f_obj.focus();
	}

	// GECKO support
	else if (f_obj.selectionStart || f_obj.selectionStart == '0') {
		var startPos = f_obj.selectionStart;
		var endPos = f_obj.selectionEnd;
		
		f_obj.value = f_obj.value.substring(0, startPos) + open + f_obj.value.substring(startPos, endPos) + close + f_obj.value.substring(endPos, f_obj.value.length);
		f_obj.selectionStart = f_obj.selectionEnd = endPos + open.length + close.length;
		f_obj.focus();
	}

	// Fallback support for other browsers
	else {
		f_obj.value += open + close;
		f_obj.focus();
	}
	
	return;
	
}


function insertABBR(id){
	f_obj = document.getElementById(id);
	
	// IE
	if (document.selection && document.selection.createRange){
		f_obj.focus();
		sel = document.selection.createRange();
		if (sel.text == '' || sel.text == null){
			abbr = prompt('Abbreviation', '');
			if (abbr == '' || abbr == null){
				return;
			}
		} else {
			abbr = sel.text
		}
		
		title = prompt('Abbreviation title', '');
		if (title == '' || title == null){
			return;
		}
		
		
		sel.text = '[abbr="' + title + '"]' + abbr + '[/abbr]';
		f_obj.focus();
	}

	// GECKO support
	else if (f_obj.selectionStart || f_obj.selectionStart == '0') {
		var startPos = f_obj.selectionStart;
		var endPos = f_obj.selectionEnd;
		
		if (startPos == endPos){		// no text selected
			abbr = prompt('Abbreviation', '');
			if (abbr == '' || abbr == null){
				return;
			}
		} else {
			abbr = f_obj.value.substring(startPos, endPos);
		}
		
		title = prompt('Abbreviation title', '');
		if (title == '' || title == null){
			return;
		}
		
		
		f_obj.value = f_obj.value.substring(0, startPos) + '[abbr="' + title + '"]' + abbr + '[/abbr]' + f_obj.value.substring(endPos, f_obj.value.length);
		f_obj.selectionStart = f_obj.selectionEnd = endPos + (title.length+9) + 7;
		f_obj.focus();
	}
}



function insertLINK(id){
	f_obj = document.getElementById(id);
	// IE
	if (document.selection && document.selection.createRange){
		f_obj.focus();
		sel = document.selection.createRange();
		if (sel.text == '' || sel.text == null){
			title = prompt('Link title', '');
			if (title == '' || title == null){
				return;
			}
		} else {
			title = sel.text
		}
		
		url = prompt('Link URL', 'http://');
		if (url == '' || url == null){
			return;
		}
		
		
		sel.text = '[url=' + url + ']' + title + '[/url]';
		f_obj.focus();
	}

	// GECKO support
	else if (f_obj.selectionStart || f_obj.selectionStart == '0') {
		var startPos = f_obj.selectionStart;
		var endPos = f_obj.selectionEnd;
		
		if (startPos == endPos){		// no text selected
			title = prompt('Link title', '');
			if (title == '' || title == null){
				return;
			}
		} else {
			title = f_obj.value.substring(startPos, endPos);
		}
		
		url = prompt('Link URL', '');
		if (url == '' || url == null){
			return;
		}
		
		
		f_obj.value = f_obj.value.substring(0, startPos) + '[url=' + url + ']' + title + '[/url]' + f_obj.value.substring(endPos, f_obj.value.length);
		f_obj.selectionStart = f_obj.selectionEnd = endPos + (title.length+6) + 6;
		f_obj.focus();
	}
}

function insertIMG(id){
	f_obj = document.getElementById(id);
	
	// IE
	if (document.selection && document.selection.createRange){
		f_obj.focus();
		sel = document.selection.createRange();
		if (sel.text == '' || sel.text == null){
			url = prompt('Image URL', '');
			if (url == '' || url == null){
				return;
			}
		} else {
			url = sel.text
		}
		
		sel.text = '[img]' + url + '[/img]';
		f_obj.focus();
	}

	// GECKO support
	else if (f_obj.selectionStart || f_obj.selectionStart == '0') {
		var startPos = f_obj.selectionStart;
		var endPos = f_obj.selectionEnd;
		
		if (startPos == endPos){		// no text selected
			url = prompt('Image URL', '');
			if (url == '' || url == null){
				return;
			}
		} else {
			url = f_obj.value.substring(startPos, endPos);
		}
		
		f_obj.value = f_obj.value.substring(0, startPos) + '[img]' + url + '[/img]' + f_obj.value.substring(endPos, f_obj.value.length);
		f_obj.selectionStart = f_obj.selectionEnd = endPos + (url.length) + 11;
		f_obj.focus();
	}
}



function insertSmile(id, smile_code){
	f_obj = document.getElementById(id);
	
	// IE
	if (document.selection && document.selection.createRange){
		f_obj.focus();
		sel = document.selection.createRange();
		
		sel.text = sel.text + smile_code;
		f_obj.focus();
	}

	// GECKO support
	else if (f_obj.selectionStart || f_obj.selectionStart == '0') {
		var startPos = f_obj.selectionStart;
		var endPos = f_obj.selectionEnd;
		
		f_obj.value = f_obj.value.substring(0, startPos) + smile_code + f_obj.value.substring(endPos, f_obj.value.length);
		f_obj.selectionStart = f_obj.selectionEnd = endPos + (smile_code.length);
		f_obj.focus();
	}
}
