/*******************************************************
    by Patrick Dent
    Copyright Circleline Design 05/10/2006
    <patrick@circlelinedesign.co.uk>
    
    General cookie functionality through JavaScript
********************************************************/

//usage:
//JSCreateCookie        val = JSGetCookie('cookie_name');
//JSGetCookie           JSCreateCookie('cookie_name', val, 180, '/');
//JSDestroyCookie       JSDestroyCookie('cookie_name', '/');

//create & refresh a cookie/set a cookies value
//notes:-
//		pass name as the cookies string name
//		pass name as the cookies value
//		pass days2expire as the cookies integer expiry in days
function JSCreateCookie(name, value, days2expire)
{
    var thedate = new Date();

    thedate.setDate(thedate.getDate() + days2expire);
    document.cookie = name + '=' + escape(value) + ((days2expire == null) ? '' : ';expires=' + thedate);
}

//get a value from a named cookie
//notes:-
//		pass name as the cookies string name
function JSGetCookie(name)
{
    if(document.cookie.length > 0) {
        var intStart = document.cookie.indexOf(name + '=');

        if(intStart != -1) { 
            intStart = intStart + name.length + 1;
            var intEnd = document.cookie.indexOf(';', intStart);

            if(intEnd == -1) {
                intEnd = document.cookie.length;
            }

            return(unescape(document.cookie.substring(intStart, intEnd)));
        } 
    }

    return(null);
}

//removed a cookie/set expiration date
//notes:-
//		pass name as the cookies string name
//		pass path as the cookies string path
//		pass domain as the optional string domain (default is current domain settings)
function JSDestroyCookie(name, path, domain)
{
    if(JSGetCookie(name)) {
        document.cookie = name + '=' +
            ((path) ? ';path=' + path : '') +
            ((domain) ? ';domain=' + domain : '') +
            ';expires=Thu, 01-Jan-70 00:00:01 GMT';
    }
}

