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

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

componentTextWithCombo.prototype.componentTextWithComboBaseOnCreate = componentTextWithCombo.prototype.onCreate;
componentTextWithCombo.prototype.onCreate = function(obj)
{
	if (PageInterface.GetValue("state") != psDesigning) {
		this.text = this.setButtonsActions(this.text);
		
		var content = this.findContentObj();
		content.innerHTML = this.text;
		this.contentObj = content;
		this._createAnswerIndicator();
	};
	this.componentTextWithComboBaseOnCreate();
};

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

componentTextWithCombo.prototype._updateAnswerIndicator = function()
{
	if (this.showCorrectAnswerIndicator && PageInterface.GetValue("allowCAI")) { //pg2- added PageInterface(...) - if in a page is disabled, you can not display it
		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) {
					objAIV.innerHTML = objAIV.innerHTML = obj.options[obj.selectedIndex].text;;
				}
				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";
				}
			}
			this.showAdditionalCAI = false;
		}
	}
}

componentTextWithCombo.prototype._clearAnswerIndicator = function() //pg2-required
{
	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) {
			if (objAI && objAIV) {
				objAI.style.backgroundColor = obj.style.backgroundColor;
				objAIV.innerHTML = '';
			}
		}
	}
}

componentTextWithCombo.prototype.setButtonsActions = function(text)
{
	if (this.automaticCheck){
		text = text.replace(/InputOnChange/g,  	 'onChange="pc.'+this.id+'.comboOnChange(this);"');
	}else{
		text = text.replace(/InputOnChange/g,  	 'onChange="return;"');
	};
	
	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);	
	
	return text;
};

componentTextWithCombo.prototype.onRepaint = function()
{
	this.complete = true;
	this._createAnswerIndicator();
	//this.setInfoButtons();	
	
	if ((PageInterface.GetValue("allowCAI") || page.isExec) && this.showCorrectAnswerIndicator)
	{	
		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[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 = correct[j].substr(1);
				onlyWord = trim(onlyWord);
				if (onlyWord == obj[obj.selectedIndex].text){
					if (_Word){
						this.setColor(obj, this.correctColor);
					}else{
						this.setColor(obj, this.incorrectColor);
					};
					break;
				};
			};			
			var id = one[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++){
			var one = inputsValues[i].split("#!#");
			var name = one[0].replace('specialID', this.id);
			var obj = document.getElementById(name);
			obj.style.background = this.backgroundSelectColor;
			var id = one[0].split('_');
			this.showWHYButton(id[1], name);
		};
		this.showAdditionalCAI = false;
	};
	
	var t = window.setTimeout("pc."+this.id+"._updateAnswerIndicator();", 1);
};

componentTextWithCombo.prototype.getComputedStyle = function(_elem, _style)
{
	var computedStyle;
	if (typeof _elem.currentStyle != 'undefined') {
		computedStyle = _elem.currentStyle;
	} else {
		computedStyle = document.defaultView.getComputedStyle(_elem, null);
	}

	return computedStyle[_style];
}

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

componentTextWithCombo.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;
			obj.style.visibility = "visible";
			found++;
		};
		counter++;
	}while (found < arr.length && counter < 100)
	
	this.complete = false;
	this._updateAnswerIndicator();
};

componentTextWithCombo.prototype.componentTextWithComboBaseResetElement = componentTextWithCombo.prototype.resetElement;
componentTextWithCombo.prototype.resetElement = function()
{
	this.componentTextWithComboBaseResetElement();
	
	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);	
		if (obj) {
			obj.selectedIndex = 0;
			obj.style.color = "";
			obj.style.backgroundColor = '';
		}
	};
	
	this._clearAnswerIndicator(); //pg2-test2
}

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

componentTextWithCombo.prototype.parseCheckAnswer = function(index)
{
	this.showAdditionalCAI = true;
	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 corrValue = one[1].split("#");
		var value = '';
		
		for (var j=0; j < corrValue.length; j++){		
			value = corrValue[j].substr(1);
			value = trim(value);
			if (value == obj[obj.selectedIndex].text){
				if (corrValue[j].charAt(0) == '-') {
					return false;
				};
			};
		};
	};
	return true;
};

componentTextWithCombo.prototype.showWHYButton = function(index, name)
{
	var id = 'ComboInsideText_'+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+'ButtonWhy_'+index);		
			if (obj) obj.style.visibility = 'inherit';
		}
	};
};

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

componentTextWithCombo.prototype.checkThisCombo = function(obj)
{
	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 corrValue = one[1].split("#");
		var value = '';
		
		if (obj.id == name){
			for (var j=0; j < corrValue.length; j++){
				value = corrValue[j].substr(1);
				value = trim(value);
				if (value == obj[obj.selectedIndex].text){
					if (corrValue[j].charAt(0) == '+') return true;
					else return false;
				};
			};
		};
	};
};

componentTextWithCombo.prototype.comboOnChange = function(obj)
{
	if ((PageInterface.GetValue("allowCAI") || page.isExec) && this.showCorrectAnswerIndicator) { //pg2-otherwise on change CAI were displayed (before checking answer)
		if (!this.complete) {
			if (this.checkThisCombo(obj)){
				this.setColor(obj, this.correctColor);
			}else{
				this.setColor(obj, this.incorrectColor);
			};
		}
	}
};

componentTextWithCombo.prototype.setColor = function(obj, color)
{
	if (color.charAt(0) != "#") color = "#" + color;
	obj.style.background = color;
};

componentTextWithCombo.prototype.mouseClickInfo = function(clickedObj)
{
	var spliteID = clickedObj.id.split('_');	
	var id = 'ComboInsideText_'+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[2]]) page.all[one[2]].show();
			break;
		}
	};
	return;
};

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

componentTextWithCombo.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;
					var objAI = document.getElementById(select.id + "_AI");
					if (objAI) objAI.style.backgroundColor = select.style.backgroundColor;
				}
			};
			var id = one[0].split('_');
			this.showWHYButton(id[4], '');
		};		
	}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;	
				};
				//this.setColor(select, this.correctColor);
				var objAI = document.getElementById(select.id + "_AI");
				if (objAI) {
					if (page.pageStatus != 'passed' && page.pageStatus != 'failed') { //pg2-test8
						this.showAdditionalCAI = false; //pg2-test2
					} else
					if (PageInterface.GetValue("allowCAI") && this.showCorrectAnswerIndicator) {
						this.showAdditionalCAI = true;
						select.style.backgroundColor = this.correctColor;
						objAI.style.backgroundColor = this.correctColor;
					}
				}
			};
			var id = one[0].split('_');
			this.showWHYButton(id[1], '');
		};
	};
	
	//this.onRepaint();
};

componentTextWithCombo.prototype.mouseOverInfo = function(obj)
{
	obj.src = resourcesURL + this.buttonInfo_srcIn;
};

componentTextWithCombo.prototype.mouseOutInfo = function(obj)
{ 
	obj.src = resourcesURL + this.buttonInfo_srcOut;
};

componentTextWithCombo.prototype.mouseClickWhy = function(clickedObj)
{
	var spliteID = clickedObj.id.split('_');	
	var id = 'ComboInsideText_'+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;
		}
	};
};

componentTextWithCombo.prototype.mouseOverWhy = function(obj)
{
	obj.src = resourcesURL + this.buttonWHY_srcIn;
};

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

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

componentTextWithCombo.prototype.interactionsCorrectResponses = function(index)
{
	var result = "";
	var sep = "";
			
	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 values = one[1].split('#');
		
		var j = -1;
		do{
			j++;
		}while(values[j].indexOf('+') == -1)			
		var correctValue = values[j].replace('+', '');
		
		if (result != '') sep = '||';
		result += sep + objName + '#!#' + correctValue;
	};
	return result;
};

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

componentTextWithCombo.prototype.interactionsCorrectResponsesAsText = function(index)
{
	var result = "";
	var sep = "";
			
	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 values = one[1].split('#');
		
		var j = -1;
		do{
			j++;
		}while(values[j].indexOf('+') == -1)			
		var correctValue = values[j].replace('+', '');
		
		if (result != '') sep = '||';
		result += sep + objName + '#!#' + correctValue;
	};
	return result;
};

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