﻿//添加cookie
var setCookie = function(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    cookieVal = c_name + "=" + decodeURI(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString() + ";path=/"); //
    document.cookie = cookieVal;
}

//读取cookies
function getCookie(name) {
    var nameValue = "";
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg)) {
        nameValue = decodeURI(arr[2]);
    }
    return nameValue;
}

function compareProduct(productid, productname, cookieName, compareCount) {

    var isExists = false;
    var pIDs = getCookie('CompareProducts').split(',');
    if (pIDs.length > 0 && getCookie('CompareProducts') != '') {
        for (var i = 0; i < pIDs.length; i++) {
            if (parseInt(productid) == parseInt(pIDs[i]))
                isExists = true;
        }
        if (isExists == false && parseInt(pIDs.length) >= parseInt(compareCount)) {
            alert('最多可以选择' + compareCount + '种产品进行对比.');
        }
        else if (isExists == false) {
            setCookie('CompareProducts', getCookie('CompareProducts') + "," + productid, 1);
            if ($("#compareBox")) {
                $("#compareBox").append("<div id='compareBox_" + productid + "'>" + productname + "<hr/></div>");
                $('#compareProducts').css("display", "");
            }
        }
    }
    else {
        setCookie('CompareProducts', productid, 1);
        if ($("#compareBox")) {
            $("#compareBox").append("<div id='compareBox_" + productid + "'>" + productname + "<hr/></div>");
            $('#compareProducts').css("display", "");
        }
    }
}

function clearCompareProduct() {
    setCookie('CompareProducts', '', 1);
    $("#compareBox").children().remove();
    $('#compareProducts').css("display", "none");
}
