// myacctlogoff.js, swalker@isomedia.com
// dynamic logoff button for myaccount side menu.
// login.php will return a JSON object with a 'loggedin' property if POSTed with loggedin=<anything>.
// the .loggedin property of the JSON object will say whether there is a current session active within login.php

// browser independent http request object.
function browserrequestobject()
{
    var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];
    if(window.ActiveXObject)
    {
        for(var i = 0; i < activexmodes.length; i++)
        {
            try
            {
                return new ActiveXObject(activexmodes[i]);
            }
            catch(e)
            {
                // squelch error
            }
        }
    }
    else if (window.XMLHttpRequest)
        return new XMLHttpRequest();
    else
        return false;
}

// browser independent function to hide a DOM id.
function hideid(id)
{
    if(document.getElementById)
        document.getElementById(id).style.display = 'block';
    else
    {
        if(document.layers)
            document.id.display = 'block';
        else
            document.all.id.style.display = 'block';
    }
}

// a hopefully less jumpy way to hide element.
function addstylesheet(stylesheet)
{
    var head = document.getElementsByTagName('head')[0];
    if(head)
    {
        var style = document.createElement('link');
        style.rel = 'stylesheet';
        style.type = 'text/css';
        style.href = stylesheet;
        head.appendChild(style);
    }
    else
        document.write('<style media="all" type="text/css">@import "' + stylesheet + '";</style>');
}

// request login status from login.php.
function loggedin()
{
    var myrequest = new browserrequestobject;
    myrequest.open("GET", "/myaccount/login.php?loggedin=1", false);
    myrequest.send(null);
    var jsondata = eval("(" + myrequest.responseText + ")");
    return jsondata.loggedin;
}

// show (unhide) log off button if login.php says we're logged in.
function dologoffbutton()
{
    if(!loggedin())
	addstylesheet('/css/myacctlogoff.css');
}

dologoffbutton();


