var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject()
{
  var xmlHttp;
  
  try
  {
    xmlHttp = new XMLHttpRequest();
  }
  catch (e)
  {
    var XmlHttpVersions = new Array("Msxml2.XMLHTTP.6.0",
                                    "Msxml2.XMLHTTP.5.0",
                                    "Msxml2.XMLHTTP.4.0",
                                    "Msxml2.XMLHTTP.3.0", 
                                    "Msxml2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    for (var i = 0; i < XmlHttpVersions.length && !xmlHttp; i++)
    {
      try
      {
         xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      }
      catch (e)
      {
      }
    }
  }
  
  if (!xmlHttp) {
    alert("Error creating the XMLHttpRequest object.");
  } else {
    return xmlHttp;
  }
}

function handleRequestStateChange()
{
  if (xmlHttp.readyState == 4)
  {
    if (xmlHttp.status == 200)
    {
      try
      {
        handleServerResponse();
      }
      catch (e)
      {
        alert ("Error reading the response: " + e.toString());
      }
    } else {
      alert("There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
}

function handleServerResponse()
{
  requestTrigger = false;

  var xmlResponse = xmlHttp.responseXML;
  
  if (!xmlResponse || !xmlResponse.documentElement) {
    throw("Invalid XML structure:\n" + xmlHttp.responseText);
  }
  
  var rootNodeName = xmlResponse.documentElement.nodeName;
  
  if (rootNodeName == "parsererror") {
    throw("Invalid XML structure:\n" + xmlHttp.responseText);
  }
  
  xmlRoot = xmlResponse.documentElement;

  if (rootNodeName != "response" || !xmlRoot.firstChild) {
    throw("Empty XML response:\n" + xmlHttp.responseText);
  }

  errorElement = xmlRoot.getElementsByTagName('error').item(0);
  
  if (!errorElement) {
    throw("response/error element expected:\n" + xmlHttp.responseText);
  }
  
  errorCodeElement = errorElement.getElementsByTagName('code').item(0);

  if (!errorCodeElement) {
    throw("response/error/code element expected:\n" + xmlHttp.responseText);
  }
  
  errorCode = errorCodeElement.firstChild.data;
  
  if (errorCode != '0') {
    errorDescription = errorElement.getElementsByTagName('description').item(0).firstChild.data;
    throw("Error response:\n" + errorDescription);
  }
  
  requestElement = xmlRoot.getElementsByTagName('request').item(0);

  if (!requestElement) {
    throw("response/request element expected:\n" + xmlHttp.responseText);
  }
  
  requestCommandElement = requestElement.getElementsByTagName('command').item(0);

  if (!requestCommandElement) {
    throw("response/request/command element expected:\n" + xmlHttp.responseText);
  }

  command = requestCommandElement.firstChild.data;
  
  requestActionElement = requestElement.getElementsByTagName('action').item(0);

  if (!requestActionElement) {
    throw("response/request/action element expected:\n" + xmlHttp.responseText);
  }

  action = requestActionElement.firstChild.data;
  
  answerElement = xmlRoot.getElementsByTagName('answer').item(0);

  if (!answerElement) {
    throw("response/answer element expected:\n" + xmlHttp.responseText);
  }
  switch (command) {
    case 'reviews':
      switch (action) {
        case 'rotate':
          reviewsRotateResponse(answerElement);
          break;
      }
      break;
  }
}

function makeRequest(params, async)
{
  if (xmlHttp)
  {
    try
    {
      xmlHttp.open("POST", "request.php?" + params, async);

      if (async) {
        xmlHttp.onreadystatechange = handleRequestStateChange;
      }

      xmlHttp.send(null);
      
      if (!async) {
        handleServerResponse();
      }
    }
    catch (e)
    {
      alert ("Can't connect to server:\n" + e.toString());
    }
  }
}

function reviewsRotateResponse(answerElement)
{
  div = document.getElementById("review_box");
  if (div != null) {
    if (navigator.userAgent.indexOf('MSIE') > -1) {
      div.innerHTML = answerElement.childNodes[0].nodeValue;
    } else {
      div.innerHTML = answerElement.textContent;
    }
  }

}

function reviews_rotate()
{
  div = document.getElementById("review_box");
  if (div != null) {
    makeRequest("cmd_command=reviews&cmd_action=rotate", true);
  }
  setTimeout("reviews_rotate()", 10000);
}

//setTimeout("reviews_rotate()", 10000);