function ajaxRequest(uri,handler,post, async, theBox){
 //alert('async'+async);
  var xmlhttp = null;
  var httpMethod = 'GET';

  // Mozilla/Safari/Opera
  if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest();
    if(xmlhttp.overrideMimeType){ // opera doesn't support this
     // alert("override");
      xmlhttp.overrideMimeType("text/plain");
    }
  }
  // IE
  else if (window.ActiveXObject) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e) {
      try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      } catch(e) {
        return false;
      }
    }
  }
  if (!xmlhttp) {
    if (arguments.length>0) {
      var throwException = new Error('XMLHttpRequest not supported in this browser!');
      throwException.name = 'AJAXREQUEST_NOT_SUPPORTED';
    }
    return false;
    // Used to test availability of XMLHttpRequest object rest is skipped
    } else if (arguments.length==0){
    return true;
  }


  if (post) httpMethod = 'POST';
  xmlhttp.open(httpMethod, uri, async);
  xmlhttp.setRequestHeader('Content-Type', 'text/xml');
  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState==4) {
      try {
        if (xmlhttp.status==200) {
          handler(xmlhttp.responseText, theBox);
        } else {
          // File could not be loaded probably 404
          var throwException = new Error('Unable to load\n' +
                                          xmlhttp.responseText);
          throwException.name = 'AJAXREQUEST_LOAD_ERROR';
          throw(throwException);
        }
      } catch(exception) {
          var throwException = new Error('Unable to parse response\n' +
                                          exception);
          throwException.name = 'AJAXREQUEST_PARSE_ERROR';
          throw(throwException);
      } finally {
        xmlhttp = null;
      }
    }
  };
 /* alert('created XML');*/
  xmlhttp.send(post);
  if (!window.ActiveXObject){
    handler(xmlhttp.responseText, theBox)
  }
}


function getBox(theBox, uri){
  setTimeout('goBox(\''+theBox+'\',\''+uri+'\');',600);
}

function goBox(theBox, uri){
  var query =  document.getElementById(theBox+'Query').value;
  /*  if( query.length > 1 ) { */

//alert(uri + query);

    var theBoxElement = document.getElementById(theBox+'Container');
    if( document.getElementById(theBox+'Support') ) {
      ajaxRequest(uri+query+'&support='+document.getElementById(theBox+'Support').checked+'&rand='+Math.floor(Math.random()*10000), doBox, '', true, theBoxElement);
    } else {
      ajaxRequest(uri+query+'&rand='+Math.floor(Math.random()*10000), doBox, '', true, theBoxElement);
    }
  /* }  */
}

function doBox(result, theBox){
   if (browser.winIE6Down){
       theBox.style.display = 'block';
   } else {
     theBox.style.visibility = 'visible';
   }
   theBox.innerHTML = result;
}

function focusResultBox(theBox, result){
  document.getElementById(theBox+'Query').value = result.firstChild.firstChild.innerHTML;
  noBox(theBox);
  setTimeout(function(){document.getElementById(theBox+'Form').submit()},500);
}

var noBoxTimeoutVar;
function noBoxTimeout(theBox){
  noBoxTimeoutVar = setTimeout('noBox(\''+theBox+'\')', 500);
}

function noBox(theBox){
  theBox = document.getElementById(theBox+'Container');
   if (browser.winIE6Down){
    theBox.style.display = 'none';
  } else {
    theBox.style.visibility = 'hidden';
  }
}

function setElInnerHTML(innerSnippet, target){
  if(browser.iE){
   var targetElement = document.getElementById(target).childNodes[0];
  }else{
    var targetElement = document.getElementById(target).childNodes[1];
  }
  targetElement.innerHTML = innerSnippet;
}

function setSpecContent(contentName){
  /* Determine current tab */
  navRoot = document.getElementById("expertmenu");
  for(var i=0;i<navRoot.childNodes.length;i++){
   if(navRoot.childNodes[i].className!=undefined){
     navRoot.childNodes[i].className =  navRoot.childNodes[i].className.split('selected')[0];
   }
  }

  /* Hide all tab contents */
  tabContentRoot = document.getElementById("columnone");
  for(var i=0;i<tabContentRoot.childNodes.length;i++){
    if(tabContentRoot.childNodes[i].className=='padding') {
      var contentRoot = tabContentRoot.childNodes[i];
      for(var i=0;i<contentRoot.childNodes.length;i++){
        if(contentRoot.childNodes[i].className=='webwijs_content'){
          contentRoot.childNodes[i].style.display='none';
        }
      }
    }
  }

  /* Display selected tab */
  if (document.getElementById('exmenu'+contentName)) document.getElementById('exmenu'+contentName).className += ' selected';
  if (document.getElementById('expert')) document.getElementById('expert').style.display = 'block';
  if (document.getElementById('expertmenu')) document.getElementById('expertmenu').style.display = 'block';
  if (document.getElementById('content_'+contentName)) document.getElementById('content_'+contentName).style.display = 'block';
};

/* end WSC */

/*  UvT functions */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function wwSelectTab(){
  // Select a tab
  if( document.getElementById('expertmenu') ) {
    var tab = document.location.href.match(/tab=(\w+)/);
      if( tab != null ) {
        var select = tab[1].toLowerCase();
        setSpecContent(select);
    } else {
      if (document.getElementById('wpobp').firstChild.nodeValue=='wp') {
        setSpecContent('profiel');
      } else {
        setSpecContent('contact');
      }
    }

    // Move to anchor position, if any
    var anchor = document.location.href.match(/#(\w+)/);
    if( anchor != null ) {
      var anchorname = anchor[1].toLowerCase();
      var pos = getAnchorPosition(anchorname);
      window.scrollTo(0,pos.y);
    }
  }

  var logo1 = document.getElementById('logoid1');

  if (logo1)
  {
    var p = logo1.parentNode.parentNode;
    
    var wp = p.clientWidth;
    var hp = p.clientHeight;

    var im = new Image();
    im.src = logo1.getAttribute('src');

    wt = im.width;
    ht = im.height;

    logo1.style.position = 'relative';
    logo1.style.top      = Math.round((hp-ht)/2)  + 'px';
    logo1.style.left     = Math.round((wp-wt)/2)  + 'px';

    flits();
  }
}

var n_flits = 1;

function flits()
{
  var logo1 = document.getElementById('logoid' + n_flits);
  n_flits++;
  var logo2 = document.getElementById('logoid' + n_flits);

  if (!(logo2))
  {
    n_flits=1;
    logo2 = document.getElementById('logoid' + n_flits);
  }

  if (!(logo1 == logo2))
  {
    logo1.style.display='none';
    logo2.style.display='block';


    setTimeout('flits()',5000);
  }
}

function centerPiece()
{
  var cnt = 1;
  var thiz = document.getElementById('logoid' + cnt);

  if (thiz)
  {
    var  p = thiz.parentNode.parentNode;
    var wp = p.clientWidth;
    var hp = p.clientHeight;    

    while(thiz)
    {
      var im = new Image();
      im.src = thiz.getAttribute('src');

      wt = im.width;
      ht = im.height;

      thiz.style.position = 'relative';
      thiz.style.top      = Math.round((hp-ht)/2)  + 'px';
      thiz.style.left     = Math.round((wp-wt)/2)  + 'px';
      cnt++;
      thiz = document.getElementById('logoid' + cnt);      
    } 
  }
}

function interLink(tab,anchorName)
{
  setSpecContent(tab);
  document.location.hash=anchorName;
}

function initPage()
{
  centerPiece();
  wwSelectTab();
}

addLoadEvent(initPage);

