/***************************************************************************

	Author 			:Charles B. Cossé 
	
	Email			:ccosse@asymptopia.org
	
	Website			:http://www.asymptopia.org
	
	Copyright		:(C) 2004-2005 Asymptopia Software.
	
 ***************************************************************************/
						//file:mv04.js
/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version. (Please note that if you use this *
 *   code you must give credit by including the Author and Copyright       *
 *   info at the top of this file).                                        *
 ***************************************************************************/
function report(){
	
	t_total=0.;
	for(nidx=1;nidx<N-2;nidx++){
		t_total+=xcells[n1][nidx].VALUE;
	}
	if(xcells[n1][N-2].lastChild)xcells[n1][N-2].removeChild(xcells[n1][N-2].lastChild);
	xcells[n1][N-2].appendChild(document.createTextNode(t_total.toFixed(2).toString()));
	xcells[n1][N-2].VALUE=t_total.toFixed(2);
	//if(n1==M-2){
	if(1){
		for(nidx=1;nidx<N-2;nidx++){
			c_total=0.
			for(midx=1;midx<M-1;midx++){
				c_total+=xcells[midx][nidx].VALUE;
			}
			if(xcells[M-1][nidx].lastChild)xcells[M-1][nidx].removeChild(xcells[M-1][nidx].lastChild);
			var avg=c_total/(M-2);
			xcells[M-1][nidx].appendChild(document.createTextNode(avg.toFixed(2).toString()));
			xcells[M-1][nidx].VALUE=avg.toFixed(2);
			//alert(avg+" "+avg.toFixed(2));
		}
	}
	
	document.body.removeChild(div);
	reportTable=document.createElement("table");
	reportTable.align="center";
	reportTable.cellPadding="0";
	reportTable.cellSpacing="0";
	reportTable.style.backgroundColor=COLOR_DEFAULTBG;
	reportTable.style.color=COLOR_DEFAULTFG;
	//reportTable.width="900px";
	xr0=reportTable.insertRow(0);
	xc00=xr0.insertCell(0);
	xc00.style.fontSize=FONTSIZE;
	
	if(passed_this_level){
		xc00.style.color=COLOR_CORRECT;
		msg="Congratulations!";
		btncolor=COLOR_CORRECT;
	}
	else{
		xc00.style.color=COLOR_WRONG;
		msg="Repeat!";
		btncolor=COLOR_WRONG;
	}
	
	var total=problems.length;
	var correct=0;
	for(var i=0;i<problems.length;i++){
		if(problems[i][1]==problems[i][2])correct=correct+1;
	}
	msg=msg+" "+correct+"/"+total;
	
	btn=document.createElement("input");
	btn.type="button";
	btn.style.fontSize=FONTSIZE;
	btn.value=msg;
	btn.style.color=btncolor;
	btn.addEventListener("click",reportCB,false);
	xc00.appendChild(btn);

	xr1=reportTable.insertRow(1);
	str=document.createTextNode(" <press enter to continue> ");
	xc10=xr1.insertCell(0);
	xc10.appendChild(str);
	xc10.style.fontsize=32;
	xc10.align="center";
	
	var winwidth = window.document.width || window.document.body.clientWidth;
	div=document.createElement("div");
	div.style.position="absolute";
	div.style.width=DIVW;
	div.style.left=(winwidth-650)/2.;
	div.style.backgroundColor=COLOR_DEFAULTBG;
	div.style.top=TOP;
	div.appendChild(reportTable);
	
	reportTable2=document.createElement("table");
	//reportTable2.width="600px";
	reportTable2.align="center";
	reportTable2.border="1";
	reportTable2.cellPadding="0";
	reportTable2.cellSpacing="0";
	reportTable2.style.fontSize=REPORTFONTSIZE;
	reportTable2.style.backgroundColor=COLOR_DEFAULTBG;
	reportTable2.style.color=COLOR_DEFAULTFG;
	
	r=reportTable2.insertRow(0);
	c=r.insertCell(0)
	c.style.fontsize=REPORTFONTSIZE;
	c.align="center";
	c.style.backgroundColor=COLOR_DEFAULTBG;
	c.style.color=COLOR_HEADING;
	str=document.createTextNode(" Question ");
	c.appendChild(str);
	
	c=r.insertCell(1)
	c.style.fontsize=REPORTFONTSIZE;
	c.align="center";
	c.style.backgroundColor=COLOR_DEFAULTBG;
	c.style.color=COLOR_HEADING;
	str=document.createTextNode(" Correct Answer ");
	c.appendChild(str);
	
	c=r.insertCell(2)
	c.style.fontsize=REPORTFONTSIZE;
	c.align="center";
	c.style.backgroundColor=COLOR_DEFAULTBG;
	c.style.color=COLOR_HEADING;
	str=document.createTextNode(" Your Answer ");
	c.appendChild(str);
	
	for(var i=0;i<problems.length;i++){
		r=reportTable2.insertRow(i+1);
		for(var j=0;j<3;j++){
			c=r.insertCell(j);
			c.style.fontsize=22;
			c.align="center";
			if(j<2)
				c.style.color="000000";
			else if(j==2 && problems[i][j]==problems[i][j-1])
				c.style.color=COLOR_CORRECT;
			else{
				c.style.color=COLOR_WRONG;
			}
		
			val=problems[i][j];
			txt=document.createTextNode(val);
			c.appendChild(txt);
		}
	}
	
	div.appendChild(reportTable2);
	document.body.appendChild(div);
	btn.focus();
	
	div2=document.createElement("div");
	div2.style.position="absolute";
	var winwidth = window.document.width || window.document.body.clientWidth;
	div2.style.left=(winwidth-(N*cellsize)-20)/2.;
	div2.style.top="500";
	div2.appendChild(timesTable);
	document.body.appendChild(div2);
}
function reportCB(e){
	document.body.removeChild(div);
	if(n1==M-1){
		erupt();//NEED:scan from n1=1 -> n1=M-1 
	}
	else{
		init();
	}
}
function erupt(){
	//alert("Eruption goes here");
	n1=1;
	init();
	//document.location="http://www.asymptopia.org";
}
function redo(e){
	n1=parseInt(e.target.id);
	n2=1;
	while(problems.length>0)
		problems.pop();
	clear_values(n1);
	init();
}
function clear_values(midx){
	for(nidx=1;nidx<N-1;nidx++){
		try{
			if(xcells[midx][nidx].lastChild){
				xcells[midx][nidx].removeChild(xcells[midx][nidx].lastChild);
				xcells[midx][nidx].VALUE=0.;
			}
		}
		catch(e){alert(e);}
	}
	theCookie.times=make_cookie_string();
	theCookie.store();
}
function check_answer(e){
	
	if(!xcells)return;
	problems.push(new Array(problem[0],problem[1],answerbox.value));
	tend=new Date().getTime();
	if(n2==1){
		if(xcells[n1][n2].lastChild)xcells[n1][n2].removeChild(xcells[n1][n2].lastChild);
		xcells[n1][n2].appendChild(document.createTextNode("0.50"));
		xcells[n1][n2].VALUE=0.5;
	}
	else{
		if(xcells[n1][n2].lastChild)xcells[n1][n2].removeChild(xcells[n1][n2].lastChild);
		val=(tend-tstart)/1000.
		xcells[n1][n2].appendChild(document.createTextNode( val.toFixed(2).toString() ));//toFixed(2) here?
		xcells[n1][n2].VALUE=(tend-tstart)/1000.;
	}
	
	if(problem[1]==answerbox.value){
		n2=n2+1;
	}	
	else{
		passed_this_level=false;
		n2=n2+1;
	}
	if(n2>N-3){
		
		report();//CB calls init()
		
		if(passed_this_level){
			n2=1;
			n1=1;//n1+1;
			while(1){
				if(xcells[n1][N-2].VALUE>0.)n1+=1;
				else break;
			}
		}
		else{
			n2=1;
			if(n1<13)clear_values(n1);
		}
		passed_this_level=true;
		
		while(problems.length>0)
			problems.pop();
			
		theCookie.times=make_cookie_string();
		theCookie.store();
		return;
	}
	
	theCookie.times=make_cookie_string();
	theCookie.store();
	
	init();
	
}	
function set_problem(){
	x1=n1,x2=n2;
	while(problem.length>0)
		problem.pop();
	Q=x1+" x "+x2+"=";
	A=x1*x2;
	problem.push(Q);
	problem.push(A);
}
function update_display(){
	c00.removeChild(textnode);
	if(n1<13){
		textnode=document.createTextNode(problem[0]);
		c00.align="center";
		c00.width="300";
		c00.appendChild(textnode);
	}
	c01.removeChild(answerbox);
	answerbox=document.createElement('input');
	answerbox.addEventListener("change",check_answer,false);
	answerbox.style.fontSize=FONTSIZE;
	answerbox.style.backgroundColor=COLOR_ANSWBG;
	answerbox.style.foregroundColor=COLOR_ANSWFG;
	answerbox.style.color=COLOR_ANSWFG;
	answerbox.style.width="100";
	answerbox.style.align="center";
	if(n1<13){
		c01.appendChild(answerbox);
		answerbox.focus();
		//setTimeout('answerbox.focus();', 10);
		tstart=new Date().getTime();
	}
}

var n1=1,n2=1;
var problem=new Array();
var problems=new Array();
var tstart=null;
var tend=null;
var date=new Date();

var COLOR_DEBUG="AAFFAA";
var COLOR_DEFAULTBG="CCCCCC";
var COLOR_DEFAULTFG="0000AA";
var COLOR_CORRECT="00BB00";
var COLOR_WRONG="AA0000";
var COLOR_HEADING="888866";
var COLOR_ANSWBG="FFAA00";
var COLOR_ANSWFG="000099";
var FONTSIZE="50";
var REPORTFONTSIZE="20";
var TIMERFONTSIZE="14";
var TOP="50";
var DIVW="650px";
//var x=new Image();
var x=document.createElement("img");
x.src="volcano_00.gif";
var div=null;
var reportTable=null;
var c00;
var c01;

function get_game_div(){
	theTable=document.createElement("table");
	theTable.align="center";
	theTable.cellPadding="5";
	theTable.style.backgroundColor=COLOR_DEFAULTBG;
	theTable.style.color=COLOR_DEFAULTFG;
	theTable.style.fontSize=FONTSIZE;
	r0=theTable.insertRow(0);
	c00=r0.insertCell(0);
	c00.width="300";
	c00.align="right";
	textnode=document.createTextNode("");
	c00.appendChild(textnode);
	
	c01=r0.insertCell(1);
	answerbox=document.createElement('input');
	answerbox.value="";	
	answerbox.style.fontSize=FONTSIZE;
	answerbox.style.width="100";
	c01.appendChild(answerbox);
	
	vspc=document.createElement("div");
	vspc.style.height="40";
	
	var winheight = window.document.height || window.document.body.clientHeight;
	div=document.createElement("div");
	div.style.backgroundColor=COLOR_DEFAULTBG;
	div.style.top=winheight/2-250;
	var winwidth = window.document.width || window.document.body.clientWidth;
	
	theTable1=document.createElement("table");
	theTable1.align="center";
	theTable1.cellPadding="5";
	theTable1.style.backgroundColor="1C7710";
	theTable1.style.color=COLOR_DEFAULTFG;
	theTable1.style.fontSize=FONTSIZE;
	xr0=theTable1.insertRow(0);
	xc00=xr0.insertCell(0);
	xc00.align="center";
	xc00.appendChild(x);
	
	timerTable=document.createElement("table");
	timerTable.align="center";
	timerTable.cellPadding="5";
	timerTable.style.backgroundColor=COLOR_DEFAULTBG;
	timerTable.style.color=COLOR_DEFAULTFG;
	timerTable.style.fontSize=TIMERFONTSIZE;
	tr0=timerTable.insertRow(0);
	tc0=tr0.insertCell(0);
	tc0.align="center";
	//tc0.appendChild(document.createTextNode(date.getTime()/1000.));
	document.tc0=tc0;

	div.appendChild(theTable1);
	//div.appendChild(timerTable);
	//timer=document.createTextNode(date.getTime()/1000.);
	//div.appendChild(timer);
	div.appendChild(theTable);
	div.style.position="absolute";
	div.style.top=TOP;
	div.style.width=x.width+14;
	div.height=DIVW;
	//div.style.left=(winwidth-x.width-10)/2.;
	div.style.left=(winwidth-408)/2.;//Hardcoded for Konqueror/Safari ...
	return(div);
}
