var logonUrl='http://account.gamesbb.com.cn/ajaxLogin.do';
var cookieUrl = 'http://account.gamesbb.com.cn/ajaxGetMessage.do';
var formid = '';
function showLogonForm(formid){
	var formStr = "";
	if(formid=='homeLogin') {
		formStr += "    <h1>用户登录<\/h1>";
    formStr += "	<div id=\"stat\" class=\"error\"><\/div>";
    formStr += "    <ul class=\"login_1\">";
		formStr += "    <li>用 户：<input id=\"log_name\" type=\"text\" \/><\/li>";
		formStr += "    <li>密 码：<input id=\"log_pass\" type=\"password\" onfocus=\"this.value=\'\';\" onkeydown=\"ctlent(event)\" \/><\/li>";
		formStr += "    <li class=\"loginBtn2\"><a href=\"javascript:logon();\">登 陆<\/a><\/li>";
		formStr += "    <li class=\"loginBtn1\"><a href=\"http:\/\/account.gamesbb.com.cn\/forgetpassword.jsp\" target=\"_blank\">忘记密码<\/a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"http:\/\/account.gamesbb.com.cn\/register.jsp\" target=\"_blank\">注册<\/a><\/li>";
		formStr += "    <\/ul>";
		formStr += "      ";
		formStr += "    <div id=\"imglist\">";
		formStr += "    <a href=\"http:\/\/pay.gamesbb.com.cn\/changecard.jsp\" target=\"_blank\"><img src=\"/guide/common\/tu\/game\/btn_04.gif\" alt=\"大型网游充值\" border=\"0\" \/><\/a>";
		formStr += "    <a href=\"javascript:StartRealMhgame();\"><img src=\"/guide/common\/tu\/game\/btn_05.gif\" alt=\"开始游戏\" border=\"0\" \/><\/a>";
		formStr += "    <a href=\"http:\/\/pay.gamesbb.com.cn\" target=\"_blank\"><img src=\"/guide/common\/tu\/game\/btn_06.gif\" alt=\"购卡充值\" border=\"0\" \/><\/a>  ";
		formStr += "    <\/div> ";
		formStr += "     ";
		//formStr += "    <div id=\"ad\"><img src=\"/guide/common\/tu\/game\/loginad.gif\" \/><\/div>";
		//formStr += "    ";
        //formStr += "		<iframe marginwidth=\"0\" marginheight=\"0\" src=\"http:\/\/www.gamesbb.com.cn\/game\/cast_leftlogon.html\" frameborder=\"0\"  scrolling=\"no\" width=\"152\" height=\"215\"></iframe>";
		formStr += "    <div class=\"bottom\"><\/div>";
		return formStr;
	}
	else if(formid=='topLogin') {
		formStr += "<a href=\"http:\/\/www.gamesbb.com.cn\/service\/question.shtml\" target=\"_blank\">帮助中心<\/a><a href=\"http:\/\/account.gamesbb.com.cn\/register.jsp\" target=\"_blank\">注册<\/a>";
		return formStr;
	}
}

function ctlent(evt){
         var k = window.event?evt.keyCode:evt.which;
	if(k==13){
		logon();
	}
}

function getRemote(url,formid){
	XSSRequest.request(formid,url,
		function(responseObj){
			setForm(responseObj,formid)}
		,30,false);
}
function changeStatForm(msg,disabledflag){
	var fnname=document.getElementById('log_name');
	var fnpass=document.getElementById('log_pass');
	fnname.disabled=disabledflag;
	fnpass.disabled=disabledflag;
	setStat(msg);
}

function logon(){
	var fnname=document.getElementById('log_name');
	var fnpass=document.getElementById('log_pass');
	url = logonUrl + '?accountKey='+fnname.value+'&accountToken='+fnpass.value;
	if(fnname.value=='' || fnpass.value==''){
		setStat('请填写完整');
		return;
	}
		changeStatForm('处理中 . . .',true);
		getRemote(url,'homeLogin');
		
}

function logout(){
	deleteCookie('CNREAL','/','.gamesbb.com.cn') ;
	deleteCookie('USERINFO','/','.gamesbb.com.cn') ;
	refreshForm('homeLogin',showLogonForm('homeLogin'));
	//refreshForm('topLogin',showLogonForm('topLogin'));
         if( typeof(cb_logout)=='function'){
           cb_logout();
         }
}

function setForm(responseObj,formid){
	var result=responseObj.result;
	var message=responseObj.message;
	if(result=="false"){
                  logout();
                  setStat(message);
	}else{
		cookieString=responseObj.cookie;
		setCookie('CNREAL', cookieString, 0, '/', '.gamesbb.com.cn',false,false);
		nickName = responseObj.nickName;
		userType = responseObj.userType;
		rnMoney = responseObj.rnMoney;
		gameCoin = responseObj.gameCoin;
		point = responseObj.point;
                  headId = responseObj.headId;
		var jsonstring = '{"nickName":"'+nickName+'", "userType":"'+userType+'", "rnMoney":"'+rnMoney+'", "gameCoin":"'+gameCoin+'", "point":"'+point+'", "headId":"'+headId+'"}';
		setCookie('USERINFO', jsonstring, 0, '/', '.gamesbb.com.cn', false, false);
		refreshForm('homeLogin',showUserInfo(getRealLogonName(),jsonstring.parseJSON(),'homeLogin'));
		//refreshForm('topLogin',showUserInfo(getRealLogonName(),jsonstring.parseJSON(),'topLogin'));
         if( typeof(cb_logon)=='function'){
           cb_logon();
         }
	}
}

function showUserInfo(cn,userinfo,formid){
	var html='';
	if(formid=='homeLogin') {
		html += "		<h1 class=\"member_center\">用户中心<\/h1>";
		html += "		<div id=\"ad2\"><img src=\"http:\/\/www.gamesbb.com.cn\/images\/head\/"+userinfo.headId+"\/default.gif\" \/><\/div>";
		html += "		<ul class=\"userinfo\">";
		html += "		<li><b>用户：<\/b><input style=\"background:none;border:none;color:#1D4B84;\" size=12 value=\""+cn+"\" title=\""+cn+"\"><\/li>";
		html += "		<li><b>等级：<\/b>"+userinfo.userType+"<\/li>";
		html += "		<li><em>[<a href=\"http:\/\/pay.gamesbb.com.cn\" target=\"_blank\">充值<\/a>]<\/em><b>R币：<\/b>" + userinfo.rnMoney + " <\/li>";
		html += "		<li><em>[<a href=\"http:\/\/pay.gamesbb.com.cn\/intochangegcoin.do\" target=\"_blank\">兑换<\/a>]<\/em><b>游戏币：<\/b>" + userinfo.gameCoin + "<\/li>";
		html += "		<li><b>RealPoints：<\/b>"+userinfo.point+"<\/li>";
		html += "		<\/ul>";
		html += "		<div id=\"imglist\">";
		html += "		<a href=\"http:\/\/rshow.gamesbb.com.cn\" target=_blank><img src=\"/guide/common\/tu\/game\/userbtn_01.gif\" alt=\"更换头像\" border=\"0\" \/><\/a>";
		html += "		<a href=\"http://bbs.gamesbb.com.cn/user_message.php\" target=_blank><img src=\"/guide/common\/tu\/game\/userbtn_02.gif\" alt=\"站内消息\" border=\"0\" \/><\/a>";
		html += "		<a href=\"http:\/\/account.gamesbb.com.cn\/baseMessageP.do\"><img src=\"/guide/common\/tu\/game\/userbtn_03.gif\" alt=\"修改资料\" border=\"0\" \/><\/a>";
		html += "		<a href=\"javascript:logout();\"><img src=\"/guide/common\/tu\/game\/userbtn_04.gif\" alt=\"退出登录\" border=\"0\" \/><\/a> <\/div>		";
		html += "		";
		html += "		<div class=\"bottom\"><\/div>";
		return html;
	}
	else if(formid=='topLogin') {
		html += "<p>欢迎用户<input style=\"background:none;border:none;color:#FFF;\" size=12 value=\""+cn+"\" title=\""+cn+"\">登录Real游戏世界！</p>";
		html += "<a href=\"http:\/\/www.gamesbb.com.cn\/service\/question.shtml\" target=\"_blank\">帮助中心<\/a><a href=\"javascript:logout()\">退出<\/a><a href=\"http:\/\/pay.gamesbb.com.cn\">立即充值<\/a><a href=\"http:\/\/account.gamesbb.com.cn/baseMessageP.do\">修改资料<\/a>";
		return html;
	}
}

function beginForm(formid){
	var realcn='';
	realcn = getRealLogonName();
	if(realcn!='') {
		getRemote(cookieUrl,formid);
	}else{
		refreshForm(formid,showLogonForm(formid));
	}
}
         
function refreshForm(formid,formstring){
	document.getElementById(formid).innerHTML = formstring;
}

function setStat(msg){
	var statFn=document.getElementById('stat');
         statFn.style.color='#FF0000';
	if(statFn!=undefined){
		statFn.innerHTML=msg;
	}
}


function getRealUserInfo(){
	var cookieField="USERINFO";
	var cookieString=getCookie(cookieField);
	if(cookieString==undefined) {
		return '';
	}
	return cookieString;

}