﻿/*
调用:
1、var text1=new ajax().connect("test.html","get","id=1")
2、var text2=new ajax().connect("test.html","post","")
*/
function ajax(){
	var xmlhttp=null;
	var xmlvalue=null;
	
	if(window.ActiveXObject){
		var arr=["Msxml2.XMLHTTP.6.0","MSXML2.XMLHTTP.5.0","MSXML2.XMLHTTP.4.0","Msxml2.XMLHTTP.3.0", "Msxml2.XMLHTTP","Microsoft.XMLHTTP"];
		for(var i=0;i<arr.length;i++){
			try{
				xmlhttp=new ActiveXObject(arr[i]);
				break;
			}catch(e){
				xmlhttp=null;
			}
		}
	}else if(window.XMLHttpRequest){
		try{
			xmlhttp=new XMLHttpRequest();
		}catch(e){
			xmlhttp=null;
		}
	}else{
		xmlhttp=null;
	}

	if(xmlhttp==null) return null; //浏览器不支持xmlhttp对象
	
	this.connect=function(url,method,postContent){
		method=method.toUpperCase();
		try{
			if(method=="GET"){
				xmlhttp.open(method,url+"?"+postContent,false);
				xmlhttp.setRequestHeader("Content-Type","text/html;charset=GB2312");
			}else{
				xmlhttp.open(method,url,false);
				xmlhttp.setRequestHeader("Content-type","text/xml");
				xmlhttp.setRequestHeader("Method","POST "+url+" HTTP/1.1");
				//xmltttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			}
			xmlhttp.send(method=="POST"?postContent:null);
			xmlvalue=unescape(xmlhttp.responseText);			
			xmlhttp=null;
			return xmlvalue;
		}catch(e){
			return null;
		}
	}
}


//type=1商品；type=2兑换；type=3赠品
function addCart(productid,type,cnt)
{
    if(type==2)//积分兑换
    {
        var isLogin=new ajax().connect("/AjaxLogin.aspx","get","date="+new Date()+"&Action=IsLogin");
        if(isLogin=="NoLogin")
        {
            //alert("请登录以后再进行积分兑换！！");
            window.location.href="/login.shtml?url="+window.location.href;
            return;
        }
        else if(isLogin.length>100)
        {
            alert("对不起，您的登陆信息出现异常，请重新登录！！");
            //window.location.href="/login.shtml?url="+window.location.href;
            return;
        }
        
        
        var isAllow = new ajax().connect("/AjaxCart.aspx","get","date="+new Date()+"&Action=CheckScore&ProductId="+productid+"&Type="+type+"&Cnt="+cnt);
        
        if(isAllow!="allow")
        {
            alert("对不起，您的可用积分少于可以兑换该产品的积分！！");
            
            //window.location.href="/index.shtml?url="+window.location.href;
            return;          
        }
        
    }

    var rst=new ajax().connect("/AjaxCart.aspx","get","date="+new Date()+"&Action=AddCart&ProductId="+productid+"&Type="+type+"&Cnt="+cnt);
    if(rst=="ok")
    {
        window.open("/CartList.aspx?url="+window.location);  
    }
    else
    {
        alert("购物失败！！！"+rst);
    }
}