/***  (C)Stephen Chalmers

 Info: www.hotspot.freeserve.co.uk/scripterlative


Description of Functions and Usage
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

bump( cookieName, increment, days, path, domain, secure )

 Creates a numeric integer cookie or increments its value.
 The value should be/is set to a positive or negative decimal integer

 Increment the value of 'mysiteVisits' by 1 and set lifetime to 30 days.
 If 'mysiteVisits' doesn't exist, create it with a value of 1.

 XCookie.bump('mysiteVisits', 1, 30);

enabled()

 Returns the boolean status of cookie support on the client.

  if( !XCookie.enabled() )
   alert("Please enable cookie support")

exists(cookieName)

 Returns the boolean existence status of a specified cookie.

  if( XCookie.exists('noSound') )
   playSound=false;

read(cookieName)

 Returns the value of a specified cookie, or "" on failure.

  if( (v = XCookie.read('uservisits')) != "" )
   document.write("Number of previous visits: " + v);

refresh(cookieName, days, path, domain, secure)

 Refreshes the lifetime of a specified cookie, preserving its current value.

   // Reset the lifetime of cookie 'userID' to 30 days
   XCookie.refresh('userID', 30);

refreshAll(days)

 Refreshes all owned cookies by the specified number of days, or 30 if no parameter is specified.


clear(cookieName)

 Deletes a specified cookie

  XCookie.clear('lastPageViewed');


set(cookieName, cookieValue, daysDuration, path, domain, secure)

 Creates a cookie with specified value, duration and optional path.

  set("userName", "J Smith", 30);

  set("userName", "J Smith", 30, '/login');


Meaning of Parameters
~~~~~~~~~~~~~~~~~~~~~
cName:  The name of the cookie.
cValue: The value to be stored.
duration: The number of full days the cookie is to exist. To create a session cookie, specify 0.
increment: The amount by which the numeric decimal integer value stored will be incremented.
domain: (Optional) The domain on which the cookie will be readable, if not the current.
path: (Optional) Represents the highest folder level at which the cookie is readable. The default value is '/'.
secure: (Optional) set to true (without quotes).

*/

var XCookie =
{
    supportStatus: false,
    escFunc: encodeURIComponent||escape,
    unescFunc: decodeURIComponent||unescape,

    read: function(cookieName) {
        var cValue="";
        if(typeof document.cookie != 'undefined') {
            cValue=(cValue=document.cookie.match(new RegExp(cookieName+'=([^;]+);?'))) ? cValue[1] : "";
        }
        return this.unescFunc(cValue);
    },

    set: function(cName, cValue, duration, path, domain, secure) {
        if(typeof path!='string' || !/\S/.test(path))
            path='/';

        if(!this.supportStatus)
            this.supportStatus=this.enabled();

        if(this.supportStatus) {
            var cs="", expDate=new Date();
            expDate.setDate(expDate.getDate() + duration);
            cValue = this.escFunc(cValue);
            cs=cName+"="+cValue+';'+'path='+path;
            if(duration)
                cs+=";expires=" + expDate.toGMTString();

            if(domain)
                cs += ';domain=' + domain;

            if(secure==true)
                cs += ';secure' ;

            document.cookie=cs;
        }
        return this.read(cName);
    },

    refresh: function(cName, duration, path, domain, secure) {
        if(path==undefined)
            path='/';
        var val = this.read(cName);

        if(val != "")
            this.set(cName, val, duration, path, domain, secure);

        return this.read(cName);
    },

    refreshAll: function( days, path, domain, secure ) {
        var rslt, rxp=/([^;=\s]+)=([^;]*);?/g, cString, count=0;

        if(days==undefined)
            days=30;

        if( typeof(cString=document.cookie)=='string' ) {
            while( (rslt=rxp.exec(cString)) ) {
                this.set( rslt[1], rslt[2], days, path, domain, secure );
                count++;
            }
        }
        return count;
    },

    bump: function( cName, increment, duration, path, domain, secure ) {
        if(path==undefined)  /** cookie by value 'increment'. If cookie does not exist **/
            path='/';           /** create it with an initial value of increment. **/
        var v;
        this.set( cName, !isNaN( v=parseInt(this.read( cName ), 10) ) ? v+increment : increment, duration, path, domain, secure );
    },

    exists: function(cName) {
        return((document.cookie && document.cookie.indexOf(cName)>-1)?true:false );
    },

    clear: function(cName) {
        this.set(cName,0,-1);
    },

    clearAll: function() {
        this.refreshAll(-1);
    },

    enabled: function() {
        var rv=false, expDate=new Date(), cString;

        if(typeof document.cookie!='undefined') {
            expDate.setDate(expDate.getDate() -1);
            for(var i=0; this.exists("XCookieSupport"+i); i++)
            ;

            cString="XCookieSupport"+i;
            document.cookie=cString + "=OK";
            rv=this.exists("XCookieSupport"+i);
            document.cookie=cString+"=OK;expires="+expDate.toGMTString();
        }
        return rv;
    }
}
