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

componentPolls_radioVertMulti = function(name) {
	this.componentPolls_radioVert = componentPolls_radioVert;
	this.componentPolls_radioVert(name);
	
	this.interactions 		= true;
	this.interactionsType 	= "performance";  		
	
	this.dotChecked = new Array();
	this.radiosLen = -1;
	this.restoredName = "";
};

componentPolls_radioVertMulti.prototype = new componentPolls_radioVert;
componentPolls_radioVertMulti.prototype.type = "componentPolls_radioVertMulti";

componentPolls_radioVertMulti.prototype.onCreate = function(obj) 
{
	this.captureMouseEvents();
	
	this.resourcesStatus = "online";
	this.resourcesURL = resourcesURL;
	if (PageInterface.GetValue("state") != psDesigning && lms && lms.name != "lmsOffline") {
		this.resourcesStatus = lms.resourcesStatus();
		if (this.disableOnlineOfflineMix) this.resourcesURL = resourcesURL;
		else this.resourcesURL = lms.resourcesURL();
	}
		
	this.resourcesURL = unescape(this.resourcesURL);
	
	this.imagesNameArr = [	this.imgChecked, this.imgNotChecked ];
	this.getPreloadImagesArr();
	
	this.pollQuestion = this.initDynamicContent(this.pollQuestion);
	this.columnsValues = this.initDynamicContent(this.columnsValues);
	this.columnsName = this.initDynamicContent(this.columnsName);
	this.comboValues = this.initDynamicContent(this.comboValues);
	this.inputTitle = this.initDynamicContent(this.inputTitle);
	
	var colCount = this.writeColumsName(this.columnsName);
	this.radiosLen = this.writeDots(this.columnsValues, colCount);
	this.writeColgroup(colCount);
};

componentPolls_radioVertMulti.prototype.writeColgroup = function(colCount)
{
	var colGR = document.getElementById(this.id + "dolGr");
	var col = null;
	var notUsedSpace = 70;
	var colWidth = parseInt(notUsedSpace/colCount);
	
	for (var i=0; i < colCount; i++)
	{
		col = document.createElement("COL");
		col.setAttribute('width', colWidth+"%");
		colGR.appendChild(col);
	}	
}

componentPolls_radioVertMulti.prototype.writeDots = function(values, columsCount)
{
	var dots = document.getElementById(this.id + "_dots");
	var dotsCell = null;
	var valuesArr = values.split("||");
	var table = null;
	var trUpper = null;
	var trLower = null;
	var cell = null;
	var img = null;
		
	for (var i=0; i < columsCount; i++)
	{
		table = document.createElement("TABLE");
		table.setAttribute('width', "100%");
		tbody = document.createElement("TBODY");
		if (this.showIconsText)
		{
			trUpper = document.createElement("TR");
		}
		trLower = document.createElement("TR");
		
		for (var j=0; j < valuesArr.length; j++)
		{
			if (trUpper) {
				cell = document.createElement("TD");
				cell.innerHTML = valuesArr[j];
				trUpper.appendChild(cell);
			}
			cell = document.createElement("TD");
			cell.imgSrcID = this.id+'#'+i+"#"+j;
			cell.compName = this.id;			
			cell.onclick = this.clickCell;
			img = document.createElement("IMG");
			img.setAttribute('id', this.id+'#'+i+"#"+j);
			img.setAttribute('src', this.resourcesURL + this.imgNotChecked);
			cell.appendChild(img);
			trLower.appendChild(cell);
		}
		if (trUpper) {
			tbody.appendChild(trUpper);
		}
		tbody.appendChild(trLower);
		table.appendChild(tbody);
		dotsCell = dots.insertCell(i+1);
		dotsCell.className = "separator";
		dotsCell.appendChild(table);
	}
	return valuesArr.length;
}

componentPolls_radioVertMulti.prototype.writeColumsName = function(values)
{
	var columsName = document.getElementById(this.id + "_columsName");
	var valuesArr = values.split("||");
	var cell = null;
	
	for (var i=0; i < valuesArr.length; i++)
	{
		cell = columsName.insertCell(i+1);
		cell.innerHTML = valuesArr[i];
		this.dotChecked[i] = -1;
	}
	return valuesArr.length;
}

componentPolls_radioVertMulti.prototype.writeComboValues = function(values)
{
	var createCombo = true;
	var valuesArr = values.split("||");	
	this.restoredName = valuesArr[0];
	
	var selTd = document.getElementById(this.id + "_select");
	selTd.innerHTML = "";
	
	if (this.writeNameInsteadCombo && (PageInterface.GetValue("state") != psDesigning)) {
		var lastName = lms.getCourseData(this.writeNameInsteadComboKey);
		if (lastName){
			this.restoredName = lastName;
			var select = document.createElement("SPAN");
			select.innerHTML = this.restoredName;
			createCombo = false;
		}
	}
	if (createCombo)
	{
		if (valuesArr.length < 2){
			var select = document.createElement("SPAN");
			select.innerHTML = this.restoredName;
		}else{
			var option = null;
			var select = document.createElement("SELECT");
			select.setAttribute('id', this.id+'_names');
			
			for (var i=0; i < valuesArr.length; i++)
			{
				option = document.createElement("OPTION");
				option.text = valuesArr[i];
				option.value = valuesArr[i];
				select.options.add(option);
			}	
		}
	}
	selTd.appendChild(select);
}

componentPolls_radioVertMulti.prototype.componentPolls_radioVertMultiBaseClickCell = componentPolls_radioVert.prototype.clickCell;
componentPolls_radioVertMulti.prototype.clickCell = function()
{
	var compObj = eval('pc.'+this.compName);
	if (compObj.locked) return;
	var valuesArr = compObj.columnsValues.split('||');
	
	var colIdArr = this.imgSrcID.split('#');
	compObj.cleanOtherFields(colIdArr[1]);
	compObj.dotChecked[colIdArr[1]] = parseInt(colIdArr[2]);
	
	var imgDot = document.getElementById(this.imgSrcID);
	imgDot.src = compObj.resourcesURL + compObj.imgChecked;	
}

componentPolls_radioVertMulti.prototype.cleanOtherFields = function(groupId)
{
	var rLen = this.radiosLen;
	if (this.notConcern){
		rLen += 1;
	};
	for (var i=0; i < rLen; i++){
		var imgDot = document.getElementById(this.id+"#"+groupId+"#"+i);
		imgDot.src = this.resourcesURL + this.imgNotChecked;
	};
};

componentPolls_radioVertMulti.prototype.onPaint = function(obj)
{
	this.writeComboValues(this.comboValues);
}	

componentPolls_radioVertMulti.prototype.checkAnswer = function() 
{
	if (this.writeNameInsteadCombo) {
		lms.setCourseData(this.writeNameInsteadComboKey, this.getComboValue());
	}
	return true;
}
componentPolls_radioVertMulti.prototype.getAnswer = function() 
{
	var select = document.getElementById(this.id+"_names");
	if (select){
		return select.selectedIndex + "," + this.dotChecked.toString();
	}else{
		return "," + this.dotChecked.toString();
	}
};

componentPolls_radioVertMulti.prototype.setAnswer = function(valStr) 
{
	if (typeof(valStr) == 'undefined' || valStr == '')return;	
	var valArr = valStr.split(',');
	var index = 0;
	var select = document.getElementById(this.id+"_names");
	if (select){
		select.selectedIndex = valArr[0];
	}
	
	for (var i=0; i < valArr.length-1; i++)
	{
		index = parseInt(valArr[i+1]);
		if (isNaN(index))
		{
			this.dotChecked[i] = -1;
		}else{
			this.dotChecked[i] = index;
		}
		var imgDot = document.getElementById(this.id+'#'+i+"#"+index);
		if (imgDot) imgDot.src = this.resourcesURL + this.imgChecked;			
	}
};

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

componentPolls_radioVertMulti.prototype.onLock = function() 
{
	obj = document.getElementById(this.id+"_names");
	if (obj) obj.disabled = true;
};

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

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

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

componentPolls_radioVertMulti.prototype.getComboValue = function()
{
	var result = "";
	var select = document.getElementById(this.id+"_names");
	
	if (select){
		result = select.options[select.selectedIndex].text;
	}else{
		result = this.restoredName;
	}
	
	return result;
}
componentPolls_radioVertMulti.prototype.interactionsStudentResponseAsText = function()
{
	var result = "";
	var colSep = "[,]";
	var rowSep = "[.]";
	var colsArr = this.columnsName.split("||");
	var choiceArr = this.columnsValues.split("||");
	var select = document.getElementById(this.id+"_names");
	
	result += this.comboDesc + rowSep;
	result += this.getComboValue() + colSep;
	
	for (var i=0; i < colsArr.length; i++)
	{
		result += colsArr[i] + rowSep;
		if (this.dotChecked[i] == -1)
		{
			result += "";
		}else{
			result += choiceArr[this.dotChecked[i]];
		}
		if (i < colsArr.length-1) result += colSep;
	};
	return result;
};

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