﻿function loadnotice(a)
{
	Ajax("post","/tips/operate.php?type=notice",showMsg);
	function showMsg(xmlHttp){
		if(xmlHttp.readyState==4){
            var root=xmlHttp.responseXML.documentElement;
			if(root.hasChildNodes)
			{
				var htmlStr="<div class=notice><ul>";
				var items=root.childNodes;
				for(var i=0;i<items.length;i++)
				{
					var title=items[i].getAttribute("title");
					var id=items[i].getAttribute("id");
					var color=items[i].getAttribute("color");
					htmlStr+="<li><a href=/news-"+id+".shtml target=_blank><font color="+color+">"+title+"</font></a></li>";
					htmlStr+="<li>&nbsp;&nbsp;&nbsp;&nbsp;</li>";
				}
				htmlStr+="</ul><div>";
				$(a).innerHTML=htmlStr;
			}
		}
	}
}

/*delete data*/
function delData(id,data,fun)
{
  	Ajax("post","/user/operate.php?type=delData&data="+data+"&id="+id+"",showMsg);
	function showMsg(xmlHttp){
		if(xmlHttp.readyState==4){
          //alert("删除成功！");
		  fun;
		}
	}
}

/* user info */
function login()
{
	var alertStr="";
	var user=$("user").value;
	var pwd=$("pwd").value;
	if(user=="") alertStr+="用户名不能为空!";
	if(pwd=="") alertStr+="密码不能为空!";
	if(alertStr!=""){
		$("loginMsg").className="loginErr";
		$("loginMsg").innerHTML=alertStr;
	}else{
		Ajax("post","/user/operate.php?type=login&user="+user+"&pwd="+pwd,showMsg);
	}
	function showMsg(xmlHttp)
	{
		$("loginMsg").className="loginMsg";
		$("loginMsg").innerHTML="<img src='/style/images/ico/loading.gif' align=absmiddle />登录中...";
		if(xmlHttp.readyState==4){
			$("loginMsg").innerHTML="";
			var re=xmlHttp.responseText;
			if(re=="1"){
				initLoginInfo("userInfoTable");
			}
		    else if(re=="2")
			{
				alert("您的帐户未审核，或者被管理员锁定！");
				}
			else{
				$("loginMsg").className="loginErr";
				$("loginMsg").innerHTML="用户名或密码错误!";
			}
		}
	}
}

function document.onkeydown()
{
	if(event.keyCode==13)
	{
		document.getElementById("btLogin").click();
		return false;
	}
}

function initLoginInfo(a)
{
	var user=getCookie("user");       
	var htmlStr="";
	if(user==null){
		htmlStr+='<table width="170" border="0" align="center" cellpadding="0" cellspacing="0">';
		htmlStr+='<tr>';
		htmlStr+='<td><div align="left"><img src="/style/images/lg1.gif" width="144" height="20" /></div></td>';
		htmlStr+='</tr>';
		htmlStr+='<tr>';
		htmlStr+='<td><input type="text"  class="newlogin" name="user" id="user" /></td>';
		htmlStr+='</tr>';
		htmlStr+='<tr>';
		htmlStr+='<td><div align="left"><img src="/style/images/lg2.gif" width="144" height="20" /></div></td>';
		htmlStr+='</tr>';
		htmlStr+='<tr>';
		htmlStr+='<td><input name="pwd" type="password" class="newlogin" id="pwd" value="" /><br>';
		htmlStr+='<div id="loginMsg"></div>';
		htmlStr+='</td>';
		htmlStr+='</tr>';
		htmlStr+='<tr>';
		htmlStr+='<td height="35"><input type="image" src="/style/images/login1.jpg" name="btLogin" id="btLogin" onclick="login();"/>&nbsp;';
		htmlStr+='<input type="image" src="/style/images/login2.jpg" name="button2" id="button2"  onclick="reg();" /></td>';
		htmlStr+='</tr>';
		htmlStr+='<tr>';
		htmlStr+='<td>忘记密码? | <a href="/tips/config.php?idx=str5">关于游戏规则</a></td>';
		htmlStr+='</tr>';
		htmlStr+='</table>';
	}
	else
	{
		getUserLoged(decodeURIComponent(user));
		htmlStr+='<div id=userLogedBar></div>';
		
	}
	$(a).innerHTML=htmlStr;
}

function reg()
{
	location.href="/tips/reg.html";
	}

function getUserLoged(user)
{
    Ajax("post","/data.php?type=getUserLoged&user="+user+"",showData);
	function showData(xmlHttp)
	{
		if(xmlHttp.readyState==4)
		{
		  var re=xmlHttp.responseText;
		  $('userLogedBar').innerHTML=re;
		}
	}	
    
}


function go(a)
{
	writeCookie("usertype",a);
	location.href="/user/";
}

function loginOut()
{
	if(confirm("确定注销吗?"))
	{
		deleteCookie("user","/");
		Clearcookie();
		initLoginInfo("userInfoTable");
	}
}

function loginOutGo()
{
	if(confirm("确定注销吗?"))
	{
		//删除用户数据
		deleteCookie("user","/");
		location.href="/tips/";
	}
}




function judgeUser()
{
	var user=getCookie("user");
	if(!user)
	{
		document.write("<font color=#cc0000>您还没有登录,3秒后返回首页...</font>");
		setTimeout("top.location.href='/index.php'",3000);
	}
}

function pay()
{
  window.open("/pay.html", "win"); 	
}

/* load newest tips/rewards */
function loadNewest(type,obj,callback)
{
	Ajax("post","/data.php?type="+type,showData);
	function showData(xmlHttp)
	{
		if(xmlHttp.readyState==4)
		{
			var root=xmlHttp.responseXML.documentElement;
			if(root.hasChildNodes)
			{
				var htmlStr="<ul>";
				var items=root.childNodes;
				for(var i=0;i<items.length;i++)
				{
					if(i%2==0){
						htmlStr+="<li class='newsestli1'>";
					}else{
						htmlStr+="<li>";
					}
					var userName=items[i].getAttribute("userName");
					htmlStr+="<div style='width:60px;float:left'><a target='_blank' href='/user/usershow.html?"+userName+"'>"+userName+"</a></div>";
					htmlStr+="<div style='width:140px;float:left'><a href='/tips'>"+cutStr(items[i].getAttribute("ta"),3)+" VS ";
					htmlStr+=cutStr(items[i].getAttribute("tb"),3)+"</a></div>";
					htmlStr+="</li>";
				}
				htmlStr+="</ul>";
				obj.innerHTML=htmlStr;
				if(callback){
					callback(obj);
				}
			}
		}else{
			obj.innerHTML="<img src='/style/images/ico/loading.gif' align=absmiddle />loading...";
		}
	}
}


function loadWinning(typeStr,obj,num)
{
	
	Ajax("post","/data.php?type=winning&typeStr="+typeStr+"&num="+num,showData);
	function showData(xmlHttp)
	{
		if(xmlHttp.readyState==4)
		{
			var root=xmlHttp.responseXML.documentElement;
			if(root.hasChildNodes)
			{	
				var htmlStr="<table border=0 class='winningTable' width='100%'>";
				var items=root.childNodes;
				for(var i=0;i<items.length;i++)
				{
					var userName=items[i].getAttribute("userName");
					var userId=items[i].getAttribute("userId");
					htmlStr+="<tr height='14' align='center'><td width='18%'><img src=/style/images/order"+(i+1)+".gif></td>";
					htmlStr+="<td width='41%' align='center'><a oncontextmenu='return false;' style='cursor:hand' onmousemove='moveBox(event);' onmouseover=showAdsearch(\""+userName+"\","+userId+",event); target='_blank' href='/"+userId+".shtml'>"+userName+"</a></td>";
					if(typeStr=="trustP"){
						htmlStr+="<td width='41%' align='center'  style='color:blue'>"+items[i].getAttribute("winning")+"</td>";
						}
					else
					{
						
						if(typeStr=="30d"){
							htmlStr+="<td width='41%' align='center' style='color:blue'>"+roundNum(parseFloat(items[i].getAttribute("winning")),3)+"%/<span style='font-size:10px; color:#006600;'>"+items[i].getAttribute("winning30dn")+"场</span></td>";
							}
						else if(typeStr=="15d"){
							htmlStr+="<td width='41%' align='center' style='color:blue'>"+roundNum(parseFloat(items[i].getAttribute("winning")),3)+"%/<span style='font-size:10px; color:#006600;'>"+items[i].getAttribute("winning15dn")+"场</span></td>";
							}
						else if(typeStr=="7d"){
							htmlStr+="<td width='41%' align='center' style='color:blue'>"+roundNum(parseFloat(items[i].getAttribute("winning")),3)+"%/<span style='font-size:10px; color:#006600;'>"+items[i].getAttribute("winning7dn")+"场</span></td>";
							}
						else
						{
							htmlStr+="<td width='41%' align='center' style='color:blue'>"+roundNum(parseFloat(items[i].getAttribute("winning")),3)+"%</td>";
						}
					}
					
					htmlStr+="</tr>";
				}
				htmlStr+="</table>";
				//alert(htmlStr);
				obj.innerHTML=htmlStr;
			}
		}else{
			obj.innerHTML="<img src='/style/images/ico/loading.gif' align=absmiddle />loading...";
		}	
	}
}

function roundNum(number,X){
// rounds number to X decimal places,default 2   
	X=(!X?2:X);
	var num=(Math.round(number*Math.pow(10,X))/Math.pow(10,X))*100;
	num=(num.toString()).substring(0,4);
	return num;
} 

//通用删除类
function dataDel(data,id)
{
	if(confirm("确定删除吗?"))
	{
		Ajax("post","/data.php?type=dataDel&data="+data+"&id="+id,showData);
		function showData(xmlHttp)
		{
			if(xmlHttp.readyState==4)
			{
			  var re=xmlHttp.responseText;
		      alert(re);
			  location.href="?";
			}
		}
	}
}

////////////////以下为定义用户弹出窗口
var HiddenTimer;//广告窗口关闭的定时器
function getMousePos(ev)//获取鼠标的位置
{
 if(ev.pageX || ev.pageY)//firefox中的位置
 {
  return {x:ev.pageX, y:ev.pageY};
 }
 return {//ie中的位置
  x:ev.clientX + document.documentElement.scrollLeft - document.documentElement.clientLeft,
  y:ev.clientY + document.documentElement.scrollTop  - document.documentElement.clientTop
 };
}

function matchAds(e)
{
  var UserBox=document.createElement("div");//广告显示窗体 
  var str="";
  UserBox.id="ShowBox";
  UserBox.setAttribute("id","UserBox");
  UserBox.setAttribute("name","BoxName");
  UserBox.style.position="absolute";
  str+='<div id="UserBox" name="BoxName">';
  str+='<div id="UserBoxC" name="UserBoxC" >loading...</div>';
  str+='</div>';
  UserBox.innerHTML=str;
  document.body.appendChild(UserBox);
  $('UserBox').style.display="none";
}


function showAdsearch(userName,userId,e)
{
	try{clearTimeout(HiddenTimer);}//关闭定时消失事件
    catch(e){}
	moveBox(e);//移动广告窗体
	var str="";
	str+='<div class="userbox"><ul><li onclick="boxfav(\''+userName+'\');">添加关注</li><li  onclick=usergo('+userId+');>历史战绩</li><li onclick="hiddenUserBox();"><img src="/style/images/delete.gif"></li></ul></div>';
	$('UserBoxC').innerHTML=str;
	$('UserBox').style.display="block";
}

function usergo(userId)
{
  window.open("/user/usershow.html?"+userId, "win"); 
}

function boxfav(fuser)
{
	if(getCookie("user")==null){alert("对不起，只有登录后才可以享用权限！");return false;};
	if(fuser==""){alert("请填入用户ID");return false;}
	if(fuser==getCookie("user")){alert("自己不用添加自己");return false;}
	url="/user/operate.php?type=adduser&fuser="+encodeURIComponent(fuser)+"";
	Ajax("post",url,showData);
	function showData(xmlHttp)
	{
		if(xmlHttp.readyState==4)
		{
			var re=xmlHttp.responseText;
		    alert(re);
		}
	}
}

//移动广告窗体
function moveBox(e)
{
	var BoxLeft=getMousePos(e).x;//当前鼠标的横坐标
	//如果广告窗体超出显示区域
	if(parseInt(document.documentElement.clientWidth)-BoxLeft<parseInt($('UserBox').style.width))
		{
		BoxLeft=(BoxLeft-parseInt($('UserBox').style.width))+"px";
		}
	else
		{
		BoxLeft=BoxLeft+"px";
		}
	$('UserBox').style.left=BoxLeft;//设置横坐标
	var BoxTop=getMousePos(e).y//当前鼠标的纵坐标
	if(parseInt(document.documentElement.clientHeight)-BoxTop<parseInt($('UserBox').style.height))
		{
		BoxTop=(BoxTop-parseInt($('UserBox').style.height)-55)+"px";
		}
	else
		{
		BoxTop=(BoxTop+5)+"px";
		}
	$('UserBox').style.top=BoxTop;//设置纵坐标
}
//鼠标移动事件
document.onmouseover=function(e)
{
  e = e || window.event;   
  var eSrc=e.target||e.srcElement;
  if(eSrc.name!="BoxName")
  {
     try{clearTimeout(HiddenTimer);} catch(e){}
     HiddenTimer=setTimeout("hiddenUserBox()",5000);
  }
  else
  {
     try{clearTimeout(HiddenTimer);} catch(e){}
  }
}
//隐藏广告窗体
function hiddenUserBox()
{
 try{clearTimeout(HiddenTimer);} 
 catch(e){}
 $('UserBox').style.display="none";
}
//定义用户弹出窗口结束



