/*-------------------------------------
Implementasi AJAX untuk menampilkan
quotes dinamis
---------------------------------------
(c) 2007 PT Gatra Mega Berjangka 
   www.moneymallfutures.com
-------------------------------------*/
var dwLatestNewsTimeOut=300000;
var dwLatestNewsTimeOutError=600000;

// holds an instance of xmlHttpNewsRequest
var xmlHttpNews = createxmlHttpNewsRequestObject();
// creates an xmlHttpNewsRequest instance
function createxmlHttpNewsRequestObject()
{
  // will store the reference to the xmlHttpNewsRequest object
  var xmlHttpNews;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create xmlHttpNewsRequest object
    xmlHttpNews = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var xmlHttpNewsVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    // try every prog id until one works
    for (var i=0; i<xmlHttpNewsVersions.length && !xmlHttpNews; i++)
    {
      try
      {
        // try to create xmlHttpNewsRequest object
        xmlHttpNews = new ActiveXObject(xmlHttpNewsVersions[i]);
      }
      catch (e) 
      {
      }
    }
  }

  // return the created object or display an error message
  if (!xmlHttpNews)
    alert("Error creating the xmlHttpNewsRequest object.");
  else
    return xmlHttpNews;
}

// read a file from the server
function process_News()
{
   // only continue if xmlHttpNews isn't void
   if (xmlHttpNews)
   {
     // try to connect to the server
     try     
     {
       // initiate reading a file from the server
       xmlHttpNews.open("GET", "/headlines.php", true);
       xmlHttpNews.onreadystatechange = News_handleRequestStateChange;
       xmlHttpNews.send(null);
     }
     catch (e)
     {
        //coba lagi setelah 5 detik
        setTimeout('process_News()',dwLatestNewsTimeOutError);
     }
   }
}

// function called when the state of the HTTP request changes
function News_handleRequestStateChange()
{
   // when readyState is 4, we are ready to read the server response
   if ((xmlHttpNews) && (xmlHttpNews.readyState == 4))
   {
     // continue only if HTTP status is "OK"
     if (xmlHttpNews.status == 200)
     {
        try
        {
           // do something with the response from the server
           News_handleServerResponse();
        }
        catch(e)  
        {
           // display error message
           //alert("Error reading the response: " + e.toString());
           //coba lagi setelah 5 detik
           setTimeout('process_News()',dwLatestNewsTimeOutError);
        }
     }
     else
     {
        //retry again
        setTimeout('process_News()',dwLatestNewsTimeOutError);
     }
   }
}

// handles the response received from the server
function News_handleServerResponse()
{
   // read the message from the server
   var xmlResponse = xmlHttpNews.responseXML;

   // catching potential errors with IE and Opera
   if (!xmlResponse || !xmlResponse.documentElement)
      throw("Invalid XML structure:\n" + xmlHttpNews.responseText);

   // catching potential errors with Firefox
   var rootNodeName = xmlResponse.documentElement.nodeName;
   if (rootNodeName == "parsererror") throw("Invalid XML structure");

   // obtain the XML's document element
   xmlRoot = xmlResponse.documentElement;

   // obtain arrays with quotes
   newsArray = xmlRoot.getElementsByTagName("news");

   // generate HTML output
   var html = "<dl>";

   // iterate through the arrays and create an HTML structure
   for (var i=0; i<newsArray.length; i++)
   {
      news=newsArray.item(i);
      n_id=news.getElementsByTagName("nid");
      n_ccslug=news.getElementsByTagName("nccslug");
      n_title=news.getElementsByTagName("ntitle");
      n_date=news.getElementsByTagName("ndate");
      n_url=news.getElementsByTagName("nurl");
      html += "<dt><a href=\""+n_url.item(0).firstChild.data+"\">"+n_title.item(0).firstChild.data+"</a></dt>"+
  		      "<dd>"+n_date.item(0).firstChild.data+"</dd>";
   }

   html+="</dl>";

   // obtain a reference to the <div> element on the page
   myDiv = document.getElementById("divNews");
   // display the HTML output
   if (myDiv!=null)
     myDiv.innerHTML = html;
   
   //setTimeout('process_News()',dwLatestNewsTimeOut);
}

window.addEvent('load',process_News);
