/**
 * Custom payment system settings scripts
 *
 * @copyright (c) 2004-2007, SWsoft
 */

/**
 * Settings list class
 */
function CustomPaymentSystemSettings(id, containerId, headerRowId,
	templateRowId, addFieldRowId, addButtonContainerId, fetchNewMethod) {

	this.id = id;
	this.containerId = containerId;
	this.headerRowId = headerRowId;
	this.templateRowId = templateRowId;
	this.addFieldRowId = addFieldRowId;
	this.addButtonContainerId = addButtonContainerId;
	this.fields = new Array();
	this.control = document.getElementById(this.containerId).tBodies[0];
	this.templateRow = document.getElementById(this.templateRowId);
	this.headerRow = document.getElementById(this.headerRowId);
	this.addFieldRow = document.getElementById(this.addFieldRowId);
	this.addButtonContainer = document.getElementById(this.addButtonContainerId);
	this.isDisabled = false;
	this.isBuilded = false;
	this.fetchNewFieldMethod = fetchNewMethod;
}

CustomPaymentSystemSettings.prototype.register = function(object) {
	object.listObject = this;
	this.fields.push(object);
}

CustomPaymentSystemSettings.prototype.updateStatus = function(isDisabled) {
	if (!this.isBuilded) {
		var list = this;
		window.setTimeout(function () { list.updateStatus(isDisabled) }, 100);
	}
	this.isDisabled = isDisabled;
	for (var i = 0; i < this.fields.length; ++i) {
		this.fields[i].updateStatus(this.isDisabled);
	}
	if (this.isDisabled) {
		disableElement(this.headerRow);
		disableButton(this.addButtonContainer);
		disableElement(this.addFieldRow);
	} else {
		enableElement(this.headerRow);
		enableElement(this.addFieldRow);
		enableButton(this.addButtonContainer);
	}
}

CustomPaymentSystemSettings.prototype.buildSelf = function() {
	this.buildAddRow();
	var fieldsNumb = this.fields.length;
	for (var i = 0; i < this.fields.length; ++i) {
		this.buildItem(this.fields[i], i + 1, false);
	}
	this.isBuilded = true;
}

CustomPaymentSystemSettings.prototype.buildItem = function(field, position, setFocus) {
	var tr = this.templateRow.cloneNode(true);
	focusControl = field.buildSelf(tr, setFocus);
	field.updatePosition(position);

	this.control.insertBefore(tr, this.addFieldRow);
	tr.removeAttribute('style');
	if (setFocus) {
		focusControl.focus();
		focusControl.select();
	}
}

CustomPaymentSystemSettings.prototype.buildAddRow = function() {
	var tr = this.control.removeChild(this.addFieldRow);
	this.control.appendChild(tr);
	tr.removeAttribute('style');
}

CustomPaymentSystemSettings.prototype.moveUp = function(field) {
	var currentRow = field.containerControl;
	var previousRow = this.fields[field.position - 2].containerControl;
	var tempRow = this.control.removeChild(currentRow);
	this.control.insertBefore(tempRow, previousRow);

	var previousField = this.fields[field.position - 2];
	this.fields[field.position - 2] = field;
	this.fields[field.position - 1] = previousField;

	previousField.updatePosition(previousField.position + 1);
	field.updatePosition(field.position - 1);
}

CustomPaymentSystemSettings.prototype.moveDown = function(field) {
	var currentRow = field.containerControl;
	var nextRow = this.fields[field.position].containerControl;
	var tempRow = this.control.removeChild(nextRow);
	this.control.insertBefore(tempRow, currentRow);

	var nextField = this.fields[field.position];
	this.fields[field.position] = field;
	this.fields[field.position - 1] = nextField;

	nextField.updatePosition(nextField.position - 1);
	field.updatePosition(field.position + 1);
}

CustomPaymentSystemSettings.prototype.remove = function(field) {
	var currentRow = field.containerControl;
	var removedRow = this.control.removeChild(currentRow);
	for (var i = field.position - 1; i < this.fields.length - 1; i++) {
		this.fields[i] = this.fields[i + 1];
		this.fields[i].updatePosition(i + 1);
	}
	this.fields.pop();
	if (this.fields.length > 0) {
		this.fields[this.fields.length - 1].updatePosition(this.fields.length);
	}
}

CustomPaymentSystemSettings.prototype.appendNew = function() {
	var field = this.fetchNewFieldMethod();
	this.register(field);
	this.buildItem(field, this.fields.length, true);
	if (this.fields.length > 1) {
		this.fields[field.position - 2].updatePosition(field.position - 1);
	}
}

CustomPaymentSystemSettings.prototype.validate = function() {
	var result = true;
	if (!this.isDisabled) {
		for (var i = 0; i < this.fields.length; i++) {
			result = this.fields[i].validate();
			if (!result) {
				break;
			}
		}
	}
	return result;
}

/**
 * Base settings field class
 */
function CustomPaymentSystemSettingsObject() {
	this.id = null;
	this.position = null;
	this.isDisabled = false;
	this.listObject = null;
	this.containerControl = null;
	this.idControl = null;
	this.positionTextControl = null;
	this.positionControl = null;
	this.upControl = null;
	this.downControl = null;
	this.deleteControl = null;
}

CustomPaymentSystemSettingsObject.prototype.buildSelf = function(parentObject) {
	this.containerControl = parentObject;

	var childList = this.containerControl.getElementsByTagName('TD');
	var columnsNumber = childList.length;

	this.positionTextControl = childList[0].getElementsByTagName('SPAN')[0];
	this.positionControl = childList[0].getElementsByTagName('INPUT')[0];

	this.upControl = childList[columnsNumber - 3].getElementsByTagName('IMG')[0];
	this.upControl.originalOnclick = this.upControl.onclick;
	this.upControl.settingsObject = this;
	this.upControl.settingsList = this.listObject;

	this.downControl = childList[columnsNumber - 2].getElementsByTagName('IMG')[0];
	this.downControl.originalOnclick = this.downControl.onclick;
	this.downControl.settingsObject = this;
	this.downControl.settingsList = this.listObject;

	this.deleteControl = childList[columnsNumber - 1].getElementsByTagName('IMG')[0];
	this.deleteControl.originalOnclick = this.deleteControl.onclick;
	this.deleteControl.settingsObject = this;
	this.deleteControl.settingsList = this.listObject;

	this.idControl = childList[columnsNumber - 1].getElementsByTagName('INPUT')[0];
	this.idControl.value = this.id;
}

CustomPaymentSystemSettingsObject.prototype.getControlName = function(type) {
	return this.listObject.id + '[' + this.position + '][' + type + ']';
}

CustomPaymentSystemSettingsObject.prototype.updateStatus = function(isDisabled) {
	this.isDisabled = isDisabled;

	if (this.isDisabled) {
		disableButton(this.upControl.parentNode);
		disableButton(this.downControl.parentNode);
		disableButton(this.deleteControl.parentNode);
		disableElement(this.positionTextControl.parentNode);
	} else {
		enableButton(this.upControl.parentNode);
		if (this.upControl.isInaccessible) {
			this.upControl.parentNode.style.cursor = 'default';
		}
		enableButton(this.downControl.parentNode);
		if (this.downControl.isInaccessible) {
			this.downControl.parentNode.style.cursor = 'default';
		}
		enableButton(this.deleteControl.parentNode);
		enableElement(this.positionTextControl.parentNode);
	}
	this.upControl.onclick = (this.isDisabled) ? function(event) {} : this.upControl.originalOnclick;
	this.downControl.onclick = (this.isDisabled) ? function(event) {} : this.downControl.originalOnclick;
	this.deleteControl.onclick = (this.isDisabled) ? function(event) {} : this.deleteControl.originalOnclick;
}

CustomPaymentSystemSettingsObject.prototype.updatePosition = function(position) {
	this.position = position;

	this.positionTextControl.innerHTML = htmlSpecialChars(this.position);
	this.positionControl.name = this.getControlName('position');
	this.positionControl.value = this.position;
	this.idControl.name = this.getControlName('id');

	if (this.position < 2) {
		this.upControl.style.visibility = 'hidden';
		this.upControl.parentNode.style.cursor = 'default';
		this.upControl.isInaccessible = true;
	} else {
		this.upControl.style.visibility = 'visible';
		this.upControl.parentNode.style.cursor = 'pointer';
		this.upControl.isInaccessible = false;
	}
	if (this.position > this.listObject.fields.length - 1) {
		this.downControl.style.visibility = 'hidden';
		this.downControl.parentNode.style.cursor = 'default';
		this.downControl.isInaccessible = true;
	} else {
		this.downControl.style.visibility = 'visible';
		this.downControl.parentNode.style.cursor = 'pointer';
		this.downControl.isInaccessible = false;
	}
	if (this.upControl.realSrc) {
		this.upControl.src = this.upControl.realSrc;
	}
	if (this.downControl.realSrc) {
		this.downControl.src = this.downControl.realSrc;
	}
	if (this.deleteControl.realSrc) {
		this.deleteControl.src = this.deleteControl.realSrc;
	}

	if ((this.position % 2) == 0) {
		this.containerControl.className = 'sb-gridtext-alter';
	} else {
		this.containerControl.className = 'sb-gridtext';
	}
}

CustomPaymentSystemSettingsObject.prototype.validate = function() {
	var result = true;
	return result;
}

/**
 * Settings field class with internalName
 * extends CustomPaymentSystemSettingsObject
 */
function CustomPaymentSystemInternalObject(internalName) {
	CustomPaymentSystemSettingsObject.call(this);

	this.internalName = internalName;
	this.internalNameControl = null;
}

CustomPaymentSystemInternalObject.prototype = new CustomPaymentSystemSettingsObject();

CustomPaymentSystemInternalObject.prototype.updateStatus = function(isDisabled) {
	CustomPaymentSystemSettingsObject.prototype.updateStatus.call(this, isDisabled);

	this.internalNameControl.disabled = this.isDisabled;
}

CustomPaymentSystemInternalObject.prototype.updatePosition = function(position) {
	CustomPaymentSystemSettingsObject.prototype.updatePosition.call(this, position);

	this.internalNameControl.name = this.getControlName('internalName');
}

CustomPaymentSystemInternalObject.prototype.validate = function() {
	var result = CustomPaymentSystemSettingsObject.prototype.validate.call(this);
	result = result &&
		validateElement('SB_Validator.notEmpty', this.internalNameControl,
			'EMPTY-CUSTOM-PAYMENT-SYSTEM-INTERNAL-NAME');
	if (result) {
		result = SB_Validator.isRegex(this.internalNameControl.value, /^[_A-Za-z\d]+$/);

		if (!result) {
			alert(SbGetLocaleByKey('WRONG-CUSTOM-PAYMENT-SYSTEM-INTERNAL-NAME'));

			try {
				this.internalNameControl.focus();
			} catch (e) {

			}
		}
	}
	return result;
}


/**
 * Settings field for owner class
 * extends CustomPaymentSystemInternalObject
 */
function CustomPaymentSystemForOwnerObject(id, position, isRequired, internalName, caption, type) {
	CustomPaymentSystemInternalObject.call(this, internalName);

	this.id = id;
	this.position = position;

	this.isRequired = isRequired;
	this.caption = caption;
	this.type = type;

	this.isRequiredControl = null;
	this.captionControl = null;
	this.typeControl = null;
}

CustomPaymentSystemForOwnerObject.prototype = new CustomPaymentSystemInternalObject();

CustomPaymentSystemForOwnerObject.prototype.buildSelf = function(parentObject) {
	CustomPaymentSystemInternalObject.prototype.buildSelf.call(this, parentObject);

	var childList = this.containerControl.getElementsByTagName('TD');

	this.isRequiredControl = childList[1].getElementsByTagName('INPUT')[0];
	this.isRequiredControl.settingsObject = this;
	this.isRequiredControl.checked = (this.isRequired) ? true : false;

	this.internalNameControl = childList[2].getElementsByTagName('INPUT')[0];
	this.internalNameControl.value = this.internalName;

	this.captionControl = childList[3].getElementsByTagName('INPUT')[0];
	this.captionControl.value = this.caption;

	this.typeControl = childList[4].getElementsByTagName('SELECT')[0];
	this.typeControl.settingsObject = this;
	for(var i = 0; i < this.typeControl.options.length; i++) {
		if (this.typeControl.options[i].value == this.type) {
			this.typeControl.selectedIndex = i;
			break;
		}
	}

	return this.internalNameControl;
}

CustomPaymentSystemForOwnerObject.prototype.updateStatus = function(isDisabled) {
	CustomPaymentSystemInternalObject.prototype.updateStatus.call(this, isDisabled);

	this.isRequiredControl.disabled = this.isDisabled;
	this.captionControl.disabled = this.isDisabled;
	this.typeControl.disabled = this.isDisabled;
}

CustomPaymentSystemForOwnerObject.prototype.updatePosition = function(position) {
	CustomPaymentSystemInternalObject.prototype.updatePosition.call(this, position);

	this.isRequiredControl.name = this.getControlName('isRequired');
	this.captionControl.name = this.getControlName('caption');
	this.typeControl.name = this.getControlName('type');
}

CustomPaymentSystemForOwnerObject.prototype.validate = function() {
	var result = CustomPaymentSystemInternalObject.prototype.validate.call(this);
	result = result &&
		validateElement('SB_Validator.notEmpty', this.captionControl,
			'EMPTY-CUSTOM-PAYMENT-SYSTEM-FOR-OWNER-CAPTION');
	return result;
}

/**
 * Settings field for owner class
 * extends CustomPaymentSystemForOwnerObject
 */
function CustomPaymentSystemForCustomerObject(id, position, isRequired, internalName, caption, type) {
	CustomPaymentSystemForOwnerObject.call(this, id, position, isRequired, internalName, caption, type);
}

CustomPaymentSystemForCustomerObject.prototype = new CustomPaymentSystemForOwnerObject();

CustomPaymentSystemForCustomerObject.prototype.validate = function() {
	var result = CustomPaymentSystemInternalObject.prototype.validate.call(this);
	result = result &&
		validateElement('SB_Validator.notEmpty', this.captionControl,
			'EMPTY-CUSTOM-PAYMENT-SYSTEM-FOR-CUSTOMER-CAPTION');
	return result;
}


/**
 * Additional settings field class
 * extends CustomPaymentSystemInternalObject
 */
function CustomPaymentSystemAdditionalObject(id, position, internalName, source) {
	CustomPaymentSystemInternalObject.call(this, internalName);

	this.id = id;
	this.position = position;
	this.source = source;

	this.sourceControl = null;
}

CustomPaymentSystemAdditionalObject.prototype = new CustomPaymentSystemInternalObject();

CustomPaymentSystemAdditionalObject.prototype.buildSelf = function(parentObject) {
	CustomPaymentSystemInternalObject.prototype.buildSelf.call(this, parentObject);

	var childList = this.containerControl.getElementsByTagName('TD');

	this.internalNameControl = childList[1].getElementsByTagName('INPUT')[0];
	this.internalNameControl.value = this.internalName;

	this.sourceControl = childList[2].getElementsByTagName('SELECT')[0];
	this.sourceControl.settingsObject = this;
	for(var i = 0; i < this.sourceControl.options.length; i++) {
		if (this.sourceControl.options[i].value == this.source) {
			this.sourceControl.selectedIndex = i;
			break;
		}
	}

	return this.internalNameControl;
}

CustomPaymentSystemAdditionalObject.prototype.updateStatus = function(isDisabled) {
	CustomPaymentSystemInternalObject.prototype.updateStatus.call(this, isDisabled);

	this.sourceControl.disabled = this.isDisabled;
}

CustomPaymentSystemAdditionalObject.prototype.updatePosition = function(position) {
	CustomPaymentSystemInternalObject.prototype.updatePosition.call(this, position);

	this.sourceControl.name = this.getControlName('source');
}