﻿/**
 * Suchfeld
 */
var ucSearch = Class.create({
    /**
    * Konstruktor
    * Erstellen des Controls
    * @param {Object} elem - Node
    */
    initialize: function (elem) {
        this.container = elem;
        this.obj = elem;

        try {
            
            // Input-Element erfassen und initialisieren
            this.input = this.container.down('input[type="text"]') || null;
            this.defaultValue = this.input.value; 
			//console.log(this.defaultValue);
            this.errMsg = this.container.down('input.error').value;
            this.resetInput();

            Event.observe(this.input, 'focus', this.clearInput.bindAsEventListener(this));
            Event.observe(this.input, 'blur', this.resetInput.bindAsEventListener(this));

            // Form-Element erfassen
            this.form = this.input.form || null;
            // Prüfung deaktiviert, da im asp-Form mehrer Submit-Buttons mit unterschiedlicher Bedeutung möglich sind
            // Event.observe(this.form, 'submit', this.onSubmit.bindAsEventListener(this));
        }
        catch (err) {
            uc.debug(err);
        }
    },

    /**
    * Standard-Text der Eingabe initialisieren
    */
    resetInput: function () {
        if (this.input && this.input.value.blank()) {
            // Standard-Text ist der Title-Text der Textbox
            // dadurch Sprach- und Config-Unabhängig
            this.input.value = this.defaultValue;
        }
    },

    /**
    * Input leeren
    * @param {Object} event
    */
    clearInput: function (event) {
        if (this.input.value.blank() || this.input.value == this.defaultValue) {
            this.input.value = '';
        }
    },

    /**
    * Vor Absenden des Formulars prüfen
    * @param {Object} event
    */
    onSubmit: function (event) {
        if (!this.check()) {
            Event.stop(event);
            alert(this.errMsg);
            this.input.focus();
        }
    },

    /**
    * Eingabeprüfung
    * @return bool
    */
    check: function () {
        return (!this.input.value.blank() && this.input.value != this.defaultValue);
    }
});

/**
* Initialisierung beim Laden
* @param {Object} el
*/
$$('.uc-Search').each(
    function (el) {
        uc.add(new ucSearch(el));
    }
);
