function setActiveStyleSheet( title )
{
    var a;
    var link = document.getElementsByTagName( "link" );
    for( var i = 0; (a = link[i]); i++ )
    {
        var tit = a.getAttribute( "title" );
        var rel = a.getAttribute( "rel" );
        var index = rel.indexOf( "Style" );
	if( tit )
	{
            if( index != -1 )
            {
                a.disabled = true;
                if( tit == title )
                    a.disabled = false;
            }
        }
    }
}

function getActiveStyleSheet()
{
    var a;
    var link = document.getElementsByTagName( "link" );
    for( var i = 0; (a = link[i]); i++ )
    {
        var tit = a.getAttribute( "title" );
        var rel = a.getAttribute( "rel" );
        var index = rel.indexOf( "Style" );
	if( tit )
            if( index != -1 )
                if( ! a.disabled )
	            return tit;
    }

    return null;
}

function getPreferredStyleSheet()
{
    var a;
    var link = document.getElementsByTagName( "link" );
    for( var i = 0; (a = link[i]); i++ )
    {
        var tit = a.getAttribute( "title" );
        var rel = a.getAttribute( "rel" );
        var index = rel.indexOf( "Style" );
        var alt = rel.indexOf( "Alt" );
        if( index != -1 )
            if( alt == -1 )
                if( tit )
                   return tit;
    }

    return null;
}

function createCookie( name, value, days )
{
    if( days )
    {
        var date = new Date();
        date.setTime( date.getTime() + ( days * 24 * 60 * 60 * 1000 ) );
        var expires = "; expires=" + date.toGMTString();
    }
    else
        expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie( name )
{
    var nameEQ = name + "=";
    var cookie_array = document.cookie.split( ';' );
    for( var i = 0; i < cookie_array.length; i++ )
    {
        var c = cookie_array[i];
        while( c.charAt(0) == ' ' )
            c = c.substring( 1, c.length );
        if( c.indexOf( nameEQ ) == 0 )
	    return c.substring( nameEQ.length, c.length );
    }

    return null;
}

window.onload = function( e )
{
    var cookie = readCookie( "style" );
    var title = cookie ? cookie : getPreferredStyleSheet();
    setActiveStyleSheet( title );
}

window.onunload = function( e )
{
    var title = getActiveStyleSheet();
    createCookie( "style", title, 365 );
}

var cookie = readCookie( "style" );
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet( title );
