function Modules_Voting_Answer(id, answer) {
	this.id 	= id;
	this.answer	= answer;
}
	
function Modules_Voting_Answers(stylePath) {
	this.stylePath = stylePath;
	this.fields = new Array();
	this.listControl = document.getElementById('fieldsList').tBodies[0];
	this.lastStyle = 1;
	this.messageNewFiled  = SbGetLocaleByKey('NEW-ANSWER');
}

Modules_Voting_Answers.prototype.register = function(id, answer) {
	this.fields.push(new Modules_Voting_Answer(id, answer));
}

Modules_Voting_Answers.prototype.reBuildList = function(setFocusOnLast) {
	if (setFocusOnLast==null) {
		setFocusOnLast = false;
	}
	this.clearList();
	var fieldsNumb = this.fields.length;
	for (var i = 0; i < fieldsNumb; ++i) {
		var number = i+1;
		this.buildItem(number, this.fields[i], setFocusOnLast && number>=fieldsNumb);
	}
	//for (var i = 0; i<this.fields.length; ++i) {
		//this.buildItem(i+1, this.fields[i]);
	//}
}

Modules_Voting_Answers.prototype.reBuildHiddens = function() {
	this.clearHiddens();
	for (var i = 0; i<this.fields.length; ++i) {
		this.buildHiddens(i+1, this.fields[i]);
	}
}

Modules_Voting_Answers.prototype.buildItem = function(number, field, setFocus) {
	var trs = this.listControl.getElementsByTagName('TR');
	var tr = trs[this.lastStyle].cloneNode(true);
	tr.className = number%2 ? "sb-gridtext" : "sb-gridtext-alter";
	var tds = tr.getElementsByTagName('TD');
	tds[1].innerHTML = number;
	var control = tds[2].getElementsByTagName('INPUT')[0];
	control.name  = 'answers['+number+']';
	control.value = field.answer;
	control.onchange = function() {votingAnswers.setValue(number, this.value);};
	tds[3].innerHTML = this.getSeqUpImageControlHtml(number);
	tds[4].innerHTML = this.getSeqDownImageControlHtml(number);
	var control = tds[0].getElementsByTagName('INPUT')[0];
	control.name = number;
	tr.removeAttribute("style");
	this.listControl.appendChild(tr);
	if (setFocus) {
		if (document.activeElement) {
			document.activeElement.blur();
		}
		control.focus();
		control.select();
	}
	this.lastStyle = this.lastStyle==1 ? 2 : 1;
	this.buildHiddens(number, field);
}

Modules_Voting_Answers.prototype.buildHiddens = function(number, field) {
	var	hidden = document.createElement('INPUT');
	hidden.type = 'hidden';
	hidden.name = 'answersId['+number+']';
	hidden.value = field.id;
	this.listControl.appendChild(hidden);
}

Modules_Voting_Answers.prototype.clearList = function() {
	var trs = this.listControl.getElementsByTagName('TR');
	for (var i = trs.length-1; i>=2; --i) {
		this.listControl.removeChild(trs[i]);
	}
	this.clearHiddens();
	this.lastStyle = 1;
}

Modules_Voting_Answers.prototype.clearHiddens = function() {
	var inputs = this.listControl.getElementsByTagName('INPUT');
	for (var i = inputs.length-1; i>=0; --i) {
		if (inputs[i].type=='hidden') {
			this.listControl.removeChild(inputs[i]);
		}
	}
}

Modules_Voting_Answers.prototype.getSeqUpImageControlHtml = function(number) {
	return number<this.fields.length
		?	this.getSeqImageControlHtml(number, 'down', 'moveDown')
		:	'';
}

Modules_Voting_Answers.prototype.getSeqDownImageControlHtml = function(number) {
	return number>1
		?	this.getSeqImageControlHtml(number, 'up', 'moveUp')
		:	'';
}

Modules_Voting_Answers.prototype.getSeqImageControlHtml = function(number, direction, method) {
	return '<img style="border-width:0px;width:16px;height:16px;cursor:pointer;" src="'+this.stylePath+'/images/sequence-'+direction+'.gif" onclick="votingAnswers.'+ method+'(\''+number+'\');"/>'
}

Modules_Voting_Answers.prototype.moveUp = function(fieldId) {
	var tmp = this.fields[fieldId-2];
	this.fields[fieldId-2] = this.fields[fieldId-1];
	this.fields[fieldId-1] = tmp;
	this.reBuildList();
}

Modules_Voting_Answers.prototype.moveDown = function(fieldId) {
	var tmp = this.fields[fieldId];
	this.fields[fieldId] = this.fields[fieldId-1];
	this.fields[fieldId-1] = tmp;
	this.reBuildList();
}

Modules_Voting_Answers.prototype.setValue = function(number, value) {
	var field = this.fields[number-1];
	field.answer = value;
	if (SB_Validator.isEmpty(value)) {
		this.processErrorForEmptyAnswer(field);
		return false;
	}
	//this.reBuildHiddens();
}

Modules_Voting_Answers.prototype.validate = function() {
	for (var i = 0; i < this.fields.length; ++i) {
		var field = this.fields[i];
		if (SB_Validator.isEmpty(field.answer)) {
			this.processErrorForEmptyAnswer(field);
			return false;
		}
	}
	return true;
}

Modules_Voting_Answers.prototype.processErrorForEmptyAnswer = function(field) {
	if (document.activeElement) {
		document.activeElement.blur();
	}
	alert(SbGetLocaleByKey('FIELD-ANSWER-IS-EMPTY'));
	//field.nameControl.focus();
	//field.nameControl.select();
}

Modules_Voting_Answers.prototype.append = function() {
	this.register('new', this.messageNewFiled);
	this.reBuildList(true);
}

Modules_Voting_Answers.prototype.selectAll = function(val) {
	var inputs = this.listControl.getElementsByTagName('INPUT');
	for (var i = 0; i < inputs.length; ++i) {
		var input = inputs[i];
		if (input.type=='checkbox' && input.name) {
			input.checked = val;
		}
	}
}

Modules_Voting_Answers.prototype.deleteSelected = function() {
	var inputs = this.listControl.getElementsByTagName('INPUT');
	var toRemove = new Array(this.fields.length);
	for (var i = 0; i < inputs.length; ++i) {
		var input = inputs[i];
		if (input.type=='checkbox' && input.name) {
			toRemove[input.name] = input.checked ? 1 : 0;
		}
	}
	var tmp = new Array();
	var backup = this.fields;
	var removedNumb = 0;
	for (var i = 0; i < this.fields.length; ++i) {
		if (toRemove[i+1]==0) {
			tmp.push(this.fields[i]);
		} else {
			++removedNumb;
		}
	}
	if (removedNumb==0) {
		alert(SbGetLocaleByKey('EMPTY-DELETE-RECORDS'));
		return true;
	}
	if (removedNumb) {
		if (tmp.length<2) {
			alert(SbGetLocaleByKey('SELECT-RECORDS-FOR-DELETE'));
			return true;
		}
		var message = SbGetLocaleByKey('DELETE-RECORDS-CONFIRM');
		message = message.replace('%s', removedNumb);
		if (confirm(message)) {
			this.fields = tmp;
			this.reBuildList();
		}
	}
}