function Trim(){
	return this.replace(/\s+$|^\s+/g,"");
}
String.prototype.Trim=Trim;
function getObject(id) { 
	if (document.getElementById) { 
		return document.getElementById(id); 
	} else if (document.all) { 
		return document.all[id]; 
	} else if (document.layers) { 
		return document.layers[id]; 
	} 
}
function getObjValue(id){
	if(getObject(id).value!=undefined)
		return getObject(id).value.Trim();
	else
		return "";
}
function 07ingXHR(){
	var xhr;
	try{
		xhr=new XMLHttpRequest();
	}catch(e){
    		var a=['MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','MICROSOFT.XMLHTTP.1.0','MICROSOFT.XMLHTTP.1','MICROSOFT.XMLHTTP'];
    		for (var i=0;i<a.length;i++){
      			try{
        			xhr = new ActiveXObject(a[i]);
        			break;
      			}catch(e){}
    		}
  	}
	return xhr;
}
function checkForm(){	
	if(getObjValue("usernames")==""||getObjValue("title")==""||getObjValue("contents")==""){
		alert("请把带 * 号的项填写完整！");
		return false;
	}
	if(getObjValue("usernames").length>20){
		alert("昵称不能大于20字");
		return false;
	}
	if(getObjValue("title").length>30){
		alert("主题不能大于30字");
		return false;
	}
	if(getObjValue("contents").length>1000){
		alert("留言内容大于1000字");
		return false;
	}
	if(getObjValue("qq")!="" && !/^[0-9]{5,10}$/.test(getObjValue("qq"))){
		alert("qq号应是纯数字");
		return false;
	}
	if(getObjValue("email")!="" && !/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(getObjValue("email"))){
		alert("email格式错误！");
		return false;
	}
	if(getObjValue("web").length>50){
		alert("主页地址大于50字");
		return false;
	}
	return true;
}
function checkrepForm(){	
	if(getObjValue("contents")==""){
		alert("回复内容不能为空！");
		return false;
	}
	return true;
}
function sendword() {	
	var xhr=07ingXHR();
	if(xhr&&checkForm()){
		getObject("submit").value="Sending...";
		getObject("submit").disabled=true;
		xhr.open("POST", "../GuestBook/Book_add.asp?a=add", true);
		xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		var aIdArray=new Array("flag="+Math.random());
		var argLen=arguments.length;
		for(i=0;i<argLen;i++){
			aIdArray[i+1]="&"+arguments[i]+"="+escape(getObjValue(arguments[i]));
		}
    	var data =aIdArray.join('');
		xhr.onreadystatechange=function(){
			if(xhr.readyState==4){
				if(xhr.status==200){
					alert(unescape(xhr.responseText));
                                        getObject("submit").value="提 交";
					getObject("submit").disabled=false;
                                        clearForm();				
					addNew();	
				}else{
					alert("您请求的页面出现异常错误！请重试！");	
				}
			}	
		};
    		xhr.send(data);
  	}
}
function sendreplyer() {	
	var xhr=07ingXHR();
	if(xhr&&checkrepForm()){
		getObject("submit").value="Sending...";
		getObject("submit").disabled=true;
		xhr.open("POST", "../GuestBook/Book_add.asp?a=replyer", true);
		xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		var aIdArray=new Array("flag="+Math.random());
		var argLen=arguments.length;
		for(i=0;i<argLen;i++){
			aIdArray[i+1]="&"+arguments[i]+"="+escape(getObjValue(arguments[i]));
		}
    	var data =aIdArray.join('');
		xhr.onreadystatechange=function(){
			if(xhr.readyState==4){
				if(xhr.status==200){
					alert(unescape(xhr.responseText));
                                        getObject("submit").value="提 交";
					getObject("submit").disabled=false;
                                        clearrepForm();				
					repNew();	
				}else{
					alert("您请求的页面出现异常错误！请重试！");	
				}
			}	
		};
    		xhr.send(data);
  	}
}
function clearForm(){
	getObject("usernames").value="";
	getObject("qq").value="";
	getObject("email").value="";
	getObject("web").value="";
	getObject("title").value="";
	getObject("contents").value="";
}
function addNew(){	
	getList(1);	
}
function clearrepForm(){
	getObject("contents").value="";
}
function repNew(){	
	getList();	
}
function makBookStr(title,username,touxian,content,tools){	
        var tempStr='<div class="book_title"><ul><li>'+unescape(title)+'</li></ul></div>\
             <div class="cle_fl"></div>\
             <div class="Tab_left w_5 other_2">\
             <div class="other_16" style="word-break:break-all;">'+unescape(username)+'</div>\
             <div class="other_16">'+touxian+'</div></div>\
             <div class="Tab_right w_6">\
             <div class="book_body">'+unescape(content)+'</div>\
             </div>\
             <div class="cle_fl"></div>\
             <div class="book_tool">'+unescape(tools)+'</div>';
	return tempStr;
}
function writeBookArr(Str){
	if(Str!=0){
		var bookList=eval("new Array("+Str+")");
		var allStr="";
		for(var i=0;i<bookList.length;i++){
		allStr+=makBookStr(bookList[i].title,bookList[i].username,bookList[i].touxian,bookList[i].content,bookList[i].tools);	
		}
	}else{
		allStr="<div style='color:#f00;text-align:center;border:1px solid #d3d3d3;width: 500px;line-height:50px;margin-top:10px;'>暂时还没有留言！</div>"
	}
	getObject("bookList").innerHTML=allStr;
}
function getList(page){
	getObject("bookList").innerHTML="<div>加载中...请稍后!</div>";	
	var xhr=07ingXHR();
	xhr.open("GET", "book_getRecord.asp?page="+page+"&r="+Math.random(), true);
	xhr.onreadystatechange=function(){
		if(xhr.readyState==4){
			if(xhr.status==200){
				writeBookArr(xhr.responseText);
				getPage();	
			}else{
				alert("您请求的页面出现异常错误！请刷新重试！");	
			}
		}
		
	}
	xhr.send(null);
}
function getadd(){
	alert("ddddd");
	getObject("bookList").innerHTML="<div>加载中...请稍后!</div>";	
	var xhr=07ingXHR();
	xhr.open("GET", "book_add.asp?a=addform", true);
	xhr.onreadystatechange=function(){
		if(xhr.readyState==4){
			if(xhr.status==200){
                                var msg=unescape(xhr.responseText);
				getObject("bookList").innerHTML=msg;
				getObject("pageList").innerHTML="";	
			}else{
				alert("您请求的页面出现异常错误！！");	
			}
		}
		
	}
	xhr.send(null);
}
function opencat(cat){
  if(cat.style.display=="none"){
    cat.style.display="";
  	} else {
    cat.style.display="none"; 
  	}
}
function getreplyer(id){
        opencat(getObject("replyer"+id))
        getObject("replyer"+id).innerHTML="<div>加载中...请稍后!</div>";
	var xhr=07ingXHR();
	xhr.open("GET", "../GuestBook/book_add.asp?a=replyerform&b="+id, true);
	xhr.onreadystatechange=function(){
		if(xhr.readyState==4){
			if(xhr.status==200){
                                var msg=unescape(xhr.responseText);
				getObject("replyer"+id).innerHTML=msg;
			}else{
				alert("您请求的页面出现异常错误！！");	
			}
		}
		
	}
	xhr.send(null);
}
function getdels(id){	
        if (!confirm("确定要删除这条留言？"))
		{
			return;
		}
	var xhr=07ingXHR();
	xhr.open("GET", "book_add.asp?a=dels&rid="+id, true);
	xhr.onreadystatechange=function(){
		if(xhr.readyState==4){
			if(xhr.status==200){
                                repNew();
			}else{
				alert("删除失败！！");	
			}
		}
		
	}
	xhr.send(null);
}
function getPage(){
	getObject("pageList").innerHTML="<div>信息加载中...请稍后!</div>";
	var xhr=07ingXHR();
	xhr.open("GET", "../GuestBook/book_getRecord.asp?act=getPageInfo&r="+Math.random(), true);
	xhr.onreadystatechange=function(){
		if(xhr.readyState==4){
			if(xhr.status==200){
				var Result=xhr.responseText.split("|");
				var tempPageStr=new Array("<div  class=\"book_page\">共有<span class=\"grn\">"+Result[0]+"</span>条留言 每页<span class=\"grn\">"+Result[1]+"</span>条留言  当前第<span class=\"red\">"+Result[3]+"/"+Result[2]+"</span>页  <a href=\"#\" onclick=\"javascript:getList('1');\">首页</a>   <a href=\"#\" onclick=\"javascript:getList("+Result[4]+");\">上一页</a>   <a href=\"#\" onclick=\"javascript:getList("+Result[5]+");\">下一页</a>  <a href=\"#\" onclick=\"javascript:getList("+Result[2]+");\">尾页</a>  转向第 <select name=\"page\" onchange=\"getList(this.value)\" style=\"width:50px;\">");
				for(var i=1;i<=Result[2];i++){
					if(i!=Result[3])
						tempPageStr[i]="<option value=\""+i+"\">"+i+"</option>";
					else
						tempPageStr[i]="<option selected=\"selected\" value=\""+i+"\">"+i+"</option>";
				}
				tempPageStr[i]="</select> 页</div>";
				var ResultStr=tempPageStr.join('');
				getObject("pageList").innerHTML=ResultStr;
			}else{
				alert("您请求的页面出现异常错误！请刷新重试！");	
			}
		}
		
	}
	xhr.send(null);
}