/*
 * Copyright (C) 1995-2011 by 4system Polska Sp. z o.o.
 * All Rights Reserved.
 */

componentMatchWords = function(name) {
	this.__componentText = __componentText;
	this.__componentText(name);

	this.interactions 		= true;
	this.interactionsType 	= "fill-in";  
};

componentMatchWords.prototype = new __componentText;
componentMatchWords.prototype.type = "componentMatchWords";

componentMatchWords.prototype.onCreate = function(obj) {
	this.captureMouseEvents();
	this.locked = false;
	this.pollQuestion = this.initDynamicContent(this.pollQuestion);
	this.choiceValues = this.initDynamicContent(this.choiceValues);
	this.inputTitle = this.initDynamicContent(this.inputTitle);
	
	var arrOpt = this.choiceValues.split("#|#");
	this.obj.dir = this.dir;

	var width = this.w - this.borderWidth*2-5;
	var t = '<span style="color:' + this.fontColor + 
			'; font-family:' + this.fontFamily + 
			'; font-size:' + this.convertFontSize(this.fontSize, "pt") +
			'px;">' + this.pollQuestion + '</span>';

	var indexArray = arrOpt[0].split("||");
	var choicesArray = arrOpt[1].split("||");
	this.correctAnswersList = choicesArray;

	t += '<table style="width:' + (width-20) + 'px;" id="match_words_tab_' + this.id + '" dir="'+this.dir+'">';
	
	if (this.answersCountToPass <= 0 || this.answersCountToPass > indexArray.length) this.answersCountToPass = indexArray.length; 
	if (this.answersCountToPass > this.randAnswersCount) this.answersCountToPass = this.randAnswersCount;
	
	this.drawedQuestionsNumbers = new Array();
	var isDrawed = new Array();
	for (var i=0; i<indexArray.length; i++) {
		this.drawedQuestionsNumbers[i] = -1;
		isDrawed[i] = 0;
	}
	
	var drawedCount = 0;
	if (PageInterface.GetValue("state") == psDesigning) this.randAnswersCount = indexArray.length;
	if (this.randAnswersCount <= 0 || this.randAnswersCount > indexArray.length) this.randAnswersCount = indexArray.length;
	
	while (drawedCount != this.randAnswersCount) 
	{
		var number = -1;
		if (PageInterface.GetValue("state") == psDesigning) {
			number = drawedCount;
			this.drawedQuestionsNumbers[drawedCount++] = number;
			isDrawed[i] = 1;
		} else {
			for (var i=0; i<indexArray.length; i++) {
				number = Math.round(Math.random());
				if (number == 1 && isDrawed[i] == 0) {
					this.drawedQuestionsNumbers[drawedCount++] = i;
					isDrawed[i] = 1;
				}
				if (drawedCount == this.randAnswersCount) break;
			}
		}
	};
	
	for (var i=0; i<this.randAnswersCount; i++) {
		var index = this.drawedQuestionsNumbers[i];
		var value;
		if (PageInterface.GetValue("state") == psDesigning) {
			value = this.correctAnswersList[index];
		} else value = "";
		
		t += "<tr>" + 
		    "<td style='width: 50%; color: " + this.fontColor + 
			"; font-family: " + this.fontFamily + 
			"; font-size: " + this.convertFontSize(this.fontSize, "pt") + "px; '>" + 
			indexArray[index] + " :</td>" +
			"<td style='width: 50%;'>" +
			"<input width='40%' id='" + this.id + "_ans_" + index + 
			"' value='" + value + "'>" + "</td></tr>";
	}
	
	t += '</table>';	
	this.setHTML(t);
};

componentMatchWords.prototype.convertFontSize = function(value, format)
{
	var fontSizesPX = new Array(0,10,13,16,18,24,32,48);
	var fontSizesPT = new Array(0, 8,10,12,14,18,24,36);
	var pxSize = 2;
	/*if (format == "px") {
		pxSize = fontSizesPX[parseInt(value)];
	}*/
	for (var i=0; i<fontSizesPX.length; i++) {
		if (format == "pt") {
			if (value == fontSizesPT[i]) {
				pxSize = fontSizesPX[i];
				break;
			}
		} else { //not used, but in future...
			if (value == fontSizesPX[i]) {
				pxSize = fontSizesPT[i];
				break;
			}
		}
	}
	
	return pxSize;
};

componentMatchWords.prototype.checkAnswer = function() { 
  return(this.parseCheckAnswer(this));
};

componentMatchWords.prototype.getAnswer = function() {
	var result = "";
	for (var i=0; i<this.randAnswersCount; i++) {
		var separator;
		if (result == "") separator = ""; else separator = "||";
		var index = this.drawedQuestionsNumbers[i];
		var obj = document.getElementById(this.id + "_ans_" + index);
		if (obj) {
			var answer = obj.value;
			result += separator + index + "::" + answer;
		}
	}
	return result;
};

componentMatchWords.prototype.getCorrectAnswer = function() {
	var result = "";
	var separator = "";
	for (var i=0; i<this.correctAnswersList.length; i++) {
		if (result == "") separator = ""; else separator = "||";
		result += separator + i + "::" + this.correctAnswersList[i];
	}
	return result;
}

componentMatchWords.prototype.setAnswer = function(valStr, showCorrect) 
{
	if (showCorrect == null || showCorrect == "undefined") showCorrect = true;
	if (showCorrect) {
		for (var i=0; i<this.randAnswersCount; i++) {
			var index = this.drawedQuestionsNumbers[i];
			var obj = document.getElementById(this.id + "_ans_" + index);
			var correctAnswer = this.correctAnswersList[index];
			if (obj) {
				obj.value = correctAnswer;
			}
		}
	} else {
		var width = this.w - this.borderWidth*2-5;
		var arrOpt = this.choiceValues.split("#|#");
		var indexArray = arrOpt[0].split("||");
		
		var answersWithNumbers = valStr.split("||");
		
		var t = '<span style="color:' + this.fontColor + '; font-family:'
			+ this.fontFamily + '; font-size:' + this.fontSize + ';">'
			+ this.pollQuestion + '</span>';
		
		t += '<table style="width:' + (width-20) + 'px;" id="match_words_tab_'
			+ this.id + '" dir="'+this.dir+'">';
		
		this.drawedQuestionsNumbers = new Array();
		for (var i=0; i<this.correctAnswersList.length; i++) {
			this.drawedQuestionsNumbers[i] = -1;
		}
		
		for (var i=0; i<answersWithNumbers.length; i++) {
			var opt = answersWithNumbers[i];
			var numberAndAnswer = answersWithNumbers[i].split("::");
			
			var index = numberAndAnswer[0];
			this.drawedQuestionsNumbers[i] = index;
			
			var value = numberAndAnswer[1];
			t += "<tr>" + 
			    "<td style='width: 50%;'>" + indexArray[index] + " :</td>" +
				"<td style='width: 50%;'>" +
				"<input width='40%' id='" + this.id + "_ans_" + index + 
				"' value='" + value + "'>" +
				"</td></tr>";
		}
		t += "</table>";
		this.setHTML(t);
	}
	if (this.locked) this.onLock();
	return;	
};

componentMatchWords.prototype.parseCheckAnswer = function(obj) 
{
	var isCorrect = false;
	var correctAnswersCount = 0;
	for (var i=0; i<this.randAnswersCount; i++) {
		var index = this.drawedQuestionsNumbers[i];
		var obj = document.getElementById(this.id + "_ans_" + index);
		var correctAnswer = this.correctAnswersList[index];
		if (obj) {
			var answer = obj.value;
			if (answer.toLowerCase() == correctAnswer.toLowerCase()) {
				correctAnswersCount++;
			}
		}
	}
	isCorrect = (correctAnswersCount >= this.answersCountToPass);
	return isCorrect;
};

componentMatchWords.prototype.onLock = function() 
{
	for (var i=0; i<this.randAnswersCount; i++) {
		var index = this.drawedQuestionsNumbers[i];
		var obj = document.getElementById(this.id + "_ans_" + index);
		if (obj) {
			obj.disabled = true;
		}
	}
	this.locked = true;
	return true;
};

componentMatchWords.prototype.onUnLock = function() 
{
	for (var i=0; i<this.randAnswersCount; i++) {
		var index = this.drawedQuestionsNumbers[i];
		var obj = document.getElementById(this.id + "_ans_" + index);
		if (obj) {
			obj.disabled = false;
		}
	}
	this.locked = false;
	return true;
};

componentMatchWords.prototype.onPaint = function(obj) {
	return true;
};

componentMatchWords.prototype.onRepaint = function(obj) {
	var correctAnswersCount = 0;
	for (var i=0; i<this.randAnswersCount; i++) {
		var index = this.drawedQuestionsNumbers[i];
		var obj = document.getElementById(this.id + "_ans_" + index);
		var correctAnswer = this.correctAnswersList[index];
		if (obj) {
			var answer = obj.value;
			if (answer.toLowerCase() == correctAnswer.toLowerCase()) {
				correctAnswersCount++;
			}
		}
	}
	
	var message = this.messageOnAfterCheckAnswer;
	message = message.replace("%s1", correctAnswersCount);
	message = message.replace("%s2", this.answersCountToPass);
	
	var width = this.w - this.borderWidth*2-5; //this.messageOnAfterCheckAnswer
	var t = '<table style="width:' + (width-20) + 'px;" id="match_words_tab_' + this.id + '" dir="'+this.dir+'">';
	t += "<tr><td></td></tr><tr><td align='center' valign='middle' style='color: " + this.fontColor + "; font-family: " + this.fontFamily + "; font-size: " + this.convertFontSize(this.fontSize, "pt") + "px; '>" + message + "</td></tr>";
	t += "</table>";
	this.setHTML(t);
}

componentMatchWords.prototype.baseUnselectable = componentMatchWords.prototype.unselectable;
componentMatchWords.prototype.unselectable = function() 
{
	return;
};

componentMatchWords.prototype.interactionsCorrectResponses = function()
{
	return this.getCorrectAnswer();
};

componentMatchWords.prototype.interactionsStudentResponse = function()
{
	return this.getAnswer();
};

componentMatchWords.prototype.interactionsCorrectResponsesAsText = function()
{
	return this.getCorrectAnswer();
};

componentMatchWords.prototype.interactionsStudentResponseAsText = function()
{
	return this.getAnswer();
};

componentMatchWords.prototype.interactionsCorrectResponsesCount = function()
{
	return this.correctAnswersList.length;
};
