var isReadOnly = this.getField("Text4").readonly;
if (isReadOnly) {
(function () {
// Get one of the fields in the group
var f = getField("Text4");
// Determine new readonly state, which
// is the opposite of the current state
var readonly = !f.readonly;
var readonly_desc = readonly ? "deactivate" : "activate";
// Ask user for password
var resp = app.response({
cQuestion: "To " + readonly_desc + " the fields, enter the password:",
cTitle: "Enter password",
bPassword: true,
cLabel: "Password"
});
switch (resp) {
case "123": // Your password goes here
getField("private").readonly = readonly;
app.alert("The fields are now " + readonly_desc + "d.", 3);
break;
case null : // User pressed Cancel button
break;
default : // Incorrect password
app.alert("Incorrect password.", 1);
break;
}
})();
for (var i=0; i<this.numFields; i++) {
var fieldName = this.getNthFieldName(i);
if (fieldName != event.target.name) {
this.getField(fieldName).readonly = false;
}
}
}
else {
for (var i=0; i<this.numFields; i++) {
var fieldName = this.getNthFieldName(i);
if (fieldName != event.target.name) {
this.getField(fieldName).readonly = true;
}
}
}
I assume you don't know what an anonymous JavaScript function is, and that's why you define one inside the "if" statement. You use such an anonymous function to create a private scope for variables, that otherwise would end up being global. If you want to use this mechanism, you need to pull out the function definition to the top level. Also, your code set the fields to writable regardless of the password entered. I moved a few things around, and this is working for me:
(function() {
var isReadOnly = this.getField("Text4").readonly;
if (isReadOnly) {
// Get one of the fields in the group
var f = getField("Text4");
// Determine new readonly state, which
// is the opposite of the current state
var readonly = !f.readonly;
var readonly_desc = readonly ? "deactivate" : "activate";
// Ask user for password
var resp = app.response({
cQuestion: "To " + readonly_desc + " the fields, enter the password:",
cTitle: "Enter password",
bPassword: true,
cLabel: "Password"
});
switch (resp) {
case "123": // Your password goes here
for (var i = 0; i < this.numFields; i++) {
var fieldName = this.getNthFieldName(i);
if (fieldName != event.target.name) {
this.getField(fieldName).readonly = false;
}
}
app.alert("The fields are now " + readonly_desc + "d.", 3);
break;
case null: // User pressed Cancel button
break;
default: // Incorrect password
app.alert("Incorrect password.", 1);
break;
}
} else {
for (var i = 0; i < this.numFields; i++) {
var fieldName = this.getNthFieldName(i);
if (fieldName != event.target.name) {
this.getField(fieldName).readonly = true;
}
}
}
})();