
var prob_done = new Array(NPROBS);
var prob_correct = new Array(NPROBS);
var Incorrect = 0;
var Correct = 0;
var Started = 0;
var Elapsed = 0;
var saveID=-1;
var i;
for (i=0; i<NPROBS; i++) 
{
	prob_done[i]=0;
	prob_correct[i]=0;

}

function start()
{

var saveID=-1;
document.forms[0].elements[0].focus();
document.forms[0].elements[0].select();
document.forms[0].elements[0].style.backgroundColor = "#FFFF99";
document.forms[0].elements[0].style.borderColor = "#990000";
scroll(0,0);
}

function today() {
var today=new Date()
return today.getMonth()+1+"/"+today.getDate()+"/"+(today.getYear() + 1900)
}

function Set_Cookie( name, value) 
{
document.cookie = name + "=" + escape(value) + '; expires=Thu, 2 Aug 2021 20:47:11 UTC; path=/'
}

// this function gets the cookie , if it exists
function Get_Cookie( name ) {
	
var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if ( ( !start ) &&
( name != document.cookie.substring( 0, name.length ) ) )
{
return null;
}
if ( start == -1 ) return null;
var end = document.cookie.indexOf( ";", len );
if ( end == -1 ) end = document.cookie.length;
return unescape( document.cookie.substring( len, end ) );
}


function timestamp_class(this_current_time, this_start_time, this_end_time, this_time_difference) { 
		this.this_current_time = this_current_time;
		this.this_start_time = this_start_time;
		this.this_end_time = this_end_time;
		this.this_time_difference = this_time_difference;
		this.GetCurrentTime = GetCurrentTime;
		this.StartTiming = StartTiming;
		this.EndTiming = EndTiming;
	}

	//Get current time from date timestamp
	function GetCurrentTime() {
	var my_current_timestamp;
		my_current_timestamp = new Date();		//stamp current date & time
		return my_current_timestamp.getTime();
		}

//Stamp current time as start time and reset display textbox
function StartTiming() 
{
	this.this_start_time = GetCurrentTime();	//stamp current time
	
}

//Stamp current time as stop time, compute elapsed time difference and display in textbox
function EndTiming() 
{
	this.this_end_time = GetCurrentTime();		//stamp current time
	this.this_time_difference = (this.this_end_time - this.this_start_time) / 1000;	//compute elapsed time
	return this.this_time_difference;	//set elapsed time in display box
}

var time_object = new timestamp_class(0, 0, 0, 0);	//create new time object and initialize it
/****************************/

function DHTMLSound(surl) {
  document.getElementById("audio").innerHTML=
    "<embed src='"+surl+"' hidden=true autostart=true loop=false CACHE=”true”>";
}


function sayit(word)
{

   var fn = "sounds/" + word + ".wav";
	DHTMLSound(fn);  

}

function writeit(text,id)
{
   var x
	if (document.getElementById)
	{
		x = document.getElementById(id);
		x.innerHTML = '';
		x.innerHTML = text;
	}
	else if (document.all)
	{
		x = document.all[id];
		x.innerHTML = text;
	}
	else if (document.layers)
	{
		x = document.layers[id];
		x.document.open();
		x.document.write(text);
		x.document.close();
	}
}

function save_game()
{
var Gamescores = "";

if (!(Gamescores=Get_Cookie('Gamescores')))Gamescores = "";
Gamescores= today() + "," + OPERATION + "," + nword + "," + Correct + "," + NPROBS + "," + Elapsed + ";" + Gamescores;

Set_Cookie('Gamescores',Gamescores);
//writeit(Gamescores,"debug");
}

function end_of_game()
{

sayit("goodjob");
save_game();
var html2 = "<img src=\"images/fireworks.gif\" width=\"100\" height=\"200\" />";
var html = "<div class=\"whbg\">Good Job!<br>" + opname + " with ";
html=html + nword;
html = html + "<br>" + Correct + " Correct out of " + NPROBS + "<br>Time: " + Elapsed + " seconds<br><br>";
html = html + "<small><a href='worksheets.php'>New Game</a><br /><a href=\"javascript:showscore('show_score.php','popscore','center','front');\">Show Saved Scores </a></small><br><br>";
html = html + "</div>";
writeit(html,"fw");
writeit("<div name = 'f1' id='f1'></div><div name = 'f2' id='f2'></div> <div name = 'f3' id='f3'></div> <div name = 'f4' id='f4'></div> <div name = 'f5' id='f5'></div> <div name = 'f6' id='f6'></div> <div name = 'f7' id='f7'></div> <div name = 'f8' id='f8'></div> <div name = 'f9' id='f9'></div> <div name = 'f10' id='f10'></div> <div name = 'f11' id='f11'></div> <div name = 'f12' id='f12'></div> <div name = 'f13' id='f13'></div> <div name = 'f14' id='f14'></div> <div name = 'f15' id='f15'></div> <div name = 'f16' id='f16'></div>","divholder");

writeit('<img src=\"images/fireworks.gif\" width=\"100\" height=\"200\" />','f1');
setTimeout("writeit('<img src=\"images/fireworks.gif\" width=\"100\" height=\"200\" />','f2')",1000);
setTimeout("writeit('<img src=\"images/fireworks.gif\" width=\"100\" height=\"200\" />','f3')",2200);
setTimeout("writeit('<img src=\"images/fireworks.gif\" width=\"100\" height=\"200\" />','f4')",3000);
setTimeout("writeit('<img src=\"images/fireworks.gif\" width=\"100\" height=\"200\" />','f5')",4100);
setTimeout("writeit('<img src=\"images/fireworks.gif\" width=\"100\" height=\"200\" />','f6')",5000);
setTimeout("writeit('<img src=\"images/fireworks.gif\" width=\"100\" height=\"200\" />','f7')",6000);
setTimeout("writeit('<img src=\"images/fireworks.gif\" width=\"100\" height=\"200\" />','f8')",7000);
setTimeout("writeit('<img src=\"images/fireworks.gif\" width=\"100\" height=\"200\" />','f9')",11000);
setTimeout("writeit('<img src=\"images/fireworks.gif\" width=\"100\" height=\"200\" />','f10')",9000);
setTimeout("writeit('<img src=\"images/fireworks.gif\" width=\"100\" height=\"200\" />','f11')",10000);
setTimeout("writeit('<img src=\"images/fireworks.gif\" width=\"100\" height=\"200\" />','f12')",9500);
setTimeout("writeit('<img src=\"images/fireworks.gif\" width=\"100\" height=\"200\" />','f13')",8000);
setTimeout("writeit('<img src=\"images/fireworks.gif\" width=\"100\" height=\"200\" />','f14')",5500);
setTimeout("writeit('<img src=\"images/fireworks.gif\" width=\"100\" height=\"200\" />','f15')",4400);
setTimeout("writeit('<img src=\"images/fireworks.gif\" width=\"100\" height=\"200\" />','f16')",3300);


}

function timing()
{
	if (!Started)
	{
		Started = 1;
		time_object.StartTiming()
	}
	Elapsed = time_object.EndTiming();
	Elapsed = Math.round(Elapsed);
	if(Elapsed > 999) Elapsed = 999;
	writeit(Elapsed + " secs","timingbox");
}


function check(id)
{
document.forms[0].elements[id].style.backgroundColor = "#FFFFFF";
document.forms[0].elements[id].style.borderColor = "#7F9DB9";
	saveID=id;
var a = document.forms[0].elements[id].value
if(a != answer[id] ) 
{
	if(!prob_done[id])
	{
		prob_done[id]=1;
		Incorrect++;
	}
	document.forms[0].elements[id].value = ""
	document.forms[0].elements[id].focus();
	document.forms[0].elements[id].select();
	document.forms[0].elements[id].style.backgroundColor = "#FFFF99";
	document.forms[0].elements[id].style.borderColor = "#990000";
	sayit("uhoh");
	saveID--;
}
else 
{
	if(!prob_done[id])
	{
		prob_done[id]=1;
		Correct++;
	}
prob_correct[id]=1;	
if(id < NPROBS-1) 
{
	document.forms[0].elements[(id+1)].focus();
	document.forms[0].elements[(id+1)].select();
		document.forms[0].elements[id+1].style.backgroundColor = "#FFFF99";
document.forms[0].elements[id+1].style.borderColor = "#990000";

}

sayit("correct");
	 	
}

writeit(Correct + " Correct. " + Incorrect + " &nbsp;Missed","totals");

timing();
	var i, done=1;
	for ( i = 0; i < NPROBS; i++)
	{
	  if(!prob_correct[i])
	  {
	  	done=0;
		break;
	  }
	}
	if(done) 
	{
		end_of_game();
		
	}

}//end check

function check_no_id()
{
	 if(saveID < -1 || saveID > NPROBS) saveID=-1;
  check(saveID+1); 	
}

var showscoreWindow=null;
function showscore(mypage,myname,pos,infocus){
var w=600;
var h=480;
if (pos == 'random')
{LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
else
{LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
settings='width='+ w + ',height='+ h + ',top=' + TopPosition + ',left=' + LeftPosition + ',scrollbars=yes,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes';showscoreWindow=window.open('',myname,settings);
if(infocus=='front'){showscoreWindow.focus();showscoreWindow.location=mypage;}
if(infocus=='back'){showscoreWindow.blur();showscoreWindow.location=mypage;showscoreWindow.blur();}

}

