/*
 * Copyright (C) 1995-2011 by 4system Polska Sp. z o.o.
 * All Rights Reserved.
 */
 
componentBlanksInText = function(name)
{
	this.__componentText = __componentText;
	this.__componentText(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";		
};

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

componentBlanksInText.prototype.componentBlanksInTextBaseOnCreate = componentBlanksInText.prototype.onCreate;
componentBlanksInText.prototype.onCreate = function(obj)
{
	this.text = this.setInputWidth(this.text);
	
	if (PageInterface.GetValue("state") != psDesigning) {
		// this.text = this.textBackup;
		this.text = this.setButtonsActions(this.text);
		this.getCorrectValues(this.inputsCorrectValues);
	};	
	
	//this.componentBlanksInTextBaseOnCreate();
};
	
componentBlanksInText.prototype.setInputWidth = function(text)
{
	text = text.replace(/inpSize/g, 'size');
	
	return text;
}

componentBlanksInText.prototype.setButtonsActions = function(text)
{	
	text = text.replace(/InputOnKeyUp/g,  		 'onKeyup="pc.'+this.id+'.keyUp(this);"');
	text = text.replace(/specialID/g, this.id);
	text = text.replace(/value=/g, '');
	return text;
};

componentBlanksInText.prototype.componentBlanksInTextOnPaint = componentBlanksInText.prototype.onPaint;
componentBlanksInText.prototype.onPaint = function() 
{
	if (this.locked) return;
	this.componentBlanksInTextOnPaint();
}

componentBlanksInText.prototype.onRepaint = function(index)
{
	if (PageInterface.GetValue("allowCAI") && this.showCorrectAnswerIndicator) // && this.stopTimeAfterCheck) //pg2-removed
	{
		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('#');
				
				for (var j=0; j < corrValues.length; j++){		
					if (this.isInArray(obj.value, corrValues)){
						this.setInputBgColor(obj, this.correctColor);
						//obj.style.background = this.correctColor;
					}else{
						this.setInputBgColor(obj, this.incorrectColor);
						//obj.style.background = this.incorrectColor;
					};
				};
			};
		};
	}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);	
				//obj.style.background = this.backgroundInputColor;
				this.setInputBgColor(obj, this.backgroundInputColor);
			};
		};
	};
};

componentBlanksInText.prototype.isInArray = function(value, arr)
{
	if (this.ignoreWhiteChars) {
		var userAnsArr = value.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--] = ""
		};			
		value = userAnsArr.join("");
	}
	for(var i=0; i < arr.length; i++){
		if (this.ignoreWhiteChars && 
				(arr[i].substring(arr[i].length-1, arr[i].length) == '.' && value.substring(value.length-1, value.length) != '.') //pg2-lack of condition if value's last character isn't a dot
			) value += '.';	
		if (value == arr[i]) return true;
	}
	
	return false;
};

componentBlanksInText.prototype.lock = function(index)
{
	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.locked = true;	
};

componentBlanksInText.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.locked = false;	
	this.complete = false;
};

componentBlanksInText.prototype.checkAnswer = function(index)
{
	if (this.showAnswerIndicatorAnyTime) this.colorMyInputs();
	return(this.parseCheckAnswer(this));
};

componentBlanksInText.prototype.colorMyInputs = function()
{
	var inputsValues = this.inputsCorrectValues.split("||");
	var result = true;
	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)){
						//obj.style.background = this.correctColor;
						this.setInputBgColor(obj, this.correctColor);
						break;				
					}else{
						//obj.style.background = this.incorrectColor;			
						this.setInputBgColor(obj, this.incorrectColor);					
					};
				};			
			};
		};
	};
};

componentBlanksInText.prototype.parseCheckAnswer = function(index)
{
	var inputsValues = this.inputsCorrectValues.split("||");
	var result = true;
	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){
			var corrValues = splited[1].split('#');
			
			for (var j=0; j < corrValues.length; j++){		
				if (this.isInArray(obj.value, corrValues)){
					result = true;
					break;				
				}else{
					result = false;
				};
			};			
			if (!result) return result;
			}
		};
	};
	return result;
};

componentBlanksInText.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)
};

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

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

componentBlanksInText.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 '';
};

componentBlanksInText.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 ;
};

componentBlanksInText.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);
		}else{
			this.setColor(keyUpObj, this.incorrectColor);
		};
	};
};

componentBlanksInText.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 = '||';
		
		if (obj){
			result += sep + obj.id + '#!#' + obj.value;
		};
	};
	return result;
};

componentBlanksInText.prototype.setAnswer = function(valStr, showCorrect)
{	
	if ((typeof(valStr) == 'undefined' || valStr == '') && !showCorrect) return;
	if (valStr == '' && (typeof(showCorrect) == 'undefined' || showCorrect.toString() == '')) 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];
			};
		};		
	}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.setColor(obj, this.correctColor);
			};
		};
	};
	this.onRepaint();
};

componentBlanksInText.prototype.setInputBgColor = function(obj, color)
{
	if (color.indexOf("#") == -1) color = "#"+color;
	obj.style.background = color;	
}

componentBlanksInText.prototype.setColor = function(obj, color)
{
	if (this.automaticCheck){
		this.setInputBgColor(obj, color);
	}else if (this.showAnswerIndicatorAnyTime) {
		this.setInputBgColor(obj, this.backgroundInputColor);
	}
};

componentBlanksInText.prototype.onFree = function() {

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

componentBlanksInText.prototype.componentBlanksInTextBaseUnselectable = componentBlanksInText.prototype.unselectable;
componentBlanksInText.prototype.unselectable = function()
{
	this.componentBlanksInTextBaseUnselectable();
	
	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 (obj){
			obj.unselectable = "";
		};
	};
};

componentBlanksInText.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('#');
			
			if (result < corrValues.length) result = corrValues.length;
		};
	};
	return result;
};

componentBlanksInText.prototype.interactionsCorrectResponses = function(index)
{
	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 values = one[1].split('#');
		
		result += sep + objName + '#!#' + values[0];
	};
	return result;
};

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

componentBlanksInText.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 corrValues = splited[1].split('#');
		
		if (typeof(corrValues[index]) == 'undefined')
		{
			result += sep + corrValues[0];
		}else{
			result += sep + corrValues[index];
		}
	};
	return result;
};

componentBlanksInText.prototype.interactionsStudentResponseAsText = 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 = '||';
		
		if (obj){
			result += sep + obj.value;
		};
	};
	return result;
};




