/**
 * login page
 */
$("#verifyCode").keydown(function(event) {
	if (event.which == 13) {
		onLoginAction();
	}
});

function onLoginAction() {
	var username = $("#username").val();
	var password = $("#password").val();
	var verifyCode = $("#verifyCode").val();

	if (isEmpty(username)) {
		$("#tipView").html("请输入账号");
		$("#tipDialog").modal('show');
	} else if (isEmpty(password)) {
		$("#tipView").html("请输入密码");
		$("#tipDialog").modal('show');
	} else if (isEmpty(verifyCode)) {
		$("#tipView").html("请输入验证码");
		$("#tipDialog").modal('show');
	} else {
		var encryPasswd = sha256_digest(password);
		login(username, encryPasswd, verifyCode);
	}

}

function login(username, password, verifyCode) {
	var params = "username=" + username + "&password=" + password
			+ "&verifyCode=" + verifyCode;
	$.post(CONTEXT_PATH + "/sysUser/login", params, function(response, status) {
		console.log(JSON.stringify(response));
		try {
			if (response.code == HTTP_OK) {
				location.href = CONTEXT_PATH + "/";
			} else {
				$("#tipView").html(response.msg);
				$("#tipDialog").modal('show');

				// 刷新验证码
				var codeImage = document.getElementById('codeImage');
				codeImage.click();
			}
		} catch (e) {
		}
	});

}