var target = null;

function initMessageBox()
{
	target = document.getElementById('content');	
	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 putExt(type, text)
{

	if (target !== null)
	{
		if (typeof target.cursorPos != 'undefined')
		{
			var cursorPos = target.cursorPos;
			if (type != 'plain' && type != 'pic' ) {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' && type != 'pic') {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' && type != 'pic') {text = '';}
			target.value += det_replace(type, text);
		}

		target.focus();
		if (typeof target.cursorPos != 'undefined') {target.onselect();}
	}
}

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 'center':
			text = '<center>'+text+'</center>';
			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 'code':
			text = '[code]'+text+'[/code]';
			break;
		case 'php':
			text = '[php]'+text+'[/php]';
			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 'url':

			val = prompt('Voer omschrijving in:', text);
			if (val !== null && val !== '') {text = '[url='+text+']'+val+'[/url]';}
			val = prompt('Voer de URL in:','http:\/\/');
				if (val !== null && val != 'http:\/\/')
				{
					if (text === '') {text = '[url]'+val+'[/url]';}
					else {text = '[url='+val+']'+text+'[/url]';}
				}
			
			break;
		case 'urlbak':
			if (/^(http:\/\/|www\.)/i.test(text))
			{
				val = prompt('Voer omschrijving in:', text);
				if (val !== null && val !== '') {text = '[url='+text+']'+val+'[/url]';}
			}
			else
			{
				val = prompt('Voer de URL in:','http:\/\/');
				if (val !== null && val != 'http:\/\/')
				{
					if (text === '') {text = '[url]'+val+'[/url]';}
					else {text = '[url='+val+']'+text+'[/url]';}
				}
			}
			break;			
		case 'img':
			if (text === '')
			{
				val = prompt('Voer de URL in:','http:\/\/');
				if (val !== null && val != 'http:\/\/') {text = '[img]'+val+'[/img]';}
			}
			else
			{
				text = '[img]'+text+'[/img]';
			}
			break;
		case 'pic':
			text = '[img]'+text+'[/img]';
			break;
		case 'quotesplits':
			text += '[/quote]\r\n[quote]';
			break;
	}
	return text;
}

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;
}

function openWindow(file,window) {
    childWindow=open(file,window,'resizable=no, scrollbars=yes, width=600,height=380');
    if (childWindow.opener == null) childWindow.opener = self;
}