<%@ LANGUAGE=VBScript %>
<%
Option Explicit
%>
<html>
<%
'----------------------------------------
'共通変数宣言
'----------------------------------------
'---- CursorTypeEnum Values ----
Const adOpenForwardOnly = 0     '前方のみ
Const adOpenKeyset = 1          '通常
Const adOpenDynamic = 2         '厳重
Const adOpenStatic = 3          '静的(CursorLocation←adUseClient)
'---- LockTypeEnum Values ----
Const adLockReadOnly = 1        '読み取り専用
Const adLockPessimistic = 2     '排他的
Const adLockOptimistic = 3      '共有的
Const adLockBatchOptimistic = 4 '共有的バッチ更新
'---- CursorLocationEnum Values ----
Const adUseClient = 1
Const adUseServer = 2
Const adUseClientBatch = 3
'データベース
Const cns_DBSource = "yuubintest"
Const cns_DNS = "dsn=yuubintest"
'画面
Public p_Yuubin
'----------------------------------------
'メインルーチン
'----------------------------------------

'前処理
m_MAE

'HTMLソースの書き出し
m_HTML

'後処理
m_ATO

'----------------------------------------
'サブルーチン
'----------------------------------------

Sub m_MAE()
'----------------------------------------
'前処理
'----------------------------------------

    'データベースに接続
    Set session(cns_DBSource) = Server.CreateObject("ADODB.Connection")
    session(cns_DBSource).Open cns_DNS
    session(cns_DBSource).CursorLocation = adUseServer

    '郵便番号の取得
    p_Yuubin = Request.Form("yuubin")

End Sub

Sub m_HTML()
'----------------------------------------
'HTMLソースの書き出し
'----------------------------------------
    Dim w_SQL, w_RS

    Response.write "<body>" & vbCr

    '本文出力
    Response.write "<form name=""form1"" onsubmit=""return form1_Check(this)"" action=""index.asp"" method=""post"">" & vbCr
    Response.write "郵便番号" & vbCr
    Response.write "<input type=""text"" name=""yuubin"" value=""" & p_Yuubin & """ size=10>" & vbCr
    Response.write "<input type=""submit"" class=""btn1"" value=""入 力"">" & vbCr
    Response.write "</form>" & vbCr
    'フォーカス位置付け
    Response.write "<SCRIPT language=JavaScript><!--" & vbCr
    Response.write "form1.yuubin.focus();" & vbCr
    Response.write "//--></SCRIPT>" & vbCr
    'エラーチェック
    Response.write "<SCRIPT language=JavaScript><!--" & vbCr
    Response.write "function form1_Check(theForm)" & vbCr
    Response.write "{" & vbCr
    Response.write "if (theForm.yuubin.value == """")" & vbCr
    Response.write "  {" & vbCr
    Response.write "  alert(""郵便番号は必ず指定して下さい。"");" & vbCr
    Response.write "  theForm.yuubin.focus();" & vbCr
    Response.write "  return (false);" & vbCr
    Response.write "  }" & vbCr
    Response.write "if (theForm.yuubin.value.length < 3)" & vbCr
    Response.write "  {" & vbCr
    Response.write "  alert(""郵便番号は、3桁以上を指定して下さい。"");" & vbCr
    Response.write "  theForm.yuubin.focus();" & vbCr
    Response.write "  return (false);" & vbCr
    Response.write "  }" & vbCr
    Response.write "if (theForm.yuubin.value.length > 8)" & vbCr
    Response.write "  {" & vbCr
    Response.write "  alert(""郵便番号は、XXX-XXXX(8桁以内)の形式で指定して下さい。"");" & vbCr
    Response.write "  theForm.yuubin.focus();" & vbCr
    Response.write "  return (false);" & vbCr
    Response.write "  }" & vbCr
    Response.write "if ( isNaN(theForm.yuubin.value.charAt(0)) == true ||" & vbCr
    Response.write "     isNaN(theForm.yuubin.value.charAt(1)) == true ||" & vbCr
    Response.write "     isNaN(theForm.yuubin.value.charAt(2)) == true ||" & vbCr
    Response.write "    (theForm.yuubin.value.length > 3 &&" & vbCr
    Response.write "     theForm.yuubin.value.charAt(3) != ""-"" ) ||" & vbCr
    Response.write "     isNaN(theForm.yuubin.value.charAt(4)) == true ||" & vbCr
    Response.write "     isNaN(theForm.yuubin.value.charAt(5)) == true ||" & vbCr
    Response.write "     isNaN(theForm.yuubin.value.charAt(6)) == true ||" & vbCr
    Response.write "     isNaN(theForm.yuubin.value.charAt(7)) == true )" & vbCr
    Response.write "  {" & vbCr
    Response.write "  alert(""郵便番号は、XXX-XXXXの形式で指定して下さい。"");" & vbCr
    Response.write "  theForm.yuubin.focus();" & vbCr
    Response.write "  return (false);" & vbCr
    Response.write "  }" & vbCr
    Response.write "}" & vbCr
    Response.write "//--></SCRIPT>" & vbCr

    If Len(p_Yuubin) >= 3 Then
        '住所の一覧
        '住所テーブルの検索
        w_SQL = "SELECT * FROM 住所"
        w_SQL = w_SQL & " WHERE (郵便番号 between '" & p_Yuubin & "' and '" & p_Yuubin & Chr(255) & "')"
        w_SQL = w_SQL & " order by 郵便番号, 市町村読み, 地区読み"
        Set w_RS = Server.CreateObject("ADODB.Recordset")
        w_RS.Open w_SQL, session(cns_DBSource), adOpenKeyset, adLockReadOnly
        If w_RS.BOF Then
            If p_Yuubin <> "" Then
                Response.write "<span style=""height:18px;filter:glow(color=yellow,STRENGTH=3);"">" & vbCr
                Response.write "登録されていません。" & vbCr
                Response.write "</span>" & vbCr
            End If
        Else
            Response.write "<table border=1 cellspacing=1 cellpadding=2>" & vbCr
            Response.write "<tr bgcolor=""#FFFFCC"">" & vbCr
            Response.write "<td align=""center"">〒</td>"
            Response.write "<td align=""center"">都道府県</td>"
            Response.write "<td align=""center"">市町村</td>"
            Response.write "<td align=""center"">地区</td>" & vbCr
            Response.write "</tr>" & vbCr
            While Not w_RS.EOF
                Response.write "<tr bgcolor=""#FFFFFF"">" & vbCr
                Response.write "<td>" & w_RS.Fields("郵便番号") & "</td>"
                Response.write "<td>" & w_RS.Fields("都道府県") & "</td>"
                Response.write "<td>" & w_RS.Fields("市町村") & "</td>"
                If IsNull(w_RS.Fields("地区")) Then
                    Response.write "<td> </td>" & vbCr
                Else
                    Response.write "<td>" & w_RS.Fields("地区") & "</td>" & vbCr
                End If
                Response.write "</tr>" & vbCr
                w_RS.MoveNext
            Wend
            Response.write "</table>" & vbCr
        End If
        w_RS.Close
        Set w_RS = Nothing
    End If

    Response.write "</body>" & vbCr

End Sub

Sub m_ATO()
'----------------------------------------
'後処理
'----------------------------------------

    'データベースの閉塞
    On Error Resume Next
    session(cns_DBSource).Close
    Set session(cns_DBSource) = Nothing
    On Error GoTo 0

End Sub
%>

</html>