// name - имя cookie
// value - значение cookie
// [expires] - дата окончания действия cookie (по умолчанию - до конца сессии)
// [path] - путь, для которого cookie действительно (по умолчанию - документ, в котором значение было установлено)
// [domain] - домен, для которого cookie действительно (по умолчанию - домен, в котором значение было установлено)
// [secure] - логическое значение, показывающее требуется ли защищенная передача значения cookie

//window.alert(document.cookie);

function SetCookie (name, key, value) {
		var argv = SetCookie.arguments;
		var argc = SetCookie.arguments.length;
		var expires = (argc > 3) ? argv[3] : null;
		var path = (argc > 4) ? argv[4] : null;
		var domain = (argc > 5) ? argv[5] : null;
		var secure = (argc > 6) ? argv[6] : false;
		var cookie = GetCookie(name, null)
		var suffix = key + '='
		n = cookie.indexOf(suffix)+suffix.length
		m = cookie.indexOf('&',n)
		newvalue = cookie.substr(0,n)+value//+cookie.substr(m,cookie.length-m)
		document.cookie = name + "=" + newvalue +
			((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
			((path == null) ? "" : ("; path=" + path)) +
			((domain == null) ? "" : ("; domain=" + domain)) +
			((secure == true) ? "; secure" : "");
}


// name - имя считываемого cookie
// kay  - ключ

function GetCookie(name, key) {
	var res	= null
	var prefix = name + "="
	var suffix = key + "="
	var cookieStartIndex = document.cookie.indexOf(prefix)
	if (cookieStartIndex == -1) {
		res = null
	}
	var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length)
	if (cookieEndIndex == -1) {
		cookieEndIndex = document.cookie.length
	}
	res = unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex))
	if (key!=null) {
		n = res.indexOf(suffix)
		if (n!=-1) {
			res = res.substr(n+suffix.length, res.length)
			res = res.substr(0,res.indexOf('&'))
		}
	}
	return res;
}


// 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 InitCookies() {
}
