/**
 * ExtJS Wizard Component
 * Copyright (C) 2008 Hans-Peter Oeri <hp@oeri.ch>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * ----------------------------------------------------------------------
 *
 * @author Hans-Peter Oeri <hp@oeri.ch>
 * @url https://saintcyr.oeri.ch/trac/ext-ux-wizard
 *
 * Prior work by
 * @author Thorsten Suckow-Homberg <ts@siteartwork.de>
 * @url http://www.siteartwork.de/wizardcomponent
 */

/**
 * Override Ext.FormPanel.bindHandler The ext2.2 version contrary to
 * documentation assumes, that all items are descendants of Ext.Field. This
 * variant checks for the existence of isValid() before calling...
 */

Ext.override(Ext.FormPanel, {

	bindHandler: function() {
		if (!this.bound) {
				return false;
		}
		var valid = true;
		this.form.items.each( function(f) {
			if (f.isValid && !f.isValid(true)) { // patch core
				valid = false;
				return false;
			}
		} );
		if (this.buttons) {
			for (var i = 0, len = this.buttons.length; i < len; i++) {
				var btn = this.buttons[i];
				if (btn.formBind === true && btn.disabled === valid) {
					btn.setDisabled(!valid);
				}
			}
		}
		this.fireEvent('clientvalidation', this, valid);
	}

} );

/**
 * Same for BasicForm
 */
Ext.override(Ext.form.BasicForm, {
	isValid : function() {
		var valid = true;
		this.items.each( function(f) {
			if (f.validate && !f.validate()) {
				valid = false;
			}
		});
		return valid;
	},

	isDirty : function() {
		var dirty = false;
		this.items.each( function(f) {
			if (f.isDirty && f.isDirty()) {
				dirty = true;
				return false;
			}
		});
		return dirty;
	},

	clearInvalid : function() {
		this.items.each( function(f) {
			if (f.clearInvalid) {
				f.clearInvalid();
			}
		});
		return this;
	},

	reset : function() {
		this.items.each( function(f) {
			if (f.reset) {
				f.reset();
			}
		});
		return this;
	}

});



