/* Memo support. Set gEnableMemos to false if you don't want memos to appear. */

var gEnableMemos = true;
var gMemoWin = false;

/* Extract the base location from the url protocol + server + path */

var gBaseURL;


function getBaseURL()
{
    var url = document.location.pathname;
    gBaseURL = url.substring( 1 );
}
 
getBaseURL();

/* Parse the url to get the external key or purl  */

var gExternalKey = null;
var gPurl = null;

function parseUrl()
{
    var url = new String( document.location );
    var urlPieces = url.split( "?" );

    if ( urlPieces.length > 1 ){
        var urlParams = urlPieces[1].split( "&" );
        for( i=0; i != urlParams.length; i++ ){
            var param = urlParams[ i ];
            var name = param.split( "=" )[0];
            var value = param.split( "=" )[1];
            
            name = name.toLowerCase();
            
            if ( name == "externalkey" ){
                gExternalKey = unescape( value );
                break;
            }

            if ( name == "purl" ){
                gPurl = value;
                break;
            }
        }
    }
}

// Parse the incoming URL BEFORE the page loads!
//
parseUrl();

/* JavaScript functions for connecting the NearSpace Locator(tm) to an html document.  */

var gLastSelection = null;
var gInitialized = false;
var gCanScriptApplet = true;

// Body's onLoad function
//
function initAfterDelay()
{
    setTimeout( "init()", 1000 );
}

// Body's onLoad function
//
function init()
{ 
    /* It turns out that this test fails on some versions 
     * of Java.  Version "1.4.2_06", for example, returns 
     * "undefined" when asked for the "applet.locateAtomByHandle" 
     * function.  So what to do?  Since browsers are increasingly
     * advanced we can assume that browsers support scripting
     * and just ignore this issue for now.  (Note that scripting
     * in IE is still not supported on the Mac.)  
     * Also, when scripting is not available the Locator applet
     * should be run in its own frame so that it can be reloaded
     * without reloading the rest of the page content and this 
     * code does not automatically make provisions for that.
     *ERS-11/16/2005
    var applet = document.getElementById( "Locator" );
    if ( applet == null || (typeof applet.locateAtomByHandle == "undefined" ) )
        gCanScriptApplet = false;
    **/
    gCanScriptApplet = true;
    
    if ( gExternalKey != null )
        locateAtom( gExternalKey );
    else 
    if ( gPurl != null )
        locateAtomByHandle( gPurl );

    gInitialized = true;
}

// Locate by external key
//
function locateAtom( key, rowID )
{
    if ( rowID != null )
        selectRow( rowID, false );
    else
        selectRow( key, false );

    if ( gCanScriptApplet ){
        // Where possible just talk to the locator directly
        document.Locator.locateAtomByExternalKey( unescape( key ) );
    } else {
        // Don't reload during initializtion
        if ( gInitialized == true )
            document.location.href = gBaseURL + "?externalKey=" + key;
    }
}

// Locate a purl in the Locator applet (ignores venue handle)
//
function locatePurl( purl )
{
    var atomHandle;
    var purlParts = purl.split( "." );
    if ( purlParts.length >= 1 )
        atomHandle = purlParts[purlParts.length - 1];
    else 
        atomHandle = purl;
        
    locateAtomByHandle( atomHandle );
}

// Locate an atom handle in the Locator applet
//
function locateAtomByHandle( atomHandle )
{
    if ( gCanScriptApplet ){
        document.Locator.locateAtomByHandle( parseInt( atomHandle ) );
    } else {
        // Don't reload during initializtion
        if ( gInitialized == true )
            document.location.href = gBaseURL + "?purl=" + atomHandle;
    }
}