<%@page contentType="text/html; charset=UTF-8"%>
<%@include file="/inc/init.jsp" %>
<%
if(ConfigUtil.isInstalled){
	out.print("<script>alert('已经安装完成，无须再次安装！');window.location.href='.'; </script>");
	return;
}else{
%>
<%
long timePageStart = System.currentTimeMillis();
class EnvServlet
{
	public long timeUse=0;
	public Hashtable htParam=new Hashtable();
	private Hashtable htShowMsg=new Hashtable();
	public void setHashtable()
	{
		Properties me=System.getProperties();
		Enumeration em=me.propertyNames();
		while(em.hasMoreElements())
		{
			String strKey=(String)em.nextElement();
			String strValue=me.getProperty(strKey);
			htParam.put(strKey,strValue);
		}
	}	
	public void getHashtable(String strQuery)
	{
		Enumeration em=htParam.keys();
		while(em.hasMoreElements())
		{
			String strKey=(String)em.nextElement();
			String strValue=new String();
			if(strKey.indexOf(strQuery,0)>=0)
			{
				strValue=(String)htParam.get(strKey);
				htShowMsg.put(strKey,strValue);
			}
		}
	}
	public String queryHashtable(String strKey)
	{
		strKey=(String)htParam.get(strKey);
		return strKey;
	}
	public long test_int()
	{
		long timeStart = System.currentTimeMillis();
		int i=0;
		while(i<3000000)i++;
		long timeEnd = System.currentTimeMillis();
		long timeUse=timeEnd-timeStart;
		return timeUse;
	}
	public long test_sqrt()
	{
		long timeStart = System.currentTimeMillis();
		int i=0;
		double db=(double)new Random().nextInt(1000);
		while(i<200000){db=Math.sqrt(db);i++;}
		long timeEnd = System.currentTimeMillis();
		long timeUse=timeEnd-timeStart;
		return timeUse;
	}
}
%>
<%
	EnvServlet env=new EnvServlet();
	env.setHashtable();
	String action=new String(" ");
	String act=new String("action");
	if(request.getQueryString()!=null&&request.getQueryString().indexOf(act,0)>=0)action=request.getParameter(act);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><%=Config.get("version")%> - 安装</title>
<script language="javascript" type="text/javascript" src="/js/jquery.js"></script>
<script language="javascript" type="text/javascript" src="/js/checkform.js"></script>
<link href="skin/install_style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form action="" method="post" name="mainform">
<div class="main">
		<div class="pr-title"><h3>服务器状态</h3></div>
		<table width="726" border="0" align="center" cellpadding="0" cellspacing="0" class="twbox">
			<tr>
				<th width="300" align="center"><strong>参数</strong></th>
				<th width="424"><strong>值</strong></th>
			</tr>
			<tr>
					<td><strong>服务器域名</strong></td>
					<td>&nbsp;<%= request.getServerName() %>(<%=request.getRemoteAddr()%>)</td>
			</tr>
			<tr>
					<td><strong>服务器操作系统</strong></td>
					<td>&nbsp;<%=env.queryHashtable("os.name")%> <%=env.queryHashtable("os.version")%> 
              <%=env.queryHashtable("sun.os.patch.level")%></td>
			</tr>
			<tr>
					<td><strong>服务器解译引擎</strong></td>
					<td>&nbsp;<%= getServletContext().getServerInfo() %></td>
			</tr>
			<tr>
					<td><strong>JAVA运行环境名称</strong></td>
					<td>&nbsp;<%=env.queryHashtable("java.runtime.name")%>&nbsp;<%=env.queryHashtable("java.runtime.version")%></td>
			</tr>
			<tr>
					<td><strong>系统安装目录</strong></td>
					<td>&nbsp;<%=request.getRealPath(request.getServletPath())%></td>
			</tr>
			</table>    
		<div class="pr-title"><h3>数据库设定</h3></div>
		<table width="726" border="0" align="center" cellpadding="0" cellspacing="0" class="twbox">
			<tr>
				<td class="onetd"><strong>数据库主机：</strong></td>
				<td><input name="host" id="host" type="text" value="<%=StringUtil.isFine(Config.get("dbhost"))?Config.get("dbhost"):"localhost"%>" class="input-txt" />
					<small>一般为localhost，可不必修改</small></td>
				</tr>
			<tr>
				<td class="onetd"><strong>数据库用户：</strong></td>
				<td><input name="dbuser" id="dbuser" type="text" value="<%=StringUtil.isFine(Config.get("username"))?Config.get("username"):""%>" class="input-txt" />
				填写数据库用户名</td>
				</tr>
			<tr>
				<td class="onetd"><strong>数据库密码：</strong></td>
				<td><div style='float:left;margin-right:3px;'>
				<input name="dbpwd" id="dbpwd" type="text"  value="<%=StringUtil.isFine(Config.get("password"))?Config.get("password"):""%>" class="input-txt" />&nbsp;填写数据库密码</div>
					<div style='float:left' id='dbpwdsta'></div>
					</td>
				</tr>
			<tr>
				<td class="onetd"><strong>数据库名称：</strong></td>
				<td>
					<div style='float:left;margin-right:3px;'><input name="basedb" id="basedb" type="text" value="<%=StringUtil.isFine(Config.get("basedb"))?Config.get("basedb"):"xinlu"%>" class="input-txt" />&nbsp;数据库名(若是JSP空间，请填写空间上的数据库名称)</div>
					<div style='float:left' id='havedbsta'></div>
					</td>
				</tr>
			<tr>
				<td class="onetd"><div id="dbconn" style="display:none"><img src="skin/images/ajax-loader.gif" width="16" height="16" /></div></td>
				<td>
					&nbsp;&nbsp;<input type="button" id="testconbt" name="testconbt" value="测试连接" style="height:30px;width:80px;" onclick="checkdb();" />
				</td>
			</tr>
			</table>
		<div class="pr-title"><h3>管理员帐号及密码</h3></div>
		<table width="726" border="0" align="center" cellpadding="0" cellspacing="0" class="twbox">
			<tr>
				<td class="onetd"><strong>用户名：</strong></td>
				<td>
					<input name="adminuser" type="text" class="input-txt" value="" maxlength="20" />&nbsp;&nbsp;只能用'0-9'、'a-z'、'A-Z' 以内范围的字符
				</td>
				</tr>
			<tr>
				<td class="onetd"><strong>密　码：</strong></td>
				<td><input name="adminpwd" type="password" class="input-txt" value="" maxlength="20" /></td>
				</tr>
			<tr>
				<td class="onetd"><strong>重复密码：</strong></td>
				<td><input name="adminpwd2" type="password" class="input-txt" maxlength="20" /></td>
				</tr>
			<tr>
				<td class="onetd"><div id="installstate" style="display:none"><img src="skin/images/ajax-loader.gif" width="16" height="16" /></div></td>
				<td><input type="button" style="height:30px;width:80px;" id="startbt" value="开始安装" onclick="checkusername();" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" style="height:30px;width:80px;" id="loginbt" value="前往后台登陆" onclick="window.location.href='.';" disabled="disabled" /></td>
			</tr>
			</table>
</div>
<div class="foot">
</div>
<input type="hidden" name="checkdbflag" id="checkdbflag" value="0" />
</form>
<script language="javascript">
function checkusername(){
	var host=document.mainform.host.value;
	var dbuser=document.mainform.dbuser.value;
	var dbpwd = document.mainform.dbpwd.value;
	var basedb = document.mainform.basedb.value;
	if(host.length<1){
	alert("数据库主机地址不能为空");
	document.mainform.host.focus();
	return false;
	}
	if(dbuser.length<1){
	alert("数据库用户名不能为空");
	document.mainform.dbuser.focus();
	return false;
	}
	if(basedb.length<1){
	alert("数据库名称不能为空！");
	document.mainform.basedb.focus();
	return false;
	}	
	var adminuser=document.mainform.adminuser.value;
	var adminpwd=document.mainform.adminpwd.value;
	var adminpwd2 = document.mainform.adminpwd2.value;
	if(adminuser.length<1||!checkAdminid(adminuser)){
	alert("请填写规范的管理员ID");
	document.mainform.adminuser.focus();
	return false;
	}
	if(adminpwd.length<6){
	alert("管理员密码不能少于6位");
	document.mainform.adminpwd.focus();
	return false;
	}
	if(adminpwd2.length<6){
	alert("管理员重复密码不能少于6位");
	document.mainform.adminpwd2.focus();
	return false;
	}
	if(adminpwd2!=adminpwd){
	alert("输入的两次密码不一致");
	document.mainform.adminpwd2.focus();
	return false;
	}
	var isok=false;
	$('#startbt').attr("disabled","disabled"); 
	$('#startbt').val("安装中...."); 
	$('#installstate').show(); 
	$.ajax({
		   url:'/dpax/installSite.jsp',
		   type:'post',
		   data:'action=conn&host='+host+"&dbuser="+dbuser+"&dbpwd="+dbpwd+"&basedb="+basedb,
		   success:function(msg){
			   if($.trim(msg)=='1'){  //数据库连接成功，继续安装
					$.ajax({
						   url:'/dpax/installSite.jsp',
						   type:'post',
						   data:'action=install&adminname='+adminuser+"&adminpwd="+adminpwd+"&host="+host+"&dbuser="+dbuser+"&dbpwd="+dbpwd+"&basedb="+basedb,
						   success:function(msg){
							   alert($.trim(msg));
							   $('#loginbt').removeAttr("disabled")
							   $('#startbt').removeAttr("disabled"); 
							   $('#startbt').val("开始安装"); 
							   $('#installstate').hide(); 
						   }
						});
			   }else{
			    alert('无法连接数据，请检查数据库是否已经创建或者数据配置是否正确!');
				$('#startbt').removeAttr("disabled"); 
			    $('#startbt').val("开始安装"); 
			    $('#installstate').hide(); 
			   }
		   }
		});
}
function checkdb(){
	var host=document.mainform.host.value;
	var dbuser=document.mainform.dbuser.value;
	var dbpwd = document.mainform.dbpwd.value;
	var basedb = document.mainform.basedb.value;
	if(host.length<1){
	alert("数据库主机地址不能为空");
	document.mainform.host.focus();
	return false;
	}
	if(dbuser.length<1){
	alert("数据库用户名不能为空");
	document.mainform.dbuser.focus();
	return false;
	}
	if(basedb.length<1){
	alert("数据库名称不能为空！");
	document.mainform.basedb.focus();
	return false;
	}
	$('#testconbt').attr("disabled","disabled"); 
	$('#testconbt').val("测试连接中...."); 
	$('#dbconn').show(); 
$.ajax({
		   url:'/dpax/installSite.jsp',
		   type:'post',
		   data:'action=conn&host='+host+"&dbuser="+dbuser+"&dbpwd="+dbpwd+"&basedb="+basedb,
		   success:function(msg){
			   if($.trim(msg)=='1'){
				alert('连接数据库正常!');
				$("#checkdbflag").val('1');
			   }else if($.trim(msg)=='0'){
				alert('无法连接数据，请检查数据库是否已经创建或者数据配置是否正确!');
			   }else{
				alert($.trim(msg));
			   }
			    $('#testconbt').removeAttr("disabled"); 
				$('#testconbt').val("测试连接"); 
				$('#dbconn').hide(); 
		   }
});
}
</script>
</body>
</html>
<%}%>