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

componentFinishSentence = function(name) 
{
	this.componentTextarea = componentTextarea;
	this.componentTextarea(name);
	
	this.text = this.addChild(new __componentText(null));
	this.textarea = this.addChild(new componentTextarea(null));
	
	this.textarea._setHTMLDef = this._setHTMLDef;
	this.textarea.baseOnPaint = this.textarea.onPaint;
	this.textarea.onPaint = this._onPaint;	
	
	this.isChanged = false;
	this.correctAnswer = "";
	this.correctAnswersList = new Array();
	this.interactions = true;
	this.interactionsType = "fill-in";
	this.textareaName = '';
};

componentFinishSentence.prototype = new componentTextarea;
componentFinishSentence.prototype.type = "componentFinishSentence";

componentFinishSentence.prototype.componentFinishSentenceBaseOnCreate = componentFinishSentence.prototype.onCreate;
componentFinishSentence.prototype.onCreate = function(obj)
{
	this.componentFinishSentenceBaseOnCreate();
	this.correctAnswersList = this.correctAnswer.split("||");
};

componentFinishSentence.prototype._onPaint = function() 
{
	if (this.locked) return;
	this.baseOnPaint();
}

componentFinishSentence.prototype.onPaint = function() 
{	
	return;	
};

//nadpisanie funkcji _setHTML dla textarea
componentTextarea.prototype._getStyleDef = function()
{
	var styleDef = ""

	if(is.ie || is.moz) 
	{
		var borderStyle = ((this.borderColor != null && this.borderColor != 'transparent') ? ("border: "+this.textareaWidth+"px " + this.borderColor + " solid;") : "border: 0px;");		
		styleDef = " style='width:" + this.w + "; height:" + (this.h - 2) + "; background-color:" + this.bgColor + ";" + borderStyle + "width:100%;" + " overflow: auto; '";
	}
	return styleDef;
};

componentFinishSentence.prototype._setHTMLDef = function(valStr) 
{
	valStr = (valStr ? valStr : "");
	var t = "<form name='" + this.id + "Form' id='" + this.id + "Form'>"
	t += "<textarea id='"+this.id+"_contentObj'name='"+this.id+"_contentObj' value='" + valStr + "'";
	t += this._getStyleDef();
	t += ' onkeyup="pc.'+this.parent.id+'.fnOnKeyUp()"';
	t += "style='font-family: "+this.fontFamily+"; font-size: "+getFontSize(this.fontSize, 'px')+";'";
	t += ">";
	t += this.parent.beginSentence;
	t += "</textarea>"
	t += "</form>";
	this.setHTML(t);
};


componentFinishSentence.prototype.componentFinishSentenceShow = componentFinishSentence.prototype.show;
componentFinishSentence.prototype.show = function() 
{
	this.componentFinishSentenceShow();
	this.text.show();
	this.textarea.show();
}

componentFinishSentence.prototype.onRepaint = function() 
{
	if (PageInterface.GetValue("allowCAI") && this.showCorrectAnswerIndicator)
	{
		this.paintTextarea();
	}
};

componentFinishSentence.prototype.paintTextarea = function() 
{
	var splitStr = "||";
	
	var actAnswer = this.textarea.getAnswer().toString();
	var ans = "";
	var arrCorrAns = new Array();
	arrCorrAns = this.textarea.correctAnswer.toString().split(splitStr);
	
	if (this.ignoreWhiteChars) {
		actAnswer = this.clearWhiteChars(actAnswer)
	};	
	
	if (!this.caseSensitive) {
		actAnswer = actAnswer.toLowerCase();
	};
	for(var i = 0; i < arrCorrAns.length; i++) {
		ans = (this.caseSensitive) ? arrCorrAns[i]:arrCorrAns[i].toLowerCase();
		if (this.ignoreWhiteChars) { //pg2-if it's apply to user's answer, why didn't to correct answer?
			ans = this.clearWhiteChars(ans);
		}
		if (this.ignoreWhiteChars && ans.substring(ans.length-1, ans.length) == '.' && actAnswer.substring(actAnswer.length-1, actAnswer.length) != '.') { //pg2-add dot only if correct answer has it and answer HAS NOT
			actAnswer += '.';
		}
		if (actAnswer == ans) {
			document.getElementById(this.textarea.id+"_contentObj").style.backgroundColor = this.elementCorrectBckColor;
			break;
		} else document.getElementById(this.textarea.id+"_contentObj").style.backgroundColor = this.elementInCorrectBckColor;
	};
};

componentFinishSentence.prototype.__getAnswer = function(){
	var str = '';
	var obj = document.getElementById(this.id + "_contentObj");
	if (obj) return obj.innerHTML;
	else return '';
}

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

componentFinishSentence.prototype.clearWhiteChars = function(userAns)
{
	var result = '';
	var userAnsArr = userAns.split("");
	var i = 0;
	while (userAnsArr[i] == " " ){
		userAnsArr[i++] = ""
	};
	i = userAnsArr.length-1
	if (userAnsArr[i] == '\n') {
		userAnsArr[i--] = "";
		userAnsArr[i--] = "";
	}
	switch (userAnsArr[i]){
		case ',' : userAnsArr[i--] = ""
		break;
		case '.' : userAnsArr[i--] = ""
		break;
		case ';' : userAnsArr[i--] = ""
		break;
		case '?' : userAnsArr[i--] = ""
		break;
		case '\n' : userAnsArr[i--] = ""
		break;
	}
	while (userAnsArr[i] == " " ){
		userAnsArr[i--] = ""
	};			
	var result = userAnsArr.join("");
	
	return result
}

componentFinishSentence.prototype.parseCheckAnswer = function(obj)
{
	var splitStr = "||";
	var result = true;
	
	var actAnswer = this.textarea.getAnswer().toString();
	if (this.ignoreWhiteChars) {
		actAnswer = this.clearWhiteChars(actAnswer)
	};	
	var ans = "";
	var arrCorrAns = new Array();
	arrCorrAns = this.textarea.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 (this.ignoreWhiteChars) { //pg2-if it's apply to user's answer, why didn't to correct answer?
			ans = this.clearWhiteChars(ans);
		}
		if (this.ignoreWhiteChars && ans.substring(ans.length-1, ans.length) == '.' && actAnswer.substring(actAnswer.length-1, actAnswer.length) != '.') { //pg2-add dot only if correct answer has it and answer HAS NOT
			actAnswer += '.';
		}
		if (actAnswer == ans) {
			var bckgColor = this.elementCorrectBckColor;
			result = true;
		}else {
			var bckgColor = this.elementInCorrectBckColor;
			result = false;
		};		
		if (this.showAnswerIndicatorAnyTime == 1){
			document.getElementById(this.textarea.id+"_contentObj").style.backgroundColor = bckgColor;
		}				
		if (result) return result;
	};
	return result;
};

componentFinishSentence.prototype.setAnswer = function(valStr, showCorrect) 
{
	//if ((typeof(valStr) == 'undefined' || valStr == '') && !showCorrect) return; //pg2-removed, 'cause if preloader is enabled, textarea maintains its color
	if (valStr == '' && (typeof(showCorrect) == 'undefined' || showCorrect.toString() == '')) showCorrect = true;
	
	var contentObj = document.getElementById(this.textarea.id+"_contentObj");
	if (contentObj) {
		if (valStr != '' && !showCorrect){
			var answersArr = valStr.split('||');
			contentObj.value = answersArr[0];
		}else{
			contentObj.value = valStr;
		};
		if (valStr == "" && this.textarea) contentObj.style.backgroundColor = this.textarea.bgColor; //pg2-if empty, clear 'backlight' color
	}
	this.onRepaint();	
};

componentFinishSentence.prototype.fnOnKeyUp = function(obj)
{
	var splitStr = "||";
	
	var actAnswer = this.textarea.getAnswer().toString();
	var ans = "";
	var arrCorrAns = new Array();
	arrCorrAns = this.textarea.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) {
			document.getElementById(this.textarea.id+"_contentObj").style.backgroundColor = '#ffffff';
			break;
		};
	};
};

componentFinishSentence.prototype.onLock = function() 
{ 
	var obj = document.getElementById(this.textarea.id+"_contentObj");
	if (obj) obj.disabled = true;
	this.textarea.locked = true;
};

componentFinishSentence.prototype.onUnLock = function() 
{
	var obj = document.getElementById(this.textarea.id+"_contentObj");
	if (obj) obj.disabled = false;
	this.textarea.locked = false;
};

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

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

componentFinishSentence.prototype.interactionsCorrectResponsesAsText = function() 
{
	return this.textarea.correctAnswer;
}

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

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