var today = new Date();
var expiry = new Date(today.getTime() + 3 * 24 * 60 * 60 * 1000);

function getCookieVal(offset) {
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1) { endstr = document.cookie.length; }
    return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie(name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
        {
            return getCookieVal(j);
        }
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
    return null;
}

function DeleteCookie(name, path, domain) {
    if (GetCookie(name))  {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; express=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

function SetCookie(name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expiry.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function setCheckbox(id) {
    var e = document.getElementById('h' + id);
    var i = document.getElementById('i' + id);

    if (e.value == 1) { 
        i.src = "/i/cbc.gif"; 
    } else { 
        i.src = "/i/cb.gif";  
    }
    e.value = 1 - e.value;
}

function setSort(str) {
    SetCookie('sort', str);
    document.location = document.location; // здесь не reload, чтобы избежать повторного post
}

function addCart(id) {
    var str = id + '=1;' + GetCookie('cart');
    SetCookie('cart', str, 0, '/');
    document.location = document.location;
}

function addFavorite() {
    window.external.AddFavorite('http://www.plazmamarket.ru','PlazmaMarket.Ru');
}

var compare = Array();

function checkCompare(id) {
    var temp = Array();
    var flag = false;
    for (var i = 0; i < compare.length; i++) {
        if (compare[i] != id) {
            temp[temp.length] = compare[i];
        } else { flag = true; }
    }
    if (!flag) temp[temp.length] = id;
    compare = temp;
    if (compare.length > 1) {
        document.location = '/shop/compare.html?id=' + compare[0] + '&id=' + compare[1];
    }
}
