var TOT_LESSONS = 85;
var LEN_LINIA = 60;

var META_VB = 40;
var META_PR = 95;
var MIN_PR = 50;

var cTecles = 0;
var t;
var t1;
var t2;

var aluVB=0;
var aluPr=0;
var aluVN=0;
var errades="";		
var lesson ="";
var lang="";

arLessTxt = new Array(TOT_LESSONS);
arLessTxt[1]="asdf ;lkj asdf ;lkj asdf ;lkj fdsa jkl; fdsa jkl; fdsa jkl;a";
arLessTxt[2]="sas; das; fas; las; kas; jas; asa; dada; affa; ala; aka; aja";
arLessTxt[3]="sad; salads; ska; ads; add; adds; af; all; ask; as; ass; ala";
arLessTxt[4]="fall; lad; lass; dad; dj; aka; la; sa; da; fa; a; al; lsd; a";
arLessTxt[5]="fgf fgf fgf jhj jhj jhj fgf fgf fgf jhj jhj jhj fgf fgf jhja";
arLessTxt[6]="shall; slash; ash; dash; flash; flask; lash; had; half; hafa";
arLessTxt[7]="hall; has; hash; fag; flag; sag; lag; gag gas glad glass sha"; 

arLessTxt[8]="aqa sws ded frf ftf ;p; lol kik juj jyj youth hostel yet tea";
arLessTxt[9]="aqsw defr gt ;plo kiju hy aq;p swlo deki frju gthy query tha";
arLessTxt[10]="qua que qui quo war wet wi wo wu why who what wheat whist wa";
arLessTxt[11]="ra re ri ro ru ta te ti to tu tra tre tri tro tru thug tiara";
arLessTxt[12]="pay peg phial photo play pledge plight ploy plug praise plea";
arLessTxt[13]="yard yes yield yoghourt you wary waylay shaggy toy they yoga";
arLessTxt[14]="quad queasy quirk quote qualify quake quite whisky kiwi area";
arLessTxt[15]="purity pultry profit prodigal priority prey pie pulse pagoda";
arLessTxt[16]="thaw their theft thigh third throw throes thud thug operetta";
arLessTxt[17]="thrust through thrift threew thrash thy thu tho this the tha";
arLessTxt[18]="whisky wish wok water we wash work white widow west walkiria";
arLessTxt[19]="refuge; relate; reply; rusty rally rap raw rash radio raffia";
arLessTxt[20]="riot; rift; rhapsody; ridge retreat reward retort rowdy rota";
arLessTxt[21]="earth; eagle; to edge away; eke; else; epilepsy; ewe eye era";
arLessTxt[22]="lady; jaw; jerky; jersey; judge keg ill will irk itself idea";

arLessTxt[23]="aza xsx dcd fvf bfb ;/; l.l k,k jmj jnj zany zinc zone zebra";
arLessTxt[24]="azsx dcfv gb ;/l. k,jm hn az;/ sxl. dck, fvjm gb hn zu pizza";
arLessTxt[25]="/a /e /i /o /u a, e, i, o, u, /a, /e, /ir, /o, /u, /blo /bla";
arLessTxt[26]="z. x. c. v. b. u, o, i, e, a zu ozo /uzo /aza /eza /iza /oza";
arLessTxt[27]="n... m... a... e... i... crazy quiz quartz zip ablaze kwanza";
arLessTxt[28]="ma me mi mo mu am em im om um mam mem mim mom mum mc alumina";
arLessTxt[29]="uza azu ezu izu ozu uzu xa xe xi xo xu axe exa ixa ox ux axa";
arLessTxt[30]="bazaar, blaze, breeze, buzz, buzzard, jazzy, brazier bazooka";
arLessTxt[31]="xex xix axe axi axo axu oxa exe exi exo exu ixe ixo ixu xexa";
arLessTxt[32]="ca ce ci co cu chac chec cic coc cuc ace uca oca ica eca aca";
arLessTxt[33]="eco ecu choca cuca cra cre cri cro cru clu cloc clic cle cla";
arLessTxt[34]="va ve vi vo vu av ev viv vov uv eve vav ava eva viva ova uva";
arLessTxt[35]="bab beb bib bob bub bra bre bri bro bru blue blo bli ble bla";
arLessTxt[36]="za zo zu az ez iz oz uz azo ezo izo ozo uzo zaza eza iza oza";
arLessTxt[37]="an en in on un napkin nation neo nine nun nu noun ni none na";
arLessTxt[38]="/mecagratis.com/dir/images/a.jpg meters/s km/hour lt/min b/a";
arLessTxt[39]="axe, taxi, exotic, exude hoax oxtail oxide oxygen flux extra";
arLessTxt[40]="abcdefghijklmnopqrstuvwxyz; abcdefghijklmnopqrstuvwxyz uoiea";

arLessTxt[41]="á é í ó ú á é í ó ú á é í ó ú ú ó í é á á é í ó ú ú ó í é áa";
arLessTxt[42]="áaá éeé íií óoó úuú áaá éeé íií óoó úuú úuú óoó íií éeé a áa";
arLessTxt[43]="papá café creí lloró baúl ágata éstas ínclito ósmosis úlcera";
arLessTxt[44]="maná cárcel ácaros práctico botánico áspero plátanos lámpara";
arLessTxt[45]="éxito léxico también probé sé célibe apéndice hélice poética";
arLessTxt[46]="níspero, sístole, días, nítido, ahí, física, biología, manía";
arLessTxt[47]="carbón, ratón, estómagos, limón, televisión, cazón, autónoma";
arLessTxt[48]="azúcar nenúfar crepúsculos únanime úlula úrico úrsido úlcera";

arLessTxt[49]="ABCDEFGHIJKLMNÑOPQRSTUVWXYZ, ABCDEFGHIJKLMNÑOPQRSTUVWXYZ -za";
arLessTxt[50]="ZYXWVUTSRQPOÑNMLKJIHGFEDCBA, ZYXWVUTSRQPOÑNMLKJIHGFEDCBA -ba";

arLessTxt[51]="As Bf Cd Dj Ed Fl Gk Hj Ik Ja Ks Lf Md Ng Ñf Ol Pñ Ql Rf Sha";
arLessTxt[52]="Tf Uj Vf Ws Xs Yg Za Aj Bk Ch Dk Eñ Fu Gl Hf It Jq Kb Lea Ma";
arLessTxt[53]="Ne Ñe Of Pt Qk Ru So Ty Un Vh Ws Xm Yc Zi Al Me In Em Bra Na";
arLessTxt[54]="ñ_ñ l:l k;k Ñ_Ñ L:L K;K Ñ_ñ L:l K;k f; f: f_ d; d: a; a: _sa";

arLessTxt[55]="b; c; d; e; g; h; i; j; l; m; n; ñ; o; p; q; r; s; t; u; a;a";
arLessTxt[56]="v; w; x; y; z; B; C; D; E; F; G; H; I; J; K; L; M; N; Ñ; ñ;a";
arLessTxt[57]="Argentina; Bolivia; Chile; Dinamarca; España; Francia; Ghana";
arLessTxt[58]="Honduras; México; Nicaragua; Panamá; Perú; Suecia; Venezuela";

arLessTxt[59]="Áa Ée Íi Óo Úu ÁaÉeÍiÓoÚu Ágoras Éufrates Ítaca Óscar Úrsula";
arLessTxt[60]="güe güi güe güi güe güi güe güi güe argüir halagüeña argüirá";
arLessTxt[61]="b: c: d: e: g: h: i: j: l: m: n: ñ: o: p: q: r: s: t: u: :va";
arLessTxt[62]="1, 1b 1c 1d 1e 1f 1. 1- 11 111, 1 de octubre del año 11; 11a";
arLessTxt[63]="2, 2B 2C 2D 2E 2F 2. 2_ 12 212, 22 de junio del año 222; 12a";
arLessTxt[64]="3, 3g 3h 3i 3J 3K 3. 3_ 13 321, 31 de marzo del año 1333; 3a";
arLessTxt[65]="4, 4G 4H 4I 4j 4K 4. 4_ 14 434, 14 de abril del año 444; 34a";
arLessTxt[66]="5, 5l 5m 5N 5Ñ 5p 5. 5_ 15 545, 25 de julio del año 555; 45a";
arLessTxt[67]="6, 6L 6M 6n 6ñ 6P 6. 6_ 16 655, 16 de mayo del año 1666; 16a";
arLessTxt[68]="7, 7q 7r 7S 7T 7u 7. 7_ 17 767, 27 de noviembre de 1777; 37a";
arLessTxt[69]="8, 8Q 8R 8s 8t 8U 8. 8_ 18 878, 18 de septiembre de 1088; 8a";
arLessTxt[70]="9, 9V 9W 9x 9y 9Z 9. 9_ 19 989, 9 de diciembre del año 9; 9a";
arLessTxt[71]="0, 0v 0w 0X 0Y 0z 0. 0_ 10 090, 20 de agosto del año 10; 10a";

arLessTxt[72]="1º de Mayo; 2º Nivel, 3º E.S.O. 48º Regto. 59ºC 67ºF 1º Dcha";
arLessTxt[73]="I'm singin' You're Rock'n'Roll Je t'aime T'estimem Ven pa'ca";
arLessTxt[74]="¡Viva! ¡Fuera! ¡Hurra! ¡Ja! ¡Eh! ¡Bravo! ¡Estupendo! ¡Ah! ¡a";
arLessTxt[75]="9ª Marcha; 8ª Edición; 4ª Pl. 3ª Clase, 2ª División. 1ª fila";
arLessTxt[76]="\"sin blanca\"; \"en la masa\", \"in fraganti\" \"¡Wow!\" \"¡Buah!\" a";
arLessTxt[77]="ampul·lós bel·ligerant il·legals il·legible col·legi il·lesa";
arLessTxt[78]="1$ 2$ 3$ 4$ 5$ 6$ 7$ 8$ 9$ 0,20$ 99$ 102$, 49.67$ 859$ a cta";
arLessTxt[79]="98%, 7%, 6%, 5%, 4%; 3%; 2%; 1%; 0%; 19% 13%, 9.65% de media";
arLessTxt[80]="1/20, 4/9; 5/8, 6/7; 9/10, 1/2 melón, 1/4 limón y 1/3 patata";
arLessTxt[81]="(EE.UU.) (CC.OO.) (U. K.) (ONU) (FAO) (NAFTA) (Unión Europea";
arLessTxt[82]="30/2=15; 4%=0.04; 8/4=6/3; meta=c%3D; 12%=0,12; 3=9/3; ab=ba";
arLessTxt[83]="¿Qué? ¿Cómo? ¿Cuándo? ¿Por qué? ¿Sí? ¿A dónde? ¿Vale? ¿Ahora";
arLessTxt[84]="1<9; 8>2; 3<7; 10>4; 5<16; 80>2; Drôme; València; Çid; Plaça";
arLessTxt[85]="cd\\ dir|more #1 #9 Do# 2€, 8€, 3€, 7€, 5€, 6€, 4€ tu@mail.va";

//"up-to-date, upside-down, topsy-turvy, no-one son-in-law hexa";

function canviaTeclat(lesson){
	var lessonTeclat="";
	if (lesson=="") {
		 lesson = document.forms.frmTeclat.selLesson.value;
	} 	
	if (parseInt(lesson) >= 1 && parseInt(lesson) < 5){lessonTeclat = "001";
	} else if (parseInt(lesson) >= 5 && parseInt(lesson) < 8){lessonTeclat = "005";
	} else if (parseInt(lesson) >= 8 && parseInt(lesson) < 23){lessonTeclat = "008";
	} else if (parseInt(lesson) >= 23 && parseInt(lesson) < 41){lessonTeclat = "023";
	} else if (parseInt(lesson) >= 49 && parseInt(lesson) < 49){lessonTeclat = "049";
	} else if (parseInt(lesson) >= 51 && parseInt(lesson) < 62){lessonTeclat = "051";
	} else if (parseInt(lesson) >= 62 && parseInt(lesson) < 72){lessonTeclat = "062";
	} else if (parseInt(lesson) >= 72 && parseInt(lesson) < 86){lessonTeclat = "";
	}
		
	lang = document.forms.frmTeclat.Teclat.value;			 
	if (lesson<10) {lesson = "00" +lesson;
	} else if (lesson<100) {lesson = "0" +lesson; }
	
	var dirImatges = "http://www.mecagratis.com/img/";
  var imatgeTeclat = dirImatges + "mg" + lang + "Teclat1280.jpg";
  var imgTecles = dirImatges + "mg" + lang + "Teclat1280L" + lessonTeclat + ".jpg";	
	
  document.getElementById("imgTeclat").src = imatgeTeclat;
	document.getElementById("imgTecles").src = imgTecles;
}


function canviaLesson(lang){
  var Lesson = document.forms.frmTeclat.selLesson.value;
	var lang = document.forms.frmTeclat.Teclat.value;
	document.location="index.php?p=curso&lang="+lang+"&lesson="+Lesson;
}


function nextLessonReg(lesson, Correu, Clau){
	var nLesson = "";
	var vb = document.forms.frmTxt.vb.value;
	var pr = document.forms.frmTxt.pr.value;
	if (lesson > TOT_LESSONS){lesson = TOT_LESSONS;}
	if (lesson<10) {nLesson = "00" + lesson;
	}	else if (lesson<100) {nLesson = "0" +lesson; }
}

function nextLesson(lesson){
	var nLesson = "";
	var vb = document.forms.frmTxt.vb.value;
	var pr = document.forms.frmTxt.pr.value;
	if (lesson > TOT_LESSONS){lesson = TOT_LESSONS;}
	if (lesson<10) {nLesson = "00" + lesson;
	}	else if (lesson<100) {nLesson = "0" +lesson; }
	window.location="index.php?p=curso&lesson="+nLesson+"&vb="+vb+"&pr="+pr;
}


function començar(lesson){		 
	document.forms.frmTxt.inTxt.value = arLessTxt[parseInt(lesson)];
	cTecles = 0;	
  aluVB=0;
  aluPr=0;
  aluVN=0;
	errades="";			
	document.forms.frmTxt.inAlu.value = "";
	document.forms.frmTxt.inErr.value = "";
	document.forms.frmTxt.inAlu.focus();
	document.forms.frmTxt.vb.value = "";
	document.forms.frmTxt.pr.value = "";
	document.forms.frmTxt.vn.value = "";
	document.forms.frmTxt.inCom.value = "";
}


function calVB(){
	t = (t2 - t1)/1000;
	return Math.round(60*60/t);
}

function calPr(e){
	var encerts=0;
	var charCode;
	
	for (i=0; i<LEN_LINIA-1; i++){
		
		if (document.forms.frmTxt.inTxt.value.substr(i, 1)==document.forms.frmTxt.inAlu.value.substr(i, 1)){	
			encerts++;
			errades += " ";			 	
		} else {errades += document.forms.frmTxt.inTxt.value.substr(i, 1);}
	}

  if (navigator.appName == "Netscape"){
    charCode = e.which;
	}else {
    charCode = e.keyCode;
  }
	
	
	if (charCode=='97'){	
			encerts++;
			errades += " ";			 	
		} else {errades += document.forms.frmTxt.inTxt.value.substr(LEN_LINIA-1, 1);}
	
	if (encerts==0){ return 0;
	} else {return Math.round(encerts * 100 / LEN_LINIA);}
}

function calVN(){
	return Math.round(document.forms.frmTxt.vb.value * document.forms.frmTxt.pr.value / 100);
}

function resultats(){
	if (aluVB<500 && aluPr>20) document.forms.frmTxt.vb.value = aluVB;
	if (aluPr>20) {document.forms.frmTxt.pr.value = aluPr;}
	if (aluPr >= META_PR) {
		 aluVN = calVN();
		 document.forms.frmTxt.vn.value = aluVN; }
	
	document.forms.frmTxt.inErr.value = errades;
	
	if (aluVB >= META_VB && aluPr == 100) {
			document.forms.frmTxt.inCom.value = "Perfect exercise. You have not looked at your keyboard, have you? Progress to the next one.";
	} else if (aluVB >= META_VB && aluPr >= META_PR) {
			document.forms.frmTxt.inCom.value = "Well, you have achieved the targets. Progress to the next exercise.";
	} else if (aluVB >= META_VB && aluPr < META_PR && aluPr >= MIN_PR) {
			document.forms.frmTxt.inCom.value = "You have achieved the speed goal and fail to reach the accuracy target. Repeat the exercise, please.";
	} else if (aluVB < META_VB && aluPr >= META_PR) {
			document.forms.frmTxt.inCom.value = "You have achieved the accuracy goal and fail to reach the speed target. Repeat the exercise, please.";
	} else if (aluVB >= 500 || aluPr >= 20) {
			document.forms.frmTxt.inCom.value = "You should visit the games page.";
	}
}		

function tecla(e){
  cTecles++;
	switch (cTecles){
    case 1:	 																
		  t1 = new Date(); 
	    break;
    case LEN_LINIA:	 																
		  t2 = new Date();
     	aluVB=calVB();
      aluPr=calPr(e);
      aluVN=0;
			if (aluPr >= META_PR) {
				aluVN = calVN();
			}
			resultats();
	    break;
	}					
}


function enviarMatrMg(){
	if (document.forms.MgMatricula.Nom.value.length==0) {
    	alert('Please enter your first name and surname.');
    	document.forms.MgMatricula.Nom.focus();
  } else if (document.forms.MgMatricula.Correu.value.length==0) {
    	alert('Please enter your e-mail adress.');
    	document.forms.MgMatricula.Correu.focus();
  } else if (document.forms.MgMatricula.Clau.value.length<6) {
    	alert('Please enter a new 6-character password.');
    	document.forms.MgMatricula.Clau.focus();
  } else if (document.forms.MgMatricula.Correu.value.length>0){
        var Byte="";
        var Arroba=0;
        var Punto=0;
        for (var i=0;i<document.forms.MgMatricula.Correu.value.length;i++){
          Byte=document.forms.MgMatricula.Correu.value.substring(i,i+1);
          if(Byte=="@")
            Arroba=Arroba+1;
          if(Byte==".")
            Punto=Punto+1;
        }
        if (Arroba!=1 || Punto==0) {
          alert ("The e-mail address is not correct.");
          document.forms.MgMatricula.Correu.focus();  
  } else document.forms.MgMatricula.submit() }
}


function enviarMgAlumne(){
	if (document.forms.frmMgAlumne.Correu.value.length==0) {
    	alert('Please enter your e-mail adress.');
    	document.forms.frmMgAlumne.Correu.focus();
  } else if (document.forms.frmMgAlumne.Clau.value.length<6) {
    	alert('Please enter your 6-character password.');
    	document.forms.frmMgAlumne.Clau.focus();
  } else if (document.forms.frmMgAlumne.Correu.value.length>0){
        var Byte="";
        var Arroba=0;
        var Punto=0;
        for (var i=0;i<document.forms.frmMgAlumne.Correu.value.length;i++){
          Byte=document.forms.frmMgAlumne.Correu.value.substring(i,i+1);
          if(Byte=="@")
            Arroba=Arroba+1;
          if(Byte==".")
            Punto=Punto+1;
        }
        if (Arroba!=1 || Punto==0) {
          alert ("The e-mail address is not correct.");
          document.forms.frmMgAlumne.Correu.focus();  
  } else document.forms.frmMgAlumne.submit() }
}


function enviarMgClau(){
	if (document.forms.frmMgClau.Correu.value.length==0) {
    	alert('Please enter your e-mail adress.');
    	document.forms.frmMgClau.Correu.focus();
  } else if (document.forms.frmMgClau.Correu.value.length>0){
        var Byte="";
        var Arroba=0;
        var Punto=0;
        for (var i=0;i<document.forms.frmMgClau.Correu.value.length;i++){
          Byte=document.forms.frmMgClau.Correu.value.substring(i,i+1);
          if(Byte=="@")
            Arroba=Arroba+1;
          if(Byte==".")
            Punto=Punto+1;
        }
        if (Arroba!=1 || Punto==0) {
          alert ("The e-mail address is not correct.");
          document.forms.frmMgClau.Correu.focus();  
  } else document.forms.frmMgClau.submit() }
}