//-- Browsercheck
function BrowserCheck()
{
	var v;
	this.opera = navigator.userAgent.indexOf('Opera') > -1;
	this.konqueror = navigator.userAgent.indexOf('Konqueror') > -1;
	this.mac = navigator.userAgent.indexOf('Macintosh') > -1;
	this.ie = !this.opera && !!document.all && !!(v = navigator.userAgent.match(/MSIE (\d(\.\d)?)/));
	if (this.ie) this.ieversion = parseFloat(v[1]);
}
var is = new BrowserCheck();

var banner = null, bannerhide = 0, sky = null, quicksearch = null, qshide = 0;

function init_banner()
{
	if ((banner = document.getElementById('mainbanner')))
	{
		if (	(is.ie && /<(object|select|embed|applet)/i.test(banner.innerHTML)) ||
			(	(	(is.ie && is.ieversion < 5.5) ||
					is.opera ||
					is.konqueror
				) &&
				/<iframe/i.test(banner.innerHTML)
			)
		)
		{
			banner.left = getOffsetLeft(banner);
			banner.top = getOffsetTop(banner);
			bannerhide = 1;
		}
	}
}

function show_banner()
{
	if (banner && bannerhide) banner.style.display = 'block';
	if (quicksearch && qshide) quicksearch.style.display = 'block';
}

function hide_banner(submenu)
{
	if (bannerhide || qshide)
	{
		if (!submenu.left)
		{
			submenu.left = getOffsetLeft(submenu)
			submenu.top = getOffsetTop(submenu);
		}

		if (banner && bannerhide)
		{
			if (!(submenu.left > banner.left + banner.clientWidth || submenu.left + submenu.clientWidth < banner.left ||
				submenu.top > banner.top + banner.clientHeight || submenu.top + submenu.clientHeight < banner.top)) banner.style.display = 'none';
		}
	
		if (quicksearch && qshide)
		{
			if (!(submenu.left > quicksearch.left + quicksearch.clientWidth || submenu.left + submenu.clientWidth < quicksearch.left ||
				submenu.top > quicksearch.top + quicksearch.clientHeight || submenu.top + submenu.clientHeight < quicksearch.top)) quicksearch.style.display = 'none';
		}
	}
}

function init_sky()
{
	if ((sky = document.getElementById('skyscraper')))
	{
		var w = getDocumentProperty('clientWidth');
		var h = getDocumentProperty('scrollHeight');
		var o = document.getElementById('layout').offsetLeft + sky.offsetLeft + sky.clientWidth;

		if (w > o && h > skytop + 600)
		{
			if ((w - o) > 40)
			{
				//-- extra breed
				google_ad_width = 160;
				google_ad_format = '160x600_as';
				sky.style.width = '168px';
			}

			document.write('<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"><\/script>');

			sky.style.position = 'absolute';
			sky.style.top = skytop + 'px';
			sky.style.visibility = 'visible';
		}
		else
		{
			sky.style.display = 'none';
			sky = null;
		}
	}
}

function show_sky()
{
	if (sky) sky.style.display = 'block';
}

function hide_sky(code)
{
	var left = getOffsetLeft(code), top = getOffsetTop(code);
	if (sky)
	{
		var skyleft = getOffsetLeft(sky), skytop = getOffsetTop(sky);
		if (!(left > skyleft + sky.clientWidth || left + code.clientWidth < skyleft ||
			top > skytop + sky.clientHeight || top + code.clientHeight < skytop)) sky.style.display = 'none';
	}
}

//-- skyscraper slide
var skymove = null;
var scrolltop = -1;
var skytop = 120;
function movesky()
{
	if (sky)
	{
                if (animate)
                {
                        if (skymove) clearTimeout(skymove);
                        skymove = setTimeout('slidesky()', 300);
                }
                else
                {
                        sky.style.top = (getDocumentProperty('scrollTop') + skytop) + 'px';
                }
	}
}

function slidesky()
{
	scrolltop = getDocumentProperty('scrollTop');
	var curtop = sky.offsetTop;

	if (curtop + 600 < scrolltop) curtop = scrolltop - 600;
	else if (curtop > scrolltop + getDocumentProperty('clientHeight')) curtop = getDocumentProperty('clientHeight');
	var diff = scrolltop + 50 - curtop;
	if (diff > 60) diff = 60;
	if (diff < -60) diff = -60;
	var newtop = curtop + Math.round(diff / 3);
	if (newtop < skytop)
	{
		newtop = skytop;
		diff = 0;
	}
	sky.style.top = newtop + 'px';
	if (diff) skymove = setTimeout('slidesky()', 30);
	else skymove = null;
}

function qs_init()
{
	quicksearch = document.getElementById('quicksearch');
	if (quicksearch)
	{
		quicksearch.left = getOffsetLeft(quicksearch);
		quicksearch.top = getOffsetTop(quicksearch);
		qshide = is.ie;
	}
}


function IE_EuroFix(text)
{
	for (var i = 0, replaced = ''; i < text.length; i++)
	{
		switch (text.charCodeAt(i))
		{
			case 128:
				replaced += '&euro;';
				break;
			case 381:
				replaced += '&acute;';
				break;
			default:
				replaced += text.charAt(i);
		}
	}

	return replaced;
}

if (window.bc) window.onload = function()
{
	var foo = new Image();
	foo.src = board_template_url + 'html/bc.php?check=' + (Math.floor(Math.random() * 100000000) * 11 + (banner && banner.childNodes.length > 1 ? 5 : 0));
}

//-- Menu
var nav_current = [], nav_hide_delay = [];

function init_menu()
{
	var li = first_child(document.getElementById('sitenav'));
	while (li)
	{
		li.menuid = 'mainmenu';
		li.onmouseover = siteNavHover;
		li.onmouseout = siteNavHideCurrentSetDelay;
		li.submenu = init_submenu(li, 'mainmenu');

		li = node_after(li);
	}
}

function unload_menu()
{
	var li = first_child(document.getElementById('sitenav'));
	while (li)
	{
		li.submenu = null;
		li = node_after(li);
	}
}
window.onunload = unload_menu;

function init_submenu(menuitem, menuid)
{
	var submenu = first_child(menuitem);
	while (submenu)
	{
		if (submenu.tagName.toLowerCase() == 'div')
		{
			submenu.menuid = menuid;
			submenu.onmouseover = siteNavHideClearDelay;
			submenu.onmouseout = siteNavHideCurrentDelay;
			if (menueffect != 'nopacity')
			{
				setOpacity(submenu, 0.95);
			}
	
			return submenu;
		}
	
		submenu = node_after(submenu);
	}

	return null;
}

//-- Inklap functies
function toggle_categorydisplay(img)
{
	var row = img.parentNode, display, selects, i;

	if (img.src == fclosed.src)
	{
		img.src =  fopen.src;
		display = '';
		delete klipklapcookie[img.id];
	}
	else if (img.src == fopen.src)
	{
		img.src = fclosed.src;
		display = 'none';
		klipklapcookie[img.id] = 1;
	}

	while (row)
	{
		if (row.tagName.toLowerCase() == 'tr')
		{
			row.className = display ? '' : 'hide';

			while ((row = node_after(row)))
			{
				row.style.display = display;
				if (is.ie)
				{
					selects = row.getElementsByTagName('select'), i = selects.length;
					while (i--)
						selects[i].style.display = display;
				}
			}

			writeCookies();
			break;
		}

		row = row.parentNode;
	}
}

//-- Message box editing functions
var target = null;
function initMessageBox()
{
	if (!target) target = document.getElementById('messageBox');
	if (target)
	{
		target.focus();
		if (target.createTextRange)
		{
			target.onkeyup = storeCursor;
			target.onclick = storeCursor;
			target.onselect = storeCursor;
			target.onselect();
		}

		if (!is.mac && target.tagName.toLowerCase() == 'textarea')
		{
			if (target.createTextRange)
				target.onkeydown = shortkey;
			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 'AND':
			if (text != '')	text = text.replace(/(\w+)\s+/g, '$1 AND ');
			else text = ' AND ';
			break;
		case 'OR':
			if (text != '')	text = text.replace(/(\w+)\s+/g, '$1 OR ');
			else text = ' OR ';
			break;
		case 'brackets':
			if (text != '')	text = '('+text+')';
			else text = '()';
			break;
		case 'title':
			if (text != '')	text = 'title:('+text+')';
			else text = 'title:';
			break;
		case 'start':
			if (text != '')	text = 'start:('+text+')';
			else text = 'start:';
			break;
		case 'bold':
			text = '[b]'+text+'[/b]';
			break;
		case 'italic':
			text = '[i]'+text+'[/i]';
			break;
		case 'underline':
			text = '[u]'+text+'[/u]';
			break;
		case 'strike':
			text = '[s]'+text+'[/s]';
			break;
		case 'sub':
			text = '[sub]'+text+'[/sub]';
			break;
		case 'sup':
			text = '[sup]'+text+'[/sup]';
			break;
		case 'small':
			text = '[small]'+text+'[/small]';
			break;
		case 'left':
			text = '[left]'+text+'[/left]';
			break;
		case 'center':
			text = '[center]'+text+'[/center]';
			break;
		case 'right':
			text = '[right]'+text+'[/right]';
			break;
		case 'listbullet':
			text = '[list]\r\n[*]'+(text.split(/\r?\n/).join('\r\n[*]'))+'\r\n[/list]';
			break;
		case 'listnum':
			text = '[list=1]\r\n[*]'+(text.split(/\r?\n/).join('\r\n[*]'))+'\r\n[/list]';
			break;
		case 'bgcolor':
			val = prompt('Voer een hexadecimale kleurcode in:','#');
			if (val !== null) text = '[bgcolor='+val+']'+text+'[/bgcolor]';
			break;
		case 'color':
			val = prompt('Voer een hexadecimale kleurcode in:','#');
			if (val !== null) text = '[color='+val+']'+text+'[/color]';
			break;
		case 'url':
			if (/^(http:\/\/|www\.)/i.test(text))
			{
				val = prompt('Please give a description:', text);
				if (val !== null && val != '') text = '[url='+text.replace(/(["\\])/g,'\\$1')+']'+val+'[/url]';
			}
			else
			{
				val = prompt('Please give us the URL:','http:\/\/');
				if (val !== null && val != 'http:\/\/')
				{
					if (text == '') text = '[url]'+val+'[/url]';
					else text = '[url='+val.replace(/([\\])/g,'\\$1')+']'+text+'[/url]';
				}
			}
			break;
		case 'img':
			if (text == '')
			{
				val = prompt('Please give us the URL:','http:\/\/');
				if (val !== null && val != 'http:\/\/') text = '[img]'+val+'[/img]';
			}
			else
			{
				text = '[img]'+text+'[/img]';
			}
			break;
		case 'table':
			text = '[table border=1 width=350 cellpadding=2 bordercolor=#000000]\r\n[tr]\r\n[td]'+(text.split(/\r?\n/).join('[/td]\r\n[/tr]\r\n[tr]\r\n[td]'))+'[/td]\r\n[/tr]\r\n[/table]';
			break;
		case 'hr':
			text += '[hr]';
			break;
		case 'quote':
			text = '[quote]\r\n'+text+'\r\n[/quote]';
			break;
		case 'pause':
			text += '\r\n\[Pause Preview]\r\n';
			break;
		case 'protect':
			text = '[protect]'+text+'[/protect]';
			break;
	}

	return text;
}

function putStr(text)
{
	putExt('plain', text);
}

function putExt(type, text)
{
	if (target)
	{
		if (target.cursorPos)
		{
			var cursorPos = target.cursorPos;
			if (type != 'plain') text = cursorPos.text;
			cursorPos.text = det_replace(type, text);
		}
		else if (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 (target.cursorPos) 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;
}

function increaseTextarea(img)
{
	if (!target) target = document.getElementById('messageBox');
	if (target)
	{
		if (target.style.height)
		{
			target.style.height = '';
			img.src = maximize.src;
			img.title = 'Increase textarea';
		}
		else
		{
			target.style.height = 2 * target.offsetHeight + 'px';
			img.src = minimize.src;
			img.title = 'Decrease textarea';
		}
	}
}

if (is.ie) document.onkeydown = function()
{
	var key = event.keyCode;
	var a = String.fromCharCode(key).toLowerCase();
	if (event.altKey && key > 64 && key < 91 && acckeys[a])
	{
		window.location.href = /^http:\/\//.test(acckeys[a]) ? acckeys[a] : board_script_url + acckeys[a];
		event.keyCode = 90; // use a valid keycode that is not in IE's context menu
		event.returnValue = 0;
		event.cancelBubble = true;
		return false;
	}

	return true;
}

function cancelEvent(e)
{
	if (e.preventDefault)
	{
		e.preventDefault();
	}
	else if (e.cancelBubble != undefined)
	{
		if (e.keyCode) e.keyCode = 0;
		e.returnValue = 0;
		e.cancelBubble = true;
	}

	return false;
}

function winopen(url, winname, width, height, extra)
{
	if (!extra) extra = ''; else extra = ',' + extra;
	var popup = window.open(url,winname,'toolbar=no,location=no,menubar=no,width='+width+',height='+height+',resizable=yes,status=no'+extra);
	try { popup.focus(); } catch(e) {}
}

function code_tags()
{
	var el = getElementsByClassName('phphighlight', 'table'), i = el.length, row, div, pre, img;

	while (i--)
	{
		row = first_child(el[i]);
		if (row.tagName.toLowerCase() != 'tr') row = first_child(row);
		div = first_child(row.childNodes[row.childNodes.length-1]);
		if (!div || div.tagName.toLowerCase() != 'div') continue;
		div.style.paddingBottom = '0px';
		pre = first_child(div);
		if (!pre || (pre.tagName.toLowerCase() != 'pre' && pre.tagName.toLowerCase() != 'code')) continue;

		if (div.scrollWidth > div.clientWidth)
		{
			if (is.ie)
			{
				div.orgPadding = (div.scrollHeight - div.clientHeight) + 'px';
				div.style.paddingBottom = div.orgPadding;
			}
			pre.style.width = div.scrollWidth + 'px';
			div.orgWidth = div.clientWidth;
			img = document.createElement('img');
			img.src = aopen.src;
			img.className = 'klipklap hand';
			img.onclick = klipklap_code;
			el[i].parentNode.insertBefore(img, node_before(node_before(el[i])));
		}
	}

	// pre tags as well
	el = getElementsByClassName('(pre|cmd)', 'div'), i = el.length;

	while (i--)
	{
		div = el[i];
		div.style.paddingBottom = '0px';
		pre = first_child(div);
		if (!pre || pre.tagName.toLowerCase() != 'pre') continue;

		if (div.scrollWidth > div.clientWidth)
		{
			if (is.ie)
			{
				div.orgPadding = (div.scrollHeight - div.clientHeight) + 'px';
				div.style.paddingBottom = div.orgPadding;
			}
			pre.style.width = div.scrollWidth + 'px';
			div.orgWidth = div.clientWidth;
			img = document.createElement('img');
			img.src = aopen.src;
			img.className = 'klipklap hand';
			img.onclick = klipklap_pre;
			div.parentNode.insertBefore(img, div);
		}
	}
}

function klipklap_code()
{
	var table = node_after(node_after(node_after(this)));
	if (!table || table.tagName.toLowerCase() != 'table') return;
	var row = first_child(table);
	if (row.tagName.toLowerCase() != 'tr') row = first_child(row);
	var div = first_child(row.childNodes[row.childNodes.length-1]);
	if (!div || div.tagName.toLowerCase() != 'div') return;
	var pre = first_child(div);
	if (!pre || (pre.tagName.toLowerCase() != 'pre' && pre.tagName.toLowerCase() != 'code')) return;

	if (this.src == aopen.src)
	{
		this.src = aclosed.src;
		table.style.width = 'auto';
		div.style.overflow = 'visible';
		if (is.ie) div.style.paddingBottom = '0px';
		pre.style.borderRight = '1px solid black';
		div.style.width = pre.scrollWidth + 'px';
		hide_sky(pre);
	}
	else
	{
		this.src = aopen.src;
		table.style.width = '100%';
		div.style.overflow = '';
		if (is.ie) div.style.paddingBottom = div.orgPadding;
		pre.style.borderRight = 'none';
		div.style.width = div.orgWidth + 'px';
		show_sky();
	}
}

function klipklap_pre()
{
	var div = node_after(this);
	if (!div || div.tagName.toLowerCase() != 'div') return;
	var pre = first_child(div);
	if (!pre || pre.tagName.toLowerCase() != 'pre') return;

	if (this.src == aopen.src)
	{
		this.src = aclosed.src;
		div.style.overflow = 'visible';
		if (is.ie) div.style.paddingBottom = '0px';
		div.style.width = pre.scrollWidth + 'px';
		hide_sky(pre);
	}
	else
	{
		this.src = aopen.src;
		div.style.overflow = '';
		if (is.ie) div.style.paddingBottom = div.orgPadding;
		div.style.width = div.orgWidth + 'px';
		show_sky();
	}
}

function more_smilies(a)
{
	var m = document.getElementById('moresmilies');
	if (m)
	{
		if (m.style.display == 'none')
		{
			m.style.display = 'block';
			a.style.display = 'none';
		}
	}
}

/* DOM functions */

function is_ignorable( nod )
{
	return (nod.nodeType == 8 || (nod.nodeType == 3 && !/[^\t\n\r ]/.test(nod.data)));
}

function node_before( sib )
{
	if (sib)
	{
		while ((sib = sib.previousSibling))
		{
			if (!is_ignorable(sib)) return sib;
		}
	}

	return null;
}

function node_after( sib )
{
	if (sib)
	{
		while ((sib = sib.nextSibling))
		{
			if (!is_ignorable(sib)) return sib;
		}
	}

	return null;
}

function last_child( par )
{
	if (par && par.nodeType == 1)
	{
		var res=par.lastChild;
		do
		{
			if (!is_ignorable(res)) return res;
		}
		while ((res = res.previousSibling));
	}

	return null;
}

function first_child( par )
{
	if (par && par.nodeType == 1)
	{
		var res=par.firstChild;
		do
		{
			if (!is_ignorable(res)) return res;
		}
		while ((res = res.nextSibling));
	}

	return null;
}

function getElementsByClassName(className, nodeName, parentElement)
{
	if (!nodeName)
		nodeName = '*';

	if (!parentElement)
		parentElement = document;

	var results = [];
	var s = parentElement.getElementsByTagName(nodeName), i = 0, element, elementClassName;
	var re = new RegExp('(^|\\s)' + className + '(\\s|$)');

	while ((element = s[i++]))
	{
		elementClassName = element.className;
		if (	elementClassName.length &&
			(	elementClassName == className ||
				re.test(elementClassName)
			)
		)
			results.push(element);
	}

	return results;
}

function getDocumentProperty(prop)
{
	return document.documentElement && document.compatMode != 'BackCompat' ? document.documentElement[prop] : document.body[prop];
}

//-- classdealer functions
function addClass(element, className)
{
	var classes = getClassList(element);
	if (classes.indexOf(className) == -1)
	{
		classes.push(className);
		setClassList(element, classes);
	}
}

function removeClass(element, className)
{
	var classes = getClassList(element), index;
	if ((index = classes.indexOf(className)) > -1)
	{
		delete classes[index];
		setClassList(element, classes);
	}
}

function replaceClass(element, oldclass, newclass)
{
	var classes = getClassList(element), index;
	if ((index = classes.indexOf(oldclass)) > -1 && classes.indexOf(newclass) == -1)
	{
		classes[index] = newclass;
		setClassList(element, classes);
	}
}

function hasClass(element, className)
{
	var elementClassName = element.className;
	return	elementClassName.length &&
		(	elementClassName == className ||
			elementClassName.match(new RegExp('(^|\\s)' + className + '(\\s|$)'))
		);
}

function getClassList(element)
{
	return element.className.length ? element.className.split(/\s+/) : [];
}

function setClassList(element, classes)
{
	element.className = classes.join(' ');
}

if (!Array.prototype.indexOf)
{
	Array.prototype.indexOf = function(searchElement, fromIndex)
	{
		var l = this.length, i = 0;
		if (fromIndex)
		{
			i = fromIndex;
			if (i < 0)
			{
				i += l;
				if (i < 0) i = 0;
			}
		}

		while (i < l)
		{
			if (this[i] === searchElement) return i;
			i++;
		}

		return -1;
	}
}

//-- Positie van element verkrijgen
function getOffsetTop(el)
{
	var offsetTop = 0;
	do
	{
		offsetTop += el.offsetTop
	}
	while ((el = el.offsetParent));

	return offsetTop;
}

function getOffsetLeft(el)
{
	var offsetLeft = 0;
	do
	{
		offsetLeft += el.offsetLeft
	}
	while ((el = el.offsetParent));

	return offsetLeft;
}

//-- IE 5.0 specific
window.undefined = window.undefined;

if (!Array.push)
{
	Array.prototype.push = function()
	{
		for (var i = 0; i < arguments.length; ++i)
		{
			this[this.length] = arguments[i];
		}

		return this.length;
	}
}

if (!window.encodeURIComponent)
{
	window.encodeURIComponent = function(s)
	{
		return escape(s).replace(/\%(21|7E|27|28|29)/g, unescape).replace(/[@+\/]/g, function(c) {
			return '%' + c.charCodeAt(0).toString(16).toUpperCase();
		});
	};
	window.decodeURIComponent = unescape;
}

if (!Object.prototype.hasOwnProperty)
{
	Object.prototype.hasOwnProperty = function(p)
	{
		return this.constructor.prototype[p] === undefined;
	}
}

//-- Image preload
var fclosed = new Image(); fclosed.src = board_template_url + 'images/icons/fieldset_closed.gif';
var fopen = new Image(); fopen.src = board_template_url + 'images/icons/fieldset_open.gif';
var fnone = new Image(); fnone.src = board_template_url + 'images/icons/fieldset_none.gif';
var ftsclosed = new Image(); ftsclosed.src = board_template_url + 'images/icons/fieldset_ts_closed.gif';
var ftsopen = new Image(); ftsopen.src = board_template_url + 'images/icons/fieldset_ts_open.gif';
var ftsnone = new Image(); ftsnone.src = board_template_url + 'images/icons/fieldset_ts_none.gif';

var aclosed = new Image(); aclosed.src = board_template_url + 'img/forum/buttonLogos/admin_closed.gif';
var aopen = new Image(); aopen.src = board_template_url + 'img/forum/buttonLogos/admin_open.gif';

var offline = new Image(); offline.src = board_template_url + 'images/icons/offline.gif';
var offline_ts = new Image(); offline_ts.src = board_template_url + 'images/icons/offline_ts.gif';
var online = new Image(); online.src = board_template_url + 'images/icons/online.gif';
var online_ts = new Image(); online_ts.src = board_template_url + 'images/icons/online_ts.gif';

var maximize = new Image(); maximize.src = board_template_url + 'img/forum/buttonLogos/maximize.gif';
var minimize = new Image(); minimize.src = board_template_url + 'img/forum/buttonLogos/minimize.gif';

//-- Initialisation
var categories = [];
var messageids = [];
var klipklapcookie = {}, skipcookie = true;








// ----------------------- phpBB code ----------------------- 

// bbCode control by
// subBlue design
// www.subBlue.com

// Startup variables
var imageTag = false;
var theSelection = false;

// Check for Browser & Platform for PC & IE specific bits
// More details from: http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
var clientPC = navigator.userAgent.toLowerCase(); // Get client info
var clientVer = parseInt(navigator.appVersion); // Get browser version

var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
var is_nav = ((clientPC.indexOf('mozilla')!=-1) && (clientPC.indexOf('spoofer')==-1)
                && (clientPC.indexOf('compatible') == -1) && (clientPC.indexOf('opera')==-1)
                && (clientPC.indexOf('webtv')==-1) && (clientPC.indexOf('hotjava')==-1));
var is_moz = 0;

var is_win = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));
var is_mac = (clientPC.indexOf("mac")!=-1);

// Helpline messages
b_help = "Bold text: [b]text[/b]";
i_help = "Italic text: [i]text[/i]";
u_help = "Underline text: [u]text[/u]";
s_help = "Strike text: [st]text[/st]";
q_help = "Quote text: [quote]text[/quote] or [quote=name]text[/quote]";
c_help = "Code display: [code]code[/code]";
l_help = "List: [list][*]text[/list]";
o_help = "Ordered list: [list=1][*]text[/list]";
p_help = "Insert image: [img]http://image_url[/img] or [img=left]http://image_url[/img] or [img=450,right]http://image_url[/img]";
w_help = "Insert URL: [url]http://url[/url] or [url=http://url]URL text[/url]";
a_help = "Close all open BBCode tags";
r_help = "Font color: [color=red]text[/color]  Tip: you can also use color=#FF0000";
f_help = "Font size: [size=x-small]small text[/size] NOTE: do not excessive use and only if necessary";
h_help = "Horizontal Line: [hr]";

// Define the bbCode tags
bbcode = new Array();
bbtags = new Array('[b]','[/b]','[i]','[/i]','[u]','[/u]','[quote]','[/quote]','[code]','[/code]','[list]\n[*]','\n[/list]','[list=1]\n[*]\n','\n[/list]','[img=left]','[/img]','[url]','[/url]','[st]','[/st]', '[hr]');
imageTag = false;

// Shows the help messages in the helpline window
function helpline(help) {
	document.post.helpbox.value = eval(help + "_help");
}


// Replacement for arrayname.length property
function getarraysize(thearray) {
	for (i = 0; i < thearray.length; i++) {
		if ((thearray[i] == "undefined") || (thearray[i] == "") || (thearray[i] == null))
			return i;
		}
	return thearray.length;
}

// Replacement for arrayname.push(value) not implemented in IE until version 5.5
// Appends element to the array
function arraypush(thearray,value) {
	thearray[ getarraysize(thearray) ] = value;
}

// Replacement for arrayname.pop() not implemented in IE until version 5.5
// Removes and returns the last element of an array
function arraypop(thearray) {
	thearraysize = getarraysize(thearray);
	retval = thearray[thearraysize - 1];
	delete thearray[thearraysize - 1];
	return retval;
}

function checkForm() {

	formErrors = false;

	if (document.post.message.value.length < 2) {
		formErrors = "You must enter a message when posting.";
	}

	if (formErrors) {
		alert(formErrors);
		return false;
	} else {
		bbstyle(-1);
		//formObj.preview.disabled = true;
		//formObj.submit.disabled = true;
		return true;
	}
}

function emoticon(text) {
	var txtarea = document.post.message;
	text = ' ' + text + ' ';
	if (txtarea.createTextRange && txtarea.caretPos) {
		var caretPos = txtarea.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
		txtarea.focus();
	} else {
		txtarea.value  += text;
		txtarea.focus();
	}
}

function bbfontstyle(bbopen, bbclose) {
	var txtarea = document.post.message;

	if ((clientVer >= 4) && is_ie && is_win) {
		theSelection = document.selection.createRange().text;
		if (!theSelection) {
			txtarea.value += bbopen + bbclose;
			txtarea.focus();
			return;
		}
		document.selection.createRange().text = bbopen + theSelection + bbclose;
		txtarea.focus();
		return;
	}
	else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))
	{
		mozWrap(txtarea, bbopen, bbclose);
		return;
	}
	else
	{
		txtarea.value += bbopen + bbclose;
		txtarea.focus();
	}
	storeCaret(txtarea);
}


function bbstyle(bbnumber) {
	var txtarea = document.post.message;

	donotinsert = false;
	theSelection = false;
	bblast = 0;

	if (bbnumber == -1) { // Close all open tags & default button names
		while (bbcode[0]) {
			butnumber = arraypop(bbcode) - 1;
			txtarea.value += bbtags[butnumber + 1];
			buttext = eval('document.post.addbbcode' + butnumber + '.value');
			eval('document.post.addbbcode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
		}
		imageTag = false; // All tags are closed including image tags :D
		txtarea.focus();
		return;
	}

	if ((clientVer >= 4) && is_ie && is_win)
	{
		theSelection = document.selection.createRange().text; // Get text selection
		if (theSelection) {
			// Add tags around selection
			document.selection.createRange().text = bbtags[bbnumber] + theSelection + bbtags[bbnumber+1];
			txtarea.focus();
			theSelection = '';
			return;
		}
	}
	else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))
	{
		mozWrap(txtarea, bbtags[bbnumber], bbtags[bbnumber+1]);
		return;
	}

	// Find last occurance of an open tag the same as the one just clicked
	for (i = 0; i < bbcode.length; i++) {
		if (bbcode[i] == bbnumber+1) {
			bblast = i;
			donotinsert = true;
		}
	}

	if (donotinsert) {		// Close all open tags up to the one just clicked & default button names
		while (bbcode[bblast]) {
				butnumber = arraypop(bbcode) - 1;
				txtarea.value += bbtags[butnumber + 1];
				buttext = eval('document.post.addbbcode' + butnumber + '.value');
				eval('document.post.addbbcode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
				imageTag = false;
			}
			txtarea.focus();
			return;
	} else { // Open tags

		if (imageTag && (bbnumber != 14)) {		// Close image tag before adding another
			txtarea.value += bbtags[15];
			lastValue = arraypop(bbcode) - 1;	// Remove the close image tag from the list
			document.post.addbbcode14.value = "Img";	// Return button back to normal state
			imageTag = false;
		}

		// Open tag
		txtarea.value += bbtags[bbnumber];
		if ((bbnumber == 14) && (imageTag == false)) imageTag = 1; // Check to stop additional tags after an unclosed image tag
		arraypush(bbcode,bbnumber+1);
		eval('document.post.addbbcode'+bbnumber+'.value = "    *"');
		txtarea.focus();
		return;
	}
	storeCaret(txtarea);
}

// From http://www.massless.org/mozedit/
function mozWrap(txtarea, open, close)
{
	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 + open + s2 + close + s3;
	return;
}

// Insert at Claret position. Code from
// http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
function storeCaret(textEl) {
	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}
