var PromptedInput = {
  init: function(inputId, prompt, emptyClass, nonemptyClass) {
     var box = rol.getObj(inputId);
     var shadow = null;
     if (box == null || box.tagName != 'INPUT' || !(box.type == 'text' || box.type == 'password') ) return;

     box.setValue = function(newval) {
       this.value = newval;
       this.className = newval ? nonemptyClass : emptyClass;
       this.setAttribute('_isEmpty', newval ? 'false' : 'true');
     }
     
     if (box.type == 'password') {
        shadow = this._clone(box);
        shadow.value = prompt;
        shadow.className = emptyClass;
     }

     if (box.value.length == 0) {
        box.setAttribute('_isEmpty', 'true');
        if (shadow != null)
           box.parentNode.replaceChild(shadow, box);
        else
           box.value = prompt;
        box.className = emptyClass;
     }
     else {
        box.setAttribute('_isEmpty', 'false');
        box.className = nonemptyClass;
     }
     

     rol.addEventHandler(box, 'blur',
       function() {
         if (box.getAttribute('_isEmpty') == 'true') {
            if (shadow != null)
               box.parentNode.replaceChild(shadow, box);
            else {
               box.className = emptyClass;
               box.value = prompt;
            }
         }
       }, false
     );

     rol.addEventHandler(box, 'focus',
       function() {
         if (box.getAttribute('_isEmpty') == 'true')
            box.value = '';
         box.className = nonemptyClass;
       }, false
     );

     rol.addEventHandler(box, 'keyup',
       function() {
         box.setAttribute('_isEmpty', box.value.length == 0 ? 'true' : 'false');
       }, false
     );

     var pasteEventName = typeof(box.onpaste) != 'undefined' ? 'paste' : 'input';
     rol.addEventHandler(box, pasteEventName,
       function() {
         box.setAttribute('_isEmpty', 'false');
       }, false
     );
   },

   _clone: function(box)
   {
     var txt = document.createElement('input');
     txt.setAttribute('type','text');

     rol.addEventHandler(txt, 'focus',
       function() {
            txt.parentNode.replaceChild(box, txt);
            box.focus(); box.focus();
       }
     );
     return txt;
   }
}

