//s-- Copyright © 2009 ExIdeas, Inc.* -->
// MessagEase challenge word ttest routines
/****************************************************************************************************************/
	var test_screen_chars_min		= 150;
	var ttest_in_progress 			= false;
	var ttest_level					= "com1000";				// first level for now

	var ttest_str_Index 			= 0							// points to where the cursor is 
	var ttest_flag_Array 			= [];						// this array keeps the conditions true or false
	var ttest_period_secs			= 60;
	
	popup_showing_flag 				= false;
/****************************************************************************************************************/
function fill_line(){							// return with ten unique words (or later with a few sentences
	var word_set = "";
	for (var i=0; i <10; i++)
		word_set += iphone_ttest_words[ttest_level][randNum(0, iphone_ttest_words[ttest_level].length -1)] + sp;		// add ten words, worry about uniqueness later
	return(word_set)
}
/****************************************************************************************************************/
function check_and_fill_screen(){			// put line2 in line 1, create a new line 2 (do this twice to create both)
	while (disp_array.length < test_screen_chars_min)
		addStrToScreen(fill_line());								// add a few uniqe words with sp
}
/******************************************** ttest vars *******************************************************/
var NO_LTRS_PER_WORD 	= 5;					// letters per word assumption, including the trailing space.
var no_of_strokes;
var no_of_incorrect_strokes;
var time_started_secs;
var time_updated 		=0
var wpm_raw;
var wpm_adjusted;
/***************************************************************************************************************/
function ttest_stats_update(){
	var time_now 					= new Date();
	var time_now_secs				= Math.round(time_now.getTime()/1000);	
	if (time_now_secs > time_updated){					// time to update
		time_updated = time_now_secs;					// update only happnes on seconds
		var elapsed_time_seconds = time_now_secs - time_started_secs;
		if (elapsed_time_seconds > 2){
			wpm_raw = Math.round((60*no_of_strokes)/(elapsed_time_seconds * NO_LTRS_PER_WORD));
			if (wpm_raw >0)
				ttest_stats_show();
		}
	}
}
/****************************************************************************************************************/
function ttest_stats_reset(){
	var time_now					= new Date();
	
	time_started_secs			=  Math.round(time_now.getTime()/1000);	
	no_of_strokes				= 0;
	no_of_incorrect_strokes		= 0;
	wpm_raw						= 0;
	wpm_adjusted				= 0;
	ttest_stats_show();
}
/****************************************************************************************************************/
function ttest_stats_show(){
	 putInDiv("dayTimeDiv", "WPM: " + wpm_raw )
}
/****************************************************************************************************************/
function showExtrasPopup(){

		str = 'Change the language using the <b>Languages</b> Menu, or try a timed test!<br>';
		str += 'Click on <b>Typing Match</b> on top and pick a category to test your typing speed using MessagEase.';
		
		
	
	str += '<br> <span class="innerTitleStyle" onmousedown="hidepopup()">--- OK ---</span>';
	
	putInDiv("innerpopText", str)
	showPleaPop();	
}
/****************************************************************************************************************/
function ttest_stats_finalize(){
	var adj = Math.round((no_of_incorrect_strokes)/(NO_LTRS_PER_WORD));
	wpm_adjusted = wpm_raw - adj;
	ttest_stats_update();
	
	var wpmstr = '<span class="innerTitleStyle">' + wpm_raw +  ' </span>';
	var wpmadjstr = '<span class="innerTitleStyle">' + wpm_adjusted + '</span>';
	
	
	var str = '<span class="innerTitleStyle">Your Results</span><br> Raw Score: ' + wpmstr + ' WPM<br> Adjusted Score: ' + wpmadjstr + ' WPM<br>';
	if (no_of_incorrect_strokes > (no_of_strokes/2))
		str += 'you are making too many mistakes! Start from  the basics and practice the ANIHORTES tests more!';
	else if (wpm_raw <8)
		str += 'Not bad! You are getting the idea, but you need a few practice sessions to become proficient!';
	else if (wpm_raw <12)
		str+= 'Good show! Sounds like you are having fun with this! Just imagine how fast you could do this if you were actually using your finger!';
	else if (wpm_raw <16)
		str += 'Wow! you are smoking! this is almost acceptable typing speed and you are doing it with a mouse! ';
	else 
		str += 'Bravo! You can see that once you get used to this, you can actually type at a pretty good speed! ';
	
	str += '<br> <span class="innerTitleStyle" onmousedown="hidepopup()">--- OK ---</span>';
	
	putInDiv("innerpopText", str)
	showPleaPop();	
}
/****************************************************************************************************************/
function StartStopTTest(param){
	hideDiv('menuDiv');
	menu_showing_flag = false;
	ttest_in_progress = true;			// assume starting
	if (param=="stop")
		ttest_in_progress = false;
	else
		ttest_level	= param;
	if(ttest_in_progress){								// we just started ttest		
		timerObject.startStop(ttest_period_secs);		// start timer this also shows the icon
		wipe();											// change the display state to test text
		ttest_str_Index= 0;								// reset the pointer
		check_and_fill_screen();
		ttest_stats_reset();							// ttest_stats_update(); is called from timer run routine
	}
	else{												// test has stopped
	 			// if there is timer left on the timer then it was prematurely stopped. if the timer ran out, then stop it.
				// stop the timer  (which should hide it) this also shows the correct icon
		timerObject.startStop(0);// used to be 55
		wipe();	
		if (disp_str_curr == initialMessage)			// throw away if it's  the same
			disp_str_curr = "";
		addStrToScreen(disp_str_curr);
		if (timerObject.TimeLeft>0){ // incomplete ttest
		}
		else{							// test was complete, gather stats 
			ttest_stats_finalize()
		}
			
	}
}
/***********************************************************************************************************************/
function ttest_check(c){
	if (c.length >1) return;			// only consider one letter commands
	no_of_strokes++;
	if (c != disp_str.charAt(ttest_str_Index))
			no_of_incorrect_strokes++;
	ttest_flag_Array[ttest_str_Index] =  ( c == disp_str.charAt(ttest_str_Index)); 			// set the flag in the arry TF
	ttest_str_Index ++;
	
	if (disp_str.charAt(ttest_str_Index) == cr){				// if reached a CR at the end of the line, remove the top line
		disp_array.splice(0, ttest_str_Index);					// remove the first  line  
		check_and_fill_screen();
		ttest_str_Index= 0;
	}
	render_display();
	//debugMessage('disp_array.length: ' + disp_array.length );
}
/***********************************************************************************************************************/
function render_ttest_string(s){					// takes a str created by the render_display and adds evluative colors for test display. 
	var c, buffer = "";								// Uses ttest_str_Index and ttest_flag_Array;

	for (var i=0; i < s.length; i++){						// traverese the length of the s for rendering
			c = s.charAt(i); 					// get the char
			if  (i < ttest_str_Index)
				if (ttest_flag_Array[i])
					c = "<span class=\"correctStyle\">" + c + "</span>";
				else
					if (c == sp ) 
						c = "<span class=\"incorrectspaceStyle\">" + c + "</span>";
					else
						c = "<span class=\"incorrectStyle\">" + c + "</span>";
			else 
				if (i == ttest_str_Index)
					c = "<span class=\"underlineStyle\">" + c + "</span>"; 
			buffer += c;
	}
	return(buffer);
}
/*****************************************************************************************************************/
function showPleaPop(){
	popup_showing_flag = true;
	changeOpac(50, "outerpopDiv")
	changeOpac(90, "innerpopDiv")
	document.getElementById("outerpopDiv").style.visibility="visible";
	document.getElementById("innerpopDiv").style.visibility="visible";
}
/*****************************************************************************************************************/
function hidepopup(){
	popup_showing_flag =false;
	document.getElementById("outerpopDiv").style.visibility="hidden";
	document.getElementById("innerpopDiv").style.visibility="hidden";
} 
/*****************************************************************************************************************/
function putInDiv(DivName, Str){
	document.getElementById(DivName).innerHTML = Str; 
}
/***************************************************************************************************************************/
function randNum(n, m){  // return an integer between 0 and n-1
  return ( n + Math.floor ( Math.random ( ) * (m -n +1)  ) );
}
/***************************************************************************************************************************/
