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

componentAnswerQuestions = function(name) 
{
	this.__componentText = __componentText;
	this.__componentText(name);
	
	this.elementHeight = 0;
	
	this.texts = new Array();
	this.inputs = new Array();
	
	this.interactions = true; //pg2-test_interactions
	this.interactionsType = "matching";
};

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

componentAnswerQuestions.prototype.onBaseCreate = function(obj) 
{
	if (PageInterface.GetValue("state") != psDesigning &&  lms && this.disableOnlineOfflineMix != false )
	{
		this.resourcesStatus 	= lms.resourcesStatus();
		this.resourcesURL 		= lms.resourcesURL();
	} else this.resourcesURL = resourcesURL;
	
	this.captureMouseEvents();
};

componentAnswerQuestions.prototype.componentAnswerQuestionsBaseOnCreate = componentAnswerQuestions.prototype.onCreate;
componentAnswerQuestions.prototype.onCreate = function(obj)
{
	this.componentAnswerQuestionsBaseOnCreate();
	
	var contentArr = this.content.split('#!#');	
	
	var elem = document.getElementById(this.id+"_textSize");
	var maxWidth = 0;
	for(var i=0; i < contentArr.length; i++){
		var oneQuestArr = contentArr[i].split('##');
		var width = this.getTextWidth(elem, oneQuestArr[0], 'nowrap');
		if (width > maxWidth) maxWidth = width;
	};
	
	var tempX = this.x + 3;
	var tempY = 15 + this.getTextHeight(elem, this.text, this.w/2, 'normal');
	
	this.elementHeight = (this.h - tempY - 5)/contentArr.length;
	
	this.correctAnswer = ''; //pg2-test_interactions
	
	for(var i=0; i < contentArr.length; i++){
		var oneQuestArr = contentArr[i].split('##');
		var textHeight = this.getTextHeight(elem, this.text, this.w/2, 'normal');
		
		var questText = new __componentText(this.id+'_QUESTION_'+i);
		questText.myParent = this; 
		questText.x = tempX;
		questText.y = this.y + tempY;
		questText.w = maxWidth;
		questText.h = this.elementHeight;
		questText.text = oneQuestArr[0];
		questText.font.family = this.answerFontFamily;
		questText.font.size = this.answerFontSize;
		questText.font.color = this.answerFontColor;
		questText.lineHeight = this.lineHeight;
		questText.align = "left";
		questText.valign = 'top';		
		questText.borderWidth = 1;
		questText.padding = 0;
		
		page.addChild(questText);
		this.texts.push(questText);
		
		var inputElem = new componentInput(this.id+'_INPUT_'+i);
		inputElem.myParent = this; 
		inputElem.x = this.x + maxWidth + 10;
		inputElem.y = this.y + tempY;
		inputElem.w = this.w - maxWidth - 20;
		inputElem.h = 22;
		//inputElem.caseSensitive = this.caseSensitive; //pg2-test_interactions -removed, it's undefined
		inputElem.fieldType = 'text';
		inputElem.inputBgColor = '#ffffff';
		inputElem.fontSize = 2;		
		inputElem.borderColor = '#000000';
		inputElem.fontFamily = 'Arial';
		inputElem.fontColor = '#000000';
		inputElem.textAlign = 'left';			
		inputElem.padding = 0;
		inputElem.maxLength = -1;
		inputElem.wasPainted = false;
		inputElem.borderColor = this.inputBorderColor;
		inputElem.inputBgColor = this.inputBgColor;		
		inputElem.baseResetElement = inputElem.resetElement;
		inputElem.resetElement = this.__resetElement;
		inputElem.fnOnKeyUp = this.__fnOnKeyUp;
		inputElem.getValue = this.__getValue;		
		inputElem.checkAnswer = this.__checkAnswer;		
		inputElem._setHTMLDef = this.inputSetHTMLDef;
		inputElem.correctAnswer = oneQuestArr[1];
		inputElem.showCorrectAnswerIndicator = this.showCorrectAnswerIndicator;
		inputElem.showAnswerIndicatorAnyTime = this.showAnswerIndicatorAnyTime;
		inputElem.correctColor = this.elementCorrectBckColor;
		inputElem.incorrectColor = this.elementInCorrectBckColor;
		
		//pg2-test_interactions
		inputElem.interactions = false;
		if (this.correctAnswer != '') this.correctAnswer += '||';
		this.correctAnswer += inputElem.correctAnswer;
		//
		
		page.addChild(inputElem);
		this.inputs.push( inputElem );		
		tempY += this.elementHeight;//sentText.h;
	};
};

componentAnswerQuestions.prototype.onRepaint = function() 
{
	//po to aby nie zmieniala sie czcionka pytania po nacisnieci na OK
	return;
}

componentAnswerQuestions.prototype.inputSetHTMLDef = function(valStr) {
	valStr = (valStr ? valStr : "");
	
	var t = '<form name="' + this.id + 'Form" id="' + this.id + 'Form" onsubmit="return false;">';
	t += '<input id="' + this.id + '_contentObj" name="' + this.id + 'Input" type="' + this.fieldType + '" value="' + valStr + '" ';
	
	if (this.maxLength != -1) {
		t += 'maxlength="' + this.maxLength + '" ';
	};
	
	if (is.ns4) {
		var cols;
		if (this.w == 0) cols = 0;
		else {
			if (this.w < 25) cols = 1;
			else cols = 1 + (parseInt((this.w - 18) / 8));
		}
		t += ' size="' + cols + '"';
	};
	t += this._getStyleDef();
	
	t += ' onkeyup="WBTObject.all[\''+this.id+'\'].fnOnKeyUp()"';
	
	t += '>';
	t += '</form>';
	
	this.setHTML(t);
};

componentAnswerQuestions.prototype.__resetElement = function()
{
	var form = getForm(this.id + "Form");
	if (form) form[0].value = '';
	this.wasPainted = false;
	this.baseResetElement();
}

componentAnswerQuestions.prototype.__fnOnKeyUp = function()
{
	var splitStr = "||";
	var actAnswer = this.getAnswer().toString();
	var ans = "";
	var result = false;
	var arrCorrAns = new Array();
	arrCorrAns = this.correctAnswer.toString().split(splitStr);
	
	if (!this.caseSensitive) {
		actAnswer = actAnswer.toLowerCase();
	};
	var input = document.getElementById(this.id+"_contentObj");
	
	for (var i = 0; i < arrCorrAns.length; i++) {
		ans = (this.caseSensitive) ? arrCorrAns[i]:arrCorrAns[i].toLowerCase();
		
		if (actAnswer != ans){
			if (this.wasPainted && input) {
				input.style.background = 'transparent';
			};
		};
	};
};

componentAnswerQuestions.prototype.__getValue = function()
{
	var contentObj = document.getElementById(this.id+"_contentObj");
	if (contentObj) {
		return contentObj.value;
	};
	return "";
};

componentAnswerQuestions.prototype.__checkAnswer = function() {
	var splitStr = "||";
	var actAnswer = this.getAnswer().toString();
	var ans = "";
	var result = false;
	var arrCorrAns = new Array();
	arrCorrAns = this.correctAnswer.toString().split(splitStr);
	
	if (!this.caseSensitive) {
		actAnswer = actAnswer.toLowerCase();
	};

	for (var i = 0; i < arrCorrAns.length; i++) {
		ans = (this.caseSensitive) ? arrCorrAns[i]:arrCorrAns[i].toLowerCase();
		if (actAnswer == ans){
			result = true;			
		}
		if (this.showAnswerIndicatorAnyTime && this.showCorrectAnswerIndicator){
			var input = document.getElementById(this.id+"_contentObj");
			if (input){
				if (result){
					input.style.background = this.correctColor;
				}else{
					input.style.background = this.incorrectColor;				
				}
				this.wasPainted = true;
			};
		};
	};
	return result;
};

componentAnswerQuestions.prototype.checkIsCorrect = function(arr, ind)
{
	for (var i=0; i < arr.length; i++){
		if (ind == arr[i]) return 1;
	};
	return 0;
};

componentAnswerQuestions.prototype.getTextWidth = function(obj, text, wrap)
{
	obj.innerHTML = text;
	obj.style.fontFamily = this.font.family;
	obj.style.whiteSpace = wrap;
	obj.style.fontSize = getFontSize(this.font.size, 'px');
	
	return obj.offsetWidth+20;
}

componentAnswerQuestions.prototype.getTextHeight = function(obj, text, width, wrap)
{
	obj.innerHTML = text;
	if (width != 0) obj.style.width = width;
	obj.style.fontFamily = this.font.family;
	obj.style.fontFamily = this.font.family;
	obj.style.whiteSpace = wrap;
	obj.style.fontSize = getFontSize(this.font.size, 'px');
	var height = obj.offsetHeight;
		
	return height;
}

componentAnswerQuestions.prototype.onShow = function() 
{
	if (PageInterface.GetValue("state") != psDesigning) {
		for (var i=0; i < this.texts.length; i++) {
			this.texts[i].show();
		};
		for (var i=0; i < this.inputs.length; i++) {
			this.inputs[i].show();
		};
	};
};
//------------------------------------------
// pg2- defined its own methods
//------------------------------------------
componentAnswerQuestions.prototype.parseCheckAnswer = function(obj)
{
	return this.checkAnswer();
};

componentAnswerQuestions.prototype.checkAnswer = function()
{
	var result = true;
	result = (this.correctAnswer.toLowerCase() == this.getAnswer().toLowerCase());
	return result;
}

componentAnswerQuestions.prototype.getAnswer = function()
{
	var result = "";
	for (var i=0; i<this.inputs.length; i++) {
		var obj = this.inputs[i];
		if (obj && typeof(obj.getAnswer) == 'function') {
			var answer = obj.getAnswer();
			if (answer == '') answer = ' ';
			if (result != '') result += '||';
			result += answer + '';
		}
	}
	return result;
}

componentAnswerQuestions.prototype.setAnswer = function(valStr, showCorrect)
{
	if ((typeof(valStr) == 'undefined' || valStr == '') && !showCorrect) return;
	if (valStr == '' && (typeof(showCorrect) == 'undefined' || showCorrect.toString() == '')) showCorrect = true;
	
	if (!showCorrect){
		var answers = valStr.split(',');
		for (var i=0; i<answers.length; i++) {
			try {
				var answer = unescape(answers[i]);
				var obj = this.inputs[i];
				if (obj && typeof(obj.setAnswer) == 'function') obj.setAnswer(answer, false);
			} catch(e) {};
		}
	} else  {
		for (var i=0; i<this.inputs.length; i++) {
			var obj = this.inputs[i];
			if (obj && typeof(obj.setAnswer) == 'function') obj.setAnswer('', true);
		}
	}
}

componentAnswerQuestions.prototype.interactionsCorrectResponsesCount = function()
{
	return 1;
};

componentAnswerQuestions.prototype.interactionsCorrectResponses = function(index)
{
	var result = '';
	var ansList = this.correctAnswer.split('||');
	for (var i=0; i<ansList.length; i++) {
		var ans = ansList[i];
		if (result != '') result += '||';
		result += escape(ans);
	}
	return result;
};

componentAnswerQuestions.prototype.interactionsStudentResponse = function()
{
	var result = '';
	var ansList = this.getAnswer().split('||');
	for (var i=0; i<ansList.length; i++) {
		var answer = ansList[i];
		if (result != '') result += ',';
		answer = answer.replace(new RegExp(',', "g"), '');
		result += escape(answer);
	}
	return result;
};

componentAnswerQuestions.prototype.interactionsCorrectResponsesAsText = function(index)
{
	return this.interactionsCorrectResponses();
};

componentAnswerQuestions.prototype.interactionsStudentResponseAsText = function()
{
	return this.interactionsStudentResponse();
}
