﻿/**************************************************
 * Exercise functions:
**************************************************/
// Define global variables:
var keyboardlayout, keyboardplaceholder, keyboard;
var todo, minutes, seconds, charactercountdone, errors, speed;
var charactercount, intervalID;

Init();

function Init()
{
	var sampletext = document.getElementById("sampletext");
	
	if(sampletext == null)
	{
		return;
	}
	todo = sampletext.getElementsByTagName("input")[0];
	todo.value = todo.value.split("  ")[0];
	
	keyboardlayout = document.getElementById("sampletext").getElementsByTagName("input")[1];
	enabledKeys = document.getElementById("sampletext").getElementsByTagName("input")[2];
	minutes = document.getElementById("statistics").getElementsByTagName("input")[0];
	seconds = document.getElementById("statistics").getElementsByTagName("input")[1];
	charactercountdone = document.getElementById("statistics").getElementsByTagName("input")[2];
	errors = document.getElementById("statistics").getElementsByTagName("input")[3];
	speed = document.getElementById("statistics").getElementsByTagName("input")[4];
	charactercount = document.getElementById("statistics").getElementsByTagName("input")[5];
	keyboardplaceholder = document.getElementById("keyboard_placeholder");

	if(keyboardplaceholder != null)
	{
		keyboard = new KeyBoard(keyboardplaceholder,keyboardlayout.value);
		keyboard.enabledKeys = enabledKeys.value;
		keyboard.PressKey(todo.value.substring(0,1));
	}
}

function CheckCharacter(e)
{	

	if(todo.value.length == 0)
	{
		//Er is geen oefentekst!
		return;
	}
	
	// Start de tijd:
	if(intervalID == null)
	{
		Stopwatch_Start();
	}
	
	// Bepaal het event en de keycode:
	var e = e || window.event;
	var _keycode = e.keyCode || e.which;
	
					
	// Haal de tekststringen op:
	var _todo = todo.value;
							
	if(_todo.charCodeAt(0) == _keycode || _keycode == 27)
	{				
		if(_keycode == 27)
		{
			errors.value++;
			ShowError();
		}
		_todo = _todo.substring(1,_todo.length);
		todo.value = _todo;
		
		charactercountdone.value++;
		charactercount.value = todo.value.length;
		
		speed.value = GetSpeed();
		
		keyboard.PressKey(_todo.substring(0,1));
		
		if (_todo.length == 0)
		{
			// Einde oefening!
			Stopwatch_Stop();
			document.forms[0].submit();
		}
	}
	else
	{
		errors.value++;
		ShowError();
	}
	return;
}

function GetSpeed()
{
	var totalSeconds = parseInt(seconds.value) + 60 * parseInt(minutes.value);
	var iSpeed = 0;
	
	if(totalSeconds > 0)
	{
		var iCharactercountdone = parseInt(charactercountdone.value);
		iSpeed = (iCharactercountdone / totalSeconds) * 60;
	}
	
	return parseInt(iSpeed);
}

function ShowError()
{
	todo.style.backgroundColor = "#FFDDDD";
	window.setTimeout("ResetError()",200);
}

function ResetError()
{
	todo.style.backgroundColor = "transparent";
}

// Stopwatch functions
function Stopwatch_Start()
{
	intervalID = window.setInterval(tick,1000);
}

function Stopwatch_Stop()
{
	window.clearInterval(intervalID);
}

function tick()
{
	var iMinutes = parseInt(minutes.value);
	var iSeconds = parseInt(seconds.value);
		
	iSeconds ++;
	
	if(iSeconds > 59)
	{
		iMinutes ++;
		iSeconds = 0;
	}
	
	minutes.value = FormatTime(iMinutes);
	seconds.value = FormatTime(iSeconds);
}

function FormatTime(str)
{
	return str;
	
	str = "" + str;
	
	if(str.length < 2)
	{
		str = "0" + str;
	}
	
	return str;
}
