// stores the reference to the XMLHttpRequest object
var xmlHttp = createXmlHttpRequestObject(); 

// retrieves the XMLHttpRequest object
function createXmlHttpRequestObject() 
{	
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // if running Internet Explorer
  if(window.ActiveXObject)
  {
    try
    {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) 
    {
      xmlHttp = false;
    }
  }
  // if running Mozilla or other browsers
  else
  {
    try 
    {
      xmlHttp = new XMLHttpRequest();
    }
    catch (e) 
    {
      xmlHttp = false;
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
 
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

// make asynchronous HTTP request using the XMLHttpRequest object 
function process()
{
  // proceed only if the xmlHttp object isn't busy
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
    // execute the quickstart.php page from the server
    xmlHttp.open("GET", "_checknews.php", true);  
    // define the method to handle server responses
    xmlHttp.onreadystatechange = handleServerResponse;
    // make the server request
    xmlHttp.send(null);
  }
}

// executed automatically when a message is received from the server
function handleServerResponse() 
{
  // move forward only if the transaction has completed
  if (xmlHttp.readyState == 4) 
  {
    // status of 200 indicates the transaction completed successfully
    if (xmlHttp.status == 200) 
    {
      // extract the XML retrieved from the server
      xmlResponse = xmlHttp.responseXML;
      // obtain the document element (the root element) of the XML structure
     // xmlDocumentElement = xmlResponse.documentElement;
      // get the text message, which is in the first child of
      // the the document element
	  
	  if(!xmlResponse || !xmlResponse.documentElement){
	  	//alert("Неверная структура xml "+xmlHttp.responseText);
	  }
	
	  
	  xmlRoot = xmlResponse.documentElement;
	  var titleArray=xmlRoot.getElementsByTagName("title");
	  var linkArray=xmlRoot.getElementsByTagName("link");
	  var idsArray=xmlRoot.getElementsByTagName("ids");
	  var dateArray=xmlRoot.getElementsByTagName("date");
	  var timeArray=xmlRoot.getElementsByTagName("time");
	  var redArray=xmlRoot.getElementsByTagName("red");
	  var imgArray=xmlRoot.getElementsByTagName("img");
	  var switchArray=xmlRoot.getElementsByTagName("switch");
	  var clArray=xmlRoot.getElementsByTagName("cl");
	  var alt, d, t, red;
		d="<span id=\"bkv\">"+dateArray.item(0).firstChild.data;
		t=" <span id=\"t1\"><sup>["+timeArray.item(0).firstChild.data+"]</sup></span></span> ";
	  var Html="<table cellpadding=0 cellspacing=0 border=0>";
	  for (var i=0; i<titleArray.length; i++){
	  
	  		d="<span id=\"bkv\">"+dateArray.item(i).firstChild.data;
			t=" <span id=\"t1\"><sup>["+timeArray.item(i).firstChild.data+"]</sup></span></span> ";
	  
			red="";
			if(redArray.item(i).firstChild.data==1){
				red="<font color=#CC3300>";
			}
			
	  		if(switchArray.item(i).firstChild.data==1){Html+="<tr><td colspan=3><p class=maintext><br><font size=1>Просмотренные новости:</font></p></td></tr>";}
	  		Html+="<tr><td valign=top><a href='/news/"+linkArray.item(i).firstChild.data+"/' id="+idsArray.item(i).firstChild.data+">"+d+t+red;
	  		Html+=titleArray.item(i).firstChild.data+"</font></a>";
	  		Html+="</td>";
	  		Html+="<td>&nbsp;</td>";
			if(imgArray.item(i).firstChild.data=="images/read.gif"){
				alt="Эту новость Вы уже читали";
			}
			else{alt="Эту новость Вы еще не читали";}
	  		Html+="<td valign=top class="+clArray.item(i).firstChild.data+"><div class=n_read><a href='/news/"+linkArray.item(i).firstChild.data+"/' id="+idsArray.item(i).firstChild.data+"><img src="+imgArray.item(i).firstChild.data+" alt='"+alt+"' border=0 align=absmiddle width=7 height=9></a></div></td></tr>";
	  }
      // update the client display using the data received from the server
      document.getElementById("mainnews").innerHTML =Html;
	  //alert(retData);
      // restart sequence
    } 
    // a HTTP status different than 200 signals an error
    else 
    {
      alert("There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
}
