function $() {
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1)
      return element;

    elements.push(element);
  }

  return elements;
}

function createXHR()
{
	if (window.XMLHttpRequest) // code for Mozilla, etc.
  {
  	xmlHttp = new XMLHttpRequest();
  }
	else if (window.ActiveXObject) // code for IE
  {
	  xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
  }
  else
  {
	  alert('Helaas, uw browser ondersteunt geen XMLHttpRequest objecten!');
  }
  return xmlHttp;
}

function xhrGET(id, url)
{
	xmlHttp = createXHR();
	
	xmlHttp.open('GET', url, true);
	xmlHttp.onreadystatechange = function()
		{
			if (xmlHttp.readyState == 4 && xmlHttp.status == 200) // if xmlhttp shows 'loaded' and 'OK'
		  {
			  if (xmlHttp.responseText)
			  {
			  	$(id).innerHTML = xmlHttp.responseText;
				}
				else
			  {
			  	alert('Geen inhoud terug gekregen!');
			  }
		  }
		  if (xmlHttp.readyState == 4 && xmlHttp.status != 200)
		  {
			  alert('Er is iets fout gegaan met het ophalen van de pagina (xmlHttp status ' + xmlHttp.status + ')');
		  }
		};
	xmlHttp.send(null);
}

function URLDecode(text)
{
	// Replace + with ' '
	// Replace %xx with equivalent character
	// Put [ERROR] in output if %xx is invalid.
	var HEXCHARS = '0123456789ABCDEFabcdef'; 
	var encoded = text;
	var plaintext = '';
	var i = 0;
	while (i < encoded.length)
	{
		var ch = encoded.charAt(i);
		if (ch == '+')
		{
			plaintext += ' ';
			i++;
		}
		else if (ch == '%')
		{
			if (i < (encoded.length-2) && HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 && HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 )
			{
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			}
			else
			{
				alert( '[URLDecode] Bad escape combination near ...' + encoded.substr(i) );
				plaintext += '%[ERROR]';
				i++;
			}
		}
		else
		{
			plaintext += ch;
			i++;
		}
	} // while
	return plaintext;
}

function toggle(id)
{
	$(id).style.display = ($(id).style.display == 'none')?'block':'none';
}

function validate(regstr, str)
{
	if (window.RegExp)
	{
		reg = new RegExp(regstr);
		return reg.test(str);
	}
	return false;
}
