/**
 * Site publication settings scripts
 *
 * @copyright (c) 2004-2007, SWsoft
 */
function SbPublicationSettingsForm(navigationManager) {
	this._navigationManager = navigationManager;
	var settingsForm = this;
	this._navigationManager.registerOnSubmitFunction(function() {return settingsForm.onSubmit();});

	this._modeControl = self.document.getElementById('publicationMode');
	this._siteUrlControl = self.document.getElementById('siteUrl');
	this._revertSettingsButton = self.document.getElementById('revertSettings');
	this._onSubmitEvents = new Array();
	this._onChangeEvents = new Array();
	this._onRevertButtonShowEvents = new Array();
	this._onRevertButtonHideEvents = new Array();
	this._onRevertSettingsEvents = new Array();
	this._ftpForm = new SbFtpSettingsForm();
	this._vpsForm = new SbVpsSettingsForm();
	this._xcopyForm = new SbXcopySettingsForm();
	
	this._revertMode = 'js';
	var button = self.document.getElementById('revertSettings');
	if (button && button.revertMode) {
		this._revertMode = button.revertMode;
	}
	this.onPublicationModeChange();
}

SbPublicationSettingsForm.prototype.isEmptyUrl = function() {
	return SB_Validator.isEmpty(this.getSiteUrl());
}

SbPublicationSettingsForm.prototype.isValidSiteUrl = function() {
	return SB_Validator.isHttpUrl(this.getSiteUrl());
}

SbPublicationSettingsForm.prototype.getSiteUrl = function() {
	return this._siteUrlControl.value;
}

SbPublicationSettingsForm.prototype.isEmptyDsn = function() {
	return this._getCurrentForm().isEmptyDsn();
}

SbPublicationSettingsForm.prototype.isValidDsn = function() {
	return this._getCurrentForm().isValidDsn();
}

SbPublicationSettingsForm.prototype.getHumanDsn = function() {
	return this._getCurrentForm().getHumanDsn();
}

SbPublicationSettingsForm.prototype.addOnSubmitEvent = function(event) {
	if ('function' == typeof(event)) {
		this._onSubmitEvents[this._onSubmitEvents.length] = event;
	}
}

SbPublicationSettingsForm.prototype.onSubmit = function() {
	var result = true;
	for(var i = 0; i < this._onSubmitEvents.length; i++) {
		var event = this._onSubmitEvents[i];
		if (false == event()) {
			result = false;
			break;
		}
	}
	return result;
}

SbPublicationSettingsForm.prototype.addOnChangeEvent = function(event) {
	if ('function' == typeof(event)) {
		this._onChangeEvents[this._onChangeEvents.length] = event;
	}
}

SbPublicationSettingsForm.prototype.onChange = function() {
	for(var i = 0; i < this._onChangeEvents.length; i++) {
		var event = this._onChangeEvents[i];
		event();
	}
	if (this._revertSettingsButton) {
		enableButton(this._revertSettingsButton)
		this._revertSettingsButton.textContainer.className = this._revertSettingsButton.enabledTextClass;
		this._revertSettingsButton.textContainer.innerHTML = htmlSpecialChars(this._revertSettingsButton.enabledText);
		this._onRevertButtonShow();
	}
	sbApplyChangesObject.registerChange();
}

SbPublicationSettingsForm.prototype.addOnRevertButtonShowEvent = function(event) {
	if ('function' == typeof(event)) {
		this._onRevertButtonShowEvents[this._onRevertButtonShowEvents.length] = event;
	}
}

SbPublicationSettingsForm.prototype._onRevertButtonShow = function() {
	for(var i = 0; i < this._onRevertButtonShowEvents.length; i++) {
		var event = this._onRevertButtonShowEvents[i];
		event();
	}
}

SbPublicationSettingsForm.prototype.addOnRevertButtonHideEvent = function(event) {
	if ('function' == typeof(event)) {
		this._onRevertButtonHideEvents[this._onRevertButtonHideEvents.length] = event;
	}
}

SbPublicationSettingsForm.prototype._onRevertButtonHide = function() {
	for(var i = 0; i < this._onRevertButtonHideEvents.length; i++) {
		var event = this._onRevertButtonHideEvents[i];
		event();
	}
}

SbPublicationSettingsForm.prototype.addOnRevertSettingsEvent = function(event) {
	if ('function' == typeof(event)) {
		this._onRevertSettingsEvents[this._onRevertSettingsEvents.length] = event;
	}
}

SbPublicationSettingsForm.prototype.setRevertMode = function(mode) {
	this._revertMode = mode;
}

SbPublicationSettingsForm.prototype.revertSettings = function() {
	var result = true;
	if (this._revertSettingsButton.disabled) {
		result = false;
	} else {
		for(var i = 0; i < this._onRevertSettingsEvents.length; i++) {
			var event = this._onRevertSettingsEvents[i];
			if (false == event()) {
				result = false;
				break;
			}
		}
		if (result) {
			if ('js' == this._revertMode) {
				this._navigationManager.getForm().reset();
				this._ftpForm.onReset();
				this._vpsForm.onReset();
				this._xcopyForm.onReset();
				this.onPublicationModeChange();
				this.onChange();
				disableButton(this._revertSettingsButton)
				this._revertSettingsButton.textContainer.className = this._revertSettingsButton.disabledTextClass;
				this._revertSettingsButton.textContainer.innerHTML = htmlSpecialChars(this._revertSettingsButton.disabledText);
				this._onRevertButtonHide();
				sbApplyChangesObject.uregisterChange();
			} else {
				sbApplyChangesObject.registerChange();
				sbApplyChangesObject.registerForcedSaving();
				this._navigationManager.go();
			}
		}
	}
}

SbPublicationSettingsForm.prototype.onPublicationModeChange = function() {
	switch (this._modeControl.value) {
		case 'FTP':
			this._xcopyForm.hide();
			this._vpsForm.hide();
			this._ftpForm.show();
			break;
	
		case 'VPS':
			this._xcopyForm.hide();
			this._ftpForm.hide();
			this._vpsForm.show();
			break;

		case 'XCopy':
			this._vpsForm.hide();
			this._ftpForm.hide();
			this._xcopyForm.show();
			break;
	}
}

SbPublicationSettingsForm.prototype.validate = function() {
	return this._getCurrentForm().validate();
}

SbPublicationSettingsForm.prototype.validateIfNotEmpty = function() {
	return this._getCurrentForm().validateIfNotEmpty();
}

SbPublicationSettingsForm.prototype._getCurrentForm = function() {
	var result = null;
	
	switch (this._modeControl.value) {
		case 'FTP':
			result = this._ftpForm;
			break;
	
		case 'VPS':
			result = this._vpsForm;
			break;

		case 'XCopy':
			result = this._xcopyForm;
			break;
	}
	return result;
}

SbPublicationSettingsForm.prototype.onChangeFtpPassword = function() {
	this._ftpForm.onChangePassword();
	this.onChange();
}


/**
 * Base settings form class
 */
function SbBaseSettingsForm() {
	this._formItems = new Array();
	this._workDirControl = self.document.getElementById('workDir');
}

SbBaseSettingsForm.prototype.isEmptyDsn = function() {
	return SB_Validator.isEmpty(this._getWorkDir());
}

SbBaseSettingsForm.prototype.isValidDsn = function() {
	return true;
}

SbBaseSettingsForm.prototype.getHumanDsn = function() {
	return this._getWorkDir();
}

SbBaseSettingsForm.prototype.hide = function() {
	this._hideItems(this._formItems);
}

SbBaseSettingsForm.prototype.show = function() {
	enableAsterix('workDir');
	this._showItems(this._formItems);
}

SbBaseSettingsForm.prototype._hideItems = function(items) {
	for(var i = 0; i < items.length; i++) {
		var inputRow = self.document.getElementById(items[i] + 'Row');
		if (inputRow) {
			inputRow.style.display = 'none';
		}
	}
}

SbBaseSettingsForm.prototype._showItems = function(items) {
	for(var i = 0; i < items.length; i++) {
		var inputRow = self.document.getElementById(items[i] + 'Row');
		if (inputRow) {
			inputRow.style.display = (document.all) ? 'block' : 'table-row';
		}
	}
}

SbBaseSettingsForm.prototype._getWorkDir = function() {
	return this._workDirControl.value;
}

SbBaseSettingsForm.prototype._addFirstSlash = function(str) {
	var result = '/';
	if (str.length > 0) {
		if ('/' != str.charAt(0)) {
			result = '/' + str;
		}
	}
	return result;
}

SbBaseSettingsForm.prototype.validate = function() {
	return true;
}

SbBaseSettingsForm.prototype.validateIfNotEmpty = function() {
	return true;
}

SbBaseSettingsForm.prototype.onReset = function() {
}

/**
 * Host settings form class
 * extends SbBaseSettingsForm
 */
function SbBaseHostSettingsForm() {
	SbBaseSettingsForm.call(this);
	this._formItems = this._formItems.concat(new Array('hostName'));
	this._hostControl = self.document.getElementById('hostName');
}

SbBaseHostSettingsForm.prototype = new SbBaseSettingsForm();

SbBaseHostSettingsForm.prototype.isEmptyDsn = function() {
	var result = SbBaseSettingsForm.prototype.isEmptyDsn.call(this);
	if (result) {
		result = SB_Validator.isEmpty(this._getHost());
	}
	return result;
}

SbBaseHostSettingsForm.prototype.isValidDsn = function() {
	var result = SbBaseSettingsForm.prototype.isValidDsn.call(this);
	if (result) {
		result = SB_Validator.isHostname(this._getHost());
	}
	return result;
}

SbBaseHostSettingsForm.prototype._getHost = function() {
	return this._hostControl.value;
}

SbBaseHostSettingsForm.prototype.validate = function() {
	var result = SbBaseSettingsForm.prototype.validate.call(this);
	if (result &&
		validateField('SB_Validator.notEmpty', 'hostName', 'EMPTY-HOST') &&
		validateField('SB_Validator.isHostname', 'hostName', 'INVALID-HOST')) {
		result = true;
	} else {
		result = false;
	}
	return result;
}

SbBaseHostSettingsForm.prototype.validateIfNotEmpty = function() {
	var result = SbBaseSettingsForm.prototype.validateIfNotEmpty.call(this);
	if (result &&
		validateFieldIfNotEmpty('SB_Validator.isHostname', 'hostName', 'INVALID-HOST')) {
		result = true;
	} else {
		result = false;
	}
	return result;
}

/**
 * FTP settings form class
 * extends SbBaseHostSettingsForm
 */
function SbFtpSettingsForm() {
	SbBaseHostSettingsForm.call(this);
	this._formItems = this._formItems.concat(new Array('ftpPort', 'ftpUser'));
	this._portControl = self.document.getElementById('ftpPort');
	this._userControl = self.document.getElementById('ftpUser');
	this._passwordControl = self.document.getElementById('ftpPassword');
	this._isFtpPasswordChangedControl = self.document.getElementById('isFtpPasswordChanged');
	this._changeFtpPasswordControl = self.document.getElementById('changeFtpPassword');
	this._initialIsFtpPasswordChangedValue = (this._isFtpPasswordChangedControl) ? this._isFtpPasswordChangedControl.value : 'false';
}

SbFtpSettingsForm.prototype = new SbBaseHostSettingsForm();

SbFtpSettingsForm.prototype.isEmptyDsn = function() {
	var result = SbBaseHostSettingsForm.prototype.isEmptyDsn.call(this);
	if (result) {
		result = SB_Validator.isEmpty(this._getUser());
	}
	return result;
}

SbFtpSettingsForm.prototype.isValidDsn = function() {
	var result = SbBaseHostSettingsForm.prototype.isValidDsn.call(this);
	if (result) {
		result = (SB_Validator.isDigits(this._getPort()) &&
			SB_Validator.notEmpty(this._getUser()) &&
			(SB_Validator.isEmpty(this._getWorkDir()) || SB_Validator.isPath(this._getWorkDir()))
			);
	}
	return result;
}

SbFtpSettingsForm.prototype.validate = function() {
	var result = SbBaseHostSettingsForm.prototype.validate.call(this);
	if (result &&
		validateField('SB_Validator.notEmpty', 'ftpPort', 'EMPTY-FTP-PORT') &&
		validateField('SB_Validator.isDigits', 'ftpPort', 'INVALID-FTP-PORT') &&
		validateField('SB_Validator.notEmpty', 'ftpUser', 'INVALID-FTP-USER') &&
		(
			SB_Validator.isEmpty(this._getWorkDir()) ||
			validateField('SB_Validator.isPath', 'workDir', 'INVALID-WORKDIR')
		) &&
		validateField('SB_Validator.notEmpty', 'siteUrl', 'EMPTY-URL') &&
		validateField('SB_Validator.isHttpUrl', 'siteUrl', 'INVALID-URL')) {
		result = true;
	} else {
		result = false;
	}
	return result;
}

SbFtpSettingsForm.prototype.validateIfNotEmpty = function() {
	var result = SbBaseHostSettingsForm.prototype.validateIfNotEmpty.call(this);
	if (result &&
		validateFieldIfNotEmpty('SB_Validator.isDigits', 'ftpPort', 'INVALID-FTP-PORT') &&
		validateFieldIfNotEmpty('SB_Validator.isPath', 'workDir', 'INVALID-WORKDIR') &&
		validateFieldIfNotEmpty('SB_Validator.isHttpUrl', 'siteUrl', 'INVALID-URL')) {
		result = true;
	} else {
		result = false;
	}
	return result;
}

SbFtpSettingsForm.prototype.getHumanDsn = function() {
	var result = 'ftp://';
	result += this._getUser();
	result += '@';
	result += this._getHost();
	var port = this._getPort() + '';
	if ('21' != port) {
		result += ':';
		result += port;
	}
	result += this._addFirstSlash(this._getWorkDir());
	return result;
}

SbFtpSettingsForm.prototype.onChangePassword = function() {
	this._hideItems(new Array('changeFtpPassword'));
	this._showItems(new Array('ftpPassword'));
	this._isFtpPasswordChangedControl.value = 'true';
}

SbFtpSettingsForm.prototype.onReset = function() {
	SbBaseSettingsForm.prototype.onReset.call(this);
	if ('false' == this._initialIsFtpPasswordChangedValue) {
		this._showItems(new Array('changeFtpPassword'));
		this._hideItems(new Array('ftpPassword'));
		if (this._isFtpPasswordChangedControl) {
			this._isFtpPasswordChangedControl.value = 'false';
		}
	}
}

SbFtpSettingsForm.prototype.hide = function() {
	SbBaseHostSettingsForm.prototype.hide.call(this);
	this._hideItems(new Array('changeFtpPassword', 'ftpPassword'));
}

SbFtpSettingsForm.prototype.show = function() {
	SbBaseHostSettingsForm.prototype.show.call(this);
	disableAsterix('workDir');
	var items;
	if ('true' == this._isFtpPasswordChangedControl.value) {
		items = new Array('ftpPassword');
	} else {
		items = new Array('changeFtpPassword');
	}
	this._showItems(items);
}

SbFtpSettingsForm.prototype._getPort = function() {
	return this._portControl.value;
}

SbFtpSettingsForm.prototype._getUser = function() {
	return this._userControl.value;
}

SbFtpSettingsForm.prototype._getPassword = function() {
	return this._passwordControl.value;
}


/**
 * VPS settings form class
 * extends SbBaseHostSettingsForm
 */
function SbVpsSettingsForm() {
	SbBaseHostSettingsForm.call(this);
	this._formItems = this._formItems.concat(new Array('vpsId', 'vpsUser', 'vpsGroup'));
	this._idControl = self.document.getElementById('vpsId');
	this._userControl = self.document.getElementById('vpsUser');
	this._groupControl = self.document.getElementById('vpsGroup');
}

SbVpsSettingsForm.prototype = new SbBaseHostSettingsForm();

SbVpsSettingsForm.prototype.isEmptyDsn = function() {
	var result = SbBaseHostSettingsForm.prototype.isEmptyDsn.call(this);
	if (result) {
		result = (SB_Validator.isEmpty(this._getId()) &&
			SB_Validator.isEmpty(this._getUser()) &&
			SB_Validator.isEmpty(this._getGroup())
			);
	}
	return result;
}

SbVpsSettingsForm.prototype.isValidDsn = function() {
	var result = SbBaseHostSettingsForm.prototype.isValidDsn.call(this);
	if (result) {
		result = (SB_Validator.isDigits(this._getId()) &&
			SB_Validator.notEmpty(this._getUser()) &&
			SB_Validator.notEmpty(this._getGroup()) &&
			SB_Validator.isPath(this._getWorkDir())
			);
	}
	return result;
}

SbVpsSettingsForm.prototype.validate = function() {
	var result = SbBaseHostSettingsForm.prototype.validate.call(this);
	if (result &&
		validateField('SB_Validator.notEmpty', 'vpsId', 'EMPTY-VPS-ID') &&
		validateField('SB_Validator.isDigits', 'vpsId', 'INVALID-VPS-ID') &&
		validateField('SB_Validator.notEmpty', 'vpsUser', 'INVALID-VPS-USER') &&
		validateField('SB_Validator.notEmpty', 'vpsGroup', 'INVALID-VPS-GROUP') &&
		validateField('SB_Validator.notEmpty', 'workDir', 'EMPTY-WORKDIR') &&
		validateField('SB_Validator.isPath', 'workDir', 'INVALID-WORKDIR') &&
		validateField('SB_Validator.notEmpty', 'siteUrl', 'EMPTY-URL') &&
		validateField('SB_Validator.isHttpUrl', 'siteUrl', 'INVALID-URL')) {
		result = true;
	} else {
		result = false;
	}
	return result;
}

SbVpsSettingsForm.prototype.validateIfNotEmpty = function() {
	var result = SbBaseHostSettingsForm.prototype.validateIfNotEmpty.call(this);
	if (result &&
		validateFieldIfNotEmpty('SB_Validator.isDigits', 'vpsId', 'INVALID-VPS-ID') &&
		validateFieldIfNotEmpty('SB_Validator.isPath', 'workDir', 'INVALID-WORKDIR') &&
		validateFieldIfNotEmpty('SB_Validator.isHttpUrl', 'siteUrl', 'INVALID-URL')) {
		result = true;
	} else {
		result = false;
	}
	return result;
}

SbVpsSettingsForm.prototype.getHumanDsn = function() {
	var result = 'vps://';
	result += this._getUser();
	result += ':';
	result += this._getGroup();
	result += '@';
	result += this._getHost();
	result += ':';
	result += this._getId();
	result += this._addFirstSlash(this._getWorkDir());
	return result;
}

SbVpsSettingsForm.prototype._getId = function() {
	return this._idControl.value;
}

SbVpsSettingsForm.prototype._getUser = function() {
	return this._userControl.value;
}

SbVpsSettingsForm.prototype._getGroup = function() {
	return this._groupControl.value;
}

/**
 * XCOPY settings form class
 * extends SbBaseSettingsForm
 */
function SbXcopySettingsForm() {
	SbBaseSettingsForm.call(this);
}

SbXcopySettingsForm.prototype = new SbBaseSettingsForm();

SbXcopySettingsForm.prototype.isValidDsn = function() {
	var result = SbBaseSettingsForm.prototype.isValidDsn.call(this);
	if (result) {
		result = SB_Validator.isPath(this._getWorkDir());
	}
	return result;
}

SbXcopySettingsForm.prototype.validate = function() {
	var result = SbBaseSettingsForm.prototype.validate.call(this);
	if (result &&
		validateField('SB_Validator.notEmpty', 'workDir', 'EMPTY-WORKDIR') &&
		validateField('SB_Validator.isPath', 'workDir', 'INVALID-WORKDIR') &&
		validateField('SB_Validator.notEmpty', 'siteUrl', 'EMPTY-URL') &&
		validateField('SB_Validator.isHttpUrl', 'siteUrl', 'INVALID-URL')) {
		result = true;
	} else {
		result = false;
	}
	return result;
}

SbXcopySettingsForm.prototype.validateIfNotEmpty = function() {
	var result = SbBaseSettingsForm.prototype.validateIfNotEmpty.call(this);
	if (result &&
		validateFieldIfNotEmpty('SB_Validator.isPath', 'workDir', 'INVALID-WORKDIR') &&
		validateFieldIfNotEmpty('SB_Validator.isHttpUrl', 'siteUrl', 'INVALID-URL')) {
		result = true;
	} else {
		result = false;
	}
	return result;
}