﻿
Ajax = function()
{
	var xmlhttp;           //xmlhttp 对象用于取xml文档
	var failed;            //xmlhttp 获取失败标致
	var elementObj;        //DOM对象名
	var method;	           //传递方法　GET　POST
	var responseStatus;    //xmlhttp　状态
	var url;               //请求服务地址
	var domObj;
	var display;

	this.xmlhttp = null;
	this.failed = true;
	this.element = 'hiddenDIV';
	this.method = 'GET';
	this.createAJAX();
}
Ajax.prototype.onLoading = function(){
	//document.getElementById('showchkname').innerHTML= 'Loading,please waited ...';
};
Ajax.prototype.onLoaded = function(){
	//document.getElementById('showchkname').innerHTML= 'loaded';
};
Ajax.prototype.onFail = function(){
	//document.getElementById('showchkname').innerHTML= 'Loading,please wait ...';
};
Ajax.prototype.onInteractive = function(){
	//alert('Loading,please wait ...');
	//document.getElementById('hiddenDIV').innerHTML= 'Loading,please wait a...';
};
Ajax.prototype.createAJAX = function() {
	try {
		this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e1) {
		try {
			this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e2) {
			this.xmlhttp = null;
		}
	}

	if (!this.xmlhttp)
	{
		if (typeof XMLHttpRequest != "undefined") {
			this.xmlhttp = new XMLHttpRequest();
		} else {
			this.failed = true;
		}
	}
};
//执行Ajax的请求
Ajax.prototype.runAJAX = function(urlstring, querystring, method, display)
{
	var self = this;
	this.url = urlstring;
	this.display = display;
	this.xmlhttp.onreadystatechange = function(){self.process.call(self)};
	
	if(method.toLowerCase() == 'get')
	{
		this.xmlhttp.open("GET", this.url, true);
		this.xmlhttp.send(null);
	}
	else if(method.toLowerCase() == 'post')
	{
		this.xmlhttp.open("POST",querystring,true);
		this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
		this.xmlhttp.send(querystring);
	}
};
Ajax.prototype.process = function()
{
	switch (this.xmlhttp.readyState)
	{
		case 1:
			this.onLoading();
			break;
		case 2:
			this.onLoaded();
			break;
		case 3:
			this.onInteractive();
			break;
		case 4:
			this.response = this.xmlhttp.responseText;
			this.responseXML = this.xmlhttp.responseXML;
			this.responseStatus = this.xmlhttp.status;
   //         document.getElementById("TopTD").innerHTML = this.response;

			if(this.responseStatus == 200)
			{

				//document.getElementById('hiddenDIV').innerHTML
				xmlDoc = this.xmlhttp.responseXML;

				if(this.display == 1)	
					this.CheckUserName(this.response);
				if(this.display == 2)
				    this.CheckCardNo(this.response);
				if(this.display == 3)
				    this.AddCart(this.response); 
				if(this.display == 4)
				    this.DeleteCart(this.response); 
				if(this.display == 5)
				    this.UpdateCart(this.response); 
				if(this.display == 6)
				    this.ClearCart(this.response); 
				if(this.display == 7)
				    this.AddSaleProduct(this.response); 
				if(this.display == 8)
				    this.AddFav(this.response); 
				if(this.display == 9)
				    this.GetCartCount(this.response); 
				if(this.display == 10)
				    this.GetCart(this.response); 
				if(this.display == 11)
				    this.GetTopSortString(this.response); 
				if(this.display == 12)
				    this.GetHeadUserInfo(this.response); 
				if(this.display == 13)
				    this.GetHeadSearchList(this.response); 
				if(this.display == 14)
				    this.GetSendAmount(this.response); 
				if(this.display == 15)
				    this.GetProductMessage(this.response);
				if(this.display == 16)
				    this.Login(this.response); 
				if(this.display == 17)
				    this.AddMessage(this.response);  
				if(this.display == 18)
				    this.GetKeFuList(this.response);  
				if(this.display == 19)
				    this.GetBottomList(this.response);  
				if(this.display == 20)
				    this.GetSortNameList(this.response); 
				if(this.display == 21)
				    this.GetProductPrice(this.response); 
				if(this.display == 22)
				    this.GetStoreCount(this.response); 
				if(this.display == 23)
				    this.GetLeftSortList(this.response); 
				if(this.display == 24)
				    this.GetLoginInfo(this.response); 
				if(this.display == 25)
				    this.AddCartSimple(this.response); 
				if(this.display == 27)	
					this.CheckPhone(this.response);
				    
}
			else
			{
				alert("There is a problem!" + this.responseStatus);
			}
			break;
	}
};
Ajax.prototype.CheckUserName = function(obj)
{
  if(obj.split('|')[0]=="1")
  {
    document.getElementById("UserSpan2").innerHTML="";
  }
  else
  {
    document.getElementById("UserSpan2").innerHTML=obj.split('|')[1];
  }
};
Ajax.prototype.CheckPhone= function(obj)
{
  if(obj.split('|')[0]=="1")
  {
    document.getElementById("PhoneSpan").innerHTML="";
  }
  else
  {
    document.getElementById("PhoneSpan").innerHTML=obj.split('|')[1];
  }
};
Ajax.prototype.CheckCardNo = function(obj)
{	
  if(obj.split('|')[0]=="1")
  {
    document.getElementById("CardSpan").innerHTML="";
  }
  else
  {
    document.getElementById("CardSpan").innerHTML=obj.split('|')[1];
  }  
};
Ajax.prototype.AddCart = function(obj)
{	
  if(obj.split('|')[0]=="1")
  {
   location.href="/MyCart.aspx"    
  }
  else
  {
    alert(obj.split('|')[1]);
  }
};
Ajax.prototype.AddCartSimple = function(obj)
{	
  if(obj.split('|')[0]=="1")
  {
   CartCount();
   document.getElementById("win").src="/style/style11/cartlist.aspx?rnd="+Math.random();
   document.getElementById("lovexin14").style.display = "";
  }
  else
  {
    alert(obj.split('|')[1]);
  }
};
Ajax.prototype.DeleteCart = function(obj)
{	
  if(obj.split('|')[0]=="1")
  {
    GetCart();
    CartCount();
    document.getElementById("win").src="/style/style11/cartlist.aspx?rnd="+Math.random();//location.href="/MyCart.aspx"
  }
  else
  {
    alert(obj.split('|')[1]);
  }   
};
Ajax.prototype.UpdateCart = function(obj)
{	
    if(obj.split('|')[0]=="1")
  {
     GetCart();
     CartCount();
     document.getElementById("win").src="/style/style11/cartlist.aspx?rnd="+Math.random();
  }
  else
  {
    alert(obj.split('|')[1]);
  }      
};
Ajax.prototype.ClearCart = function(obj)
{	
    if(obj.split('|')[0]=="1")
  {
     GetCart();
     CartCount();
     document.getElementById("win").src="/style/style11/cartlist.aspx?rnd="+Math.random();
  }
  else
  {
    alert(obj.split('|')[1]);
  }      
};
Ajax.prototype.AddSaleProduct = function(obj)
{	
  if(obj.split('|')[0]=="1")
  {
    alert('代销商品添加成功');
  }
  else
  {
    alert(obj.split('|')[1]);
  }      
};
Ajax.prototype.AddFav = function(obj)
{	
  if(obj.split('|')[0]=="1")
  {
    alert('成功添加商品到收藏夹');
  }
  else
  {
    alert(obj.split('|')[1]);
  }      
};
Ajax.prototype.GetCartCount = function(obj)
{	
  document.getElementById("CartCount").innerHTML=obj;   
};
Ajax.prototype.GetCart = function(obj)
{	
  document.getElementById("CartTD").innerHTML=obj;   
};
Ajax.prototype.GetTopSortString = function(obj)
{	
  document.getElementById("TopSortTD").innerHTML=obj;   
};
Ajax.prototype.GetHeadUserInfo = function(obj)
{	
  document.getElementById("UserSpan").innerHTML=obj;  
};
Ajax.prototype.GetHeadSearchList = function(obj)
{	
  document.getElementById("SearchTD").innerHTML=obj;  
};
Ajax.prototype.GetSendAmount = function(obj)
{
  var samount = obj;
  document.getElementById('EmsPrice').value=parseFloat(samount).toFixed(2);
  document.getElementById('EmsSpan').innerHTML=parseFloat(samount).toFixed(2);
  document.getElementById('AllSpan').innerHTML=(parseFloat(document.getElementById('ProductPrice').value)+parseFloat(samount)).toFixed(2);
};
Ajax.prototype.GetProductMessage = function(obj)
{	
  document.getElementById("ProductMessageTD").innerHTML=obj;  
};
Ajax.prototype.Login = function(obj)
{	
  if(obj.split('|')[0]=="1")
  {
    GetProductMessage(obj.split('|')[1],1);
  }
  else if(obj.split('|')[0]=="-3")
  {
       alert(obj.split('|')[1]); 
       document.getElementById("Vimg").src="/ValidateCode.aspx?"+Math.random();
  }
  else
  {
    alert(obj.split('|')[1]);
  }
};
Ajax.prototype.AddMessage = function(obj)
{	
  if(obj.split('|')[0]=="1")
  {
    GetProductMessage(obj.split('|')[1],1);
  }
  else if(obj.split('|')[0]=="-3")
  {
       alert(obj.split('|')[1]); 
       document.getElementById("Vimg").src="/ValidateCode.aspx?"+Math.random();
  }
  else
  {
    alert(obj.split('|')[1]);
  }
};
Ajax.prototype.GetKeFuList = function(obj)
{	
  document.getElementById("KeFuTD").innerHTML=obj;  
};
Ajax.prototype.GetBottomList = function(obj)
{	
  if(document.getElementById("BottomTD"))
        document.getElementById("BottomTD").innerHTML=obj;  
};
Ajax.prototype.GetSortNameList = function(obj)
{	
  if(document.getElementById("SortNameTD"))
        document.getElementById("SortNameTD").innerHTML=obj;  
};
Ajax.prototype.GetProductPrice = function(obj)
{	
  if(document.getElementById("PriceSpan"))
        document.getElementById("PriceSpan").innerHTML=obj.split('|')[0]+obj.split('|')[1];  
   if(document.getElementById("Price"))
        document.getElementById("Price").value=obj.split('|')[0];  
};
Ajax.prototype.GetStoreCount = function(obj)
{	
  if(document.getElementById("StoreSpan"))
        document.getElementById("StoreSpan").innerHTML=obj;  
};
Ajax.prototype.GetLeftSortList = function(obj)
{	
  if(document.getElementById("LeftSortTD"))
        document.getElementById("LeftSortTD").innerHTML=obj;  
};
Ajax.prototype.GetLoginInfo = function(obj)
{	
  if(document.getElementById("LoginSpan"))
        document.getElementById("LoginSpan").innerHTML=obj;  
};



//商品详细页面添加入购物车
function AddCart(ProductId)
{
    var Count = document.getElementById("BuyCount").value;
    var Price = document.getElementById("Price").value;
    if(Count=="")
    {
        alert('请输入购买数量');
        document.getElementById("BuyCount").focus();
        return false;
    }
    if(parseInt(Count)!=Count)
    {
        alert('请输入正确的购买数量');
        document.getElementById("BuyCount").focus();
        return false;
    }
    var myremark ="";
    var times="0";
    try{
    times=document.getElementById("Hidden2").value;  
    }
    catch(e)
    {
    times="err"
    } 
    if( times!="err")
    {
    for(var i=0;i<times;i++)
    {
        if(document.getElementById("mypr"+i).value=="请选择")
        {
            alert('请输入购买的尺寸/颜色');
             return false;
        }
        if(myremark=="")
        {
            myremark=document.getElementById("mypr"+i).value;
        }
        else
        {
            myremark=myremark+","+document.getElementById("mypr"+i).value;
        }
    }
    }
    if(document.getElementById("spid").value=="1")
    {
	    if(document.getElementById("srid").value==""||document.getElementById("srid").value=="例如：500g 半斤 2个 1棵等")
    {
        alert('请输入购买量');
        document.getElementById("srid").focus();
        return false;
    }
	Count="1";
	myremark="购买量："+document.getElementById("srid").value
    }
    var obj = new Ajax();   
    var url = "/Page/AddCart.aspx?ProductId="+ProductId+"&Num="+Count+"&Price="+Price+"&Remark="+myremark+"&rnd="+Math.random();
    obj.runAJAX(url,url,'get',3); 
}
//商品列表页面添加入购物车
function AddCartSimple(Price,ProductId,tp)
{
    if(tp=="1")
    {
        window.open("/detail-"+ProductId+".ga","","");
    }
    else
    {
    var obj = new Ajax();   
    var url = "/Page/AddCart.aspx?ProductId="+ProductId+"&Num=1&Price="+Price+"&Remark="+"&rnd="+Math.random();
    obj.runAJAX(url,url,'get',25); 
    }
}
function AddCartSimple2(Price,ProductId,tp)
{
    var Count = document.getElementById("BuyCount").value;
    if(Count=="")
    {
        alert('请输入购买数量');
        document.getElementById("BuyCount").focus();
        return false;
    }
    if(parseInt(Count)!=Count)
    {
        alert('请输入正确的购买数量');
        document.getElementById("BuyCount").focus();
        return false;
    }
    if(tp=="1")
    {
        location.href="/detail-"+ProductId+".ga";
    }
    else
    {
    var myremark ="";
    var times="0";
    try{
    times=document.getElementById("Hidden2").value;  
    }
    catch(e)
    {
    times="err"
    } 
    if( times!="err")
    {
    for(var i=0;i<times;i++)
    {
        if(document.getElementById("mypr"+i).value=="请选择")
        {
            alert('请输入购买的尺寸/颜色');
             return false;
        }
        if(myremark=="")
        {
            myremark=document.getElementById("mypr"+i).value;
        }
        else
        {
            myremark=myremark+","+document.getElementById("mypr"+i).value;
        }
    }
    }
    if(document.getElementById("spid").value=="1")
    {
	    if(document.getElementById("srid").value==""||document.getElementById("srid").value=="例如：500g 半斤 2个 1棵等")
    {
        alert('请输入购买量');
        document.getElementById("srid").focus();
        return false;
    }
	Count="1";
	myremark="购买量："+document.getElementById("srid").value
    }
    var obj = new Ajax();   
    var url = "/Page/AddCart.aspx?ProductId="+ProductId+"&Num="+Count+"&Price="+Price+"&Remark="+myremark+"&rnd="+Math.random();
    obj.runAJAX(url,url,'get',25); 
    }
}
function AddFav(ProductId)
{
    var obj = new Ajax();   
    var url = "/Page/AddFav.aspx?ProductId="+ProductId+"&rnd="+Math.random();
    obj.runAJAX(url,url,'get',8); 
}
function AddSaleProduct(ProductId)
{
    var obj = new Ajax();   
    var url = "/Page/AddSaleProduct.aspx?ProductId="+ProductId+"&rnd="+Math.random();
    obj.runAJAX(url,url,'get',7); 
}
//获取商品库存
function GetStoreCount(p,c,m)
{
     var obj = new Ajax();   
    var url = "/Page/GetStoreCount.aspx?ProductId="+p+"&Color="+c+"&Model="+m+"&rnd="+Math.random();
//    alert(url);
    obj.runAJAX(url,url,'get',22); 
}




