greyOut = function(txt, doGreyOut) {
    txt.setStyle({fontStyle: 'normal', 
                  color : doGreyOut ? '#B3B3B3' : 'black'});
}

txtOnBlur = function(event) {
    greyOut(this, this.no_data);

    if(this.no_data) {
        this.value = this.hint_text;
    }
}

txtOnFocus = function(event) {
    if(this.no_data) {
        this.value = '';
    }
    greyOut(this, false);
}

txtOnChange = function(event) {
    this.no_data = (this.value == '');
}

bindToFormSubmit = function(item) {
    var elem = item.parentNode;

    do {
        if (elem.tagName && elem.tagName.toLowerCase() == 'form') {
            Event.observe(elem, 'submit', function() {
                                            if(this.no_data)
                                                this.value = ''
                                          }.bindAsEventListener(item));
            break;
        } else {
            elem = elem.parentNode;
            if (elem == null)
                break;
        }
    } while(true);
}

initTextInputControl = function(txt, hint_text, no_data) {
    if (no_data) {
        txt.value = hint_text
        greyOut(txt, true)
    }
        
    txt.hint_text = hint_text;
    txt.no_data = no_data;

    txt.observe('blur', txtOnBlur.bindAsEventListener(txt));
    txt.observe('focus', txtOnFocus.bindAsEventListener(txt));
    txt.observe('change', txtOnChange.bindAsEventListener(txt));
    bindToFormSubmit(txt);

    txt.getClearedValue = function(elem) {
        return this.no_data ? '' : this.value.strip();
    }.bind(txt);
}
