// Restituisce una istanza dell'oggetto XMLHttp (cross-browser)
function getXMLHttp()
{
  var xmlhttp=false;
  // Utilizziamo la compilazione condizionale di JScript
  // per far fronte alle vecchie versioni di
  // Internet Explorer che non supportano i blocchi try/catch.
  /*@cc_on @*/
  /*@if (@_jscript_version >= 5)
  // Prova la creazione dell'oggetto ActiveX XMLHTTP
  try
  {
    xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
  }
  catch (e)
  {
    try
    {
      xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
    }
    catch (E)
    {
      xmlhttp = false;
    }
  }
  @end @*/
  // Se il browser non è Internet Explorer viene creato
  // l'oggetto XMLHttpRequest (Netscape, Mozilla)
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    xmlhttp = new XMLHttpRequest();
  }
  return xmlhttp;
}

// Si connette all'URL specificato, ne preleva i contenuti
// e li visualizza come contenuto del tag avente l'ID specificato
function getURL(url, id)
{
  var xmlhttp = getXMLHttp();
  if (!xmlhttp)
  {
    alert('XMLHttp non supportato dal browser');
    return false;
  }

  showLoading(true);
  xmlhttp.open('GET', url, true);
  xmlhttp.onreadystatechange=function()
  {
    if (xmlhttp.readyState==4)
    {
      showLoading(false);
      if (xmlhttp.status == 200)
      {
        var elem = document.getElementById(id);
        if(elem == null)
          alert('Elemento inesistente: ' + id);
        else
          elem.innerHTML = xmlhttp.responseText;
      }
      else if (xmlhttp.status == 404)
        alert('URL inesistente: ' + url);
      else
        alert('Errore: ' + xmlhttp.status);
    }
  }
  xmlhttp.send(null)
}

// Visualizza o nasconde il DIV con la scritta LOADING
function showLoading(b)
{
  var elem = document.getElementById('loading')
  elem.style.left = document.body.scrollWidth / 2;
  if (b)
    elem.style.visibility = 'visible';
  else
    elem.style.visibility = 'hidden';
}

