<%@ include file="/include/in_Import.jsp" %>
<%@ include file="/include/in_Function.jsp" %>
<jsp:useBean id="sutil" class="kr.co.slight.jinhae.util.GYUtil" scope="page" />
<%@ page contentType="text/html;charset=euc-kr" %>

<%
	String textfield = request.getParameter("textfield");
	textfield = sutil.toEucKr(textfield);
	if(textfield == null) textfield = "";
%>

<%!
	static {
        try {
            new kr.co.slight.jinhae.pool.JDCConnectionDriver("org.postgresql.Driver","jdbc:postgresql:hanmaum","postgres","");
        } catch(Exception e) {
           	System.out.println("<br><br>JDCConnectionDriver Error : " + e.toString());
        }
	}

%>

<%
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	SESSION
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
	String spage	= request.getParameter("spage");
	if (spage == null) spage = "1";

	int iPage = Integer.parseInt(spage);
	
	int totalCnt = 0;	//글의 총 갯수
	int totalPage;		//총 페이지 갯수
	int pageSize = 10;	//한페이지에 보여줄 글의 최대 갯수
	int blockSize = 10;	//한 화면에 보여줄 최대 페이지 갯수
	int blockPage;		//한 화면에 보여지는 페이지 중 첫번째 페이지 번호
	
	int start_no	= (iPage-1)*pageSize + 1;	//해당 페이지의 첫번째 row number
	int end_no		= iPage*pageSize;			//해당 페이지의 마지막 row number(최대값)
	
	Connection		conn		= null;
	Statement		stmt		= null;
	ResultSet		rs			= null;
	ResultSet		rtList		= null;

	try {
	    StringBuffer	query		= new StringBuffer();
	    StringBuffer	query_cnt	= new StringBuffer();

		query_cnt.append("select count(*) from zip_code");		
		if(textfield != null && !textfield.equals("")) {
			query_cnt.append(" where address like '%"+ textfield +"%' ");
		} else {
			query_cnt.append(" where 1 = 0 ");
		}
	
		query.append("select * from zip_code where address like '%"+ textfield +"%'");

		query.append(" limit ").append(pageSize).append(" offset ").append(start_no-1);

		conn	= DriverManager.getConnection("jdbc:jdc:jdcpool");
		stmt	= conn.createStatement();
		rs	= stmt.executeQuery(query_cnt.toString());
		
		while ( rs.next() ) {
			totalCnt = rs.getInt(1);
		}
		
		rs.close();
		
		totalPage = ((int) (totalCnt - 1)/pageSize ) + 1;
		blockPage = ((int) (iPage -1)/blockSize ) * blockSize + 1;
		
		rtList = stmt.executeQuery(query.toString());
		
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=euc-kr" />

<link rel="stylesheet" href="Common/hstyle.css" type="text/css">
<title>우편번호 검색</title>

<script language="javascript">
<!--
	function onSearch() {
		var frm = document.form1;

		frm.action = "post_search.jsp";
		frm.target = "_self";
		frm.submit();
	}

	function bbsList(spage, keytype, keyword) {
		location.href="post_search.jsp?spage=" + spage + "&keytype=" + keytype + "&textfield=" + keyword;
		return;
	}

	function onSetPostNo(postNo, addr) {
		opener.onSetPostNo(postNo,addr);
		self.close();
		
	}

	function onLoad() {
		document.form1.textfield.focus();
	}
//-->
</script>
</head>

<STYLE type=text/css>
	body {
		background-color:#ffffff;
		margin: 13px 0px;
		text-align:center;
	}
-->
</STYLE>

<body onload="javascript:onLoad();">
<form name="form1" method="post">
<table width="448" border="0" cellspacing="0" cellpadding="0">
	<tr>
		<td><img src="Image/ha_0sc05.gif" width="448" height="62"></td>
	</tr>
	<tr>
		<td height="200" align="center" valign="top" background="Image/ha_0sc09.gif"><br>
			<table width="90%" border="0" cellspacing="0" cellpadding="0">
				<tr>
					<td height="30">찾고자 하는 주소의 동/읍/면 이름을 입력하세요.</td>
				</tr>
				<tr>
					<td id="ma1">&nbsp;</td>
				</tr>
				<tr>
					<td>
						<table width="100%" border="0" cellspacing="0" cellpadding="0" style="padding:4 0 0 10">
							<tr>
								<td height="2" colspan="2" background="Image/ha_backc01.gif"></td>
							</tr>
							<tr>
								<td width="50%" height="30">동(읍/면)</td>
								<td width="50%">
									<input name="textfield" type="text" id="textfield" value="<%=textfield%>" size="20">
									<a href="javascript:onSearch();">
										<img src="Image/ha_but_s07.gif" width="36" height="17" align="absmiddle" border="0" />
									</a>
								</td>
							</tr>
							<tr>
								<td height="2" colspan="2" background="Image/ha_backc01.gif"></td>
							</tr>          
						</table>
					</td>
				</tr>
				<tr>
					<td>&nbsp;</td>
				</tr>
			</table>

			<table width="90%" border="0" cellspacing="0" cellpadding="0">
<%
	if (totalCnt > 0) {

		int i = totalCnt;
		int no = 0;

		String postNo = "";
		String address = "";
		String detail = "";

		while (rtList.next()) {			    
			postNo	= rtList.getString("zip_code");								
			address	= rtList.getString("address");
			detail = rtList.getString("detail");
%>			
				<tr>
					<td width="16%" height="30"><%= postNo.substring(0, 3) %> - <%= postNo.substring(3, 6) %></td>
					<td width="84%" align="left">
						<a href="javascript:onSetPostNo('<%=postNo%>','<%= address %><%=detail%>');">&nbsp;<%= address %>&nbsp;<%=detail%></a>
					</td>
				</tr>
<%
		}
	} else {
%>
				<tr>
					<td colspan="2" align="center">주소가 없습니다.</td>
				</tr>
<% 
	} 
%>
			</table>
		</td>
	</tr>
	<tr>
		<td height="26" align="center" background="Image/ha_0sc09.gif">
<%
//이전 페이지
		if ( iPage == 1 ) {
%>        
						<img src="/images/ic_spre.gif" width="6" height="8">
<%
		} else {
%>
		                <a href="javascript:bbsList('<%=iPage-1%>', '', '<%=textfield%>')";>
							<img src="/images/ic_spre.gif" width="6" height="8" border="0">
						</a>
<%
		}
//	페이지 Loop
		int loopSize = Math.min(blockPage + blockSize, totalPage + 1);
	
		for ( int j=blockPage; j<loopSize ; j++ ) {
			if (spage.equals(j+"")) {
%>                      
			<a href="javascript:bbsList('<%=j%>', '', '<%=textfield%>')";><strong>[<%=j%>]</strong></a>
<%
			} else {
%>                      
			<a href="javascript:bbsList('<%=j%>', '', '<%=textfield%>')";>[<%=j%>]</a>	
<%
			}
		}

/*-----------------------------------------
	다음 페이지
-----------------------------------------*/
		if ( iPage == totalPage ) {
%>
                                <img src="/images/ic_snex.gif" width="6" height="8">
<%
		} else {
%>
			<a href="javascript:bbsList('<%=iPage+1%>', '', '<%=textfield%>')";><img src="/images/ic_snex.gif" width="6" height="8" border="0"></a>
<%
		}
%>
		</td>
	</tr>
	<tr>
		<td height="26" align="center" background="Image/ha_0sc09.gif">
			<a href="javascript:self.close();">
				<img src="Image/ha_but_l41.gif" width="59" height="21" border="0" />
			</a>
		</td>
	</tr>
	<tr>
		<td><img src="Image/ha_0sc08.gif" width="448" height="11"></td>
	</tr>
</table>
<br />

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

<%
		if ( rs != null ) rs.close();
		if ( stmt != null ) stmt.close();
		if ( conn != null ) conn.close();

	} catch (Exception e) {

		if ( rs != null ) rs.close();
		if ( stmt != null ) stmt.close();
		if ( conn != null ) conn.close();

		// 에러페이지로 이동............
		out.println("JSP >> Exception : " + e.toString());
	}
%>