/*
 * Copyright (C) 1995-2011 by 4system Polska Sp. z o.o.
 * All Rights Reserved.
 */
 
componentTextWithBlanks = function(name)
{
	this.componentTextWithBlanks = __componentText;
	this.componentTextWithBlanks(name);
	
	this.correctValues = new Array(new Array(), new Array());
	this.complete = false;
	this.addInactive = false;	
	this.iTimerID == null;
	
	this.interactions = true;
	this.interactionsType = "fill-in";		
	this.valBackup = new Array(); //pg2
};

componentTextWithBlanks.prototype = new __componentText;
componentTextWithBlanks.prototype.type = "componenTextWithBlanks";

componentTextWithBlanks.prototype.componentTextWithBlanksBaseOnCreate = componentTextWithBlanks.prototype.onCreate;
componentTextWithBlanks.prototype.onCreate = function(obj)
{
	if (PageInterface.GetValue("state") != psDesigning) {
		// this.text = this.textBackup;
		this.text = this.setButtonsActions(this.text);
		this.getCorrectValues(this.inputsCorrectValues);
		
		var content = this.findContentObj();
		content.innerHTML = this.text;	
		this._createAnswerIndicator(); //pg2-test
	}
	
	//this.componentTextWithBlanksBaseOnCreate();
};

componentTextWithBlanks.prototype.setButtonsActions = function(text)
{
	text = text.replace(/InputOnKeyUp/g,  		 'onKeyup="pc.'+this.id+'.keyUp(this);"');
	text = text.replace(/MouseOverButtonAdd/g,   'onMouseOver="pc.'+this.id+'.mouseOverAdd(this);"');
	text = text.replace(/MouseOutButtonAdd/g,    'onMouseOut="pc.'+this.id+'.mouseOutAdd(this);"');
	text = text.replace(/MouseClickButtonAdd/g,  'onMouseDown="pc.'+this.id+'.mouseClickAdd(this);"');
	text = text.replace(/MouseOverButtonInfo/g,  'onMouseOver="pc.'+this.id+'.mouseOverInfo(this);"');
	text = text.replace(/MouseOutButtonInfo/g,   'onMouseOut="pc.'+this.id+'.mouseOutInfo(this);"');
	text = text.replace(/MouseClickButtonInfo/g, 'onMouseDown="pc.'+this.id+'.mouseClickInfo(this);"');
	text = text.replace(/MouseOverButtonWHY/g,   'onMouseOver="pc.'+this.id+'.mouseOverWhy(this);"');
	text = text.replace(/MouseOutButtonWHY/g,    'onMouseOut="pc.'+this.id+'.mouseOutWhy(this);"');
	text = text.replace(/MouseClickButtonWHY/g,  'onMouseDown="pc.'+this.id+'.mouseClickWhy(this);"');
	text = text.replace(/inherit/g, 'hidden');	
	text = text.replace(/specialID/g, this.id);	
	text = text.replace(/value=/g, '');
	return text;
};

componentTextWithBlanks.prototype.onRepaint = function(index)
{
	this._createAnswerIndicator(); //pg2-test

	if (PageInterface.GetValue("allowCAI") && this.showCorrectAnswerIndicator)
	{
		var inputsValues = this.inputsCorrectValues.split("||");
		
		for (var i=0; i < inputsValues.length; i++){
			if (inputsValues != ''){
				var splited = inputsValues[i].split('#!#');
				var name = splited[0].replace('specialID', '');
				var obj = document.getElementById(this.id+name);	
				var corrValues = splited[1].split('#');
				if (obj) {
					for (var j=0; j < corrValues.length; j++){		
						if (this.isInArray(obj.value, corrValues) || this.isInArray(this.valBackup[obj.id], corrValues)){
							obj.style.backgroundColor = this.correctColor; //pg2- backgroun 2 backgroundColor
						}else{
							obj.style.backgroundColor = this.incorrectColor; //pg2- backgroun 2 backgroundColor
						};
						var objAI = document.getElementById(obj.id + "_AI"); //pg2-test3
						if (objAI) { //pg2-test3 -otherwise it doesn't work in Firefox 2.0.0.20 (etc), just Firefox 6.0.2
							objAI.style.display = "block";
							objAI.style.backgroundColor = obj.style.backgroundColor;
							objAI.style.background = obj.style.backgroundColor;
						}
						//var objAI = document.getElementById(obj.id + "_AI");
						//if (objAI) objAI.style.backgroundColor = obj.style.backgroundColor;
					};
				}
				var id = splited[0].split('_');
				this.showWHYButton(id[1], name);	
			};
		};
	} else
	if (page.solved == true){
		var inputsValues = this.inputsCorrectValues.split("||");
		for (var i=0; i < inputsValues.length; i++){
			if (inputsValues != ''){
				var splited = inputsValues[i].split('#!#');
				var name = splited[0].replace('specialID', '');
				var obj = document.getElementById(this.id+name);
				if (obj) {
					obj.style.background = this.backgroundInputColor;
					if (this.valBackup[obj.id]) obj.value = this.valBackup[obj.id]; //pg2-test5
					this.showAdditionalCAI = false;
				}
				var id = splited[0].split('_');
				this.showWHYButton(id[1], name);	
			};
		};
	}
	
	var t = window.setTimeout("pc."+this.id+"._updateAnswerIndicator();", 1); //pg2-test
};

componentTextWithBlanks.prototype.isInArray = function(value, arr)
{
	for(var i=0; i < arr.length; i++){
		if (value == arr[i]) return true;
	}
	return false;
};


componentTextWithBlanks.prototype.lock = function(index)
{
	this._createAnswerIndicator(); //pg2-test
	
	var arr = this.inputsCorrectValues.split("||");
	var found = 0;
	var counter = 0;
	do{
		var obj = document.getElementById(this.id+'inputInsideText_'+counter);
		var addButton = document.getElementById(this.id+'bttonAddInput_'+counter);
		if (obj)  {
			obj.disabled = true;
			if (addButton) this.hideAddButton(addButton, 'out');
			found++;
		};
		counter++;
	}while (found < arr.length && counter < 100);
	
	this._updateAnswerIndicator(); //pg2-test
};

componentTextWithBlanks.prototype.unlock = function()
{
	var arr = this.inputsCorrectValues.split("||");
	var found = 0;
	var counter = 0;
	do{
		var obj = document.getElementById(this.id+'inputInsideText_'+counter);
		var addButton = document.getElementById(this.id+'bttonAddInput_'+counter);
		if (obj)  {
			obj.disabled = false;
			if (addButton) this.hideAddButton(addButton, 'in');
			found++;
		};
		counter++;
	}while (found < arr.length && counter < 100)
	
	this.complete = false;
	
	this._updateAnswerIndicator();
};

componentTextWithBlanks.prototype.componentTextWithBlanksBaseUnselectable = componentTextWithBlanks.prototype.unselectable;
componentTextWithBlanks.prototype.unselectable = function()
{
	this.componentTextWithBlanksBaseUnselectable();
	var arr = this.inputsCorrectValues.split("||");
	var found = 0;
	var counter = 0;
	do{
		var obj = document.getElementById(this.id+'inputInsideText_'+counter);
		if (obj)  {
			obj.unselectable = "";
			found++;
		};
		counter++;
	}while (found < arr.length && counter < 100)	
};

componentTextWithBlanks.prototype.componentTextWithBlanksBaseResetElement = componentTextWithBlanks.prototype.resetElement;
componentTextWithBlanks.prototype.resetElement = function()
{
	this.componentTextWithBlanksBaseResetElement();

	var arr = this.inputsCorrectValues.split("||");
	var found = 0;
	var counter = 0;
	do{
		var obj = document.getElementById(this.id+'inputInsideText_'+counter);
		if (obj)  {
			obj.value = "";
			obj.style.color = "";
			obj.style.backgroundColor = '';
		};
		counter++;
	}while (found < arr.length && counter < 100);
	this._clearAnswerIndicator(); //pg2-test2
}

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

componentTextWithBlanks.prototype.parseCheckAnswer = function(index)
{
	var inputsValues = this.inputsCorrectValues.split("||");
	var result = true;
	this.showAdditionalCAI = true; //pg2-test2
	for (var i=0; i < inputsValues.length; i++){
		if (inputsValues != ''){
			var splited = inputsValues[i].split('#!#');
			var name = splited[0].replace('specialID', '');
			var obj = document.getElementById(this.id+name);		
			var corrValues = splited[1].split('#');
			
			for (var j=0; j < corrValues.length; j++){		
				if (this.isInArray(obj.value, corrValues) || this.isInArray(this.valBackup[obj.id], corrValues)) { //*
					result = true;
					break;				
				}else{
					result = false;
				};
			};			
			if (!result) return result;
		};
	};
	return result;
};

componentTextWithBlanks.prototype.setInfoButtons = function(index)
{

	var arr = this.inputsCorrectValues.split("||");
	var found = 0;
	do{
		var obj = document.getElementById(this.id+'bttonAddInput_'+found);
		if (obj)  {
			obj.src = resourcesURL + this.buttonInfo_srcOut;
			obj.style.visibility = 'inherit';
		};
		found++;
	}while (found < arr.length)
};

componentTextWithBlanks.prototype.showWHYButton = function(index, name)
{
	var id = 'inputInsideText_'+index;
	var splitedButtonsAndPopups = this.buttonsAndPopups.split('||');
	for (var i=0; i < splitedButtonsAndPopups.length; i++){
		var splited = splitedButtonsAndPopups[i].split('#!#');
		if (splited[0] == id) {
			var obj = document.getElementById(this.id+'inputWhy_'+index);		
			if (obj) obj.style.visibility = 'inherit';
		}
	};
};

componentTextWithBlanks.prototype.hideAddButton = function(obj, action)
{

	if (obj) {
		obj.name = 'hide';
		if (action == 'out'){
			this.mouseOutAdd(obj);
		}else{
			this.mouseOverAdd(obj);
		};
	}
};

componentTextWithBlanks.prototype.showAddButton = function(obj, action)
{
	
	if (obj) {
		obj.name = 'show';
		if (action == 'out'){
			this.mouseOutAdd(obj);
		}else{
			this.mouseOverAdd(obj);
		};
	}
};

componentTextWithBlanks.prototype.getNextLetter = function(value, correct)
{
	var inputValue = value;
	var correctValue = correct;
	var splitedValue = inputValue.split('');
	var splitedCorr = correctValue.split('');
	return (value+splitedCorr[splitedValue.length]);
};

componentTextWithBlanks.prototype.checkIsCorrectValue = function(value, index)
{
	var inputValue = value;
	var splitedValue = inputValue.split('');
	var result = false;
	for (var i=0; i < this.correctValues[index].length; i++){
		var correct = this.correctValues[index][i].split('');
		if (value == '') return this.correctValues[index][i];
		for (var j=0; j < splitedValue.length; j++){
			if (splitedValue[j] == correct[j]){
				result = true;
			}else {
				result = false;
				break;
			};
			if (j == eval(splitedValue.length-1) && result) return this.correctValues[index][i];
		};
	};
	return '';
};

componentTextWithBlanks.prototype.getCorrectValues = function(text)
{
	var value = text;
	var str ='';
	var inputIdAndValues = value.split('||');
	for (var i=0; i < inputIdAndValues.length; i++){
		var values = inputIdAndValues[i].split('#!#');
		var idArr = values[0].split('_');
		this.correctValues[idArr[1]] = values[1].split('#');
		str += idArr[1] + ' | ';
	};
	return ;
};

componentTextWithBlanks.prototype.keyUp = function(keyUpObj)
{
	if (this.complete) 
	var name = '';
	
	name = keyUpObj.id.toString();
	var id = name.split('_');
	
	var addButton = document.getElementById(this.id+'bttonAddInput_'+id[1]);
	if (keyUpObj){
		var correctValue = this.checkIsCorrectValue(keyUpObj.value, id[1]);	
		if (correctValue != ''){
			this.setColor(keyUpObj, this.correctColor);
			
			if (correctValue == keyUpObj.value)
				this.hideAddButton(addButton, 'out');
			else 
				this.showAddButton(addButton, 'out');
		}else{
			this.setColor(keyUpObj, this.incorrectColor);
			this.hideAddButton(addButton, 'out');
		};
	};
};

componentTextWithBlanks.prototype.mouseClickWhy = function(clickedObj)
{
	var name = '';
	name = clickedObj.id.toString();
	var spliteID = name.split('_');	
	
	var id = 'inputInsideText_'+spliteID[1];
	var splitedButtonsAndPopups = this.buttonsAndPopups.split('||');
	for (var i=0; i < splitedButtonsAndPopups.length; i++){
		var one = splitedButtonsAndPopups[i].split('#!#');
		if(one[0] == id){
			if (page.all[one[1]]) page.all[one[1]].show();
			break;
		}
	};
};

componentTextWithBlanks.prototype.mouseOverInfo = function(obj)
{

	obj.src = resourcesURL + this.buttonInfo_srcIn;
};

componentTextWithBlanks.prototype.mouseOutInfo = function(obj)
{

	obj.src = resourcesURL + this.buttonInfo_srcOut;
};

componentTextWithBlanks.prototype.mouseClickInfo = function(obj)
{
	var name = '';
	name = obj.id.toString();
	var spliteID = name.split('_');	
	var splited = this.buttonsAndPopups.split('||');
	for(var i=0; i < splited.length; i++){
		var one = splited[i].split("#!#");
		if ('inputInsideText_'+spliteID[1] == one[0]){
			if (page.all[one[2]]) page.all[one[2]].show();
		};
	};
};

componentTextWithBlanks.prototype.getAnswer = function()
{

	var arr = this.inputsCorrectValues.split("||");
	var result = '';
	var sep = '';
	
	for(var i=0; i < arr.length; i++){
		var one = arr[i].split('#!#');
		var objName = one[0].replace('specialID', this.id);
		var obj = document.getElementById(objName);
		if (result != '') sep = '||';
		var ans = "";
		if (obj) {
			ans = obj.value;
			if (ans == "" && typeof(this.valBackup) != "undefined" && this.valBackup[obj.id] != "" && typeof(this.valBackup[obj.id]) != "undefined") {
				ans = this.valBackup[obj.id];
			}
			result += sep + obj.id + '#!#' + ans; //obj.value;
		};
	};
	return result;
};

componentTextWithBlanks.prototype.setAnswer = function(valStr, showCorrect)
{	
	if ((typeof(valStr) == 'undefined' || valStr == '') && !showCorrect) return;
	if (valStr == '' && (typeof(showCorrect) == 'undefined' || showCorrect == '')) showCorrect = true;
	
	if (!showCorrect){
		var arr = valStr.split("||");
		for(var i=0; i < arr.length; i++){
			var one = arr[i].split('#!#');
			var obj = document.getElementById(one[0]);
			
			if (obj){
				obj.value = one[1];
				this.valBackup[obj.id] = obj.value; //*
				var objAI = document.getElementById(obj.id + "_AI");
				if (objAI) objAI.style.backgroundColor = obj.style.backgroundColor;
			};
			var id = one[0].split('_');
			this.showWHYButton(id[1], '');
		};		
	}else{
		var arr = this.inputsCorrectValues.split("||");
		for(var i=0; i < arr.length; i++){
			var one = arr[i].split('#!#');
			var objName = one[0].replace('specialID', this.id);
			var obj = document.getElementById(objName);
			
			if (obj){
				var values = one[1].split('#');
				obj.value = values[0];
				this.valBackup[obj.id] = obj.value;
				this.setColor(obj, this.correctColor);
				this.valBackup[obj.id] = obj.value; //*
				//obj.value = ''; //pg2-test4
				var objAI = document.getElementById(obj.id + "_AI");
				if (objAI) {
					//if (PageInterface.GetValue("allowCAI") && this.showCorrectAnswerIndicator) obj.style.backgroundColor = this.correctColor;
					objAI.style.backgroundColor = obj.style.backgroundColor;
					if (page.pageStatus != 'passed' && page.pageStatus != 'failed') {
						this.showAdditionalCAI = false; //pg2-test2
					} else
					if (PageInterface.GetValue("allowCAI") && this.showCorrectAnswerIndicator) {
						this.showAdditionalCAI = true;
						obj.style.backgroundColor = this.correctColor;
						objAI.style.backgroundColor = this.correctColor;
					}
				}
			};
			var id = one[0].split('_');
			this.showWHYButton(id[1], '');
		};
	};
};

componentTextWithBlanks.prototype.mouseClickAdd = function(clickedObj)
{
	if (clickedObj.name == 'hide') return;
	var name = '';
	name = clickedObj.id.toString();
	
	var spliteID = name.split('_');	
	if (!this.complete){
		var obj = document.getElementById(this.id+'inputInsideText_'+spliteID[1]);
		if (obj){
			this.setColor(obj, 'transparent')
			var correctValue = this.checkIsCorrectValue(obj.value, spliteID[1]);	
			if (correctValue != '' && correctValue != obj.value) {
				var newValue = this.getNextLetter(obj.value, correctValue)
				obj.value = newValue;
			};
			if (obj.value == correctValue){
				this.hideAddButton(clickedObj, 'out');
			}else{
				this.showAddButton(clickedObj, 'in');
			};
			if (correctValue != '') {
				this.setColor(obj, this.correctColor);
			}else {
				this.setColor(obj, this.incorrectColor);
			};
		};
		this.iTimerID = setTimeout('document.getElementById("'+this.id+'inputInsideText_'+spliteID[1]+'").focus()', 100);
	};
};

componentTextWithBlanks.prototype.setColor = function(obj, color)
{
	if (this.automaticCheck){
		obj.style.backgroundColor = color; //pg2- instead of 'background'
	};
};

componentTextWithBlanks.prototype.mouseOverAdd = function(obj)
{

	if (obj.name == 'hide'){
		obj.src = resourcesURL + this.buttonAddInactive_srcIn;
	}else
	obj.src = resourcesURL + this.buttonAdd_srcIn;
};

componentTextWithBlanks.prototype.mouseOutAdd = function(obj)
{

	if (obj.name == 'hide'){
		obj.src = resourcesURL + this.buttonAddInactive_srcOut;
	}else
	obj.src = resourcesURL + this.buttonAdd_srcOut;
};

componentTextWithBlanks.prototype.mouseOverWhy = function(obj)
{

	obj.src = resourcesURL + this.buttonWHY_srcIn;
};

componentTextWithBlanks.prototype.mouseOutWhy = function(obj)
{
	obj.src = resourcesURL + this.buttonWHY_srcOut;
};

componentTextWithBlanks.prototype.onFree = function() {

	if (this.iTimerID != null) 
		window.clearTimeout(this.iTimerID);
}

componentTextWithBlanks.prototype.interactionsCorrectResponsesCount = function()
{
	var result = 0;
	var inputsValues = this.inputsCorrectValues.split("||");
	
	for (var i=0; i < inputsValues.length; i++){
		if (inputsValues != ''){
			var splited = inputsValues[i].split('#!#');
			var corrValues = splited[1].split('#');
			
			result += corrValues.length;
		};
	};
	return result;
};

componentTextWithBlanks.prototype.interactionsCorrectResponses = function(index)
{
	var result = "";
	var sep = "";
	var inputsValues = this.inputsCorrectValues.split("||");
	
	for (var i=0; i < inputsValues.length; i++){
		if (result != '') sep = '||';
		var splited = inputsValues[i].split('#!#');
		var objName = splited[0].replace('specialID', this.id);
		var corrValues = splited[1].split('#');
		
		result += sep + objName + '#!#' + corrValues[0];
	};
	return result;
};

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

componentTextWithBlanks.prototype.interactionsCorrectResponsesAsText = function(index)
{
	var result = "";
	var sep = "";
	var inputsValues = this.inputsCorrectValues.split("||");
	
	for (var i=0; i < inputsValues.length; i++){
		if (result != '') sep = '||';
		var splited = inputsValues[i].split('#!#');
		var objName = splited[0].replace('specialID', this.id);
		var corrValues = splited[1].split('#');
		
		result += sep + objName + '#!#' + corrValues[0];
	};
	return result;
};

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

componentTextWithBlanks.prototype._createAnswerIndicator = function()
{
	if (this.showCorrectAnswerIndicator) {	
		var inputsValues = this.inputsCorrectValues.split("||");		
		for (var i=0; i < inputsValues.length; i++) {
			var one = inputsValues[i].split("#!#");
			var name = one[0].replace('specialID', this.id);
			var obj = document.getElementById(name);

			var newE = document.getElementById(name + "_AI");
			if (!newE) {
				var newE = document.createElement('div');
				newE.id = name + "_AI";
				newE.className = this.contentObj.className;
				newE.innerHTML = "<div id='"+(name+"_AIV")+"' style='padding-left:5px;padding-top:2px;'>"+obj.value+"</div>";
				newE.style.display = "none";
				newE.style.position = "absolute";
				var size = getElementSize(obj);
				newE.style.left = size.x + "px";
				newE.style.top = size.y + "px";
				newE.style.width = size.w + "px";
				newE.style.height = size.h + "px";
				newE.style.backgroundColor = "#cccccc";
				this.obj.appendChild(newE);
			}
		}
	}
}

componentTextWithBlanks.prototype._updateAnswerIndicator = function()
{
	if (this.showCorrectAnswerIndicator) {// && this.showAdditionalCAI) {
		this._createAnswerIndicator();
		if (this.showAdditionalCAI) {
			var inputsValues = this.inputsCorrectValues.split("||");		
			for (var i=0; i < inputsValues.length; i++) {
				var one = inputsValues[i].split("#!#");
				var name = one[0].replace('specialID', this.id);
				var obj = document.getElementById(name);
				var objAI = document.getElementById(name + "_AI");
				var objAIV = document.getElementById(name + "_AIV");
				if (objAIV && PageInterface.GetValue("allowCAI")) { //pg2-modyfication
					if (this.valBackup[obj.id] && this.valBackup[obj.id] != "") {
						objAIV.innerHTML = this.valBackup[obj.id];
						obj.value = ''; //pg2-test
					} else {
						objAIV.innerHTML = obj.value;
						this.valBackup[obj.id] = obj.value;
						obj.value = '';
					}
				}
				if (objAI) {
					var size = getElementSize(obj);
					objAI.style.left = size.x + "px";
					objAI.style.top = size.y + "px";
					objAI.style.width = size.w + "px";
					objAI.style.height = size.h + "px";
					objAI.style.backgroundColor = obj.style.backgroundColor;
					objAI.style.display = (obj.disabled) ? "inline-block":"none";
					if (objAI.style.display == "none" && this.valBackup[obj.id]) obj.value = this.valBackup[obj.id]; //pg2-test7
				}
			}
			this.showAdditionalCAI = false; //pg2-test2
		}
	}
}

componentTextWithBlanks.prototype._clearAnswerIndicator = function()
{
	var inputsValues = this.inputsCorrectValues.split("||");
	for (var i=0; i<inputsValues.length; i++) {
		var one = inputsValues[i].split("#!#");
		var name = one[0].replace('specialID', this.id);
		var obj = document.getElementById(name);
		var objAI = document.getElementById(name + "_AI");
		var objAIV = document.getElementById(name + "_AIV");
		if (obj) {
			this.valBackup[obj.id] = '';
			if (objAI && objAIV) {
				objAI.style.backgroundColor = obj.style.backgroundColor;
				objAIV.innerHTML = '';
			}
		}
	}
}
