function newXMLHttpRequest() {
  var xmlreq = null;
  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 addReferCnt(url, num, responseHandler, isXMLHandler) {
  // Obtain an XMLHttpRequest instance
  var req = newXMLHttpRequest();

  // Set the handler function to receive callback notifications from the request object
  var handlerFunction = getReadyStateHandler(req, responseHandler);
  req.onreadystatechange = handlerFunction;
  
  // Open an HTTP POST connection
  // 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
  req.send("num="+num);
}

function getReadyStateHandler(req, responseHandler, isXMLHandler) {
  // 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 Payload of the response to the handler function
      	if (isXMLHandler){
	        responseHandler(req.responseXML);
	    } else {
		    responseHandler(req.responseText);
	    }
      } else {
        // An HTTP problem has occurred
        alert("HTTP Ajax Post error: "+req.status);
      }
    }
  }
}
