/*文件名：userLoginStatus.js 
文件描述：判断当前页面用户（session）是否已经登录
创建时间：2010-9-26
*/
//打log
function userLoger(tokenId)
{
	var param={'url':document.URL, 'log_token_id':tokenId};
	ajax(bugerUrl.ajax.userLoger, param, 4000, true);
}
//get the logininfo 
function GetLoginedInfo(f)
{
	var ret = null;
	$.ajax({
		url:bugerUrl.ajax.userLoginedInfo,
		type:"POST",
		async:false,
		timeout:2000,
		success:function(data,status,xml)
		{
			data=eval("("+data+")");
			if(data.ret == true)
			{
				ret = data;
				try{
				f(ret);
				}catch(E){}
			}
		},
		error:function(xmlReq,status,error)
		{	
			alert("timeout!");
		}});
	return ret;
}
var loginInfo = null;
//设置登录态tip条  当flag为ture时，跳转使用target_blank 且提示“欢迎字样信息”，用于首页中
function SetLoginedStatus(filter,flag)
{
	loginInfo = GetLoginedInfo();
	if(loginInfo)
	{	
	    feed_cnt = loginInfo.feed_cnt;
	    feedStr  = feed_cnt<=0 ? "["+feed_cnt+"]":"<font color=red>["+feed_cnt+"]</font>";
	    titleStr = feed_cnt<=0 ? '个人中心：个人动态、修改个人资料等！' :"您有 "+feed_cnt+" 条未读动态，点击查看个人动态！";
	    helloStr = !flag ? "欢迎您：":"";
		var str = loginInfo.bugerid!="0" ? "<a href='home.jsp?target=mybuger' title='设置及管理我的布格的所有信息'>管理布格</a> <a href='buger.jsp?bugerid="+loginInfo.bugerid+"' title='进入自己的布格'>go布格</a> ":
						"<a href='home.jsp?target=newbg' title='可以简单快速创建属于自己的布格空间！'><font color=red>创建我的布格</font></a>";
		$(filter).html(helloStr+"<a href='home.jsp' title=\""+titleStr+"\">"+
				loginInfo.unick+"("+loginInfo.uid+")<b>"+feedStr+"</b></a>"+" "+str+"&nbsp;积分："+
			loginInfo.point+"&nbsp;<a href='' onclick='onUserLogout(location.href,true);return false;' title='安全退出布格'>退出</a>&nbsp;");
	}
	else
	{/*
		$(filter).html("<input id=user style='width:100px;'>&nbsp;"+
					"<input style='width:100px' type='password' id=psw>&nbsp;<input style='width:100px' id=code> "+
						"<img style='height:25px;' src='servlet/ValidateCodeServlet.images' onclick='onMycodeValidate(this)'>");
	*/}
	//userLoger(Math.random());
}
function onMycodeValidate(img)
{
	img.src= 'servlet/ValidateCodeServlet.images?xx='+Math.random(); 
}

/*此函数以ajax方式登出用户，并且成功之后跳转新的url
*/
function onUserLogout(url,bAlert)
{
	if(bAlert && !confirm("确定要退出？"))
		return ;
	bAlert = bAlert==undefined ? true : bAlert;//如果未定义此参数，默认为开启提示
	//使用ajax方式进行用户名是否已经存在的请求
	$.ajax({
	url:bugerUrl.ajax.logout,
	type:"POST",
	async:false,
	timeout:2000,
	success:function(data,status,xml)
	{
		data=eval("("+data+")");
		if(data.ret == true )
		{
			if(bAlert) alert("登出成功！");
			location.assign(url);
		}
		else if(bAlert) 
			alert("登出失败，可能的原因为:\n"+data.msg);
	},
	error:function(xmlReq,status,error)
	{	
		//alert("timeout!");
	}});	
}
//注册用户
function registUser(uid,umail,unick,psw,sex,birthday,safequestion, ans,code)
{
	var param = {'uid':uid,'umail':umail,'unick':unick,'psw':hex_md5(psw),'sex':sex,'birthday':birthday,'safequestion':safequestion,
					'ans':ans,'code':code,'textcode':'测试内容编码'};	
	return ajax(bugerUrl.ajax.registUser, param,8000,false);
}

//myinfo中用到的
//获得用户信息---由session得知是否已经登录
function getUserBasicInfo()
{
	return ajax("../"+bugerUrl.ajax.getUserBasicInfo, null, 4000,false);
}
//修改用户信息
function doModUserInfo(unick,sex,birthday,mail,phone)
{
	if(unick==null || sex==null || birthday==null || mail==null || phone==null)
		return null;
	var param = {'unick':unick,'sex':sex,'birthday':birthday,'mail':mail,
				'phone':phone,'textcode':'测试内容编码','kind':'basicInfo'};
	return ajax('../'+bugerUrl.ajax.modUserBasicInfo,param,4000,false);
}
//修改用户密码
function doModUserPsw(oldPsw,newPsw)
{
	if(oldPsw == null || newPsw==null)
		return null;
	var param = {'oldPsw':oldPsw,'psw':newPsw,'kind':'psw'};
	return ajax('../'+bugerUrl.ajax.modUserBasicInfo,param,4000,false);
}
//修改用户登录名
function doModLoginName(uname,umail)
{
	if(uname == null || umail==null)
		return null;
	var param = {'uname':uname,'umail':umail,'kind':'loginName'};
	return ajax('../'+bugerUrl.ajax.modUserBasicInfo,param,4000,false);
}

