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

componentCopyOutSentence = function(name) 
{
	this.__componentText = __componentText;
	this.__componentText(name);
	
	this.text = "";
	this.texts = new Array();
	this.textAreaComponent = new Array();
};

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

componentCopyOutSentence.prototype.onCreate = function(obj)
{
	this.correctAnswersList = this.correctAnswer.split("||");
	
	var splitedContent = this.content.split('|@|');
	
	var pairSentAreaHeight = (this.h - splitedContent.length-1)/splitedContent.length;
	
	var tempX = this.x;
	var tempY = this.y;
	
	for(var i=0; i < splitedContent.length; i++){
		var parirArr = splitedContent[i].split('#!#');
		if (parirArr[1] != '') var answersArr = parirArr[1].split('@!@');
		
		var textComp = new __componentText;
		textComp.myParent = this; 
		textComp.id = this.id+'_sent_'+i; 
		textComp.x = tempX;
		textComp.y = tempY;
		textComp.h = pairSentAreaHeight/2;
		textComp.w = this.w;
		textComp.font.family = this.font.family;
		textComp.font.size = this.font.size;
		textComp.font.color = this.font.color;		
		textComp.text = parirArr[0];
		page.addChild(textComp);		
		this.texts.push( textComp );
		
		var areaComp = new componentTextarea;
		areaComp.myParent = this; 
		areaComp.id = this.id+'_area_'+i; 
		areaComp.x = tempX;
		areaComp.y = tempY + pairSentAreaHeight/2;
		areaComp.h = pairSentAreaHeight/2;
		areaComp.w = this.w;
		areaComp.correctAnswer = answersArr[0];
		areaComp.caseSensitive = (answersArr[1] == 'true')?true:false;
		areaComp.borderColor = this.borderColor;
		areaComp.ignoreWhiteChars = this.ignoreWhiteChars;
		areaComp.clearWhiteChars = this.__clearWhiteChars;
		areaComp._setHTMLDef = this.__setHTMLDef;
		areaComp.onLock = this.__onLock;
		areaComp.onUnLock = this.__onUnLock;
		areaComp.getAnswer = this.__getAnswer;
		areaComp.parseCheckAnswer = this.__parseCheckAnswer;
		areaComp.setAnswer = this.__setAnswer;
			
		page.addChild(areaComp);		
		this.textAreaComponent.push( areaComp );
		
		tempY += pairSentAreaHeight + (i+1);
	};
};

componentCopyOutSentence.prototype.onRepaint = function() 
{
	if (PageInterface.GetValue("allowCAI") && this.showCorrectAnswerIndicator)
	{
		for (var i=0; i < this.textAreaComponent.length; i++) {
			var obj = document.getElementById(this.textAreaComponent[i].id + "_contentObj");
			if (!this.textAreaComponent[i].checkAnswer()) {
				obj.style.border = "1px solid " + this.elementInCorrectBorderColor;
				obj.style.background = this.elementInCorrectBckColor;
			}else { 
				obj.style.border = "1px solid " + this.elementCorrectBorderColor;
				obj.style.background = this.elementCorrectBckColor;
			};
		};
	};
};

componentCopyOutSentence.prototype.parseCheckAnswer = function()
{
	return true;
}

componentCopyOutSentence.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
}

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

componentCopyOutSentence.prototype.onShow = function() 
{
	if (PageInterface.GetValue("state") != psDesigning) {
		for (var i=0; i < this.texts.length; i++) {
			this.texts[i].show();
			this.textAreaComponent[i].show();
		};
	};
};

componentCopyOutSentence.prototype.__setHTMLDef = function(valStr) 
{
	if (this.locked) return;

	var borderStyle = '';
	var t = '';
	valStr = (valStr ? valStr : "");
	if (this.myParent.borderColor == 'transparent') {
		borderStyle = '0px';
	} else {
		borderStyle = '1px solid '+this.myParent.borderColor;
	}

	t = "<textarea id='"+this.id+"_contentObj' name='textarea' style='width:" + this.w + "; height:" + (this.h - 2) + "; border: " + borderStyle + "; overflow: auto;'>"
	t += "</textarea>"
	
	this.setHTML(t);
};

componentCopyOutSentence.prototype.__parseCheckAnswer = function(obj)
{
	if (!obj) return(false);
	var splitStr = "||";
	var actAnswer = this.getAnswer().toString();
	if (this.ignoreWhiteChars){
		actAnswer = this.clearWhiteChars(actAnswer);
	}
	var ans = "";
	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();
		var obj = document.getElementById(this.id + "_contentObj");
		//if (this.ignoreWhiteChars && ans.substring(ans.length-1, ans.length) == '.') actAnswer += '.'; //pg2-remove
		if (this.ignoreWhiteChars) { //pg2-new
			ans = this.clearWhiteChars(ans);
		}
		if (actAnswer == ans) {
			if (this.myParent.showAnswerIndicatorAnyTime && this.myParent.showCorrectAnswerIndicator){
				obj.style.border = "1px solid " + this.myParent.elementCorrectBorderColor;
				obj.style.background = this.myParent.elementCorrectBckColor;
			}
			return (true);
		} else //pg2- 'cause incorrect answer indicator wasn't shown
		if (this.myParent.showAnswerIndicatorAnyTime && this.myParent.showCorrectAnswerIndicator){
			obj.style.border = "1px solid " + this.myParent.elementInCorrectBorderColor;
			obj.style.background = this.myParent.elementInCorrectBckColor;
		};
	};
	return (false);
};

componentCopyOutSentence.prototype.__onAfterLoad = function(name)
{
	this.onUnLock();
}

componentCopyOutSentence.prototype.colorMyDiv = function(name)
{
	var obj = document.getElementById(name + "_contentObj");
	obj.style.border = "1px solid " + this.borderColor;
	obj.style.background = 'transparent';		
}

componentCopyOutSentence.prototype.__setAnswer = function(valStr, showCorrect) 
{
	if ((typeof(valStr) == 'undefined' || valStr == '') && !showCorrect) return;
	if (valStr == '' && (typeof(showCorrect) == 'undefined' || showCorrect.toString() == '')) showCorrect = true;
	
	if (showCorrect){
		var correctArr = new Array();
		if (this.correctAnswer != '') correctArr = this.correctAnswer.split('||');
		var obj = document.getElementById(this.id + "_contentObj");
		if (obj) obj.value = correctArr[0];			
	}else{
		var obj = document.getElementById(this.id + "_contentObj");
		if (obj) obj.value = valStr;	
	};
//	this.onRepaint()
};

componentCopyOutSentence.prototype.__onLock = function() 
{
	var obj = document.getElementById(this.id + "_contentObj");
	obj.disabled = true;
};
componentCopyOutSentence.prototype.__onUnLock = function() 
{
	var obj = document.getElementById(this.id + "_contentObj");
	obj.disabled = false;
};
