// EmailFormatCondition.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
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

var EmailFormatCondition = FormCondition.extend({
	
	init: function (feedbackString, fieldObject) {
		this._super(feedbackString, fieldObject);
	},
	
	//	------------------------------------------------------------------------------------------------------------
	//	--- update -------------------------------------------------------------------------------------------------	0.00
	//	------------------------------------------------------------------------------------------------------------
	//	Methods called to change properties dynamically
	
	//	Reanalyze situation and set property "this.valid" accordingly
	updateValid: function () {
		if (this.isEmailFormat(this.getValue())) {
			this.setValid(true);
			return;
		}
		this._super();
	},
	
	//	------------------------------------------------------------------------------------------------------------
	//	------------------------------------------------------------------------------------------------- update ---
	//	------------------------------------------------------------------------------------------------------------
	
	//	------------------------------------------------------------------------------------------------------------
	//	--- isEmailFormat ------------------------------------------------------------------------------------------	0.00
	//	------------------------------------------------------------------------------------------------------------
	
	isEmailFormat: function (str) {
		//	Empty string is valid (ensures this condition can be applied to fields that do not require values)
		if (str.length < 1) {
			return true;	
		}
		//	If value has length the format needs to be valid
		if (str.length > 6) {
			//	Length is sufficient
			var atIndex = str.indexOf("@");
			if (atIndex > 0) {
				//	@ is valid
				var dotIndex = str.indexOf(".", atIndex);
				if (dotIndex - atIndex > 2) {
					//	Enough characters between @ and .
					if ((str.length - 2) > dotIndex) {
						//	Enough characters after .
						//	If it gets here the string must be good
						return true;
					}
				}
			}
		}
		return false;			
	}

	//	------------------------------------------------------------------------------------------------------------
	//	------------------------------------------------------------------------------------------ isEmailFormat ---
	//	------------------------------------------------------------------------------------------------------------
	
});


