/*
 * Copyright (C) 1995-2011 by 4system Polska Sp. z o.o.
 * All Rights Reserved.
 */
 
componentComboInText = function(name)
{
	this.__componentText = __componentText;
	this.__componentText(name);
	
	this.correctValues = new Array(new Array(), new Array());
	this.complete = false;
	this.buttons = new Array();
	
	this.interactions = true;
	this.interactionsType = "numeric";		
};

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

componentComboInText.prototype.componentComboInTextBaseOnCreate = componentComboInText.prototype.onCreate;
componentComboInText.prototype.onCreate = function(obj)
{
	if (PageInterface.GetValue("state") != psDesigning &&  lms && this.disableOnlineOfflineMix != false ) 
	{
		this.resourcesStatus 	= lms.resourcesStatus();
		this.resourcesURL 		= lms.resourcesURL();
	} else this.resourcesURL = resourcesURL;
	
	if (PageInterface.GetValue("state") != psDesigning) {
		this.text = this.setComboOnChange(this.text);
	};
	this.componentComboInTextBaseOnCreate();
	
	var tempX = this.x + 3;
	var tempY = this.y + 3;		
	var imageHeigth = 0;
	var imageWidth = 0;	
	
	if (this.playAudio == 'click'){
		var audioButtonsArr = new Array(this.audioPlay, this.audioPause, this.audioStop, '_play_', '_pause_', '_stop_', 'Play', 'Pause', 'Stop');		
		for (var i=0; i < 3; i++){
			var image = new componentImage;
			image.myParent = this; 
			image.id = this.id+audioButtonsArr[i+3]+i; 
			image.x = tempX;
			image.y = tempY;
			var imageTest = new Image();
			imageTest.src = this.resourcesURL + audioButtonsArr[i];
			image.w = imageTest.width;
			image.h = imageTest.height;
			image.src = audioButtonsArr[i];
			image.cursor = 'hand';
			
			image.captureMouseEvents = this.captureMouseEvents;
			var funct = 'this.onMouseClickImage'+audioButtonsArr[i+6];
			image.eventClick = eval(funct);
			
			page.addChild(image);
			this.buttons.push(image);
			
			if (image.h > imageHeigth) imageHeigth = image.h;
			tempX +=  image.w + 5;
		};
		
		var backupText = this.text;
		var rows = '<table height="'+eval(imageHeigth+10)+'"><tr><td></td></tr></table>';
		
		this.text = rows + backupText;
	};
};

componentComboInText.prototype.componentComboInTextBaseOnPaint = componentComboInText.prototype.onPaint;
componentComboInText.prototype.onPaint = function()
{
	if (this.locked) return;
	this.componentComboInTextBaseOnPaint();
}

componentComboInText.prototype.editorPaint = function()
{
	return '';
}

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

componentComboInText.prototype.setComboOnChange = function(text)
{
	text = text.replace(/InputOnChange/g,  	 'onChange="pc.'+this.id+'.comboOnChange(this);"');
	
	text = text.replace(/specialID/g, this.id);	
	return text;
};

componentComboInText.prototype.onRepaint = function(index)
{
	this.complete = true;
	//this.setInfoButtons();	
	
	if (PageInterface.GetValue("allowCAI") && this.showCorrectAnswerIndicator) // && this.stopTimeAfterCheck) //pg2-removed
	{	
		var inputsValues = this.inputsCorrectValues.split("||");		
		for (var i=0; i < inputsValues.length; i++){
			var one = inputsValues[i].split("#!#");
			var correct = one[1].split('#');
			var name = one[0].replace('specialID', this.id);
			var obj = document.getElementById(name);
			var value = obj.options[obj.selectedIndex].text;
			for (var j=0; j < correct.length; j++){
				if (correct[j].indexOf('+') != -1) var _Word = true 
				else var _Word = false;
				var onlyWord = this.sliceBeginsCharsFromStrings('+', correct[j]);
				onlyWord = this.sliceBeginsCharsFromStrings('-', onlyWord);
				if (onlyWord == obj[obj.selectedIndex].text){
					if (_Word){
						this.setColor(obj, this.correctColor);
					}else{
						this.setColor(obj, this.incorrectColor);
					};
					break;
				};			
			};			
		};
	}else if (page.solved == true) {
		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);
			obj.style.background = this.backgroundSelectColor;
		};
	};
};

componentComboInText.prototype.lock = function(index)
{
	var arr = this.inputsCorrectValues.split("||");
	var found = 0;
	var counter = 0;
	do{ 
		var obj = document.getElementById(this.id+'ComboInsideText_'+counter);
		if (obj){
			obj.disabled = true;
			found++;
		};
		counter++;
	}while (found < arr.length && counter < 100)
	this.locked = true;	
};

componentComboInText.prototype.unlock = function(index)
{
	var arr = this.inputsCorrectValues.split("||");
	var found = 0;
	var counter = 0;
	do{ 
		var obj = document.getElementById(this.id+'ComboInsideText_'+counter);
		if (obj){
			obj.disabled = false;
			found++;
		};
		counter++;
	}while (found < arr.length && counter < 100)
	
	this.locked = false;	
	this.complete = false;
};

componentComboInText.prototype.checkAnswer = function(index)
{
	if (this.showAnswerIndicatorAnyTime && this.showCorrectAnswerIndicator) this.colorMyCombos();
	return(this.parseCheckAnswer(this));
};

componentComboInText.prototype.colorMyCombos = function(index)
{
	var inputsValues = this.inputsCorrectValues.split("||");
	var result = true;
	
	for (var i=0; i < inputsValues.length; i++){
		var one = inputsValues[i].split("#!#");
		var correct = one[1].split(',');
		var name = one[0].replace('specialID', this.id);
		var obj = document.getElementById(name);
		var corrValue = one[1].split("#");

		for (var j=0; j < corrValue.length; j++){
			var clearWord = this.sliceBeginsCharsFromStrings('+', corrValue[j]);
			clearWord = this.sliceBeginsCharsFromStrings('-', clearWord);
			if (clearWord == obj[obj.selectedIndex].text){
				if (corrValue[j].indexOf('+') != -1){
					obj.style.background = this.correctColor;
					break;
				}else{
					obj.style.background = this.incorrectColor;
				};
			};
		};
	};
};

componentComboInText.prototype.sliceBeginsCharsFromStrings = function(letter, string)
{
	var arr = string.split('');
	while (arr[0] == letter) {
		arr = arr.slice(1, arr.length);
	};
	return arr.join('');
}

componentComboInText.prototype.parseCheckAnswer = function(index)
{
	var inputsValues = this.inputsCorrectValues.split("||");
	var result = false;
	var clearWord = '';
	for (var i=0; i < inputsValues.length; i++){
		var one = inputsValues[i].split("#!#");
		var correct = one[1].split(',');
		var name = one[0].replace('specialID', this.id);
		var obj = document.getElementById(name);
		var corrValue = one[1].split("#");
		
		for (var j=0; j < corrValue.length; j++){
			clearWord = corrValue[j];
			if (clearWord.substr(0,1) == '+'){
				clearWord = clearWord.substr(1,clearWord.length-1)
			}else if (clearWord.substr(0,1) == '-'){
				clearWord = clearWord.substr(1,clearWord.length-1)
			}			
			if (clearWord == obj[obj.selectedIndex].text){
				if (corrValue[j].indexOf('+') != -1) result = true;
					else result = false;
			};
		};
		if (!result) return result;
	};
	return result;
};

componentComboInText.prototype.checkThisCombo = function(obj)
{
	var inputsValues = this.inputsCorrectValues.split("||");
	var result = true;
	for (var i=0; i < inputsValues.length; i++){
		var one = inputsValues[i].split("#!#");
		var name = one[0].replace('specialID', this.id);
		var corrValue = one[1].split("#");
		if (obj.id == name){
			for (var j=0; j < corrValue.length; j++){
				var word = corrValue[j].replace('+', '');
				word = word.replace('-', '');
				if (word == obj[obj.selectedIndex].text){
					if (corrValue[j].indexOf('+') != -1) return true;
						else return false;
				};
			};
		};
	};
};

componentComboInText.prototype.comboOnChange = function(obj)
{
	if (!this.automaticCheck) {
		this.setColor(obj, this.backgroundSelectColor); 
		return;
	}
	
	if (this.checkThisCombo(obj)){
		this.setColor(obj, this.correctColor);
	}else{
		this.setColor(obj, this.incorrectColor);
	};
};

componentComboInText.prototype.setColor = function(obj, color)
{
	obj.style.background = color;
};

componentComboInText.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[obj.selectedIndex].text;
		};
	};
	return result;
};

componentComboInText.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 select = document.getElementById(one[0]);
			if (select){
				for (var j=0; j< select.length; j++)
					if (select.options[j].text == one[1])
						select.selectedIndex = j;			
			};
		};		
	}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 select = document.getElementById(objName);
			
			if (select){
				var values = one[1].split('#');
				var j = -1;
				do{
					j++;
				}while(values[j].indexOf('+') == -1)			
				
				var correctValue = values[j].replace('+', '');
				for (var j=0; j< select.length; j++){
					if (select.options[j].text == correctValue)
						select.selectedIndex = j;	
				};
				if (this.automaticCheck) this.setColor(select, this.correctColor);
					else this.setColor(select, this.backgroundSelectColor);
			};
		};	
	};
	this.onRepaint();
};

componentComboInText.prototype.interactionsCorrectResponsesCount = function()
{
	var result = 0;
	var arr = this.inputsCorrectValues.split('||');
	for(var i=0; i < arr.length; i++){
		var one = arr[i].split('#!#');
		var values = one[1].split('#');
		result += values.length;
	};	
	return result;
};

componentComboInText.prototype.interactionsCorrectResponses = function(index)
{
	var result = 0;
	var sep = "";
	var arr = this.inputsCorrectValues.split('||');
	for(var i=0; i < arr.length; i++){
		var one = arr[i].split('#!#');
		var values = one[1].split('#');
		if (result != '') sep = '||';
		result += sep + values[0];
	};	
	return result;
};

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

componentComboInText.prototype.interactionsCorrectResponsesAsText = function(index)
{
	var result = 0;
	var sep = "";
	var arr = this.inputsCorrectValues.split('||');
	for(var i=0; i < arr.length; i++){
		var one = arr[i].split('#!#');
		var values = one[1].split('#');
		if (result != '') sep = '||';
		result += sep + values[0];
	};	
	return result;
};

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