/* ---------------------------------------------
-
-  JavaScript Form Assistant
-  Version 0.73, Build 20080910
-  Thomas A. Corey, Talsoft Enterprises Inc.
-  http://www.talsoftent.com
-
-----------------------------------------------*/

FormFieldAssistant = function(target_form, target_field, message) {
	if (typeof(target_form) != "string")
		alert("Binding failed, wrong object type for target_form reference.");

	if (typeof(target_field) != "string")
		alert("Binding failed, wrong object type for target_field reference.");

	this.container = document.forms[target_form];
	this.field = this.container.elements[target_field];
	this.field.assistant = this;
	this.assistant = null;
	this.deferred = (typeof(message) == "function");
	this.message = message;

	this.registered_blur = [];
	this.registered_focus = [];
};

FormFieldAssistant.prototype.registerMethod = function (callback_method, is_focus) {
	if (typeof(callback_method) !== "function") return;
	if (is_focus) this.registered_focus.push(callback_method);
	else this.registered_blur.push(callback_method);
};

FormFieldAssistant.prototype.registerMethods = function(callback_method) {
	this._registerMethods(arguments);
};

FormFieldAssistant.prototype._registerMethods = function(callback_methods) {
	for (var a = 0; a < callback_methods.length; a++) {
		this.registerMethod(callback_methods[a]);
	}
}

FormAssistant = function(target_container) {
	if (typeof(target_container) != "string")
		alert("Binding failed, wrong object type.");

	this.container = document.getElementById(target_container);
	this.default_text = this.container.innerHTML;
	this.messages = [];
};

FormAssistant.prototype.add = function (field_assistant) {
	field_assistant.assistant = this;
	this.messages.push(field_assistant);
};

FormAssistant.prototype.addRange = function(field_assistant) {
	this._addRange(arguments);
};

FormAssistant.prototype._addRange = function(field_assistants) {
	for (var a = 0; a < field_assistants.length; a++) {
		this.add(field_assistants[a]);
	}
};

FormAssistant.prototype.fieldBlur = function() {
	var router = this.assistant;
	var dispatcher = router.assistant;
	dispatcher.container.innerHTML = dispatcher.default_text;

	if (router.registered_blur != null && router.registered_blur.length != 0) {
		var blur_method;
		for (var a = 0; a < router.registered_blur.length; a++) {
			blur_method = router.registered_blur[a];
			blur_method.call(this);
		}
	}
};

FormAssistant.prototype.fieldFocus = function() {
	var router = this.assistant;
	var dispatcher = router.assistant;

	dispatcher.container.innerHTML = (router.deferred ? router.message.call(this) : router.message);

	if (router.registered_focus != null && router.registered_focus.length != 0) {
		var focus_method;
		for (var a = 0; a < router.registered_focus.length; a++) {
			focus_method = router.registered_focus[a];
			focus_method.call(this);
		}
	}
};

FormAssistant.prototype.bind = function() {
	if (this.messages == null || this.messages.length == 0) return;
	var item;
	for (var a = 0; a < this.messages.length; a++) {
		item = this.messages[a];
		item.field.onfocus = this.fieldFocus;
		item.field.onblur = this.fieldBlur;
	}
};


if (!Array.prototype.for_each) {
	Array.prototype.for_each = function(callback) {
		var len = this.length;
		if (typeof(callback) != "function") throw new TypeError();

		for (var a = 0; a < len; a++) {
			callback.call(this[a]);
		}
	}
}