// This code uses parts from http://www.howtocreate.co.uk/tutorials/index.php?tut=0&part=14

function getRefToElement(divID) {
  // customised to suit this particular page - DON'T COPY THIS UNLESS YOU KNOW WHAT YOU ARE DOING!!
  if (document.getElementById)
    return document.getElementById(divID);
  // MSIE
  if (document.all)
    return document.all[divID];
  // Old NS
  if (document.layers)
    return(document.layers[divID+'C'].document.layers[divID]);
  // No idea what uses this...
  if (document[divID+'C'])
    return document[divID+'C'].document[divID];
  // Fail...
  return(false);
}

function xmlhttpChange(URL, ELID, Interval, xmlhttp) {
/*
  var dayarray = new Array("Нед","Пон","Вто","Сря","Чет","Пет","Съб");
  var montharray=new Array("Яну","Фев","Мар","Апр","Май","Юни","Юли","Авг","Сеп","Окт","Ное","Дек");
  // var montharray=new Array("01","02","03","04","05","06","07","08","09","10","11","12");

  var mydate = new Date();
  var year = mydate.getYear();
  if (year < 1000)
    year += 1900;
  var day = mydate.getDay();
  var month = mydate.getMonth();
  var daym = mydate.getDate();
  if (daym < 10)
    daym = "0" + daym;
  var hours = mydate.getHours();
  var minutes = mydate.getMinutes();
  var seconds = mydate.getSeconds()
  if (hours <= 9)
    hours = "0" + hours;
  if (minutes <= 9)
    minutes = "0" + minutes;
  if (seconds <= 9)
    seconds = "0" + seconds;
  var cdate = dayarray[day] + ", " + daym + "." + montharray[month] + "." + year + " " + hours + ":" + minutes + ":" + seconds;
*/
  if (xmlhttp.readyState == 4) { // if xmlhttp shows "loaded"  - NA for local files
    if ((xmlhttp.status == 200) || (xmlhttp.status == 304)) { // if "OK"
      // TODO: Check if getRefToElement returns false
// Also consider the xmlhttp.status also varies from browser to browser.
// (xmlhttp.status==404) // file not found
// (xmlhttp.status==200) // file found and loaded
// (xmlhttp.status==304) // file found, but determined unchanged and loaded from cache
// Opera 8.x really loves that last status, I haven't noticed it with Mozilla or IE6
// yet. I don't have a Mac to test Safari.
      getRefToElement(ELID).innerHTML = unescape(xmlhttp.responseText);// + "<br />Updated: " + cdate;
      xmlhttp = false;
      if (Interval > 0)
        setTimeout('loadXMLDoc("' + URL + '", "' + ELID + '", ' + Interval + ')' , Interval);
    } else {
      if (Interval > 0)
        setTimeout('loadXMLDoc("' + URL + '", "' + ELID + '", ' + Interval + ')' , Interval*4);
    }
  }
  /* else { // Getting 1, 1, 2, 3 here. Have no idea what they mean, progressbar? } */
}

function loadXMLDoc(URL, ELID, Interval) {
  // @URL       - Script to generate the contents / static document
  // @ELID      - Element ID
  // @Interval  - in what interval should the contents be reloaded. 0 means once only
  var xmlhttp = false;
  var xmlhttpChangeFunction = function() {
    xmlhttpChange(URL, ELID, Interval, xmlhttp);
  };
  if (window.XMLHttpRequest) {        // code for Mozilla, etc.
    xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = xmlhttpChangeFunction;
    xmlhttp.open("GET", URL, true);
    xmlhttp.send(null);
  } else if (window.ActiveXObject) {  // code for IE
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
    if (xmlhttp) {
      xmlhttp.onreadystatechange = xmlhttpChangeFunction;
      xmlhttp.open("GET", URL, true);
      xmlhttp.send();
    }
  }
  if (!xmlhttp) {
    alert("Browser too old, you'll have to reload this page manually.");
  }
}

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