/**
* Javascript
* JS: Search View (Formular handling)
*
* @author i-fabrik GmbH
* @copyright 2008 i-fabrik GmbH
* @version $Id: validate_search_form.js,v 1.1 2009-07-29 11:51:26 heiko Exp $
*
*/


var ValidateSearchForm = new Class({
	Implements: [Options, Events],

	options: {
		defaultSearchTerm: 'Search term',
		minChars         : 3,
		debugMode        : false,
		onStart          : $empty(),
		onFailure        : $empty()
	},

	initialize: function(form, query, options) {
		this.setOptions(options);

		this.form  = form;
		this.query = query;

		if(!this._check())
			return;

		this.setupForm();
	},

	setupForm: function() {
		var self = this;

		if($defined(this.options.failureSearchTerm)) {
			this.options.failureSearchTerm = new Fx.Slide(self.options.failureSearchTerm, {
				duration: 200,
				link    : 'chain'
			}).hide();
		}

		this.form.addEvent('submit', function(ev) {
			ev.stop();
			var self_form = this;

			if(self.validate())
				self_form.submit();
		});

		this.fireEvent('onStart', this);
	},

	validate: function() {
		var res       = false;
		var q_value   = (this.query.get('value')) ? this.query.get('value').clean().toLowerCase() : '';
		var q_default = this.options.defaultSearchTerm.clean().toLowerCase();

		if(q_value != '' && q_value.length >= this.options.minChars) {
			res = true;

			if(q_default != '' && q_value == q_default)
				res = false;
		}

		if(!res)
			this.fireEvent('onFailure', this);

		return res;
	},

	_check: function() {
		var i   = 1;
		var msg = '';

		if(!$defined(this.form)) {
			msg+= i + ": Required element 'Form' not found.\n";
			i++;
		}

		if(!$defined(this.query)) {
			msg+= i + ": Required element 'Query' not found.\n";
			i++;
		}

		if(this.options.debugMode == true && msg != '') {
			alert(msg);
			return false;
		}

		return true;
	}

});

/*
var Search = {

	options: {
		'form'                  : null,
		'formInfoQueryEmpty'    : null,
		'formQuery'             : null,
		'formQueryDefaultPhrase': '',
		'formQueryMinChars'     : 3
	},

	start: function(options) {
		Search.options = $merge(Search.options, options||{});

		Search.setupForm();
	},

	setupForm: function() {
		if($defined(Search.options.form) && $defined(Search.options.formQuery)) {

			Search.form      = Search.options.form;
			Search.formQuery = Search.options.formQuery;

			if($defined(Search.options.formInfoQueryEmpty)) {
				Search.formInfoQueryEmpty = new Fx.Slide(Search.options.formInfoQueryEmpty, {
					duration: 200,
					link    : 'chain'
				}).hide();
			}

			Search.form.addEvent('submit', function(ev) {
				ev.stop();

				if(Search.checkForm())
					this.submit();
				else {
					Search.showInfoQueryEmpty();
					Search.formQuery.focus();
				}
			});

			if(!Search.checkForm()) {
				Search.showInfoQueryEmpty();
				Search.formQuery.focus();
			}

		}
	},

	checkForm: function() {
		var res       = false;
		var q_value   = (Search.formQuery.get('value')) ? Search.formQuery.get('value').clean().toLowerCase() : '';
		var q_default = Search.options.formQueryDefaultPhrase.clean().toLowerCase();

		if(q_value != '' && q_value.length >= Search.options.formQueryMinChars) {
			res = true;

			if(q_default != '' && q_value == q_default)
				res = false;
		}

		return res;
	},

	showInfoQueryEmpty: function() {
		if($defined(Search.formInfoQueryEmpty)) {
			Search.formInfoQueryEmpty.slideIn().chain(function() {
				this.slideOut.delay(3000, this);
			});
		}
	}
};
*/
