/*
    Functions used only by the minilogin page.
    
    Requires:
    - locale.js (installed by common.js)
    - session.js (installed by common.js)
    - util.js (installed by common.js)
*/

/*
    Displays the minilogin page in an iframe, but only if the user
    is not logged in.
    
    Invoked by country landing pages.

    This could have been done using an iframe coded into the page
    that would be hidden if the user is logged in, but that would 
    degrade response time, since the request would be made anyway.
    
    Many sites just place the login form on the unprotected page, 
    but that's not secure.
*/
function maybeDisplayLoginForm()
{
    if ( loggedIn() )
    {
        addSessionTimeoutListener(displayLoginForm);
    }
    else
    {
        displayLoginForm();
    }
}

function displayLoginForm()
{
    var loginbox = document.getElementById('login-box');
    if ( loginbox != null )
    {
        var height = loginbox.style.height.replace('px','');
        var src    = "/account/" + currentLocale() + "/minilogin.html";

        var iframedef = loginbox.innerHTML;

        // replace value of src attribute with minilogin.html
        // ... String.replace(/src=[^ ]/,...) doesn't work
        // ASSUMPTION: src attribute is preceded by and followed by 
        // at least one space
        var p1 = iframedef.indexOf(' src=');
        if ( p1 != -1 )
        {
            var p2 = iframedef.indexOf(' ', p1+1);
            if ( p2 != -1 )
            {
                iframedef = iframedef.substring(0, p1)
                    + ' src="' + src + '"'
                    + iframedef.substring(p2);
            }
        }
        loginbox.innerHTML = iframedef;        

        loginbox.style.display = 'block';
    }

    var loginContainer = document.getElementById('login-container');
    if ( loginContainer != null )
    {
        loginContainer.style.display = 'block';
        fixNavHeight();
    }
}
onPageLoad(maybeDisplayLoginForm);


