function Trim(){
	return this.replace(/\s+$|^\s+/g,"");
}
String.prototype.Trim=Trim;

function $(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($(id).value!=undefined)
		return $(id).value.Trim();
	else
		return "";
}

function HXCMSXHR(){
	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;
	}
	return true;
}
function checkrepForm(){	
	if(getObjValue("contents")==""){
		alert("回复内容不能为空！");
		return false;
	}
	return true;
}
function sendword() {	
	var xhr=HXCMSXHR();
	if(xhr&&checkForm()){
		$("submit").value="Sending...";
		$("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));
                                        $("submit").value="提 交";
					$("submit").disabled=false;
                                        clearForm();				
					addNew();	
				}else{
					alert("您请求的页面出现异常错误！请重试！");	
				}
			}	
		};
    		xhr.send(data);
  	}
}
function sendreplyer() {	
	var xhr=HXCMSXHR();
	if(xhr&&checkrepForm()){
		$("submit").value="Sending...";
		$("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));
                                        $("submit").value="提 交";
					$("submit").disabled=false;
                                        clearrepForm();				
					repNew();	
				}else{
					alert("您请求的页面出现异常错误！请重试！");	
				}
			}	
		};
    		xhr.send(data);
  	}
}
function clearForm(){
	$("usernames").value="";
	$("title").value="";
	$("contents").value="";
}

function addNew(){	
	getList(1);	
}
function clearrepForm(){
	$("contents").value="";
}
function repNew(){	
	getList();	
}
function makBookStr(title,username,touxian,content,tools){	
        var tempStr='<div style="background:#FBF7FB;padding:5px;margin-top:5px;border-top:1px solid #FF6600;">'+unescape(title)+'</div>\
             <table width="100%" border="0" cellpadding="0" cellspacing="0"><tr style="height: 100%">\
             <td valign="top" style="padding:5px;width:80px;font-size:12px;">\
             <div style="word-break:break-all;">'+unescape(username)+'</div>\
             <div>'+touxian+'</div></td>\
             <td style="word-break:break-all;padding:5px;">\
             <div style="text-align: left;">'+unescape(content)+'</div>\
             </td>\
             </tr></table>\
             <div style="text-align: right;font-size:12px;padding-right:5px;">'+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;line-height:50px;margin:10px;'>暂时还没有留言！</div>"
	}
	$("bookList").innerHTML=allStr;
}
function getList(page){
	$("bookList").innerHTML="<div>加载中...请稍后!</div>";	
	var xhr=HXCMSXHR();
	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(){
	$("bookList").innerHTML="<div>加载中...请稍后!</div>";	
	var xhr=HXCMSXHR();
	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);
				$("bookList").innerHTML=msg;
				$("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($("replyer"+id))
        $("replyer"+id).innerHTML="<div>加载中...请稍后!</div>";
	var xhr=HXCMSXHR();
	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);
				$("replyer"+id).innerHTML=msg;
			}else{
				alert("您请求的页面出现异常错误！！");	
			}
		}
		
	}
	xhr.send(null);
}
function getdels(id){	
        if (!confirm("确定要删除这条留言？"))
		{
			return;
		}
	var xhr=HXCMSXHR();
	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(){
	$("pageList").innerHTML="<div>信息加载中...请稍后!</div>";
	var xhr=HXCMSXHR();
	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>共有<span style=\"font-size:12px;\">"+Result[0]+"</span>条留言 每页<span style=\"font-size:12px;\">"+Result[1]+"</span>条留言  当前第<span style=\"font-size:12px;\">"+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('');
				$("pageList").innerHTML=ResultStr;
			}else{
				alert("您请求的页面出现异常错误！请刷新重试！");	
			}
		}
		
	}
	xhr.send(null);
}