Array.prototype.pushToCache = pushToCache;
var cache = new Array();
var httpRequest = new createXmlHttpRequestObject();
var workerURL = '/calendar.php';
var updateInterval = 1000;

function pushToCache(strData)
{  if (pagetype == 'presscenter') workerURL = '/calendar.php';
  if (pagetype == 'info') workerURL = '/company_calendar.php';
  this.push(strData);
  requestData();
}


function createXmlHttpRequestObject()
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    // try every prog id until one works
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
    {
      try
      {
        // try to create XMLHttpRequest object
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      }
      catch (e) {}
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else
    return xmlHttp;
}

// main function for sending requests
function requestData()
{
  if (httpRequest)
  {
    try
    {
      if ((httpRequest.readyState == 4) || (httpRequest.readyState == 0))
      {
        var params = '';
        if (cache.length > 0)
        {
          params = cache.shift();
          if (params.length > 0)
          {
            params += '&salt=' + Math.random();
            httpRequest.open("POST", workerURL, true);
            httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            httpRequest.onreadystatechange = requestRecieved;
            httpRequest.send(params);
          }
        }
      }
      else
      {
        setTimeout("requestData();", updateInterval);
      }
    }
    catch(e) { alert(e.toString()); }
  }
}

// recieve answer
function requestRecieved()
{
  // continue if the process is completed
  if (httpRequest.readyState == 4)
  {
    // continue only if HTTP status is "OK"
    if (httpRequest.status == 200)
    {
      try
      {
        // process the server's response
       readRecievedAnswer();
      }
      catch(e)
      {
        // display the error message
        alert(e.toString());
      }
    }
    else
    {
      // display the error message
      alert(httpRequest.statusText);
    }
  }
}

function readRecievedAnswer()
{  var response = httpRequest.responseText;
  var calend_div = document.getElementById('calendar_div');
  var calend = document.getElementById('calendar');
  var curtain = document.getElementById('calendar_curtain');
  calend_div.innerHTML = response + '<div id="calendar_curtain" class="calend_curtain">&nbsp;</div>';
  curtain.style.display = 'none';
  calend.style.display = 'block';
  document.updateTips();}

function requestCalendar(month, year)
{  var calend = document.getElementById('calendar');
  var curtain = document.getElementById('calendar_curtain');
  calend.style.display = 'none';
  curtain.style.display = 'block';
  cache.pushToCache('month=' + month.toString() + '&year=' + year.toString() + '&as=1');
  return false;}