    function FieldController()
    {
        this.init = function(elementId)
        {
            this.fieldElement = document.getElementById( elementId );
            this.defaultMaxlength = this.fieldElement.getAttribute("maxlength");
            this.defaultValue = this.fieldElement.value;
            this.emptyCaptionShown = false;
            this.fieldElement.controller = this;
            var controller = this;
            this.addListener("focus", function(){controller.hideGrayTextIfShown();});
            this.addListener("blur", function()
            {
                controller.fireBlur();
                if(controller.fieldElement.value != controller.defaultValue)
                {

                    controller.fireChange();
                }
                controller.showGreyTextIfEmpty();
            });
            this.addListener("keydown", function(){controller.fireChange();});
            this.addListener("change", function()
            {
                if(controller.fieldElement.value != controller.defaultValue)
                    controller.fireChange();
                controller.showGreyTextIfEmpty();
            });
            this.showGreyTextIfEmpty();
        };

        this.setDefaultValueIfNeeded = function()
        {
            if (trim(this.fieldElement.value).length == 0)
                this.fieldElement.defaultValue = this.emptyCaption;
        };
        
        this.onChangeHandler = undefined;
        this.onBlurHandler = undefined;
        this.emptyCaption = "";
        this.emptyCaptionStyleClass = "emptyCaption";

        this.hideGrayText = function()
        {
            this.fieldElement.value = "";
            if (this.defaultMaxlength != null)
                this.fieldElement.setAttribute("maxlength", this.defaultMaxlength)
            this.fieldElement.className = "";
            this.emptyCaptionShown = false;
        };

        this.hideGrayTextIfShown = function()
        {
            if (this.emptyCaptionShown)
                this.hideGrayText();
        };

        this.showGreyText = function()
        {
            this.fieldElement.className = this.emptyCaptionStyleClass;
            if (this.defaultMaxlength != null)
                this.fieldElement.setAttribute("maxlength", this.emptyCaption.length)
            this.fieldElement.value = this.emptyCaption;
            this.emptyCaptionShown = true;
        };

        this.showGreyTextIfEmpty = function()
        {
            if (this.isEmpty())
                this.showGreyText();
        };

        this.isEmpty = function()
        {
            return this.emptyCaptionShown || trim(this.fieldElement.value).length == 0 ||
                   this.fieldElement.value == this.emptyCaption;
        };

        this.addListener = function (event, listener)
        {
            if (this.fieldElement.addEventListener)
                this.fieldElement.addEventListener(event, listener, false);
            else
                this.fieldElement.attachEvent('on' + event, listener);
        };

        this.fireChange = function()
        {
            if(this.onChangeHandler)
            {
                this.onChangeHandler(this);
            }
        };

        this.fireBlur = function()
        {
            if(this.onBlurHandler)
            {
                this.onBlurHandler(this);
            }
        };

        this.getValue = function()
        {
            return this.isEmpty() ? "" : this.fieldElement.value;
        };

        this.getElement = function()
        {
            return this.fieldElement;
        };

    }

