//  Based on:  Bill Dortchs "Night of the Living Cookie" Version (25-Jul-96)

function FixCookieDate(date) {
    //  Function to correct for 2.x Mac date bug.  Call this function to
    //  fix a date object prior to passing it to SetCookie.
    //  IMPORTANT:  This function should only be called *once* for
    //  any given date object!  See example at the end of this document.
    var base = new Date(0);
    var skew = base.getTime(); // dawn of (Unix) time - should be 0
    if (skew > 0) {  // Except on the Mac - ahead of its time
        date.setTime (date.getTime() - skew);
    }
} // end FixCookieDate()

function GetCookie(name) {
    //  Function to return the value of the cookie specified by "name".
    //    name - String object containing the cookie name.
    //    returns - String object containing the cookie value, or null if
    //      the cookie does not exist.
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg) {
            return getCookieVal(j);
        }
        i = document.cookie.indexOf(" ", i) + 1;
        if ( i == 0 )
            break;
    } // end while
    return null;
} // end GetCookie()

function getCookieVal(offset) {
    // "Internal" function to return the decoded value of a cookie
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1) {
        endstr = document.cookie.length;
    }
    return unescape(document.cookie.substring(offset, endstr));
} // end getCookieVal()

function IsString(object) {
    // Because a string created with 'new' is of type 'object',
    // use this function to determine for sure whether an object is a string.
    if ( object == null )
        return ( false );
    if ( typeof(object.valueOf()) != 'string' )
        return ( false );
    return ( true );
} // end IsString()

function ParseIntAlways(str) {
    // Works like parseInt(str, 10) except will return 0 instead of NaN.
    var iNum = 0;
    iNum = parseInt(str, 10);
    if ( isNaN(iNum) ) iNum = 0;
    return ( iNum );
} // end ParseIntAlways()

function SetCookie(name,value,expires,path,domain,secure) {
    //  The first two parameters are required.
    //  Function to create or update a cookie.
    //    name - String object containing the cookie name.
    //    value - String object containing the cookie value.  May contain
    //      any valid string characters.
    //    [expires] - Date object containing the expiration data of the cookie.  If
    //      omitted or null, expires the cookie at the end of the current session.
    //      Also can be set to a string consisting of the amount of time into the future when the cookie should expire.
    //      For example, '1hour' or '7years' or '1minute' or '20seconds' etc.
    //      expires can also be set to 'never'
    //    [path] - String object indicating the path for which the cookie is valid.
    //      If omitted or null, uses the path of the calling document.
    //    [domain] - String object indicating the domain for which the cookie is
    //      valid. If omitted or null, uses the domain of the calling document.
    //    [secure] - Boolean (true/false) value indicating whether cookie transmission
    //      requires a secure channel (HTTPS).  
    if ( IsString(expires) ) {
        var exp = new Date();
        var szCount = new String();
        var iCount = 0;
        var szTimeType = new String();
        var nextChar = new String();
        var msTimeFromNow = 0;
        var idx = 0;
        for ( var idx = 0; idx < expires.length; idx++ ) {
            nextChar = expires.charAt(idx);
            if ( nextChar < "0" || nextChar > "9" ) {
                szTimeType = expires.substr(idx).toLowerCase();
                break;
            } // end if
            szCount = szCount.concat(nextChar);
        } // end for idx
        iCount = ParseIntAlways(szCount);
        if ( iCount == 0 ) iCount = 1;
        if ( szTimeType == 'never' ) // set 5 years in the future.
            msTimeFromNow = exp.getTime() + (5 * 365 * 24 * 60 * 60 * 1000);
        else if ( szTimeType == 'second' || szTimeType == 'seconds' )
            msTimeFromNow = exp.getTime() + (iCount * 1000);
        else if ( szTimeType == 'minute' || szTimeType == 'minutes' )
            msTimeFromNow = exp.getTime() + (iCount * 60 * 1000);
        else if ( szTimeType == 'hour' || szTimeType == 'hours' )
            msTimeFromNow = exp.getTime() + (iCount * 60 * 60 * 1000);
        else if ( szTimeType == 'day' || szTimeType == 'days' )
            msTimeFromNow = exp.getTime() + (iCount * 24 * 60 * 60 * 1000);
        else if ( szTimeType == 'week' || szTimeType == 'weeks' )
            msTimeFromNow = exp.getTime() + (iCount * 7 * 24 * 60 * 60 * 1000);
        else if ( szTimeType == 'month' || szTimeType == 'months' )
            msTimeFromNow = exp.getTime() + (iCount * 30 * 24 * 60 * 60 * 1000);
        else if ( szTimeType == 'year' || szTimeType == 'years' )
            msTimeFromNow = exp.getTime() + (iCount * 365 * 24 * 60 * 60 * 1000);
        else
            msTimeFromNow = 0;
        if ( msTimeFromNow > 0 ) {
            exp.setTime(msTimeFromNow);
            expires = exp;
            FixCookieDate(expires);
        } // end if
    } // end if IsString()
    document.cookie = name + "=" + escape (value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
} // end SetCookie()

function DeleteCookie(name,path,domain) {
    //  Function to delete a cookie. (Sets expiration date to start of epoch)
    //    name -   String object containing the cookie name
    //    path -   String object containing the path of the cookie to delete.  This MUST
    //             be the same as the path used to create the cookie, or null/omitted if
    //             no path was specified when creating the cookie.
    //    domain - String object containing the domain of the cookie to delete.  This MUST
    //             be the same as the domain used to create the cookie, or null/omitted if
    //             no domain was specified when creating the cookie.
    if ( GetCookie(name) ) {
        document.cookie = name + "=" +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        "; expires=Thu, 01-Jan-70 00:00:01 GMT";
     }
} // end DeleteCookie()

