//	eKLIPS Track Javascript

var DisplayAll = true;

function DisplayCompetitorTaskData( CompetitorIndex )
{
	//	Display Competitor's number and name (or whatever is in Competitor array) in current competitor text box.

//	alert( "DisplayCompetitorTaskData" );

	var textobj = document.getElementById( "currentcompetitor" );

	if ( textobj != null )
	{
		textobj.value = Competitor[ CompetitorIndex ];
	} else	{
			alert( "textobj == null!" );
		}

	//	Make competitor task limits visible.

	var svgdoc = document.trackdata.getSVGDocument();

	if ( svgdoc != null )
	{
		var ElementName = "competitortaskdata" + CompetitorIndex;

		var svgobj = svgdoc.getElementById( ElementName);

		if ( svgobj != null )
		{
			var svgstyle = svgobj.getStyle();
			if ( svgstyle != null )
			{
				svgstyle.setProperty('display', 'inherit');
			} else	{
					alert( "svgstyle == null" );
				}
		} else	{
				alert( "svgobj == null (" + ElementName + ")" );
			}
	} else	{
			alert( "svgdoc == null!" );
		}
}

function HideCompetitorTaskData( CompetitorIndex )
{
	//	Clear current competitor text box.

	var textobj = document.getElementById( "currentcompetitor" );

	if ( textobj != null )
	{
		textobj.value = "";
	} else	{
			alert( "textobj == null!" );
		}

	if ( DisplayAll == false )
	{
		//	We're not displaying all competitors at once, so 
		//	do not hide competitor task data.

		return;
	}

	//	Make competitor task limits invisible.

	var svgdoc = document.trackdata.getSVGDocument();

	var ElementName = "competitortaskdata" + CompetitorIndex;

	var svgobj = svgdoc.getElementById( ElementName);

	if ( svgobj != null )
	{
		var svgstyle = svgobj.getStyle();
		if ( svgstyle != null )
		{
			svgstyle.setProperty('display', 'none');
		} else	{
				alert( "svgstyle == null" );
			}
	} else	{
			alert( "svgobj == null (" + ElementName + ")" );
		}
}

function SelectCompetitor( CompetitorListIndex, DisplayTaskData )
{
	//	Make competitor visible.

	if ( CompetitorListIndex <= 1 )
	{
		//	"All" or "None", so nothing to do!

		return;
	}

	var svgdoc = document.trackdata.getSVGDocument();

	var CompetitorIndex = CompetitorListIndex - 2;	//	Account for "All" and "None"

	var ElementName = "competitor" + CompetitorIndex;
//	alert( ElementName );

	var svgobj = svgdoc.getElementById( ElementName );

	if ( svgobj != null )
	{
		var svgstyle = svgobj.getStyle();
		if ( svgstyle != null )
		{
//			alert( document.selectcompetitorsform.competitors.options[ CompetitorListIndex ].selected );

			if ( document.selectcompetitorsform.competitors.options[ CompetitorListIndex ].selected )
			{
//				alert( "Setting display=inherit" );

				svgstyle.setProperty('display', 'inherit');
			} else	{
//					alert( "Setting display=none" );

					svgstyle.setProperty('display', 'none');
				}

			if ( DisplayTaskData == true )
			{
				DisplayCompetitorTaskData( CompetitorIndex );
			} else	{
					HideCompetitorTaskData( CompetitorIndex );
				}
		} else	{
				alert( "svgstyle == null" );
			}
	} else	{
			alert( "svgobj == null (" + ElementName + ")" );
		}
}

function SelectAll()
{
	DisplayAll = true;

	for ( var c = 0; c < document.selectcompetitorsform.competitors.length; c++ )
	{
		document.selectcompetitorsform.competitors.options[ c ].selected = 1;

		SelectCompetitor( c, false );
	}
	
	document.selectcompetitorsform.competitors.options[ 1 ].selected = 0;	//	Don't select "None".
	document.selectcompetitorsform.competitors.options[ 0 ].selected = 1;	//	So "All" is displayed in drop down list.
}

function SelectNone()
{
	DisplayAll = false;

	for ( var c = 0; c < document.selectcompetitorsform.competitors.length; c++ )
	{
		document.selectcompetitorsform.competitors.options[ c ].selected = 0;
//		alert( "Should be false: " + document.selectcompetitorsform.competitors.options[ c ].selected );

		SelectCompetitor( c, false );
	}
	
	document.selectcompetitorsform.competitors.options[ 1 ].selected = 1;	//	So "None" is displayed in drop down list.
}

function SelectCompetitors()
{
	var SelectedCompetitorIndex = document.selectcompetitorsform.competitors.selectedIndex;

	switch( SelectedCompetitorIndex )
	{
		case 0:

			SelectAll();
			break;

		case 1:

			SelectNone();
			break;

		default:

			SelectCompetitor( SelectedCompetitorIndex, true );
	}
}

/*
 * This function turns on/off the grid, which is actually two grids.
 */

function hilite_grid()
{
	hilite_elem( document.hilite_form.grid, 'westgridlines' );
	hilite_elem( document.hilite_form.grid, 'eastgridlines' );
}

/*
 * This function turns on/off the grid numbers, which is actually two grids.
 */

function hilite_gridnumbers()
{
	hilite_elem( document.hilite_form.gridnumbers, 'westgridnumbers' );
	hilite_elem( document.hilite_form.gridnumbers, 'eastgridnumbers' );
}

/*
 * This function turns on/off various layers of the SVG picture.
 * It is called when the user clicks on any of the checkboxes in
 * the form below.
 *
 * Input Parameters:
 *   checkbox     - Form object (checkbox) that was clicked on.
 *   element_name - SVG element name that should be made visible/
 *                  invisible.
 */

function hilite_elem (checkbox, element_name)
{
	var svgobj;
	var svgstyle;
	var svgdoc = document.trackdata.getSVGDocument();

	// For each element, get the element's style object, then set
	// its visibility according to the state of the checkbox.

	svgobj = svgdoc.getElementById(element_name);

	if ( svgobj == null )
	{
		return;
	}

	svgstyle = svgobj.getStyle();
	if (!checkbox.checked)
	{
		// Hide layer.

		svgstyle.setProperty('display', 'none');
	} else	{
			// SHow layer.

			svgstyle.setProperty('display', 'inherit');
		}
}

function CleanUp()
{
}

/*
 * This function initializes the display of layers of the SVG picture.
 *
 * Input Parameters:
 */

function Initialize()
{
//	Initiaize Layers.

	hilite_elem( document.hilite_form.distancelimits, 'distancelimits' );
	hilite_elem( document.hilite_form.judgedeclaredgoals, 'judgedeclaredgoals' );
	hilite_elem( document.hilite_form.judgedeclaredtargets, 'judgedeclaredtargets' );
	hilite_elem( document.hilite_form.clps, 'clps' );
	hilite_grid();
	hilite_gridnumbers();

	//	Initialize Competitor Selection.

	SelectAll();

	window.defaultStatus = "Welcome to the eKLIPS Results System Track Display!";
}
