// Programmer: Aaron Klapheck
// Program Date: 3-Apr-08
// 


NS6 = (document.getElementById&&!document.all)
IE = (document.all)
NS = (navigator.appName=="Netscape" && navigator.appVersion.charAt(0)=="4")





// validate() makes sure that the number a person enters is 
// 

function validate()
{
	var bill = window.document.sine_form.cycles_text.value
	if (bill == 1)
	{
		position()
	}
	else if (bill == 2)
	{
		position()
	}
	else if (bill == 3)
	{
		position()
	}
	else if (bill == 4)
	{
		position()
	}
	else
	{
		alert("Enter an integer less than 5 and greater than 0")
	}
}





// position() starts the small square at the same location for each press of the "Start Graph" button
// 

var top_distance
function position() 
{
	var the_div_axis
	var the_div_sine
	var image_height = document.axis_image.height;
	if(IE)
	{
		the_div_axis = document.all.axisDiv.style;
		the_div_sine = document.all.sineDotDiv.style;
	}
	if(NS6||NS)
	{
		the_div_axis = document.axisDiv;
		the_div_sine = document.sineDotDiv;
	}
	the_div_sine.left = 360;
	top_distance = (parseInt(the_div_axis.top) + image_height/2);
	the_div_sine.top = top_distance + 'px'
	
	sineValuesField()
}



// sineValuesField() calculates 144 values of sine for each 2pi radians in length.
// 

var large_rounded_sines = new Array(); // used to find y-values of sine wave
var segments = 144	// this defines the number of pices we will be breaking the sign wave into.
function sineValuesField() 
{
	var cycles = window.document.sine_form.cycles_text.value // cycles = cycles of 2pi we will be graphing the sine wave.
	var sines = new Array();
	segments = 144*cycles;
	for(sine_input = 0; sine_input <= segments*cycles; sine_input++)
	{
		sines[sine_input] = Math.sin(cycles*2*Math.PI - sine_input*(cycles*2*Math.PI)/segments)
		large_rounded_sines[sine_input] = Math.round(100*sines[sine_input]) 
	}
	index = 0
	
	doError("lenght of large_rounded_sines = " + large_rounded_sines.length); 
	doError("segments = " + segments);
	
	sineWave()
}


// sineWave() moves the small square to the specified x and y values. Responsible for animation.
//

var the_timeout
var index = 0 // used to find x-values of sine wave
var speed = 20
function sineWave()
{
	var the_div
	if(IE)
	{
		the_div = document.all.sineDotDiv.style;
	}
	if(NS6||NS)
	{
		the_div = document.sineDotDiv;
	}
	
	index++ 
	if(index >= segments)
	{
		index = 0
	}
	the_div.left = 360 + index
	the_div.top = top_distance + large_rounded_sines[index]
	
	the_timeout = setTimeout("sineWave();", speed)
}
	
	

function doError(the_message) 
{ 
	
	if (debug == "show") 
	{ 
        window.document.the_form.the_text.value += the_message + "\n"; 
    } 
}
	
	
	


