function saveProfile(value, revert, settings, self) {
	var data = {
		cmd: (self.id == "email_address") ? "change_email" : "save_profile"
	};
	data[self.id] = value;

	$.ajax({
		type: "POST",
		url: "/ma/",
		data: data,
		success: function(data) {
			var val = value;

			if (self.id == "email_address") {
//				alert("'Please confirm your new email address' message here");
                                setErrorMessage("verify_email_address", myaccount_msgs["verify_email_address"]);
				val = revert;
			} else if ($(self).hasClass("jedit_dropdown")) {
				val = dropdown_data[self.id][value];
			}

			$("#" + self.id).html(val);
		},
		error: function (request) {
			if (request.status === 500) {
				//alert("system error");
			} else if (request.status === 501) {
				//alert("error: " + request.status + " (" + request.responseText + ")");
				var error_id = request.responseText.split(",")[0];
				setErrorMessage(error_id, myaccount_errors[error_id]);
			} else {
				//alert("unknown error");
			}
			$("#" + self.id).html(revert);
		}
	});
	return "<span class='saving'>Saving...</span>";
}

function savePassword() {
	$.ajax({
		type: "POST",
		url: "/ma/",
		data: {
			cmd: "change_password",
			old_password: $("#old_password").val(), 
			new_password1: $("#new_password1").val(),
			new_password2: $("#new_password2").val()
		},
		success: function(data) {
			var password_preview = $("#password_info").html();
			window.setTimeout(function() {
				$("#password_info").html(password_preview);
			}, 3 * 1000);
			$("#password_info").html("<span class='saving'>Saved new password</span>");
			onCancelChangePassword();
		},
		error: function (request) {
			if (request.status === 500) {
				//alert("system error");
			} else if (request.status === 501) {
				//alert("error: " + request.status + " (" + request.responseText + ")");
				var error_id = request.responseText.split(",")[0];
				setErrorMessage(error_id, myaccount_errors[error_id]);
				$("#" + {
					bad_old_password: "old_password",
					bad_new_password: "new_password1",
					new_passwords_not_match: "new_password2"
				}[error_id]).focus();
			} else {
				//alert("unknown error");
			}
		}
	});
	// TODO: somehow display saving progress
}

function savePasswordQuestion() {
	var password_question_new = {
		id: $("#password_question_id").val(),
		question: $("#password_question_id option:selected").text(),
		answer: $("#password_answer").val()
	};

	$.ajax({
		type: "POST",
		url: "/ma/",
		data: {
			cmd: "change_password_question",
			old_password: $("#old_password2").val(), 
			password_question_id: password_question_new.id,
			password_answer: password_question_new.answer
		},
		success: function(data) {
			password_question = password_question_new;
			window.setTimeout(function() {
				$("#question_info").html(password_question.question);
			}, 3 * 1000);
			$("#question_info").html("<span class='saving'>Saved new question</span>");
			onCancelChangePasswordQuestion();
		},
		error: function (request) {
			if (request.status === 500) {
				//alert("system error");
			} else if (request.status === 501) {
				//alert("error: " + request.status + " (" + request.responseText + ")");
				var error_id = request.responseText.split(",")[0];
				setErrorMessage(
					error_id == "bad_old_password" ? "bad_old_password_qa" : error_id,
					myaccount_errors[error_id]
				);
				$("#" + {
					bad_old_password: "old_password2",
					bad_password_question: "password_question",
					bad_password_answer: "password_answer"
				}[error_id]).focus();
			} else {
				//alert("unknown error");
			}
		}
	});
	// TODO: somehow display saving progress
}

function setErrorMessage(id, msg) {
	$("#myny-myaccount-error").attr("class", id || "");
	$("#myny-myaccount-error p").html(msg || "");

	if (msg) {

	    $("#myny-myaccount-error").show();

            window.setTimeout(function() {
                $nymag_j("#myny-myaccount-error").fadeOut(1000);
            }, 6500);

	} else {
		$("#myny-myaccount-error").hide();
	}
}

function fieldOnBlur(self) {
	$("#" + self.id + "_save").hide();
	$("#" + self.id + "_edit").show();
	setErrorMessage();
	return true;
}

function fieldOnEdit(self) {
	setErrorMessage();

	onCancelChangePassword();
	onCancelChangePasswordQuestion();

	$("#" + self.id + "_edit").hide();
	$("#" + self.id + "_save").show();

	// a work-around for IE focus bug
	window.setTimeout('$("#' + self.id + ' input").focus()', 100);

	return true;
}

function fieldOnSubmit(self) {
	return true;
}

function onEdit(id) {
	$("#" + id).click();
}

function onSave(id) {
	$("#" + id + " form").submit();
}

function onCancel(id) {
}

function onChangePassword() {
	onCancelChangePasswordQuestion();
	$(".password-change-closed").attr("class", "password-change-opened");
	$("#password_preview").hide();
	$("#password_edit").show();
	$("#old_password").focus();
}

function onChangePasswordQuestion() {
	onCancelChangePassword();
	$(".question-change-closed").attr("class", "question-change-opened");
	$("#question_preview").hide();
	$("#question_edit").show();
	$("#old_password2").focus();
}

function onCancelChangePassword() {
	setErrorMessage();
	$(["old_password", "new_password1", "new_password2"]).each(function(i, v) {
		$("#" + v).val("");
	});
	$(".password-change-opened").attr("class", "password-change-closed");
	$("#password_edit").hide();
	$("#password_preview").show();
}

function onCancelChangePasswordQuestion() {
/*
	setErrorMessage();

	$(".question-change-opened").attr("class", "question-change-closed");
	$("#question_edit").hide();
	$("#question_preview").show();

	$("#old_password2").val("");
	$("#password_question_id").attr("selectedIndex",
		$("#password_question_id option[value=" + password_question.id + "]").get(0).index);
	$("#password_answer").val(password_question.answer);
*/
}
