var undefined;

window.onweb = false
window.oncd = true
var url = window.location.href
url = url.toLowerCase()
if ( url && url.match( /http/ ) )
    {
    window.oncd = false
    }
if ( url && ( -1 != url.search( /spiritandtruth.org/ ) ) )
    {
    window.onweb = true
    }

var appName = navigator.appName;
var split = appName.split( ' ' );
var isMicrosoft = ( split[ 0 ] == 'Microsoft' )

// Carry across text size setting if other tools have set it
// as a navigator variable.
var textSize = readCookie( 'textSize' )
if ( textSize ) wlx.wlx_textSize = textSize
if (! wlx.wlx_textSize ) 
    {
    wlx.wlx_textSize = navigator.wlx_textSize
    }
if (! wlx.wlx_textSize ) 
    {
    wlx.wlx_textSize = 'medium'
    }

// Carry across translation setting if other tools have set it
// as a navigator variable.
var translation = readCookie( 'translation' )
if ( translation ) wlx.wlx_translation = translation
if (! wlx.wlx_translation ) 
    {
    wlx.wlx_translation = navigator.translation
    }
if (! wlx.wlx_translation ) 
    {
    wlx.wlx_translation = 'NASB Bible'
    }
//alert( 'wlx.wlx_translation ' + wlx.wlx_translation )

//alert( 'wlx ' + parent )

// Perform a search.
doSearch = function()
    {
    // If we did a previous lookup.
    if ( wlx.address ) 
        {
        // Update includes address.
        updateForm( wlx.address )
        }
    else
        {
        // Update translation only.
        updateForm()
        }

    // Are we searching for a location?
    var search = parent.location.search
    if ( ! search )
        {
        search = parent.location.hash
        }
    else
        {

        search = decodeURIComponent( search )
        search = search.substring( 1, search.length )
        search = search.replace( /\+/g, " " );

        // Decode parts.
        var textSize = getSearchComponent( search, "textSize" );
        if ( textSize != undefined )
            {
            wlx.wlx_textSize = textSize;
            }
        var topic = getSearchComponent( search, "topic" )
        if ( topic != undefined )
            {
            search = topic;
            }

        // Only search once (parent window retains search
        // so we only want to do it the first time we are started).
        //alert( 'wlx.searched ' + wlx.searched );
        if ( ! wlx.searched ) 
            {
            wlx.searched = true
            goTopicOrAddress( search )
            }
        }
    }

getSearchComponent = function( search, keyword )
    {
    // Decode parts.
    var parts = search.split( '&' );
    for ( var i = 0 ; i < parts.length ; i++ ) 
        {
        var halves = parts[i].split('=');
        if (1 < halves.length)
            {
            if ( keyword == halves[0] )
                {
                //alert( keyword + ' = ' + halves[1]);
                return halves[1];
                }
            }
        }
    return undefined
    }

goX = function( url )
    {
    wlx.address = updateForm( url )
    lookupX( url )
    }

// Read a control from our form.
controlFromName = function ( controlName )
    {

    var doc = window.document
    //alert( 'controlFromName doc ' + doc )
    var form = doc[ 'nav' ]
    //alert( 'controlFromName ' + form )
    var control = form[ controlName ]
    return control
    }

// Read the value of a specified control.
readValue = function ( controlName )
    {
    var control = controlFromName( controlName )
    var value = control.value
    return value
    }

// Read the selection of a specified control.
readSelection = function ( controlName )
    {
    var control = controlFromName( controlName )
    var index = control.selectedIndex
    var opt = control.options[ index ]
    var sel = opt.text
    return sel
    }

// Update the value of a control.
updateValue = function( controlName, text )
    {
    control = controlFromName( controlName )
    if ( control !== undefined && text )
        {
        control.value = text
        }
    }

// Update the slide selector control.
updateSelection = function( controlName, text )
    {
    control = controlFromName( controlName )
    if ( control !== undefined )
        {
        var options = control.options;
        if ( 
            options !== undefined && 
            options.length !== undefined &&
            options.length != '' 
            )
            {
            for ( var i = 0 ; i < options.length ; ++i )
                {
                if ( options[ i ].text == text )
                    {
                    control.selectedIndex = i;
                    return;
                    }
                }
            }
        }
    }

onTextSize = function( selector )
    {
    var oldSize = wlx.wlx_textSize
    wlx.wlx_textSize = readSelection ( 'textsize' )
    //alert( oldSize + ' ' + wlx.wlx_textSize )
    if ( oldSize != wlx.wlx_textSize )
        {
        saveCookies()
        var pane = window
        // Reload my sibling frame and myself.
        var sibling = pane.parent.bible
        if ( sibling ) {
            sibling.location.reload( true );
        }
        pane.location.reload( true )
        }

    }

onTranslation = function( selector )
    {
    var translationNew = readSelection ('translation' )
    var translationOld = wlx.wlx_translationOld
    //alert ( 'onTranslation ' + translationOld + ' -> ' + translationNew )

    //wlx.wlx_translationOld = wlx.wlx_translation
    wlx.wlx_translation = translationNew
    wlx.wlx_translationOld = wlx.wlx_translation

    // If translation is changing.
    if ( translationOld != translationNew )
        {
        saveCookies()

        // Disable automatic lookup on change in translation.
        if (false) {

            // New way
            if (1)
                {
                onGoAddress()
                }
            // Old way
            else
               { 
                //alert ( 'onTranslation changed ' + translationNew )
                var pane = window
                if ( parent.bible )
                    {
                    pane = parent.bible
                    var url = pathFromTitle( translationNew )
                    pane.location.href = url
                    }
                }
            }
        }
    }

goTopicOrAddress = function( topicOrAddress )
    {
    //alert( 'goTopicOrAddress ' + topicOrAddress )
    var testAddr = new Address( topicOrAddress )
    var isAddr = testAddr.isValid()
    if ( !isAddr )
        {
        if ( window.oncd )
            {
            alert( 'Cannot search for topics from cdrom: ' + topicOrAddress )
            return
            }
        if ( -1 == topicOrAddress.indexOf( 'search.php') )
            {
            if ( topicOrAddress != wlx.address )
                {
                wlx.address = updateForm( topicOrAddress )
                topicOrAddress = './search.php?topic=' + topicOrAddress
                lookupX( topicOrAddress, false )
                }
            }
        }
    else
        {
        goX( topicOrAddress )
        }
    }

ignoreAction = function()
    {
    return false;
    }

onSubmit = function()
    {
    var topic = readValue( 'topic' )
    goTopicOrAddress( topic )
    return false
    }

onGoAddress = function( theEvent )
    {
    //alert( 'onGoAddress entry' )
    if ( theEvent != undefined && theEvent.type == 'keypress' )
        {
        if ( ! isMicrosoft )
            {
            return true;
            }
        if ( theEvent.keyCode != 13 && theEvent.keyCode != 9 )
            {
            return true;
            }
        }
    var book = readSelection( 'book' )
    var chapter = readSelection( 'chapter' )
    var verse = readSelection( 'verse' )
    var address = book + ' ' + chapter + ':' + verse
    goX( address )
    }

onStudy = function( theEvent )
    {
    //alert( 'onStudy entry' )
    if ( theEvent != undefined && theEvent.type == 'keypress' )
        {
        if ( ! isMicrosoft )
            {
            return true;
            }
        if ( theEvent.keyCode != 13 && theEvent.keyCode != 9 )
            {
            return true;
            }
        }
    var book = readSelection( 'book' )
    var chapter = readSelection( 'chapter' )
    var verse = readSelection( 'verse' )
    var address = book + ' ' + chapter + ':' + verse
    goX( address )
    }

onTopicEntry = function( theEvent )
    {
    if ( theEvent != undefined && theEvent.type == 'keypress' )
        {
        if ( ! isMicrosoft )
            {
            return true;
            }
        if ( theEvent.keyCode != 13 && theEvent.keyCode != 9 )
            {
            return true;
            }
        }

    onSubmit()
    return false
    }

updateForm = function( search )
    {
    if ( search )
        {
        var address = new Address( search )
        if ( address.isValid () )
            {
            updateSelection( 'book', address.book() )
            updateSelection( 'chapter', address.chapter() )
            updateSelection( 'verse', address.verse() )
            search = address.human()
            }
        updateValue( 'topic', search )
        }
    if ( wlx.wlx_translation )
        {
        updateSelection( 'translation', wlx.wlx_translation )
        }
    return search
    }
