// 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 close_news()
{
	try {
		if(nn) clearTimeout(nn);
	} catch (e) {}
}
function news(ty)
{
	nn = setTimeout('run_news("'+ty+'")', 400);
}
function run_news(ty)
{
  // proceed only if the xmlHttp object isn't busy
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
	//alert(ty)
// execute the quickstart.php page from the server
	xmlHttp.open("GET", "/index/news/xml.php?type="+ty, true);
    // define the method to handle server responses
	xmlHttp.onreadystatechange = newsResponse;
    // make the server request
    xmlHttp.send(null);
  }
  else {
    // if the connection is busy, try again after one second  
	//alert('process('+func+','+type+')');
	try {
		if(nn) clearTimeout(nn);
	} catch (e) {}
	nn = setTimeout('run_news("'+ty+'")', 200);
  }
}

// executed automatically when a message is received from the server
function newsResponse() 
{
  // 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
      xmlRoot = xmlResponse.documentElement;
      // get the text message, which is in the first child of
      // the the document element
	  news_id = xmlRoot.getElementsByTagName("news_id");
	  news_date = xmlRoot.getElementsByTagName("news_date");
	  news_title = xmlRoot.getElementsByTagName("news_title");
	  ty = xmlRoot.getElementsByTagName("ty");
	  type = ty.item(0).firstChild.data;

	  var news_div = document.getElementById("news_content");
	  
	  var txt = "";
	  for (var i=0; i<news_id.length; i++) { 
	  
	  	txt += '<div class="item">';
		if (tmpDate != news_date.item(i).firstChild.data)
			if (news_id.item(i).firstChild.data=='no') {
				txt += news_title.item(i).firstChild.data;
				continue;
			}
		  	txt += '<span class="date">'+news_date.item(i).firstChild.data+'</span>';
		txt += '<span class="title"><a href="#" onClick=window.open("/news/news_detail.php?n_id='+news_id.item(i).firstChild.data+'","news","width=500,height=300")>'+news_title.item(i).firstChild.data+'</a></span>\
			</div>';
			
		var tmpDate = news_date.item(i).firstChild.data;
	  }
	  txt += '<div class="more"><a href="/news/news.php?type='+type+'">More..</a></div>'
	news_div.innerHTML = txt;
	
	document.getElementById("news").style.background='url(/images/index/tab.jpg)';
	document.getElementById("system").style.background='url(/images/index/tab.jpg)';
	document.getElementById("school").style.background='url(/images/index/tab.jpg)';
	document.getElementById(type).style.background='url(/images/index/tab_over.jpg)';  

	} 
    // a HTTP status different than 200 signals an error
    else 
    {
      alert("There was a problem accessing the server: " + xmlHttp.statusText);
	  //alert('_su');
    }
  }
}
