

// holds an instance of XMLHttpRequest
var xmlHttpNews = createXmlHttpRequestObjectNews();

// creates an XMLHttpRequest instance
function createXmlHttpRequestObjectNews() 
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttpNews;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttpNews = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = new Array('MSXML2.xmlHttpNews.6.0',
                                    'MSXML2.xmlHttpNews.5.0',
                                    'MSXML2.xmlHttpNews.4.0',
                                    'MSXML2.xmlHttpNews.3.0',
                                    'MSXML2.XMLHTTP',
                                    'Microsoft.XMLHTTP');
    // try every prog id until one works
    for (var i=0; i<XmlHttpVersions.length && !xmlHttpNews; i++) 
    {
      try 
 
      { 
        // try to create XMLHttpRequest object
        xmlHttpNews = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {}
    }
  }
  // return the created object or display an error message
  if (!xmlHttpNews)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttpNews;
}

// read a file from the server
function displayNews(showpage, numperpage) {

	var contentScroller = document.getElementById('contentscroller');
	contentScroller.innerHTML = "<div style=\"width:100%;text-align:center\"><img src=\"images/loader.gif\" alt=\"WAIT! It's loading...\" /></div>";

  // only continue if xmlHttp isn't void
  if (xmlHttpNews)
  {
    // try to connect to the server
    try
    {
	var theUrl = "processing/getnews.php?showpage=" + showpage + "&numperpage=" + numperpage;
		//alert(theUrl);
    	// initiate reading a file from the server
    	xmlHttpNews.open("GET", theUrl, true);
    	xmlHttpNews.onreadystatechange = handleRequestStateChangeNews;
    	xmlHttpNews.send(null);
    }
    // display the error in case of failure
    catch (e)
    {
		contentScroller.innerHTML = "There was a problem loading the News. Please try again later.";
      //alert("Can't connect to server:\n" + e.toString());
    }
  }
}

// function called when the state of the HTTP request changes
function handleRequestStateChangeNews() 
{
	var contentScroller = document.getElementById('contentscroller');
  // when readyState is 4, we are ready to read the server response
  if (xmlHttpNews.readyState == 4) 
  {
    // continue only if HTTP status is "OK"
    if (xmlHttpNews.status == 200) 
    {
      try
      {
        // do something with the response from the server
        handleServerResponseNews();
      }
      catch(e)
      {
        // display error message
        //alert("Error reading the response: " + e.toString());
		contentScroller.innerHTML = "There was a problem loading the News. Please try again later.";
      }
    } 
    else
    {
      // display status message
      //alert("There was a problem retrieving the data:\n" +             xmlHttpNews.statusText);
	  contentScroller.innerHTML = "There was a problem loading the News. Please try again later.";
    }
  }
}

 
// handles the response received from the server
function handleServerResponseNews()
{
	pageTracker._trackPageview("/news" );
  	// read the message from the server
  	var xmlResponseNews = xmlHttpNews.responseXML;
	if (typeof document.all == "undefined") {
		xmlResponseNews.normalize();
	}
  	// obtain the XML's document element
 	 xmlRootNews = xmlResponseNews.documentElement;  
  	// obtain arrays with manufacturer names and ids 
  	newsHeadlinesArray = xmlRootNews.getElementsByTagName("headline");
	newsDatesArray = xmlRootNews.getElementsByTagName("date");
	newsContentsArray = xmlRootNews.getElementsByTagName("newscontent");
	newsPhotosArray = xmlRootNews.getElementsByTagName("photo1");
	news2ndPhotosArray = xmlRootNews.getElementsByTagName("photo2");
	youTubesArray = xmlRootNews.getElementsByTagName("youtube");
	youTubesLinksArray = xmlRootNews.getElementsByTagName("youtubelink");
  	// generate HTML output
	html = "";
  	// iterate through the arrays and create an HTML structure
	html = "<div style=\"width:95%\">"
	for (var i=0; i < newsHeadlinesArray.length; i++) {
		html += "<p class=\"date\">" + newsDatesArray.item(i).firstChild.data + " - <span class=\"newsheadline\">" + decoderFunc2(newsHeadlinesArray.item(i).firstChild.data) + "</span></p>";
		//html += "<h1 class=\"newsheadline\">" + decoderFunc2(newsHeadlinesArray.item(i).firstChild.data) + "</h1>";
		var photo1 = newsPhotosArray.item(i).firstChild.data;
		var photo2 = news2ndPhotosArray.item(i).firstChild.data;
		if(photo1 != "placeholder.jpg" && photo1 != "nophoto.jpg") {
			html += "<div class=\"contentimage\"><img src=\"photos/news/" + photo1 + "\" alt=\"" + decoderFunc2(newsHeadlinesArray.item(i).firstChild.data) + "\" /></div>";
		}
		if(photo2 != "placeholder.jpg" && photo2 != "nophoto.jpg") {
			html += "<div class=\"contentimage\"><img class=\"contentimage\" src=\"photos/news/" + photo2 + "\" alt=\"" + decoderFunc2(newsHeadlinesArray.item(i).firstChild.data) + "\" /></div>";
		}
		var youtube = youTubesArray.item(i).firstChild.data;
		var youtubelink = youTubesLinksArray .item(i).firstChild.data;
		if(youtube != "noyoutube") {
			html += "<div style=\"float:left;\"><object width=\"247\" height=\"207\"><param name=\"movie\" value=\"http://www.youtube.com/v/" + youtubelink + "&amp;hl=en\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"http://www.youtube.com/v/" + youtubelink + "&amp;hl=en\" type=\"application/x-shockwave-flash\" wmode=\"opaque\" width=\"247\" height=\"207\"></embed></object></div>";
		}
		if(photo1 == "placeholder.jpg" && photo2 == "placeholder.jpg" && youtube != "noyoutube") {
			html += "<br clear=\"all\" /><br />";
		} else if (photo1 != "placeholder.jpg" || photo2 != "placeholder.jpg" || youtube != "noyoutube") {
			html += "<br clear=\"all\" /><br />";
		}
		html += newsContentsArray.item(i).firstChild.data;
		
		html += "<br />";
		
		if(i != 4) {
			html += "<div class=\"horizrule\"></div>";
		}
	}
	
	html += "<p class=\"redline\">";
	
	//show back and next buttons depending on what page they are on
	if(window.showpage != 1) {
		html += "<a href=\"#\" onclick=\"showprevpage()\">Previous page</a>&nbsp;";
	}
	if(window.showpage != window.totalnumberofpages) {
		html += "<a href=\"#\" onclick=\"shownextpage()\">Next page</a>&nbsp;";
	}
	html += "</p>";
	html += "</div><br clear=\"all\" />";
	document.getElementById('contentscroller').innerHTML = html;
	
}
