/* regular expressions */

var EMPTY = /^\s*$/;

function switchforum(x)
{
	var s = x.options[x.selectedIndex];
	if (s.value != '')
	{
		if (s.id == 'switchCat')
			this.location.href = board_script_url +'/list_category/'+ s.value;
		else
			this.location.href = board_script_url +'/list_topics/' + s.value;
	}
}

function switchdestinationforum(x)
{
	var s = x.options[x.selectedIndex];
	if (s.id == 'switchCat' || s.value == '')
	{
		x.selectedIndex = 0;
		return false;
	}
	return true;
}

function switchpage(x,topicid,keywords)
{
	var s = x.options[x.selectedIndex].value;
	if (s != '') this.location.href = board_script_url +'/list_messages/'+topicid+'/' + s + (typeof keywords != 'undefined' ? '//' + keywords : '');
}

function switchquotepage(s)
{
	if (s != '')
	{
		document.forms[0].elements['data[offset]'].value = s;
		document.forms[0].elements['data[type]'].value = 'pagethru';
		document.forms[0].submit();
	}
}

function switchfaqpage(x)
{
	var s = x.options[x.selectedIndex].value;
	if (s != '') this.location.href = board_script_url +'/faq/' + s;
}

function switchforumpage(x,forumid)
{
	var s = x.options[x.selectedIndex].value;
	if (s != '') this.location.href = board_script_url +'/list_topics/'+forumid+'/' + s;
}

function switchbookmarkpage(x)
{
	var s = x.options[x.selectedIndex].value;
	if (s != '') this.location.href = board_script_url +'/list_bookmarks/' +s;
}

function selectsearchforum(x)
{
	var i = x.options.length;
	while (i--)
	{
		if (x.options[i].id == 'switchCat') x.options[i].selected = false;
	}
}

function selectfavouriteforum(x)
{
	var s = 0, i = x.length;
	while (i--)
	{
		if (x.options[i].id == 'switchCat') x.options[i].selected = false;
		else if (x.options[i].selected)
		{
			if (s == 3) x.options[i].selected = false;
			else s++;
		}
	}
}

function bookmarkCheck(checkbox)
{
	var el = checkbox.form.elements, i = el.length, all_checked = true;
	while (i--)
	{
		if (el[i].type == 'checkbox' && el[i].name != 'checkem' && !el.checked)
		{
			all_checked = false;
			break;
		}
	}
	el['checkem'].checked = all_checked;
}

function checkEmAll(checkbox)
{
	var el = checkbox.form.elements, i = el.length;
	while (i--)
	{
		if (el[i].type == 'checkbox' && el[i].name != 'checkem')
			el[i].checked = checkbox.checked;
	}
}

function quote_title(messageid, user)
{
	if (topicname.length > 40)
		topicname = topicname.substr(0,40)+'...';
	quote('[message='+messageid+']'+user+' in "'+topicname+'"[/message]\n');
}

function quote_user(userid, user)
{
	quote('[user='+userid+']'+user+'[/user]\n');
}

function quote(string)
{
	var target = document.getElementById( 'messageBox' );
	target.value += string ;
	target.focus();
}

function q()
{
	return '"';
}

function bookmark_dialog(form,type)
{
	var titel = form['data[name]'].value;
	if (type == 1)
	{
		titel += ' in "'+document.forms['bookmark_form'].elements['data[name]'].value+'"';
		if (titel.length > 60) titel = titel.substr(0,56)+'..."';
	}

	var string = prompt('Onder welke titel wilt u de bookmark bewaren?',titel);
	if (string == null)
	{
		return false;
	}
	if (EMPTY.test(string))
	{
		alert('U heeft geen titel opgegeven.');
		return false;
	}
	else
	{
		form['data[name]'].value = string;
		return true;
	}
}

function calcCharLeft_Fast(f,maxLength,s)
{
	var str = f.elements[s].value, len = str.length, re = /[&<>"]/g, c = re.exec(str);
	while (c != null)
	{
		switch(c[0])
		{
			case '&':
				len += 4;
				break;
			case '<':
				len += 3;
				break;
			case '>':
				len += 3;
				break;
			case '"':
				len += 5;
				break;
		}
		c = re.exec(str);
	}

	if (len > maxLength)
	{
		f.elements[s].value = calcCharLeft_Extended(maxLength,str);
		calcCharLeft_Fast(f,maxLength,s);
		return false;
	}

	f.elements['charsleft'].value = maxLength - len;
	return true;
}

function calcCharLeft_Extended(maxLength,str)
{
	var i = 0, j = str.length, len = 0;
	do
	{
		switch(str.charAt(i))
		{
			case '&':
				len += 5;
				break;
			case '<':
				len += 4;
				break;
			case '>':
				len += 4;
				break;
			case '"':
				len += 6;
				break;
			default:
				len++;
		}
	} while (len <= maxLength && ++i < j);

	return str.substring(0,i);
}

function showtopic(topicid)
{
	if (topicid.match(/^[1-9]\d*$/))
	{
		window.open(board_script_url +'/list_messages/'+topicid,'Topic');
	}
	else
	{
		alert('Het topic id is geen getal of niet ingevuld.');
	}
}

function switchcss(x)
{
	var n = x.selectedIndex, el = x.form.elements, i = 0;
  	do
	{
		el['data[csschoice'+i+']'].value = (1&n)+'';
		n = n >>> 1;
	} while (++i < 3);
}

function enableSplitIds(x)
{
	var s = x.checked ? '' : 'none', i = messageids.length;
	while (i--)
	{
		document.getElementById('div_split_'+messageids[i][0]).style.display = s;
	}
}

function enableDelIds(x)
{
	var s = x.checked ? '' : 'none', i = messageids.length;
	while (i--)
	{
		document.getElementById('div_del_'+messageids[i][0]).style.display = s;
	}
}

function collectIds(action)
{
	var t = document.getElementById(action+'_messageids');
	if (t != null)
	{
		t.value = '';
		var el, i = 0, j = messageids.length;

		if (document.getElementById(action).checked)
		{
			do
			{
				el = document.getElementById('mark_'+action+'_messageid_'+messageids[i][0]);
				if (el.checked) t.value += (i > 0 ? ',' : '') + el.value;
			} while (++i < j);
		}
	}
}

function topicsearch(form)
{
	if (EMPTY.test(form.elements['data[filter_keywords]'].value))
	{
		form.elements['data[filter_keywords]'].focus();
		return false;
	}
	return true;
}

function quicksearch(form)
{
	if (EMPTY.test(form.elements['data[q]'].value))
	{
		form.elements['data[q]'].focus();
		return false;
	}

	var searchtype='all', w = form.elements['where'], i = w.length;
	while (i--)
	{
		if (w[i].checked)
		{
			searchtype = w[i].value;
			break;
		}
	}

	switch (searchtype)
	{
		case 'all':
			form.elements['data[forums][]'].disabled = true;
			return true;
		case 'google':
			window.open('http://www.google.com/search?q='+escape(form.elements['data[q]'].value), '_blank');
			return false;
		default:
			form.elements['data[forums][]'].value = searchtype;
			form.elements['data[forums][]'].disabled = false;
			return true;
	}
}

function checksearchform(form)
{
	if (EMPTY.test(form.elements['data[q]'].value)
		&& EMPTY.test(form.elements['data[poster]'].value)
		&& EMPTY.test(form.elements['data[topicstarter]'].value))
	{
		alert('Geen trefwoorden / poster of topicstarter opgegeven');
		form.elements['data[q]'].focus();
		return false;
	}
	return true;
}

function checkedituserform(form)
{
	if (EMPTY.test(form.elements['data[pwd]'].value))
	{
		alert('Voer je wachtwoord in ter bevestiging');
		form.elements['data[pwd]'].focus();
		return false;
	}
	return true;
}

function scrolltoanchor()
{
	var h = window.location.hash;
	if (h != '') window.location.hash = h;
}

function scrolltoresults()
{
	if (navigator.userAgent.indexOf('Opera') > -1)
	{
		window.onload = function() { if (document.body.scrollTop == 0) scrollTo(0, document.getElementsByName('hitstart').item(0).offsetTop || 0); }
	}
	else if (window.location.hash == '')
	{
		if (document.all) window.location.hash = '#hitstart';
		else window.onload = function() { window.location.hash = '#hitstart'; }
	}
}

function encrypt_password(form)
{
	if (form.elements['data[passwordencryption]'].checked)
	{
		if (md5_vm_test())
		{
			var pwd = form.elements['data[password]'].value;
			var uname = form.elements['data[username]'].value;
			var reactid = form.elements['data[reactid]'].value;
			form.elements['data[password]'].value = hex_md5(hex_md5(pwd)+uname+reactid);
		}
		else
		{
			form.elements['data[passwordencryption]'].checked = false;
			return confirm('Je browser is niet in staat je wachtwoord te versleutelen\n\nLog in zonder versleuteling?');
		}
	}
	return true;
}

function setdata(form,data)
{
	var s = form.elements[data], a=[];
	if (s != null) {
		if (typeof s.length != 'undefined')
		{
			var i = s.length;
			while (i--) a[s[i].id] = s[i].value;
		}
		else a[s.id] = s.value;
		form.elements['data['+data+']'].value = serialize(a);
	}
}

function unserialize(data)
{
	re = /(\w):([\d.]+)[:;]/g;
	return unserialize_do(data);
}

function unserialize_do(data)
{
	var cur, ret = '';
	if ((cur = re.exec(data)) !== null)
	{
		switch (cur[1])
		{
			case 'a':
				ret = [];
				var i = cur[2];
				while (i--) ret[unserialize_do(data)] = unserialize_do(data);
				break;
			case 'o':
				ret = {};
				var i = cur[2];
				while (i--) ret[unserialize_do(data)] = unserialize_do(data);
				break;
			case 's':
				ret = ''+data.substr(re.lastIndex+1, cur[2]);
				re.lastIndex += cur[2]-1;
				break;
			case 'i':
				ret = cur[2]/1;
				break;
			case 'd':
				ret = cur[2]/1;
				break;
			case 'b':
				ret = cur[2] ? true : false;
				break;
		}
	}
	return ret;
}

function serialize(data)
{
	return typeof data.toPHP == 'function' ? data.toPHP() : '';
}

Number.prototype.toPHP=function()
{
	return (Math.floor(this) == this ? 'i' : 'd') + ':' + this + ';';
}

String.prototype.toPHP=function()
{
	var s = this.replace(/\r\n|\r|\n/g, '\r\n');
	return 's:' + s.length + ':"' + s + '";';
}

Boolean.prototype.toPHP=function()
{
	return 'b:' + (this==true ? 1 : 0) + ';';
}

Array.prototype.toPHP=function()
{
	var o = this, s = '', count = 0, item;
	for (item in o)
	{
		if (typeof o[item] == 'function' || typeof o[item] == 'undefined') continue;
		count++;
		if (isNaN(item)) s += 's:' + item.length + ':"' + item + '";';
		else s += 'i:' + item + ';';
		if (o[item] === null) s +='N;';
		else s += o[item].toPHP();
	}
	return 'a:' + count + ':{' + s + '}';
}

Object.prototype.toPHP=function()
{
	var o = this, s = '', count = 0, item;
	for (item in o)
	{
		if (typeof o[item] == 'function' || typeof o[item] == 'undefined') continue;
		count++;
		if (isNaN(item)) s += 's:' + item.length + ':"' + item + '";';
		else s += 'i:' + item + ';';
		if (o[item] === null) s +='N;';
		else s += o[item].toPHP();
	}
	return 'o:' + count + ':{' + s + '}';
}