function togglePassword(id) {
    if (!document.getElementById(id)) { return; }

    var password_field = document.getElementById(id);
    
//  Create the text field
    var text_field = document.createElement('input');
    text_field.setAttribute('type','text');
    text_field.style.display = 'none';

//  Create the container for the checkbox and label
    var span = document.createElement('span');
    span.className = 'passwordtoggle';

//  Create the checkbox
    var checkbox = document.createElement('input');
    checkbox.setAttribute('type','checkbox');
    checkbox.setAttribute('id',id+'-checkbox');

//  Create the label
    var label = document.createElement('label');
    var label_text = document.createTextNode('show password');
    label.setAttribute('for',id+'-checkbox');
    label.appendChild(label_text);

//  Put the checkbox and label into the container
    span.appendChild(checkbox);
    span.appendChild(label);

//  Toggle the display of the password and text fields
    checkbox.onclick = function() {
        if (this.checked) {
            password_field.style.display = 'none';
            text_field.style.display = 'inline';
        } else {
            text_field.style.display = 'none';
            password_field.style.display = 'inline';
        }
    };

//  Keep the values of the password and text fields in sync
    var syncFields = function() {
        if (password_field.style.display == 'none') {
            password_field.value = text_field.value;
        } else {
            text_field.value = password_field.value;
        }
    };

    password_field.onkeyup = syncFields;
    text_field.onkeyup = syncFields;

//  Insert the container with the checkbox and label after the password field
    if (password_field.parentNode.lastChild == password_field) {
        password_field.parentNode.appendChild(span);
    } else {
        password_field.parentNode.insertBefore(span,password_field.nextSibling);
    }

//  Insert the text field before the container
    span.parentNode.insertBefore(text_field,span);

    syncFields();

}

togglePassword('login-password');