var xmlHttpConf = createXmlHttpRequestObject_conf();

/* creates an XMLHttpRequest instance */
function createXmlHttpRequestObject_conf() 
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttpConf;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttpConf = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = 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<XmlHttpVersions.length && !xmlHttpConf; i++) 
    {
      try 
      { 
        // try to create XMLHttpRequest object
        xmlHttpConf = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {}
    }
  }
  // return the created object or display an error Item
  if (!xmlHttpConf)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttpConf;
}


function ConfaddItem(idItem)
{
  // only continue if xmlHttpConf isn't void
  if (xmlHttpConf)
  {
    // try to connect to the server
    try
    {
	  if (idItem == 'clear'){
	    var params = "?conf=" + idItem;
	  }else{
        var idPcodeElement = 'conf_pcode_' + idItem;
        var pcode = document.getElementById(idPcodeElement).value;
        
        // create the params string
        var params = "?conf=" + pcode;
	  }
      // initiate the asynchronous HTTP request
      xmlHttpConf.open("GET", "srv_confronto.php" + params, true);
	  xmlHttpConf.onreadystatechange = handleRequestStateChange_conf;
      xmlHttpConf.send(null);
    }
    // display the error in case of failure
    catch (e)
    {
      //alert("Can't connect to server:\n" + e.toString());
    }
  }
}

function ConfrmItem(idItem)
{
  // only continue if xmlHttpConf isn't void
  if (xmlHttpConf)
  {
    // try to connect to the server
    try
    {
      var idPcodeElement = 'delprod_' + idItem;
      var pcode = document.getElementById(idPcodeElement).value;
      // create the params string
      var params = "?delprod=" + pcode;
	  //var params = "";
      // initiate the asynchronous HTTP request
      xmlHttpConf.open("GET", "srv_confronto.php" + params, true);
	  xmlHttpConf.onreadystatechange = handleRequestStateChange_conf;
      xmlHttpConf.send(null);
    }
    // display the error in case of failure
    catch (e)
    {
      alert("Can't connect to server:\n" + e.toString());
    }
  }
}

// function called when the state of the HTTP request changes
function handleRequestStateChange_conf() 
{
  if (xmlHttpConf.readyState == 1 || xmlHttpConf.readyState == 2 || xmlHttpConf.readyState == 3) 
  {
	document.body.style.cursor = 'wait';
  }
  // when readyState is 4, we are ready to read the server response  
  if (xmlHttpConf.readyState == 4) 
  { //alert(xmlHttp.status);
    // continue only if HTTP status is "OK"
    if (xmlHttpConf.status == 200) 
    {
      try
      {
        // do something with the response from the server
        handleServerResponse_conf();
      }
      catch(e)
      {
        // display error message
        alert("Error reading the response: " + e.toString());
      }
    } 
    else
    {
      // display status message
      alert("There was a problem retrieving the data:\n" + 
            xmlHttpConf.statusText);
    }
  }
}

// handles the response received from the server
function handleServerResponse_conf()
{
  // retrieve the server's response packaged as an Text object
  var Response = xmlHttpConf.responseText;
  // catching potential errors with IE and Opera
  if (Response.length == 0)
    throw("Invalid XML structure:\n" + xmlHttpConf.responseText);
  
  //var box_confronto = document.getElementById("confronto_box");
  var box_confronto = document.getElementById("box_confronta");

  if (Response == 'FULL'){
	messaggio();
  }else if(Response == 'EMPTY'){
	/*
	if (box_confronto.className == "box"){
	  box_confronto.className = "hidden";
    }
    */
    if (document.getElementById("box_confronta").style.display=="block" || document.getElementById("box_confronta").style.display=="inline") {
		$j("#box_confronta").slideUp();	
	}
  }else{
	
    //document.getElementById("cart").scrollIntoView(true);
    //fadeup("confronto",255,126,153);
    //fadeup("box_confronta",255,126,153);

	if (document.getElementById("box_confronta").style.display=="none") {
		$j("#box_confronta").slideDown();	
	}
	/* 
    if (box_confronto.className == "hidden"){
	  box_confronto.className = "box";
    }
    */
    
    // display
	myDiv = document.getElementById("confronto_content");
    myDiv.innerHTML = Response ;
  }
  document.body.style.cursor = 'auto';
}