// ==== write_cookie ==============================================================================
// name - the unique name of the data to store
// value - the value to associate to the given name
// ================================================================================================
function write_cookie(name, value)
{
    document.cookie = name + '=' + value + '; expires=Fri, 1 Jan 2020 01:23:56 UTC; path=/;'
}

// ==== read_cookie ===============================================================================
// returns an assoc array of the name/value pairs of the cookie
// ================================================================================================
function read_cookie()
{
    // init vars
    var pairs = document.cookie.split(';');
    var nv_pairs = new Array();
    
    // get the name / value pairs
    for (var i = 0; i < pairs.length; i ++)
    {
        var nvs = pairs[i].split('=');
        if (nvs.length == 2) nv_pairs[nvs[0].replace(' ', '')] = nvs[1].replace(' ', '');
    }
    
    // return the pairs
    return nv_pairs;
}

// ==== login_redirect ============================================================================
function login_redirect()
{
    // get the keys
    var pairs = read_cookie();
    var key_1 = typeof(pairs['key_1']) != 'undefined' && pairs['key_1'] != '' ? pairs['key_1'] : false;
    var key_2 = typeof(pairs['key_2']) != 'undefined' && pairs['key_2'] != '' ? pairs['key_2'] : false;
    
    // remove the keys to stop secondary redirects if old
    write_cookie('key_1', '');
    write_cookie('key_2', '');
    
    // redirect if needed
    if (key_1 !== false || key_2 !== false)
        window.location = '/login.php?key_1=' + key_1 + '&key_2=' + key_2;
}