var xmlHttp;    //用于保存XMLHttpRequest对象的全局变量

//用于创建XMLHttpRequest对象
function createXmlHttp() {
    //根据window.XMLHttpRequest对象是否存在使用不同的创建方式
    if (window.XMLHttpRequest) {
       xmlHttp = new XMLHttpRequest();                  //FireFox、Opera等浏览器支持的创建方式
    } else {
       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");//IE浏览器支持的创建方式
    }
}

//刷新购物车
function refreshCart() {
    sendRequest("");
}

//向购物车添加产品
function addProduct(pid) {
    sendRequest("?action=add&pid=" + pid);
}

//清空购物车
function emptyCart() {
    sendRequest("&action=empty");
}

//删除购物车内单件产品
function delProduct(pid) {
    sendRequest("&action=del&pid=" + pid);
}

//向服务器发送操作请求
function sendRequest(params) {
    createXmlHttp();                        //创建XmlHttpRequest对象
    xmlHttp.onreadystatechange = showCartInfo;
    xmlHttp.open("GET", "addToCart.asp"+params, true);
    xmlHttp.send(null);
  // 	xmlHttp.open("post", "addToCart.asp", true);
//	xmlHttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
//	xmlHttp.send("params="+escape(params));
}

//将服务器响应信息写入购物车div中
function showCartInfo() {
    if (xmlHttp.readyState == 4) {
       // document.getElementById("shoppingcart").innerHTML = xmlHttp.responseText;
	   alert(xmlHttp.responseText);
    }
}
