<%@ Page Language="VB" %>

<html>
<head>
<style>
TH {
   TEXT-ALIGN: right
}
SPAN.hidden {
   DISPLAY: none
}
SPAN.error {
   DISPLAY: inline; COLOR: black; BACKGROUND-COLOR: pink
}
</style>
<script>
var req;

function getAddress(postalCode) {
  var url = encodeURI("GetAddress.aspx?p=" + postalCode);
  getAddressCallBack(url);
}

function getAddressCallBack(url) {
  req = getXMLHttpRequest();
  if (req) {
    req.onreadystatechange = getAddressProcessReqChange;
    req.open("GET", url, true);
    req.send();
    window.status = "読み込み中です..."
  }
}

function getAddressProcessReqChange() {
  if (req.readyState == 4) {
    if (req.status == 200) {
      var response = req.responseXML.documentElement;
      var rc = response.getElementsByTagName("rc")[0].firstChild.data;
      var msg = document.getElementById("message");
      if (rc == "1") {
        msg.className = "hidden";
        document.getElementById("txtKen").value =
          response.getElementsByTagName("ken")[0].firstChild.data;
        document.getElementById("txtAddress").value =
          response.getElementsByTagName("city")[0].firstChild.data +
          response.getElementsByTagName("town")[0].firstChild.data;
        window.status = "読み込み完了!";
      } else {
        msg.className = "error";
      }
    } else {
      window.status = req.statusText;
    }
  }
}

function getXMLHttpRequest() {
  var x = null;
	if (typeof XMLHttpRequest != "undefined") {
		x = new XMLHttpRequest();
	} else {
		try {
			x = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				x = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
			}
		}
	}
	return x;
}
</script>
</head>
<body>
    <form runat="server">
        <h3>Ajax3.aspx</h3>
        <table>
            <tbody>
                <tr>
                    <td align="right">
                      〒
                    </td>
                    <td>
                        <asp:TextBox id="txtPostalCode" onblur="getAddress(this.value);" runat="server" Width="7em"></asp:TextBox>
                        <span class="hidden" id="message">郵便番号が不正です...再入力してください.</span>
                    </td>
                </tr>
                <tr>
                    <td align="right">
                        都道府県</td>
                    <td>
                        <asp:TextBox id="txtKen" runat="server" Width="7em"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td align="right">
                        住所
                    </td>
                    <td>
                        <asp:TextBox id="txtAddress" runat="server" Width="20em"></asp:TextBox>
                    </td>
                </tr>
            </tbody>
        </table>
    </form>
</body>
</html>
