

/***********************************

conjugation trainer functions

***********************************/


attempts=0;
correct=0;
i=0;
ratio=0;
cookie_str = "";

var lang;
var sub;


if(lang=="de")
{
	right="Richtig!";
	wrong="Das ist leider falsch";
	noattempts="Du hast noch gar keinen Versuch gemacht!";
	print_result1="Du hast \n\n";
	print_result2="%  richtig gemacht.";
	noverb="Du musst erst ein Wort haben!";
}
if(lang=="en")
{
	right="That's right!";
	wrong="Sorry! That's wrong!";
	noattempts="You didn't even try!";
	print_result1="Your result is \n\n";
	print_result2="%.";
	noverb="You have to choose a new word first!";
}


IE = (document.all);
NS4 = (document.layers);

GECKO = (navigator.userAgent.indexOf("Gecko") != -1) ? true : false;
OPERA = (navigator.userAgent.indexOf("Opera/") != -1) ? true : false;

if (NS4) document.captureEvents(Event.KEYPRESS);
document.onkeypress = doKey;

var obj;

function init() {
  obj = document.getElementById("trainer");
  if(obj)
  {
	obj.addEventListener("keypress", doKey, true);
  }
}

if (GECKO || OPERA) onload = init;


function returnOpera()
{
	if (OPERA)
	{
		return false;
	}	
}


function doKey(e) 
{ 
	if(NS4)
	{
		whichASC = e.which;	
	}
	else if(IE)
	{
		whichASC = event.keyCode;	
	}
	else if(GECKO || OPERA)
	{
		whichASC = e.keyCode;	
	}

	if (whichASC == 13)
	{
		check();
		return false;
	}
}


function verb (num_verbs) 
{
	document.trainer.guess.value="";
	//pr_hint = document.getElementById("pronoun_hint");
	//pr_hint.value = "test";
	p=document.selection.selpronoun.selectedIndex;
	if (p == 11) p=Math.floor(Math.random()*16);
	if (p == 10) 
	{
		p=Math.floor(Math.random()*15);
		if (p == 6) p=Math.floor(Math.random()*6);
	}
	i=Math.floor(Math.random()*num_verbs);
	document.trainer.trverb.value=infinitiv[id_arr[i]];
	document.trainer.trtrans.value=translation[id_arr[i]];

	if (p == 0 || p == 11 || p == 12) 
	{
		solution=yo[id_arr[i]];
		verb_form="yo";
	}
	if (p == 1 || p == 10) 
	{
		solution=tu[id_arr[i]];
		verb_form="tú";
	}
	if (p == 2) 
	{
		solution=ella[id_arr[i]];
		verb_form="ella";
	}
	if (p == 5 || p == 14) 
	{
		solution=nosotros[id_arr[i]];
		verb_form="nosotros";
	}
	if (p == 8) 
	{
		solution=ellos[id_arr[i]];
		verb_form="ellos";
	}
	if (p == 3) 
	{
		solution=ella[id_arr[i]];
		verb_form="él";
	}
	if (p == 4) 
	{
		solution=ella[id_arr[i]];
		verb_form="usted";
	}
	if (p == 7) 
	{
		solution=ellos[id_arr[i]];
		verb_form="ellas";
	}
	if (p == 9) 
	{
		solution=ellos[id_arr[i]];
		verb_form="ustedes";
	}
	if (p == 6 || p == 15) 
	{
		solution=vosotros[id_arr[i]];
		verb_form="vosotros";
	}
	if (p == 13) 
	{
		solution=(sub==1) ? tu[id_arr[i]]:yo[id_arr[i]];
		verb_form=(sub==1) ? "tú":"yo";
	}
	document.trainer.trpronoun.value = "(" + verb_form + ")";
	document.trainer.guess.focus();
	this.href="#contrain";
}


function check () 
{
	attempts++;
	verb_attempts[id_arr[i]]++;
	if (document.trainer.guess.value == solution) 
	{
		alert(right);
		verb_correct[id_arr[i]]++;
		correct++;
		verb(num_verbs);
	}
	else 
	{
		document.trainer.guess.value="";
		alert(wrong);
		document.trainer.guess.focus();
	}
	// set cookie with results for all verbs
	cookie_str += verb_tense + "|";
	now = new Date();
	tmstp = now.getTime();
	cookie_str += tmstp + "|";
	cookie_str += correct + "/" + attempts + "|";
	for(k = 0; k < id_arr.length; k++)
	{
		cookie_str += ";" + id_arr[k] + "/";	
		cookie_str += verb_correct[id_arr[k]] + "/";			
		cookie_str += verb_attempts[id_arr[k]];		
	}
	setSessCookie("verb_results",cookie_str + ";");
	cookie_str = "";
	return false;
}

function solve () 
{
	document.trainer.guess.value=solution;
}


function result()
{
	if (attempts == 0) alert(noattempts);
	else 
	{
		ratio=Math.ceil(correct*100/attempts);
		alert(print_result1+ratio+print_result2);
	}
	return;
}


function conjugation() 
{
	if (document.trainer.trverb.value == "") 
	{
		alert(noverb);
		return;
	}
	alert(" yo "+ yo[id_arr[i]]+
	"\n tú "+tu[id_arr[i]]+
	"\n ella "+ella[id_arr[i]]+
	"\n nosotros "+nosotros[id_arr[i]]+
	"\n vosotros "+vosotros[id_arr[i]]+
	"\n ellos "+ellos[id_arr[i]]);
	return;
}

function add_accent(letter)
{
	document.trainer.guess.value+=letter;
	document.trainer.guess.focus();
}


function getUsage(site)
{
	
	url = "http://www.google.com/search?q=";
	query = solution;	

	if(site.length > 0)
	{
		if(site == "groups")
		{
			url = "http://groups.google.com/groups?q=";
		}
		else
		{
			query += "+site:" + site;
		}
	}

	url += query + "&lr=lang_es";
	window.open(url,"usage");

}





