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

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

	this.waitingTryCount = 0;

	this.resourcesStatus = "online";
	this.resourcesURL = "";

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

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

componentPollMultiAnswer.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.choiceValues = this.initDynamicContent(this.choiceValues);
	this.inputTitle = this.initDynamicContent(this.inputTitle);
	this.pollQuestion = this.initDynamicContent(this.pollQuestion);
	
	var arrOpt = this.choiceValues.split("||");
	var arrLength = arrOpt.length;

	var width = this.w-this.borderWidth*2;
	var t = '<table style="width:'+width+'px; height:100%;" id="polls_multi_tab_'+this.id+'" dir="'+this.dir+'">';
	t += '<tr><td colspan="2" style="font-color:'+this.fontColor+'; font-family:'+this.fontFamily+'; font-size:'+this.fontSize+'">'+this.pollQuestion+'</td></tr>';
	
	// var t = '<span style="color:' + this.fontColor + '; font-family:' + this.fontFamily + '; font-size:' + this.fontSize + ';">' + this.pollQuestion + '</span>';
	// t += '<table style="width:' + width + 'px; height:100%;" id="polls_multi_tab_' + this.id + '" >';

	for(var i = 0; i < arrLength; i++){
		t += '<tr><td style="width:1%;">';
		t += '<img src="'+this.resourcesURL+this.imgNotChecked+'" name="'+this.id+'_img_'+i.toString()+'" onclick="this.owner.changeImg(this);">';
		t += '</td>';
		t += '<td align='+this.textAlign+' style="color:'+this.fontColor+'; font-family:'+this.fontFamily+'; font-size:'+this.fontSize+';" >';
		t += arrOpt[i];
		t += '</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);
	
};

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

componentPollMultiAnswer.prototype.getAnswer = function() 
{
	var form = document.getElementById( "polls_multi_tab_" + this.id );
	var i, obj;
	var valStr= "";
	var indexArr = this.choiceValues.toString().split("||");
	var separator = "";

	if (form) {
		for (i=0; i<indexArr.length; i++) {
			obj = document.getElementsByName(this.id+"_img_"+i.toString());
			if ( obj[0].src.match(this.imgChecked) ) {
				if (valStr != '') separator = "||";
				valStr += separator + indexArr[i].toString();
			};
		};
	};

	if (this.inputVisible) {
		obj = document.getElementById(this.id + "_openQuestion");
		if(obj.value!="") valStr += "||[%Open%]:" + obj.value;
	}

	return(valStr);
};

componentPollMultiAnswer.prototype.setAnswer = function(valStr) 
{
	var arrOpt = this.choiceValues.split("||");
	var arrLength = arrOpt.length;
	var valStrArr = valStr.split('||');
	
	for(var i = 0; i < arrLength; i++){
		if (this.isValueInArray(arrOpt[i], valStrArr)){
			var obj = document.getElementsByName(this.id+"_img_"+i.toString());
			if (obj[0]) obj[0].src = this.resourcesURL + this.imgChecked;
		};
	}
	
	if (this.inputVisible) {
		obj = document.getElementById(this.id + "_openQuestion");
		var inpVal = valStr.split("[%Open%]:")[1];
		if (obj && typeof(inpVal) != 'undefined') obj.value = inpVal;		
	}	
};

componentPollMultiAnswer.prototype.isValueInArray = function(value, array) 
{
	for(var i = 0; i < array.length; i++){
		if (array[i] == value) return true;
	};
	return false;
}

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

componentPollMultiAnswer.prototype.onLock = function() {
	var obj;
	for (var i=0; i< this.choiceValues.split("||").length; i++) {
		obj = document.getElementsByName(this.id+"_img_"+i.toString());
		if (obj[0]) obj[0].onclick = "javascript:void(0); return false;";
	}
	if (this.inputVisible) {
		obj = document.getElementById(this.id+"_openQuestion");
		if (obj) obj.disabled = true;
	}  
  return(true);
};

componentPollMultiAnswer.prototype.onUnLock = function() {
  var obj;
  for (var i=0; i< this.choiceValues.split("||").length; i++) {
	obj = document.getElementsByName(this.id+"_img_"+i.toString());
	if (obj[0]) obj[0].onclick = "this.owner.changeImg(this); return false;";
  }
  return(true); 
};

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

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

componentPollMultiAnswer.prototype.changeImg = function(obj) {
	if (this.locked) return;
	
	obj.src = this.resourcesURL + ((obj.src.match(this.imgChecked))?this.imgNotChecked:this.imgChecked);
  	this.answer = obj.id;
};

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

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

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

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

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