// JavaScript Document
//
var xmlHttp;
var url;
var WaitPic="<img src=\"image/ref.gif\" alt=\"等待加载\" width=\"16\" height=\"16\" />";
var viewModel;//等待显示区域
var waitTime=1000;//切换等待时间
function createXMLHttpRequest() {
		xmlHttp = false;
		//开始初始化XMLHttpRequest对象
		if(window.XMLHttpRequest) { //Mozilla 浏览器
			xmlHttp = new XMLHttpRequest();
			if (xmlHttp.overrideMimeType) {//设置MiME类别
				xmlHttp.overrideMimeType('text/xml');
			}
		}
		else if (window.ActiveXObject) { // IE浏览器
			try {
				xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
		}
		if (!xmlHttp) { // 异常，创建对象实例失败
			window.alert("不能创建XMLHttpRequest对象实例.");
			return false;
		}
}

/*
requestPage请求页面方法
model:请求的加载的模块
viewPage:请求的页面
ext：页面的扩展名
variable：追加传递的变量
*/
function requestPage(model,viewPage,ext,variable) {
      createXMLHttpRequest();
	  viewModel=model;
      document.getElementById(model).innerHTML=WaitPic+"等待……";
	  url=viewPage+"."+ext+"?time="+new Date().getTime()+"&"+variable;
	  xmlHttp.open("GET", url, true);
      xmlHttp.onreadystatechange = responsePage;
      xmlHttp.send(null);
}

function responsePage()
{
	if(xmlHttp.readyState == 4) 
	{
		if (xmlHttp.status == 200)
		{
			setTimeout("viewContent()",waitTime);
		}
		else
		{
			alert("服务器异常，不能响应！");
			document.getElementById(viewModel).innerHTML="";
		}
    }
	else
	{
		if(xmlHttp.readyState == 1)
		{document.getElementById(viewModel).innerHTML=WaitPic+"正在加载……";}
		if(xmlHttp.readyState == 2)
		{document.getElementById(viewModel).innerHTML=WaitPic+"已经加载……";}
		if(xmlHttp.readyState == 3)
		{document.getElementById(viewModel).innerHTML=WaitPic+"下载中……";}
	}
	
}
//显示模块
function viewContent()
{
	//alert(xmlHttp.responseText);
	document.getElementById(viewModel).innerHTML=xmlHttp.responseText;
}

function isValidString(string)
{
    var validChars="!@#$%^&*()<>/\\;'\",.！·#￥%……—*（）《》；‘、，。";
    var isValidString=true;
    var aChar;
    for(i=0;i<string.length && isValidString==true;i++)
    {
        aChar=string.toLowerCase().charAt(i);
        if(validChars.indexOf(aChar)!=-1)
        isValidString=false;
    }
    return isValidString;
}

function AdminDenglu()
{
    var adminid=document.getElementById("adminid").value;
    var adminpwd=document.getElementById("adminpwd").value; 
    if(adminid=="")
    {
       alert("姓名不完整，请检查");
       return ;
    }
    else if(adminpwd=="")
    {
       alert("密码不完整，请检查");
       return ;
    }
    else if(!isValidString(adminid) && !isValidString(adminpwd))
    {
       alert("您输入的用户名或者密码存在非法字符，请检查");
    }
    else
    {
	    adminid=encodeURI(encodeURI(adminid));
		adminpwd=encodeURI(encodeURI(adminpwd));
	    
	    var url="Guestbook?time="+new Date().getTime();
	    var QueryString="action=land&adminid="+adminid+"&adminpwd="+adminpwd;
	    createXMLHttpRequest();
	    document.getElementById("dl").setAttribute("disabled","disabled");//将按钮置为锁定
	    xmlHttp.open("POST", url, true);
	    xmlHttp.onreadystatechange = adminMessage;
	    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	    xmlHttp.send(QueryString);
    }
}   

function adminMessage()
{
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
            xmlDoc = xmlHttp.responseXML;
			var error=xmlDoc.getElementsByTagName("error").item(0).firstChild.nodeValue;
			var errorText=xmlDoc.getElementsByTagName("errorText").item(0).firstChild.nodeValue;
			var status=xmlDoc.getElementsByTagName("status").item(0).firstChild.nodeValue;
			var Adminname=xmlDoc.getElementsByTagName("Adminname").item(0).firstChild.nodeValue;
			if(error==1)
			{
				alert(errorText);
				document.getElementById("dl").removeAttribute("disabled");//将按钮置为击活
			}
			else
			{
				if(status==1)
				{//添加正常
					alert("欢迎您管理员！"+Adminname);
					location.href="index.jsp";
				}
				else
				{
					alert("登陆失败，错误原因：数据库异常！");
					document.getElementById("dl").removeAttribute("disabled");//将按钮置为击活
				}
			}
        }
		else
		{
			alert("服务器异常");
			document.getElementById("dl").removeAttribute("disabled");//将按钮置为击活
		}
    }
}   

function Zhuxiao()
{
	var url="Guestbook?time="+new Date().getTime();
	var QueryString="action=zhuxiao";
	createXMLHttpRequest();
	document.getElementById("zx").setAttribute("disabled","disabled");
	xmlHttp.open("POST", url, true);
	xmlHttp.onreadystatechange = adminzhuxiao;
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	xmlHttp.send(QueryString);
}

function adminzhuxiao()
{
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
            xmlDoc = xmlHttp.responseXML;
			var status=xmlDoc.getElementsByTagName("status").item(0).firstChild.nodeValue;
			if(status==1)
			{//操作正常
				location.href="index.jsp";
			}
			else
			{
				alert("注销失败，错误原因：数据库异常！");
				document.getElementById("zx").removeAttribute("disabled");//将按钮置为击活
			}
        }
		else
		{
			alert("服务器异常");
			document.getElementById("zx").removeAttribute("disabled");//将按钮置为击活
		}
    }
}

function deleteMessage(id)
{
    var GBid=id;
    GBid=encodeURI(encodeURI(GBid));
	var url="Guestbook?time="+new Date().getTime();
	var QueryString="action=delete&GBid="+GBid;
	createXMLHttpRequest();
	document.getElementById("del").setAttribute("disabled","disabled");
	xmlHttp.open("POST", url, true);
	xmlHttp.onreadystatechange = delMessage;
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");    
    xmlHttp.send(QueryString);
}

function delMessage()
{
    if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
            xmlDoc = xmlHttp.responseXML;
			var status=xmlDoc.getElementsByTagName("status").item(0).firstChild.nodeValue;
			if(status==1)
			{//操作正常
				alert("删除留言成功！");
				requestPage('display','display','jsp','');
			}
			else
			{
				alert("删除失败，错误原因：数据库异常！");
				document.getElementById("del").removeAttribute("disabled");//将按钮置为击活
			}
        }
		else
		{
			alert("服务器异常");
			document.getElementById("del").removeAttribute("disabled");//将按钮置为击活
		}
    }
}

function GuestBookaddMessage()
{
	var GuestName=document.getElementById("GuestName").value;
	var checkNum=document.getElementById("checkNum").value;
	var GBContent=document.getElementById("GBContent").value;
	if(GuestName==""||checkNum==""||GBContent=="")
	{
		alert("姓名，验证码，留言内容不完整，请检查");
	}
	else if(GuestName.length<2)
	{
		alert("请填写真实姓名，否则不能正常留言！");
	}
	else if(GuestName.length>8)
	{
	    alert("你输入的姓名过长！");
	}
    else if(!isValidString(GuestName))
	{
		alert("你输入的姓名存在非法字符,请检查");
	}
	else
	{
		GuestName=encodeURI(encodeURI(GuestName));
		GBContent=encodeURI(encodeURI(GBContent));
	
		var url="Guestbook?time="+new Date().getTime();
		var QueryString="action=add&GuestName="+GuestName+"&checkNum="+checkNum+"&GBContent="+GBContent;
		createXMLHttpRequest();
		document.getElementById("button_addMessage").setAttribute("disabled","disabled");//将按钮置为锁定
		xmlHttp.open("POST", url, true);
	    xmlHttp.onreadystatechange = parseMessage;
	    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");    
	    xmlHttp.send(QueryString);
	}
}
function parseMessage()
{
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
            xmlDoc = xmlHttp.responseXML;
			var error=xmlDoc.getElementsByTagName("error").item(0).firstChild.nodeValue;
			var errorText=xmlDoc.getElementsByTagName("errorText").item(0).firstChild.nodeValue;
			var status=xmlDoc.getElementsByTagName("status").item(0).firstChild.nodeValue;
			var GuestName=xmlDoc.getElementsByTagName("GuestName").item(0).firstChild.nodeValue;
			//alert(xmlHttp.getAllResponseHeaders());
			if(error==1)
			{
				alert(errorText);
				document.getElementById("button_addMessage").removeAttribute("disabled");//将按钮置为击活
				}
			else
			{
				if(status==1)
				{//添加正常
					alert("感谢您："+GuestName+",您的留言添加成功，我们将尽快回复，回复后显示！");
					requestPage('display','display','jsp','');
					document.getElementById("liuyan").innerHTML="";
				}
				else
				{
					alert("添加失败，错误原因：数据库异常！");
					document.getElementById("button_addMessage").removeAttribute("disabled");//将按钮置为击活
				}
			}
        }
		else
		{
			alert("服务器异常");
			document.getElementById("button_addMessage").removeAttribute("disabled");//将按钮置为击活
		}
    }
}

function reMessage(s)
{
    var GBid=s;//document.getElementById(s).value;
    var reContent=document.getElementById("reContent_"+s).value;
    if(reContent=="")
    {
       alert("请输入您要回复的内容！");
       return ;
    }
    else
    { 
	    reContent=encodeURI(encodeURI(reContent));
	    var url="Guestbook?time="+new Date().getTime();
	    var QueryString="action=rebook&GBid="+GBid+"&reContent="+reContent;
	    createXMLHttpRequest();
	    document.getElementById("retijiao").setAttribute("disabled","disabled");
	    xmlHttp.open("POST", url, true);
	    xmlHttp.onreadystatechange = reBook;
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");    
	    xmlHttp.send(QueryString);
    }
}

function reBook()
{
    if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
            xmlDoc = xmlHttp.responseXML;
			var error=xmlDoc.getElementsByTagName("error").item(0).firstChild.nodeValue;
			var errorText=xmlDoc.getElementsByTagName("errorText").item(0).firstChild.nodeValue;
			var status=xmlDoc.getElementsByTagName("status").item(0).firstChild.nodeValue;
			if(error==1)
			{
				alert(errorText);
				document.getElementById("retijiao").removeAttribute("disabled");//将按钮置为击活
				}
			else
			{
				if(status==1)
				{//添加正常
					alert("回复留言成功！");
					requestPage('display','display','jsp','');
				}
				else
				{
					alert("添加失败，错误原因：数据库异常！");
					document.getElementById("retijiao").removeAttribute("disabled");//将按钮置为击活
				}
			}
        }
		else
		{
			alert("服务器异常");
			document.getElementById("retijiao").removeAttribute("disabled");//将按钮置为击活
		}
    }
}

function Search()
{
    var key=document.getElementById("scname").value;
    var radio=document.xsbd.radio;
    if(key=="")
    {
        alert("请输入搜索关键字");
    }
    else
    {
		 for(i=0;i<radio.length;i++)
		 {
			 if(radio[i].checked==true)
			 {
				 requestPage('display','search','jsp',"checkey="+key+"&type="+radio[i].value);
			 }
		 }
	}
}