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

componentList = function(name) {
	//inherited
	this.componentCombo = componentCombo;
	this.componentCombo(name);
	this.size = 0;
	this.multiple = false;
	
	this.interactionsType = "fill-in";
	this.locked = false;
};

componentList.prototype = new componentCombo;
componentList.prototype.type = "componentList";

componentList.prototype.getSize = function() {
	if (is.ns4) {
		return ((this.h - 6) / 20);
	} else {
		return ((this.h - 6) / 16);
	}
};

componentList.prototype.onPaint = function(obj) {
	if (this.locked) return;

	var t = '<form id="' + this.id + 'Form" name="' + this.id + 'Form">';
	t += '<select dir="' + this.dir +'" style="width:' + this.w + 'px;" ';
	t += ' size="' + this.getSize() + '"';
	if (this.multiple) {
		t += ' multiple';
		t += ' title="mutliselect = Ctrl+ ..."';
	}

	if (this.disabled == true) {
		t += ' disabled="disabled"';
	}	

	t += ' onfocus="WBTObject.all[\''+this.id+'\'].invokeEvent(\'FocusIn\')"';
	t += ' onblur="WBTObject.all[\''+this.id+'\'].invokeEvent(\'FocusOut\')"';
	
	t += ' id="' + this.id + 'Select" ';
	//t += ' onChange="page.findChildByName(\'' + this.id + '\').eventOnChange();"';
	t += ' style="font-family: ' + this.textFontFamily + ';';
	t += ' font-size: ' + this.textFontSize + 'px; color: ' + this.textFontColor + ';';
	t += ' font-style: ' + this.textFontStyle + '; font-weight: ' + this.textFontWeight + ';"> ';

    var changed = unquotedString(this.listValues);
  
    var arrOpt 	= changed.split("||");	
	this.optLength = arrOpt.length;
	for (var i = 0; i < arrOpt.length; i++) {
		var sel = "";
		if (i == this.selectedIndex) sel = "selected";
		t += '<option id="' + this.id + '_option' + i + '" value="' + i + '" ' + sel + '>' + arrOpt[i] + '</option>';
	}
	
	t += '</select><br><p id="' + this.id + 'Text" style="font-family: ' + this.textFontFamily + ';';
	t += ' font-size: ' + this.textFontSize + 'px; color: '+this.correctAnswersColor+';"></p>';
	
	t += "</form>";
	
	if (this.showHints && !this.hintShowInStep && ((this.hintWidth == 0 || this.hintHeight == 0))) {
		t += '<div id="'+this.id+'_textSize" style="position:absolute; visibility:hidden; left:0px; top:0px; border: solid #000000 0px;"></div>';
	}
	this.setHTML(t);
	this.onRepaint();
};

componentList.prototype.onRepaint = function(obj) {
	var obj = document.getElementById(this.id + "Select");
	if (PageInterface.GetValue("allowCAI") && this.showCorrectAnswerIndicator){
		var correctAnswersList = this.correctAnswer.split("||");
		var index = 0;
		if (obj) {
			for (var i=0; i<obj.options.length; i++) {
				var optObj = obj.options[i];
				if (optObj.selected) {
					if (correctAnswersList[index] == i) {
						optObj.style.background = this.correctColor;
						if (index+1 < correctAnswersList.length) index++;
					} else {
						optObj.style.background = this.incorrectColor;
					}
				} else {
					if (correctAnswersList[index] == i) {
						optObj.style.background = this.incorrectColor;
						if (index+1 < correctAnswersList.length) index++;
					} else {
						//optObj.style.background = this.correctColor;
					}
				}
				optObj.selected = false;
				//optObj.style.background = this.incorrectColor;
			}
		}
	} else
	if (obj) {
		for (var i=0; i<obj.options.length; i++) {
			var optObj = obj.options[i];
			if (optObj) optObj.style.background = 'transparent';
		}
	}
};

componentList.prototype.checkAnswer = function() {
	var obj = document.getElementById(this.id + "Select");
	var ans = obj.selectedIndex;
	var ans2 = obj.value;
	return(this.parseCheckAnswer(this));
};

componentList.prototype.parseCheckAnswer = function(obj) {
	var result = true;
	var correctAnswersList = this.correctAnswer.split("||");
	var index = 0;
	var obj = document.getElementById(this.id + "Select");
	if (!obj) result = false; else {
		for (var i=0; i<obj.options.length; i++) {
			var optObj = obj.options[i];
			if (correctAnswersList[index] == i) {
				if (!optObj.selected) return false;
				if (index+1 < correctAnswersList.length) index++;
			} else {
				if (optObj.selected) return false;
			}
		}
	}
	return result;
};

componentList.prototype.getAnswer = function() {
	var result = "";
	var sep = "";
	var obj = document.getElementById(this.id + "Select");
	if (obj){
		for (var i=0; i<obj.options.length; i++) {
			var optObj = obj.options[i];
			if (optObj.selected) result += sep + i;
			if (result != "") sep = "||";
		}
	}
	return result;
};

componentList.prototype.setAnswer = function(valStr, showCorrect) {
	if (typeof(showCorrect) == "undefined") showCorrect = true;
	if (typeof(valStr) == "undefined") valStr = "";
	if (showCorrect) valStr = this.correctAnswer;
	var answersList = valStr.split("||");
	var index = 0;
	var obj = document.getElementById(this.id + "Select");
	if (obj) {
		for (var i=0; i<obj.options.length; i++) {
			var optObj = obj.options[i];
			if (answersList[index] == i) {
				optObj.selected = true;
				if (index+1 < answersList.length) index++;
			} else {
				optObj.selected = false;
			}
		}
	}
	this.onRepaint();
	return;
};

componentList.prototype.onLock = function() 
{
	var form = page.doc.forms[this.id + "Form"];
	if (form) form[0].disabled = true;
	this.locked = true;
	return true;
};

componentList.prototype.lock = function() {
	return this.onLock();
};

componentList.prototype.onUnLock = function() 
{
	var form = page.doc.forms[this.id + "Form"];
	if (form) form[0].disabled = false;
	this.locked = false;
	return true;
};

componentList.prototype.unlock = function() {
	return this.onUnLock(); //pg2-was onUnlock instead onUnLock
}

//---------------------------------------------------------------------------------------------------------------------
componentList.prototype.interactionsCorrectResponsesCount = function()
{
	var corrAnsList = this.correctAnswer.split("||");
	return corrAnsList.length;
};

componentList.prototype.interactionsCorrectResponses = function()
{
	return this.correctAnswer;
};

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

componentList.prototype.interactionsCorrectResponsesAsText = function()
{
	var changed = unquotedString(this.listValues);
	var textAnswersList = changed.split("||");	
	var correctAnswersList = this.correctAnswer.split("||");
	var result = "";
	var sep = "";
	
	for (var i=0; i<correctAnswersList.length; i++) {
		var index = parseInt(correctAnswersList[i]);
		
		result += sep + textAnswersList[index];
		if (result != "") sep = '||';
	}	
	return result;
};

componentList.prototype.interactionsStudentResponseAsText = function()
{
	var changed = unquotedString(this.listValues);
	var textAnswersList = changed.split("||");	
	var answersList = this.getAnswer().split("||");
	var result = "";
	var sep = "";
	
	for (var i=0; i<answersList.length; i++) {
		var index = parseInt(answersList[i]);
		
		result += sep + textAnswersList[index];
		if (result != "") sep = '||';
	}	
	return result;
};
