
var xmlHttp = createXmlHttpRequestObject();

var updateInterval = 5; // how many seconds to wait to get new message

var errorRetryInterval = 30; // seconds to wait after server error

var debugMode = true;

var loader = "<div id='loader'><img src='images/loader.gif'></div>";

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 display($message)
{
  myDiv = document.getElementById("ajax-content-div");
  myDiv.innerHTML = $message;

}

function displayError($message)
{
  display("Error retrieving the news message! Will retry in " +  errorRetryInterval + " seconds." +  (debugMode ? "<br/>" + $message : ""));
}

function displayWithSectMenu($message)
{
  myDiv = document.getElementById("container");
  myDiv.innerHTML = $message;
}


function read(what, id)
{
  if (xmlHttp)
  {
    try
    {
      display(loader)
      xmlHttp.open("GET", "readajax.php?what=" + what + "&id=" + id, true);
      xmlHttp.onreadystatechange = handleGettingContents;
      xmlHttp.send(null);
    }
    catch(e)
    {
      displayError(e.toString());
    }
  }
}

function handleGettingContents() 
{
  if (xmlHttp.readyState == 4) 
  {
    if (xmlHttp.status == 200) 
    {
      try
      {
        getContents();
      }
      catch(e)
      {
        displayError(e.toString());
      }
    } 
    else
    {
      displayError(xmlHttp.statusText);   
    }
  }
}

function getContents()
{
  var response = xmlHttp.responseText;
  if (response.indexOf("ERRNO") >= 0 || response.indexOf("error") >= 0  || response.length == 0)
    throw(response.length == 0 ? "Server error." : response);
  display(response);
}


function readwsm(what, id, qrystr)
{
  if (xmlHttp)
  {
    try
    {
      displayWithSectMenu(loader);
      xmlHttp.open("GET", "readajax.php?what=" + what + "&id=" + id + qrystr, true);
      xmlHttp.onreadystatechange = handleWithSectMenuContents;
      xmlHttp.send(null);
    }
    catch(e)
    {
      displayWithSectMenu(e.toString());
    }
  }
}

function handleWithSectMenuContents() 
{
  if (xmlHttp.readyState == 4) 
  {
    if (xmlHttp.status == 200) 
    {
      try
      {
        getWithSectMenuContents();
      }
      catch(e)
      {
        displayWithSectMenu(e.toString());
      }
    } 
    else
    {
      displayWithSectMenu(xmlHttp.statusText);   
    }
  }
}

function getWithSectMenuContents()
{
  var response = xmlHttp.responseText;
  if (response.indexOf("ERRNO") >= 0 || response.indexOf("error") >= 0  || response.length == 0)
    throw(response.length == 0 ? "Server error." : response);
  displayWithSectMenu(response);

  window.fireEvent('domready');

}


window.addEvent('domready', function(){
		var links = $$(".leftMenuLink");
		links.each(function(link, i){
			link.addEvent('click', function(e){
				this.addClass('selectedCont');
				links.each(function(other, j){
					if (other != link){
						other.removeClass('selectedCont');
					}
				});
			});
		});

});

