var isIe = false;

/*
 * 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
    try {
        xmlreq = new XMLHttpRequest();
    } catch (e0) {
        var imgObj = new Image();
        imgObj.src = "/xmlhttprequestnonmscreatefail";
    }
  } else if (window.ActiveXObject) {
	  isIe = true;
    try {
      xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e1) {
      try {
        xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e2) {
		  alert("Ϊ¸�¹ԃ±¾θվ£¬ȫʽ¼¶źµŤ¯@Ƿ");
		  var imgObj1 = new Image();
          imgObj1.src = "/xmlhttprequestmscreatefail";
      }
    }
  }

  return xmlreq;
}

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) {
      // Check that a successful server response was received
      if (req.status == 200) {
        // Pass the XML payload of the response to the 
        // handler function
        responseXmlHandler(req.responseXML);

      } else {
        // An HTTP problem has occurred
        alert("HTTP error: "+req.status);
		    var imgObj = new Image();
        imgObj.src = "/xmlhttpresponsefail"+req.status;        
      }
    }
  }
}


function loadHttpXml(url,param, afterProccess) {
  // Obtain an XMLHttpRequest instance
  var req = newXMLHttpRequest();

  // Set the handler function to receive callback notifications
  // from the request object
  var handlerFunction = getReadyStateHandler(req, afterProccess);
  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;charset=utf-8");
  // Send form encoded data stating that I want to add the 
  // specified item to the cart.
  if(param.trim() == ''){
      if(isIe){
	  req.send();
      }else{
	  req.send(null);
      }
  }else{
	  req.send(param);
  }
}

function getReadyStateHandlerWithParam(req, responseXmlHandler, callBackParam) {
	

  // 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
        responseXmlHandler(req.responseXML,callBackParam);
       
      } else {
        // An HTTP problem has occurred
        alert("HTTP error: "+req.status);
		   var imgObj = new Image();
        imgObj.src = "/xmlhttpresponsefail"+req.status;        
      }
    }
    
  }
}


function loadHttpXmlWithParam(url,param, afterProccess, callBackParam) {
  // Obtain an XMLHttpRequest instance
  var req = newXMLHttpRequest();
  // Set the handler function to receive callback notifications
  // from the request object
  var handlerFunction = getReadyStateHandlerWithParam(req, afterProccess, callBackParam);
  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;charset=utf-8");
  // Send form encoded data stating that I want to add the 
  // specified item to the cart.
  if(param.trim() == ''){
      if(isIe){
	  req.send();
      }
      else{
	  req.send(null);
      }
  }
  else{
	  req.send(param);
  }
}

String.prototype.trim=function(){
     var
         r=/^\s+|\s+$/,
         a=this.split(/\n/g),
         i=a.length;
     while(i-->0)
         a[i]=a[i].replace(r,'');
     return a.join('\n');
}

