/**
 * Setup prompt in text box specified
 *
 * @id the id of the text box, it is expected to have prompt attribute assigned
 */ 
function promptSetup(id) {
  var el = document.getElementById(id);
  if (!el || el.type != 'text') { 
    return;
  }
  var pr = el.getAttribute("prompt");
  if (!pr || pr == "") {
    return;
  }
  el.value = pr;
  el.style.color = '#888';
  el.is_focused = 0;
  el.onfocus = function () { promptFocus(id); }
  el.onblur = function () { promptBlur(id); }
}

/**
 * Test whether the current value in textbox is the default prompt
 *
 * @objid the id of the text box, it is expected to have prompt attribute assigned
 */ 
function testDefault(objid) {
  var obj = document.getElementById(objid); 
  if (!obj) {
    return false;
  }
  var ph = obj.getAttribute("prompt");
  return obj.value == "" || obj.value == ph;
}

/**
 * Called when a text box loses focus
 *
 * @objid the id of the text box, it is expected to have prompt attribute assigned
 */ 
function promptBlur(objid) {
  var obj = document.getElementById(objid); 
  var ph = obj.getAttribute("prompt");
  if (obj.is_focused && ph && obj.value == "") {
    obj.is_focused = 0;
    obj.value = ph;
    obj.style.color = '#888';
  }
}

/**
 * Called when a text box gains focus
 *
 * @objid the id of the text box, it is expected to have prompt attribute assigned
 */ 
function promptFocus(objid) {
  var obj = document.getElementById(objid); 
  if (!obj.is_focused) {
    obj.value = '';
    obj.is_focused = 1;
    obj.style.color = '#000';
  }
}
