//========================================================================
// Api Speakcat 1.0
// Conté les funcions (DOM) que es fan servir en els exercicis
//  Copyright (C) 2006,07  	Albert Trias Mansilla
//					Xavier Vallejo López
//========================================================================


//Variables globals fetes servir.
var invisible,ko,ok,capcalera_ok=0,demo_ok=0;

ko="../../imatges/cross.png";
ok="../../imatges/tick.png";
invisible="../../imatges/invisible.png";

var prova=null;
var ajudaobert=0;
var dialegobert=0;
var linkajuda=0;
var tipusArrosega;
var flagguirion=0;
var ultim=1;
var tguiriafegit=0;
var teclatpos=-1;


/*-----------------------------------------------------Codi de generació de la taula del teclat guiri-----------------------------------------------*/

var teclat=document.createElement("img");
teclat.src="../../imatges/keyboard.png";
teclat.id='TeclatGuiri';

var taula=document.createElement('tbody');
var tb=document.createElement('table');
tb.className='teclatguiri';
tb.id='tguiri';
tb.border=1;

/*Generació de la primera fila de la taula, element a element*/

fila= document.createElement('tr');
//à
casella= document.createElement('td');
lletra= document.createTextNode('à');
casella.onclick=function(){
document.getElementById(ultim).value=document.getElementById(ultim).value+'à';
return (false);
};
casella.appendChild(lletra);
fila.appendChild(casella);

//é
casella= document.createElement('td');
lletra= document.createTextNode('é');
casella.onclick=function(){
document.getElementById(ultim).value=document.getElementById(ultim).value+'é';
return (false);
};
casella.appendChild(lletra);
fila.appendChild(casella);

//è
casella= document.createElement('td');
lletra= document.createTextNode('è');
casella.onclick=function(){
document.getElementById(ultim).value=document.getElementById(ultim).value+'è';
return (false);
};
casella.appendChild(lletra);
fila.appendChild(casella);

//í
casella= document.createElement('td');
lletra= document.createTextNode('í');
casella.onclick=function(){
document.getElementById(ultim).value=document.getElementById(ultim).value+'í';
return (false);
};
casella.appendChild(lletra);
fila.appendChild(casella);

//ï
casella= document.createElement('td');
lletra= document.createTextNode('ï');
casella.onclick=function(){
document.getElementById(ultim).value=document.getElementById(ultim).value+'ï';
return (false);
};
casella.appendChild(lletra);
fila.appendChild(casella);

//ó
casella= document.createElement('td');
lletra= document.createTextNode('ó');
casella.onclick=function(){
document.getElementById(ultim).value=document.getElementById(ultim).value+'ó';
return (false);
};
casella.appendChild(lletra);
fila.appendChild(casella);

//ò
casella= document.createElement('td');
lletra= document.createTextNode('ò');
casella.onclick=function(){
document.getElementById(ultim).value=document.getElementById(ultim).value+'ò';
return (false);
};
casella.appendChild(lletra);
fila.appendChild(casella);

//ú
casella= document.createElement('td');
lletra= document.createTextNode('ú');
casella.onclick=function(){
document.getElementById(ultim).value=document.getElementById(ultim).value+'ú';
return (false);
};
casella.appendChild(lletra);
fila.appendChild(casella);

//ü
casella= document.createElement('td');
lletra= document.createTextNode('ü');
casella.onclick=function(){
document.getElementById(ultim).value=document.getElementById(ultim).value+'ü';
return (false);
};
casella.appendChild(lletra);
fila.appendChild(casella);

//ç
casella= document.createElement('td');
lletra= document.createTextNode('ç');
casella.onclick=function(){
document.getElementById(ultim).value=document.getElementById(ultim).value+'ç';
return (false);
};
casella.appendChild(lletra);
fila.appendChild(casella);

//-
casella= document.createElement('td');
lletra= document.createTextNode('-');
casella.onclick=function(){
document.getElementById(ultim).value=document.getElementById(ultim).value+'-';
return (false);
};
casella.appendChild(lletra);
fila.appendChild(casella);
//afegim la fila generada a la taula
taula.appendChild(fila);

/*Generació de la segona fila de la taula, element a element*/

fila= document.createElement('tr');
//À
casella= document.createElement('td');
lletra= document.createTextNode('À');
casella.onclick=function(){
document.getElementById(ultim).value=document.getElementById(ultim).value+'À';
return (false);
};
casella.appendChild(lletra);
fila.appendChild(casella);

//É
casella= document.createElement('td');
lletra= document.createTextNode('É');
casella.onclick=function(){
document.getElementById(ultim).value=document.getElementById(ultim).value+'É';
return (false);
};
casella.appendChild(lletra);
fila.appendChild(casella);

//È
casella= document.createElement('td');
lletra= document.createTextNode('È');
casella.onclick=function(){
document.getElementById(ultim).value=document.getElementById(ultim).value+'È';
return (false);
};
casella.appendChild(lletra);
fila.appendChild(casella);

//Í
casella= document.createElement('td');
lletra= document.createTextNode('Í');
casella.onclick=function(){
document.getElementById(ultim).value=document.getElementById(ultim).value+'Í';
return (false);
};
casella.appendChild(lletra);
fila.appendChild(casella);

//Ï
casella= document.createElement('td');
lletra= document.createTextNode('Ï');
casella.onclick=function(){
document.getElementById(ultim).value=document.getElementById(ultim).value+'Ï';
return (false);
};
casella.appendChild(lletra);
fila.appendChild(casella);

//Ó
casella= document.createElement('td');
lletra= document.createTextNode('Ó');
casella.onclick=function(){
document.getElementById(ultim).value=document.getElementById(ultim).value+'Ó';
return (false);
};
casella.appendChild(lletra);
fila.appendChild(casella);

//Ò
casella= document.createElement('td');
lletra= document.createTextNode('Ò');
casella.onclick=function(){
document.getElementById(ultim).value=document.getElementById(ultim).value+'Ò';
return (false);
};
casella.appendChild(lletra);
fila.appendChild(casella);

//Ú
casella= document.createElement('td');
lletra= document.createTextNode('Ú');
casella.onclick=function(){
document.getElementById(ultim).value=document.getElementById(ultim).value+'Ú';
return (false);
};
casella.appendChild(lletra);
fila.appendChild(casella);

//Ü
casella= document.createElement('td');
lletra= document.createTextNode('Ü');
casella.onclick=function(){
document.getElementById(ultim).value=document.getElementById(ultim).value+'Ü';
return (false);
};
casella.appendChild(lletra);
fila.appendChild(casella);

//Ç
casella= document.createElement('td');
lletra= document.createTextNode('Ç');
casella.onclick=function(){
document.getElementById(ultim).value=document.getElementById(ultim).value+'Ç';
return (false);
};
casella.appendChild(lletra);
fila.appendChild(casella);

//'
casella= document.createElement('td');
lletra= document.createTextNode('\'');
casella.onclick=function(){
document.getElementById(ultim).value=document.getElementById(ultim).value+'\'';
return (false);};
casella.appendChild(lletra);
fila.appendChild(casella);
//Afegim la fila a la taula
taula.appendChild(fila);
//Afegim el TBODY a la taula, i la completem.
tb.appendChild(taula);

/*----------------------------------------------------Fi Codi de generació de la taula del teclat guiri-----------------------------------------------*/


/*Funció que llegeix la direcció web de la pàgina, i li aplica un parser
per determinar en quin apartat som, quina unitat, objectiu i exercici*/
function genera_capcalera()
{
	//Obtenim la URL
	var a =location.href;
	//Per defecte tipus 1 (Establert)
	var tipus=1;

	var tm=a.split('/');
	var c=tm[tm.length-2];
	var d=c.split('_');

	if (d[0]=="Lliure") tipus=0;
	else if(tm[tm.length-3]=="Context_Cultural") tipus=2;

	var b=(tm[tm.length-1]);
	var tm2=b.split('_');
	//Obtenim la unitat
	var Un=parseInt(tm2[0]);
	//L'objectiu
	var Obj=parseInt(tm2[1]);
	var tm3=tm2[2].split('.');
	//I l'exercici
	var exer=parseInt(tm3[0]);
	//Amb aquestes dades, ja podem generar els títols dels exercicis.
	Capcalera(Un,Obj,exer,tipus);
}

/*
Linkajuda, determina quin link s'ha de posar a l'ajuda
0 -> cap
1 -> Select
2 -> Omplir TextBox
3 -> Arrosegar
4 -> Penjat
5 -> Sopa de Lletres
6 -> Radiobutton
*/

/*S'afegeix dinàmicament a cada exercici, posant-li un títol que es mostri per pantalla.
A més a més, prepara els div on anirà l'ajuda, diàleg i respostes*/
function Capcalera(Unitat,Objectiu,Exercici,Tipus){
	var x,z;

	//Noms de totes les unitats, tant de l'itinerari establert com del lliure.
	var NomUnitat=new Array("Informació Personal (I)","Informació Personal (II)",
	"Temps i horaris","Accions quotidianes","Requisits, indicacions i instruccions",
	"Localitzacions i direccions","Fets i esdeveniments passats (I)","Fets i esdeveniments passats (II)","Dates i plans de futur","Opinions, gustos i preferències");

	//Tots els exercicis de l'itinerari Lliure es poden classificar en dues categories.
	var NomObjectiuLL=new Array("Activitats de comprensió","Exercicis de gramàtica");

	//Ara venen tots els títols de l'itinerari establert.
	var NomObjectiu=new Array(
	new Array("Saludar i acomiadar-se","Identificar-se i demanar a algú que s'identifiqui",
	"Demanar i dir el nom","Demanar i dir la procedència","Demanar i dir l'adreça","Demanar i dir el  telèfon","Demanar i dir l'edat i la data de naixement","Activitats globals"),
	new Array("Identificar una tercera persona i dir com es diu","Demanar i dir qui és algú",
	"Demanar el nom d'una tercera persona","Presentar una tercera persona i respondre a una presentació","Demanar i dir què estudia algú","Demanar i dir on estudia algú","Activitats  globals"),
	new Array("Demanar i dir l'hora","Demanar i dir horaris de serveis diversos","Demanar i dir horaris de transports","Demanar i dir horaris de cursos i classes",
	"Demanar i dir la durada d'un curs","Activitats globals"),
	new Array("Demanar i dir què fa algú ara, en el moment present","Demanar i dir què fa algú habitualment","Demanar i dir amb quina freqüència es realitzen accions quotidianes","Activitats globals"),
	new Array("Expressar de manera personal una intenció, una necessitat, una obligació, una possibilitat","Dir i demanar de manera impersonal quins requisits  són necessaris per fer alguna cosa","Activitats globals"), 
	new Array("Demanar i dir on és un lloc dins d'un edifici","Donar punts de referència dins d'un edifici","Demanar i indicar on és un lloc en una ciutat i donar-ne punts de referència",
	"Demanar i dir com es va a un lloc","Activitats globals"),
	new Array("Demanar i donar informació  sobre accions quotidianes passades","Demanar i donar informació sobre esdeveniments passats","Activitats globals"),
	new Array("Demanar i dir què ha fet algú avui","Demanar i dir si ja s'ha fet una cosa","Comparar fets recents amb fets passats","Activitats globals"),
	new Array("Projectes per al futur","Demanar i dir una data futura","Activitats globals"),
	new Array("Expressar gustos sobre coses i activitats","Expressar  preferències sobre coses i activitats","Proposar de fer alguna cosa","Activitats globals")
	);

	//Finalment, els noms dels objectius del Context Cultural
	var NomObjectiuC=new Array("Alfabet","Preguntes auxiliars","Números",
	"Telèfons","Transports","Calendari","Horaris","Productes i aliments","Geografia");
	
	//Codi per crear e insertar el títol.
	b=document.getElementById("inserta");
	//Genera el títol gros
	x=document.createElement('h1');
	x.id="Unitat";
	b.appendChild(x);
	/*Si és un exercici del Context Cultural, afegim el nom de Context Cultular, 
	altrament afegirem el títol de la unitat*/
	if(Tipus==2){
		z=document.createTextNode("Context Cultural");
	}
	else z=document.createTextNode(NomUnitat[Unitat-1]);
	x.appendChild(z);
	//A continuació, el subtítol
	x=document.createElement('h2');
	x.id="Objectiu";
	b.appendChild(x);
	//Determina de quin dels 3 grans apartats és.
	if (Tipus==1){
		z=document.createTextNode(NomObjectiu[Unitat-1][Objectiu-1]);}
	else if (Tipus==0){
		z=document.createTextNode(NomObjectiuLL[Objectiu-1]);
	}
	else if(Tipus==2){
		z=document.createTextNode(NomObjectiuC[Objectiu-1]);
	}
	x.appendChild(z);
	x=document.createElement('h3');
	x.id="Exercici";
	b.appendChild(x);
	//I ara genera la tercera frase, que indica quin número d'exercici és.
	z=document.createTextNode("Exercici "+Exercici+"  ");
	x.appendChild(z);
	b=document.getElementById("principal");
	
	//Finalment, crearem els div on s'hi afegirà els texts de l'ajuda, diàlegs i respostes.
	z=document.createElement('div');
	z.id="ajuda";
	z.style.display="none";
	b.appendChild(z);
	t=document.createElement('div');
	t.id="dialeg";
	t.style.display="none";
	b.appendChild(t);
	m=document.createElement('div');
	m.id="respostes";
	b.appendChild(m);
	afegeixSaltLinia("respostes"); //truc perquè l'ajuda funcioni en Opera
	
	if(Unitat==1){
		document.body.style.background="url(../../ninots/noia_objectius_t2.png) no-repeat fixed bottom right";
	}
	else if(Unitat==2){
		document.body.style.background="url(../../ninots/musculman2_t2.png) no-repeat fixed bottom right";
	}
	else if(Unitat==3){
		document.body.style.background="url(../../ninots/musculman_t2.png) no-repeat fixed bottom right";
	}
	else if(Unitat==4){
		document.body.style.background="url(../../ninots/noia2_t2.png) no-repeat fixed bottom right";
	}
	else if(Unitat==5){
		document.body.style.background="url(../../ninots/musculman3_t2.png) no-repeat fixed bottom right";
	}
	else if(Unitat==6){
		document.body.style.background="url(../../ninots/noia3_t2.png) no-repeat fixed bottom right";
	}
	else if(Unitat==7){
		document.body.style.background="url(../../ninots/musculman4_t2.png) no-repeat fixed bottom right";
	}
	else if(Unitat==8){
		document.body.style.background="url(../../ninots/musculman5_t2.png) no-repeat fixed bottom right";
	}
	else if(Unitat==9){
		document.body.style.background="url(../../ninots/noia4_t2.png) no-repeat fixed bottom right";
	}
	else if(Unitat==10){
		document.body.style.background="url(../../ninots/musculman6_t2.png) no-repeat fixed bottom right";
	}
	
}


/*----------------------------------------Funcions per generar i esborrar les ajudes i diàlegs-------------------------------------------------------*/

//Al clicar en l'icona de tancar diàleg, es crida aquesta funció que elimina els nodes fills generat
//per l'afegeixDiàleg
function borrarDialeg(){
	x=document.getElementById("dialeg");
	x.style.display="none";
	while(x.hasChildNodes() ){
		x.removeChild(x.firstChild);
	}
	dialegobert=0; //reinicialitza el botó de diàlleg
}

//Es fa servir quan es fa click en el botó Text. Genera el quadre i el mostra.
function afegeixDialeg(Dialeg){
	if (dialegobert==0){
		y=document.getElementById("dialeg");
		y.style.display="block";
		z=document.createElement("img");
		z.onclick=borrarDialeg; 
		z.src="../../imatges/cancel.png";
		z.className='dreta';
		y.appendChild(z);
		x=document.createElement('br');
		y.appendChild(x);
		//No fem servir afegirImatge, perquè en IE al crear un div buit (en afegirImatge), genera un espai en blanc!
		bandera=document.createElement("img");
		bandera.src="../../imatges/flag-catala.png";
		bandera.className="dibu";
		y.appendChild(bandera);
		x=document.createTextNode(Dialeg);
		//bastard no es fa servir, però és necessari per poder fer les operacions.
		bastard=document.createElement("HOLA");
		while(x.nodeValue.indexOf('\n')!=-1){
			bastard.appendChild(x);
			x.splitText(x.nodeValue.indexOf('\n'));
			x1=document.createTextNode(x.nextSibling.nodeValue);
			y=document.getElementById("dialeg");
			y.appendChild(x);
			t=document.createElement('br');
			y.appendChild(t);
			x=x1;
			x.deleteData(0,1);
		}
		y=document.getElementById("dialeg");
		y.appendChild(x);
		Neteja("dialeg"); //necessari per posar bé les banderes
		dialegobert=1;  //al borrar el diàleg, es posarà a 0
	}
else{
borrarDialeg();
}
}

//Al clicar en l'icona de tancar l'ajuda, es crida aquesta funció que elimina els nodes fills generat
//per l'afegeixAjuda
function borrarAjuda(){
	x=document.getElementById("ajuda");
	x.style.display="none";
	while(x.hasChildNodes() ){
		x.removeChild(x.firstChild);
	}
	delete(x);
	ajudaobert=0; //reinicialitza el botó d'ajuda.
}

//Es fa servir quan es fa click en el botó Ayuda/Help. Genera el quadre i el mostra.
function afegeixAjuda(Ajuda){
	if (ajudaobert==0){
		y=document.getElementById("ajuda");
		y.style.display="block";
		var y2=document.createElement('div');
		y.appendChild(y2);
		y2.id="ajuda2";
		z=document.createElement("img");
		z.onclick=borrarAjuda; 
		z.src="../../imatges/cancel.png";
		z.className='dreta';
		y2.appendChild(z);
		x=document.createElement('br');
		//Primer es posa l'ajuda en castella
		y2.appendChild(x);
		//No fem servir afegirImatge, perquè en IE al crear un div buit (en afegirImatge), genera un espai en blanc!
		bandera=document.createElement("img");
		bandera.src="../../imatges/flag-spanish.png";
		bandera.className="dibu";
		y2.appendChild(bandera);
		x=document.createTextNode(Ajuda);
		//bastard no es fa servir, però és necessari per poder fer les operacions.
		bastard=document.createElement("HOLA");
		bastard.appendChild(x);
		if (x.nodeValue.indexOf('\n')!=-1){
			x.splitText(x.nodeValue.indexOf('\n'));
			m=document.createTextNode(x.nextSibling.nodeValue+" ");
			y=document.getElementById("ajuda2");
			x.nodeValue=x.nodeValue+" ";
			y.appendChild(x);
			//Un cop afegit el text, a continuació s'ha de crear el link amb la demo de l'exercici
			z=document.createElement("a");
			if (linkajuda==1){
				z.href="../../Demos/demo3cas.html";
			}
			else if (linkajuda==2){
				z.href="../../Demos/demo4cas.html";
			}
			else if(linkajuda==3){
				z.href="../../Demos/demo7cas.html";
			}
			else if(linkajuda==4){
				z.href="../../Demos/demo2cas.html";
			}
			else if(linkajuda==5){
				z.href="../../Demos/demo9cas.html";
			}
			else if(linkajuda==6){
				z.href="../../Demos/demo2cas.html";
			}
			if(linkajuda>0){
				z.charset="Demo";
				j=document.createTextNode("[Demo]");
				z.appendChild(j);
				y.appendChild(z);
			}
			Neteja("ajuda2"); //necessari per posar bé les banderes
			x=document.createElement('br');
			//Ara es crea el text de l'ajuda en anglès
			y.appendChild(x);
			bandera=document.createElement("img");
			bandera.src="../../imatges/flag-english.png";
			bandera.className="dibu";
			y.appendChild(bandera);
			m.deleteData(0,1);
			x=m;
			y=document.getElementById("ajuda2");
			y.appendChild(x);
			z=document.createElement("a");
			if (linkajuda==1){
				z.href="../../Demos/demo3eng.html";
			}
			else if (linkajuda==2){
				z.href="../../Demos/demo4eng.html";
			}
			else if(linkajuda==3){
				z.href="../../Demos/demo7eng.html";
			}
			else if(linkajuda==4){
				z.href="../../Demos/demo2eng.html";
			}
			else if(linkajuda==5){
				z.href="../../Demos/demo9eng.html";
			}
			else if(linkajuda==6){
				z.href="../../Demos/demo2eng.html";
			}
			if(linkajuda>0){
			z.charset="Demo";
			j=document.createTextNode("[Demo]");
			z.appendChild(j);
			y.appendChild(z);
			}
		}
		Neteja("ajuda2"); //necessari per posar bé les banderes
		ajudaobert=1; //al borrar l'ajuda, es posarà a 0
	}	
else{
borrarAjuda();
}
}

/*----------------------------------------Fi funcions per generar i esborrar les ajudes i diàlegs-----------------------------------------------------*/

/*----------------------------------------Funcions d'estilisme i generar botons--------------------------------------------------------------------------*/

//Crea el botó per mostrar a la pàgina.
function afegeixBoto(nom,identificador,funcio){
	var x;
	x=document.createElement('button');
	 x.id=nom.toLowerCase().replace(/['&:;,áéíóöúñ\/\-\.\(\)¡!]/g,'');
	
	x.appendChild(document.createTextNode(nom)); 
	x.style.lineHeight = "1.2em";
	/*Depenent del navegador caldrà fer-ho d'una manera o una altra..*/
	if ((!document.all)&&(document.getElementById))
		x.setAttribute('onclick',funcio);          
	if ((document.all)&&(document.getElementById))
		x['onclick']=new Function(funcio);
	document.getElementById(identificador).appendChild(x);
};

//Crea l'etiqueta b d'afegir la negreta i mostra per pantalla el text.
function afegeixTextNegreta(text,identificador){
	var y=document.createElement("b"),x=document.createTextNode(text);
	y.appendChild(x);
	document.getElementById(identificador).appendChild(y);
	if (prova!=null){
			prova.appendChild(y);
	}
};

//Crea l'etiqueta i d'afegir la cursiva i mostra per pantalla el text.
function afegeixTextCursiva(text,identificador){
	var y=document.createElement("i"),x=document.createTextNode(text);
	y.appendChild(x);
	document.getElementById(identificador).appendChild(y);
	if (prova!=null){
			prova.appendChild(y);
	}
};

//Afegeix el Text del paràmetre d'entrada al document.
function afegeixText(text,identificador){
	if(document.getElementById && document.createElement){
		var x=document.createTextNode(text);
		document.getElementById(identificador).appendChild(x);
		if (prova!=null){
			prova.appendChild(x);
		}
	}
	else alert('El teu navegador no accepta DOM de nivell 1');
};

//Funció feta servir en els diccionaris, afegeix un text i un salt de línia.
function afegeixTextSL(text,identificador){
	if(document.getElementById && document.createElement){
		var x=document.createTextNode(text);
		document.getElementById(identificador).appendChild(x);
		afegeixSaltLinia(identificador);
	}
	else alert('El teu navegador no accepta DOM de nivell 1');
};

//Fa un salt de línia.
function afegeixSaltLinia(identificador){
	if(document.getElementById && document.createElement){
		var x=document.createElement('br');
		document.getElementById(identificador).appendChild(x);
		if (prova!=null){
			prova.appendChild(x);
		}
	}
	else alert('El teu navegador no accepta DOM de nivell 1');
};

//Tal i com indica, elimina tots els fills de l'identificador (normalment és principal).
function NetejaPantalla(identificador){
	var el=document.getElementById(identificador);
	while (el.hasChildNodes())  
		el.removeChild(el.firstChild);
	delete(el);
};

/*Funcions per afegir paràgrafs i tancar-los (etiqueta p del codi html)*/

function afegeixParagraf(text,identificador){
	if(document.getElementById && document.createElement){
		var x=document.createElement('p');
		document.getElementById(identificador).appendChild(x);
		var y=document.createTextNode(text);
		document.getElementById(identificador).appendChild(y);
		if (prova!=null){
			prova.appendChild(x);
			prova.appendChild(y);
		}
	}
	else alert('El teu navegador no accepta DOM de nivell 1');
};

function afegeixTancarParagraf(identificador){
	if(document.getElementById && document.createElement){
		x=document.createElement('\p');
		document.getElementById(identificador).appendChild(x);
		if (prova!=null){
			prova.appendChild(x);
		}
	}
	else alert('El teu navegador no accepta DOM de nivell 1');
};

/*----------------------------------------Fi funcions d'estilisme i generar botons--------------------------------------------------------------------*/

function isObject(o) {
	return (typeof(o)=="object");
};
function isArray(o) {
	return (isObject(o) && (o.length) &&(!isString(o)));
};
function isFunction(o) {
	return (typeof(o)=="function");
};
function isString(o) {
	return (typeof(o)=="string");
};

//Crea els desplegables dels exercicis.
function afegeixSelect(contingut,identificador,tipusselect){
	var x,op,i;

	linkajuda=1; //definim el tipus d'exercici per identificar-lo en l'enllaç de la demo
	x=document.createElement('select');
	x.setAttribute('position','relative');
	x.setAttribute('id',ident);
	x.className='selectnocorregit';
	x.onfocus=function(){
		var im;
		this.className='selectnocorregit';
		im=document.getElementById("dibu"+this.id);
		im.className='imatgeinvisible';
		im.alt="";
		im.src=invisible;
	}
	for(i=0;i<contingut.length;i++){
	
		op=document.createElement('option');

		

		op.text=contingut[i];
		op.value=i;		
		try{
				x.add(op, null); // standards compliant; doesn't work in IE
		}
		catch(ex){
				x.add(op); // IE only
		}
	}
	//Afegim el desplegable
	document.getElementById(identificador).appendChild(x);
	if (prova!=null){
			prova.appendChild(x);
	}

	x.options[0].selected=1;
	//Segons el paràmetre d'entrada, determina si és un desplegable de múltiples opcions o no.
	if (tipusselect==1){ 
		x.onchange=UnicaOpcio;
	}
	
	//Afegim la imatge invisible al costat del desplegable, que després serà substituïda per ok o ko.
	x=document.createElement('img');
	x.src=invisible;
	x.className='imatgeinvisible';
	x.id="dibu"+ident++;
	
	document.getElementById(identificador).appendChild(x);
	if (prova!=null){
			prova.appendChild(x);
	}
};

/*Funció cridada pel select, si s'executa indica que entre tots els 
desplegables de l'exercici, no pot haver seleccionades opcions repetides */
function UnicaOpcio(){
	var x=0,element;
	while(x<ident)
	{
		element=document.getElementById(x);
		if (element.id!=this.id){
			if ((this.value)==(element.value)){
				element.options[0].selected=1;
			}
		}
		x++;
	}
};

/*Funcions per trobar la posició X i Y per posar el teclat guiri*/
function findPosX(obj)
{
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
    if(obj.offsetParent)
		while(1)
        {
			curtop += obj.offsetTop;
			if(!obj.offsetParent)
              break;
			obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}

/*Crea un textbox on l'usuari pot escriure la resposta*/
function afegeixEntradaText(identificador)
{
	var x,j;
	linkajuda=2; //Indica el tipus d'exercici per la demo
	x=document.createElement('input');
	x.className="textboxnocorregit";
	x.setAttribute('type','text'); 
	x.setAttribute('style','position:relative');
	x.setAttribute('id',ident);

	//Al fer click en el quadre, al costat ha d'aparèixer la icona que permet activar el teclat guiri
	x.onfocus=function(){
		var j,im;
		this.className='textboxnocorregit';
		im=document.getElementById("dibu"+this.id);
		im.className='imatgeinvisible';
		im.alt="";
		im.src=invisible;
		
		j=document.getElementById(((this.id)*100)+100);
		j.style.display='inline';
		if((teclatpos>-1)&&(teclatpos!=this.id)){
			j=document.getElementById(teclatpos*100+100);
			j.style.display='none';
		}
		teclatpos=this.id;
		if ((tguiriafegit==1)&&(ultim!=this.id)){ 
			var t3;
			t3=document.getElementById('tguiri');
			t3.parentNode.removeChild(t3);
			tguiriafegit=0;
		}
	};

	//La llargada del TextBox vé determinada per la longitud de la solució a escriure.
	if(isArray(Solucio[ident])) x.size=Solucio[ident][0].length+3;
	else x.size=Solucio[ident].length+3;
	//Afegim el TextBox al document
	document.getElementById(identificador).appendChild(x);
	if (prova!=null){
		prova.appendChild(x);
	}
	//Com en el Select, també s'afegeix la imatge invisible.
	x=document.createElement('img');
	x.src=invisible;
	x.className='imatgeinvisible';
	x.id="dibu"+ident++;
	document.getElementById(identificador).appendChild(x);
	if (prova!=null){
		prova.appendChild(x);
	}

	//Ara es crea la icona del teclat, i al fer click a sobre d'ella s'ha d'activar i desactivar el teclat (toogle).
	x=document.createElement('img');
	x.src=teclat.src;
	x.id=(ident)*100; 
/*
Fem en el id del teclat +1 i * per 100 per generar un identificador únic pel teclat. El procés invers és dividir
per 100 i -1.
Sabem que no és una solució elegant, però tot i així és la més senzilla (es suposa que no hi haurà un exercici
amb 100 o més TextBox, i donat el cas sempre es podrien afegir 0's a la multiplicació).
*/
	x.style.display='none';
	x.onclick=function(){
		if(tguiriafegit==0){ //mostrar el teclat
			ultim=((this.id/100)-1);
			//Busca coordenades
			tb.style.top=20+findPosY(this)+'px';
			tb.style.left=findPosX(this)+'px';
			document.getElementById('inserta').appendChild(tb);
			tguiriafegit=1;
			tb.display="block";//afegit per problema opera
		}
		else{ //eliminar el teclat existent
			var t3=document.getElementById('tguiri');
			t3.display="none"; //afegit per problema opera
			t3.parentNode.removeChild(t3);
			tguiriafegit=0;
			if(ultim!=((this.id/100)-1)){
				document.getElementById('inserta').appendChild(tb);
				//Busca coordenades
				tb.style.top=20+findPosY(this)+'px';
				tb.style.left=findPosX(this)+'px';
				tguiriafegit=1;
				ultim=(this.id/100)-1;
			}
		}
	};
	//I ho afegim al document.
	document.getElementById(identificador).appendChild(x);
	if (prova!=null){
		prova.appendChild(x);
	}
};

function afegeixImatge(localitzacio,identificador){
	var x=document.createElement('img');
	x.src=localitzacio;
	x.className="dibu";
	document.getElementById(identificador).appendChild(x);
	if (prova==null){
		prova=document.createElement('div');
		prova.id="test";
	}
	else{
		prova.appendChild(x);
	}
};

//Abans s'ha d'haver crdiat a un afegeixImatge!!!
function Neteja(identificador){
	if (prova!=null){
		document.getElementById(identificador).appendChild(prova);
		prova=null;
	}
	var x=document.createElement('br');
	x.clear="left";
	document.getElementById(identificador).appendChild(x);
};

//Afegeix la icona del so i carrega el fitxer de so.
function afegeixSo(id,identificador){
	var x,y;
	y=document.createElement('a');
	y.href="javascript://";
	y.id=id;

	x=document.createElement('img');
	x.src="../../imatges/sound.png";
	x.setAttribute('border','0');

	y.onclick=function(){
		engega(id);
	};
	
	y.appendChild(x);
	document.getElementById(identificador).appendChild(y);
	if (prova!=null){
		prova.appendChild(y);
	}
};

var idanterior=0;
//Controla la reproducció del so.
function engega(id){
	if (idanterior!=0){
		soundManager.stop(idanterior);
	}
	idanterior=id;
	soundManager.play(id,1);
};


/*Funció important ja que comprova si la resposta de l'usuari és bona o no.
Corretgeix els dos tipus de selects (amb o sense opció única) i els Textbox
No corregeix aquí els exercicis de RadioButton ni els d'ordenar.*/
function corretgeix(){
	var x=0,element,a,j,fi,s,i,correctes=0,incorrectes=0;
	
	//Primer de tot borrem el teclat extranger si existeix
	if (tguiriafegit==1){ 
		var t3;
		t3=document.getElementById('tguiri');
		t3.parentNode.removeChild(t3);
		tguiriafegit=0;
	}
	//Fi borrat teclat

	while(x<ident)
	{
		element=document.getElementById(x); //recollim l'element amb l'identificador x
		if(isArray(Solucio[x])) {//serveix pels casos que hi ha més d'una opció correcta
			a=0;
			fi=0;
			if (element.nodeName=="SELECT"){
				while ((Solucio[x].length>a)&&(fi==0)){
					for (i = element.length - 1; i>=0; i--){
						if (element.options[i].selected){
						s=element.options[i];
						}
					}
					if (s.text==Solucio[x][a]){
						fi=1;
					}
					a++;
				}
			}
			else{	
				while ((Solucio[x].length>a)&&(fi==0)){			
					if(element.value==Solucio[x][a]) fi=1;
					a=a+1;
				}
			}
			if (fi==1){/*Si esta ben resolt*/
				if(element.nodeName=='SELECT') element.clasName='selectcorrecte';
				else element.className='textboxcorrecte';
				element=document.getElementById("dibu"+x); 
				element.className='imatgecorrecte';
				element.src=ok;
				element.alt="correcte";
				correctes++;
			}
			else{			
				if(element.nodeName=='SELECT') element.clasName='selectincorrecte';
				else element.className='textboxincorrecte';
				element=document.getElementById("dibu"+x);
				element.className='imatgeincorrecte';
				element.src=ko; 
				element.alt="incorrecte";
				incorrectes++;
			}
		}
		else{//si només te una opcio
			if (element.nodeName=="SELECT"){
				for (i = element.length - 1; i>=0; i--){
					if (element.options[i].selected){
						s=element.options[i];
					}
				}
				if (s.text==Solucio[x]){ //ho posem en verd
					s.className='selectcorrecte';
					element=document.getElementById("dibu"+x); 
					element.className='imatgecorrecte';
					element.src=ok;
					element.alt="correcte";
					correctes++;
				}
				else{ // ho posem en vermell
					s.className='selectincorrecte';
					element=document.getElementById("dibu"+x);
					element.className='imatgeincorrecte';
					element.src=ko; 
					element.alt="incorrecte";
					incorrectes++;		
				}
			}
			else{
				if((element.value==(Solucio[x]))){ //si el text es igual a la solucio posaho en verd
					element.className='textboxcorrecte';
					element=document.getElementById("dibu"+x); 
					element.className='imatgecorrecte';
					element.src=ok;
					element.alt="correcte";
					correctes++;
				}
				else{ // ho posem en vermell
					element.className='textboxincorrecte';
					element=document.getElementById("dibu"+x);
					element.className='imatgeincorrecte';
					element.src=ko; 
					element.alt="incorrecte";
					incorrectes++;
				}
			}
		}
		x++;
	}
	//Crida a la funció d'abaix per generar les estadístiques.
	puntua("respostes",correctes,incorrectes);
};

//Calcula i mostra a sota dels exercicis el nombre de respostes bones i dolentes i el temps que s'ha tardat en resoldre l'exercici.
var corregit=0;
function puntua(identificador,correctes,incorrectes){
	//Primer calcula el temps que s'ha tardat en fer l'exercici.
	var newdate = new Date();
	var fi=newdate.getTime();
	var temps=fi-inici;
	delete newdate;
	var node = document.getElementById(identificador);
	if(corregit){ //cas que abans ja s'havia corregit, eliminem els resultats anterior i afegim els nous.
		var respostes = document.getElementById("numrespostes");
		respostes.removeChild(respostes.lastChild);
		var x=document.createTextNode((correctes+incorrectes));
		respostes.appendChild(x);
		var encerts = document.getElementById("correctes");
		encerts.removeChild(encerts.lastChild);
		var x=document.createTextNode(correctes);
		encerts.appendChild(x);
		var errors = document.getElementById("incorrectes");
		errors.removeChild(errors.lastChild);
		var x=document.createTextNode(incorrectes);
		errors.appendChild(x);
		var encerror = document.getElementById("percentatge");
		encerror.removeChild(encerror.lastChild);
		var x=document.createTextNode(Math.round((correctes/(correctes+incorrectes))*100));
		encerror.appendChild(x);
		var tempsseg = document.getElementById("temps");
		tempsseg.removeChild(tempsseg.lastChild);
		var x=document.createTextNode(Math.round(temps/1000));
		tempsseg.appendChild(x);
	}
	else{ //primer cop que es calculen les estadístiques, aquí es creen.
		afegeixSaltLinia(identificador);
		
		calculadora=document.createElement("img");
		calculadora.src="../../imatges/calculator.png";
		calculadora.className="dibu";
		document.getElementById(identificador).appendChild(calculadora);
		afegeixTextNegreta("Nombre de respostes: ",identificador);
		var y=document.createElement("b"),x=document.createTextNode((correctes+incorrectes));
		y.id="numrespostes";
		y.appendChild(x);
		document.getElementById(identificador).appendChild(y);

		afegeixSaltLinia(identificador);
		amunt2=document.createElement("img");
		amunt2.src="../../imatges/thumb_up.png";
		amunt2.className="dibu";
		document.getElementById(identificador).appendChild(amunt2);
		afegeixTextNegreta("Correctes: ",identificador);
		var y=document.createElement("b"),x=document.createTextNode(correctes);
		y.id="correctes";
		y.appendChild(x);
		document.getElementById(identificador).appendChild(y);

		afegeixSaltLinia(identificador);
		avall=document.createElement("img");
		avall.src="../../imatges/thumb_down.png";
		avall.className="dibu";
		document.getElementById(identificador).appendChild(avall);
		afegeixTextNegreta("Incorrectes: ",identificador);
		var y=document.createElement("b"),x=document.createTextNode(incorrectes);
		y.id="incorrectes";
		y.appendChild(x);
		document.getElementById(identificador).appendChild(y);

		afegeixSaltLinia(identificador);
		pastis=document.createElement("img");
		pastis.src="../../imatges/chart_pie.png";
		pastis.className="dibu";
		document.getElementById(identificador).appendChild(pastis);
		afegeixTextNegreta("Percentatge encerts: ",identificador);
		var y=document.createElement("b"),x=document.createTextNode(Math.round((correctes/(correctes+incorrectes))*100));
		y.id="percentatge";
		y.appendChild(x);
		document.getElementById(identificador).appendChild(y);
		afegeixTextNegreta("%",identificador);
		
		afegeixSaltLinia(identificador);
		rellotge=document.createElement("img");
		rellotge.src="../../imatges/time.png";
		rellotge.className="dibu";
		document.getElementById(identificador).appendChild(rellotge);
		afegeixTextNegreta("Temps: ",identificador);
		var y=document.createElement("b"),x=document.createTextNode(Math.round(temps/1000));
		y.id="temps";
		y.appendChild(x);
		document.getElementById(identificador).appendChild(y);

		afegeixTextNegreta(" segons",identificador);
		//Marquem que ja hi és les estadístiques.
		corregit=1;
	}
};

/*Funció important, mostra les solucions de l'exercici a l'usuari.
Aquí les mostra pels TextBox i Selects (aquí no fa per solucions múltiples)*/
function soluciona()
{
var x=0,element,fi,a,i;

while(x<ident)
{
	element=document.getElementById(x); //recollim l'element amb l'identificador x
	fi=0;
	if(isArray(Solucio[x])){//serveix pels casos que hi ha més d'una opció correcta
		a=0;
		if (element.nodeName=="SELECT")
			{
				while((Solucio[x].length>a)&&(fi==0))
				{	
					for (i = element.length - 1; i>=0; i--) {
						s=element.options[i];
						if ((s.text==Solucio[x][a]))
						{
							s.selected=1;
							fi=1;
						}
					}
					a++;
				}
			}		
		//Comença el tros de codi per multiples opcions
		else
		while ((Solucio[x].length>a)&&(fi==0)){
			if(element.value==Solucio[x][a]) fi=1;
			a=a+1;
		}
		if(fi==0){
			element.value=Solucio[x][0];
		}
	}
	if (element.nodeName=="SELECT"){		
		for (i = element.length - 1; i>=0; i--){
			if (element.options[i].text==Solucio[x]){
				s=element.options[i];
				s.selected=1;
			}
		}
		
		if(s.className=='selectincorrecte'){
			s.className='selectnocorregit';
		}
		element=document.getElementById("dibu"+x); 
		if (s.className=='selectcorrecte'){
			element.src=ok; 
			element.alt="ok";
			element.className='imatgecorrecte';
		}
		else{
			element.src=invisible;
			element.alt="";
			element.className='imatgeinvisible';
		}
		x++;	
		}
		else 
		{
			element.value=Solucio[x];
			//Finalitza el codi per multiples opcions
			if(element.className=='textboxincorrecte'){ 
				element.className='textboxnocorregit';
				element=document.getElementById("dibu"+x);
				element.src=invisible; 
				element.alt="";
				element.className='imatgeinvisible';
			}
		x++;
		}
	}
};

/*Solucions pels casos en que un exercici tingui múltiples opcions*/
function solucionamultiple()
{
var x=0,element,fi,a;

while(x<ident)
{
	element=document.getElementById(x); //recollim l'element amb l'identificador x
	fi=0;
	if(isArray(Solucio[x])) {//serveix pels casos que hi ha més d'una opció correcta
		a=0;
		
	//Comença el tros de codi per multiples opcions
	while ((Solucio[x].length>a)&&(fi==0)){
			if(element.value==Solucio[x][a]) fi=1;
			a=a+1;
		}
		if(fi==0) element.value=Solucio[x][0];
		}
		else element.value=Solucio[x];
	//Finalitza el codi per multiples opcions
	
	if(element.className=='selectincorrecte'){ 
		element.className='selectnocorregit';
		element=document.getElementById("dibu"+x);
		element.src=invisible; 
		element.alt="";
		element.className='imatgeinvisible';
	}
	
	else if(element.className=='textboxincorrecte'){ 
		element.className='textboxnocorregit';
		element=document.getElementById("dibu"+x);
		element.src=invisible; 
		element.alt="";
		element.className='imatgeinvisible';
	}
	x++;
}
};

//A continuació posem funcions que es fan servir per corretgir exercicis de tipus d'ordenar

var primercop=0;
function solucionacolumna(){

var y,x;
if (primercop==0){
for(f=0;f<frases.length;f++){

	x=document.getElementById("solu"+f);
	x.className='soluarrosega';

	if(tipusArrosega==2){
		var pri,ult;
		tros=Solucio[f];
		ordenacio=new Array();//[frases[f][1]-i];	
		for(i=frases[f][0];i<frases[f][1];i++)
		{
			a=document.getElementById("dragDiv"+i);
			pri=tros.indexOf(a.firstChild.data);
			ult=tros.lastIndexOf(a.firstChild.data);
			if(pri==ult){ 
				ordenacio=ordenacio.concat(pri);
			}
		}
			var reconstruccio=0;
			var ele=0;
		while(reconstruccio<(Solucio[f].length))
		{
			var pos=0, min=1000, tro=0;
			for(var apt=0; apt<(ordenacio.length); apt++){
					if(ordenacio[apt]>=reconstruccio)
						{
							tro=1;
							if(min>ordenacio[apt]){
									min=ordenacio[apt];
									pos=apt;
								}
						}
				}
				if(tro==0) reconstruccio=(Solucio[f].length); 			
				else{	reconstruccio=ordenacio[pos]+1;

				i=frases[f][0]+ele;
				i=frases[f][0]+pos;
				a=document.getElementById("dragDiv"+i).firstChild.data;
				afegeixText(a,"solu"+f);
				afegeixSaltLinia("solu"+f);
				tro=0;
				ele++;
				}
		}
	}
	else{
		afegeixText(Solucio[f],"solu"+f);
	}
}
primercop=1;
}
};

function corretgeixcolumna(){
var a,i,text,f,correctes=0,incorrectes=0;
for(f=0;f<frases.length;f++)
{
	text="";
	for(i=frases[f][0];i<frases[f][1];i++)
	{
		a=document.getElementById("dragDiv"+i);
		text=text+a.firstChild.data;
	}	
	
	a=document.getElementById("dibu"+f);
	if (Solucio[f]==text){ 
		
		a.src=ok;
		a.className='imatgecorrecte';
		a.alt="correcte";
		correctes++;
	}
	else{ 
		a.alt="incorrecte";
		a.src=ko;
		a.className='imatgeincorrecte';
		incorrectes++;
	}
}
puntua("principal",correctes,incorrectes);
};

/*A partir d'ara venen totes les funcions que fan falta per poder fer
exercicis d'ordenar frases i columnes*/
function afegeixOrdenarFrase(contingut,posicio)
{
var i,x,prov=elementordena,y;

for(i=0;i<contingut.length;i++){
		
		afegeixTextIntercanviable(contingut[i],posicio,"dragDiv"+elementordena++);
   }
frases=frases.concat(new Array(new Array(prov,elementordena)));

x=document.createElement('img');
x.className='imatgeinvisible';
x.src=invisible;

x.id="dibu"+ident; document.getElementById(posicio).appendChild(x);
x=document.createElement('p');

x.id="solu"+ident++;
document.getElementById(posicio).appendChild(x);
};


function afegeixOrdenarColumna(contingut,posicio)
{
var i,x,prov=elementordena,y;

for(i=0;i<contingut.length;i++){
		afegeixTextCIntercanviable(contingut[i],posicio,"dragDiv"+elementordena++);
   }
frases=frases.concat(new Array(new Array(prov,elementordena)));

x=document.createElement('img');
x.src=invisible;
x.className='imatgeinvisible';

x.id="dibu"+ident; document.getElementById(posicio).appendChild(x);
x=document.createElement('p');

x.id="solu"+ident++;
document.getElementById(posicio).appendChild(x);
};

function afegeixTextIntercanviable(text,identificador,id){
	if(document.getElementById && document.createElement){
		
		var x=document.createElement('span'),y;
		
		x.id=id;
		y=(document.createTextNode(text)); 
		x.appendChild(y);
		x.className="testSquare";
	

	document.getElementById(identificador).appendChild(x);
	y='  ' ;
	x=document.createTextNode(y);
	document.getElementById(identificador).appendChild(x);
		
	}
};
	
	
function afegeixTextCIntercanviable(text,identificador,id){
	if(document.getElementById && document.createElement){
		
		var x=document.createElement('p'),y,j=document.createElement('p');
		j.id=id;


		y=(document.createTextNode(text)); 
		j.appendChild(y);
		
		j.className="testSquare";
		j.onmousedown= function(){document.getElementsByTagName("body")[0].className="dragging";  };
		j.onmouseup= function(){document.getElementsByTagName("body")[0].className=" "; };
	document.getElementById(identificador).appendChild(j);	
	}
};

//Deixant els ordenar, ara  ens centrem en exercicis de seleccionar rodones (radio button)
var radio=0;
var radioCount=0;
function afegeixRadioButton(Contingut,posicio){

/*Per evitar problemes amb imatges i RadioButton, es finalitza el div"test"*/
if (prova!=null){
	if(prova.hasChildNodes()){
		document.getElementById(posicio).appendChild(prova);
	}
	prova=null;
}

var ident,x,classe,
y=document.createElement("form");
y.id="f"+radio;
y.onfocus=function(){

var im;
im=document.getElementById("dibu"+(this.id.slice('1')));
im.className="imatgeinvisible";
im.src=invisible;
im.alt="";

}
y.setAttribute('disabled',false);

	if(isArray(Contingut)){
	classe="radio"+radio;
	document.getElementById(posicio).appendChild(y);
		
	for(ident=0;ident<Contingut.length;ident++){
	//Codi necessari perquè funcioni en IE, Firefox i Opera
	//Cal declarar la x a null cada cop, ja que Firefox i Opera comproven que no ha estat creada.
		var x = null;
		var newLabel = document.createElement("label");
		newLabel.htmlFor = radioCount;
		newLabel.appendChild(document.createTextNode(Contingut[ident]));
		try{
		//Només ho executarà correctament IE, els altres navegadors generaran error
		x=document.createElement('<input type="radio" id='+radioCount+' name='+classe+'  value="'+Contingut[ident]+'" />');
		
		}
		catch(e){
		//Probablement falla perquè no fem servir IE
		}
		// Si entra en la condició, no està creat el radioButton i ara el creem de la manera estàndard
		if (!x){
		x=document.createElement("input");
		x.setAttribute('type','radio');
		x.setAttribute('name',classe);
		x.setAttribute('id',radioCount);
		x.setAttribute('value',Contingut[ident]);
		}
		//L'afegim i el mostrem
		
		y.appendChild(x);
		y.appendChild(newLabel);
		x.onfocus=function(){
					var vv=document.getElementById("dibu"+this.parentNode.id.slice('1'));
					vv.className="imatgeinvisible";
					vv.src=invisible;
					vv.alt="";

				}
		afegeixSaltLinia(y.id);
		radioCount++;
	}

		x=document.createElement('img');
		x.src=invisible;
		x.className='imatgeinvisible';
		x.id="dibu"+radio++;
		y.appendChild(x);
		

	}
	else{
		alert("Error");
	}

};

function getRadioButtonSelectedValue(ctrl)
{	
    for(i=0;i<ctrl.length;i++)
        if(ctrl[i].checked) return ctrl[i].value;
};


var primercopradio=0;
function solucionaradio(){
	var num2,element,x,y;
	if (primercopradio==0){
		for(num2=0;num2<radio;num2++)
		{
			element=document.getElementById("f"+num2); 
			y=document.createElement('div');
			
			x=document.createTextNode(Solucio[num2]);
			y.className='solucioradio';
			y.appendChild(x);
			element.appendChild(y);
			
		}
	primercopradio=1;
	}
};


function corregirradio(){
var num2=0,element,correctes=0,incorrectes=0;

for(num2=0;num2<radio;num2++){
	
		if(Solucio[num2]==getRadioButtonSelectedValue(document.getElementById("f"+num2))){
			element=document.getElementById("dibu"+num2); 
			element.src=ok;
			element.alt="correcte";
			element.className='imatgecorrecte';
			correctes++;
		}
		else{
			element=document.getElementById("dibu"+num2); 
			element.src=ko;
			element.alt="incorrecte";
			element.className='imatgeincorrecte';
			incorrectes++;
		}
}
puntua("principal",correctes,incorrectes);
};

//afegim contador de temps
var thisdate = new Date();
var inici= thisdate.getTime();

function afegeixExercici(Contingut,tipus,solu,posicio)
{
/* Contingut és el que es veurà a la web, és a dir tots els continguts de les diferents etiquetes
Tipus dòna el tipus d'etiquetes.
solu és la solucio de l'exercici
posicio el lloc on s'inserira tot l'exercici
Contingut i tipus son un array del mateix tamany, encara que tinguem alguna etiqueta sense contingut, en aquest cas entrarem com a 
contingut ""
*/
var x;

if (capcalera_ok==0){
 	genera_capcalera();
	capcalera_ok=1;
}
if (Contingut.length!=tipus.length)
	alert("Problema en el pas de parametres "+Contingut.length+" "+tipus.length);
	
	Solucio=solu;

for(x=0;x<Contingut.length;x++){
	if(tipus[x]=="Select") afegeixSelect(Contingut[x],posicio,0);
	if(tipus[x]=="Paragraf") afegeixParagraf(Contingut[x],posicio);
	if(tipus[x]=="Text") afegeixText(Contingut[x],posicio);
	if(tipus[x]=="SaltLinia") afegeixSaltLinia(posicio);
	if(tipus[x]=="Neteja") Neteja(posicio);
	if(tipus[x]=="TextNegreta") afegeixTextNegreta(Contingut[x],posicio);
	if(tipus[x]=="TextCursiva") afegeixTextCursiva(Contingut[x],posicio);
	if(tipus[x]=="Imatge") afegeixImatge(Contingut[x],posicio);
	if(tipus[x]=="EntradaText") afegeixEntradaText(posicio);
	if(tipus[x]=="TancarParagraf") afegeixTancarParagraf(posicio);
	if(tipus[x]=="SelectU") afegeixSelect(Contingut[x],posicio,1);
	if(tipus[x]=="So")  afegeixSo(Contingut[x],posicio);
	if(tipus[x]=="OrdenarFrase"){ afegeixOrdenarFrase(Contingut[x],posicio); linkajuda=3; tipusArrosega=1;}
	if(tipus[x]=="OrdenarColumna"){ afegeixOrdenarColumna(Contingut[x],posicio); linkajuda=3; tipusArrosega=2;}
	if(tipus[x]=="RadioButton"){ afegeixRadioButton(Contingut[x],posicio); linkajuda=6;}

}

//AFEGIM DEMO EN CATALA AL COSTAT DE L'exercici

if(demo_ok==0){
	y=document.getElementById("Exercici");
	z=document.createElement("a");

	if (linkajuda==1){
		z.href="../../Demos/demo3cat.html";
	}
	else if (linkajuda==2){
		z.href="../../Demos/demo4cat.html";
	}
	else if(linkajuda==3){
		z.href="../../Demos/demo7cat.html";
	}
	else if(linkajuda==4){
		z.href="../../Demos/demo2cat.html";
	}
	else if(linkajuda==5){
		z.href="../../Demos/demo9cat.html";
	}
	else if(linkajuda==6){
		z.href="../../Demos/demo2cat.html";
	}
	if(linkajuda>0){
		z.charset="Demo";
		j=document.createTextNode("[Demo]");
		z.appendChild(j);
		demo_ok=1;
		y.appendChild(z);
	}
}
};

