﻿_class.Cart = function () {
    this.Add = function (product_id) {
        if (this.Has_id(product_id)) {
        }
        else {
            this.Change(product_id, "1");
        }
        this.Reset();
        li("Shopping Cart");

    }
    this.Delete = function (product_id) {
        var id_list = "";
        var num_list = "";
        if (cookie("product_id") == null) {
            return;
        }
        var id = cookie("product_id").split(",");
        var number = cookie("product_quantity").split(",");
        for (var i = 0; i < id.length; i++) {
            if (id[i] == product_id) {
                continue;
            }
            id_list = id_list + id[i] + ",";
            num_list = num_list + number[i] + ",";
        }
        if (id_list != "") {
            cookie("product_id", id_list.substr(0, id_list.length - 1), 60 * 60 * 24 * 7);
            cookie("product_quantity", num_list.substr(0, num_list.length - 1), 60 * 60 * 24 * 7);
        }
        else {
            cookie("product_id", 0);
            cookie("product_quantity", 0);
        }
        this.Reset();
    }
    //转化为数字, 失败转化为0
    function ToNumber(value) {
        if (value == "") {
            return 0;
        }
        else {
            return Number(value);
        }
    }

    this.Reset = function () {
        var lis = $("cart", "li");
        if (lis == null) {
            return;
        }
        var sum_price = 0;
        var sum_weight = 0;
        for (var i = 0; i < lis.length; i++) {
            var li = lis[i];
            var id = li.id.replace("product", "");
            if (this.Has_id(id) == false) {
                $(li, false);
                if (i < lis.length - 1) {
                    $(lis[i + 1], _y, 40);
                    $$(lis[i + 1], _y, 0);
                }
                continue;
            }
            var price = $(li, "font0", "=") || 0;
            var weight = $(li, "font1", "=") || 0;
            var num = GetNumber(id);

            $(li, "input?.name=product_number", "=", num);

            var allprice = number(price * num, 2);
            var allweight = number(weight * num, 2);
            sum_price += price * num;
            sum_weight += weight * num;

            $(li, "font?.id=count_price", "=", allprice);
            $(li, "font?.id=count_weight", "=", allweight);

        }
        $("sum_price", "=", number(sum_price, 2));
        $("sum_weight", "=", number(sum_weight, 2));
    }
    this.Has_id = function (product_id) {
        if (cookie("product_id")) {
            var id = cookie("product_id").split(',');
            for (var i = 0; i < id.length; i++) {
                if (id[i] == product_id) {
                    return true;
                }
            }
            return false;
        }
        else {
            return false;
        }
    }

    function GetNumber(product_id) {
        if (cookie("product_id")) {
            var id = cookie("product_id").split(',');
            var number = cookie("product_quantity").split(',');
            for (var i = 0; i < id.length; i++) {
                if (id[i] == product_id) {
                    return number[i];
                }
            }
            return "0";
        }
        else {
            return "0";
        }
    }

    this.Modify = function (product_id, product_number) {
        var id = cookie("product_id").split(',');
        var number = cookie("product_quantity").split(',');
        var quantity = "";
        for (var i = 0; i < id.length; i++) {
            if (i > 0) {
                quantity += ",";
            }
            if (id[i] == product_id && product_number != "") {
                quantity += product_number;
            }
            else if (number.length > i) {
                quantity += number[i];
            }
            else {
                quantity += "0";
            }
        }
        cookie("product_quantity", quantity, 60 * 60 * 24 * 7);
    }

    this.Change = function (product_id, product_number) {
        if (isNaN(product_number) || product_number.indexOf(".") != -1 || product_number.indexOf("-") != -1 || product_number.indexOf("+") != -1 || product_number == 0) {
            this.Reset();
            return;
        }
        if (cookie("product_id") && cookie("product_quantity")) {
            if (this.Has_id(product_id)) {
                this.Modify(product_id, product_number);
            }
            else {
                cookie("product_id", cookie("product_id") + "," + product_id, 60 * 60 * 24 * 7);
                this.Modify(product_id, product_number);
            }
        }
        else {
            cookie("product_id", product_id, 60 * 60 * 24 * 7);
            cookie("product_quantity", product_number, 60 * 60 * 24 * 7);
        }
        this.Reset();
    }

    this.Clear = function () {
        cookie("product_id", 0);
        cookie("product_quantity", 0);
        this.Reset();
    }


    this.Pay = function () {
        li("/AddToList");
    }

    this.AddOrder = function () {
        var freight = $("order_freight", "=");
        if (window.confirm("Do you really want to add new order?")) {
            li("Shopping Cart", "AddOrder", "freight_id=" + freight);
        }
    }
}
var Cart = new _class.Cart();
