
function insertAtCursor(field, value){
	// IE
	if (document.selection){
		field.focus();
		sel = document.selection.createRange();
		sel.text = value;
		return;
	}
	// ETC
	if (field.selectionStart || field.selectionStart == '0'){
		var startPos = field.selectionStart;
		var endPos = field.selectionEnd;
		field.value = field.value.substring(0, startPos)
			+ value
			+ field.value.substring(endPos, field.value.length);
		return;
	}
	// WHAT ELSE
	field.value += value;
}

function replaceSelection(field, begin, end){
	var scrollPos = field.scrollTop;
	// IE
	if (document.selection){
		field.focus();
		sel = document.selection.createRange();
		var replace = begin + sel.text + end
		sel.text = replace;
		if (sel.text != ''){
			sel.moveStart('character', -replace.length);
			sel.select();
		}
	}
	// ETC
	else if (field.selectionStart || field.selectionStart == '0'){
		var startPos = field.selectionStart;
		var endPos = field.selectionEnd;
		field.value = field.value.substring(0, startPos) + begin + field.value.substring(startPos, endPos) + end + field.value.substring(endPos, field.value.length);
		field.setSelectionRange(endPos + begin.length + end.length, endPos + begin.length + end.length);
	}
	field.scrollTop = scrollPos;
}

function insertText(txt)
{
	var area = document.form.content;
	area.focus();
	replaceSelection(area, txt, '');
}

function insertTag(begin, end)
{
	var area = document.form.content;
	area.focus();
	replaceSelection(area, begin, end);
}

function insertUrl()
{
	var url = prompt("Entrez l'adresse de la page :",'http://');
	if (url.length > 10) {
		var comment = prompt("Entrez le texte de votre lien :",url);
		if (comment.length == 0 || comment == url) {
			return insertText('[lien]'+url+'[/lien]');
		}
		else {
			return insertText('[lien='+url+']'+comment+'[/lien]');
		}
	}
	else {
		alert('Cette adresse semble non valide.');
	}
}

