<%@ page language="java"
	contentType="text/html;charset=Shift_JIS"
	import="java.util.*,java.io.*,openolap.viewer.common.Messages"
%>
<%!

    /** ======================================================================= 
  	    デフォルトのプール名、スキーマ名の設定部
		=====================================================================*/
//	private String defaultSchemaName = "oo_meta";
//	private String defaultPoolName = "jdbc/WroxTC41";


	/** デフォルトのスキーマ名、コネクションプール名をセッションへ登録する */
	public void setConnectInfo(HttpServletRequest request) {
	
		request.getSession().setAttribute("searchPathName", Messages.getString("PostgresDAOFactory.meta"));
		request.getSession().setAttribute("connectionPoolName", Messages.getString("PostgresDAOFactory.poolName"));
	}

%>
<%

	String loginErrorMessage = "";
	if ( (request.getAttribute("loginResult") != null) && 
         (request.getAttribute("loginResult").equals("failed")) ) {
		loginErrorMessage = Messages.getString("loginJSP.loginFailMSG");

	}

%>

<html>
<head>
	<title>OpenOLAP Viewer</title>
	<meta http-equiv="Content-Type" content="text/html;charset=Shift_JIS">
	<script type="text/javascript" src="./spread/js/spreadFunc.js"></script>
	<link rel="stylesheet" href="css/common.css" type="text/css">
		<style type="text/css">
	<!--
		body {
			background-image : url("images/bg.gif");
			text-align : center;
		}
		
		th,td{
			color : navy;
		}
		th {
			text-align : right;
		}
		
		td {
			padding-left : 20px;
		}

		/* Button Login*/
		input.normal_login, input.up_login, input.out_login{
			width : 97;
			height : 28;
			background : url("images/button/login.gif") no-repeat;
			border-style : none;
			font-family : Arial;
			color : navy;
		}

		input.over_login, input.down_login{
			width : 97;
			height : 28;
			background : url("images/button/login_r.gif") no-repeat;
			border-style : none;
			font-family : Arial;
			color : white;
		}

		/* Button Reset */
		input.normal_reset, input.up_reset, input.out_reset{
			width : 97;
			height : 28;
			background : url("images/button/reset.gif") no-repeat;
			border-style : none;
			font-family : Arial;
			color : navy;
		}

		input.over_reset, input.down_reset{
			width : 97;
			height : 28;
			background : url("images/button/reset_r.gif") no-repeat;
			border-style : none;
			font-family : Arial;
			color : white;
		}


	-->
	</style>

</head>
<body onload="init();IE6check();" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<form name="form_main" method="post" action="">
<table width="600" border="0" align="center" bgcolor="white" height="100%" cellpadding="0" cellspacing="0">
	<tr>
	<td align="center">
	<h1 style="margin-bottom:0">
<img src="images/login_title.jpg">

	</h1>
	<table width="321" style="margin-bottom:50">
	</table>
	<table id="tableForm" style="display:block;">
		<tr>
			<th>ユーザー名：</th>
			<td>
				<input name="user" type="text" value="">
			</td>
		</tr>
		<tr>
			<th>パスワード：</th>
			<td>
				<input name="password"  type="password" value="">
			</td>
		</tr>
		<tr>
<%
	String fileName="login.ini";
	String strPath=application.getRealPath(request.getServletPath());
	if(System.getProperty("os.name").substring(0,4).equals("Wind")) {
		strPath=strPath.substring(0,strPath.lastIndexOf("\\") + 1)+fileName;
	} else {  
		strPath=strPath.substring(0,strPath.lastIndexOf("/") + 1)+fileName;
	}

	boolean dispListFLG = true; // コネクションの選択リストを表示するか？

	BufferedReader br = null;

	try{
		FileInputStream fis = new FileInputStream(strPath);
		InputStreamReader isr = new InputStreamReader(fis,"Shift_JIS");
		br = new BufferedReader(isr);

		String line="";
		String UseList="";
		String ListName="";
		String SchemaName="";
		String PoolName="";

		line = br.readLine();
		int selectCount = 0;
		while(line != null){
			line = br.readLine();
			if (line!=null || ("").equals(line)){

				if(line.indexOf("USE_LIST")==0){
					UseList=line.substring(9);
					if ("true".equals(UseList)) {
						if(selectCount == 0) { // 初期の場合のみ表示
							out.println("<th>接続先：</th>");
							out.println("<td>");
							out.println("<select name='connectSource' id='connectSource' useList='"+UseList+"'>");
							selectCount++;
						}
					} else if ("false".equals(UseList)) { // UES_LISTにfalseが設定されていた
						dispListFLG = false;
						setConnectInfo(request);	// セッションにデフォルト値を登録
						break;
					}

				}
				
				if(line.indexOf("LIST_NAME")==0){
					ListName=line.substring(10);
				}
				if(line.indexOf("SCHEMA_NAME")==0){
					SchemaName=line.substring(12);
				}
				if(line.indexOf("POOL_NAME")==0){
					PoolName=line.substring(10);

					// エラーチェックを行い、エラー時はセッションにデフォルト値を登録する
					if ((ListName == null) || (SchemaName == null) || (PoolName == null)) {
						setConnectInfo(request);
						out.println("<option schemaName='' poolName=''></option>");
						break;
					}

					out.println("<option schemaName='"+SchemaName+"' poolName='"+PoolName+"'>"+ListName+"</option>");
				}


			}

		}

		br.close();

	}catch(FileNotFoundException e){
		setConnectInfo(request);	// セッションにデフォルト値を登録
		dispListFLG = false;
	}catch(IOException e){
		setConnectInfo(request);	// セッションにデフォルト値を登録
		dispListFLG = false;
		e.printStackTrace();
	}catch(Exception e) {
		setConnectInfo(request);	// セッションにデフォルト値を登録
		dispListFLG = false;
		e.printStackTrace();
	}

	if (dispListFLG) {
		out.println("</select>");
		out.println("</td>");
		out.println("</tr>");
	}
%>

	</table>

	<div id="divSubmitBtn" style="display;block;" class="command">
		<input type="button" name="" value="" class="normal_login" onClick="login();" onMouseOver="className='over_login'" onMouseDown="className='down_login'" onMouseUp="className='up_login'" onMouseOut="className='out_login'">
		<input type="button" name="" value="" class="normal_reset" onClick="resetText();" onMouseOver="className='over_reset'" onMouseDown="className='down_reset'" onMouseUp="className='up_reset'" onMouseOut="className='out_reset'">
	</div>
</td>
</tr>
</table>

	<!-- 接続先情報送信用エリア -->
	<input type="hidden" name="schemaName" id="schemaName" value="">
	<input type="hidden" name="poolName" name="poolName" value="">

</form>
</body>
</html>

<script language="JavaScript">

	var errorMessage = "<%= loginErrorMessage %>";
	var dispListFLG = <%= dispListFLG %>;

	function IE6check(){
		if(navigator.appVersion.indexOf("MSIE 6")==-1){
			document.all.tableForm.style.display="none";
			document.all.divSubmitBtn.innerHTML="OpenOLAPのサポートブラウザは　Microsoft Internet Explorer 6　となっております。";
		}
	}

	function init() {
		document.form_main.user.focus();
		document.form_main.user.select();

		if (errorMessage != "") {
			showErrorMessage(errorMessage);
		}
	}

	function showErrorMessage(message) {
		alert(message);
	}

	function login() {

		if ( ( document.form_main.user.value == "" ) && ( document.form_main.password.value == "" ) ) {
			showMessage("8");
			document.form_main.user.focus();
			document.form_main.user.select();
			return;
		} else if ( document.form_main.user.value == "" ) {
			showMessage("9");
			document.form_main.user.focus();
			document.form_main.user.select();
			return;
		} else if( document.form_main.password.value == "" ) {
			showMessage("10");
			document.form_main.password.focus();
			document.form_main.password.select();
			return;
		}

		// コネクション情報を設定
		if(dispListFLG == true) {
			
			var selectedOption = document.form_main.connectSource.options[document.form_main.connectSource.selectedIndex];
			var schemaNameString = selectedOption.schemaName;
			var poolNameString   = selectedOption.poolName;

			document.form_main.schemaName.value = schemaNameString;
			document.form_main.poolName.value   = poolNameString;

		} else {
			// クライアントからは、コネクション情報は送信しない
		}

		document.form_main.action = "Controller?action=login";
		document.form_main.target = "_self";
		document.form_main.submit();
	}


	function resetText() {
		document.form_main.user.value = "";
		document.form_main.password.value = "";

		document.form_main.user.focus();
		document.form_main.user.select();
	}

</script>
