
var comparedGoodsCnName = '';
function GetComparedGoodsCNName() {
    if (comparedGoodsCnName == null || comparedGoodsCnName == '') {
        if (window.location.toString().indexOf('www') >= 0)
            comparedGoodsCnName = 'ComparedGIDs3w';
        else
            comparedGoodsCnName = 'ComparedGIDs';
    }
    return comparedGoodsCnName;
}
 
function AddComparedGoods(goodsId, goodsName, goodsCode, goodsUrl, goodsImageUrl) {
    if(AddToComparedGoods(goodsId))
    $("#ezop-public-events").trigger("AddToComparedGoodsEvent", [goodsId, goodsName, goodsCode, goodsUrl, goodsImageUrl]);
}
function RemoveComparedGoods(goodsId) {
    if(RemoveFromComparedGoods(goodsId))
    $("#ezop-public-events").trigger("RemoveFromComparedGoodsEvent", [goodsId]);
}

function AddToComparedGoods(goodsId) {
    if (IsInComparedGoods(goodsId)) return false;
    var c = ComparatorGetCookie(GetComparedGoodsCNName());
    if (c == null) c = '';
    c = c + "_" + goodsId.toString() + "_";

    ComparatorSetCookie(GetComparedGoodsCNName(), 'IDs', c, 365);
    return true;
}

function RemoveFromComparedGoods(goodsId) {
    var c = ComparatorGetCookie(GetComparedGoodsCNName());
    if (c != null && c.length > 0) {
        c_start = c.indexOf("_" + goodsId.toString() + "_");
        if (c_start != -1) {
            c = c.replace("_" + goodsId.toString() + "_", "");
            ComparatorSetCookie(GetComparedGoodsCNName(), 'IDs', c, 365);
            return true;
        }
    }
    return false;
}

function IsInComparedGoods(goodsId) {
    var c = ComparatorGetCookie(GetComparedGoodsCNName());
    if (c != null && c.length > 0) {
        c_start = c.indexOf("_" + goodsId.toString() + "_");
        if (c_start != -1) return true;
    }
    return false;
}

function ClearComparedGoodsList() {
    var c = ComparatorGetCookie(GetComparedGoodsCNName());
    if (c != null && c.length > 0) {
        var allGoodsId = c.split("_", 100);
        for (i = 0; i <= allGoodsId.length; i++) {
            if(allGoodsId[i] != null && allGoodsId[i] != '')
                RemoveComparedGoods(allGoodsId[i]);
        }
    }
}

function ComparatorSetCookie(c_name, c_value_name, value, expiredays) {
    var exdate = new Date();
    if (value != null) value = value.replace(c_value_name + "=", "");
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + c_value_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString() + ";path=/");
}
function ComparatorGetCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}
