
/**
 * Funzione che apre una xml httprequest e registra un hanlder
 * per refreshare i comboJoin con la funzione updateComboJoin
 * parametri:
 * sendUrl l'url a cui si manda la richiesta (stringa)
 * formNameUpdate il nome del form da refreshare(stringa) a seguito della chiamata della funzione updateComboJoin
 * restuisce true se esiste l'oggetto XMLHttpRequest
 */
function callAjax (sendUrl, formNameUpdate) {

  // Obtain an XMLHttpRequest instance
  var req = newXMLHttpRequest();

  if ( !req) {
    return false;//non esiste XMLHttpRequest
  }

  // Set the handler function to receive callback notifications
  // from the request object
  var handlerFunction = getReadyStateHandler(req, updateComboJoin, formNameUpdate);
  req.onreadystatechange = handlerFunction;

  // Open an HTTP POST connection to the shopping cart servlet.
  // Third parameter specifies request is asynchronous.
  req.open("POST", sendUrl, true);

  // Specify that the body of the request contains form data
  req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

  var URLto = buildPOST(formNameUpdate);

  req.send(URLto);

  return true;
}




/*
 * Returns a new XMLHttpRequest object, or false if this browser
 * doesn't support it
 */
function newXMLHttpRequest() {

  var xmlreq = false;

  if (window.XMLHttpRequest) {

    // Create XMLHttpRequest object in non-Microsoft browsers
    xmlreq = new XMLHttpRequest();

  } else if (window.ActiveXObject) {

    // Create XMLHttpRequest via MS ActiveX
    try {
      // Try to create XMLHttpRequest in later versions
      // of Internet Explorer

      xmlreq = new ActiveXObject("Msxml2.XMLHTTP");

    } catch (e1) {

      // Failed to create required ActiveXObject

      try {
        // Try version supported by older versions
        // of Internet Explorer

        xmlreq = new ActiveXObject("Microsoft.XMLHTTP");

      } catch (e2) {

        // Unable to create an XMLHttpRequest with ActiveX
      }
    }
  }

 return xmlreq;

}


 function buildPOST(theFormName) {

  	theForm = document.forms[theFormName];
	var qs = '';
	for (e=0;e<theForm.elements.length;e++) {

          if (theForm.elements[e].name!='') {
		var name = theForm.elements[e].name;
		qs+=(qs=='')?'':'&'
		qs+= name+'='+escape(theForm.elements[e].value);
		}
	}
	qs+="\n";
	return qs;
     }


/*
 * Returns a function that waits for the specified XMLHttpRequest
 * to complete, then passes its XML response
 * to the given handler function.
 * req - The XMLHttpRequest whose state is changing
 * responseXmlHandler - Function to pass the XML response to
 */
 function getReadyStateHandler(req, responseXmlHandler, form) {

  // Return an anonymous function that listens to the
  // XMLHttpRequest instance
  return function () {

    // If the request's status is "complete"
    if (req.readyState == 4) {

      // Check that a successful server response was received
      if (req.status == 200) {

        // Pass the XML payload of the response to the
        // handler function
        if ( responseXmlHandler ) {
           responseXmlHandler(req.responseXML, form);
        }

      } else {

        // An HTTP problem has occurred
        alert("HTTP error: "+req.status);
      }
    }
  }
}



  function updateComboJoin( responseXml, formName) {

	if ( !responseXml) return;

     	var root = responseXml.getElementsByTagName("combojoin")[0];

        if ( !root) return;

     	var items = root.getElementsByTagName("item");

     	if ( !items) return;

	var form = document.forms[formName];

     	for (var i = 0 ; i < items.length ; i++) {

            var item = items[i];

            var name = item.getAttribute("name");

            var options = item.getElementsByTagName("option");

            var comboJoin = form.elements[name];

            if ( comboJoin ) {
               comboJoin.options.length = 1;

               for( var j=0; j<options.length; j++) {

                  var value = options[j].getAttribute('value');
                  var label = options[j].getAttribute('label');
                  var sel = options[j].getAttribute('sel');

                  if ( comboJoin) {
                     comboJoin.options[j+1] = new Option( label, value, false, false);
                     if (sel) comboJoin.options[j+1].selected =true;
                  }
                }
            }
        }
    }

