var msg_freq = 0;
$(document).ready(
	function ()
	{
		 msg_freq == 59;
		 ajax_refreshMessage();
	     window.setInterval(function (){
	     	msg_freq ++;
	     	if(msg_freq == 60)
	     	{	
	     		msg_freq = 0;
	     		ajax_refreshMessage();
	     	}
	     },1000);
	}
);
var ajax_refreshMessage = function ()
{
	$.get("message.php",{'act':'c'},function(data){
		//alert(data.length);
		if(data == '')
			return ;
		//临时处理Memcache经常取不到问题，待修改
		if(data.length > 5)
			return ;
		
		var darr = data.split(':');
		var msg = darr[0];
		var msn = darr[1];
		if(msg != 0)
		{
			$('#plmsg').html("<img src='images/197.gif'/><a href='message.php?act=ls'>"+"新消息</a>("+msg+")");
		}
		else
		{
			$('#plmsg').html("<a href='message.php?act=ls'>消息</a>");
		}
		if(msn != 0)
		{
			$('#plmis').html("<img src='images/085.gif'/><a href='mission.php'>新任务</a>("+msn+")");
		}
		else
		{
			$('#plmis').html("<a href='mission.php'>任务</a>");
		}
	});
};
//全选

var selectAll = function()
{
	var tmp = $("#chb").attr('checked');
	
	if(tmp == '')
	{
		str = "checked";
	}
	else
	{
		str = '';
	}
	$(":checkbox[name='chb'][@checked]").attr('checked',str);
}

String.prototype.trim = function()
{
   return this.replace(/(^\s+)|\s+$/g,"");
}
 
var send_msg = function()
{
	var to = $("#to_who").val();
	var subj = $("#subject").val();
	var m_body = $("#m_body").val();
	var v_code = $("#v_code").val();
	if($("#to_who").val().length == 0)
	{
		 $("#to_w").html("<font color=red>俱乐部不能为空!</font>");
		 $("#to_w").show("slow");
		 return false;
	}
	else if($("#subject").val().trim().length == 0)
	{
		 $("#to_s").html("<font color=red>标题不能为空!</font>");
		 $("#to_s").show("slow");
		 return false;
	}
	else if(m_body.length == 0)
	{
		$("#to_c").html("<font color=red>内容不能为空!</font>");
		$("#to_c").show("slow");
		return false;
	}
	else if(v_code.length == 0)
	{
		$("#to_v").html("<font color=red>验证码不能为空!</font>");
		$("#to_v").show("slow");
		return false;
	}
	else
	{
		//alert(to + subj + m_body +v_code);
		return true;
	}
}
//刷新验证码
var refreshCode = function()
{
	$("#rndimg").attr("src","valicode3.php?p=msg&m=" + Math.random());
}
//删除信息
var delSpecilRecord = function()
{
	var chkd = $(":checkbox[name='chb'][checked]");
	var res = "";
	var tmp = "&id[]=";
	var count = 0;
	chkd.each(function(){ 
		count++;
		var isCheck = this.value; 
		res +=tmp+isCheck; 
	}); 
	if(count == 0)
		return;
	var url = "message.php?act=rm"+res;
	if(confirm("你是否确认要删除"+count+"条信息?") == true)
	{
		window.location.replace(url);
	}
}
//删除草稿
var delSpecilDraft = function ()
{
	var chkd = $(":checkbox[name='chb'][checked]");
	var res = "";
	var tmp = "&id[]=";
	var count = 0;
	chkd.each(function(){ 
		count++;
		var isCheck = this.value; 
		res +=tmp+isCheck; 
	}); 
	if(count == 0)
		return;
	var url = "message.php?act=rd"+res;
	if(confirm("你是否确认要删除"+count+"条信息?") == true)
	{
		window.location.replace(url);
	}
}
var clearRecord = function ()
{
	var url = "message.php?act=clr";
	if(confirm("你是否确认要清空收件箱？") == true)
	{
		window.location.replace(url);
	}
}
var clearDraft = function()
{
	var url= "message.php?act=clo";
	if(confirm("你是否确认要清空收件箱？") == true)
	{
		window.location.replace(url);
	}
}
var sbmtMSG = function ()
{
	if (send_msg())
		$('#m_f').submit();
}