﻿var ShopCarCookie="";
        function ShowCar()
        {
            
            _Main.GetShopCar(ShowCar_Callback)
        }
        function ShowCar_Callback(res)
        {
            ShopCarCookie = res.value;
            if(document.getElementById('ProductPanel').innerHTML=="")
            {
                InitItem();
            }
            document.getElementById('Car').style.display="block";
        }
        function MouseOverShowCar()
        {
            if(document.getElementById('ProductPanel').innerHTML=="")
            {
                InitItem();
            }
            document.getElementById('Car').style.display="block";
        }
        function HiddenCar()
        {
            document.getElementById('Car').style.display='none';
        }
        function Add(pid,name,pic,price)
        {
            if(document.getElementById('ProductPanel')==null)
            {
                alert('请先登陆后再进行操作。');
                return false;
            }
            if(ShopCarCookie.match(/ShopCar=.*/)==null)
            {
                if(document.getElementById('ProductPanel').innerHTML=="")
                {
                    InitItem();
                }
                ShopCarCookie+="ShopCar="+pid+"|1|"+price;
                
                AddItem(name,pic,price);
            }
            else
            {
                var Val = ShopCarCookie;
                Val= Val.match(/ShopCar=.*/)[0];
                var c=ShopCarCookie;
                var cc = c.replace(Val,"");
                ShopCarCookie=cc;
                Val = Val.replace("ShopCar=","");
                var MatchCount=0;
                var valueArr = Val.split(",");
                ShopCarCookie+="ShopCar=";
                for(var i=0;i<valueArr.length;i++)
                {
                    var tempvalue = valueArr[i].split('|');
                    if(tempvalue[0]==pid)
                    {
                            MatchCount=MatchCount+1;
                            tempvalue[1] = parseInt(tempvalue[1])+1;
                    }
                        if(i==0)
                        {
                            ShopCarCookie+=tempvalue[0]+"|"+tempvalue[1]+"|"+tempvalue[2];
                        }
                        else
                        {
                            ShopCarCookie+=","+tempvalue[0]+"|"+tempvalue[1]+"|"+tempvalue[2];
                        }
                }
                if(MatchCount==0)
                {
                    
                    if(document.getElementById('ProductPanel').innerHTML=="")
                    {
                        InitItem();
                    }
                    ShopCarCookie+=","+pid+"|1|"+price;
                    AddItem(name,pic,price);
                }
            }
            _Main.UpdateShopCar(ShopCarCookie,Add_Callback);
            
        }
        function Add_Callback(res)
        {
            //alert(res.value);
            InitItemParam();
            document.getElementById('Car').style.display="block";
        }
        function AddItem(name,pic,price)
        {
            document.getElementById('ProductPanel').innerHTML=document.getElementById('ProductPanel').innerHTML+'<div class="List"><div class="Img"><img onload="DrawImage(this,68,68);" src="'+pic.split(".")[0]+"_128100."+pic.split(".")[1]+'" /></div><div class="Topic">'+name+' ￥<span name="Price">'+price+'</span>元</div><div class="Clear"></div></div>';
        }
        function InitItemParam()
        {
            _Main.GetShopCar(InitItemParam_Callback);
        }
        function InitItemParam_Callback(res)
        {
            //alert(res.value);
            ShopCarCookie = res.value;
            if(ShopCarCookie.match(/ShopCar=.*/)!=null)
            {
                var Val = ShopCarCookie;
                Val= Val.match(/ShopCar=.*/)[0];
                Val = Val.replace("ShopCar=","");
                Val = Val.split(",");
                document.getElementById('CountPrice').innerHTML="0.00";
                document.getElementById('Amound').innerHTML="0";
                for(var i=0;i<Val.length;i++)
                {
                    document.getElementById('Amound').innerHTML = parseInt(document.getElementById('Amound').innerHTML) + parseInt(Val[i].split("|")[1]);
                    
                    document.getElementById('CountPrice').innerHTML = parseFloat(document.getElementById('CountPrice').innerHTML)+(parseFloat(Val[i].split("|")[2])*parseFloat(Val[i].split("|")[1]));
                    
                }
            }
        }
        
        function InitItem()
        {
            if(ShopCarCookie.match(/ShopCar=.*/)!=null)
            {
                var Val = ShopCarCookie;
                Val= Val.match(/ShopCar=.*/)[0];
                Val = Val.replace("ShopCar=","");
                Val = Val.split(",");
                for(var i=0;i<Val.length;i++)
                {
                    document.getElementById('Loading').style.display='block';
                    _Main.GetProduct(Val[i].split("|")[0],GetProduct_Callback);
                }
            }
        }
        function GetProduct_Callback(res)
        {
            document.getElementById('Loading').style.display='none';
            AddItem(res.value.split("|")[0],res.value.split("|")[1],res.value.split("|")[2]);
        }
        
        
        function ChangeAmount(id,Amount)
        {
                if(Amount.replace(/\d{1,4}/,"")!="")
                {
                    alert('输入的商品数量不正确。');
                    window.location='/Cart';
                    return false;
                }
            var Val = ShopCarCookie;
                Val= Val.match(/ShopCar=.*/)[0];
                ShopCarCookie = ShopCarCookie.replace(Val,"");
                Val = Val.replace("ShopCar=","");
                var valueArr = Val.split(",");
                ShopCarCookie+="ShopCar=";
                var NowCount=0;
                for(var i=0;i<valueArr.length;i++)
                {
                    var tempvalue = valueArr[i].split('|');
                    if(tempvalue[0]==id)
                    {

                        tempvalue[1] = Amount;
                            
                    }
                        if(i==0)
                        {
                            if(tempvalue[1]!="0")
                            {
                                ShopCarCookie+=tempvalue[0]+"|"+tempvalue[1]+"|"+tempvalue[2];
                                NowCount=NowCount+1;
                            }
                        }
                        else
                        {
                            if(tempvalue[1]!="0")
                            {
                                if(NowCount!=0)
                                {
                                    ShopCarCookie+=","+tempvalue[0]+"|"+tempvalue[1]+"|"+tempvalue[2];
                                    NowCount=NowCount+1;
                                }
                                else
                                {
                                    ShopCarCookie+=tempvalue[0]+"|"+tempvalue[1]+"|"+tempvalue[2];
                                    NowCount=NowCount+1;
                                }
                            }
                        }
                }
                if(NowCount==0)
                {
                    ShopCarCookie=ShopCarCookie.replace("ShopCar=","");
                }
                _Main.UpdateShopCar(ShopCarCookie);
                window.location='/Cart';
        }