// stores the reference to the XMLHttpRequest object
var xmlHttp = createXmlHttpRequestObject(); 

// retrieves the XMLHttpRequest object
function createXmlHttpRequestObject() 
{	
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // if running Internet Explorer <= 6.0
  if(window.ActiveXObject) {
    try {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) {
      xmlHttp = false;
    }
  }
  // if running Mozilla or other browsers
  else {
    try {
      xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
      xmlHttp = false;
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

// make asynchronous HTTP request using the XMLHttpRequest object 
function changeLanguage(lang) {
  setStatusInfo('<img src=\'http://euro-fx.com/euro/ajaxload.gif\' width=\'16\' height=\'16\' alt=\'\' />');
  rates_date = document.getElementById('RatesDate').value;
  euro_default = document.getElementById('input_EUR').value;
  document.getElementById('Lang').value = lang;
  // proceed only if the xmlHttp object isn't busy
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
    // execute the ratesxml.php page from the server
    var UrlToRequest = "/ratesxml.php";
    UrlToRequest += "?lang=" + lang;
    UrlToRequest += "&rates_date=" + rates_date;
    UrlToRequest += "&EUR=" + euro_default;
    xmlHttp.open("GET", UrlToRequest, true);
    // define the method to handle server responses
    xmlHttp.onreadystatechange = handleServerResponse;
    // make the server request
    xmlHttp.send(null);
  }
  else
    // if the connection is busy, try again after one second  
    setTimeout('changeLanguage(lang)', 1000);
}

// make asynchronous HTTP request using the XMLHttpRequest object 
function changeDate(rates_date) {
  setStatusInfo('<img src=\'http://euro-fx.com/euro/ajaxload.gif\' width=\'16\' height=\'16\' alt=\'\' />');
  lang = document.getElementById('Lang').value;
  euro_default = document.getElementById('input_EUR').value;
  document.getElementById('RatesDate').value = rates_date;
  // proceed only if the xmlHttp object isn't busy
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
    // execute the ratesxml.php page from the server
    var UrlToRequest = "/ratesxml.php";
    UrlToRequest += "?lang=" + lang; 
    UrlToRequest += "&rates_date=" + rates_date; 
    UrlToRequest += "&EUR=" + euro_default; 
    UrlToRequest += "&rand=" + Math.random(); 
    xmlHttp.open("GET", UrlToRequest, true);
    // define the method to handle server responses
    xmlHttp.onreadystatechange = handleServerResponse;
    // make the server request
    xmlHttp.send(null);
  }
  else
    // if the connection is busy, try again after one second  
    setTimeout('changeDate(rates_date)', 1000);
}

// imposta messaggio di stato
function setStatusInfo(stringa) {
    document.getElementById('StatusZone').innerHTML = stringa;
}

// executed automatically when a message is received from the server
function handleServerResponse() 
{
  // move forward only if the transaction has completed
  if (xmlHttp.readyState == 4) 
  {
    // status of 200 indicates the transaction completed successfully
    if (xmlHttp.status == 200) 
    {
        // read the message from the server
        var xmlResponse = xmlHttp.responseXML;
        // obtain the XML's document element
        xmlRoot = xmlResponse.documentElement;  
        // obtain arrays with book titles and ISBNs 
        arrSigle = xmlRoot.getElementsByTagName("sigla");
        arrValute = xmlRoot.getElementsByTagName("valuta");
        arrTassi = xmlRoot.getElementsByTagName("tasso");
        arrValori = xmlRoot.getElementsByTagName("valore");
        var lang = xmlRoot.getElementsByTagName("lang").item(0).firstChild.nodeValue;
        var rates_date = xmlRoot.getElementsByTagName("rates_date").item(0).firstChild.nodeValue;
        var human_date = xmlRoot.getElementsByTagName("human_date").item(0).firstChild.nodeValue;
        // generate HTML output
        var html = "";
        var sigla = "";
        var valuta = "";
        var valore = "";
        var label_v = "";
        var input_v = "";
        // iterate through the arrays and create an HTML structure
        for (var i=0; i<arrSigle.length; i++) {
          sigla = arrSigle.item(i).firstChild.nodeValue;
          valuta = arrValute.item(i).firstChild.nodeValue;
          tasso = arrTassi.item(i).firstChild.nodeValue;
          valore = arrValori.item(i).firstChild.nodeValue;
          label_v = "label_" + sigla;
          $MyDiv = document.getElementById(label_v);
          if ($MyDiv) {
            $MyDiv.innerHTML = '&nbsp;' + valuta;
          }
          indiceTasso = trovaIndiceTasso(sigla);
          Valute[indiceTasso].tasso = tasso;
          input_v = "input_" + sigla;
          $MyInput = document.getElementById(input_v);
          if ($MyInput) {
            $MyInput.value = valore;
          }
        }
        document.getElementById('Lang').value = lang;
        document.getElementById('RatesDate').value = rates_date;
        changeLangDate();
        var start_day = xmlRoot.getElementsByTagName("start_day").item(0).firstChild.nodeValue;
        var len_month = xmlRoot.getElementsByTagName("len_month").item(0).firstChild.nodeValue;
        var week_string = xmlRoot.getElementsByTagName("week_string").item(0).firstChild.nodeValue;
        var month_name = xmlRoot.getElementsByTagName("month_name").item(0).firstChild.nodeValue;
        var rates_date_ym = xmlRoot.getElementsByTagName("rates_date_ym").item(0).firstChild.nodeValue;
        var rates_date_mm = xmlRoot.getElementsByTagName("rates_date_mm").item(0).firstChild.nodeValue;
        var rates_date_mp = xmlRoot.getElementsByTagName("rates_date_mp").item(0).firstChild.nodeValue;
        var rates_date_yp = xmlRoot.getElementsByTagName("rates_date_yp").item(0).firstChild.nodeValue;
        var month_name_mm = xmlRoot.getElementsByTagName("month_name_mm").item(0).firstChild.nodeValue;
        var month_name_mp = xmlRoot.getElementsByTagName("month_name_mp").item(0).firstChild.nodeValue;
        ScriviGiorni(week_string,month_name,rates_date,start_day,len_month,rates_date_ym,rates_date_mm,rates_date_mp,rates_date_yp,month_name_mm,month_name_mp);
        setStatusInfo(human_date);
    }
    // a HTTP status different than 200 signals an error
    else {
      alert("There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
}
