﻿// JScript File

ShoppingCart = function (
) { this.initialize.apply(this, arguments); };
ShoppingCart.prototype =
{
    initialize: function () {
        // set info for cookie
        this.path = '/';
        var exp = new Date();
        exp.setDate(exp.getDate() + 1);

        // get cookie shoping cart
        this.cookieID = new CookieHelper("$ID", '', this.path, exp);    // an array that holds all stored IDs
        this.cookieImage = new CookieHelper("$Image", '', this.path, exp); // an arry that holds all stored Images
        this.cookieName = new CookieHelper("$Name", '', this.path, exp); // an array that holds all stored Names
        this.cookieAmount = new CookieHelper("$Amount", '', this.path, exp); // an array that holds all stored Amounts
        this.cookiePrice = new CookieHelper("$Price", '', this.path, exp); // an array that holds all stored Prices
        this.cookiePromotion = new CookieHelper("$Promotion", '', this.path, exp); // an array that holds all stored Promotion value
        this.cookieType = new CookieHelper("$Type", '', this.path, exp); // an array that holds all stored type of product

        // GUI
        this.table = $$("tblCart");
        this.sum = $$("lblSum");
        this.promotion = $$("lblPromotion");
        this.deliver = $$("lblDeliver");
        this.remain = $$("lblRemain");

        // sum
        this.total = 0; // total of product value
        this.totalp = 0; // total of promotion for every product
        this.cost = 0; // cost of deliver
        this.totalr = 0; // remain total = total - totalp - cost

        this.BuildCart();
    },
    GetAndDeleteTemp: function () {
        // get temp product
        var temp = new CookieHelper("$Temp", '', this.path);
        var value = temp.Get();
        if (value) {
            temp.Delete();
            return value.split("$");
        }
        return [];
    },
    Min: function (x, y) {
        return Math.min(x, y);
    },
    MinChain: function () {
        var min = arguments[0];
        for (var i = 1, n = arguments.length; i < n; i++) {
            min = this.Min(min, arguments[i]);
        }
        return min;
    },
    BuildCart: function () {
        //console.log("Start building");
        var arr = this.GetAndDeleteTemp();    // [id,name,price,promotion,type], amount is set to 1
        //if (arr.length > 0) 
        this.ClearCart();

        var arrID = [];
        var arrImage = [];
        var arrName = [];
        var arrAmount = [];
        var arrPrice = [];
        var arrPromotion = [];
        var arrType = [];

        var tmp = this.cookieID.Get();
        if (tmp) arrID = tmp.split("$");

        tmp = this.cookieImage.Get();
        if (tmp) arrImage = tmp.split("$");

        tmp = this.cookieName.Get();
        if (tmp) arrName = tmp.split("$");

        tmp = this.cookieAmount.Get();
        if (tmp) arrAmount = tmp.split("$");

        tmp = this.cookiePrice.Get();
        if (tmp) arrPrice = tmp.split("$");

        tmp = this.cookiePromotion.Get();
        if (tmp) arrPromotion = tmp.split("$");

        tmp = this.cookieType.Get();
        if (tmp) arrType = tmp.split("$");

        var count = this.MinChain(arrID.length, arrImage.length, arrName.length, arrAmount.length, arrPrice.length, arrPromotion.length, arrType.length);

        var addNew = true;

        for (var i = 0; i < count; i++) {
            if (arrID[i] == arr[0]) {
                arrAmount[i]++;
                // save amount to cookie
                this.cookieAmount.Set(arrAmount.join("$"));
                this.cookieAmount.Save();
                addNew = false;
            }

            this.AddRow(arrImage[i], arrName[i], arrAmount[i], arrPrice[i], arrPromotion[i]);
        }
        if (addNew && arr.length > 0) this.AddNewRow(arr, arrID, arrImage, arrName, arrAmount, arrPrice, arrPromotion, arrType);

        // no row add
        if (this.table != null && this.table.rows.length == 2) {
            this.AddNothingRow();
        }
    },

    ClearCart: function () {
        if (this.table != null)
        {
            while (this.table.rows.length > 2) 
            {
                this.table.deleteRow(1);
            }
        }
    },

    AddNewRow: function (arr, arrID, arrImage, arrName, arrAmount, arrPrice, arrPromotion, arrType)  // [id,name,price,promotion,type]
    {
        arrID[arrID.length] = arr[0];
        arrImage[arrImage.length] = arr[1];
        arrName[arrName.length] = arr[2];
        arrAmount.push(1);
        arrPrice[arrPrice.length] = arr[3];
        arrPromotion[arrPromotion.length] = arr[4];
        arrType[arrType.length] = arr[5];

        this.cookieID.Set(arrID.join("$"));
        this.cookieID.Save();

        this.cookieImage.Set(arrImage.join("$"));
        this.cookieImage.Save();

        this.cookieName.Set(arrName.join("$"));
        this.cookieName.Save();

        this.cookieAmount.Set(arrAmount.join("$"));
        this.cookieAmount.Save();

        this.cookiePrice.Set(arrPrice.join("$"));
        this.cookiePrice.Save();

        this.cookiePromotion.Set(arrPromotion.join("$"));
        this.cookiePromotion.Save();

        this.cookieType.Set(arrType.join("$"));
        this.cookieType.Save();

        this.AddRow(arr[1], arr[2], 1, arr[3], arr[4]);
    },
    AddRow: function (image, name, amount, price, promotion) {
        if (this.table == null) return;
        var row = this.table.insertRow(this.table.rows.length - 1);
        //row.className = 'shopping-cart-content-body';
        //console.log([name,amount,price]);

        // check to delete
        this.AddCell(row, "<input type='checkbox' name='chkSelectItemInCart' value='" + row.rowIndex + "' onclick='CheckItemInCart()' />").className = "pl20";
        //name
        this.AddCell(row, "<img src='" + image + "' alt='" + name + "' width='100' class='fl mr10' /><span class='name'>" + name + "</a>").className = "product";
        // amount 
        this.AddCell(row, "<input type='text' class='txtquantity' value='" + amount + "' onchange='SaveCart(this)'/>");
        // price
        this.AddCell(row, addCommas(price) + " VNĐ");
        // total
        this.AddCell(row, addCommas(amount * price) + " VNĐ");

        // total
        this.total += amount * price;
        this.totalp += amount * promotion;
        this.totalr = this.total - this.totalp + this.cost;
        this.sum.innerHTML = addCommas(this.total) + " VNĐ";
        this.promotion.innerHTML = addCommas(this.totalp) + " VNĐ";
        this.remain.innerHTML = addCommas(this.totalr) + " VNĐ";
        this.deliver.innerHTML = addCommas(this.cost) + " VNĐ";
    },
    DeleteRow: function (index) {
        this.table.deleteRow(index);
        if (this.table.rows.length == 2) {
            this.AddNothingRow();
        }
    },
    AddCell: function (row, value, css) {
        var cell = row.insertCell(-1);
        //if(css) cell.className = css;
        cell.innerHTML = value;
        return cell;
    },
    UpdateCell: function (cell, value) {
        cell.innerHTML = value;
    },
    Save: function (index, txt) {
        var value = parseInt($F(txt));
        var arrAmount = this.cookieAmount.Get().split("$");
        var arrPrice = this.cookiePrice.Get().split("$");
        var arrPromotion = this.cookiePromotion.Get().split("$");
        if (isNaN(value)) {
            if (arrAmount.length > index - 1) {
                txt.value = arrAmount[index - 1];
            }
        }
        else {
            if (value < 1) value = 1;
            else if (value > 100) value = 100;
            txt.value = value;
            if (arrPrice.length > index - 1 && arrAmount.length > index - 1 && arrPromotion.length > index - 1) {
                // change amount*price of row
                var price = arrPrice[index - 1];
                var promotion = arrPromotion[index - 1];
                this.table.rows[index].cells[4].innerHTML = addCommas(price * value) + " VNĐ";
                // change total, totalp, totalr
                this.total = this.total - price * (arrAmount[index - 1] - value);
                this.totalp = this.totalp - promotion * (arrAmount[index - 1] - value);
                this.totalr = this.total - this.totalp + this.cost;
                this.sum.innerHTML = addCommas(this.total) + " VNĐ";
                this.promotion.innerHTML = addCommas(this.totalp) + " VNĐ";
                this.remain.innerHTML = addCommas(this.totalr) + " VNĐ";
                // save amount to cookie
                arrAmount[index - 1] = value;
                this.cookieAmount.Set(arrAmount.join("$"));
                this.cookieAmount.Save();
            }

        }
        document.getElementById("spProductsQuality").innerHTML = GetProductQuality();
    },
    DeleteItem: function (index) {
        var arrID = this.cookieID.Get().split("$");
        var arrImage = this.cookieImage.Get().split("$");
        var arrName = this.cookieName.Get().split("$");
        var arrAmount = this.cookieAmount.Get().split("$");
        var arrPrice = this.cookiePrice.Get().split("$");
        var arrPromotion = this.cookiePromotion.Get().split("$");
        var arrType = this.cookieType.Get().split("$");

        if (arrID.length > index - 1 && arrImage.length > index - 1 && arrName.length > index - 1 && arrAmount.length > index - 1 && arrPrice.length > index - 1 && arrPromotion.length > index - 1 && arrType.length > index - 1) {
            var price = arrPrice[index - 1];
            var promotion = arrPromotion[index - 1];
            var amount = arrAmount[index - 1];

            // remove element at positon index - 1 of array
            arrID.splice(index - 1, 1);
            arrImage.splice(index - 1, 1);
            arrName.splice(index - 1, 1);
            arrAmount.splice(index - 1, 1);
            arrPrice.splice(index - 1, 1);
            arrPromotion.splice(index - 1, 1);
            arrType.splice(index - 1, 1);

            // change total, totalp, totalr
            this.total = this.total - price * amount;
            this.totalp = this.totalp - promotion * amount;
            this.totalr = this.total - this.totalp + this.cost;
            this.sum.innerHTML = addCommas(this.total) + " VNĐ";
            this.promotion.innerHTML = addCommas(this.totalp) + " VNĐ";
            this.remain.innerHTML = addCommas(this.totalr) + " VNĐ";

            // save to cookie
            this.cookieID.Set(arrID.join("$"));
            this.cookieID.Save();

            this.cookieImage.Set(arrImage.join("$"));
            this.cookieImage.Save();

            this.cookieName.Set(arrName.join("$"));
            this.cookieName.Save();

            this.cookieAmount.Set(arrAmount.join("$"));
            this.cookieAmount.Save();

            this.cookiePrice.Set(arrPrice.join("$"));
            this.cookiePrice.Save();

            this.cookiePromotion.Set(arrPromotion.join("$"));
            this.cookiePromotion.Save();

            this.cookieType.Set(arrType.join("$"));
            this.cookieType.Save();

            // delete table
            this.DeleteRow(index);
            // edit ProductQuality
            document.getElementById("spProductsQuality").innerHTML = GetProductQuality();
        }
    },
    AddNothingRow: function () {
        var row = this.table.insertRow(this.table.rows.length - 1);
        //row.className = 'shopping-cart-content-body';

        //
        var cell = row.insertCell(-1);
        cell.colSpan = 5;
        cell.innerHTML = "<p class='pl10'>Bạn không có mặt hàng nào trong giỏ hàng</p>";
    },
    BuildTotalByCost: function (cost) {
        this.cost = cost;
        this.totalr = (this.total - this.totalp) + parseInt(this.cost);
        this.deliver.innerHTML = addCommas(this.cost) + " VNĐ";
        this.remain.innerHTML = addCommas(this.totalr) + " VNĐ";
    }
    ,
    GetQuality: function () {
        var amount = this.cookieAmount.Get().split("$");
        var total = 0;

        for (var i = 0, n = amount.length; i < n; i++) {
            total += parseInt(amount[i]);
        }

        return total;
    }
}


// Tuannt 29/11/2010


ShoppingCart1 = function (
) { this.initialize.apply(this, arguments); };
ShoppingCart1.prototype =
{
    initialize: function () {
        // set info for cookie
        this.path = '/';
        var exp = new Date();
        exp.setDate(exp.getDate() + 1);

        // get cookie shoping cart
        this.cookieID = new CookieHelper("$ID", '', this.path, exp);    // an array that holds all stored IDs
        this.cookieImage = new CookieHelper("$Image", '', this.path, exp); // an arry that holds all stored Images
        this.cookieName = new CookieHelper("$Name", '', this.path, exp); // an array that holds all stored Names
        this.cookieAmount = new CookieHelper("$Amount", '', this.path, exp); // an array that holds all stored Amounts
        this.cookiePrice = new CookieHelper("$Price", '', this.path, exp); // an array that holds all stored Prices
        this.cookiePromotion = new CookieHelper("$Promotion", '', this.path, exp); // an array that holds all stored Promotion value
        this.cookieType = new CookieHelper("$Type", '', this.path, exp); // an array that holds all stored type of product

        // GUI
        this.table = $$("tblCart1");
        this.sum = $$("lblSum1");
        this.promotion = $$("lblPromotion1");
        this.remain = $$("lblRemain1");

        // sum
        this.total = 0; // total of product value
        this.totalp = 0; // total of promotion for every product
        this.cost = 0; // cost of deliver
        this.totalr = 0; // remain total = total - totalp - cost

        this.BuildCart();
    },
    GetAndDeleteTemp: function () {
        // get temp product
        var temp = new CookieHelper("$Temp", '', this.path);
        var value = temp.Get();
        if (value) {
            temp.Delete();
            return value.split("$");
        }
        return [];
    },
    Min: function (x, y) {
        return Math.min(x, y);
    },
    MinChain: function () {
        var min = arguments[0];
        for (var i = 1, n = arguments.length; i < n; i++) {
            min = this.Min(min, arguments[i]);
        }
        return min;
    },
    BuildCart: function () {
        //console.log("Start building");
        var arr = this.GetAndDeleteTemp();    // [id,name,price,promotion,type], amount is set to 1
        //if (arr.length > 0) 
        this.ClearCart();

        var arrID = [];
        var arrImage = [];
        var arrName = [];
        var arrAmount = [];
        var arrPrice = [];
        var arrPromotion = [];
        var arrType = [];

        var tmp = this.cookieID.Get();
        if (tmp) arrID = tmp.split("$");

        tmp = this.cookieImage.Get();
        if (tmp) arrImage = tmp.split("$");

        tmp = this.cookieName.Get();
        if (tmp) arrName = tmp.split("$");

        tmp = this.cookieAmount.Get();
        if (tmp) arrAmount = tmp.split("$");

        tmp = this.cookiePrice.Get();
        if (tmp) arrPrice = tmp.split("$");

        tmp = this.cookiePromotion.Get();
        if (tmp) arrPromotion = tmp.split("$");

        tmp = this.cookieType.Get();
        if (tmp) arrType = tmp.split("$");

        var count = this.MinChain(arrID.length, arrImage.length, arrName.length, arrAmount.length, arrPrice.length, arrPromotion.length, arrType.length);

        var addNew = true;

        for (var i = 0; i < count; i++) {
            if (arrID[i] == arr[0]) {
                arrAmount[i]++;
                // save amount to cookie
                this.cookieAmount.Set(arrAmount.join("$"));
                this.cookieAmount.Save();
                addNew = false;
            }

            this.AddRow(arrImage[i], arrName[i], arrAmount[i], arrPrice[i], arrPromotion[i]);
        }
        if (addNew && arr.length > 0) this.AddNewRow(arr, arrID, arrImage, arrName, arrAmount, arrPrice, arrPromotion, arrType);

        // no row add
        if (this.table != null && this.table.rows.length == 2) {
            this.AddNothingRow();
        }
    },

    ClearCart: function () {
    if (this.table != null)
    {
        while (this.table.rows.length > 2) {
            this.table.deleteRow(1);
        }
        }
    },

    AddNewRow: function (arr, arrID, arrImage, arrName, arrAmount, arrPrice, arrPromotion, arrType)  // [id,name,price,promotion,type]
    {
        arrID[arrID.length] = arr[0];
        arrImage[arrImage.length] = arr[1];
        arrName[arrName.length] = arr[2];
        arrAmount.push(1);
        arrPrice[arrPrice.length] = arr[3];
        arrPromotion[arrPromotion.length] = arr[4];
        arrType[arrType.length] = arr[5];

        this.cookieID.Set(arrID.join("$"));
        this.cookieID.Save();

        this.cookieImage.Set(arrImage.join("$"));
        this.cookieImage.Save();

        this.cookieName.Set(arrName.join("$"));
        this.cookieName.Save();

        this.cookieAmount.Set(arrAmount.join("$"));
        this.cookieAmount.Save();

        this.cookiePrice.Set(arrPrice.join("$"));
        this.cookiePrice.Save();

        this.cookiePromotion.Set(arrPromotion.join("$"));
        this.cookiePromotion.Save();

        this.cookieType.Set(arrType.join("$"));
        this.cookieType.Save();

        this.AddRow(arr[1], arr[2], 1, arr[3], arr[4]);
    },
    AddRow: function (image, name, amount, price, promotion) {
        if (this.table == null) return;
        var row = this.table.insertRow(this.table.rows.length - 1);
        //row.className = 'shopping-cart-content-body';
        //console.log([name,amount,price]);

        this.AddCell(row, addCommas(name)).className = "pl20";
        this.AddCell(row, addCommas(amount)).className = "tc";
        this.AddCell(row, addCommas(price) + " VNĐ");
        // total
        this.AddCell(row, addCommas(amount * price) + " VNĐ");

        // total
        this.total += amount * price;
        this.totalp += amount * promotion;
        this.totalr = this.total - this.totalp + this.cost;
        this.sum.innerHTML = addCommas(this.total) + " VNĐ";
        this.promotion.innerHTML = addCommas(this.totalp) + " VNĐ";
        this.remain.innerHTML = addCommas(this.totalr) + " VNĐ";
    },
    DeleteRow: function (index) {
        this.table.deleteRow(index);
        if (this.table.rows.length == 2) {
            this.AddNothingRow();
        }
    },
    AddCell: function (row, value, css) {
        var cell = row.insertCell(-1);
        //if(css) cell.className = css;
        cell.innerHTML = value;
        return cell;
    },
    UpdateCell: function (cell, value) {
        cell.innerHTML = value;
    },
    Save: function (index, txt) {
        var value = parseInt($F(txt));
        var arrAmount = this.cookieAmount.Get().split("$");
        var arrPrice = this.cookiePrice.Get().split("$");
        var arrPromotion = this.cookiePromotion.Get().split("$");
        if (isNaN(value)) {
            if (arrAmount.length > index - 1) {
                txt.value = arrAmount[index - 1];
            }
        }
        else {
            if (value < 1) value = 1;
            else if (value > 100) value = 100;
            txt.value = value;
            if (arrPrice.length > index - 1 && arrAmount.length > index - 1 && arrPromotion.length > index - 1) {
                // change amount*price of row
                var price = arrPrice[index - 1];
                var promotion = arrPromotion[index - 1];
                this.table.rows[index].cells[4].innerHTML = addCommas(price * value) + " VNĐ";
                // change total, totalp, totalr
                this.total = this.total - price * (arrAmount[index - 1] - value);
                this.totalp = this.totalp - promotion * (arrAmount[index - 1] - value);
                this.totalr = this.total - this.totalp + this.cost;
                this.sum.innerHTML = addCommas(this.total) + " VNĐ";
                this.promotion.innerHTML = addCommas(this.totalp) + " VNĐ";
                this.remain.innerHTML = addCommas(this.totalr) + " VNĐ";
                // save amount to cookie
                arrAmount[index - 1] = value;
                this.cookieAmount.Set(arrAmount.join("$"));
                this.cookieAmount.Save();
            }

        }
    },
    DeleteItem: function (index) {
        var arrID = this.cookieID.Get().split("$");
        var arrImage = this.cookieImage.Get().split("$");
        var arrName = this.cookieName.Get().split("$");
        var arrAmount = this.cookieAmount.Get().split("$");
        var arrPrice = this.cookiePrice.Get().split("$");
        var arrPromotion = this.cookiePromotion.Get().split("$");
        var arrType = this.cookieType.Get().split("$");

        if (arrID.length > index - 1 && arrImage.length > index - 1 && arrName.length > index - 1 && arrAmount.length > index - 1 && arrPrice.length > index - 1 && arrPromotion.length > index - 1 && arrType.length > index - 1) {
            var price = arrPrice[index - 1];
            var promotion = arrPromotion[index - 1];
            var amount = arrAmount[index - 1];

            // remove element at positon index - 1 of array
            arrID.splice(index - 1, 1);
            arrImage.splice(index - 1, 1);
            arrName.splice(index - 1, 1);
            arrAmount.splice(index - 1, 1);
            arrPrice.splice(index - 1, 1);
            arrPromotion.splice(index - 1, 1);
            arrType.splice(index - 1, 1);

            // change total, totalp, totalr
            this.total = this.total - price * amount;
            this.totalp = this.totalp - promotion * amount;
            this.totalr = this.total - this.totalp + this.cost;
            this.sum.innerHTML = addCommas(this.total) + " VNĐ";
            this.promotion.innerHTML = addCommas(this.totalp) + " VNĐ";
            this.remain.innerHTML = addCommas(this.totalr) + " VNĐ";

            // save to cookie
            this.cookieID.Set(arrID.join("$"));
            this.cookieID.Save();

            this.cookieImage.Set(arrImage.join("$"));
            this.cookieImage.Save();

            this.cookieName.Set(arrName.join("$"));
            this.cookieName.Save();

            this.cookieAmount.Set(arrAmount.join("$"));
            this.cookieAmount.Save();

            this.cookiePrice.Set(arrPrice.join("$"));
            this.cookiePrice.Save();

            this.cookiePromotion.Set(arrPromotion.join("$"));
            this.cookiePromotion.Save();

            this.cookieType.Set(arrType.join("$"));
            this.cookieType.Save();

            // delete table
            this.DeleteRow(index);
        }
    },
    AddNothingRow: function () {
        var row = this.table.insertRow(this.table.rows.length - 1);
        //row.className = 'shopping-cart-content-body';

        //
        var cell = row.insertCell(-1);
        cell.colSpan = 5;
        cell.innerHTML = "<p class='pl10'>Bạn không có mặt hàng nào trong giỏ hàng</p>";
    },
    BuildTotalByCost: function (cost) {
        this.cost = cost;
        this.totalr = (this.total - this.totalp) + parseInt(this.cost);
        this.remain.innerHTML = addCommas(this.totalr) + " VNĐ";
    }
    ,
    GetQuality: function () {
        var amount = this.cookieAmount.Get().split("$");
        var total = 0;

        for (var i = 0, n = amount.length; i < n; i++) {
            total += parseInt(amount[i]);
        }

        return total;
    }
}
