/*-------------------------------------
Implementasi AJAX untuk menampilkan
quotes dinamis
---------------------------------------
(c) 2007 PT Gatra Mega Berjangka 
   www.moneymallfutures.com
--------------------------------------
programmer: Zamrony P. Juhara
-------------------------------------*/
var dwQuoteTimeOut=120000;
var dwQuoteTimeOutError=300000;

// holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();
// creates an XMLHttpRequest instance
function createXmlHttpRequestObject()
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = 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<XmlHttpVersions.length && !xmlHttp; i++)
    {
      try
      {
        // try to create XMLHttpRequest object
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      }
      catch (e) 
      {
      }
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else
    return xmlHttp;
}

// read a file from the server
function update_index_quotes()
{
   // only continue if xmlHttp isn't void
   if (xmlHttp)
   {
     // try to connect to the server
     try
     {       
        // initiate reading a file from the server
       xmlHttp.open("GET", "/quotes.php?s=%5EDJI+%5EIXIC+%5EN225+%5EHSI+%5EKS200+%5EKS11+%5EJKSE+%5ESSEC+%5EGSPC+%5ESTI+%5ETWII&f=sl1c1p2", true);
       xmlHttp.onreadystatechange = handleRequestStateChange;
       xmlHttp.send(null);
     }
     catch (e)
     {
        //coba lagi setelah 30 detik        
        setTimeout('update_index_quotes()',dwQuoteTimeOutError);
     }   
   }
}

// function called when the state of the HTTP request changes
function handleRequestStateChange()
{
   // when readyState is 4, we are ready to read the server response
   if (xmlHttp.readyState == 4)
   {
     // continue only if HTTP status is "OK"
     if (xmlHttp.status == 200)
     {
        try
        {           
           // do something with the response from the server
           handleServerResponse();
        }
        catch(e)  
        {
           //coba lagi setelah 30 detik
           setTimeout('update_index_quotes()',dwQuoteTimeOutError);
        }
     }
     else
     {
        //coba lagi setelah 30 detik        
        setTimeout('update_index_quotes()',dwQuoteTimeOutError);
     }
   }
}

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

   // catching potential errors with IE and Opera
   if (!xmlResponse || !xmlResponse.documentElement)
      throw("Invalid XML structure:\n" + xmlHttp.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
   quoteArray = xmlRoot.getElementsByTagName("quotes");
   // generate HTML output
   var html = "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr bgcolor=\"#1D71AA\">"+
              "<th>&nbsp;</th>"+
              "<th style=\"color:#FFFFFF\">Last</th>"+
              "<th style=\"color:#FFFFFF\">Change</th>"+
              "<th style=\"color:#FFFFFF\">Pct</th>"+
              "<th>&nbsp</th>"+
              "</tr>";

   // iterate through the arrays and create an HTML structure
   for (var i=0; i<quoteArray.length; i++)
   {
      quote=quoteArray.item(i);
      q_symbol=quote.getElementsByTagName("s");
      q_last=quote.getElementsByTagName("l1");
      q_chg=quote.getElementsByTagName("c1");
      q_pct_chg=quote.getElementsByTagName("p2");

      bgcol= (i%2) ? "bgcolor=\"#c0d0e0\"" : "";

      adata=q_chg.item(0).firstChild.data;
      if (adata.charAt(0)=='+')
      {
        astyle="style=\"color:#20A020\"";
        imgtag="<img src=\"/images/arrow_up.gif\" />";
      } else
      {
        if (adata.charAt(0)=='-')
        { 
          astyle="style=\"color:#E01010\"";
          imgtag="<img src=\"/images/arrow_down.gif\" />";
        }
        else
        {
          astyle="";
          imgtag="";
        }
      }
      html += "<tr "+bgcol+">"+
              "<td>"+q_symbol.item(0).firstChild.data+"</td>"+
              "<td><div align=\"center\">"+q_last.item(0).firstChild.data+"</div></td>"+
              "<td "+astyle+"><div align=\"center\">"+q_chg.item(0).firstChild.data+"</div></td>"+
              "<td "+astyle+"><div align=\"center\">"+q_pct_chg.item(0).firstChild.data+"</div></td>"+
              "<td><div align=\"center\">"+imgtag+"</div></td>"+
              "</tr>";
   }
   
   tgl=new Date();
   html+="<tr><td align=\"center\" colspan=\"5\">"+tgl.getDate()+"/"+(tgl.getMonth()+1)+"/"+tgl.getFullYear()+"</td></tr>";
   html+="</table>";

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

window.addEvent('load',update_index_quotes);
