// FormCondition.class.js (Fertile Form JS Framework) || Version: 0.01 || Last Updated: 2010-08-26 17:00 || Updated by: Hidde-Finne Peters || Created: 2010-08-23 by Hidde-Finne Peters
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//	Base class for all form conditions

var FormCondition = Class.extend({

	init: function (feedbackString, fieldObject) {
		this.field = null;
		this.feedback = feedbackString;
		this.valid = null;
		
		if (fieldObject) {
			this.setField(fieldObject);
		}
	},
	
	//	------------------------------------------------------------------------------------------------------------
	//	--- setField -----------------------------------------------------------------------------------------------	0.00
	//	------------------------------------------------------------------------------------------------------------

	setField: function (fieldObject) {
		this.field = $(fieldObject);
		if (this.field) {
			this.field.keyup($.proxy(this.fieldChangeHandler, this));
			this.field.change($.proxy(this.fieldChangeHandler, this));
		}
		
		this.updateValid();
	},
	
	//	------------------------------------------------------------------------------------------------------------
	//	----------------------------------------------------------------------------------------------- setField ---
	//	------------------------------------------------------------------------------------------------------------

	//	------------------------------------------------------------------------------------------------------------
	//	--- fieldHandlers ------------------------------------------------------------------------------------------	0.00
	//	------------------------------------------------------------------------------------------------------------

	fieldChangeHandler: function () {
		this.updateValid();
	},
	
	//	------------------------------------------------------------------------------------------------------------
	//	------------------------------------------------------------------------------------------ fieldHandlers ---
	//	------------------------------------------------------------------------------------------------------------

	//	------------------------------------------------------------------------------------------------------------
	//	--- getValue -----------------------------------------------------------------------------------------------	0.00
	//	------------------------------------------------------------------------------------------------------------
	//	Returns the value of field (or null if no field set)

	getValue: function () {
		if (this.field) {
			return this.field.val();
		}
		return null;
	},
	
	//	------------------------------------------------------------------------------------------------------------
	//	----------------------------------------------------------------------------------------------- getValue ---
	//	------------------------------------------------------------------------------------------------------------

	//	------------------------------------------------------------------------------------------------------------
	//	--- valid --------------------------------------------------------------------------------------------------	0.00
	//	------------------------------------------------------------------------------------------------------------
	
	setValid: function (value) {
		if (value != this.valid) {
			this.valid = value;
			$(this).trigger('validChange');
		}
	},
	
	getValid: function () {
		return this.valid;	
	},
	
	//	------------------------------------------------------------------------------------------------------------
	//	-------------------------------------------------------------------------------------------------- valid ---
	//	------------------------------------------------------------------------------------------------------------
	
	//	------------------------------------------------------------------------------------------------------------
	//	--- getFeedback --------------------------------------------------------------------------------------------	0.00
	//	------------------------------------------------------------------------------------------------------------
	
	getFeedback: function () {
		return this.feedback;
	},
	
	//	------------------------------------------------------------------------------------------------------------
	//	-------------------------------------------------------------------------------------------- getFeedback ---
	//	------------------------------------------------------------------------------------------------------------
	
	//	------------------------------------------------------------------------------------------------------------
	//	--- update -------------------------------------------------------------------------------------------------	0.00
	//	------------------------------------------------------------------------------------------------------------
	//	Methods called to change properties dynamically
	
	//	Reanalyze situation and set property "this.valid" accordingly
	updateValid: function () {
		this.setValid(false);
	}
	
	//	------------------------------------------------------------------------------------------------------------
	//	------------------------------------------------------------------------------------------------- update ---
	//	------------------------------------------------------------------------------------------------------------
	
});
