/////////////////////////////////////////////////////////////
// General Cookie related utility functions
/////////////////////////////////////////////////////////////

// Returns cookie value for a given name
// or null if no cookie with this name found
function getCookie(name) {
	var i = -1, c = document.cookie;
	do {
		if ((i = c.indexOf(name + "=", i + 1)) == -1)
			return null;
	}
	while (i != 0 && c.charAt(i - 1) != " ");
    var v = unescape(c.substring(++i + name.length, (i = c.indexOf(";", i)) == -1 ? c.length : i));
    return v;
}

// Sets cookie with a given name and value.
// If "expires" is omitted then the cookie expires when the user's session ends.
// See help for more details on other input params
function setCookie(name, value, expires, path, domain, secure) {
	document.cookie = name + "=" + escape(value) + (expires ? ";expires=" + expires.toGMTString() : "") +
		(path ? ";path=" + path : ";path=/") + (domain ? ";domain=" + domain : "") + (secure ? ";secure" : "");
}

// Removes cookie with a given name if exist.
// To remove cookie it is enough to set its expiration date to the past.
function removeCookie(name, path, domain) {
	if (getCookie(name)) {
		document.cookie = name + "=" +
			((path) ? ";path=" + path : "") +
			((domain) ? ";domain=" + domain : "") +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}
}
