/*
 * hawkeye.js (Basic Version)
 * -----------------------------------------------------------------------------
 * This is an unpublished work the copyright in which vests in Roke Manor
 * Research Ltd. All rights reserved.
 *
 * The information contained herein is the property of Roke Manor Research
 * Ltd and is supplied without liability for errors or omissions. No part
 * may be reproduced, disclosed or used except as authorised by contract
 * or other written permission. The copyright and the foregoing restriction
 * on reproduction, disclosure and use extend to all media in which the
 * information may be embodied.
 * -----------------------------------------------------------------------------
 *
 * v1.0 created 4/7/2001 afj
 * v1.1 modified platformTest 26/7/2001 jpt
 */

// Define some preset values
// These values MUST remain constant

	var comboName = 0;
	var comboTitle = 1;
	var comboDefaultText = 2;
    var comboRef = 3;

    var numberOfOptionsDisplayed = 8;
	var stringDelimiter1 = "&";
    var applet;

// Data Segment ------------------------------------------------------------------------------------------------------------

// Build an array of the combo boxes

    // Create the array
    var aCombos = new Array();

	// Set up Combo Data
/*
		var iMatch = aCombos.length;
	    // Save combo data into array
		aCombos[iMatch] = new Array( "match", "Match", "------------ Please Wait -------------" );
*/
		var iInnings = aCombos.length;
		// Save combo data into array
		aCombos[iInnings] = new Array( "innings", "Innings", "------------ Please Wait -------------" );

		var iBowler = aCombos.length;
		// Save combo data into array
		aCombos[iBowler] = new Array( "bowler", "Bowler", "------------ Please Wait -------------" );

		var iBatsman = aCombos.length;
		// Save combo data into array
		aCombos[iBatsman] = new Array( "batsman", "Batsman", "------------ Please Wait -------------" );

		var iPlugin = aCombos.length;
		// Save combo data into array
		aCombos[iPlugin] = new Array( "plugin", "Analysis Mode ", "------------ Please Wait -------------" );
/*
		var iRuns = aCombos.length;
		// Save combo data into array
		aCombos[iRuns] = new Array( "runs", "Number of Runs", "---- Please Wait ----" );

		var iOutcome = aCombos.length;
		// Save combo data into array
		aCombos[iOutcome] = new Array( "outcome", "Wicket Outcome", "---- Please Wait ----" );

		var iBathand = aCombos.length;
		// Save combo data into array
		aCombos[iBathand] = new Array( "bathand", "Batting Hand", "---- Please Wait ----" );

		var iBowlteam = aCombos.length;
		// Save combo data into array
		aCombos[iBowlteam] = new Array( "bowlteam", "Bowling Team", "---- Please Wait ----" );

		var iBatteam = aCombos.length;
		// Save combo data into array
		aCombos[iBatteam] = new Array( "batteam", "Batting Team", "---- Please Wait ----" );

		var iEnd = aCombos.length;
		// Save combo data into array
		aCombos[iEnd] = new Array( "end", "Bowling End", "---- Please Wait ----" );

		var iSeries = aCombos.length;
		// Save combo data into array
		aCombos[iSeries] = new Array( "series", "Series", "---- Please Wait ----" );
*/

// Page layout scheme

    var aPageLayout = new Array(

        "White Line",

        "New Row",        "ComboBox", iInnings, "50%",           "ComboBox", iBowler, "50%",                   "End Row",

        "White Line",

        "New Row",        "ComboBox", iBatsman, "50%",           "ComboBox", iPlugin, "50%",                   "End Row",

        "White Line",

        "The End");

// Code Segment ------------------------------------------------------------------------------------------------------------

	// Build the table containing all the combo boxes
    function buildTable()
    {
        var moreItems = true;
        var i = 0;

        while ( moreItems && ( i < aPageLayout.length ) )
        {
            //alert( "i= " + i + ",  aPageLayout[i]= " + aPageLayout[i] );

            // Find the item type
            if ( aPageLayout[i] == "New Row" )
            {
                // Start a new row in the table
                buildNewRow();

                i++;
            }
            else if ( aPageLayout[i] == "End Row" )
            {
                // Start a new row in the table
                buildEndRow();

                i++;
            }
            else if ( aPageLayout[i] == "ComboBox" )
            {
                // Build a new combo box
                buildComboStart( aPageLayout[ i + 2 ] );
                buildComboTitle( aPageLayout[ i + 1 ] );
                buildCombo( aPageLayout[ i + 1 ] );
                buildComboEnd();

                i = i + 3;
            }
            else if ( aPageLayout[i] == "White Line" )
            {
                // Draw a White Line across the table
                buildWhiteLine( aPageLayout[ i + 1 ] );

                i++;
            }
            else if ( aPageLayout[i] == "The End" )
            {
                // No more items - end of page layout
                moreItems = false;
            }
            else
            {
                // Well something is wrong somewhere
                moreItems = false;

                // Write an error msg
                document.writeln( '<b>There is an error in the page layout array<b>' );
            }

        }
    }

	// Write out the start of a row
 	function buildNewRow()
	{
        document.writeln( '<tr>' );
        document.writeln( '<td width="100%" >' );
        document.writeln( '<table cellspacing="0" cellpadding="0" bgcolor="#60A438" border="0" width="524" > ' );
        document.writeln( '<tr>' );
        document.writeln( '<td>' );
        document.writeln( '&nbsp;&nbsp;' );
        document.writeln( '</td>' );
	}

	// Write out the end of a row
 	function buildEndRow()
	{
        document.writeln( '</tr>' );
        document.writeln( '</table>' );
        document.writeln( '</td>' );
        document.writeln( '</tr>' );
	}

	// Write out the start of a row
 	function buildComboStart( itemWidth )
	{
        document.writeln( '<td width=' + itemWidth + '> ' );
        document.writeln( '<table cellspacing="0" cellpadding="0" bgcolor="#60A438" border="0" >' );
	}

	// Write out the end of a row
 	function buildComboEnd()
	{
        document.writeln( '</table>' );
        document.writeln( '</td>' );
	}

	// Draw the titles for the combo boxes
 	function buildComboTitle( combo )
	{
        document.writeln( '<tr>' );
        document.writeln( '<th align="left" valign="bottom" >' );
        document.writeln( aCombos[combo][comboTitle] );
        document.writeln( '</th>' );
        document.writeln( '</tr>' );
	}

	// Build the filter combo boxes
	function buildCombo( combo )
	{
        document.writeln( '<tr>' );
        document.writeln( '<td valign="bottom" " >' );
        document.writeln( '<font size="2.5">' );
 		document.writeln( '<select name="' + aCombos[combo][comboName] + '" size="1" onchange="setCombo(' + combo + ')" style="font-size: 9" disabled>' );

        for( var i = 0; i < numberOfOptionsDisplayed; i++)
        {
            document.writeln( '<option value="' + i + '" >' + aCombos[combo][comboDefaultText] + '</option>' );
        }

        document.writeln( '</select>' );
        document.writeln( '</font>' );
        document.writeln( '</td>' );
        document.writeln( '</tr>' );
	}

	// Draw a line across the table
    function buildWhiteLine()
    {
        document.writeln( '<tr>' );
        document.writeln( '<td  width="100%" ><img src="images/empty.gif" width="1" height="5"></td>' );
        document.writeln( '</tr>' );
        document.writeln( '<tr>' );
        document.writeln( '<td  width="100%" bgcolor="#FFFFFF" ><img src="images/empty.gif" width="2" height="2" border="0"></td>' );
        document.writeln( '</tr>' );
    }

	// Enable the Combo Boxes
    function enableComboBoxes()
    {
        for ( var i = 0; i < aCombos.length; i++ )
        {
            aCombos[i][comboRef].disabled = false;
        }
    }

	// Set up the references to all the combo boxes
    function getComboRefs()
    {
        var i = 0;

        while ( document.HawkEye.elements[i] )
        {
            for ( var j = 0; j < aCombos.length; j++ )
            {

                if ( document.HawkEye.elements[i].name == aCombos[j][comboName] )
                {
                    aCombos[j][comboRef] = document.HawkEye.elements[i];
                }
            }

            i++;
        }
    }

	// Get the all the options for all the combo boxes
    function getComboData()
    {
		var sOptionsData;
		var aOptionsData;
		var newText;
		var optionName;

        for ( var i = 0; i < aCombos.length; i++ )
        {
            // Clear any contents
            aCombos[i][comboRef].options.length = 0;

            // Call the applet
        	sOptionsData = new String( applet.getComboItems( aCombos[i][comboName] ) );
        	aOptionsData = sOptionsData.split( stringDelimiter1 );

			// Load options into combo
			for( var j = 1 ; j < aOptionsData.length ; j++ )
	        {
	            newText = aOptionsData[j];
	            optionName = new Option(newText, newText, false, false);

	            aCombos[i][comboRef].options[ aCombos[i][comboRef].options.length ] = optionName;
	        }

	        aCombos[i][comboRef].selectedIndex = aOptionsData[0];
		}
    }

	// Pass the users selction back to the applet
    function setCombo( comboNumber )
    {
		var sValue = aCombos[comboNumber][comboRef].options[ aCombos[comboNumber][comboRef].selectedIndex ].value;

		// Call the applet
		applet.onChange( aCombos[comboNumber][comboName], sValue );

		// Get the new options for all the combos
		getComboData();

    }

	function init()
	{
        // Set up a short cut to the applet
		applet = document.applets["HawkEyeApplet"].getAdapter();

         // Get the refs to all the combos
        getComboRefs();

        var inUse = "";

        for ( var i = 0; i < aCombos.length; i++)
        {
            inUse += aCombos[i][comboName] + stringDelimiter1;
        }

        //alert( "inUse= " + inUse );

        // Tell the applet which combos are in use
        applet.setComboOrder( inUse );

        // Load all the combos with data
        getComboData();

        // Enable all the combos
        enableComboBoxes();
	}

    function platformTest()
    {
        if ( !is_win32 )
        {
            document.write("Sorry, your platfrom is not support, <a href='requirements.html' >please check our requirements page.</a>");
            document.location = "requirements.html"
        }
    }

    function rstclr()
    {
		// Call the applet
		applet.onChange( "rstclr", "rstclr" );

        // Load all the combos with data
        getComboData();
    }

