/*
 * Copyright (C) 2004 Stephen Ostermiller
 * http://ostermiller.org/contact.pl?regarding=Bookmarklets
 *
 * This bookmarklet is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This bookmarklet 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.  See the
 * GNU General Public License for more details.
 */

/**
 * Clears all cookies for the current page.
 *
 * @name Clear Cookies
 *
 * @substitute cookie_list       a
 * @substitute sub_domain        b
 * @substitute location_pathname c
 * @substitute document_cookie   d
 * @substitute cookie_index      e
 * @substitute cookie_count      f
 *
 * @compatible Firefox
 * @compatible Netscape
 * @compatible Opera
 * @compatible Internet Explorer
 * @compatible Mozilla
 * @notcompatible Konqueror
 * @compatible Safari
 *
 * @category Cookies
 * @category Utilities
 *
 * @keyword clear cookies bookmarklet
 * @keyword delete cookies bookmarklet
 * @keyword remove cookies bookmarklet
 * @keyword expire cookies bookmarklet
 */
void(
    (function(){
        var cookie_list,sub_domain,location_pathname,cookie_index,cookie_count;
        cookie_count=0;
        // Get the semicolon separated list of cookies and make an array out of them
        cookie_list=document.cookie.split("; ");
        // Go through each cookie and delete it.  The &&cooki_list[cookie_index] check is there
        // for the case in which there are no cookies and the split returns the empty string.
        for(cookie_index=0;cookie_index<cookie_list.length&&cookie_list[cookie_index];cookie_index++){
            cookie_count++;
            // Each cookie needs to be expired on all possible domains.  For example:
            // .subdomain.example.com
            // subdomain.example.com
            // .example.com
            // example.com
            // .com
            // com
            for(sub_domain="."+location.host;sub_domain;sub_domain=sub_domain.replace(/^(?:\.|[^\.]+)/,"")){
                // Each cookie needs to be expired on all possible paths.
                // Take one character at a time off the path.
                for(location_pathname=location.pathname;location_pathname;location_pathname=location_pathname.replace(/.$/,"")){
                    // set an expired cookie to delete the cookie
                    // The new cookie will have the same name and value as the old cookie,
                    // but will be deleted immediately by the browser's cookie management
                    document.cookie=(
                       cookie_list[cookie_index]+
                       "; domain="+
                       sub_domain+
                       "; path="+
                       location_pathname+
                       "; expires="+
                       // Take a couple years off of today's date
                       new Date((new Date()).getTime()-1e11).toGMTString()
                    );
                }
            }
        }
        alert("Expired "+cookie_count+" cookies");
    })()
)
