var M_INDEPENDIENTE=0;
var M_SCO=1;
var M_MAIL=2;

var R_COMBO=1;	
var R_INPUT=2;
var R_CHECK=3;
var R_RADIO=4;
var R_EMAITZAK=5;
var R_ANITZ = 6;
var R_ESALDI = 7;
var hutsKop = 0;

function Ejercicio(titulo,instrucciones,modo,autozuzenketa){
	this.id=__idObj++;
	this.preguntas=new Array();
	this.titulo=titulo;
	this.instrucciones=instrucciones;
	this.addPregunta=addPregunta;
	this.getPregunta=getPregunta;
	this.isCorrecta=isCorrecta;
	this.toScreen=toScreen;	
	this.getTitulo=getTitulo;
	this.getInstrucciones=getInstrucciones;
	this.corregir=corregir;
	this.putCorrectas=putCorrectas;
	this.modo=modo;
	if (autozuzenketa != null)
		this.autozuzenketa = autozuzenketa;
	else 
		this.autozuzenketa = true;
	this.corregirIndependiente=corregirIndependiente;
	this.corregirSco=corregirSco;
	this.startDate=null;
// Emaitza posible guztiak gordetzeko
	this.posibles = new Array()
	this.addPosibles=addPosibles;
	this.getAutozuzenketa = getAutozuzenketa;


	function addPosibles(gap){			
		for (i=0;i<gap.respuestas.length;i++){
			this.posibles[this.posibles.length]=gap.respuestas[i];		
		}
	}

	function addPregunta(pregunta){			
		this.preguntas[this.preguntas.length]=pregunta;		
	}
	function isCorrecta(idx){
		var p=this.preguntas[idx];
		return p.isCorrecta();
	}	
	function getPregunta(idx){
		return this.preguntas[idx];
	}
	function getTitulo(){
		return this.titulo;
	}
	function getInstrucciones(){
		return this.instrucciones;
	}	
	function getAutozuzenketa(){
		return this.autozuzenketa;
	}	
	function toScreen(){
		var i=0;
		var html = "";
		if (this.getInstrucciones() != "") 
			html = html + "<div class='instrukzioak'>" + this.getInstrucciones() + "</div>";
		if (this.getTitulo() != "") 
			html = html + "<H1>" + this.getTitulo() + "</H1>";
		html = html + "<form name='frm' method='post' action='javascript:corregir(this)'>";
		for(i=0;i<this.preguntas.length;i++){
			var p=this.preguntas[i];			
			html+="<div class='preguntasdiv'>";
			html+=p.getHtml(this.posibles);
			html+="</div>";
		}		
		html+="<div class='botoiak'>";
		if (this.autozuzenketa) {
			html+="<input name='elBoton' id='elboton' type='submit' value='Zuzendu'>";
			html+="<input name='elBoton2' id='elboton2' type='button' value='Erantzun zuzenak ikusi' disabled='true' onclick='ejercicio.putCorrectas()'>";
		} else {
			html+="<input name='elBoton2' id='elboton2' type='button' value='Ereduak ikusi' onclick='ejercicio.putCorrectas()'>";
		}
		html+="</div></form>";
		if (this.modo == "R_COMBO") {
			html+="<div class='inpr_aukerak'><div class='izenburua'>Hutsuneetako aukerak</div><ol>";
			for(i=0;i<this.preguntas.length;i++){
				var p=this.preguntas[i];			
				html+=p.getInprHtml(this.posibles);
			}		
			html+="</ol></div>";
		} else { 
			if (this.modo == "R_ANITZ") {
				html+="<div class='inpr_aukerak'><div class='izenburua'>Hutsuneetako aukerak</div><ul>";
				var posibles = this.posibles;
				posibles = shuffle(posibles);
	    			for (i=0;i<posibles.length;i++){    			
    		      		 html+="<li>" + posibles[i] + "</li>";
	  			}
				html+="</ul></div>";
			}
		}
		html += "</div>";
		var capa = document.getElementById("div_pregunta");
		capa.innerHTML = html;
//		div_pregunta.innerHTML=html;

		this.startDate=new Date();		
	}
	function putCorrectas(){		
		var i,k;
		var g;		
		for (i=0;i<this.preguntas.length;i++){
			g=this.preguntas[i].elemento;			
			for (k=0;k<g.length;k++){
				if (g[k].TIPO==TIPO_GAP){
					g[k].putCorrecta();
				}
			}
		}
	}			
	function corregirIndependiente(){
		var i=0;
		var contador=0;	
		var zenbat = 0;
		var zuzenak = 0;	
		for(i=0;i<this.preguntas.length;i++){
			if (this.preguntas[i].isCorrecta()==true) contador++;
//			alert("Pregunta ("+i+") "+this.preguntas[i].isCorrecta());
			zenbat = zenbat + this.preguntas[i].getNumGaps();
			zuzenak = zuzenak + this.preguntas[i].getGapsOk();
		}
		alert((intentos + 1) + ". saiakera:\n" + zenbat+" hutsune betetzeko, "+zuzenak+" ondo.\n\n\n(" + reintentos + " saiakera dituzu)\n");
//		if (this.preguntas[0].getGapsOk()== 1){
//			alert(this.preguntas[0].getNumGaps()+" hutsune betetzeko, "+this.preguntas[0].getGapsOk()+" ondo.");
//		}else{
//			alert(this.preguntas[0].getNumGaps()+" hutsune betetzeko, "+this.preguntas[0].getGapsOk()+" ondo.");
//		}
		intentos++;
		if (zuzenak==zenbat){
			frm.elBoton.disabled=true;
			frm.elBoton2.disabled=true;
			alert("Ariketa zuzena");			
		} else if (intentos>=reintentos) {
			frm.elBoton.disabled=true;
			frm.elBoton2.disabled=false;
			alert("Ez dago saiakera gehiago");
		}
	}
	function corregirSco(){
		this.corregirIndependiente();		
		if ( (intentos>=reintentos) || (this.preguntas[0].getGapsOk()==this.preguntas[0].getNumGaps())) {
			var score=(this.preguntas[0].getGapsOk()/this.preguntas[0].getNumGaps())*100;			
			var ahora=new Date();
			var tiempo_total=Math.round((ahora.getTime()-this.startDate.getTime())/1000);
			if (isNaN(score) || score<0) score=0;
			if (isNaN(tiempo_total) || tiempo_total<0) tiempo_total=0;
			send2Ims(score,tiempo_total);
		}
	}	
	function corregir(){
			this.corregirIndependiente();
//		var LMS=FindAPI(parent);
//		if (LMS!=null) {			
//			this.corregirSco();					
//		} else {
//			this.corregirIndependiente();
//		}				
	}
}
var TIPO_TEXTO=1;
var TIPO_GAP=2;
var intentos=0;
function Pregunta(){
	this.id=__idObj++;
	this.elemento=new Array();
	this.addElemento=addElemento;	
	this.getHtml=getHtml;
	this.getInprHtml=getInprHtml;
	this.isCorrecta=isCorrecta;
	this.getNumGaps=getNumGaps;	
	this.getNum=getNum;		
	this.getGapsOk=getGapsOk;	
	function getNum(tipo){
		var i;
		var cuantos=0;
		for (i=0;i<this.elemento.length;i++){
			var e=this.elemento[i];
			if (e.getTipo()==tipo){
				cuantos++;
			}
		}
		return cuantos;
	}
	function getGapsOk(){
		var i;
		var cuantos=0;
		for (i=0;i<this.elemento.length;i++){
			var e=this.elemento[i];
			if ((e.getTipo()==TIPO_GAP) && (e.isCorrecta())){
				cuantos++;
			}
		}
		return cuantos;
	}
	function getNumGaps(){
		return this.getNum(TIPO_GAP);
	}
	function addElemento(obj){
		this.elemento[this.elemento.length]=obj;
	}
	function getHtml(posibles){		
		var i;
		var html="";

// Emaitza guztiak erakutsi
		if (this.elemento[1].render == "R_EMAITZAK") {
//			posibles.sort(shuffle);
			posibles = shuffle(posibles);
			html="<div class='posibleak'>";
	    		for (i=0;i<posibles.length;i++){    			

	    	      	 html+="<span>"+posibles[i]+"</span>";
	  		}
	  		html+="</div>";
		}

		for (i=0;i<this.elemento.length;i++){			
			var e=this.elemento[i]			
			html+=e.getHtml(posibles);
			if (html!="") html+="&nbsp;";
		}

		return html;				
	}

	function getInprHtml(posibles) {
		var html = "";
// Inprimatzeko bertsiorako bukaeran hutsune aukerak erakutsi
		if (this.elemento[1].render == "R_COMBO") {
			for (i=0;i<this.elemento.length;i++){			
				var e=this.elemento[i];
				if (this.elemento[i].getTipo() == TIPO_GAP) {			
					html+=e.getInprHtml();
				}
			}
		}
		return html;
	}

	function isCorrecta(){
		var i;		
		for (i=0;i<this.elemento.length;i++){	
			var e=this.elemento[i];			
			if (e.getTipo()==TIPO_GAP){			
				if (!e.isCorrecta()) return false;
			}
		}
		return true;
	}
}
function Texto(){
	this.id=__idObj++;
	this.TIPO=TIPO_TEXTO;
	this.texto="";
	this.getHtml=getHtml;
	this.setTexto=setTexto;
	this.getTexto=getTexto;
	this.getTipo=getTipo;
	
	function setTexto(t){
		this.texto=t
	}
	function getTexto(){
		return this.texto;
	}
	function getHtml(){
		return this.texto;	
	}
	function getTipo(){
		return this.TIPO;
	}
}
function Gap(render){
	this.id=__idObj++;
	this.TIPO=TIPO_GAP;
	this.render=render;
	this.maxLength=0;
	this.pista="";
	this.respuestas=new Array();
	this.correcta=new Array();
	this.getHtml=getHtml
	this.getInprHtml=getInprHtml
	this.isCorrecta=isCorrecta
	this.addValor=addValor;	
	this.getTipo=getTipo;	
	this.putCorrecta=putCorrecta;		
	this.setPista=setPista;
	
	this.Feedback = "";
	this.setFeedback = setFeedback;
	this.getFeedback = getFeedback;

	this.defektuz = "";
	this.setDefektuz = setDefektuz;
	this.getDefektuz = getDefektuz;


	function setPista(p){
		this.pista=p;
	}
	function setFeedback(texto) {
		this.Feedback = texto;
	}
	function getFeedback() {
		return this.Feedback;
	}

	function setDefektuz(texto) {
		this.defektuz = texto;
	}
	function getDefektuz() {
		return this.defektuz;
	}

	function addValor(v,c){
		this.respuestas[this.respuestas.length]=v
		this.correcta[this.correcta.length]=c;		
		if (v.length>this.maxLength) this.maxLength=v.length;
	}	
	function getRender(){
		return this.render;
	}	
	function putCorrecta(){		
		var feed="";
//		if (this.getFeedback() != "") {
//			feed += "<img src='/jetspeed/images/laguntza.gif' align='absmiddle' hspace=5 onClick='feedBackErakutsi(" + this.id + ",event)' />";
//			feed += "<span class='feedback' style='visibility:hidden;' id='feedback_" + this.id + "'>" + this.getFeedback() + "</span>";
//		}
		valor = "";
		for (i=0;i<this.respuestas.length;i++){
			if (this.correcta[i]){
				if (valor!="") valor+=", ";
				valor+=this.respuestas[i];
			}
		}
		valor= feed + "("+valor+")";
		eval('div_id'+this.id+'.innerHTML="'+valor+'";');		
	}
	function getHtml(ordenRespuestas){
		var i;				
		var html="";
		
		var luzera = this.respuestas[0].length;
		var luz_osoa = luzera;
		luz_osoa = (luz_osoa / 70) + 1;
		if (luzera < 5) luzera = 5;
		if (luzera > 100) luzera = 100;

		ordenHueco=new Array();
		ordenHueco = this.respuestas.slice(0);
//		ordenHueco.sort();
		ordenHueco = shuffle(ordenHueco);
//		ordenRespuestas.sort();
		ordenRespuestas = shuffle(ordenRespuestas);
  		switch (this.render){
	  		case "R_INPUT":
			case "R_EMAITZAK":{		    		
		    	      	 html="<span name='id"+this.id+"' class='pregunta' onMouseOut=\"__eventMouseOut()\"onMouseOver=\"__eventMouseOver('"+this.pista+"')\">";

// Defektuzko datuen bidez betetzeko
		    	      	html+="<input name='id"+this.id+"' type='text' size=" + luzera + " value='" + this.getDefektuz() + "'>";
		    	      	html+="</span>";
		  		break;
	  		}
	  		case "R_ANITZ":{		
				hutsKop++;
				html="<span class='inpr_hutsune'>(" + hutsKop + ")</span><select name='id"+this.id+"'><option value='-1' selected></option>";
		    		for (i=0;i<ordenRespuestas.length;i++){    			
		    	      	 html+="<option value='"+ordenRespuestas[i]+"'>"+ordenRespuestas[i]+"</option>";
		  		}
		  		html+="</select>";
		  		break;
		  	}
	  		case "R_COMBO":
			default:{		
				hutsKop++;
				html="<span class='inpr_hutsune'>(" + hutsKop + ")</span><select name='id"+this.id+"'><option value='-1' selected></option>";
		    		for (i=0;i<ordenHueco.length;i++){    			
		    	      	 html+="<option value='"+ordenHueco[i]+"'>"+ordenHueco[i]+"</option>";
		  		}
		  		html+="</select>";
		  		break;
		  	}
		  	case "R_CHECK":{	
				html+="<span>";			
		    		for (i=0;i<ordenRespuestas.length;i++){		    	      	
		    		html+="<br><input name='id"+this.id+"' type='checkbox' value='"+ordenRespuestas[i]+"'>"+ordenRespuestas[i];
		  		}		  				  		
		  		html+="</span>";		  		
		  		break;
		  	}
		  	case "R_RADIO":{				
		    		for (i=0;i<ordenRespuestas.length;i++){
		    			html+="<br><input name='id"+this.id+"' type='radio' value='"+ordenRespuestas[i]+"'>"+ordenRespuestas[i];
		  		}
		  		html+="<br>";
		  		break;
		  	}
			case "R_ESALDI":{		    		
		    	      	html="<br /><span name='id"+this.id+"' class='pregunta'>";
		    	      	html+="<input name='id"+this.id+"' type='text' size=100 value='" + this.getDefektuz() + "'>";
		    	      	html+="</span><br />";
		  		break;
	  		}
			case "R_PARRAFO":{		    		
		    	      	html="<br /><span name='id"+this.id+"' class='pregunta'>";
		    	      	html+="<textarea name='id"+this.id+"' type='text' rows='" + luz_osoa + "' cols=100>" + this.getDefektuz() + "</textarea>";
		    	      	html+="</span><br />";
		  		break;
	  		}
	  	}
	  	
	  	html+="<b><span id='div_id"+this.id+"'></span></b>";
		if (this.render == "R_ESALDI") html+="<br />";
  		return html;
	}
	function isChecked(a,r,c){
		var i;		
		for (i=0;i<a.length;i++){
			if (a[i]==r) return c;
		}
		return false;
	}	
	function isCheckOk(a,r,c){
		var i;
		var correctas=0;
		var correctasUser=0;		
		for (i=0;i<r.length;i++) if (c[i]) correctas++;		
		for (i=0;i<r.length;i++){
			if (isChecked(a,r[i],c[i])) correctasUser++;
		}
		if (a.length>correctas) return false;
		else return (correctas==correctasUser);
	}
	function isCorrecta(){		
		var elegida="";
		var aElegida=new Array();
		if (((this.render=="R_INPUT") || (this.render=="R_COMBO")) || ((this.render=="R_ANITZ") || (this.render == "R_EMAITZAK")) || (this.render == "R_ESALDI")) {
			elegida=eval("frm.id"+this.id+".value");
		}else if (this.render=="R_RADIO") {
			var l,i;
			l=eval("frm.id"+this.id+".length");
			for (i=0;i<l;i++){
				var b=eval("frm.id"+this.id+"["+i+"].checked");
				if (b) elegida=eval("frm.id"+this.id+"["+i+"].value");
			}
		}else if (this.render=="R_CHECK") {
			var l,i;			
			l=eval("frm.id"+this.id+".length");
			for (i=0;i<l;i++){
				b=eval("frm.id"+this.id+"["+i+"].checked");				
				if (b) aElegida[aElegida.length]=eval("frm.id"+this.id+"["+i+"].value");				
			}
		}		
		var i;
		if (this.render=="R_CHECK"){
			return isCheckOk(aElegida,this.respuestas,this.correcta);
		}else{						
			if (elegida=="") return false;
			for (i=0;i<this.respuestas.length;i++){
				if (this.respuestas[i]==elegida) return this.correcta[i];
			}
		}
		//return true;
	}
	function getTipo(){
		return this.TIPO;
	}

	function getInprHtml(){
		var i;				
		var html="<li><ul>";
		
		ordenHueco=new Array();
		ordenHueco = this.respuestas.slice(0);
		ordenHueco = shuffle(ordenHueco);
    		for (i=0;i<ordenHueco.length;i++){    			
    	      	 html+="<li>"+ordenHueco[i]+"</li>";
  		}
		html += "</ul></li>";
  		return html;
	}

}

function shuffle(o) {
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x); return o; 
}

var __idObj=0;
///////////////////////////
function corregir(){
ejercicio.corregir();
}

function __eventMouseOver(q){		
//	div_alt.style.top=window.event.clientY;
//	div_alt.style.left=window.event.clientX;		
//	div_alt_texto.innerHTML=q;
//	div_alt.style.visibility='visible';	
}
function __eventMouseOut(){
//	div_alt.style.visibility='hidden';
}
////////////////////////////////////////////////////////////////
window.onunload=function descarga(){
//	ejercicio.corregirIndependiente();
//	var LMS=FindAPI();
//	if (LMS!=null){
//			var score=(ejercicio.preguntas[0].getGapsOk()/ejercicio.preguntas[0].getNumGaps())*100;			
//			var ahora=new Date();
//			var tiempo_total=Math.round((ahora.getTime()-ejercicio.startDate.getTime())/1000);
//			if (isNaN(score) || score<0) score=0;
//			if (isNaN(tiempo_total) || tiempo_total<0) tiempo_total=0;
//			send2Ims(score,tiempo_total);	
//	}
}

function feedBackErakutsi(zein, ev) {
var px, py, cadena, obj;
if (!document.all)
{
event = ev;
py = ev.pageY; 
px = ev.pageX;
}
else{
py = event.clientY +document.body.scrollTop; 
px = event.clientX +document.body.scrollLeft;
}

px = (px - 220) + "px";
py = py + "px";

	capa = document.getElementById("feedback_" + zein);
//	capa.style.position = "relative";
	capa.style.left = px;
//	capa.style.top = py;
	capa.style.visibility = "visible";
}
