function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}
function ltrim(str, chars) {
    chars = chars || "s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
function rtrim(str, chars) {
    chars = chars || "s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

//kiem tra browser cho dung cookie khong
function checkBrowserEnableCookie() {
    var cookieEnabled = (navigator.cookieEnabled) ? true : false

    //if not IE4+ nor NS6+
    if (typeof navigator.cookieEnabled == "undefined" && !cookieEnabled) {
        document.cookie = "testcookie"
        cookieEnabled = (document.cookie.indexOf("testcookie") != -1) ? true : false
    }

    if (cookieEnabled) return true;
    else return false;

}

//tao cookie
function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = ";expires=" + date.toGMTString();
    }
    else
        var expires = "";
    document.cookie = name + "=" + value + expires + ";path=/"; //645;636;4607
}

//doc cookie
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';'); //ca{645 636 4607 lg=3}
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return "";
}

//xoa bo cookie
function eraseCookie(name) {
    createCookie(name, "", -1);
}


//doc so luong san pham dang co trong gio hang
function countShoppingCart(name) 
{
if(document.getElementById('count_shopping_cart')!=null)
{
    //tao cookie gio hang neu cua co
    if (readCookie(name) == "") {
        //createCookie(name,'',1);
        document.getElementById('count_shopping_cart').innerHTML = "0";
        //document.getElementById('count_shopping_cart_top').innerHTML = "<b>0</b>";
        //alert('create a new cookie shopping cart');
    } else {
        //add gia tri vao shopping cart
        var current_cart = readCookie(name);
        var ca = current_cart.split(',');
        number_product = ca.length - 1;
        document.getElementById('count_shopping_cart').innerHTML = "<b>" + number_product + "</b>";
        //document.getElementById('count_shopping_cart_top').innerHTML = "<b>" + number_product + "</b>";
    }
    //$("#shopping_cart_list").load("Cart/CartItemList.aspx");
    }
}


function emptyShoppingCart(name) {
    createCookie(name, '-', 1);
}
//them san pham vao gio hang
function addToShoppingCart(productId, colorId, quantity) {
    //tao cookie gio hang neu cua co

    if (readCookie('shopping_cart') == null) {
        createCookie('shopping_cart', '', 1);
        //alert('create a new cookie shopping cart');
    }
    //add gia tri vao shopping cart
    var current_cart = readCookie('shopping_cart');
    //kiem tra xem da duoc them vao cart hay chua
    if (current_cart.search(',' + productId + '-') == -1) 
    {        
        var new_cart = current_cart + ',' + productId + '-' + colorId + '-' + quantity;
        createCookie('shopping_cart', new_cart, 1);       
        countShoppingCart('shopping_cart');
        if (document.getElementById('item_' + productId) != null)
            document.getElementById('item_' + productId).innerHTML = 'Đã thêm vào giỏ hàng';
    }
    else 
    {
        alert('Sản phẩm này đã có trong giỏ hàng !');
    }
}




//Thêm sản phẩm giỏ hàng và chuyển tới trang giỏ hàng
function addToCartRedirect(productId, colorId, quantity) {
    if (readCookie('shopping_cart') == null) {
        createCookie('shopping_cart', '', 1);
    }
    var current_cart = readCookie('shopping_cart');

    if (current_cart.search(',' + productId + '-') == -1) {
        //var new_cart = current_cart + ',' + productId + '-' + quantity;
        var new_cart = current_cart + ',' + productId + '-' + colorId + '-' + quantity;
        createCookie('shopping_cart', new_cart, 1);
        //countShoppingCart('shopping_cart');
        window.location = "Cart/Cart.aspx";
    } else {
        alert('Sản phẩm này đã có trong giỏ hàng !');
    }
}

function checkItemInCart(productId) {
    var current_cart = readCookie('shopping_cart');
    //kiem tra xem da duoc them vao cart hay chua
    if (current_cart.search(',' + productId + '-') != -1) {
        document.write('Đang trong giỏ hàng');
    }
}

//xoa bo item trong cart
function deleteFromCart(productId, colorId, quantity) {
    if (confirm('Bạn muốn xóa sản phẩm này khỏi giỏ hàng ? ')) {  
        var current_cart = readCookie('shopping_cart');
        current_cart = current_cart + ",";
        new_cart = current_cart.replace("," + productId + '-' + colorId + '-' + quantity+",",",");
        new_cart = new_cart.replace("," + productId + '-' + colorId+",", "");
        new_cart = new_cart.replace("," + productId + '-' + quantity+",", "");
        new_cart = new_cart.replace("," + productId + '-', "");
        if(current_cart.indexOf(',')==-1)
        {
         createCookie('shopping_cart','', 1);        
        }
        else
            createCookie('shopping_cart', rtrim(new_cart,','), 1);
        countShoppingCart('shopping_cart');
        //ok nhay ve trang gio hang moi
       // window.location.reload();
        window.location.href = 'Cart.aspx';
    }
}

//cap nhat so luong san pham trong cart
function updateQuantityInCart(productId, colorId, quantity) {
    var newQuantity = document.getElementById("itemQuantity_" + productId).value;
    newQuantity = parseInt(newQuantity);
    if (newQuantity < 1) {
        //Nếu cập nhật số lượng < 1 thì coi như là xóa sản phẩm khỏi cart
        deleteFromCart(productId, colorI, dquantity);
    }
    else {
        if (newQuantity > 99) {
            alert('Bạn chỉ được phép mua tối đa số lượng 99 cho mỗi sản phẩm');
            newQuantity = 99;
        }

        var current_cart = readCookie('shopping_cart');
        new_cart = current_cart.replace("," + productId + '-' + colorId + '-' + quantity, "," + productId + '-' + colorId + '-' + newQuantity);
        //alert("gia tri cu:" + current_cart + "|Gia tri moi:" + new_cart);
        createCookie('shopping_cart', new_cart, 1);
        countShoppingCart('shopping_cart');
        //ok nhay ve trang gio hang moi
        window.location.href = 'Cart.aspx';
    }
}

function updateColorInCart(productId, colorId) {

    //    alert("voa toi ham");
    var newColorId = document.getElementById("itemColor_" + productId).value;
    newColorId = parseInt(newColorId);
    var current_cart = readCookie('shopping_cart');
    new_cart = current_cart.replace("," + productId + '-' + colorId, "," + productId + '-' + newColorId);
    // alert("gia tri cu:" + current_cart + "|Gia tri moi:" + new_cart);
    createCookie('shopping_cart', new_cart, 1);
    countShoppingCart('shopping_cart');
    //ok nhay ve trang gio hang moi
    window.location.href = 'Cart.aspx';
}



//Luu danh sach san pham da xem
function addToViewHistory(productId) {
    if (readCookie('product_history') == null) {
        createCookie('product_history', ',', 1);
    }
    var current_list = readCookie('product_history');
    if (current_list.search(',' + productId + ',') == -1) {
        var new_list = "," + productId + current_list;
        createCookie('product_history', new_list, 1);
    }
    ///alert(current_list);
}
