﻿/*****************************************************************************/
/*
 * DefaultTextFieldManager. This manager displays default text values for
 * input fields when no value is entered. removes the default value if
 * the text input receives focus.
 *
 * requires prototype.js and rvlprototype.js AND expects the page to contain only one form. Overwrites the onsubmit
 */
var DefaultTextFieldManager = Class.create({

    /// Constructor. may be called before dom:loaded.
    /// pass an object with parameters. Only the first parameters is required
    ///
    /// hashDefaultTexts -> REQUIRED FIELD (a Hash containing a mapping from the id of
    ///                                a form control to its intented defaultValue. If using ASP.NET controls,
    ///                                pass the ClientId for each field as the key.
    ///
    /// defaultActiveClassName -> 'txtFieldDefaulted' (the className that is added if the textfield contains its default value)
    ///
    /// example:
    ///
    /// new DefaultTextFieldManager({
    ///     new Hash({'<%= txtName.ClientID %>' : '<%= Master.GetText("i18n_contacts_clinics_institution_name_default") %>'})
    /// });

    
    initialize: function(objParameters)
    {
        objParameters = $H(objParameters);

        this.hashFieldIdsToDefaultValues = objParameters.getOptional('hashDefaultTexts', null);
        if(this.hashFieldIdsToDefaultValues == null) alert("YOU MUST PROVIDE THE hashDefaultTexts PARAMETER IF YOU WISH TO USE THE DefaultTextFieldManager");
        this.defaultActiveClassName = objParameters.getOptional('defaultActiveClassName', 'txtFieldDefaulted');
        
        document.observe('dom:loaded', this._OnDomLoaded.bind(this));
    },
    _OnDomLoaded : function()
    {
        this.hashFieldIdsToDefaultValues.keys().each(this.activateDefaultableTextField.bind(this));

        document.forms[0].onsubmit = this.onDocumentSubmit.bind(this);
    },
    activateDefaultableTextField : function(fieldId)
    {
        var field = $(fieldId);
        Event.observe(field, 'focus', this.onDefaultableTextFieldFocus.bindAsEventListener(this));
        Event.observe(field, 'blur', this.onDefaultableTextFieldChange.bindAsEventListener(this));
        this.onDefaultableTextFieldChange(null, field);
    },
    setFieldValue: function (field, value)
    {
        this.onDefaultableTextFieldFocus(null,field);
        $(field).value = value;
        this.onDefaultableTextFieldChange(null,field);
    },
    onDefaultableTextFieldFocus  :function(e, field)
    {
        var elem = (e != null && Event.element(e)) || $(field);
        var value = $F(elem);
        if(value == this.hashFieldIdsToDefaultValues.get(elem.id))
        {
            elem.value = '';
            elem.removeClassName(this.defaultActiveClassName);
        }
    },
    onDefaultableTextFieldChange : function (e, field)
    {
        var elem = (e != null && Event.element(e)) || field;
        var value = $F(elem);
        if(value.strip() == '' || value == this.hashFieldIdsToDefaultValues.get(elem.id))
        {
            elem.value = this.hashFieldIdsToDefaultValues.get(elem.id);
            elem.addClassName(this.defaultActiveClassName);
        }
    },
    clearField : function(field)
    {
        field.value = "";
        this.onDefaultableTextFieldChange(null, field);
    },
    
    // get the value with default value removed, only needed if you're not waiting for the submit (that's handled automatically)
    getValue : function(field)
    {
        this.onDefaultableTextFieldFocus(null, field);
        return $F(field);
    },
    onDocumentSubmit : function(e)
    {
        var clearFunction = this.onDefaultableTextFieldFocus.bind(this, null);
        this.hashFieldIdsToDefaultValues.keys().each(clearFunction);
        return true;
    }

});
     
    



