///////////////////////////////////////////////////////////////////////////////
//  news.js
//  Grecon UK website news downloader script
//    Author: Sean O'Kelly (seanok.125@gmail.com)
//    Date: 2011-09-10
///////////////////////////////////////////////////////////////////////////////


// get_news function: to get the "News and Articles" content
function get_news() {
  var url = "/cgi-bin/news?type=news";
  var container = "news_container";
  download(url, container);
}


// get_exhibitions function: to get the "Exhibitions" content
function get_exhibitions() {
  var url = "/cgi-bin/news?type=exhibitions";
  var container = "exhibitions_container";
  download(url, container);
}


// download_news function: to download the news and put it on the page
function download(url, container) {
  var request;
  // Code for normal browsers:
  try {
    request = new XMLHttpRequest();
  }
  // Redundancy for various versions of internet explorer:
  catch (e) {
    try {
      request = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
      try {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e) {
        return;
      }
    }
  }
  
  // function to be called when the http request's state changes:
  request.onreadystatechange = function() {
    if(request.readyState == 4) {
      var news_container = document.getElementById(container);
      if(request.status == 200) {
        // set the contents of the news div to be the response from the server
        // all html formatting is done server-side
        news_container.innerHTML = request.responseText;
      }
      else {
        var p = document.createElement("p");
        p.innerHTML = "There are no items to display.";
        news_container.appendChild(p);
      }
    }
  }
  
  // Send the get request
  request.open("GET", url, true);
  request.send(null);
}


///////////////////////////////////////////////////////////////////////////////
//  End of news.js.
///////////////////////////////////////////////////////////////////////////////

