function decoderFunc2(thisstring) {
	var returnstring = unescape(thisstring);
	var returnstring2 = returnstring.replace(/\+/g, " ");
	return returnstring2;
}

// holds an instance of XMLHttpRequest
var xmlHttpMail = createXmlHttpRequestObject();

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

// read a file from the server
function displayNews(showpage, numperpage) {
  // only continue if xmlHttp isn't void
  if (xmlHttpMail)
  {
    // try to connect to the server
    try
    {
	var theUrl = "getnews.php?showpage=" + showpage + "&numperpage=" + numperpage;
		//alert(theUrl);
    	// initiate reading a file from the server
    	xmlHttpMail.open("GET", theUrl, true);
    	xmlHttpMail.onreadystatechange = handleRequestStateChangeMail;
    	xmlHttpMail.send(null);
    }
    // display the error in case of failure
    catch (e)
    {
      //alert("Can't connect to server:\n" + e.toString());
    }
  }
}

// function called when the state of the HTTP request changes
function handleRequestStateChangeMail() 
{
  // when readyState is 4, we are ready to read the server response
  if (xmlHttpMail.readyState == 4) 
  {
    // continue only if HTTP status is "OK"
    if (xmlHttpMail.status == 200) 
    {
      try
      {
        // do something with the response from the server
        handleServerResponseMail();
      }
      catch(e)
      {
        // display error message
        alert("Error reading the response: " + e.toString());
      }
    } 
    else
    {
      // display status message
      alert("There was a problem retrieving the data:\n" + 
            xmlHttpMail.statusText);
    }
  }
}

 
// handles the response received from the server
function handleServerResponseMail()
{

  	// read the message from the server
  	var xmlResponseMail = xmlHttpMail.responseXML;
  	// obtain the XML's document element
 	 xmlRootMail = xmlResponseMail.documentElement;  
  	// obtain arrays with manufacturer names and ids 
  	newsTitlesArray = xmlRootMail.getElementsByTagName("title");
	newsDatesArray = xmlRootMail.getElementsByTagName("date");
	newsContentsArray = xmlRootMail.getElementsByTagName("content");
	newsPhotosArray = xmlRootMail.getElementsByTagName("photo");
	news2ndPhotosArray = xmlRootMail.getElementsByTagName("photo2");
	youTubesArray = xmlRootMail.getElementsByTagName("youtube");
  	// generate HTML output
	html = "";
  	// iterate through the arrays and create an HTML structure
	for (var i=0; i < newsTitlesArray.length; i++) {
		html += "<p class=\"date\">" + decoderFunc2(newsDatesArray.item(i).firstChild.data) + "</p>";
		html += "<h1 class=\"newstitle\">" + decoderFunc2(newsTitlesArray.item(i).firstChild.data) + "</h1>";
		var photo1 = newsPhotosArray.item(i).firstChild.data;
		var photo2 = news2ndPhotosArray.item(i).firstChild.data;
		if(photo1 != "placeholder.jpg") {
			html += "<img class=\"contentimage\" src=\"newsimages/" + photo1 + "\" width=\"230\" height=\"230\" alt=\"" + decoderFunc2(newsTitlesArray.item(i).firstChild.data) + "\" />";
		}
		if(photo2 != "placeholder.jpg") {
			html += "<img class=\"contentimage\" src=\"newsimages/" + photo2 + "\" width=\"230\" height=\"230\" alt=\"" + decoderFunc2(newsTitlesArray.item(i).firstChild.data) + "\" />";
		}
		var youtube = youTubesArray.item(i).firstChild.data;
		if(youtube != "noyoutube") {
			html += "<div style=\"float:left;margin-top:8px;\"><object width=\"230\" height=\"230\"><param name=\"movie\" value=\"http://www.youtube.com/v/" + youtube + "&amp;hl=en\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"http://www.youtube.com/v/" + youtube + "&amp;hl=en\" type=\"application/x-shockwave-flash\" wmode=\"opaque\" width=\"230\" height=\"230\"></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\" />";
		}
		html += newsContentsArray.item(i).firstChild.data;
		if(i != 4) {
			html += "<div class=\"horizrule\"><img class=\"horiz\" src=\"images/horiz_bird.gif\" width=\"32\" height=\"19\" alt=\"Bird\" /></div>";
		}
	}
	
	var myDiv = document.getElementById("newscontent");
	// display the HTML output
	myDiv.innerHTML = html;
	
}
