// FormController.class.js (Website Framework) || Version: 0.18 || Last Updated: 2010-10-07 17:00 || Updated by: Hidde-Finne Peters || Created: 2010-08-20 by Hidde-Finne Peters
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

var FormController = Class.extend({

	init: function (form_id, submit_button_id) {
		this.formId 			= form_id;
		this.submitButtonId 	= submit_button_id;
		this.form 				= null;
		this.submitButton 		= null;
		this.fieldControllers 	= Array();
		this.valid = true;
		this.used = false;
		
		$('document').ready($.proxy(this.documentReady, this));
	},
	
	//	------------------------------------------------------------------------------------------------------------
	//	--- documentReady ------------------------------------------------------------------------------------------	0.00
	//	------------------------------------------------------------------------------------------------------------
	//	Called after the document.ready event was triggered (when the page has finished loading and all physical objects are accesable)
	
	documentReady: function () {
		//	Set this.form
		this.form = $('#'+ this.formId);
		if (this.form.length == 0) {
			this.form = null;
		}
		//	Set this.submitButton
		if (this.submitButtonId) {
			//	submitButtonId is set, try to find the submitButton by id
			this.submitButton = $('#'+ this.submitButtonId);
		}
		if (!this.submitButton || this.submitButton.length == 0) {
			//	Try to find submitButton by default
			if (this.formId) {
				this.submitButton = $('#'+ this.formId +' input[type=image]');
				if (this.submitButton.length == 0) {
					this.submitButton = $('#'+ this.formId +' input[type=submit]');
				}
			}
		}
		if (this.submitButton && this.submitButton.length == 0) {
			this.submitButton = null;
		}
		
		//	Only submit form when valid
		if (this.form) {
			this.form.submit($.proxy(this.formSubmitHandler, this));
		}
		
		//	Check submit type
		if (this.submitButton) {
			switch (this.submitButton.attr('tagName')) {
				case 'IMG':
					this.submitButton.click($.proxy(this.submitButtonClickHandler, this));
					break;
				default:
					//alert('submitButton of type "'+ this.submitButton.attr('tagName') +'" not yet supported');
					break;
			}
		}

	},
	
	//	------------------------------------------------------------------------------------------------------------
	//	------------------------------------------------------------------------------------------ documentReady ---
	//	------------------------------------------------------------------------------------------------------------
	
	//	------------------------------------------------------------------------------------------------------------
	//	--- handlers -----------------------------------------------------------------------------------------------	0.00
	//	------------------------------------------------------------------------------------------------------------
	
	formSubmitHandler: function () {
		if (this.valid) {
			//	In order for this to be valid the fields with conditions required interaction, such as .keyup and .change. We can assume it was a human and not a bot
			var spamCheckField = $('#'+ this.formId +' input[name=spam_check_passed]');
			if (spamCheckField.length > 0) {
				spamCheckField.attr('value', 'true');
			}
			
			//	loader image (for uploads)	1.13
			var loadImg = $('#'+ this.formId +' img[id=loader]');
			if (loadImg.length > 0) {
				loadImg.css('display', 'block');
				if (this.submitButton) {
					this.submitButton.css('display', 'none');
				}
			}
			
			return true;
		} else {
			this.setUsed(true);
			$(this).trigger('triedInvalidSubmit');
			return false;
		}
	},
	
	submitButtonClickHandler: function () {
		this.form.submit();
	},
	
	//	------------------------------------------------------------------------------------------------------------
	//	----------------------------------------------------------------------------------------------- handlers ---
	//	------------------------------------------------------------------------------------------------------------

	//	------------------------------------------------------------------------------------------------------------
	//	--- addCondition -------------------------------------------------------------------------------------------	0.00
	//	------------------------------------------------------------------------------------------------------------
	
	addCondition: function (fieldName, condition) {
		if (fieldName && condition) {
			var fieldController = this.getFieldController(fieldName);
			
			if (!fieldController) {
				fieldController = this.addFieldController(fieldName);
			}
			
			//	Add the condition to the FieldController
			fieldController.addCondition(condition);
		}
	},
	
	//	------------------------------------------------------------------------------------------------------------
	//	------------------------------------------------------------------------------------------- addCondition ---
	//	------------------------------------------------------------------------------------------------------------
	
	//	------------------------------------------------------------------------------------------------------------
	//	--- removeCondition ----------------------------------------------------------------------------------------	0.00
	//	------------------------------------------------------------------------------------------------------------
	
	removeCondition: function (fieldName, condition) {
		if (fieldName && condition) {
			var fieldController = this.getFieldController(fieldName);
			
			if (fieldController) {
				//	Remove the condition to the FieldController
				fieldController.removeCondition(condition);
			}
		}
	},
	
	//	------------------------------------------------------------------------------------------------------------
	//	---------------------------------------------------------------------------------------- removeCondition ---
	//	------------------------------------------------------------------------------------------------------------
	
	//	------------------------------------------------------------------------------------------------------------
	//	--- fieldController manipulation ---------------------------------------------------------------------------	0.00
	//	------------------------------------------------------------------------------------------------------------
	
	getFieldController: function (fieldName) {
		for (var i = 0; i < this.fieldControllers.length; i++) {
			if (this.fieldControllers[i].fieldName == fieldName) {
				return this.fieldControllers[i];
			}
		}
		return null;
	},
	
	addFieldController: function (fieldName) {
		var fieldController = new FieldController(fieldName, this.formId);
		$(fieldController).bind('validChange', $.proxy(this.fieldValidChange, this));
		this.fieldControllers.push(fieldController);
		return fieldController;
	},
	
	//
	
	fieldValidChange: function () {
		this.updateValid();
	},
	
	//	------------------------------------------------------------------------------------------------------------
	//	--------------------------------------------------------------------------- fieldController manipulation ---
	//	------------------------------------------------------------------------------------------------------------

	//	------------------------------------------------------------------------------------------------------------
	//	--- used ---------------------------------------------------------------------------------------------------	0.00
	//	------------------------------------------------------------------------------------------------------------
	//	True after user has "used" the field (focus out)
	
	setUsed: function (value) {
		if (value != this.used) {
			this.used = value;
			this.updateFieldsUsed();
		}
	},
	
	//	------------------------------------------------------------------------------------------------------------
	//	--------------------------------------------------------------------------------------------------- used ---
	//	------------------------------------------------------------------------------------------------------------

	//	------------------------------------------------------------------------------------------------------------
	//	--- valid --------------------------------------------------------------------------------------------------	0.00
	//	------------------------------------------------------------------------------------------------------------	
	
	setValid: function (value) {
		if (value != this.valid) {
			this.valid = value;
			this.updateLayout();
		}
	},
	
	//	------------------------------------------------------------------------------------------------------------
	//	-------------------------------------------------------------------------------------------------- valid ---
	//	------------------------------------------------------------------------------------------------------------
	
	//	------------------------------------------------------------------------------------------------------------
	//	--- getFeedback --------------------------------------------------------------------------------------------	0.00
	//	------------------------------------------------------------------------------------------------------------	
	
	getFeedback: function () {
		var feedback = '';
		for (var i = 0; i < this.fieldControllers.length; i++) {
			if (!this.fieldControllers[i].valid) {
				var fieldFeedback = this.fieldControllers[i].getFeedback();
				if (fieldFeedback == '') {
					fieldFeedback = 'Het veld "'+ this.fieldControllers[i].fieldName +'" is nog niet juist ingevuld.';
				}
				feedback += '- '+ fieldFeedback +'\n';
			}
		}
		return feedback;
	},
	
	//	------------------------------------------------------------------------------------------------------------
	//	-------------------------------------------------------------------------------------------- getFeedback ---
	//	------------------------------------------------------------------------------------------------------------
	
	//	------------------------------------------------------------------------------------------------------------
	//	--- alertFeedback ------------------------------------------------------------------------------------------	0.00
	//	------------------------------------------------------------------------------------------------------------	
/*
	alertFeedback: function () {
		var feedback = 'Nog niet alle velden zijn juist ingevuld\n\n';
		feedback += this.getFeedback();
		alert(feedback);
	},
*/
	//	------------------------------------------------------------------------------------------------------------
	//	------------------------------------------------------------------------------------------ alertFeedback ---
	//	------------------------------------------------------------------------------------------------------------
	
	//	------------------------------------------------------------------------------------------------------------
	//	--- update -------------------------------------------------------------------------------------------------	0.00
	//	------------------------------------------------------------------------------------------------------------
	
	updateValid: function () {
		for (var i = 0; i < this.fieldControllers.length; i++) {
			if (!this.fieldControllers[i].valid) {
				this.setValid(false);
				return;
			}
		}
		this.setValid(true);
	},
	
	updateFieldsUsed: function () {
		for (var i = 0; i < this.fieldControllers.length; i++) {
			this.fieldControllers[i].setUsed(this.used);
		}
	},
	
	updateLayout: function () {
		if (this.submitButton) {
			switch (this.submitButton.attr('tagName')) {
				case 'IMG':
					//	submitButton is of type "image"
					if (this.valid) {
						this.submitButton.attr('src', this.submitButton.attr('src').replace(new RegExp('_disabled\\b'), '_default'));
						this.submitButton.css('cursor', 'pointer');
					} else {
						this.submitButton.attr('src', this.submitButton.attr('src').replace(new RegExp('_default\\b'), '_disabled'));
						this.submitButton.attr('src', this.submitButton.attr('src').replace(new RegExp('_over\\b'), '_disabled'));
						this.submitButton.css('cursor', 'auto');
					}
					break;
				case 'INPUT':
					if (this.submitButton.attr('type') == 'image') {
						//	submitButton is of type "image"
						if (this.valid) {
							this.submitButton.attr('src', this.submitButton.attr('src').replace(new RegExp('_disabled\\b'), '_default'));
							this.submitButton.css('cursor', 'pointer');
						} else {
							this.submitButton.attr('src', this.submitButton.attr('src').replace(new RegExp('_default\\b'), '_disabled'));
							this.submitButton.attr('src', this.submitButton.attr('src').replace(new RegExp('_over\\b'), '_disabled'));
							this.submitButton.css('cursor', 'auto');
						}
					} else if (this.submitButton.attr('type') == 'submit') {
						//	submitButton is of type "submit"
						//	DO NOT DISABLE BUTTON, EVENTS WILL NOT BE FIRED FROM DISABLED SUBMIT, FEEDBACK WILL NOT BE SHOWN. FORM SCRIPT HANDLES THE SUBMIT WELL ENOUGH WITHOUT DISABLING.
						/*if (this.valid) {
							this.submitButton.attr('disabled', '');
						} else {
							this.submitButton.attr('disabled', 'disabled');
						}*/
					}
					break;
				default:
					alert('submitButton of type "'+ this.submitButton.attr('tagName') +'" not yet supported');
					break;
			}
		}
	}
	
	//	------------------------------------------------------------------------------------------------------------
	//	------------------------------------------------------------------------------------------------- update ---
	//	------------------------------------------------------------------------------------------------------------
	
});
