
var testList;
var xmlhttp;

function acquireTestSources( ) {
    var url = "/cgi-bin/genTest.pl";
    xmlhttp=null;
    if ( window.XMLHttpRequest ) {
        xmlhttp=new XMLHttpRequest( );
    }
    if ( xmlhttp != null ) {
        xmlhttp.onreadystatechange = readList;
        xmlhttp.open( "GET", url, true );
        xmlhttp.send( null );
    }
    else {
        alert("Your browser does not support XMLHTTP.");
    }
}

function getSelText( ) {
    var txt = '';

    /*
    ** return the selected text from the screen if any
    */

    if ( window.getSelection ) {
        txt = window.getSelection();
    }
    else if ( document.getSelection ) {
        txt = document.getSelection();
    }
    else if ( document.selection ) {
        txt = document.selection.createRange( ).text;
    }
    return( txt );
}

function genTest( ) {
    var testName = 'gtest' + ( new Date( ).getTime( ));
    var time = new Date( ).getTime( );
    var txt = "";
    var url = document.location.pathname + document.location.search + document.location.hash;

    /*
    ** generate the test parameters and past
    ** those to a function that will post to
    ** a servlet
    */

    txt = getSelText( );
    if ( txt == undefined || txt == '' || txt.length <= 0 ) {
        alert( "please select some text before pressing the generate test button" );
        return;
    }
    postTest( url, txt, testName  );
//    window.location.reload( false );
}

function getUrlParams( ) {
    var qs = document.location.search;
    var hash = document.location.hash;

    /*
    ** return the url parameters as a hash table
    */

    if (!qs && hash) {
        qs = hash.substring(1);
    }
    if (qs && hash) {
        qs += '&'+hash.substring(1);
    }

    var params = qs.substring(1).split('&');
    var paramsObj = {};
    var id = null;
    for ( var i=0; i<params.length; i++ ) {
        var kv = params[ i ].split( "=" );
        if ( kv[ 0 ] == 'id' ) {
            id = kv[ 1 ];
        }
        paramsObj[ kv[ 0 ]] = kv[ 1 ];
    }
    return paramsObj;
}

function hasAutoTestFlag( ) {
    var parms = getUrlParams( );

    /*
    ** this is a boolean function that return
    ** whether or not the curator's test flag 
    ** was on the url
    */

    if ( parms.t && parms.t == 1 ) {
        return( 1 );
    }
    return( 0 );
}

function postTest( url, txt, testName ) {
    var form = "<form method=post action=/cgi-bin/genTest.pl>\n";
    var field;

    /*
    ** popup a little form to select the testfile then
    ** post the params to a servlet that will generate
    ** the test descriptor and insert it into the test 
    ** template
    */

    form += "<input type=hidden name=url value='" + url + "'>\n";
    form += "<input type=hidden name=txt value='" + txt + "'>\n";
    form += "<input type=hidden name=testName value='" + testName + "'>\n";
    for( t in testList ) {
        var checked = '';

        if ( testList[ t ].length <= 0 ) {
            continue;
        }
        if ( testList[ t ].indexOf( 'auto' ) >= 0 ) {
            checked = 'checked';
        }
        form += "<input type=radio name=src value='" + testList[ t ] + "' " + checked + ">" + 
            testList[ t ].slice( testList[ t ].lastIndexOf( "/" ) + 1 )  + "<br>\n";
    }
    form += "<input type=submit name=submit onclick='window.close( )'><br>\n";
    form += "</form><br>\n";
    display = window.open( '', 'newwin', 'menubar=0,location=no,status=no,directories=no,toolbar=no,scrollbars=yes,height=110,width=190' );
    display.document.write( form );
    display.document.close( );
}

function readList( ) {
    if ( xmlhttp.readyState == 4 ) {
        if ( xmlhttp.status == 200 ) {
            testList = xmlhttp.responseText.split( "\n" );
        }
        else {
            alert( "Problem retrieving data" );
        }
    }
}

if ( hasAutoTestFlag( )) {    
    document.getElementById( 'autoTestButton' ).style.display = 'inline';
    acquireTestSources( );
}
else {
    document.getElementById( 'autoTestButton' ).style.display = 'none';
}


