/**
 * Block module editor control script
 *
 * @copyright (c) 2004-2006, SWsoft
 */
function SbBlockModuleEditor(moduleInstanceId, formId) {
	this._moduleInstanceId = moduleInstanceId;
	this._formId = formId;
	this._onLoadEventsList = new Array();
	this._onUnloadEventsList = new Array();
	this._onSubmitEventsList = new Array();
	this._onDiscardEventsList = new Array();
	this._dialog = top.sbBlockModuleEditorDialogObject;
	var editor = this;
	this._dialog.addOnCloseEvent(function () {editor._onUnloadEvent();});
	addEvent(self, 'load', function (event) {editor._onLoadEvent(event);});
	addEvent(self, 'unload', function (event) {editor._dialog.resetAll();});
	self.document.onkeypress = function (event) {editor._onKeyPressEvent(event);};
}

SbBlockModuleEditor.prototype.getFormObject = function() {
	return self.document.forms[this._formId];
}

SbBlockModuleEditor.prototype.setAction = function(action) {
	self.document.forms[this._formId].action = baseUrl + action;
}

SbBlockModuleEditor.prototype.submit = function() {
	var result = true;
	for (var i = 0; i < this._onSubmitEventsList.length; i++) {
		var method = this._onSubmitEventsList[i];
		var res = method();
		if (false == res) {
			result = false;
			break;
		}
	}
	if (result) {
		this._dialog.resetAll();
		self.document.forms[this._formId].submit();
	}
}

SbBlockModuleEditor.prototype.discard = function() {
	for (var i = 0; i < this._onDiscardEventsList.length; i++) {
		var method = this._onDiscardEventsList[i];
		method();
	}
	this.unload();
}

SbBlockModuleEditor.prototype.unload = function() {
	this._dialog.hide();
}

SbBlockModuleEditor.prototype.addOnLoadEvent = function(method) {
	this._onLoadEventsList.push(method);
}

SbBlockModuleEditor.prototype.addOnUnloadEvent = function(method) {
	this._onUnloadEventsList.push(method);
}

SbBlockModuleEditor.prototype.addOnSubmitEvent = function(method) {
	this._onSubmitEventsList.push(method);
}

SbBlockModuleEditor.prototype.addOnDiscardEvent = function(method) {
	this._onDiscardEventsList.push(method);
}

SbBlockModuleEditor.prototype._onUnloadEvent = function() {
	var result = true;
	for (var i = 0; i < this._onUnloadEventsList.length; i++) {
		var method = this._onUnloadEventsList[i];
		var res = method();
		if (false == res) {
			result = false;
			break;
		}
	}
	return result;
}

SbBlockModuleEditor.prototype._onLoadEvent = function(event) {
	for (var i = 0; i < this._onLoadEventsList.length; i++) {
		var method = this._onLoadEventsList[i];
		method(event);
	}
}

SbBlockModuleEditor.prototype._onKeyPressEvent = function(event) {
	if (document.all) event = self.event;
	try {
		if (document.all) {
			if ('TEXTAREA' == event.srcElement.tagName) {
				return true;
			}
		} else {
			if ('object' == typeof(event.target)) {
				if ('TEXTAREA' == event.target.tagName) {
					return true;
				}
			}
		}
	} catch (e) {}
	if (event.keyCode == 13) {
		stopEvent(event);
		this.submit();
		return false;
	}
	return true;
}

SbBlockModuleEditor.prototype.addHidden = function(name, value) {
	var input = self.document.createElement("input");
	input.setAttribute('type', 'hidden');
	input.setAttribute('name', name);
	input.value = value;
	self.document.forms[this._formId].appendChild(input);
}