var M_INDEPENDIENTE=0;
var M_SCO=1;
var M_MAIL=2;
var aktibo = "";
var aukeratua = "";

function Ejercicio(){
	this.id=__idObj++;
	this.preguntas=new Array();
	this.correctas = new Array();
	this.addPregunta=addPregunta;
	this.getPregunta=getPregunta;
	this.toScreen=toScreen;	
	this.corregir=corregir;
	this.putCorrectas=putCorrectas;
	this.corregirIndependiente=corregirIndependiente;
	this.corregirSco=corregirSco;
	this.startDate=null;
	this.contador = 0;
	this.cuantasColumnas = cuantasColumnas;
	this.titulos=new Array();
	this.addTitulo = addTitulo;

	this.getNumGaps=getNumGaps;	
	this.getGapsOk=getGapsOk;	

	function getNumGaps() {
		return (this.preguntas.length * (this.cuantasColumnas() - 1));
	}
	function getGapsOk() {
		return this.contador;
	}

	function addPregunta(pregunta){			
		this.preguntas[this.preguntas.length]=pregunta;
		zuzenak = pregunta.getCorrectas();
		for (i=0; i<zuzenak.length; i++) {
			if (this.correctas[i] == null) this.correctas[i] = new Array();
			this.correctas[i][this.correctas[i].length] = new Array(zuzenak[i],pregunta.id);
		}
	}
	function addTitulo(titulo) {
		this.titulos[this.titulos.length] = titulo;
	}
	function isCorrecta(idx){
		var p=this.preguntas[idx];
		return p.isCorrecta();
	}	
	function getPregunta(idx){
		return this.preguntas[idx];
	}
	function getInstrucciones(){
		return this.instrucciones;
	}	
	function cuantasColumnas() {
//		var cuantas = 0;
//		for (i=0; i < this.correctas.length; i++) {
//			if (this.correctas[i].length > cuantas) cuantas = this.correctas[i].length;
//		}
//		return cuantas;
		return this.correctas.length;
	}
	function toScreen(){		
		var i=0;
		var html = "";
		html = html + "<form name='frm' method='post' action='javascript:corregir(this)'>";
		html += "<table class='columnas' cellpadding=0 cellspacing=0 border=0><tr>";
		zenbat = this.cuantasColumnas();
		for (i=1; i < zenbat; i++) {
//			this.correctas[i].sort();
			this.correctas[i] = shuffle(this.correctas[i]);
		}
		if (this.titulos.length > 0) {
			html += "<tr>";
			if (this.titulos.length > 0) {
				html += "<th class='izenburuak' id='lehena'>" + this.titulos[0].getTexto() + "</th>";
				for (i=1; i<this.titulos.length; i++) {
					html += "<th class='izenburuak'>" + this.titulos[i].getTexto() + "</th>";
				}
			}
			html += "</tr>";
		}
		for (i=0; i<this.preguntas.length; i++) {
			var zuzenak = this.preguntas[i].getCorrectas();
			html += "<tr><th id='id" + this.preguntas[i].id + "' onClick='zutabeClick(this)' class='zutabe'>" + zuzenak[0] + "</th>";
			for (j=1; j < zenbat; j++) {
				html += "<td id='_" + this.preguntas[i].id + "_" + j + "' class='zutabe' onClick='aukeraClick(this)' value=" + this.correctas[j][i][1] + ">" + this.correctas[j][i][0] + "</td>";
			}
			html += "</tr>";
		}
		html += "</table>";
		html+="<div class='botoiak'>";
		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()'>";
		html+="</form></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];			
			g.putCorrecta();
		}
		frm.elBoton2.disabled=true;
	}			
	function corregirIndependiente(){
		var i=0;
		this.contador=0;		
		for(i=0;i<this.preguntas.length;i++){
			this.contador = this.contador + this.preguntas[i].isCorrecta();
		}
//		alert(this.getNumGaps()+" elementu ordenatzeko, "+this.getGapsOk()+" ondo.");
		alert((intentos + 1) + ". saiakera:\n" + this.getNumGaps()+" elementu sailkatzeko, "+this.getGapsOk()+" ondo.\n\n\n(" + reintentos + " saiakera dituzu)\n");
//		if (this.getGapsOk()== 1){
//			alert(this.getNumGaps()+" elementu ordenatzeko, "+this.getGapsOk()+" ondo.");
//		}else{
//			alert(this.getNumGaps()+" elementu ordenatzeko, "+this.getGapsOk()+" ondo.");
//		}
		intentos++;
		if (this.getGapsOk()==this.getNumGaps()){
			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();					
//		}
				
	}				
}
var intentos=0;
function Pregunta(columna,texto){
	this.id=__idObj++;
	this.columna = columna;
	this.correctas = new Array();
	this.addCorrecta = addCorrecta;
	this.getCorrectas = getCorrectas;
	this.getZuzentzeko = getZuzentzeko;
	this.isCorrecta = isCorrecta;
	this.putCorrecta = putCorrecta;
	this.getColumna = getColumna;
	this.getId = getId;

	function getId() {
		return this.id;
	}
	function getColumna(){
		return this.columna;
	}
	function addCorrecta(texto) {
		this.correctas[this.correctas.length] = texto;
	}
	function getCorrectas() {
		return this.correctas;
	}

	function getZuzentzeko(){		
	  	html="<td>&nbsp;<b><ul class='zuzenak' id='div_id"+this.id+"'></ul></b>" + "</td>";
		return html;
	}

	function isCorrecta() {
		var zuzenak = 0;
		for (i=1; i<this.correctas.length; i++) {
			id = "_" + this.id + "_" + i;
			capa = document.getElementById(id);
			if (capa.className == "zutabe_aukera") {
				if (capa.getAttribute("value") == this.id) zuzenak++;
			}
		}
		return zuzenak;
	}

	function putCorrecta() {
		for (i=1; i<this.correctas.length; i++) {
			id = "_" + this.id + "_" + i;
			capa = document.getElementById(id);
			capa.innerHTML += "<div class='zuzen'>" + this.correctas[i] + "</div>";
		}
	}
}

function Titulo(id,texto){
	this.setTexto = setTexto;
	this.getTexto = getTexto;
	this.getId = getId;
	if (!parseInt(id)) id =__idObj++;
	this.id = id;
	this.texto = texto;

	function getId() {
		return this.id;
	}
	function setTexto(texto) {
		this.texto = texto;
	}
	function getTexto(){
		return this.texto;
	}
}

function zutabeClick(zein) {
	if (aktibo != null) 
		aktibo.className = "zutabe";
	zein.className = "aukera";
//	aktibo = zein.cloneNode(true);
	aktibo = zein;
}

function aukeraClick(zein) {
	if (aktibo != null) {
		col_akt = getColumna(aktibo.id);
		col = getColumna(zein.id);
		if (col == 0) return false;
		if (col_akt == col) return false;
		fila_akt = getFila(aktibo.id);
		id = "_" + fila_akt + "_" + col;
		capa = document.getElementById(id);
		balio = zein.innerHTML;
		atrib = zein.getAttribute("value");
		zein.innerHTML = capa.innerHTML;
		zein.setAttribute("value",capa.getAttribute("value"));
		if (zein.className == "zutabe_aukera") zein.className = "zutabe";
		capa.innerHTML = balio;		
		capa.setAttribute("value",atrib);
		capa.className = "zutabe_aukera";
//		aktibo.className = "";
//		aktibo = null;
	}
}

function getFila(zein) {
	if (zein.indexOf("id") > -1)
		return zein.substring(2);
	else {
		ind = zein.lastIndexOf("_");
		return zein.substring(1,ind);
	}
}
function getColumna(zein) {
	if (zein.indexOf("id") > -1)
		return 0;
	else {
		ind = zein.lastIndexOf("_");
		return zein.substring(ind + 1);
	}
}

function ezabatu(zein) {
	var capa = document.getElementById("_" + zein.id);
//	capa.innerHTML = "";
	capa.parentNode.removeChild(capa);
	zein.className = "";
}

function garbitu(testua) {
	testua = testua.toUpperCase();
	testua = testua.replace(/<BR>/g,"");
	testua = testua.replace(/<BR\/>/g,"");
	testua = testua.replace(/<BR \/>/g,"");
	testua = testua.replace(/<P>/g,"");
	testua = testua.replace(/<\/P>/g,"");
	kar = unescape("%0A");
	var re=new RegExp(kar,'g');
	testua = testua.replace(re,"");
	kar = unescape("%0D");
	var re=new RegExp(kar,'g');
	testua = testua.replace(re,"");
//	testua = TrimLeft(testua);
//	testua = TrimRight(testua);
	testua = trim(testua);

	return testua;
}
function trim(cadena){ 
	for(i=0; i<cadena.length; ){
		if(cadena.charAt(i)==" ")
			cadena=cadena.substring(i+1, cadena.length);
		else break; 
	}
	for(i=cadena.length-1; i>=0; i=cadena.length-1){
		if(cadena.charAt(i)==" ") cadena=cadena.substring(0,i);
		else break; 
	}
	return cadena;
}

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.onsubmit=function descarga(){	
//	ejercicio.corregirIndependiente();
//	var LMS=FindAPI();
//	if (LMS!=null){
//			var score=(ejercicio.getGapsOk()/ejercicio.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);	
//	}
//}


