/**
 * Feedback fileds
 *
 * @author Dobrenko Andrey <adobrenko@swsoft.com>
 * @author Evgueni Palchukovsky <epalchukovsky@swsoft.com>
 * @since 2006/06/22
 * @package SB
 * @subpackage Feedback
 * @copyright (c) 2004-2006, SWsoft
 */

originalSubmitFunction = document.forms.SB_WizardFormBlockModule.submit;
document.forms.SB_WizardFormBlockModule.submit = FeedbackFieldsSubmit;

function FeedbackFieldsSubmit() {
	var isValid = feedbackFields.validate();
	if (isValid) {
		document.forms.SB_WizardFormBlockModule.submit = originalSubmitFunction;
		document.forms.SB_WizardFormBlockModule.submit();
	}
}

function Modules_Feedback_Field(name, type, isIncluded, isRequired, isReserved) {
	this.name = name;
	this.type = type;
	this.nameControl = null;
	this.isIncluded = isIncluded;
	this.isRequired = isRequired;
	this.isReserved = isReserved;
}

function Modules_Feedback_Fields(stylePath, messageNewFiled, messageOneIncReq) {
	this.stylePath = stylePath;
	this.fields = new Array();
	this.fieldsAdd = new Array();
	this.reservedFields = new Array();
	this.listControl = document.getElementById('fieldsList').tBodies[0];
	this.lastStyle = 1;
	this.messageNewFiled = messageNewFiled;
	this.messageOneIncReq = messageOneIncReq;
}

Modules_Feedback_Fields.prototype.register = function(name, type, isIncluded, isRequired, isReserved) {
	if (isIncluded) {
		this.fields.push(new Modules_Feedback_Field(name, type, isIncluded!=0, isRequired!=0, isReserved!=0));
	} else {
		if (isReserved) {
			this.fieldsAdd.push(new Modules_Feedback_Field(name, type, isIncluded!=0, isRequired!=0, isReserved!=0));
		}
	}
	if (isReserved) {
		this.reservedFields.push(new Modules_Feedback_Field(name, type, isIncluded!=0, isRequired!=0, isReserved!=0));
	}
}

Modules_Feedback_Fields.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);
	}
	this.buildAddButton();
}

Modules_Feedback_Fields.prototype.buildItem = function(number, field, setFocus) {
	var trs = this.listControl.getElementsByTagName('TR');
	var tr = trs[0].cloneNode(true);
	tr.className = number%2 ? "sb-gridtext" : "sb-gridtext-alter";
	//var tr = document.getElementById('fieldList');
	var tds = tr.getElementsByTagName('TD');
	tds[0].innerHTML = number;
	//tds[1].innerHTML = this.getIncludeControlHtml(number, field);
	tds[1].innerHTML = this.getRequireControlHtml(number, field);
	var nameControl = tds[2].getElementsByTagName('INPUT')[0];
	field.nameControl = nameControl;
	nameControl.name = number;
	nameControl.value = field.name;
	nameControl.onchange = function() {return feedbackFields.setName(this.name, this.value);};
	var typeControl = tds[3].getElementsByTagName('SELECT')[0]
	typeControl.name = number;
	typeControl.selectedIndex = field.type-1;
	typeControl.onchange = function() {feedbackFields.setType(this.name, this.selectedIndex);};
	tds[4].innerHTML = this.getSeqDownImageControlHtml(number);
	tds[5].innerHTML = this.getSeqUpImageControlHtml(number);
	//
	var deleteControl = tds[6].getElementsByTagName('IMG')[0];
	deleteControl.onclick=function() {feedbackFields.deleteItem(number);};
	if (deleteControl.realSrc) {
		deleteControl.src = deleteControl.realSrc;
	}
	//selControl.name = number;
	tr.removeAttribute("style");
	this.listControl.appendChild(tr);
	if (setFocus) {
		if (document.activeElement) {
			document.activeElement.blur();
		}
		document.anchors[0].focus();
		nameControl.focus();
		nameControl.select();
	}
	this.lastStyle = this.lastStyle==1 ? 2 : 1;
	this.buildHiddens(number, field);
}

Modules_Feedback_Fields.prototype.buildAddButton = function() {
	//:INFO: adobrenko@: build row for add item
	//var trs = this.listControl.getElementsByTagName('TR');
	var tr_tmp = document.getElementById('addFieldButton');
	var tr = tr_tmp.cloneNode(true);
	var tds = tr.getElementsByTagName('TD');
	var selectControl = tds[2].getElementsByTagName('SELECT')[0];
	//var options = '';
	for (var i=0; i<this.fieldsAdd.length; i++) {
		var	option = document.createElement('OPTION');
		option.value=this.fieldsAdd[i].name;
		theText=document.createTextNode(this.fieldsAdd[i].name);
		option.appendChild(theText);
		//option.text=this.fieldsAdd[i].name;
		selectControl.appendChild(option);
	}
	tr.removeAttribute("style");
	var addControl = tds[4].getElementsByTagName('IMG')[0];
	if (addControl.realSrc) {
		addControl.src = addControl.realSrc;
	}
	this.listControl.appendChild(tr);
}

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

Modules_Feedback_Fields.prototype.buildHiddens = function(number, field) {
	var	hidden = document.createElement('INPUT');
	hidden.type = 'hidden';
	hidden.name = 'fields['+number+'][title]';
	hidden.value = field.name;
	this.listControl.appendChild(hidden);
	var	hidden = document.createElement('INPUT');
	hidden.type = 'hidden';
	hidden.name = 'fields['+number+'][type]';
	hidden.value = field.type;
	this.listControl.appendChild(hidden);
	var	hidden = document.createElement('INPUT');
	hidden.type = 'hidden';
	hidden.name = 'fields['+number+'][included]';
	hidden.value = field.isIncluded ? '1' : '0';
	this.listControl.appendChild(hidden);
	var	hidden = document.createElement('INPUT');
	hidden.type = 'hidden';
	hidden.name = 'fields['+number+'][required]';
	hidden.value = field.isRequired ? '1' : '0';
	this.listControl.appendChild(hidden);
}

Modules_Feedback_Fields.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_Feedback_Fields.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_Feedback_Fields.prototype.getIncludeControlHtml = function(number, field) {
	return this.getOnOffImageControlHtml(number, 'switchInclude', field.isIncluded);
}

Modules_Feedback_Fields.prototype.getRequireControlHtml = function(number, field) {
	return this.getOnOffImageControlHtml(number, 'switchRequire', field.isRequired);
}

Modules_Feedback_Fields.prototype.getOnOffImageControlHtml = function(number, method, onOff) {
	//return '<img src="'+this.stylePath+'/icons/'+(onOff ? 'run.png' : 'shutdown.png')+'" alt="" onclick="feedbackFields.'+method+'(\''+number+'\')" style="cursor:pointer;width:16px;height:16px;">';
	return '<input type="checkbox" name="" onclick="feedbackFields.'+method+'(\''+number+'\')"'+(onOff ? 'checked="checked"' : '')  +'>';
}

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

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

Modules_Feedback_Fields.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="feedbackFields.'+ method+'(\''+number+'\');"/>'
}

Modules_Feedback_Fields.prototype.switchInclude = function(fieldId) {
	if (this.fields[fieldId-1].isIncluded && !this.isIncludedMinimum(2, true)) {
		return;
	}
	this.fields[fieldId-1].isIncluded = !this.fields[fieldId-1].isIncluded;
	this.reBuildList();
}

Modules_Feedback_Fields.prototype.switchRequire = function(fieldId) {
	this.fields[fieldId-1].isRequired = !this.fields[fieldId-1].isRequired;
	this.reBuildList();
}

Modules_Feedback_Fields.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_Feedback_Fields.prototype.moveDown = function(fieldId) {
	var tmp = this.fields[fieldId];
	this.fields[fieldId] = this.fields[fieldId-1];
	this.fields[fieldId-1] = tmp;
	this.reBuildList();
}

Modules_Feedback_Fields.prototype.setName = function(fieldId, name) {
	var field = this.fields[fieldId-1];
	if (SB_Validator.isEmpty(name)) {
		this.processErrorForEmptyFiledName(field);
		return false;
	} else {
		if (this.isFieldReserved(field.name) && field.name != name) {
			this.fieldsAdd.push(new Modules_Feedback_Field(field.name, field.type, field.isIncluded, field.isRequired, field.isReserved));
			this.reBuildReserveFields();
		} else {
			if (this.isFieldReserved(name) && field.name != name) {
				this.deleteReserveField(name);
				this.reBuildReserveFields();
			}
		}
		field.name = name;
		this.reBuildHiddens();
		return true;
	}
}

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

Modules_Feedback_Fields.prototype.processErrorForEmptyFiledName = function(field) {
	if (document.activeElement) {
		document.activeElement.blur();
	}
	setTimeout(function() {
		tabPanelToggle('sb_panel', document.getElementById('2'));
		alert(SbGetLocaleByKey('FIELDNAME-IS-EMPTY'));
		field.nameControl.focus();
		field.nameControl.select();
	}, 200);
}

Modules_Feedback_Fields.prototype.setType = function(fieldId, type) {
	this.fields[fieldId-1].type = type+1;
	this.reBuildHiddens();
}

Modules_Feedback_Fields.prototype.append = function() {
		var trs = this.listControl.getElementsByTagName('TR');
		var lenLastTr = trs.length-1;
		var tr = trs[lenLastTr];
		var tds = tr.getElementsByTagName('TD');
		var checkbox = tds[1].getElementsByTagName('INPUT')[0];
		var selectNameControl = tds[2].getElementsByTagName('SELECT')[0];
		var selectTypeControl = tds[3].getElementsByTagName('SELECT')[0];
		if (selectNameControl.selectedIndex == -1) {
			//alert(SbGetLocaleByKey('FIELDNAME-IS-EMPTY'));
			//return false;
			var fieldText = SbGetLocaleByKey('NEW-FIELD');
		} else {
			var fieldText = selectNameControl.options[selectNameControl.selectedIndex].value;
		}
		var fieldType = selectTypeControl.options[selectTypeControl.selectedIndex].value;
		var required  = (checkbox.checked) ? 1 : 0;
		
		this.deleteReserveField(fieldText);
		this.register(fieldText, fieldType, 1, required, 0);
		this.reBuildList(true);
}

Modules_Feedback_Fields.prototype.deleteItem = function(number) {
	var message = SbGetLocaleByKey('DELETE-RECORD-CONFIRM');
	if (confirm(message)) {
		var backup = this.fields;
		var tmp = new Array();
		var deleteField; 
		number--;
		for (var i = 0; i < this.fields.length; i++) {
			if (number!=i) {
				tmp.push(this.fields[i]);
			} else {
				deleteField = this.fields[i];
			}
		}
		this.fields = tmp;
		if (this.isIncludedMinimum(1, true)) {
			if (this.isFieldReserved(deleteField.name)) {
				this.fieldsAdd.push(new Modules_Feedback_Field(deleteField.name, deleteField.type, deleteField.isIncluded, deleteField.isRequired, deleteField.isReserved));
			}	
			this.reBuildList();
		} else {
			this.fields = backup;
		}
	}
}

/*Modules_Feedback_Fields.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_Feedback_Fields.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) {
		var message = SbGetLocaleByKey('DELETE-RECORDS-CONFIRM');
		message = message.replace('%s', removedNumb);
		if (confirm(message)) {
			this.fields = tmp;
			if (this.isIncludedMinimum(1, true)) {
				this.reBuildList();
			} else {
				this.fields = backup;
			}
		}
	} else {
		alert(SbGetLocaleByKey('SELECT-RECORDS-FOR-DELETE'));
	}
}
*/

Modules_Feedback_Fields.prototype.isIncludedMinimum = function(minimum, message) {
	var included = 0;
	for (var i = 0; included<=minimum && i<this.fields.length; ++i) {
		if (this.fields[i].isIncluded) {
			included++;
		}
	}
	var isMinimum = included>=minimum;
	if (!isMinimum && message) {
		alert(this.messageOneIncReq);
	}
	return isMinimum;
}

Modules_Feedback_Fields.prototype.isFieldReserved = function(fieldName) {
	for (var i = 0; i<this.reservedFields.length; i++) {
		if (this.reservedFields[i].name == fieldName) {
			return true;
		}
	}
	return false;
}

Modules_Feedback_Fields.prototype.deleteReserveField = function(fieldName) {
	var tmp = new Array();
	for (var i = 0; i<this.fieldsAdd.length; i++) {
		if (this.fieldsAdd[i].name!=fieldName) {
			tmp.push(this.fieldsAdd[i]);
		}
	}
	this.fieldsAdd = tmp;
}

Modules_Feedback_Fields.prototype.reBuildReserveFields = function() {
	var trs = this.listControl.getElementsByTagName('TR');
	var lenLastTr = trs.length-1;
	this.listControl.removeChild(trs[lenLastTr]);
	this.buildAddButton();
}


