﻿/**
 * 购物车，联合Cookies对象使用
 *
 *    Author： y
 * Create At： 2009-5-25
 */
function Cart() { }
Cart.prototype = {
    cookiesName: "CART_ITEM",//cookies标识
    
    //购物车模板
    cartTemplate: "<tr><td>{$id$}</td><td>{$name$}</td><td>￥{$salePrice$}</td><td><input value='{$quantity$}' {$script$}/></td><td><button onclick='{$del$}' type='button'></button></td><tr>",

    //购物车单项
    item: {
        id: 0,         //商品编号
        quantity: 0,   //购买数量
        name: "",      //商品名称
        salePrice: 0,  //销售价格
        cooperatePrice: 0,//合作商品价格 
        listimage: ""  //商品图片
    },

    items: [],         //购物车项组合

    itemsCount: 0,     //商品品种数量

    totalQuantity: 0,  //商品总量

    totalSalePrice: 0, //商品总额
    
    totalCooperatePrice: 0, //商品促销总价

    //添加购物车项
    addItem: function(id, quantity) {
        var isExist = false;    //用于判断是否新增
        if ((new Cookies()).select(this.cookiesName) != "undefined") {
            var str = (new Cookies()).select(this.cookiesName);
            this.toObject(str); //对象化购物车项

            for (var j = 0; j < this.items.length; j++) {
                if (this.items[j].id == id) {
                    isExist = true;
                    this.items[j].quantity = parseInt(this.items[j].quantity) + parseInt(quantity);
                    break;
                }
            }
        }
        if (!isExist) {
            this.item.id = id;
            this.item.quantity = quantity;
            this.items.push(this.item);
        }
        str = this.serializer(this.items);     //序列化items，然后写入cookies
        (new Cookies()).add(this.cookiesName, str, { expireHours: 336,path: "/" }); //加入Cookie，CART_ITEM，两星期后过期
        //alert((new Cookies()).select("CART_ITEM"));
    },

    //删除购物车项
    delItem: function(id) {
        if (confirm("确定删除？")) {
            var str = (new Cookies()).select(this.cookiesName);
            this.toObject(str); //对象化购物车项
            for (var i = 0; i < this.items.length; i++) {
                if (this.items[i].id == id) {
                    this.items.splice(i, 1);
                    break;
                }
            }
            str = this.serializer(this.items);     //序列化items，然后写入cookies
            //(new Cookies()).add("CART_ITEM", "", { path: "/" }); //加入Cookie，CART_ITEM
            (new Cookies()).add(this.cookiesName, str, { path: "/" }); //加入Cookie，CART_ITEM
        }
    },

    //修改购物车项
    updateItem: function(id, quantity) {
        var str = (new Cookies()).select(this.cookiesName);
        //alert(str);
        this.toObject(str); //对象化购物车项
        for (var i = 0; i < this.items.length; i++) {
            if (this.items[i].id == id) {
                this.items[i].quantity = quantity;
                break;
            }
        }

        str = this.serializer(this.items);     //序列化items，然后写入cookies
        (new Cookies()).add(this.cookiesName, str, { path: "/" }); //加入Cookie，CART_ITEM
    },

    //建造购物车
    buildCart: function(data) {
        var arr = data.split("|");
        var html = "";
        this.totalSalePrice = 0; //计算商品总额前先清零
        this.totalCooperatePrice = 0;//计算合作商品价总额
        this.totalQuantity = 0;  //计算商品总量前先清零
        this.itemsCount = 0;     //计算商品品种数量前先清零
        for (var i = 0; i < arr.length; i++) {
            this.item = eval("(" + arr[i] + ")");
            //alert(arr[i]);
            var tem;
            tem = this.cartTemplate;
            tem = tem.replace(/\{\$id\$\}/, this.item.id);
            tem = tem.replace(/\{\$name\$\}/, this.item.name);
            tem = tem.replace(/\{\$salePrice\$\}/, this.item.salePrice.toFixed(2));
            tem = tem.replace(/\{\$quantity\$\}/, this.item.quantity);
            tem = tem.replace(/\{\$listimage\$\}/, this.item.listimage);
            tem = tem.replace(/\{\$script\$\}/, "onblur=\"if(this.value=='')this.value='1';updateItem(" + this.item.id + ", this.value);\" onkeyup=\"this.value=this.value.replace(/[^0-9]/g,'').replace(/^[^1-9]/g, 1);\"");
            //tem = tem.replace(/\{\$script\$\}/, "onblur=\"\" onkeyup=\"this.value=this.value.replace(/[^0-9]/g,'').replace(/^[^1-9]/g, 1);\"");
            tem = tem.replace(/\{\$del\$\}/, "delItem(" + this.item.id + ");");
            tem = tem.replace(/\{\$cooperatePrice\$\}/, this.item.cooperatePrice.toFixed(2));
            html += tem;
            //alert(tem);

            //计算商品总额
            this.totalSalePrice += this.item.salePrice * this.item.quantity;
            //计算商品总量
            this.totalQuantity += this.item.quantity;
            //计算商品品种数量
            this.itemsCount += 1;
            //计算商品促销总价
            this.totalCooperatePrice += this.item.cooperatePrice * this.item.quantity;
        }
        //alert(this.totalPromPrice);
        return html;
    },

    //清空购物车
    clearCart: function() {
        if (confirm("确定清空购物车？")) {
            (new Cookies()).del(this.cookiesName);
        }
    },

    //序列化items对象
    serializer: function(its) {
        var str = "";
        for (var i = 0; i < its.length; i++) {
            str += "{";
            str += "id:" + its[i].id + ", quantity:" + its[i].quantity
            str += "}|";
        }
        str = str.substring(0, str.lastIndexOf("|"));
        return str;
    },

    //对象化购物车项
    toObject: function(str) {
        if (str != "") {
            this.items = str.split("|");
            for (var i = 0; i < this.items.length; i++) {
                this.items[i] = eval("(" + this.items[i] + ")");
            }
        }
    },
    
    //计算商品总量
    calTotalQuantity: function(){
        this.totalQuantity = 0;
        for(var i=0; i< this.items.length; i++){
            this.totalQuantity += parseInt(this.items[i].quantity);
        }
    }
}
