﻿// JScript 文件
function Cart(){
	this.Id = "";
	this.Cnt = 0;
	this.Type = 0;
}

function Carts(){
	this.ProductCount = 0;
	this.ProductList = new Array();
	
	if (typeof Carts._initialized == "undefined") {

		Carts.prototype.Indexof = function (ProductId) {
		  var idx = 0;
		  for (var i=1; i<=this.ProductCount; i++)	
	          {
			if (this.ProductList[i].Id == ProductId){
			 	
			  idx = i;	
			  break;
			}
                  }
		  return idx;
	        }

		Carts.prototype.Add = function (ProductId,ProductCnt,ProductType) {
		aCarts.LoadCookies();
			var idx = this.Indexof(ProductId);
			//alert("idx="+idx);
			if (idx == 0) {
				this.ProductCount++;
				var aCart = new Cart();
				aCart.Id = ProductId;
				aCart.Cnt = ProductCnt;
				if(ProductType==null||ProductType==""||ProductType<0)
				{
				    ProductType=0
				}
				aCart.Type= ProductType;
				this.ProductList[this.ProductCount] = aCart;
				
			}else{
			    var strCnt=this.ProductList[idx].Cnt.toString();
			    if(strCnt.indexOf("-")>0)
			    {
			         this.ProductList[idx].Cnt = Number(strCnt.split("-")[0]) + Number(ProductCnt);
			    }
			    else
			    {
				     this.ProductList[idx].Cnt = Number(this.ProductList[idx].Cnt) + Number(ProductCnt);
				}
			}
			//alert(aCart.Type);			
		}

		Carts.prototype.Modify = function (ProductId,ProductCnt) {
			aCarts.LoadCookies();
			var idx = this.Indexof(ProductId);
			
			if (idx > 0) {				
				this.ProductList[idx].Cnt = ProductCnt;				
				if (this.ProductList[idx].Cnt<0) {
					this.ProductList[idx].Cnt = 0;
				}
			}
		}

		Carts.prototype.Delete = function (ProductId) {
		  aCarts.LoadCookies();
			var idx = this.Indexof(ProductId);
			if (idx > 0) {
			  for (var i=idx+1; i<=this.ProductCount; i++)	
		          {
			    this.ProductList[i-1] = this.ProductList[i];
			  }
			  this.ProductList[this.ProductCount] = null;
			  this.ProductCount--;
			}
		}

		Carts.prototype.SaveCookies = function () {
		
			var s = ""; 
			//alert("pcount="+ this.ProductCount); 
			for (var i=1; i<=this.ProductCount; i++)	
		        {
			   s  = s + this.ProductList[i].Id.toString() + "-"+ this.ProductList[i].Cnt.toString() +"-"+this.ProductList[i].Type.toString()+"|";
			}
			
		    SetCookie("ProductCount",this.ProductCount);
			//alert(document.cookie);	
	  		SetCookie("ProductCart",s);
			//alert(document.cookie);
		}

		Carts.prototype.ClearCookies = function () {
			SetCookie("ProductCount","0");
			SetCookie("ProductCart"," ");			  
			
		}

		Carts.prototype.LoadCookies = function () {		
		 var ProductCart = GetCookie("ProductCart");// ShoppingUser.getProductCount().value;
			if (ProductCart ==  null) 
			{
				this.ProductCart = "";
				this.ProductCount = 0;
			}
			else
			{
				var idx = 0;  var pIdx = 0;
				var s = new String(ProductCart);
				var ts = ""; //alert(ProductCart);

				idx =  s.indexOf("|");	
				while (idx > 0) { 
					ts = s.substring(0,idx); 
					s = s.substring(idx+1); 
					idx =  s.indexOf("|");	

					idx2 = ts.indexOf("-");
					var aCart = new Cart();
					aCart.Id = ts.substring(0,idx2);
					aCart.Cnt = ts.substring(idx2+1);
					//alert("cartid = "+aCart.Id +" cnt="+aCart.Cnt);
					pIdx ++;	
					this.ProductList[pIdx] = aCart;
				}
			}

		    var pCount= GetCookie("ProductCount");// ShoppingUser.getProductCount().value;
			//alert("load cookes ,pcount =  "+ pCount);
			if (pCount ==  null) 
			{
				this.ProductCount = 0;
				//SetCookie("ProductCount","0");
			} else { this.ProductCount = pCount; }   

		}


	}
}

var aCarts = new Carts();

function TopLoad()
{
	aCarts = new Carts();
	aCarts.LoadCookies();
	 //$('#demo2').show();
     //    $("#proCountShow_Index").html(aCarts.ProductCount);
}

//productid产品主键
//cnt 数量
//ProductType 正常购买不传或者0 赠品1 兑换2 
function AddSubmitCnt(productid,cnt,ProductType)
{
   if(parseInt(ProductCompare(productid,100,cnt))!=100)
   {
    aCarts.Add(productid,cnt,ProductType);
	aCarts.SaveCookies();

   }else{ alert("此产品每次购买数量不大于100瓶！"); }; 
     window.open("/CartList.shtml");  
  	  //window.open("/CartList.shtml?list="+escape(location+Date())); 
 }
//最终页购买?list="+escape(location+Date())
function AddSubmitProduct(productid,cnt,ProductType)
{


//  if(parseInt(ProductCompare(productid,100,cnt))==100)
//   {
//       alert("此产品每次购买数量不大于100瓶！"); 
//       window.open("..//CartList.aspx");  
//   }
//  else if(parseInt(ProductCompare(productid,5,cnt))==5)
//  {
//       alert("限购产品每次购买限购5瓶！"); 
//       window.open("../CartList.aspx");  
//  }else
//  {
//     if(parseInt(UpdateQueryXGProd(productid))>0)//限购产品
//     {
//       if(parseInt(cnt)>5){alert("限购产品每次购买限购5瓶！");return false; }
//       else
//       {
//        aCarts.Add(productid,cnt,ProductType);
//        aCarts.SaveCookies();
//        window.open("../CartList.aspx");  
//        }
//     }else
//     { 
      if(parseInt(cnt)>100)
      {
        alert("此产品每次购买数量不大于100瓶！");
        return false;
      }
      else
      { 
         aCarts.Add(productid,cnt,ProductType);
         aCarts.SaveCookies();
         window.open("../CartList.shtml");  
      }
//      return true;
//     }
//  }
}

function UpdateQueryXGProd(productid)
{
   var result=new ajax().connect("/XGProudct.aspx","get","Product="+productid+"&Time="+Date());
  return result;   
}

function QueryXGProd(productid)
{
   var result=new ajax().connect("/XGProudct.aspx","get","Product="+productid+"&Time="+Date());
   if(parseInt(result)>0)//为限购产品
   {
     if(parseInt(ProductCompare(productid,5,1))==5)
    {
      alert("限购专区产品每次购买限购5瓶！");
      return false;
    }
     return true;
   }else//普通产品
   {
     if(parseInt(ProductCompare(productid,100,1))==100)
    {
      alert("此产品每次购买数量不大于100瓶！");
      return false;
    }
    return true;
   }
   
}

function ProductCompare(productid,type,cnt)
{
var numProduct=0;
 if(GetCookie("ProductCart")!=null){
 
   var Cartlists=GetCookie("ProductCart").split("|");
    for(var i=0;i<Cartlists.length;i++)
    {
     if(productid==Cartlists[i].split("-")[0])
     {
          if(type==5)
          { 
            if((parseInt(Cartlists[i].split("-")[1])+parseInt(cnt))>5)
            {
             //alert("此产品每次购买限购5瓶！");
            numProduct=5;
            }
          }else
          {
            if((parseInt(Cartlists[i].split("-")[1])+parseInt(cnt))>100)
            {
             //alert("此产品每次购买限购100瓶！");
            numProduct=100;
            }
          }
      }
    }

   }else {  numProduct=1;  }
   return numProduct;
}
//限购产品添加
function AddXGBuyProduct(productid,cnt,ProductType)
{
   if(parseInt(ProductCompare(productid,5,cnt))!=5)
   {
    aCarts.Add(productid,cnt,ProductType);
	aCarts.SaveCookies();
   }else{ alert("限购专区产品每次购买限购5瓶！"); };
	 window.open("/CartList.shtml?list="+escape(location+Date()));  
}

//产品组合：onclick="AddPackage2(0,'3869|3909|3977|3949')" 
function AddPackage2(ProductType,ProductIdList)
{
    var sum=0;
    var cnt=1;
	var arr=new Array();
	arr=ProductIdList.split('|')
	for(i=0;i<arr.length;i++)
	{
	if(parseInt(ProductCompare(arr[i],100,1))!=100)
     {
      aCarts.Add(arr[i],cnt,ProductType);
	  aCarts.SaveCookies();
     }else{ sum++;  };  
	}
	if(sum>0){ alert("该组合中每个产品购买数量不大于100瓶！");}
	 window.open("/CartList.shtml?list="+escape(location+Date()));  
}

//用户中心添加到购物(遍历复选框)1
function AddPackage(ProductType,checkboxName)
{   
    var sum=0;
	var cnt=1;
	var Product=document.getElementsByName(checkboxName);
	for(i=0;i<Product.length;i++)
	{
		if(Product[i].checked==true)
		{	sum++;
		if(parseInt(ProductCompare(Product[i].value,100,cnt))!=100)
		  {
			aCarts.Add(Product[i].value,cnt,ProductType);
			aCarts.SaveCookies();
		  }else{ alert("此产品每次购买数量不大于100瓶！");  }
		}
	}
	
  if(sum>0){  window.open("/CartList.shtml?list="+escape(location+Date())); }
}


//购物车页产品添加
function AddCartProduct(productid,cnt,ProductType)
{
 if(parseInt(ProductCompare(productid,100,cnt))!=100)
   {
    aCarts.Add(productid,cnt,ProductType);
	aCarts.SaveCookies();
   }else{ alert("此产品每次购买数量不大于100瓶！"); }; 
   window.location.href='/CartList.shtml?list='+escape(location+Date());  
}

//修改数量
function ModifySubmitCnt(productid,cnt)
{
    var reg=/^[0-9]+$/   //至少一个长度
    if(!reg.test(cnt))
	{
	   alert("只能输入数字");
	   return false;
	}
	if(parseFloat(cnt)<=0)
	{
	   alert("购买数量必须大于1");
	   return false;
	}
    aCarts.Modify(productid,cnt);    
	aCarts.SaveCookies();
}

//删除Cookie
function DeleteSubmitCnt(productid)
{
    aCarts.Delete(productid);
	aCarts.SaveCookies();
}

function InitCookies(cookieName)
{
    var date=new Date();
    date.setTime(date.getTime()-100000);
    document.cookie=cookieName+"=0;expires="+date.toGMTString();
}
//继续购物关闭当前窗口
function closeWindow()
{
	if(document.all)
	window.opener=null;
	else
	window.open('','_parent','');
	window.close();
}

$(document).ready
(

    function()
    {
	    TopLoad();
    }
)

