﻿// JScript 文件
$(function(){
    GetShoppingCart();
    GetDeletedShoppingCart();
})
//获取购物车
function GetShoppingCart()
{
    var cart1=Cookie.get("KDX.KLW.Cart");
    if(cart1!=null && cart1.length>0)
    {
        var data="Time="+new Date()+"&ptype=0&productInfo="+cart1;
        $.ajax({
        type:"get",url:"/Ashx/Order/ShoppingCart.ashx",data:data,
        success:function (msg)
        {
            $("#tableShopCart tbody").html("");
            $("#tableShopCart tbody").html(msg);           
            CalculationTotal();
        }});
    }else{
        $("#tableShopCart tbody").html("");
    }
}
//获取回收站中产品
function GetDeletedShoppingCart()
{
    var cart2=Cookie.get("KDX.KLW.DeleteCart");
    if(cart2!=null && cart2.length>0)
    {
        var dataDel="Time="+new Date()+"&ptype=1&productInfo="+cart2;
        $.ajax({
        type:"get",url:"/Ashx/Order/ShoppingCart.ashx",data:dataDel,
        success:function (msg)
        {
            $("#ShopCartDeleted").empty().append(msg);
        }});
    }
}
//获取购物车
function GetCart1()
{
    var x = Cookie.get("KDX.KLW.Cart");//初始化购物车
    alert(x);
    var y = Cookie.get("KDX.KLW.DeleteCart");//初始化购物车
    alert(y);
}
function showDel(delID)
{
    $('#'+delID).show();
}
function hideDel(delID)
{
    $('#'+delID).hide();
}
function DelCart(pID,pNum,pType,otherinfo)
{
    DeleteSettled("Cart",pID,pType,otherinfo); //deletedshoppingcart
    if(otherinfo){
        DelCombination(otherinfo);
    }
    else{
        $('#tr'+pID+"_"+pType).remove();
    }
    AddCartSettled('DeleteCart',pID,pNum,pType,otherinfo);//addDeleteCart    
    GiftProcess("Cart",pID,pNum,pType,"del",1);//38女人节    
    CalculationTotal();
    GetDeletedShoppingCart();
}
function DelCombination(otherinfo){
     $("#tr"+otherinfo).remove();
}

function resumeCart(pID,pNum,pType,otherinfo)
{
    if(otherinfo){
         $('#trDel'+otherinfo).remove();
           DeleteSettled("DeleteCart",pID,pType,otherinfo);
          AddCartSettled("Cart",pID,pNum,pType,otherinfo);
          GetShoppingCart();
        return;
    }    
    var existProduct=oneYuan._existProduct(pID);//1元选购加
    if(!existProduct && oneYuan._ifProduct())  //1元选购加
        alert('对不起，每张订单只能选1个特惠商品');//1元选购加
    else{//1元选购加
        pNum=existProduct?pNum:1;//1元选购加
        DeleteSettled("DeleteCart",pID,pType);
        $('#trDel'+pID+"_"+pType).remove();
        AddCartSettled("Cart",pID,pNum,pType);
        
        GiftProcess("DeleteCart",pID,pNum,pType,"del",pType);//38女人节

        GetShoppingCart();
    }
}
// +/- 修改产品数量
function ChangeProductQuantity(pID,numID,type)
{
    if(type==1)
    {
        var amount=$(numID).next('name=pNum');
        if(parseInt(amount.val())>1)
        {
            amount.val(parseInt(amount.val())-1);
            UpdateProductNumber(pID,amount.val(),1)
        }
        else
        {
            alert("购买数量必须大于1");
            return false;
        }
    }
    else
    {
        var amount=$(numID).prev('name=pNum');
        amount.val(parseInt(amount.val())+1);
        UpdateProductNumber(pID,amount.val(),1);
    }
}
    
//更新数量pid,pIsDisCount,pIsJewel,mIsJewel
function UpdateProductNumber(pID,amount,pType)
{ 
    var reg=/^\d*$/ //验证是否为数字
    if(!reg.test(amount))
    {
        alert("只能输入数字");
        return false;
    }
    if(parseFloat(amount)<=0)
    {
        alert("购买数量必须大于1");
        return false;
    }
    if(parseInt(pType) == 1)
    {
        CalculationTotal();
    }
    //更新购物车中产品的数量
    ModifyCart(pID,amount,pType);
    
    GiftProcess("Cart",pID,amount,pType,"update",1);//38女人节
}
//四舍五入函数 formatFloat("1212.1252", 2)
function formatFloat(src, pos)
{
    return Math.round(src*Math.pow(10, pos))/Math.pow(10, pos);
}
//计算总价
function CalculationTotal()
{
    //产品Id
    var commonPrice = $("#tableShopCart span[name='pCommonPrice']");
    var salePrice  = $("#tableShopCart span[name='pSalePrice']");  //价格
    var jewlPrice = $("#tableShopCart span[name='pJewlPrice']");  //钻卡价格
    var inputProductCount = $("#tableShopCart input[name='pNum']"); //数量
    var hidBuyProductType = $("#tableShopCart input[name='hidBuyProductType']"); //类别
    var subTotal= $("#tableShopCart span[name='subTotal']");//产品小计
	var sumCommonPrice=0;
    var sumPrice=0;
    var productList = "";
    salePrice.each(function(i)
    {
        var productSubTotal=0;
        //判断是否是购买的产品
        if($(hidBuyProductType[i]).val()==1)
        {
            if($(jewlPrice[i]).text()!="")
            {
                productSubTotal=parseFloat($(jewlPrice[i]).text()) * parseFloat($(inputProductCount[i]).val());
            }
            else
            {
                productSubTotal=parseFloat($(this).text()) * parseFloat($(inputProductCount[i]).val());
            }
        }
        $(subTotal[i]).html(productSubTotal.toFixed(2).toString());//每行的小计功能
        var commonSubTotal = parseFloat($(commonPrice[i]).text()) * parseFloat($(inputProductCount[i]).val());
        //判断是否是购买的产品
        if($(hidBuyProductType[i]).val()==1)
        {
            sumPrice=sumPrice + productSubTotal;//总金额
            sumCommonPrice=sumCommonPrice+commonSubTotal;
        }
        
    });
    //购物车部分
    $("#moneySave").text("￥"+formatFloat(sumCommonPrice-sumPrice,2).toString());
    $("#moneyTotal").text("￥"+formatFloat(sumPrice,2).toString());
    Cookie.set(new Cookie("KDX.KLW.total",formatFloat(sumPrice,2).toString()));//1元选购加
    oneYuan._forProduct();//1元选购加
}
//验证购物车是否为空
function validateShoppingcart()
{
    var cart=Cookie.get("KDX.KLW.Cart");
    if(cart.length>0 && cart!=null)
    {
        //验证购物车中是否全是赠品
        return ValidateCartIsValidate(cart);
    }
    else
    {
        alert("购物车不能为空！");
        return false;
    }
}

function ValidateCartIsValidate(cart)
{
    var pList=cart.split('|');
    if(pList.length>0)
    {
        var cartProductList="";//购物车中正品ID列表
        var cartActivityIDList="";
        for(var i=0;i<pList.length;i++)
        {
            var product = pList[i].split(',');
            var productType = product[2];
            var giftType=Cookie.get("KDX.KLW._k6gifttype");
            if(productType == "2" && giftType!=3)//是赠品且不是注册赠品,才需要验证
            {
                cartActivityIDList += product[0]+",";
            }
            else
            {
                cartProductList += product[0]+",";
            }
        }
        var activityProductList=cartActivityIDList.split(",");
        var result="true";
        for(var j=0;j<activityProductList.length-1;j++)
        {
            var data="Time="+new Date()+"&ptype=4&productID="+activityProductList[j];
            $.ajax({
            type:"get",url:"/Ashx/Gift/GetActivityProduct.ashx",data:data,async:false,
            success:function (msg)
            {
                result="false";
                if(msg.length>0){
                    var activityList=msg.split(',');//活动产品列表
                    var productList=cartProductList.split(',');//产品列表
                    for(var k=0;k<productList.length;k++)
                    {
                        for(var m=0;m<activityList.length;m++)
                        {
                            if(activityList[m]==productList[k]) {result="true";break;}
                        }
                        if(result=="true") break;
                    }
                    if(k>=productList.length){alert("对不起,您所选的赠品必须与对应的产品一起购买!");result="false";}
                }
            }});
            if(result=="true") {break;}
        }
        return result=="true";
    }
}