Event.observe(document, "dom:loaded", function () {
    setupValueLabels();
});

function setupValueLabels () {
    $$("input[type=text]").each(function (input) {
        var labelText = input.readAttribute("data-value-label");
        if (!labelText) return;
        if (input.readAttribute("type") != "text") return;
        
        if (input.readAttribute("data-value-label-password")) {
            // Special case for fields that need to turn into password
            // fields.
            setupValueLabelPassword(input);
            return;
        }
        
        if (input.value == "") input.value = labelText;
        
        input.observe("focus", function (e) {
            if (input.value == labelText) input.value = "";
        });
    
        input.observe("blur", function (e) {
            if (input.value == "") input.value = labelText;
        });
    });
};

function setupValueLabelPassword (textInput) {
    textInput.observe("focus", function (e) {
        var passInput = new Element("input", {
            "type": "password",
            "class": textInput.readAttribute("class"),
            "name": textInput.readAttribute("name"),
            "id": textInput.readAttribute("id")
        });
        
        textInput.insert({ after: passInput });
        textInput.remove();
        passInput.focus();
    });
};

function getCookie(name) {
  var start = document.cookie.indexOf(name + "=");
  var len = start + name.length + 1;
  if ((!start) && (name != document.cookie.substring(0, name.length))) return null;
  if (start == -1) return null;
  var end = document.cookie.indexOf( ";", len );
  if (end == -1) end = document.cookie.length;
  return unescape(document.cookie.substring(len, end));
}