
function createRequest(url, callback) {
  // Obtain an XMLHttpRequest instance
  var req;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    } else 
        req = new XMLHttpRequest();

  // Set the handler function to receive callback notifications
  // from the request object
  var handlerFunction = getReadyStateHandler(req, callback);
  req.onreadystatechange = handlerFunction;

  // Open an HTTP POST connection to the shopping cart servlet.
  // Third parameter specifies request is asynchronous.
  req.open("POST", url, true);

  // Specify that the body of the request contains form data
  req.setRequestHeader("Content-Type", 
                       "application/x-www-form-urlencoded");

  // Send form encoded data stating that I want to add the 
  // specified item to the cart.
    document.body.style.cursor = "wait";
    return req;
}

/*
 * 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) {

  // Return an anonymous function that listens to the 
  // XMLHttpRequest instance
  return function () {

    // If the request's status is "complete"
    if (req.readyState == 4) {
        document.body.style.cursor = "auto";
      
      // Check that a successful server response was received
      if (req.status == 200) {
        // Pass the XML payload of the response to the 
        // handler function access .responseXML or .responseText
        responseXmlHandler(req);

      } else {

        // An HTTP problem has occurred
        //  204 No content
        //  400 Bad Request
        alert("HTTP error: "+req.status+"\n"+req.responseText);
      }
    }
  }
}

function Pair(property,value) 
{ 
	this.property=property;
	this.value=value;
}

function QueryString()
{
	this.arr = Array();
	this.add=function add(p,v) {
                /* append new pair to the array */
		this.arr.splice(this.arr.length,0,new Pair(p,v));
//                alert("adding " + p + " = " + v);
	};
	this.toString = function() {
		var query = "";
		for(var i=0;i<this.arr.length;i++) {
//                    alert("appending " + this.arr[i].property + " = " + this.arr[i].value);
			if (query.length > 0)
                            query += "&";
			query += this.arr[i].property; 
			query += "=";
			query += escape(this.arr[i].value);	
		}
//                alert("Return " + query.substr(50));
		return query;
	};
}
function createFormQuery(form) {
    var query = new QueryString();
    for (i = 0; i < form.elements.length; i++) {
        var element = form.elements[i];
        if (element.name.length != 0 ) {
//          alert(element.type + " : " + element.name + " = " + element.value);
            if (element.type == "checkbox")
                query.add(element.name,(element.checked)?"true":"false");
            else
                query.add(element.name,element.value);
        }
    }
    return query;
}
