
// This is the JavaScript code that does the numerical computation for the stick problem
// Copyright HTF, MIT October, 1998
// Written by Michael Kashambuzi

// declare variable;
var runs;
var x1, x2;
var min, max;
var successes=0;
var delay = 500;
var animationHandle;
var i;
var j=1;
var System = java.lang.System;

// function that starts the simulation
function begin()
{
	runs = document.input_output.runs.value;

	if (runs <= 25)
	{
		delay = 500;
		animationHandle = setTimeout("calculate(runs)", delay);	
	}

	else
	{
		delay = 100;
		animationHandle = setTimeout("calculate(runs)", delay);	
	}
}

function calculate()
{
	document.input_output.j.value = j;

	x1 = Math.random();
	x2 = Math.random();

	min = Math.min(x1, x2);
	max = Math.max(x1, x2);

	//document.input_output.total.value = j;

	System.out.println("min = " + min);
	System.out.println("max = " + max);
	System.out.println("max - min = " + (max-min) );
	System.out.println("1 - max = " + (1-max) );
	System.out.println();

	
	if ( (min < 0.5) && ( (max-min) < 0.5 ) && ( (1 - max) < 0.5 ) )
	{
		successes++;

		document.input_output.x1Good.value = x1;
		document.input_output.x2Good.value = x2;
		document.graph.appendGoodPoints( x1, x2, eval(runs) );
		document.input_output.successes.value = successes + "/" + j + " = " + (successes/j);
		//document.input_output.fraction.value = (successes/j);
  	}

	else
	{
		document.input_output.x1Bad.value = x1;
		document.input_output.x2Bad.value = x2;
		document.graph.appendBadPoints( x1, x2, eval(runs) );
		document.input_output.successes.value = successes + "/" + j + " = " + (successes/j);
	}

	j++;

	if ( eval(runs) > 25 )
	{
		if ( j <= eval(runs) )
		{
			calculate();
		}

		else
		{
			document.graph.plotGoodPoints();
			document.graph.plotBadPoints();
		}
	}
	
	else 
	{
		if ( j <= eval(runs) )
		{
			animationHandle = setTimeout("calculate()", delay);	
		}
	}
}