// arguments omitted or null default to:
//  path - current page
//  domain - current domain
//  expires - current session
//  secure - not secure
function z_setcookie (name, value, path, domain, expires, secure) {
    var newcookie = name + "=" + escape (value)
	+ (expires ? "; expires=" + expires.toUTCString () : "")
	+ (path ? "; path=" + path : "")
	+ (domain ? "; domain=" + domain : "")
	+ (secure ? "; secure" : "");
    document.cookie = newcookie;
}

function z_getcookie (name) {
    var dc = document.cookie;
    // make sure we get the exact name and not a substring of another name
    var str = "; " + name + "=";
    var pos1 = dc.indexOf (str);
    if (pos1 == -1) {
	// see if it's the first one and only the first one
	str = name + "=";
	pos1 = dc.indexOf (str);
	if (pos1 != 0) {
	    // check for ie cookie with no value
	    str = "; " + name + ";";
	    pos1 = dc.indexOf (str);
	    if (pos1 != -1) {
		return "";
	    }
	    // check for ie cookie with no value in first position
	    str = name + ";";
	    pos1 = dc.indexOf (str);
	    if (pos1 == 0) {
		return "";
	    }
	    // check for ie cookie with no value in last position
	    str = "; " + name;
	    pos1 = dc.indexOf (str);
	    if (pos1 == dc.length - str.length) {
		return "";
	    }
	    // cookie not found
	    return undefined;
	}
    }
    pos1 += str.length;
    var pos2 = dc.indexOf (";", pos1);
    return unescape(dc.substring (pos1, pos2 == -1 ? dc.length : pos2));
}

function z_deletecookie (name, path, domain) {
    z_setcookie (name, "", path, domain, z_expirationdate (-1));
}

function z_expirationdate (years, days, hours, minutes, seconds) {
    return new Date (
	new Date ().getTime () + 1000 * (
	    60 * (
		60 * (
		    24 * (
			365 * (years ? years : 0) + (days ? days : 0)
		    ) + (hours ? hours : 0)
		) + (minutes ? minutes : 0)
	    ) + (seconds ? seconds : 0)
	) - new Date(0).getTime ()
    );
}
