function hex(iValue){
  return parseInt(iValue).toString(16).toUpperCase();
}

__escapeTable = new Object();
__unescapeTable = new Object();

__escapeTable['401']  = '%A8';       // Ё
__unescapeTable['A8'] = 0x401;       // Ё
__escapeTable['451']  = '%B8';       // ё
__unescapeTable['B8'] = 0x451;       // ё

for (i = 0xC0; i <= 0xFF; i++)   // А-Яа-я
{
  __unescapeTable[hex(i)] = String.fromCharCode(i + 0x350); 
  __escapeTable[hex(i + 0x350)] = '%' + hex(i);
}

_escape = function(sString){
  return escape(sString).replace(/%u0([0-9a-f]{3})/gi, 
                                     function(sMatch, sCode) { 
                                      return __escapeTable[sCode.toUpperCase()] || sMatch
                                    });
}

_unescape = function(sString){
  return unescape(sString.replace(/%([0-9a-f]{2})/gi, 
                                    function(sMatch, sCode){ 
                                      return __unescapeTable[sCode.toUpperCase()] || sMatch 
                                    }));
}

// name - имя cookie
// value - значение cookie
// [expires] - дата окончания действия cookie (по умолчанию - до конца сессии)
// [path] - путь, для которого cookie действительно (по умолчанию - документ, в котором значение было установлено)
// [domain] - домен, для которого cookie действительно (по умолчанию - домен, в котором значение было установлено)
// [secure] - логическое значение, показывающее требуется ли защищенная передача значения cookie

function setCookie(name, value, expires, path, domain, secure) {
        var curCookie = name + "=" + _escape(value) +
                ((expires) ? "; expires=" + expires.toGMTString() : "") +
                ((path) ? "; path=" + path : "") +
                ((domain) ? "; domain=" + domain : "") +
                ((secure) ? "; secure" : "");
//        if (!caution || (name + "=" + _escape(value)).length <= 4000)
//                document.cookie = curCookie;
//        else
//                if (confirm("Cookie превышает 4KB и будет вырезан !"))
                        document.cookie = curCookie;
}

// name - имя считываемого cookie

function getCookie(name) {
        var prefix = name + "=";
        var cookieStartIndex = document.cookie.indexOf(prefix);
        if (cookieStartIndex == -1)
                return null;
        var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
        if (cookieEndIndex == -1)
                cookieEndIndex = document.cookie.length;
        return _unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

// name - имя cookie
// [path] - путь, для которого cookie действительно
// [domain] - домен, для которого cookie действительно
function deleteCookie(name, path, domain) {
        if (getCookie(name)) {
                document.cookie = name + "=" + 
                ((path) ? "; path=" + path : "") +
                ((domain) ? "; domain=" + domain : "") +
                "; expires=Thu, 01-Jan-70 00:00:01 GMT";
        }
}

function showCookie(name) {
        var prefix = name + "=";
        var cookieStartIndex = document.cookie.indexOf(prefix);
        if (cookieStartIndex == -1)
                return null;
        var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
        if (cookieEndIndex == -1)
                cookieEndIndex = document.cookie.length;
        alert (_unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex)));
}

function showCookies() {
 alert(document.cookie);
 alert(_unescape(document.cookie));
}

