// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
function clearGhost(element) 
{
  if (element.value == element.ghostValue)
  {
    element.value = "";
    element.className = "normal";
    return true;
  }
  return false;
}

function setGhost(element, ghostText) 
{
  if (element.value == "" || element.value == ghostText)
  {
    element.value = ghostText;
    element.ghostValue = ghostText;
    element.className = "with_ghost";
    return true;
  }
  return false;
}

function clearPasswordGhost(element) 
{
  if (clearGhost(element))
  {
    if (element.type == "text")
    {
      newEl = ie_safe_change_type(element, "password");
      
      newEl.focus();
      newEl.select();
    }
  }
}

function setPasswordGhost(element, ghostText) 
{
  if (setGhost(element, ghostText))
  {
    if (element.type == "password")
    {
        newEl = ie_safe_change_type(element, "text");
    }
  }
}

function ie_safe_change_type(element, type)
{
  try {
     element.type  = type;
     return element;
  } catch (e) {
     var newElement = null;
     var tempStr = "";
     var nameStr = element.getAttribute("name");
     var sizeStr = element.getAttribute("size");
     var onFocusStr = element.getAttribute("onfocus");
     var onBlurStr = element.getAttribute("onblur");
     var classStr = element.getAttribute("class");
     var idStr = element.getAttribute("id");
     try {
        newElement = document.createElement("<input type=\"" +type+ "\" name=\"" + nameStr+ "\">");
     } catch (e) {}
     if (!newElement) {
        newElement = document.createElement("input");
        newElement.setAttribute("type", type);
        newElement.setAttribute("name", nameStr);        
     }
     newElement.setAttribute("id", idStr);
     newElement.setAttribute("size", sizeStr);
     newElement.setAttribute("onfocus", onFocusStr);
     newElement.setAttribute("onblur", onBlurStr);
     newElement.setAttribute("class", classStr); 
     newElement.value = element.value;
     newElement.ghostValue = element.ghostValue;
     newElement.className = element.className;
     element.parentNode.insertBefore(newElement, element);
     element.parentNode.removeChild(element);
     return newElement;
  }
  
}


function clearGhosts()
{
  $A($$('input.with_ghost')).each( function(e) { clearGhost(e) });
}