Cookie (manipulation)

var
Cookie            = {}
Cookie.Get        = GetCookie
Cookie.Set        = SetCookie
Cookie.Unset      = UnsetCookie

Get

Cookie.Get( key )

function GetCookie( search )
{
    var key = "";
    var val = "";

    if ( "" != search )
    {
        var bits = document.cookie.split( ";" );
        var n    = bits.length;

        for ( var i=0; i < n; i++ )
        {
            var keyval = bits[i].split( "=" );

            if ( (2 == keyval.length) && (keyval[0].trim() == search) )
            {
                val = keyval[1].trim();
                break;
            }
        }
    }

    return val;
}

Set

Cookie.Set( path, cname, cvalue, expiration_days )

function SetCookie( path, cname, cvalue, exdays )
{
    var p       = ("" != path) ? path : "/";
    var d       = new Date(); d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = (0 != exdays) ? "expires="+d.toUTCString() + ";" : "";
    var secure  = 'https:' == location.protocol ? ";secure" : "";
    var cookie  = cname + "=" + cvalue + "; " + expires + " " + "path=" + p + secure + ";SameSite=strict";
    
    document.cookie = cookie;
}

Unset

Cookie.Unset( cname )

function UnsetCookie( name )
{
	document.cookie = name + "=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/";
}

Helper functions

function SetIDTypeCookie( idtype )
{
	SetCookie( "/", "idtype", idtype, 1 );
}

function SetSessionIDTypeCookie( sid )
{
	SetCookie( "/", "sessionid", sid, 1 );
}

function UnsetIDTypeCookie()
{
	document.cookie = "idtype=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/";
}

function UnsetSessionIDTypeCookie()
{
	document.cookie = "sessionid=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/";
}

function SetSelectCookieFilter( event )
{
    var select = event.target;
    var path   = location.pathname;
    var name   = select.name;
    var value  = select.options[select.selectedIndex].value;

    SetCookie( path, name, value, 0 );

    location.reload();
}