function imfFetch(url, target) {
  document.getElementById(target).innerHTML = 'Fetching data...';
  var req;
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (req != undefined) {
    req.onreadystatechange = function() {imfFetchDone(req, url, target);};
    req.open('GET', url, true);
    req.send('');
  }
  else {
    document.getElementById(target).innerHTML = 'Error: Failed to fetch data.';
  }
}  

function imfFetchDone(req, url, target) {
  if (req.readyState == 4) { // only if req is "loaded"
    if (req.status == 200) { // only if "OK"
      document.getElementById(target).innerHTML = req.responseText;
    } else {
      document.getElementById(target).innerHTML='Error: Failed to fetch data.';
    }
  }
}

function imfAddLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function imfFillDivs() {
  var prefix = 'imf_guest_'; // should be lower case
  var divs = document.getElementsByTagName('div');
  var host = window.location.hostname;
  for(var i = 0; i < divs.length; i++) {
    var id = divs[i].getAttribute('id');
    var thisPrefix;
    if(id) {
      thisPrefix = divs[i].getAttribute('id').substring(0, prefix.length);
    }
    else {
      continue;
    }
    if(thisPrefix.toLowerCase() == prefix.toLowerCase()) {
//      var id = divs[i].getAttribute('id');
      var args = id.split('_');
      var groups = '';
      var time = '';
      var box = '';
      for (var j = 0; j < args.length; j++) {
        if (args[j].substring(0,7).toLowerCase() == 'groups:') {
          groups = args[j].substring(7);
        }
        else if (args[j].substring(0,5).toLowerCase() == 'time:') {
          time = args[j].substring(5);
        }
        else if (args[j].substring(0,4).toLowerCase() == 'box:') {
          box = ';box';
        }
      }
      if (time != '' && groups != '') {
        imfFetch('http://' + host + '/cgi-bin/guestlookup.cgi?research_group=' + groups + ';time=' + time + box, id);
      }
      else if (time != '') {
        imfFetch('http://' + host + '/cgi-bin/guestlookup.cgi?time=' + time + box, id);
      }
      else if (groups != '') {
        imfFetch('http://' + host + '/cgi-bin/guestlookup.cgi?research_group=' + groups + box, id);
      }
    }
  }
}

// if the script is loaded after the main page content we can begin
// immediately. Otherwise we must wait the entire page to load.
if (document.getElementById('footer')) {
  imfFillDivs();
}
else {
  imfAddLoadEvent(function() {
    imfFillDivs();
  });
}

//if (document.getElementById('guests')) {
//  imfFetch('http://www.math.ku.dk/cgi-bin/guestlookup.cgi?research_group=top', 'guests');
//  imfFillDivs();
//}
//else {
//  imfAddLoadEvent(function() {
//    imfFetch('http://www.math.ku.dk/cgi-bin/guestlookup.cgi?research_group=top', 'guests');
//    imfFillDivs();
//});
//}


