// FormProtectionController.class.js (Fertile Form JS Framework) || Version: 0.01 || Last Updated: 2011-02-03 21:30 || Updated by: Hidde-Finne Peters || Created: 2010-12-17 by Hidde-Finne Peters
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

var FormProtectionController = Class.extend({

	init: function (form_id, key, submit_button_id) {
		this.formId 			= form_id;
		this.antibotPrimaryKey	= key;
		this.submitButtonId 	= submit_button_id;
		this.form				= null;
		this.keyField			= null;
		this.submitButton		= null;
		this.interactionDetected = 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 () {

		this.form = $('#'+ this.formId);
		if (this.form.length == 0) {
			this.form = null;
		}

		//	No use doing anything without a form
		if (this.form) {
			
			this.keyField = $('#'+ this.formId +' input[name=antibot_primary_key]');
			if (this.keyField.length == 0) {
				this.keyField = null;
			}
			
			//	No use doing anything without the keyField
			if (this.keyField) {
	
				var uiObjects = Array();
		
				$('#'+ this.formId +' input').each(function(){
					if (this.type != 'hidden') {
						uiObjects.push(this);
					}
				});
				$('#'+ this.formId +' select').each(function(){
					uiObjects.push(this);
				});
				$('#'+ this.formId +' textarea').each(function(){
					uiObjects.push(this);
				});
		
				for (var i = 0; i < uiObjects.length; i++) {
					$(uiObjects[i]).bind('click', $.proxy(this.interfaceInteractionHandler, this));
					$(uiObjects[i]).bind('focus', $.proxy(this.interfaceInteractionHandler, this));
				}
				
				if (this.submitButtonId) {
					this.submitButton = $('#'+ this.submitButtonId);
					if (this.submitButton.length > 0) {
						this.submitButton.click($.proxy(this.interfaceInteractionHandler, this));
					}
				}
				
			}
			
		}
	},
	
	//	------------------------------------------------------------------------------------------------------------
	//	------------------------------------------------------------------------------------------ documentReady ---
	//	------------------------------------------------------------------------------------------------------------
	
	//	------------------------------------------------------------------------------------------------------------
	//	--- handlers -----------------------------------------------------------------------------------------------	0.00
	//	------------------------------------------------------------------------------------------------------------
	
	interfaceInteractionHandler: function () {
		this.setInteractionDetected(true);
	},
	
	//	------------------------------------------------------------------------------------------------------------
	//	----------------------------------------------------------------------------------------------- handlers ---
	//	------------------------------------------------------------------------------------------------------------
	
	//	------------------------------------------------------------------------------------------------------------
	//	--- interactionDetected ------------------------------------------------------------------------------------	0.00
	//	------------------------------------------------------------------------------------------------------------
	
	setInteractionDetected: function (value) {
		if (value != this.interactionDetected) {
			this.interactionDetected = value;
			this.updateFormPrimaryKey();
		}
	},
	
	//	------------------------------------------------------------------------------------------------------------
	//	------------------------------------------------------------------------------------ interactionDetected ---
	//	------------------------------------------------------------------------------------------------------------
	
	//	------------------------------------------------------------------------------------------------------------
	//	--- update -------------------------------------------------------------------------------------------------	0.00
	//	------------------------------------------------------------------------------------------------------------
	
	updateFormPrimaryKey: function () {
		if (this.interactionDetected) {
			this.keyField.val(this.antibotPrimaryKey);
		} else {
			this.keyField.val('');
		}
	}
	
	//	------------------------------------------------------------------------------------------------------------
	//	------------------------------------------------------------------------------------------------- update ---
	//	------------------------------------------------------------------------------------------------------------
	
});
