$(function() {
	$("#account").focus();
	var O_LogBtn = $("#O_LogBtn");
	var O_CookieDate = $("#cookieDate");
	var O_Account = $("#account");
	var O_Psd = $("#password");
	var cookieName = 'loginInfo';
	if($.cookie(cookieName)){
		O_Account.val($.cookie(cookieName).split('-')[0]);
		O_Psd.val($.cookie(cookieName).split('-')[1]);
		O_CookieDate.attr("checked", true);
	}
	$("body").bind('keyup', function(event) {
		if(event.keyCode == "13") {
			O_LogBtn.focus();
			login();
		}
	});
	$("#authImg").click(function() {
		$(this).attr("src", basePath + "?m=tool.valid&token=" + Math.round(Math.random() * 10000));
	});
	O_LogBtn.click(function() {
		login();
	});
}); 
function login() {
	var O_Loading = $("#O_Loading");
	var O_LogBtn = $("#O_LogBtn");
	
	var O_Account = $("#account");
	var O_Password = $("#password");
	var O_Valid = $("#valid");
	var O_CookieDate = $("#cookieDate");

	var v_account = $.trim(O_Account.val()).replace(/ /g, '').replace(/'/g, '');
    var v_password = $.trim(O_Password.val()).replace(/ /g, '').replace(/'/g, '');
	var v_valid = O_Valid.val();
	var v_isSetCookieDate = O_CookieDate.attr("checked") ? "1" : "0";
	
	alert(v_valid);
	
	if(v_account == "") {
		alert("用户名不能为空！");
		O_Account.val("");
		O_Account.focus();
	} else if(v_password == "") {
		alert("密码不能为空！");
		O_Password.val("");
		O_Password.focus();
	} else if(v_valid == "") {
		alert("验证码不能为空！");
		O_Valid.focus();
	} else {
		O_Loading.html("正在验证...");
		$.post(basePath + "?m=admin/login.check",{ account:v_account, password:v_password, valid:v_valid, isSetCookieDate:v_isSetCookieDate }, function(data) {
			O_Loading.html("");
			if(data[0] == "success") {
				var cookieName = 'loginInfo';
				if(O_CookieDate.attr("checked")) { $.cookie(cookieName, data[2], {path:'/', expires:365}); }
				else { $.cookie(cookieName, data[2], {path:'/', expires:-1}); }
				window.location.href = basePath + "?m=admin";
			}
			if(data[0] == "error") {
				alert(data[1]);
				if(data[2] == "valid is null") { O_Valid.focus(); }
				else if(data[2] == "valid error") { O_Valid.focus(); }
				else if(data[2] == "account is null") { O_Account.val("");O_Account.focus(); }
				else if(data[2] == "password is null") { O_Password.val("");O_Password.focus(); }
				else if(data[2] == "admin is not exists") { O_Account.focus(); }
				else if(data[2] == "admin is stopped") { O_Account.focus(); }
				else if(data[2] == "password error") { O_Password.val("");O_Password.focus(); }
			}
		}, "json");
	}
}