if ((typeof XMLHttpRequest == undefined) && (window.ActiveXObject))
{
  function my_XMLHttpRequest() 
  {
    var arrSig = ["MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
    for (var i=0; i<arrSig.length; i++)
    {
      try
      {
        var oRequest = new ActiveXObject(arrSig[i]);
        return oRequest;
      }
      catch (oError)
      {}
    }
    throw new Error("MSXML is not installed on your system.");
  }
}

function addURLParam(sURL, sParamName, sParamValue)
{
  sURL += (sURL.indexOf("?") == -1 ? "?" : "&");
  sURL += encodeURIComponent(sParamName) + "=" + encodeURIComponent(sParamValue);
  return sURL;
}

function addPostParam(sParams, sParamName, sParamValue)
{
  if (sParams.length > 0) { sParams += "&"; }
  return sParams + encodeURIComponent(sParamName) + "=" + encodeURIComponent(sParamValue);
}

var bXmlHttpSupport = (typeof XMLHttpRequest != "undefined" || window.ActiveXObject);
var Http = new Object;

Http.get = function (sURL, fnCallback)
{
  if (bXmlHttpSupport)
  {
    try { var oRequest = new XMLHttpRequest(); }
    catch (e) { var oRequest = new my_XMLHttpRequest(); }
    oRequest.open("get", sURL, true);
    oRequest.onreadystatechange = function() { if (oRequest.readyState == 4) { fnCallback(oRequest.responseText); }};
    oRequest.send(null);
  }
  else if (navigator.javaEnabled() && typeof java != "undefined" && typeof java.net != "undefined")
  {
    setTimeout(function () { fnCallback(httpGet(sURL)); }, 10);
  }
  else
  {
    alert("Your browser doesn't support HTTP requests.");
  }
}

Http.post = function (sURL, sParams, fnCallback)
{
  if (bXmlHttpSupport)
  {
    try { var oRequest = new XMLHttpRequest(); }
    catch (e) { var oRequest = new my_XMLHttpRequest(); }
    oRequest.open("post", sURL, true);
    oRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    oRequest.onreadystatechange = function() { if (oRequest.readyState == 4) { fnCallback(oRequest.responseText); }};
    oRequest.send(sParams);
  }
  else if (navigator.javaEnabled() && typeof java != "undefined" && typeof java.net != "undefined")
  {
    setTimeout(function () { fnCallback(httpPost(sURL, sParams)); }, 10);
  }
  else
  {
    alert("Your browser doesn't support HTTP requests.");
  }
}

// --------- funzioni specifiche -------------- //

var voce_corrente = "";

function goto_mese_handler(sData)
{
  if (voce_corrente == "estratti")
  {
    document.getElementById("cal-estratti").innerHTML = sData;
  }
  if (voce_corrente == "gioca")
  {
    document.getElementById("cal-gioca").innerHTML = sData;
  }
}

function goto_mese(_t, _d)
{
  var sURL = path_base + "cal_lotto.php";
  sURL = addURLParam(sURL, "d", _d.toString());
  sURL = addURLParam(sURL, "t", _t.toString());
  voce_corrente = _t;
  if (voce_corrente == "estratti")
  {
    document.getElementById("cal-estratti").innerHTML = "<div class=\"ajax_loader\">&nbsp;</div>";
  }
  if (voce_corrente == "gioca")
  {
    document.getElementById("cal-gioca").innerHTML = "<div class=\"ajax_loader\">&nbsp;</div>";
  }
  setTimeout(function() { Http.get(sURL, goto_mese_handler) }, 500);
}

function goto_giorno_handler(sData)
{
  document.getElementById("central-column").innerHTML = sData;
}

function goto_giorno(_t, _d)
{
  var sURL = path_base;
  if (_t == "estratti") { sURL = sURL + "get_estratti.php"; } 
  if (_t == "gioca") { sURL = sURL + "get_giocare.php"; } 
  sURL = addURLParam(sURL, "d", _d.toString());
  sURL = addURLParam(sURL, "t", _t.toString());
  voce_corrente = _t;
  document.getElementById("central-column").innerHTML = "<div class=\"ajax_loader\">&nbsp;</div>";
  setTimeout(function() { Http.get(sURL, goto_giorno_handler) }, 500);
}

function ordina_vincite_handler(sData)
{
  document.getElementById("central-column").innerHTML = sData;
}

function ordina_vincite(_t, _p)
{
  var sURL = path_base + "get_vincite.php"; 
  sURL = addURLParam(sURL, "w", _t.toString());
  sURL = addURLParam(sURL, "n", _p.toString());
  document.getElementById("central-column").innerHTML = "<div class=\"ajax_loader\">&nbsp;</div>";
  setTimeout(function() { Http.get(sURL, goto_giorno_handler) }, 500);
}

function popola_guestbook_handler(sData)
{
  var _n, _s;
  _n = sData.substring(0, 10);
  _s = sData.substring(10);
  if (_n != 0)
  {
    var oA = document.getElementById("ancora");
    oA.setAttribute("href", "javascript: popola_guestbook("+_n+");");
    oA.style.display = "block";
  }
  oEl = document.getElementById("thread-list");
  oEl.innerHTML = _s + oEl.innerHTML;
  document.getElementById("ancora_ajax_loader").style.display = "none";  
}

function popola_guestbook(_p)
{
  var sURL = path_base + "get_guestbook.php"; 
  sURL = addURLParam(sURL, "n", _p.toString());
  document.getElementById("ancora_ajax_loader").style.display = "block";
  document.getElementById("ancora").style.display = "none";
  setTimeout(function() { Http.get(sURL, popola_guestbook_handler) }, 500);
}

function nuovo_commento_handler(sData)
{
  var oSubmit = document.getElementById("invia");
  oSubmit.disabled = true;
  oSubmit.style.display = "inline";
  if (sData.indexOf("OK") < 0)
  {
    var oEl = document.getElementById("form-alert-box");
    oEl.innerHTML = sData;
    oEl.style.display = "block";
    oSubmit.disabled = false;
  }
  else
  {
    var oEl = document.getElementById("form-feedback-box");
    oEl.innerHTML = sData;
    oEl.style.display = "block";
  }
  document.getElementById("submit_ajax_loader").style.display = "none";  
}

function guestbook_validate()
{
  document.getElementById("form-alert-box").style.display = "none";  
  document.getElementById("form-feedback-box").style.display = "none";  
  oEl =  document.getElementById("form_commento");
  var sURL = path_base + "nuovo_commento.php"; 
  var sParams = "";
  sParams = addPostParam(sParams, "nick", oEl.elements["nick"].value);
  sParams = addPostParam(sParams, "email", oEl.elements["email"].value);
  sParams = addPostParam(sParams, "text", oEl.elements["commento"].value);
  document.getElementById("submit_ajax_loader").style.display = "inline";
  document.getElementById("invia").style.display = "none"; 
  setTimeout(function() { Http.post(sURL, sParams, nuovo_commento_handler) }, 500);
}

function mostra_tip(sMsg, sDst)
{ 
  oEl = document.getElementById("form-tip");
  oEl.innerHTML = "<p>" + sMsg + "</p><p class=\"chiusura\">chiudi</p>";
  oEl.style.display = "block";
  oEl.style.marginLeft = "15%";
}

function rimuovi_tip()
{
  oEl = document.getElementById("form-tip");
  oEl.innerHTML = "";
  oEl.style.display = "none";
}

function preProcessing()
{
  var _img = new Image(35,35); 
  _img.src = path_base + "images/ajax_loader.gif"; 
}