+var userMgr = {
+ _adduserDisplayed: false,
+ _changepassDisplayed: false,
+
+ init: function() {
+ $("#user_close").unbind("click").click(function () {
+ userMgr.close()
+ });
+
+ $("#change_pass").unbind("click").click(function() {
+ userMgr.toggleChangePass();
+ return false;
+ });
+ $("#changepass").unbind("submit").submit(function() {
+ try {
+ userMgr.changepass();
+ } catch(e) {}
+ return false;
+ });
+
+ if (sypSettings.loggedUser != "admin") {
+ return;
+ }
+
+ $("#add_user").show();
+ $("#add_user").unbind("click").click(function () {
+ userMgr.toggleAddUser();
+ return false;
+ });
+ $("#newuser").unbind("submit").submit(function() {
+ try {
+ userMgr.add();
+ } catch(e) {}
+ return false;
+ });
+
+ },
+
+ disableForms: function() {
+ $("#newuser_name, #newuser_password, #newuser_password_confirm, #newuser_submit").attr("disabled", "disabled");
+ $("#pass_current, #pass_new, #pass_new_confirm, #pass_submit").attr("disabled", "disabled");
+ },
+
+ enableForms: function() {
+ $("#newuser_name, #newuser_password, #newuser_password_confirm, #newuser_submit").removeAttr("disabled");
+ $("#pass_current, #pass_new, #pass_new_confirm, #pass_submit").removeAttr("disabled");
+ },
+
+ resetForms: function() {
+ $("#newuser_name, #newuser_password, #newuser_password_confirm").val("");
+ $("#pass_current, #pass_new, #pass_new_confirm").val("");
+ },
+
+ uninit: function() {
+ this.close();
+ $("#add_user").unbind("click");
+ $("#add_user").hide();
+ $("#change_pass").unbind("click");
+ $("#user_close").unbind("click");
+ $("#newuser").unbind("submit");
+ $("#changepass").unbind("submit");
+ },
+
+ close: function() {
+ this.closeChangePass();
+ this.closeAddUser();
+ },
+
+ toggleChangePass: function() {
+ if (this._changepassDisplayed) {
+ this.closeChangePass();
+ } else {
+ this.showChangePass();
+ }
+ },
+
+ showChangePass: function() {
+ if (!Admin.cancelCurrentFeature()) {
+ return;
+ }
+ this.closeAddUser();
+
+ $(document).unbind("keydown").keydown(function(e) {
+ if (e.keyCode == 27) {
+ userMgr.closeChangePass()
+ e.preventDefault();
+ }
+ });
+
+ this.resetForms();
+ this.enableForms();
+ $("#user_area, #changepass").show();
+ this.commError("");
+
+ // XXX: setTimeout needed because otherwise, map becomes hidden in IE. Why ??
+ window.setTimeout(function() {
+ $("#pass_current").focus();
+ }, 0);
+
+ this._changepassDisplayed = true;
+ },
+
+ closeChangePass: function() {
+ if (!this._changepassDisplayed) {
+ return;
+ }
+ $("#user_area, #changepass").hide();
+ $(document).unbind("keydown");
+ this._changepassDisplayed = false;
+ },
+
+ changepass: function() {
+ var newpass = $("#pass_new").val();
+ var newpass_confirm = $("#pass_new_confirm").val();
+ if (newpass != newpass_confirm) {
+ this.commError(SypStrings.userPasswordmatchError);
+ $("#pass_new").focus().select();
+ return;
+ }
+
+ if (!newpass) {
+ this.commError(SypStrings.emptyPasswordError);
+ $("#pass_new").focus().select();
+ return;
+ }
+
+ var curpass = $("#pass_current").val();
+ if (newpass == curpass) {
+ this.commError(SypStrings.changeSamePass);
+ $("#pass_new").focus().select();
+ return;
+ }
+
+ this.commError("");
+
+ AjaxMgr.add({
+ form: $("#changepass"),
+ oncomplete: OpenLayers.Function.bind(this.ajaxReply, this),
+ throbberid: "user_throbber",
+ onsend: function() {
+ // we need a timeout; otherwise those fields will not be submitted
+ window.setTimeout(function() {
+ // removes focus from #password before disabling it. Otherwise, opera
+ // prevents re-focusing it after re-enabling it.
+ $("#pass_current, #pass_new, #pass_new_confirm").blur();
+ userMgr.disableForms();
+ }, 0);
+ }
+ });
+ },
+
+ toggleAddUser: function() {
+ if (this._adduserDisplayed) {
+ this.closeAddUser();
+ } else {
+ this.showAddUser();
+ }
+ },
+
+ showAddUser: function() {
+ if (!Admin.cancelCurrentFeature()) {
+ return;
+ }
+
+ this.closeChangePass();
+
+ $(document).unbind("keydown").keydown(function(e) {
+ if (e.keyCode == 27) {
+ userMgr.closeAddUser()
+ e.preventDefault();
+ }
+ });
+
+ $("#user_area, #newuser").show();
+ this.resetForms();
+ this.enableForms();
+ this.commError("");
+
+ // XXX: setTimeout needed because otherwise, map becomes hidden in IE. Why ??
+ window.setTimeout(function() {
+ $("#newuser_name").focus();
+ }, 0);
+
+ this._adduserDisplayed = true;
+ },
+
+ closeAddUser: function() {
+ if (!this._adduserDisplayed) {
+ return;
+ }
+ $("#user_area, #newuser").hide();
+ $(document).unbind("keydown");
+ this._adduserDisplayed = false;
+ },
+
+ add: function() {
+ var newuser_name = $("#newuser_name").val();
+ if (!newuser_name) {
+ this.commError(SypStrings.newUserNonameError);
+ $("#newuser_name").focus();
+ return;
+ }
+
+ var newuser_pass = $("#newuser_password").val();
+ var newuser_pass_confirm = $("#newuser_password_confirm").val();
+ if (newuser_pass != newuser_pass_confirm) {
+ this.commError(SypStrings.userPasswordmatchError);
+ $("#newuser_password").focus().select();
+ return;
+ }
+
+ if (!newuser_pass) {
+ this.commError(SypStrings.emptyPasswordError);
+ $("#pass_new").focus().select();
+ return;
+ }
+
+ this.commError("");
+
+ AjaxMgr.add({
+ form: $("#newuser"),
+ oncomplete: OpenLayers.Function.bind(this.ajaxReply, this),
+ throbberid: "user_throbber",
+ onsend: function() {
+ // we need a timeout; otherwise those fields will not be submitted
+ window.setTimeout(function() {
+ // removes focus from #password before disabling it. Otherwise, opera
+ // prevents re-focusing it after re-enabling it.
+ $("#newuser_name, #newuser_password, #newuser_password_confirm").blur();
+ userMgr.disableForms();
+ }, 0);
+ }
+ });
+ },
+
+ ajaxReply: function (data) {
+ if (!data) {
+ // here, we need a timeout because onsend timeout sometimes has not been triggered yet
+ var self = this;
+ window.setTimeout(function() {
+ self.enableForms();
+ }, 0);
+ this.commError(SypStrings.ServerError);
+ return;
+ }
+
+ var xml = new OpenLayers.Format.XML().read(data);
+ if (!xml.documentElement) {
+ // here, we need a timeout because onsend timeout sometimes has not been triggered yet
+ var self = this;
+ window.setTimeout(function() {
+ self.enableForms();
+ }, 0);
+ this.commError(SypStrings.UnconsistentError);
+ return;
+ }
+
+ var needFormEnabling = true;
+ var focusEl = null;
+
+ switch (xml.documentElement.nodeName.toLowerCase()) {
+ case "error":
+ switch (xml.documentElement.getAttribute("reason")) {
+ case "unauthorized":
+ pwdMgr.reset();
+ $("#cookie_warning").show();
+ Admin.reset();
+ this.uninit();
+ break;
+ case "server":
+ this.commError(SypStrings.ServerError);
+ if (this._adduserDisplayed) {
+ focusEl = $("#newuser_name");
+ } else if (this._changepassDisplayed) {
+ focusEl = $("#pass_current");
+ }
+ break;
+ case "request":
+ this.commError(SypStrings.RequestError);
+ if (this._adduserDisplayed) {
+ focusEl = $("#newuser_name");
+ } else if (this._changepassDisplayed) {
+ focusEl = $("#pass_current");
+ }
+ break;
+ case "wrongpass":
+ this.commError(SypStrings.changePassBadPass);
+ focusEl = $("#pass_current");
+ break;
+ case "newuser_exists":
+ this.commError(SypStrings.newUserExistsError);
+ focusEl = $("#newuser_name");
+ break;
+ default:
+ this.commError(SypStrings.UnconsistentError);
+ if (this._adduserDisplayed) {
+ focusEl = $("#newuser_name");
+ } else if (this._changepassDisplayed) {
+ focusEl = $("#pass_current");
+ }
+ break;
+ }
+ break;
+ case "success":
+ switch (xml.documentElement.getAttribute("request")) {
+ case "newuser":
+ this.commSuccess(SypStrings.newUserSuccess);
+ needFormEnabling = false;
+ break;
+ case "changepass":
+ this.commSuccess(SypStrings.changePassSuccess);
+ needFormEnabling = false;
+ break;
+ default:
+ this.commError(SypStrings.UnconsistentError);
+ focusEl = $("newuser_name");
+ break;
+ }
+ break;
+ default:
+ this.commError(SypStrings.UnconsistentError);
+ focusEl = $("newuser_name");
+ break;
+ }
+
+ if (needFormEnabling) {
+ // here, we need a timeout because onsend timeout sometimes has not been triggered yet
+ var self = this;
+ window.setTimeout(function() {
+ self.enableForms();
+ if (focusEl) {
+ focusEl.select().focus();
+ }
+ }, 0);
+ } else {
+ if (focusEl) {
+ focusEl.focus().select();
+ }
+ }
+
+ },
+
+ commSuccess: function (message) {
+ $("#user_comm").text(message);
+ $("#user_comm").removeClass("error success").addClass("success");
+ },
+
+ commError: function (message) {
+ $("#user_comm").text(message);
+ $("#user_comm").removeClass("error success").addClass("error");
+ }
+}
+