function wrapSelection(txtarea, lft, rgt)
{
	if (typeof document.all != 'undefined' && navigator.userAgent.indexOf("Opera") < 0)
	{
		IEWrap(txtarea, lft, rgt);
	}
	else if (document.getElementById)
	{
		mozWrap(txtarea, lft, rgt);
	}
}

function mozWrap(txtarea, lft, rgt)
{
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;

	if (selEnd==1 || selEnd==2) selEnd=selLength;
	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);
	txtarea.value = s1 + lft + s2 + rgt + s3;
}

function IEWrap(txtarea, lft, rgt)
{
	strSelection = document.selection.createRange().text;
	if (strSelection!="")
	{
		document.selection.createRange().text = lft + strSelection + rgt;
	}
	else
	{
		txtarea.value += lft + rgt;
	}
}

function getCurrentSelection(txtarea)
{
	if (typeof document.all != 'undefined')
	{
		return IEGet();
	}
	else if (document.getElementById)
	{
		return mozGet(txtarea);
	}
	return '';
}

function mozGet(txtarea)
{
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	if (selEnd==1 || selEnd==2) selEnd=selLength;
	return (txtarea.value).substring(selStart, selEnd);
}

function IEGet()
{
	return document.selection.createRange().text;
}

function setSelection(txtarea, str)
{
	if (typeof document.all != 'undefined')
	{
		IESet(str);
	}
	else if (document.getElementById)
	{
		mozSet(txtarea, str);
	}
}

function mozSet(txtarea, str)
{
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	if (selEnd==1 || selEnd==2) selEnd=selLength;
	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);
	txtarea.value = s1 + str + s3;
}

function IESet(str)
{
	strSelection = document.selection.createRange().text;
	if (strSelection!="")
	{
		document.selection.createRange().text = str;
	}
}


function storeCursor(element)
{
	if (typeof document.all != 'undefined' && element.createTextRange )
		element.cursorPos = document.selection.createRange().duplicate();
}

function putStr( text )
{
	var target = document.getElementById( "messageBox" );

	if ( target )
	{
		if (typeof document.all != 'undefined' && target.cursorPos)
		{
			var cursorPos = target.cursorPos;
			cursorPos.text = cursorPos.text.charAt(cursorPos.text.length - 1) == ' ' ? text + ' ' : text;
			target.focus();
		}
		else
		{
			wrapSelection(target,'',text);
			target.focus();
		}
	}
}

document.onkeypress = function(e)
{
	if (typeof document.all != 'undefined')
	{
		key=event.keyCode; txtarea=document.getElementById('messageBox');
		if (key == 1) wrapSelectionWithLink(txtarea);
		if (key == 2) wrapSelection(txtarea,'[b]','[/b]');
		if (key == 9) wrapSelection(txtarea,'[i]','[/i]');
		if (key == 21) wrapSelection(txtarea,'[u]','[/u]');
		if (key == 19) wrapSelection(txtarea,'[s]','[/s]');
	}
	else if (document.getElementById)
	{
		ctrl=e.ctrlKey;
		shft=e.shiftKey;
		chr=e.charCode;
		if (ctrl) if (shft) if (chr==65) wrapSelectionWithLink(document.getElementById('messageBox'));
		if (ctrl) if (shft) if (chr==66) wrapSelection(document.getElementById('messageBox'),'[b]','[/b]');
		if (ctrl) if (shft) if (chr==73) wrapSelection(document.getElementById('messageBox'),'[i]','[/i]');
		if (ctrl) if (shft) if (chr==85) wrapSelection(document.getElementById('messageBox'),'[u]','[/u]');
		if (ctrl) if (shft) if (chr==83) wrapSelection(document.getElementById('messageBox'),'[s]','[/s]');
	}
	return true;
}

function mouseover(el) {
	el.className = "tb-raise";
	}

function mouseout(el) {
	el.className = "tb-buttons";
}

function mousedown(el) {
	el.className = "tb-press";
}

function mouseup(el) {
	el.className = "tb-raise";
}

function writeToolbar( isMaximized, showDocumentLibrary )
{
	document.write('<div id="toolbar">');
	document.write('<img class="tb-buttons" onmouseover="mouseover(this);" onmouseout="mouseout(this);" onmousedown="mousedown(this);" onmouseup="mouseup(this);" onclick="wrapSelection(document.getElementById(\'messageBox\'),\'[b]\',\'[/b]\');" src="img/icons/toolbar/bold.gif" title="Maak de geselecteerde tekst vet. Sneltoets: ctrl-shift-b">');
	document.write('<img class="tb-buttons" onmouseover="mouseover(this);" onmouseout="mouseout(this);" onmousedown="mousedown(this);" onmouseup="mouseup(this);" onclick="wrapSelection(document.getElementById(\'messageBox\'),\'[i]\',\'[/i]\');" src="img/icons/toolbar/italic.gif" title="Zet de geselecteerde tekst schuin. Sneltoets: ctrl-shift-o">');
	document.write('<img class="tb-buttons" onmouseover="mouseover(this);" onmouseout="mouseout(this);" onmousedown="mousedown(this);" onmouseup="mouseup(this);" onclick="wrapSelection(document.getElementById(\'messageBox\'),\'[u]\',\'[/u]\');" src="img/icons/toolbar/underline.gif" title="Onderstreep de geselecteerde tekst. Sneltoets: ctrl-shift-u">');
	document.write('<img class="tb-buttons" onmouseover="mouseover(this);" onmouseout="mouseout(this);" onmousedown="mousedown(this);" onmouseup="mouseup(this);" onclick="wrapSelection(document.getElementById(\'messageBox\'),\'[s]\',\'[/s]\');" src="img/icons/toolbar/strike.gif" title="Doorstreep de geselecteerde tekst. Sneltoets: ctrl-shift-s">');
	document.write('<img class="tb-line" src="img/icons/toolbar/hline.gif" width=4 height=18>');
	document.write('<img class="tb-buttons" onmouseover="mouseover(this);" onmouseout="mouseout(this);" onmousedown="mousedown(this);" onmouseup="mouseup(this);" onclick="wrapSelection(document.getElementById(\'messageBox\'),\'[sup]\',\'[/sup]\');" src="img/icons/toolbar/sup.gif" title="Zet de tekst in subscript">');
	document.write('<img class="tb-buttons" onmouseover="mouseover(this);" onmouseout="mouseout(this);" onmousedown="mousedown(this);" onmouseup="mouseup(this);" onclick="wrapSelection(document.getElementById(\'messageBox\'),\'[sub]\',\'[/sub]\');" src="img/icons/toolbar/sub.gif" title="Zet de tekst in superscript">');
	document.write('<img class="tb-line" src="img/icons/toolbar/hline.gif" width=4 height=18>');
	document.write('<img class="tb-buttons" onmouseover="mouseover(this);" onmouseout="mouseout(this);" onmousedown="mousedown(this);" onmouseup="mouseup(this);" onclick="placeList(document.getElementById(\'messageBox\'),\'*\');" src="img/icons/toolbar/list_bullet.gif" title="Maak lijst van tekst">');
	document.write('<img class="tb-buttons" onmouseover="mouseover(this);" onmouseout="mouseout(this);" onmousedown="mousedown(this);" onmouseup="mouseup(this);" onclick="placeList(document.getElementById(\'messageBox\'),\'1\');" src="img/icons/toolbar/list_num.gif" title="Maak genummerde lijst van tekst">');
	document.write('<img class="tb-line" src="img/icons/toolbar/hline.gif" width=4 height=18>');
	document.write('<img class="tb-buttons" onmouseover="mouseover(this);" onmouseout="mouseout(this);" onmousedown="mousedown(this);" onmouseup="mouseup(this);" onclick="wrapSelectionWithLink(document.getElementById(\'messageBox\'));" src="img/icons/toolbar/link.gif" width=18 height=18 title="Maak een hyperlink van de tekst">');
	document.write('<img class="tb-buttons" onmouseover="mouseover(this);" onmouseout="mouseout(this);" onmousedown="mousedown(this);" onmouseup="mouseup(this);" onclick="placeImage(document.getElementById(\'messageBox\'));" src="img/icons/toolbar/image.gif" width=18 height=18 title="Voeg een afbeelding toe aan de tekst">');
	document.write('<img class="tb-line" src="img/icons/toolbar/hline.gif" width=4 height=18>');
	//document.write('<img class="tb-buttons" onmouseover="mouseover(this);" onmouseout="mouseout(this);" onmousedown="mousedown(this);" onmouseup="mouseup(this);" onclick="doUndo(document.getElementById(\'messageBox\'));" src="img/icons/toolbar/undo.gif" width=18 height=18 title="Maak laatste actie ongedaan">');
	//document.write('<img class="tb-buttons" onmouseover="mouseover(this);" onmouseout="mouseout(this);" onmousedown="mousedown(this);" onmouseup="mouseup(this);" onclick="doRedo(document.getElementById(\'messageBox\'));" src="img/icons/toolbar/redo.gif" width=18 height=18 title="Maak laatste actie gedaan">');

	if ( isMaximized )
		document.write('<img class="tb-buttons" onmouseover="mouseover(this);" onmouseout="mouseout(this);" onmousedown="mousedown(this);" onmouseup="mouseup(this);" onclick="setContentToOriginal();window.close();" src="img/icons/toolbar/minimize.gif" width=18 height=18 title="Sluit venster en voeg tekst in">');
	else
		document.write('<img class="tb-buttons" onmouseover="mouseover(this);" onmouseout="mouseout(this);" onmousedown="mousedown(this);" onmouseup="mouseup(this);" onclick="openLargeEditWindow();" src="img/icons/toolbar/maximize.gif" width=18 height=18 title="Open een groot tekst invulveld">');

	if ( showDocumentLibrary )
	{
		document.write('<img class="tb-line" src="img/icons/toolbar/hline.gif" width=4 height=18>');
		document.write('<img class="tb-buttons" onmouseover="mouseover(this);" onmouseout="mouseout(this);" onmousedown="mousedown(this);" onmouseup="mouseup(this);" onclick="openDocumentLibrary();" src="img/icons/toolbar/documentlibrary.gif" width=18 height=18 title="Open de documentverzameling">');
	}


	document.write('</div>');
}

function openLargeEditWindow()
{
	window.open(board_template_url+'html/popupeditor.html','Editor','width=600,height=400,resizable=yes');
}

function openDocumentLibrary()
{
	window.open(board_script_url+'/list_documents_small','DocumentLibrary','width=900,height=500,resizable=yes,scrollbars=yes');
}


function getContentFromOriginal()
{
	document.getElementById('messageBox').value=window.opener.document.getElementById('messageBox').value;
}

function setContentToOriginal()
{
	//window.opener.saveUndo(window.opener.document.getElementById('messageBox'));
	window.opener.document.getElementById('messageBox').value=document.getElementById('messageBox').value;window.close();
}

function rescaleTextarea(txtarea)
{
	if (typeof document.all != 'undefined')
	{
		txtarea.style.height=(document.body.clientHeight-80)+'px';
		txtarea.style.width=(document.body.clientWidth-15)+'px';
	}
	else
	{
		txtarea.style.height=(window.innerHeight-80)+'px';
		txtarea.style.width=(window.innerWidth-15)+'px';
	}
}

function wrapSelectionWithLink(txtarea)
{
	var my_link = prompt("Enter URL:","http://");
	if (my_link != null)
	{
		lft="[url=" + my_link + "]";
		rgt="[/url]";
		wrapSelection(txtarea, lft, rgt);
	}
	return;
}

function wrapSelectionWithBgColor(txtarea)
{
	var my_link = prompt("Enter Hex color:","#");
	if (my_link != null)
	{
		lft="[bgcolor=" + my_link + "]";
		rgt="[/bgcolor]";
		wrapSelection(txtarea, lft, rgt);
	}
	return;
}

function wrapSelectionWithColor(txtarea)
{
	var my_link = prompt("Enter Hex color:","#");
	if (my_link != null)
	{
		lft="[color=" + my_link + "]";
		rgt="[/color]";
		wrapSelection(txtarea, lft, rgt);
	}
	return;
}

function placeImage(txtarea)
{
	if(getCurrentSelection(txtarea) != '')
	{
		wrapSelection(txtarea, '[img]', '[/img]');
		return;
	}
	var my_link = prompt("Enter image URL","http://");
	if (my_link != null && my_link !='http://')
	{
		lft="[img]" + my_link;
		rgt="[/img]";
		wrapSelection(txtarea, lft, rgt);
	}
	return;
}

function placeTable(txtarea)
{
	var sel = getCurrentSelection(txtarea);
	var rows = sel.split('\n');
	var out = '[table border=1 bgcolor=#FFFFFF width=350 cellpadding=2 bordercolor=#000000]';
	for(i=0;i<rows.length;i++)
	{
		out += '\n[tr]';
		var cells = rows[i].split('\t');
		for(j=0;j<cells.length;j++)
		{
			out += '\n[td]'+cells[j]+'[/td]';
		}
		out += '\n[/tr]';
	}
	out += '\n[/table]'

	setSelection(txtarea,out);
	return;
}

function placeList(txtarea, type)
{
	var sel = getCurrentSelection(txtarea);
	var rows = sel.split('\n');
	var out = '[list='+type+']';
	for(i=0;i<rows.length;i++)
	{
		if(rows[i] != '')
			out += '\n[*]'+rows[i];
	}
	out += '\n[/list]'
	setSelection(txtarea,out);
	return;
}

function showBgColor(x)
{
	if (typeof document.all != 'undefined' || !colorpopup)
	{
		var my_link = prompt("Enter color","#");
		if (my_link != null && my_link !='#')
		{
			lft="[bgcolor="+my_link+"]";
			rgt="[/bgcolor]";
			wrapSelection(txtarea, lft, rgt);
		}
	}
	else
	{
		document.xTag = 'bgcolor';
			showColorPallete(document.getElementById(x));
	}
	return;
}

function showColor(x)
{
	if (typeof document.all != 'undefined' || !colorpopup) {
		var my_link = prompt("Enter color","#");
		if (my_link != null && my_link !='#') {
			lft="[color="+my_link+"]";
			rgt="[/color]";
			wrapSelection(txtarea, lft, rgt);
		}
	}
	else
	{
		document.xTag = 'color';
		showColorPallete(document.getElementById(x));
	}
	return;
}

function showColorPallete(buttonElement)
{
	setTimeout('dismisscolorpalette();',3000);

	document.getElementById("colorpalette").style.left = getOffsetLeft(buttonElement);
	document.getElementById("colorpalette").style.top = getOffsetTop(buttonElement) + buttonElement.offsetHeight;
	document.getElementById("colorpalette").style.visibility="visible";
}

function getOffsetTop(elm)
{
	var mOffsetTop = elm.offsetTop;
	var mOffsetParent = elm.offsetParent;

	while(mOffsetParent)
	{
		mOffsetTop += mOffsetParent.offsetTop;
		mOffsetParent = mOffsetParent.offsetParent;
	}

	return mOffsetTop;
}

function getOffsetLeft(elm)
{

	var mOffsetLeft = elm.offsetLeft;
	var mOffsetParent = elm.offsetParent;

	while(mOffsetParent)
	{
		mOffsetLeft += mOffsetParent.offsetLeft;
		mOffsetParent = mOffsetParent.offsetParent;
	}

	return mOffsetLeft;
}

function dismisscolorpalette()
{
	if(document.getElementById("colorpalette"))
		document.getElementById("colorpalette").style.visibility="hidden";
}

function enableSplitIds( element )
{
	tds = document.getElementsByName('mark_message_split_td');
	state = element.checked ? '':'none';
	document.getElementById('split_messageids').value='';

	for( var i = 0; i < tds.length; i++ )
	{
		tds[i].style.display = state;
	}
}

function enableDelIds( element )
{
	tds = document.getElementsByName('mark_message_del_td');
	state = element.checked ? '':'none';
	document.getElementById('del_messageids').value='';

	for( var i = 0; i < tds.length; i++ )
	{
		tds[i].style.display = state;
	}
}

function toggleSelectID( element, target_element )
{
	if ( targetbin = document.getElementById(target_element) )
	{
		var selectids = targetbin.value.split(',');
		var newselectids = Array()

		if ( !element.checked )
		{
			for( var i=0; i < selectids.length; i++ )
			{
				if ( selectids[i] != '' && selectids[i] != element.value )
					newselectids.push(selectids[i]);
			}
		}
		else
		{
			newselectids = selectids;
			newselectids.push(element.value);
		}
		document.getElementById(target_element).value = newselectids.join(',');
	}
}

function shortenString( str, maxlength )
{
	if ( str.length > Math.round(maxlength) )
		return str.substring(0,Math.round(maxlength)-2)+'<span style="color:#333;font-size:8px">...</span>';
	else
		return str;
}

function copyrml2opener(form, document_id, document_name, document_size, document_is_image, document_thumbnail, document_download_link, document_key, document_library_id)
{
	p = window.opener;

	var rmltag = '';
	var rmltype = '';

	rmltype = document.getElementById('insert_type_'+document_id).value;

	if ( document_thumbnail == '' )
		document_thumbnail = board_template_url +'img/icons/document.gif';

	if(rmltype == 'thumbnail')
		rmltag = '[DOCLIB='+document_id+','+document_key+','+document_library_id+',thumb]';
	else if(rmltype == 'thumbnailandlink')
		rmltag = '[URL='+document_download_link+'][DOCLIB='+document_id+','+document_key+','+document_library_id+'][/URL]';
	else if(rmltype == 'image')
		rmltag = '[DOCLIB='+document_id+','+document_key+','+document_library_id+']';
	else if(rmltype == 'nameandlink')
		rmltag = '[URL='+document_download_link+']'+document_name+'[/URL]';
	else
		rmltag = '[URL='+document_download_link+']'+document_name+'[/URL]';



	//confirm(rmltag);

	if(rmltag)
		p.putStr('\n'+rmltag+'\n');
}