
function submitForm() {

	var errmsg = '';

	target = document.getElementById('subjectBox');
	if (target && target.value.length == 0)	    errmsg += '- Subject (missing)\n';
	if (target && target.value.length > 150)	errmsg += '- Subject (too large - max 150 chars)\n';

	target = document.getElementById('messageBox');
	if (target && target.value.length == 0)	    errmsg += '- Message (missing)\n';
	if (target && target.value.length > 65535)	errmsg += '- Message (too large - max 65.535 chars)\n';

	target = document.getElementById('option_rss');
	target2 = document.getElementById('option_staff');

	if (target.checked == true && target2.checked == true) errmsg += '- Options (choose either rss/staff only, you can\'t pick both)\n'; 
	
	if (errmsg.length != 0)
	{
		alert('Incorrect or incomplete data for the following fields:\n\n' + errmsg + '\nPlease fix these errors and try submitting this form again.');
		return false;
	}

	return true;
}

function submitComment() {
	var errmsg = '';

	target = document.getElementById('commentBox');
	if (target && target.value.length == 0)	    errmsg = 'you didn\'t enter a comment.\n';

	if (errmsg.length != 0) {
		alert('The comment can\'t be added because ' + errmsg + '');
		return false;
	}
	return true;
}


/* 
	MessageBox functions for UBB tags
	--------------------------------------------------------------------
	(c) 2004-2005, BServed Hosting 

	Written by: Bas Verhoeven (js@bserved.nl) for NordicBots

	Feel free to use this code, but leave this copyright notice. I like
	to have my name spammed :)
*/

var target = null;
function initMessageBox()
{
	target = document.getElementById('messageBox');
	if (target)
	{
		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;
		}
	} else {
		alert('Warning: Could not find messagebox. BBCode toolbar disabled.');
	}
}

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 '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('Enter a hexadecimal color:','#');
			if (val !== null) text = '[bgcolor='+val+']'+text+'[/bgcolor]';
			break;
		case 'color':
			val = prompt('Enter a hexadecimal color:','#');
			if (val !== null) text = '[color='+val+']'+text+'[/color]';
			break;
		case 'url':
			if (/^(http:\/\/|www\.)/i.test(text))
			{
				val = prompt('Enter a description:', text);
				if (val !== null && val != '') text = '[url='+text+']'+val+'[/url]';
			}
			else
			{
				val = prompt('Enter the URL:','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('Enter the URL to the image:','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;
	}

	return text;
}

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

function putExt(type, text)
{
	if (target)
	{
		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;
	if (!e)	e = window.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 cancelEvent(e)
{
	if (typeof e.preventDefault != 'undefined')
	{
		e.preventDefault();
	}
	else if (typeof e.cancelBubble != 'undefined')
	{
		if (e.keyCode) e.keyCode = 0;
		e.returnValue = 0;
		e.cancelBubble = true;
	}

	return false;
}

