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

var dwFrontForexTimeOut=120000;
var dwFrontForexTimeOutError=300000;

// holds an instance of xmlHttpForexRequest
var xmlHttpForex = createxmlHttpForexRequestObject();

// creates an xmlHttpForexRequest instance
function createxmlHttpForexRequestObject()
{
  // will store the reference to the xmlHttpForexRequest object
  var xmlHttpForex;

  // this should work for all browsers except IE6 and older
  try
  {
    // try to create xmlHttpForexRequest object
    xmlHttpForex = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var xmlHttpForexVersions = 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<xmlHttpForexVersions.length && !xmlHttpForex; i++)
    {
      try
      {
        // try to create xmlHttpForexRequest object
        xmlHttpForex = new ActiveXObject(xmlHttpForexVersions[i]);
      }
      catch (e) 
      {
      }
    }
  }

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

// read a file from the server
function process_forex()
{
   // only continue if xmlHttpForex isn't void
   if (xmlHttpForex)
   {
     // try to connect to the server
     try
     {
       // initiate reading a file from the server
       xmlHttpForex.open("GET", "/quotes.php?s=EURUSD=X+USDJPY=X+GBPUSD=X+USDCHF=X+AUDUSD=X+USDIDR=X+USDKRW=X&f=sl1bad1t1", true);
       xmlHttpForex.onreadystatechange = forex_handleRequestStateChange;
       xmlHttpForex.send(null);
     }
     catch (e)
     {
        //retry again setelah 30 detik
        setTimeout('process_forex()',dwFrontForexTimeOutError);
     }
   }
}

// function called when the state of the HTTP request changes
function forex_handleRequestStateChange()
{
   // when readyState is 4, we are ready to read the server response
   if (xmlHttpForex.readyState == 4)
   {
     // continue only if HTTP status is "OK"
     if (xmlHttpForex.status == 200)
     {
        try
        {
           // do something with the response from the server
           forex_handleServerResponse();
        }
        catch(e)  
        {
           //retry again setelah 30 detik
           setTimeout('process_forex()',dwFrontForexTimeOutError);
        }
     }
     else
     {
        //retry again setelah 5 detik
        setTimeout('process_forex()',dwFrontForexTimeOutError);
     }
   }
}

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

   // catching potential errors with IE and Opera
   if (!xmlResponse || !xmlResponse.documentElement)
      throw("Invalid XML structure:\n" + xmlHttpForex.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\">Bid</th>"+
              "<th style=\"color:#FFFFFF\">Ask</th>"+
              "<th style=\"color:#FFFFFF\">Time</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_bid=quote.getElementsByTagName("b");
      q_ask=quote.getElementsByTagName("a");
      q_date=quote.getElementsByTagName("d1");
      q_time=quote.getElementsByTagName("t1");      
      if (i%2==0)
      {
        bgcol="";
        tdcol="bgcolor=\"#e0e0e0\"";
      } else
      {
        bgcol="bgcolor=\"#c0d0e0\"";
        tdcol="bgcolor=\"#a0b0c0\"";
      } 

      asymbol=q_symbol.item(0).firstChild.data;
      asymbol=asymbol.replace("=X","");
      asymbol=asymbol.replace("USD","");	  var n_last=new Number(q_last.item(0).firstChild.data);
	  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':
		    slast=n_last.toFixed(4);
		    sbid=n_bid.toFixed(4);
			sask=n_ask.toFixed(4);
			break;
	    case 'JPY':;
		case 'IDR':;
		    slast=n_last.toFixed(2);
		    sbid=n_bid.toFixed(2);
			sask=n_ask.toFixed(2);
			break;
		default:
		    slast=n_last.toFixed(2);
		    sbid=n_bid.toFixed(2);
			sask=n_ask.toFixed(2);
			break;
			
	  }

      html += "<tr "+bgcol+">"+
              "<td><div>"+asymbol+"</div></td>"+
              "<td><div align=\"center\">"+slast+"</div></td>"+
              "<td "+tdcol+"><div align=\"center\">"+sbid+"</div></td>"+
              "<td><div align=\"center\">"+sask+"</div></td>"+
              "<td "+tdcol+"><div align=\"right\">"+q_date.item(0).firstChild.data+" "+q_time.item(0).firstChild.data+"</div></td>"+
              "</tr>";
   }
   html+="</table>";

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

window.addEvent('load',process_forex);
