var intervalID;

// Switch panel visibility.
function SwitchQuotesPanel(_panel_name)
{
      HideAllQuotesPanels();
      
      if (document.getElementById(_panel_name))
            document.getElementById(_panel_name).style.display = "block";
}

// Hide all quotes panels.
function HideAllQuotesPanels()
{
      if (document.getElementById("divQuotesCurrency"))
            document.getElementById("divQuotesCurrency").style.display = "none";
            
      if (document.getElementById("divQuotesIndexes"))
            document.getElementById("divQuotesIndexes").style.display = "none";
            
      if (document.getElementById("divQuotesCommodity"))
            document.getElementById("divQuotesCommodity").style.display = "none";
}

// Get XML HTTP object.
function getXmlHTTP() {
    var xmlHTTP = null;

    try { xmlHttp = new XMLHttpRequest(); }
    catch (e) {
        try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); }
        catch (e) {
            try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); }
            catch (e) { alert("Your browser does not support AJAX!"); return false; }
        }
    }

    return xmlHttp;
}

// Sets quotes refresh.
function SetQuotesRefresh(_type, _path)
{
      var _interval;
      
      clearTimeout(intervalID);
      
      _interval = document.getElementById("sel_intervals").value;
      
      if (parseInt(_interval) == 0)
      {
            document.getElementById("divQuotesRefreshState").innerHTML = document.getElementById("hdn_quotes_not_refresh_cap").value;
            document.getElementById("btn_refresh").disabled = "";
      }
      else
      {
            document.getElementById("divQuotesRefreshState").innerHTML = document.getElementById("hdn_quotes_refresh_cap").value.replace("{interval}",
                                                                         document.getElementById("sel_intervals").options[document.getElementById("sel_intervals").selectedIndex].text);
            document.getElementById("btn_refresh").disabled = "disabled";
            
            RefreshQuotes(_interval, _type, _path)
      }
}

// Refresh quotes.
function RefreshQuotes(_interval, _type, _path)
{
      if (parseInt(_interval) != 0)
            intervalID = setTimeout("GetQuotes('" + _interval + "', '" + _type + "', '" + _path + "');", _interval * 1000);
}

// Gets quotes.
function GetQuotes(_interval, _type, _path)
{
      var xmlHTTP = null;
      var xmlDoc = null;
      var _container = null;
      var _nodes = new Array();
      var _content;

      clearTimeout(intervalID);
    
      xmlHTTP = getXmlHTTP();

      xmlHTTP.onreadystatechange = function()
      {
            if (xmlHTTP.readyState == 4)
            {
                  if (xmlHTTP.responseText != "")
                        xmlDoc = xmlHTTP.responseXML;

                  // Sets currencies quotes.
                  _container = document.getElementById("divQuotesCurrency");

                  if (_container != null)
                  {
                        _content = "";

                        if (xmlDoc != null)
                        {
                              _nodes = xmlDoc.getElementsByTagName("currencies");
                              
                              if (_nodes != null && _nodes.length != 0)
                              {
                                    if (document.all)
                                          _content = _nodes[0].text;
                                    else
                                          _content = _nodes[0].textContent;
                              }
                        }

                        if (_content != "")
                              _container.innerHTML = _content;
                  }

                  // Set stock indexes quotes.
                  _container = document.getElementById("divQuotesIndexes");

                  if (_container != null)
                  {
                        _content = "";

                        if (xmlDoc != null)
                        {
                              _nodes = xmlDoc.getElementsByTagName("indexes");

                              if (_nodes != null && _nodes.length != 0)
                              {
                                    if (document.all)
                                          _content = _nodes[0].text;
                                    else
                                          _content = _nodes[0].textContent;
                              }
                        }

                        if (_content != "")
                              _container.innerHTML = _content;
                  }

                  // Set commodities market quotes.
                  _container = document.getElementById("divQuotesCommodity");

                  if (_container != null)
                  {
                        _content = "";

                        if (xmlDoc != null)
                        {
                              _nodes = xmlDoc.getElementsByTagName("commodities");

                              if (_nodes != null && _nodes.length != 0)
                              {
                                    if (document.all)
                                          _content = _nodes[0].text;
                                    else
                                          _content = _nodes[0].textContent;
                              }
                        }

                        if (_content != "")
                              _container.innerHTML = _content;
                  }
                  
                  // Set quotes date and time.
                  _container = document.getElementById("divQuotesDate");

                  if (_container != null)
                  {
                        _content = "";

                        if (xmlDoc != null)
                        {
                              _nodes = xmlDoc.getElementsByTagName("date");

                              if (_nodes != null && _nodes.length != 0)
                              {
                                    if (document.all)
                                          _content = _nodes[0].text;
                                    else
                                          _content = _nodes[0].textContent;
                              }
                        }

                        if (_content != "")
                              _container.innerHTML = _content;
                  }

                  RefreshQuotes(_interval, _type, _path);
            }
      }

      xmlHTTP.open("GET", _path + "?cm=" + _type + "&random=" + Math.random(), true);
      xmlHTTP.send(null);
}

// Seta chart refresh.
function SetChartRefresh(_path)
{
      var _interval;
      
      clearTimeout(intervalID);
      
      _interval = document.getElementById("sel_intervals").value;
      
      if (parseInt(_interval) == 0)
      {
            document.getElementById("divQuotesRefreshState").innerHTML = document.getElementById("hdn_quotes_not_refresh_cap").value;
            document.getElementById("btn_refresh").disabled = "";
      }
      else
      {
            document.getElementById("divQuotesRefreshState").innerHTML = document.getElementById("hdn_quotes_refresh_cap").value.replace("{interval}",
                                                                         document.getElementById("sel_intervals").options[document.getElementById("sel_intervals").selectedIndex].text);
            document.getElementById("btn_refresh").disabled = "disabled";
            
            RefreshQuotesChart(_interval, _path)
      }
}

// Refresh quotes chart.
function RefreshQuotesChart(_interval, _path)
{
      if (parseInt(_interval) != 0)
            intervalID = setTimeout("GetChart('" + _interval + "', '" + _path + "');", _interval * 1000);
}

// Gets chart.
function GetChart(_interval, _path)
{
      var _quote;
      
      clearTimeout(intervalID);
      
      _quote = document.getElementById("sel_quotes_list").value;
      
      document.getElementById("img_quote_chart").src = _path + "/" + _quote.toLowerCase() + "15_482x240x4.gif?random=" + Math.random();
      
      RefreshQuotesChart(_interval, _path);
}

// Chart's quote changed.
function ChartQuoteChanged(_path)
{
      var _interval;
      
      clearTimeout(intervalID);
      
      _interval = document.getElementById("sel_intervals").value;
      
      GetChart(_interval, _path);
}
