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

componentPollComboAnswer = function(name) {
	this.__componentText = __componentText;
	this.__componentText(name);

	this.interactions 		= true;
	this.interactionsType 	= "fill-in";  
};

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

componentPollComboAnswer.prototype.onCreate = function(obj) {
	this.captureMouseEvents();
	
	this.pollQuestion = this.initDynamicContent(this.pollQuestion);
	this.choiceValues = this.initDynamicContent(this.choiceValues);
	this.inputTitle = this.initDynamicContent(this.inputTitle);
	
	var arrOpt = this.choiceValues.split("#|#");
	this.obj.dir = this.dir;

	var width = this.w - this.borderWidth*2-5;
	var t = '<span style="color:' + this.fontColor + '; font-family:' + this.fontFamily + '; font-size:' + this.fontSize + ';">' + this.pollQuestion + '</span>';
	var j = 1;
	var comboName = "";

	var indexArray = arrOpt[0].split("||");
	var choicesArray = arrOpt[1].split("||");
	var comboName = indexArray[0];

	t += '<table style="width:' + width + 'px;" id="polls_combo_tab_' + this.id + '" dir="'+this.dir+'">';
	t += '<tr><td><select name="' + this.id + '_sel_1" style="width:' + width + 'px; color:' + this.fontColor + '; font-family:' + this.fontFamily + '; font-size:' + this.fontSize + ';"';
	t += ' onChange="pc.'+ this.id + '.invokeEvent(\'Change\');">';

	for (var i = 0; i < indexArray.length; i++) {
		if (indexArray[i] == comboName) {
			t += '<option>' + choicesArray[i] + '</option>';
		} else {
			j++;
			comboName = indexArray[i];
			t += '</select></td></tr>';
			t += '<tr><td><select name="' + this.id + '_sel_' + j.toString() + '" style="width:' + width + 'px; color:' + this.fontColor + '; font-family:' + this.fontFamily + '; font-size:' + this.fontSize + ';"';
			t += ' onChange="pc.'+ this.id + '.invokeEvent(\'Change\');">';
			t += '<option>' + choicesArray[i] + '</option>';  		
		}
	}
	this.choiceAmount = j;
	t += '</select></td></tr>';
  
	if (this.inputVisible) {
		t += '<tr><td colspan="2">';
		t += '<table><tr><td style="color:' + this.fontColor + '; font-family:' + this.fontFamily + '; font-size:' + this.fontSize + ';">';
		t += this.inputTitle + '</td>';
		t += '<td align='+this.textAlign+'><input id="' + this.id + '_openQuestion" style="width:'+this.inputWidth+';"></td></tr></table>';
		t += '</td></tr>';
	}  
	t += '</table>';	
	this.setHTML(t);
	
};

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

componentPollComboAnswer.prototype.getAnswer = function() {
	var form = document.getElementById( "polls_combo_tab_" + this.id );
	var valStr= "";
	var separator = "";
	var obj;

	if (form) {
		for (var i=0; i < (this.choiceAmount); i++) {
			obj = document.getElementsByName(this.id + "_sel_" + (i+1).toString());
			obj = obj[0];
			if (obj) {
				valStr += separator + obj[obj.selectedIndex].text;
				separator = "||";
			}
		}
	}
	//tu skonczylem
	if (this.inputVisible) {
		obj = document.getElementById(this.id + "_openQuestion");
		if(obj.value!="") valStr += "||[%Open%]:" + obj.value;
	}

	return(valStr);
};

componentPollComboAnswer.prototype.setAnswer = function(valStr) 
{
	var arrOpt =valStr.split("||");
	
	for (var i = 0; i < arrOpt.length; i++) {
		var obj = document.getElementById(this.id+"_sel_"+(i+1).toString());
		if (obj){
			for (var j = 0; j < obj.options.length; j++) {
				if (arrOpt[i] == obj.options[j].text) obj.selectedIndex = j;
			};
		};
	};
	if (this.inputVisible) {
		obj = document.getElementById(this.id + "_openQuestion");
		var inpVal = valStr.split("[%Open%]:")[1];
		if (obj && typeof(inpVal) != 'undefined') obj.value = inpVal;		
	}	
};

componentPollComboAnswer.prototype.parseCheckAnswer = function(obj) 
{
	if (!obj) return false;
	return true;
};

componentPollComboAnswer.prototype.onLock = function() 
{
	var obj;
	var indexArray = this.choiceValues.split("#|#");
	indexArray = indexArray[0].split("||");

	for (var i=0; i< indexArray.length; i++) {
		obj = document.getElementsByName(this.id+"_sel_"+i.toString());
		if (obj[0]) obj[0].disabled = true;
	}
	if (this.inputVisible) {
		obj = document.getElementById(this.id+"_openQuestion");
		if (obj) obj.disabled = true;
	}
	
	return(true); 
};

componentPollComboAnswer.prototype.onUnLock = function() 
{
	var obj;
	var indexArray = this.choiceValues.split("#|#");
	indexArray = indexArray[0].split("||");

	for (var i=0; i< indexArray.length; i++) {
		obj = document.getElementsByName(this.id+"_sel_"+i.toString());
		if (obj[0]) obj[0].disabled = false;
	}
	return(true); 
};

componentPollComboAnswer.prototype.onPaint = function(obj) {
	return true;
};

componentPollComboAnswer.prototype.baseUnselectable = componentPollComboAnswer.prototype.unselectable;
componentPollComboAnswer.prototype.unselectable = function() 
{
	this.baseUnselectable();
	if (this.inputVisible) {
		var input = document.getElementById(this.id + "_openQuestion");
		if (input){
			input.unselectable = "";
		};
	};
};

componentPollComboAnswer.prototype.interactionsCorrectResponses = function()
{
	return "";
};

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

componentPollComboAnswer.prototype.interactionsCorrectResponsesAsText = function()
{
	return "";
};

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

componentPollComboAnswer.prototype.interactionsCorrectResponsesCount = function()
{
	return 1;
};
