function $(objID) {return document.getElementById(objID)};
function $F(objID) {return document.getElementById(objID).value};
function $$(node){return document.getElementsByTagName(node)};
function AJAX(){
	this.xmlhttp=function (){
		var Version = ["MSXML2.XMLHTTP","Microsoft.XMLHTTP"];
		try{ for(var i=0;i<Version.length;i++)return new ActiveXObject(Version[i]);}
		catch(e){try{return new XMLHttpRequest();}catch(e){return null;}}
	}
	var _p={asynchronous:true,method:"GET",parameters:"",mime:"text/html;charset=GB2312"};
	for(var key in arguments[0]){ _p[key]=arguments[0][key]; }
	var _x=this.xmlhttp();
	if (_x!=null){
		var _url=_p["url"];
		if (_p["parameters"]==""||_p["method"].toUpperCase()=="POST") _url=_url;
		else _url+="?"+_p["parameters"];
		_url+=(_url.indexOf("?") >= 0)?"&nowtime="+new Date().getTime():"?nowtime=" + new Date().getTime();
		_x.open(_p["method"],_url,_p["asynchronous"]);
		if(_x.overrideMimeType)_x.overrideMimeType(_p["mime"]);
		_x.onreadystatechange=function (){
			if(_x.readyState==4){
				if(_x.status==200) {  
					if($("loading")) $("loading").innerHTML="";
					_p["onComplete"]?_p["onComplete"](_x):"";
				}else _p["onError"]?_p["onError"](_x):"";
			}else if($("loading")) $("loading").innerHTML=(_p["onLoading"]?_p["onLoading"]:"");
		};
		if(_p["method"].toUpperCase()=="POST")_x.setRequestHeader("Content-Type","application/x-www-form-urlencoded");	
		_x.send(_p["method"].toUpperCase()=="POST"?_p["parameters"]:null);	
	}
}

function collapse(objName){
	obj = $(objName)
	if (obj.style.display == 'none') {
		obj.style.display='block'; 
	} else {
		obj.style.display='none';
	}
}

function tip(event,thisself,ip){
	this.hide_suggest=function(){if($("suggest")!=null)document.body.removeChild($("suggest"));}
	if(!document.all){
		window.document.addEventListener("click",this.hide_suggest,false);
		thisself.addEventListener("mouseout",this.hide_suggest,false)
	}else{
		window.document.attachEvent("onclick",this.hide_suggest);
		thisself.attachEvent("onmouseout",this.hide_suggest)
	}
	new AJAX({
	    url:"INC/Book.ASP",
		parameters:"act=ipaddress&ip="+ip,
		onComplete:suggestbody
	});
	evt=event?event:window.event
	var x=evt.clientX;
	var y=evt.clientY;
	function suggestbody(req){
		if(req.responseText!=""){
			if($("suggest")){document.body.removeChild($("suggest"));}
			var suggestdiv=document.createElement("DIV");
			suggestdiv.setAttribute("id","suggest");
			suggestdiv.style.left = x + document.body.scrollLeft + "px";
			suggestdiv.style.top =  y + document.body.scrollTop + "px";
			suggestdiv.style.width ="250px";
			suggestdiv.style.height="40px";
			suggestdiv.style.zIndex='15';
			suggestdiv.style.position='absolute';
			suggestdiv.style.textalign="left";
			document.body.appendChild(suggestdiv);
			var result=req.responseText;
			$("suggest").innerHTML="<div style='border:1px solid #23A3EC;padding:5px;background:#E5F2F8'>"+result+"</div>";
		}
	}
}

function add(){
	if ($F('username')==""||$('username').value.length>12){
       showerr("昵称不能为空,且不大于12个字.");
	   $('username').value = $('username').value.substring(0, 12)
	   return false; 
	}else{
		if ($F('content')==""||$('content').value.length>500){
			showerr("请填写内容,且内容不多于500个字.");
			$('content').value = $('content').value.substring(0, 500)
			return false;
		}else{
			function callback(req){			
				showerr(req.responseText);
				if (req.responseText.indexOf("成功")>0){
					$("content").value="";
					show("");
				}
			}
			var myAjax=new AJAX({
				url:"INC/Book.ASP",
				parameters:"act=add&username="+escape($F('username'))+"&content="+escape($F('content')),
				method:"POST",
				onComplete:callback,
				onLoading:"正在发表留言...",
				onError:function(re){showerr("发生错误,错误代码:"+re.status)}
			});
		}
	}
}


function show(param){
    //alert(param)
     if (param==""){
	    params="act=showgb";
    }else{
        params=param;
	}
	function callback(req){			
		$("gbook_view").innerHTML=(req.responseText);
	}
	var myAjax=new AJAX({
		url:"INC/Book.ASP",
		parameters:params,
		onComplete:callback,
		onLoading:"正在加载留言...",
		onError:function(re){showerr("发生错误,错误代码:"+re.status)}
	});
}

function cloading(){
	if ($("loading2"))$("loading2").innerHTML="";
}

function showerr(errstr){
	if ($("loading")){
		$("loading").innerHTML=errstr;
		setTimeout('$("loading").innerHTML=""',3000);
	}
	if ($("loading2")){
		$("loading2").innerHTML=errstr;
		setTimeout('cloading()',3000);
	}

}

function textCounter(sElement,maxlimit) { 
	field = sElement;
	if (field.value.length > maxlimit){
		field.value = field.value.substring(0, maxlimit); 		
	} else{
		document.getElementById("remLen").value = maxlimit - field.value.length;
	}
	if (field.value.length == maxlimit){
		showerr("请将字符限制在500个以内");
	}
}


function login(){
	if ($F('admin')==""||$F('pws')==""){
		showerr("请填写管理或密码.");
		return false;
	}else{
		function callback(req){			
			showerr(req.responseText);
			if (req.responseText.indexOf("成功")>0){
				$("admin").value="";
				$("pws").value="";
				$("loginbt").innerHTML="退出";
				$("loginbt").onclick=logout;
				collapse("loginbox");
				show("");
			}
		}
		var myAjax=new AJAX({
			url:"INC/Book.ASP",
			parameters:"act=login&admin="+escape($F('admin'))+"&pws="+escape($F('pws')),
			onComplete:callback,
			onLoading:"正在登陆......"
		});
	}
}

function checklogin(){
	function callback(req){			
		if (eval(req.responseText)==1){
			$("loginbt").innerHTML="管理";
			$("loginbt").onclick=openlogin;
		}else{
			$("loginbt").innerHTML="退出";
			$("loginbt").onclick=logout;
		}
	}
	var myAjax=new AJAX({
		url:"INC/Book.ASP",
		parameters:"act=checklogin",
		onComplete:callback
	});
	//setTimeout('checklogin()',5000);
}

function openlogin(){
	collapse('loginbox');
}

function logout(){
	function callback(req){
		$("loginbt").innerHTML="管理";
		$("loginbt").onclick=openlogin;
		show("");
	}
	var myAjax=new AJAX({
		url:"INC/Book.ASP",
		parameters:"act=logout",
		onComplete:callback,
		onLoading:"正在退出......"
	});
}

function cAlert(){
	if ($("shield"))document.body.removeChild($("shield"));
	if ($("alertFram"))document.body.removeChild($("alertFram"));
	document.body.oncontextmenu = function(){return true;}
	for (i=0;i<$$("select").length;i++){$$("select")[i].style.visibility="visible";}
}

function showregb(cid,param){
	var shield = document.createElement("DIV");
	shield.id = "shield";
	shield.style.position = "absolute";
	shield.style.left = "0px";
	shield.style.top = "0px";
	shield.style.width = document.body.clientWidth+"px";
	shield.style.height = document.body.clientHeight+document.body.scrollHeight+"px";
	shield.style.background = "#ccc";
	shield.style.zIndex = "10000";
	shield.style.filter = "alpha(opacity=0)";
	shield.style.opacity = 0;

	var alertFram = document.createElement("DIV");
	alertFram.id="alertFram";
	alertFram.style.position = "absolute";
	alertFram.style.left = "52.7%";
	alertFram.style.top = "70%";
	alertFram.style.marginLeft = "-225px" ;
	alertFram.style.marginTop = -75+document.documentElement.scrollTop+"px";
	alertFram.style.width = "350px";
	alertFram.style.zIndex = "10001";
	strHtml="<ul style=\"list-style:none;margin:0px;padding:0px;width:400px!important;width:350px;height:320px !important;height:200px;line-height:35px;text-align:left;border:1px solid #BBDDE5;background:#E8F0F8\">\n";
	strHtml+="<li style=\"padding-left:20px;height:30px !important;height:25px;background:#F4FAFB;color:#9CACAF;font-weight:bold;border-bottom:1px solid #BBDDE5;\">回复该条留言<span id=\"loading2\" style=\"padding-left:20px;color:#FF0000\"></span></li>\n";
	strHtml+="<li style=\"text-align:center;margin-top:5px;\"><textarea cols=\"40\" rows=\"10\" id=\"recontent\" name=\"recontent\"></textarea></li>\n";
	strHtml+="<li style=\"text-align:center;height:25px;\"><input type=\"button\" value=\" 确 定 回 复 \" id=\"do_OK\" onclick=\"regb("+cid+",'"+param+"');\" />&nbsp;&nbsp;<input type=\"button\" value=\"取消\" onclick=\"doOk()\" />&nbsp;&nbsp;</li>\n";
	strHtml+="</ul>\n";
	alertFram.innerHTML = strHtml;
	document.body.appendChild(alertFram);
	document.body.appendChild(shield);
	for (i=0;i<$$("select").length;i++){$$("select")[i].style.visibility="hidden";}
	
	this.setOpacity = function(obj,opacity){
		if(opacity>=1)opacity=opacity/100;
		try{ obj.style.opacity=opacity; }catch(e){}
		try{ 
			if(obj.filters.length>0&&obj.filters("alpha")){
				obj.filters("alpha").opacity=opacity*100;
			}else{
				obj.style.filter="alpha(opacity=\""+(opacity*100)+"\")";
			}
		}catch(e){}
	}
	var c= 0;
	this.doAlpha = function(){
		if (++c > 20){clearInterval(ad);return 0;}
		setOpacity(shield,c);
	}
	var ad = setInterval("doAlpha()",1);

	this.doOk = function(){
		if ($("shield"))document.body.removeChild($("shield"));
		if ($("alertFram"))document.body.removeChild($("alertFram"));
		document.body.oncontextmenu = function(){return true;}
		for (i=0;i<$$("select").length;i++){$$("select")[i].style.visibility="visible";}
	}
	$("do_OK").focus();
	document.body.oncontextmenu = function(){return false;}
}


function regb(cid,param){
	if ($('recontent').value==""){
       showerr("请填写回复内容");
	   return false; 
	}else{
		function callback(req){			
			showerr(req.responseText);
			if (req.responseText.indexOf("完毕")>0){
				$("recontent").value="";
				cAlert();
				show(param);
			}
		}
		var myAjax=new AJAX({
			url:"INC/Book.ASP",
			parameters:"act=regb&cid="+cid+"&recontent="+escape($F('recontent')),
			method:"POST",
			onComplete:callback,
			onLoading:"正在回复留言...",
			onError:function(re){showerr("发生错误,错误代码:"+re.status)}
		});
	}
}


function delgb(id,param){
	var myAjax=new AJAX({
		url:"INC/Book.ASP",
		parameters:"act=delgb&id="+id,
		onLoading:"正在删除......",
		onComplete:function(re){show(param)}
	});
}


function delregb(id,param){
	var myAjax=new AJAX({
		url:"INC/Book.ASP",
		parameters:"act=delregb&id="+id,
		onLoading:"正在删除......",
		onComplete:function(re){show(param)}
	});
}

function comment(id,param){
	var myAjax=new AJAX({
		url:"INC/Book.ASP",
		parameters:"act=comment&id="+id,
		onLoading:"正在置顶......",
		onComplete:function(re){show(param)}
	});
}

function recomment(id,param){
	var myAjax=new AJAX({
		url:"INC/Book.ASP",
		parameters:"act=recomment&id="+id,
		onLoading:"正在取消置顶......",
		onComplete:function(re){show(param)}
	});
}


show(""); checklogin();setInterval('show("")',120000);
