/**
 * Custom payment system settings scripts
 *
 * @copyright (c) 2004-2007, SWsoft
 */
function CustomSettingsOwnerList(controlId) {
	this.controlId = controlId;
	this.control = document.getElementById(this.controlId);
	this.fields = new Array();
	this.isDisabled = !this.control.checked;
}

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

CustomSettingsOwnerList.prototype.updateStatus = function(isDisabled) {
	this.isDisabled = isDisabled;
	for (var i = 0; i < this.fields.length; i++) {
		this.fields[i].updateStatus(isDisabled);
	}
}

CustomSettingsOwnerList.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 object class
 */
function CustomSettingsObject(controlId, required) {
	this.controlId = controlId;
	this.control = document.getElementById(this.controlId);
	this.isRequired = ('on' == required) ? true : false;
}

CustomSettingsObject.prototype.validate = function() {
	var result = (!this.isRequired) || validateField('SB_Validator.notEmpty', this.controlId, 'CUSTOM-PAYMENT-FIELD-EMPTY');
	return result;
}

CustomSettingsObject.prototype.updateStatus = function(isDisabled) {
	this.control.disabled = isDisabled;
}

/**
 * String settings object
 * extends CustomSettingsObject
 */
function CustomSettingsObjectText(controlId, required) {
	CustomSettingsObject.call(this, controlId, required);
}

CustomSettingsObjectText.prototype = new CustomSettingsObject();


/**
 * e-mail settings object
 * extends CustomSettingsObject
 */
function CustomSettingsObjectMail(controlId, required) {
	CustomSettingsObject.call(this, controlId, required);
}

CustomSettingsObjectMail.prototype = new CustomSettingsObject();

CustomSettingsObjectMail.prototype.validate = function() {
	var result = CustomSettingsObject.prototype.validate.call(this);
	result = result &&
		(
			(!this.isRequired) ||
			validateField('SB_Validator.isEmail', this.controlId, 'CUSTOM-PAYMENT-WRONG-MAIL')
		);
	return result;
}

/**
 * Double settings object
 * extends CustomSettingsObject
 */
function CustomSettingsObjectDouble(controlId, required, decimalPoint) {
	CustomSettingsObject.call(this, controlId, required);
	this.decimalPoint = decimalPoint;
}

CustomSettingsObjectDouble.prototype = new CustomSettingsObject();

CustomSettingsObjectDouble.prototype.validate = function() {
	var result = CustomSettingsObject.prototype.validate.call(this);
	var validatorResult = SB_Validator.isFloat(this.control.value, this.decimalPoint);
	result = result &&
		(
			(!this.isRequired) ||
			extendedValidateField(validatorResult, this.controlId, 'CUSTOM-PAYMENT-WRONG-DOUBLE')
		);
	return result;
}

/**
 * Currency settings object
 * extends CustomSettingsObject
 */
function CustomSettingsObjectCurrency(controlId, required, decimalPoint) {
	CustomSettingsObject.call(this, controlId, required);
	this.decimalPoint = decimalPoint;
}

CustomSettingsObjectCurrency.prototype = new CustomSettingsObject();

CustomSettingsObjectCurrency.prototype.validate = function() {
	var result = CustomSettingsObject.prototype.validate.call(this);
	var validatorResult = SB_Validator.isFloat(this.control.value, this.decimalPoint);
	result = result &&
		(
			(!this.isRequired) ||
			extendedValidateField(validatorResult, this.controlId, 'CUSTOM-PAYMENT-WRONG-CURRENCY')
		);
	return result;
}

/**
 * Integer settings object
 * extends CustomSettingsObject
 */
function CustomSettingsObjectInteger(controlId, required) {
	CustomSettingsObject.call(this, controlId, required);
}

CustomSettingsObjectInteger.prototype = new CustomSettingsObject();

CustomSettingsObjectInteger.prototype.validate = function() {
	var result = CustomSettingsObject.prototype.validate.call(this);
	result = result &&
		(
			(!this.isRequired) ||
			validateField('SB_Validator.isInt', this.controlId, 'CUSTOM-PAYMENT-WRONG-INTEGER')
		);
	return result;
}


/**
 * Integer settings object
 * extends CustomSettingsObject
 */
function CustomSettingsObjectNumeric(controlId, required) {
	CustomSettingsObject.call(this, controlId, required);
}

CustomSettingsObjectNumeric.prototype = new CustomSettingsObject();

CustomSettingsObjectNumeric.prototype.validate = function() {
	var result = CustomSettingsObject.prototype.validate.call(this);
	result = result &&
		(
			(!this.isRequired) ||
			validateField('SB_Validator.isDigits', this.controlId, 'CUSTOM-PAYMENT-WRONG-NUMERIC')
		);
	return result;
}


/**
 * Phone settings object
 * extends CustomSettingsObject
 */
function CustomSettingsObjectPhone(controlId, required) {
	CustomSettingsObject.call(this, controlId, required);
}

CustomSettingsObjectPhone.prototype = new CustomSettingsObject();

CustomSettingsObjectPhone.prototype.validate = function() {
	var result = CustomSettingsObject.prototype.validate.call(this);
	result = result &&
		(
			(!this.isRequired) ||
			validateField('SB_Validator.isPhone', this.controlId, 'CUSTOM-PAYMENT-WRONG-PHONE')
		);
	return result;
}

/**
 * Phone settings object
 * extends CustomSettingsObject
 */
function CustomSettingsObjectDate(controlId, required) {
	CustomSettingsObject.call(this, controlId, required);
	this.yearControl = document.getElementById(this.controlId + 'year');
	this.monthControl = document.getElementById(this.controlId + 'month');
	this.dayControl = document.getElementById(this.controlId + 'day');
}

CustomSettingsObjectDate.prototype = new CustomSettingsObject();

CustomSettingsObjectDate.prototype.updateStatus = function(isDisabled) {
	this.yearControl.disabled = isDisabled;
	this.monthControl.disabled = isDisabled;
	this.dayControl.disabled = isDisabled;
}

CustomSettingsObjectDate.prototype.validate = function() {
	var isMonthNotEmpty = SB_Validator.notEmpty(this.monthControl.value);
	var isDayNotEmpty = SB_Validator.notEmpty(this.dayControl.value);
	var isYearNotEmpty = SB_Validator.notEmpty(this.yearControl.value);
	var result = (!this.isRequired) ||
		(
			extendedValidateField(isMonthNotEmpty, this.monthControl.id, 'CUSTOM-PAYMENT-EMPTY-MONTH') &&
			extendedValidateField(isDayNotEmpty, this.dayControl.id, 'CUSTOM-PAYMENT-EMPTY-DAY') &&
			extendedValidateField(isYearNotEmpty, this.yearControl.id, 'CUSTOM-PAYMENT-EMPTY-YEAR')
		);
	if (isMonthNotEmpty || isDayNotEmpty || isYearNotEmpty) {
		result = result &&
			validateField('SB_Validator.isDigits', this.monthControl.id, 'CUSTOM-PAYMENT-WRONG-MONTH') &&
			validateField('SB_Validator.isDigits', this.dayControl.id, 'CUSTOM-PAYMENT-WRONG-DAY') &&
			validateField('SB_Validator.isDigits', this.yearControl.id, 'CUSTOM-PAYMENT-WRONG-YEAR');
		if (result) {
			var monthResult = SB_Validator.isBetween(this.monthControl.value, 1, 12);
			var dayResult = SB_Validator.isBetween(this.dayControl.value, 1, 31);
			var yearResult = SB_Validator.isBetween(this.yearControl.value, 1970, 10000);
			result =
				extendedValidateField(monthResult, this.monthControl.id, 'CUSTOM-PAYMENT-WRONG-MONTH') &&
				extendedValidateField(dayResult, this.dayControl.id, 'CUSTOM-PAYMENT-WRONG-DAY') &&
				extendedValidateField(yearResult, this.yearControl.id, 'CUSTOM-PAYMENT-WRONG-YEAR');
		}
	}
	return result;
}