/*
Script Name: Javascript Cookie Script
Author: Public Domain, with some modifications
Script Source URI: http://techpatterns.com/downloads/javascript_cookies.php
Version 1.1.2
Last Update: 4 October 2007

Changes:
1.1.2 changes function names and indentation.
1.1.1 fixes a problem with Get_Cookie that did not correctly handle case
where cookie is initialized but it has no "=" and thus no value, the 
Get_Cookie function generates a NULL exception. This was pointed out by olivier, thanks

1.1.0 fixes a problem with Get_Cookie that did not correctly handle
cases where multiple cookies might test as the same, like: site1, site

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
*/

// If not found, defaultVal is returned. 
function v_getCookie( name, /* optional: */ defaultVal ) 
{
    // first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    var a_all_cookies = document.cookie.split( ';' );
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f
    
    for ( i = 0; i < a_all_cookies.length; i++ ) {
        // now we'll split apart each name=value pair
        a_temp_cookie = a_all_cookies[i].split( '=' );
        
        // and trim left/right whitespace while we're at it
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

        // if the extracted name matches passed name
        if ( cookie_name == name ) {
            b_cookie_found = true;
            // we need to handle case where cookie has no value
            // but exists (no = sign, that is):
            if ( a_temp_cookie.length > 1 ) {
                cookie_value = 
                    unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
            }
            // note that in cases where cookie is initialized but 
            // no value, null is returned
            return cookie_value;
            break;
        }
        a_temp_cookie = null;
        cookie_name = '';
    }

    if ( !b_cookie_found ) {
        /* will return undefined if defaultVal is undefined. */
        return defaultVal;
    }
}

/*
 * Only the first 2 parameters are required, the cookie name, the cookie
 * value. "expires" is in minutes.
 *
 * Generally you don't need to worry about domain, path or secure for most 
 * applications so unless you need that, leave those parameters blank in the 
 * function call.
 */
function v_setCookie( name, value, expires, path, domain, secure ) 
{
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime( today.getTime() );

    // if the expires variable is set, make it be expressed in
    // milliseconds.
    if ( expires ) {
        expires = expires * 1000 * 60;
    }

    /* Should be expressed in milliseconds. */
    var expires_date = new Date( today.getTime() + (expires) );

    document.cookie = name + "=" + escape( value ) +
        ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
        ( ( path ) ? ";path=" + path : "" ) + 
        ( ( domain ) ? ";domain=" + domain : "" ) +
        ( ( secure ) ? ";secure" : "" );
}

function v_setDictionaryCookie( name, dict, expires, path, domain, secure ) 
{
    var jsonVal = YAHOO.lang.JSON.stringify(dict);
    v_setCookie( name, jsonVal, expires, path, domain, secure );
}

function v_getDictionaryCookie( name, /* optional: */ defaultDict ) 
{
    var jsonVal = v_getCookie( name );
    if( ! isSet(jsonVal) || "[]" == jsonVal ) {
        return defaultDict;
    }

    return YAHOO.lang.JSON.parse(jsonVal);
}

// this deletes the cookie when called
function v_deleteCookie( name, path, domain ) 
{
    if ( v_getCookie( name ) ) document.cookie = name + "=" +
        ( ( path ) ? ";path=" + path : "") +
        ( ( domain ) ? ";domain=" + domain : "" ) +
        ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function v_areCookiesEnabled()
{
    v_setCookie( 'test', 'none', '', '/', '', '' );
    if ( v_getCookie( 'test' ) ) {
        v_deleteCookie('test', '/', '');
        return true;
    } 
    return false;
}

function v_getAllCookiesDict()
{
    var values = new Object();
    if (document.cookie == "") return values;
    
    var cookies = document.cookie.split(";");
    for(var i=0;i<cookies.length;i++){
        var pair = cookies[i].split("=");
        values[pair[0]] = unescape(pair[1]);
    }
    return values;
}
