var incompetent_browser = document.all && navigator.userAgent.indexOf('MSIE') > -1 && navigator.userAgent.indexOf('Opera') == -1;
var target = null;

function initMessageBox()
{
	target = document.getElementById('textarea');
	if (target !== null)
	{
		target.focus();
		if (typeof target.createTextRange != 'undefined')
		{
			target.onkeydown = shortkey;
			target.onkeyup = storeCursor;
			target.onclick = storeCursor;
			target.onselect = storeCursor;
			target.onselect();
		}
		else
		{
			target.onkeypress = shortkey;
		}
	}
}

function storeCursor()
{
	this.cursorPos = document.selection.createRange().duplicate();
}

function det_replace(type, text)
{
	var val = '';
	switch (type)
	{
		case 'plain':
			break;
		case 'bold':
			text = '[b]'+text+'[/b]';
			break;
		case 'italic':
			text = '[i]'+text+'[/i]';
			break;
		case 'underline':
			text = '[u]'+text+'[/u]';
			break;
		case 'titel':
			text = '[titel]'+text+'[/titel]';
			break;
		case 'subtitel':
			text = '[subtitel]'+text+'[/subtitel]';
			break;
		case 'strike':
			text = '[s]'+text+'[/s]';
			break;
		case 'sub':
			text = '[sub]'+text+'[/sub]';
			break;
		case 'sup':
			text = '[sup]'+text+'[/sup]';
			break;
		case 'spoiler':
			text = '[spoiler]'+text+'[/spoiler]';
			break;
		case 'quote':
			text = '[quote]'+text+'[/quote]';
			break;
		case 'listbullet':
			text = '\r\n[*]'+(text.split(/\r?\n/).join('\r\n[*]'))+'\r\n';
			break;
		case 'link':
			if (/^(http:\/\/|www\.)/i.test(text))
			{
				val = prompt('Voer omschrijving in:', text);
				if (val !== null && val !== '') {text = '[link='+text+']'+val+'[/link]';}
			}
			else
			{
				val = prompt('Voer de URL in:','http:\/\/');
				if (val !== null && val != 'http:\/\/')
				{
					if (text === '') {text = '[link]'+val+'[/link]';}
					else {text = '[link='+val+']'+text+'[/link]';}
				}
			}
			break;
		case 'email':
			if (text)
			{
				val = prompt('Voer omschrijving in:', text);
				if (val !== null && val !== '') {text = '[email='+text+']'+val+'[/email]';}
			}
			else
			{
				val = prompt('Voer het emailadres in:');
				if (val !== null)
				{
					if (text === '') {text = '[email]'+val+'[/email]';}
					else {text = '[email='+val+']'+text+'[/email]';}
				}
			}
			break;
	}
	return text;
}

function putStr(text)
{
	putExt('plain', text);
}

function putExt(type, text)
{

	if (target !== null)
	{
	
		if (typeof target.cursorPos != 'undefined')
		{
			var cursorPos = target.cursorPos;
			if (type != 'plain') {text = cursorPos.text;}
			cursorPos.text = det_replace(type, text);
		}
		else if (typeof target.selectionStart != 'undefined')
		{
			// remember scrollposition
			var scrollTop = target.scrollTop;
			var sStart = target.selectionStart;
			var sEnd = target.selectionEnd;
			if (type != 'plain') {text = target.value.substring(sStart, sEnd);}
			text = det_replace(type, text);
			target.value = target.value.substr(0, sStart) + text + target.value.substr(sEnd);
			var nStart = sStart == sEnd ? sStart + text.length : sStart;
			var nEnd = sStart + text.length;
			target.setSelectionRange(nStart, nEnd);
			// reset scrollposition
			target.scrollTop = scrollTop;
		}
		else
		{
			if (type != 'plain') {text = '';}
			target.value += det_replace(type, text);
		}
		target.focus();
		if (typeof target.cursorPos != 'undefined') {target.onselect();}
	}
}

function shortkey(e)

{
	if (!e) {e = event;}
	var key = 0;
	if (e.KeyCode) {key = e.KeyCode;}
	else if (e.which) {key = e.which - 32;}
	if (e.ctrlKey && !e.shiftKey)
	{
		switch (key)
		{
			case 66:
				putExt('bold');
				return cancelEvent(e);
			case 73:
				putExt('italic');
				return cancelEvent(e);
			case 83:
				putExt('strike');
				return cancelEvent(e);
			case 85:
				putExt('underline');
				return cancelEvent(e);
		}
	}
	return true;
}