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

// holds an instance of xmlHttpFXTickerRequest
var xmlHttpFXTicker = createxmlHttpFXTickerRequestObject();
// creates an xmlHttpFXTickerRequest instance
function createxmlHttpFXTickerRequestObject()
{
  // will store the reference to the xmlHttpFXTickerRequest object
  var xmlHttpFXTicker;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create xmlHttpFXTickerRequest object
    xmlHttpFXTicker = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var xmlHttpFXTickerVersions = 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<xmlHttpFXTickerVersions.length && !xmlHttpFXTicker; i++)
    {
      try
      {
        // try to create xmlHttpFXTickerRequest object
        xmlHttpFXTicker = new ActiveXObject(xmlHttpFXTickerVersions[i]);
      }
      catch (e) 
      {
      }
    }
  }
  // return the created object or display an error message
  if (!xmlHttpFXTicker)
    alert("Error creating the xmlHttpFXTickerRequest object.");
  else
    return xmlHttpFXTicker;
}

// read a file from the server
function update_forex_ticker()
{
   // only continue if xmlHttpFXTicker isn't void
   if (xmlHttpFXTicker)
   {
     // try to connect to the server
     try
     {
       // initiate reading a file from the server
       xmlHttpFXTicker.open("GET", "quotes.php?s=EURUSD=X+USDJPY=X+GBPUSD=X+USDCHF=X+AUDUSD=X+USDIDR=X&f=sba", true);
       xmlHttpFXTicker.onreadystatechange = ticker_handleRequestStateChange;
       xmlHttpFXTicker.send(null);
     }
     catch (e)
     {
        //retry again setelah 30 detik
        setTimeout('update_forex_ticker()',dwTickerForexTimeOut);
     }
   }
}
// function called when the state of the HTTP request changes
function ticker_handleRequestStateChange()
{
   // when readyState is 4, we are ready to read the server response
   if (xmlHttpFXTicker.readyState == 4)
   {
     // continue only if HTTP status is "OK"
     if (xmlHttpFXTicker.status == 200)
     {
        try
        {
           // do something with the response from the server
           ticker_handleServerResponse();
        }
        catch(e)  
        {
           //retry again setelah 30 detik
           setTimeout('update_forex_ticker()',dwTickerForexTimeOut);
        }
     }
     else
     {
        //retry again setelah 30 detik
        setTimeout('update_forex_ticker()',dwTickerForexTimeOut);
     }
   }
}
// handles the response received from the server
function ticker_handleServerResponse()
{
   // read the message from the server
   var xmlResponse = xmlHttpFXTicker.responseXML;
   // catching potential errors with IE and Opera
   if (!xmlResponse || !xmlResponse.documentElement)
      throw("Invalid XML structure:\n" + xmlHttpFXTicker.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 = "<ul style=\"display:inline\">";   var txtcol="color:#ffffff";   // iterate through the arrays and create an HTML structure   for (var i=0; i<quoteArray.length; i++)   {      if (i%2==0)      {        txtcol="color:#ffffff";      } else      {        txtcol="color:#d0e0f0";      }       quote=quoteArray.item(i);      q_symbol=quote.getElementsByTagName("s");      q_bid=quote.getElementsByTagName("b");      q_ask=quote.getElementsByTagName("a");      asymbol=q_symbol.item(0).firstChild.data;      asymbol=asymbol.replace("=X","");      asymbol=asymbol.replace("USD","");
	  var n_bid=new Number(q_bid.item(0).firstChild.data);	  var n_ask=new Number(q_ask.item(0).firstChild.data);
	  switch (asymbol)
	  {
	    case 'EUR':;
		case 'GBP':;
		case 'CHF':;
		case 'AUD':
		    sbid=n_bid.toFixed(4);
			sask=n_ask.toFixed(4);
			break;
	    case 'JPY':;
		case 'IDR':;
		    sbid=n_bid.toFixed(2);
			sask=n_ask.toFixed(2);
			break;
		default:
		    sbid=n_bid.toFixed(2);
			sask=n_ask.toFixed(2);
			break;
			
	  }
	  
      html += "<li style=\"display:inline;padding-right:5px;"+txtcol+"\">"+asymbol+"="+              sbid+"/"+              sask+              "</li>";   }   html+="</ul>";
   // obtain a reference to the <div> element on the page   myDiv = document.getElementById("divForexTicker");   // display the HTML output
   if (myDiv!=null)
     myDiv.innerHTML = html;   
   setTimeout('update_forex_ticker()',dwTickerForexTimeOut);
}

window.addEvent('load',update_forex_ticker);