

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

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!";
		wrong1='';
		porpara='';
        noattempts="You didn't even try!";
        print_result1="Your result is \n\n";
        print_result2="%. \n\n";
        print_result3="Correct ";
        print_result5="Errors ";
        print_result4="Attempts ";
        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/7") != -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) 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)
        {
                whichASC = e.keyCode;
        }

        if (whichASC == 13)
        {
                check();
                return false;
        }
}


function verb_se(num_verbs)
{
        document.trainer.guess.value="";
        i=Math.floor(Math.random()*num_verbs);
        document.trainer.first.value=first[id_arr[i]];
        document.trainer.last.value=last[id_arr[i]];
        document.trainer.translation.value=translation[id_arr[i]];
		wrong1=wrong+"\n"+aplication[id_arr[i]];
		porpara=regla[id_arr[i]];
        solution=answer[id_arr[i]];
        document.trainer.guess.focus();
        this.href="#contrain";
}


function checkpor ()
{
//        attempts++;
//        verb_attempts[id_arr[i]]++;
		if ((document.trainer.guess.value == solution) || (document.trainer.por.value == porpara))
        {
        document.trainer.guess.value=solution;
		}
        else
        {
        attempts++;
                document.trainer.guess.value="";
                alert(wrong1);
                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_se_results",cookie_str + ";");
        cookie_str = "";
        return false;
}

function checkpara ()
{
//        attempts++;
//        verb_attempts[id_arr[i]]++;
		if ((document.trainer.guess.value == solution) || (document.trainer.para.value == porpara))
        {
        document.trainer.guess.value=solution;
        }
        else
        {
        attempts++;
                document.trainer.guess.value="";
                alert(wrong1);
                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_se_results",cookie_str + ";");
        cookie_str = "";
        return false;
}


function check ()
{
        attempts++;
        verb_attempts[id_arr[i]]++;
        if (document.trainer.guess.value == solution)
        {
                alert(right);
                verb_correct[id_arr[i]]++;
                correct++;
                verb_se(num_verbs);
        }
        else
        {
                document.trainer.guess.value="";
                alert(wrong1);
                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_se_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);
                errors=(attempts-correct);
                alert(print_result1+ratio+print_result2+print_result3+correct+"\n"+print_result5+errors+"\n"+print_result4+attempts);
        }
        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=";
                }
                query += "+site:" + site;
        }

        url += query + "&lr=lang_es";
        window.open(url,"usage");

}


