function loaded()
{
  ajaxClient = new AjaxClient();
  setHeight();
}

function showDiv(id)
{
  var el = document.getElementById(id);
  if (el != null)
  {
    if (el.style.display == "") el.style.display = "none";
    else el.style.display = "";
  }
}

var xmlHttp;
var keyUpDelay = null;

function stateChanged() 
{ 
  if (xmlHttp.readyState==4) document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
}

function clearHints(field)
{
  while (field.hasChildNodes()) field.removeChild(field.lastChild);
}

function showHints()
{
  var hintDiv = getDiv("hintDiv");
  if (hintDiv!=null && hintDiv.div!=null) clearHints(hintDiv.div);
  var keywords = document.getElementById("searchBox").value;
  if (keywords.length>0)
  {
    if (keyUpDelay != null) clearTimeout(keyUpDelay);
    keyUpDelay = setTimeout("getHints()",250);
  }
  else hintDiv.div.style.display = "none";
}

function getHints()
{
  var keywords = document.getElementById("searchBox").value;
  ajaxClient.sendHttpRequest("GET","hints.jsp?keywords="+encodeURIComponent(keywords),updateHints);
  document.getElementById("hintDiv").style.display="block";
}

function updateHints(xmlHttp)
{
  var doc = xmlHttp.responseXML.documentElement;
  if (doc.nodeName == "suggestions")
  {
    var hintDiv = getDiv("hintDiv");
    var items = doc.childNodes;
    if (items.length==0) hintDiv.div.style.display = "none";
    else
    {
      var hideDiv = true;
      for (var n=0; n<items.length; ++n)
      {
        if (items[n].nodeType == 1 && items[n].nodeName == "keyword") // skip text nodes
        {
          hideDiv = false;
          break;
        }
      }
      if (hideDiv) hintDiv.div.style.display = "none";
      else
      {
     	for (var n=0; n<items.length; ++n)
	    {
	      if (items[n].nodeType == 1 && items[n].nodeName == "keyword") // skip text nodes
	      {
	        var div = document.createElement("div");
	        div.style.width = "100%";
	        div.style.styleFloat = "left";
	        div.setAttribute("tabindex","0",0);
	        div.setAttribute("id","hint"+n);
	        if (items[n+1] != null && items[n+1].nodeType == 1 && items[n+1].nodeName == "keyword") div.setAttribute("nextid","hint"+(n+1));
	        if (items[n-1] != null && items[n-1].nodeType == 1 && items[n-1].nodeName == "keyword") div.setAttribute("previousid","hint"+(n-1));
	        div.onmouseover = function() { this.style.backgroundColor = "#d0eb15"; this.style.cursor = "pointer"; };
	        div.onmouseout  = function() { this.style.backgroundColor = ""; };
	        div.onfocus 	= function() { this.style.backgroundColor = "#d0eb15"; this.style.cursor = "pointer"; };
	        div.onblur 		= function() { this.style.backgroundColor = ""; };
	        div.onclick 	= function() { useHint(this.firstChild.firstChild.data); };
	        div.onkeydown 	= function(e) { hintKeyFunction(this, hintKey(e), this.firstChild.firstChild.data); };
	        hintDiv.div.appendChild(div);
	        div2 = document.createElement("div");
	        text = document.createTextNode(items[n].firstChild.data);
	        div2.appendChild(text);
	        div2.style.width = "100%";
	        div2.style.margin = "2px 5px";
	        div.appendChild(div2);
	      }
	    }
      }
    }
  }
}

function useHint(word)
{
  if (word.indexOf(" ")>-1) word = "'"+word+"'";
  document.getElementById("searchBox").value = word;
  document.getElementById("hintDiv").style.display = "none";
  document.forms.searchForm.submit();
}

function hintKey(e)
{
  if (!e) var e = window.event;
  if (e.which) return e.which;
  else if (e.keyCode) return e.keyCode;
  return null;
}

function checkFocus(key)
{
  if (key==40 && document.getElementById("hint0")!=null) { document.getElementById("hint0").focus(); event.returnValue = false; }		  	// IE
  else if (key==40 && document.getElementById("hint1")!=null) { document.getElementById("hint1").focus(); event.returnValue = false; }	// Firefox
}

function hintKeyFunction(el,key,word)
{
  if (key==13) useHint(word);
  if (key==38)
  {
	if (el.getAttribute("previousid")) document.getElementById(el.getAttribute("previousid")).focus();
    event.returnValue = false;
	return false;
  }
  if (key==40)
  {
	if (el.getAttribute("nextid")) document.getElementById(el.getAttribute("nextid")).focus();
    event.returnValue = false;
	return false;
  }
  return true;
}

function hintKeySubmit(key,elID)
{
  if (key==13) document.getElementById(elID).submit();
}

function clientHeight()
{
  return filterResults(window.innerHeight?window.innerHeight:0,document.documentElement?document.documentElement.clientHeight:0,document.body?document.body.clientheight:0);
}

function filterResults(n_win, n_docel, n_body)
{
  var n_result = n_win ? n_win : 0;
  if (n_docel && (!n_result || (n_result > n_docel))) n_result = n_docel;
  return n_body && (!n_result || (n_result > n_body))?n_body:n_result;
}

function setHeight()
{
  var left   = getDiv("bottomLeft").getHeight()+11;
  var middle = getDiv("bottomMiddle").getHeight()+11;
  var right  = getDiv("bottomRight").getHeight()+11;
  var window = clientHeight()-223;
  var heighest = left;
  if (middle>heighest) heighest = middle;
  if (right>heighest)  heighest = right;
  if (window>heighest) heighest = window;
  document.getElementById("bottomContainer").style.height = heighest+"px";
}

function toggleBold(id)
{
  var el = document.getElementById(id);
  if (el != null)
  {
    if (el.style.fontWeight == "" || el.style.fontWeight == "normal") el.style.fontWeight = "bold";
    else el.style.fontWeight = "normal";
  }
}

function checkEmail(address)
{
  var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
  if (filter.test(address)) return true;
  else return false;
}

function getCheckedValue(radioObj)
{
  if (!radioObj) return "";
  var radioLength = radioObj.length;
  if (radioLength == undefined)
  {
	if (radioObj.checked) return radioObj.value;
	else return "";
  }
  for (var i = 0; i < radioLength; i++)
  {
	if (radioObj[i].checked) return radioObj[i].value;
  }
  return "";
}

function checkForm(formName)
{
  var form  = document.getElementById(formName);
  var error = false;
  var geslacht = getCheckedValue(form.geslacht);
  var licentie = getCheckedValue(form.licentie);
  var errorMsg = "De volgende velden zijn niet (correct) ingevuld:\n";
  if (form.aanbieder.value == null || form.aanbieder.value == "") 			                { error = true; errorMsg += "\n- naam aanbieder"; }
  if (form.leidinggevende.value == null || form.leidinggevende.value == "")  		        { error = true; errorMsg += "\n- naam leidinggevende"; }
  if (form.factuuradres.value == null || form.factuuradres.value == "") 	   			    { error = true; errorMsg += "\n- factuuradres"; }
  if (form.correspondentieadres.value  == null || form.correspondentieadres.value == "")   { error = true; errorMsg += "\n- correspondentieadres"; }
  if (form.voornaam.value == null || form.voornaam.value == "") 						    { error = true; errorMsg += "\n- voornaam"; }
  if (form.achternaam.value == null || form.achternaam.value == "") 					    { error = true; errorMsg += "\n- achternaam"; }
  if (geslacht == null || geslacht == "") 												    { error = true; errorMsg += "\n- geslacht"; }
  if (form.email.value == null || form.email.value == "" || !checkEmail(form.email.value)) { error = true; errorMsg += "\n- e-mail"; }
  if (licentie == null || licentie == "") 												    { error = true; errorMsg += "\n- in bezit van geldige licentie"; }
  if (error) alert(errorMsg);
  else document.getElementById(formName).submit();
}