var CommentsPageSize=10;
var TempData;
function LoadComment(Page,ID,T)
{
	if($("#commentstext").length==0){alert('评论显示容器“commentstext”丢失！');return;}
	$("#commentstext").html('评论载入中...');
	if($('#commentspage').length==0)$("#commentstext").after('<div id="commentspage" class="digg"></div>');
	$.getJSON('ajax/commentsjson.asp?id='+ID+'&s='+CommentsPageSize+'&p='+Page+'&t='+T+'&d='+Math.random(),function(data) {
	  $("#commentstext").empty();
	  $("#msgcounts").text(data.counts);
	  if(data.comments.length==0){ $("#commentstext").html('暂无评论。');return;}
	  $.each(data.comments,function(i,item){
			$("#commentstext").append('<ul><li class="t1"><b>'+item.u_username+'</b> 发表于 <span>'+item.c_date+'</span> </li><li class="t2">'+item.c_text+'</li></ul>');});
		var Pages1= 10;
		if(Page > data.pagecount) Page = data.pagecount;
		var StartPage = Page-Divisible(Pages1,2);
		if(StartPage<1)StartPage=1;
		StopPage = StartPage+Pages1-1;
		if(data.pagecount<=StopPage)StopPage = data.pagecount;
		if(StopPage-Pages1<StartPage)StartPage=StopPage-Pages1;
		if(StartPage<1)StartPage=1;
		var Str='<span class="no">'+Page+'/'+data.pagecount+'页 '+data.counts+'条</span> ';
		Str += Page>1?' <a href="javascript:;" onclick="LoadComment(1);">首页</a> <a href="javascript:;" onclick="LoadComment('+ (Page-1) +');">上一页</a> ':' <a href="javascript:;">首页</a> <a href="javascript:;">上一页</a> ';
		for(var i=StartPage;i<=StopPage;i++)
		{
			Str += Page==i ? ' <span class="current"><strong>'+i+'</strong></span> ':' <a href="javascript:;" onclick="LoadComment('+i+');">'+i+'</a> ';
		}
		Str += Page<data.pagecount ?' <a href="javascript:;" onclick="LoadComment('+ (Page+1) +');">下一页</a> <a href="javascript:;" onclick="LoadComment('+ data.pagecount +');">尾页</a>':' <a href="javascript:;">下一页</a> <a href="javascript:;">尾页</a> ';
		$("#commentspage").html(Str);
	  	$("#msgcounts").text(data.counts);
	});
}
function Divisible(exp1,exp2){	var rslt = Math.round(exp1)/Math.round(exp2);return rslt >= 0 ? Math.floor(rslt):Math.ceil(rslt);} 

function Login()
{
	if($('#LoginWindow').length==0)$('<IFRAME id="LoginWindow" src="" frameborder="0" scrolling="no" ></IFRAME>').appendTo("body");
	if($("#LoginWindow").attr("src")=="")$("#LoginWindow").attr("src","alertlogin.asp");
	$("#LoginWindow").load(function(){IFrameReSize("LoginWindow");IFrameReSizeWidth("LoginWindow");});
	ShowWindow('messWindow','LoginWindow');
}		
function AlertLoginReturn(S)
{
	if(S==1)
	{
		$('#loginmsg').hide();$('#CommentsForm').show();HideWindow();
		if(TempData)InfoFavorites(TempData);
	}
}
function InfoFavorites(ID)
{
	$.ajax({
	   type:"get",
	   cache:false,
	   url: "Ajax/InfoFavorites.asp?ID="+ID,
	   success:function(msg){
		 if(msg=='0')
		 {
			 TempData=ID;Login();
		 }else if(msg=='1'){
			alert('恭喜您，收藏成功！');
		 }else{
			alert('您已收藏,请不要重复操作！');
		 }
	   },error:function(XMLHttpRequest, textStatus, errorThrown){document.write(XMLHttpRequest.responseText);}
	}); 
}
function PostComments(ID,A)
{
	var T=$('#text',$('#CommentsForm'));
	var Tv=$.trim(T.val());
	T.val(Tv);
	if(Tv==''){alert('请填写内容!');T.focus();return false}
	if(Tv.length<2 || Tv.length>100){alert('内容请保持在2~100个字以内!');T.focus();return false}
	var P=$('#proof',$('#CommentsForm'));
	if(P.val()==''){alert('请填写验证码!');P.focus();return false}
	$.get('Ajax/CheckCode.asp?proof='+escape(P.val()), function(data)
		{
		if(data=='false'){get_Code('','CommentsForm');alert('验证码错误!');P.focus();return false;}
		$.ajax({
		   type: "POST",
		   url: "CommentsSave.asp?ID="+ID+'&T='+A,
		   data:'text='+escape(Tv)+'&proof='+escape(P.val()),
		   success:function(msg){
			 alert(msg);
			 LoadComment(1,ID,A);
			 T.val('');P.val('');get_Code('','CommentsForm');
		   },
		   error:function (XMLHttpRequest, textStatus, errorThrown) {
			   alert('提交错误请与管理员联系!');
			} 
		}); 
	});
	
}

function PostReport(ID){
	var T=$('#text',$('#ReportForm'));
	var Tv=$.trim(T.val());
	T.val(Tv);
	if(Tv==''){alert('请填写举报原因!');T.focus();return false}
	if(Tv.length<2 || Tv.length>100){alert('举报原因请保持在2~100个字以内!');T.focus();return false}
	var P=$('#proof',$('#ReportForm'));
	if(P.val()==''){alert('请填写验证码!');P.focus();return false}
	$.get('Ajax/CheckCode.asp?proof='+escape(P.val()), function(data){
		if(data=='false'){get_Code('','ReportForm');alert('验证码错误!');P.focus();return false;}
		$.ajax({
		   type: "POST",
		   url: 'ReportSave.asp?id='+ID,
		   data:'text='+escape(Tv)+'&proof='+escape(P.val()),
		   success:function(msg){
			 alert(unescape(msg));
			 T.val('');P.val('');get_Code('','ReportForm');HideWindow();
		   },
		   error:function (XMLHttpRequest, textStatus, errorThrown) {
			   alert('提交错误请与管理员联系!');
			} 
		}); 
	});
}
function PostMessage(ID){
	var T=$('#text',$('#MessageForm'));
	var Tv=$.trim(T.val());
	var Title=$('#title',$('#MessageForm'));
	var Titlev=$.trim(Title.val());
	T.val(Tv);
	Title.val(Titlev);
	if(Titlev==''){alert('请填写标题!');Title.focus();return false}
	if(Tv==''){alert('请填写留言内容!');T.focus();return false}
	if(Tv.length<2 || Tv.length>100){alert('留言内容请保持在2~100个字以内!');T.focus();return false}
	var P=$('#proof',$('#MessageForm'));
	if(P.val()==''){alert('请填写验证码!');P.focus();return false}
	$.get('Ajax/CheckCode.asp?proof='+escape(P.val()), function(data){
		if(data=='false'){get_Code('','MessageForm');alert('验证码错误!');P.focus();return false;}
		$.ajax({
		   type: "POST",
		   url: 'MessageSave.asp?id='+ID,
		   data:'title='+escape(Titlev)+'&text='+escape(Tv)+'&proof='+escape(P.val()),
		   success:function(msg){
			 alert(unescape(msg));
			 Title.val('');T.val('');P.val('');get_Code('','MessageForm');HideWindow();
		   },
		   error:function (XMLHttpRequest, textStatus, errorThrown) {
			   alert('提交错误请与管理员联系!');
			} 
		}); 
	});
}
$(function(){if($('#messWindow').length==0)$('<div id="messWindow"></div>').appendTo("body"); });

function GetIpSource(IP)
{
	$.ajax({
	   type:"get",
	   url: "Ajax/IpSource.asp?IP="+IP,
	   success:function(msg){
		   $('#ipsource').html(msg);
	   },error:function (XMLHttpRequest, textStatus, errorThrown) {
			   alert(XMLHttpRequest.responseText);
			} 
	});
}
