﻿AddProductToCart = function (id, image, name, price, promotion, type) {
    var path = "/";
    var exp = new Date();
    exp.setDate(exp.getDate() + 1);

    var cookie = new CookieHelper("$Temp", [id, image, name, price, promotion, type].join("$"), path, exp);
    cookie.Save();
    alert("Bạn đã thêm " + name + " vào giỏ hàng!");
    var sp = new ShoppingCart();
    document.getElementById("spProductsQuality").innerHTML = GetProductQuality();
}

SelectAllItemInCart = function (chkSelectAll) {
    if (typeof chkSelectAll == 'undefined')
        return;

    var chkSelectList = document.getElementsByName("chkSelectItemInCart");

    for (var i = 0, n = chkSelectList.length; i < n; i++) {
        chkSelectList[i].checked = chkSelectAll.checked;
    }
}

CheckItemInCart = function () {
    var c = document.getElementsByName("chkSelectItemInCart");
    var checkAll = true;

    for (var i = 0; i < c.length; i++) {
        if (!c[i].checked) {
            checkAll = false;
            break;
        }
    }

    var o = document.getElementById("chkSelectAllItemInCart");
    if (o == null) return;

    o.checked = checkAll;
}

SaveCart = function (txt) {
    var index = txt.parentNode.parentNode.rowIndex;
    if (sp) sp.Save(index, txt);
}

DeleteItemsInCart = function () {
    var c = document.getElementsByName("chkSelectItemInCart");
    if (typeof c == 'undefined') return;

    if (!sp) return;

    for (var i = c.length - 1; i >= 0; i--) {
        if (c[i].checked) sp.DeleteItem(c[i].value);
    }
}


CheckCart = function () {
    var path = '/';
    var cookieID = new CookieHelper("$ID", '', this.path);    // an array that holds all stored IDs
    var tmp = cookieID.Get();
    var url = window.location.protocol + "//" + window.location.host + "/" + window.location.pathname.split('/')[1] + "/Thanh-toan/index.wte";
    if (!tmp) {
        alert("Bạn chưa có hàng trong giỏ!!!")
        return;
    }
    else {
        window.location = url;
    }
}

GetProductQuality = function () {
    this.path = '/';
    var exp = new Date();
    exp.setDate(exp.getDate() + 1);

    var cookieAmount = new CookieHelper("$Amount", '', this.path, exp).Get();
    var total = 0;
    if (cookieAmount) {
        arr = cookieAmount.split("$");
        for (var i = 0, n = arr.length; i < n; i++) {
            total += parseInt(arr[i]);
        }
    }
    return total;
}


