// 用户
/*
 依赖:ajax.js,cookie.js
*/
var loginok=null;
var logoutok=null;
/*
 用户登录验证
 contextpath:request的上下文路径
 username:用户名
 password:密码
 callback:登录成功后回调函数
*/
function login(contextpath,username,password,callback) {
	document.getElementById("error_username").innerHTML="";
	document.getElementById("error_password").innerHTML="";
	ajaxget(contextpath+"/user/login.xml?username="+username+"&password="+password,function(xmlHttp) {
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				var dom=xmlHttp.responseXML;
				var status=dom.getElementsByTagName("status")[0].firstChild.nodeValue;
				if (status=="1") {
					document.getElementById("error_username").innerHTML=dom.getElementsByTagName("error")[0].firstChild.nodeValue;
				} else if(status=="2") {
					document.getElementById("error_password").innerHTML=dom.getElementsByTagName("error")[0].firstChild.nodeValue;
				} else {
					setCookie("__user",dom.getElementsByTagName("userid")[0].firstChild.nodeValue,365*24*60*60*1000);
					turnoff();
					if (callback) {
					callback(dom);
					}
				}
			}
		}
	});
	return false;
}

/*
 用户退出
 callback:用户退出后的回调函数
*/
function logout(callback) {
  delCookie("__user");
  if (callback) {
    callback();
  }
}

/*
 * 打开用户登录对话框时调用
 */
function loginopen() {
	document.loginform.username.focus();
}

/*
 * 用户名和密码框按下回车
 */
function loginenter(event,contextpath,username,password,callback) {
  event=event?event:window.event;
	if(event.keyCode!=13) return;
	login(contextpath,username,password,callback);
}