
Hedera.Conf = new Class({
	Extends: Hedera.Form

	,activate: function() {
		this.$('user-model').setInfo('c', 'myClient', 'hedera');

		if (this.hash.get('verificationToken'))
			this.onPassChangeClick();
	}
	
	,onPassChangeClick: function() {
		this.$('old-password').value = '';
		this.$('new-password').value = '';
		this.$('repeat-password').value = '';

		var verificationToken = this.hash.get('verificationToken');
		this.$('old-password').style.display = verificationToken ? 'none' : 'block';
		this.$('change-password').show();

		if (verificationToken)
			this.$('new-password').focus();
		else
			this.$('old-password').focus();
	}

	,onPassModifyClick: function() {
		try {
			var oldPassword = this.$('old-password').value;
			var newPassword = this.$('new-password').value;
			var repeatedPassword = this.$('repeat-password').value;

			if (newPassword == '' && repeatedPassword == '')
				throw new Error(_('Passwords empty'));
			if (newPassword !== repeatedPassword)
				throw new Error(_('Passwords doesn\'t match'));
		
			var verificationToken = this.hash.get('verificationToken');
			var params = {newPassword: newPassword};

			if (verificationToken) {
				params.verificationToken = verificationToken;
				this.conn.send('core/restore-password', params,
					this._onPassChange.bind(this));
			} else {
				let userId = this.gui.user.id;
				params.oldPassword = oldPassword;
				this.conn.lbSend('PATCH',
					`Accounts/${userId}/changePassword`, params,
					this._onPassChange.bind(this)
				);
			}
		} catch (e) {
			Htk.Toast.showError(e.message);
		}
	}

	,_onPassChange: function(json, error) {
		if (!error) {
			this.$('change-password').hide();
			this.hash.unset('verificationToken');
			Htk.Toast.showMessage(_('Password changed!'));
			this.$('user-form').refresh();
		} else {
			Htk.Toast.showError(error.message);

			if (this.hash.get('verificationToken'))
				this.$('new-password').select();
			else
				this.$('old-password').select();
		}
	}
	
	,onPassInfoClick: function() {
		this.$('password-info').show();
	}
	
	,onAddressesClick: function() {
		this.hash.set({form: 'account/address-list'});
	}
});

