//******************************************************************************
//フォームからの入力値より、ＰＨＰ経由でライブドア天気情報を取得
//------------------------------------------------------------------------------
function WeatherForm() {
  var html = '<div id="weather">';
  html += '<div id="weatherparm" class="f1214">';
  html += '  <form name="fm">';
  html += '    <div id="slctcity">';
  html += '      <select name="keycity" onchange="WeatherSearch();">';
  html += '        <option value="77">滋賀県　大津</option>';
  html += '        <option value="78">滋賀県　彦根</option>';
  html += '        <option value="79">京都府　京都</option>';
  html += '        <option value="80">京都府　舞鶴</option>';
  html += '        <option value="81" selected="selected">大阪府　大阪</option>';
  html += '        <option value="82">兵庫県　神戸</option>';
  html += '        <option value="83">兵庫県　豊岡</option>';
  html += '        <option value="84">奈良県　奈良</option>';
  html += '        <option value="85">奈良県　風屋</option>';
  html += '        <option value="86">和歌山県　和歌山</option>';
  html += '        <option value="87">和歌山県　潮岬</option>';
  html += '      </select>';
  html += '    </div>';
  html += '    <div id="slctday">';
  html += '      <select name="keyday" onchange="WeatherSearch();">';
  html += '        <option value="today" selected="selected">今日の天気</option>';
  html += '        <option value="tomorrow">明日の天気</option>';
  html += '        <option value="dayaftertomorrow">あさっての天気</option>';
  html += '      </select>';
  html += '    </div>';
  html += '  </form>';
  html += '</div>';
  html += '<div class="box160_1_tp mg_tp_9 mg_lt_9 mg_rt_9 mg_bm_9">';
  html += '  <div class="box160_1_bm">';
  html += '    <div class="box160_1_md">';
  html += '<div id="result"></div>';
  html += '    </div>';
  html += '  </div>';
  html += '</div>';
  html += '</div>';
  document.write(html);
}
//------------------------------------------------------------------------------
function WeatherSearch() {
// 非同期通信を行うためのXMLHttpRequestオブジェクトを生成
  try { WeatherReq = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch(e) { WeatherReq = new XMLHttpRequest(); }
// サーバーからの応答時の処理を定義（結果をページへ反映）
  WeatherReq.onreadystatechange = function() {
    var msg = document.getElementById("result");
    if (WeatherReq.readyState == 4) {
      if (WeatherReq.status == 200) {
        var ctt="";
//responseXMLにてサーバーからの応答をXMLDocumentオブジェクトとして取得
        var xmldoc=WeatherReq.responseXML.documentElement;
        var doctitle = getNodeValue(xmldoc, "title",0);
        var doctelop = getNodeValue(xmldoc, "telop",0);
        var imgtitle = getNodeValue(xmldoc, "title",1);
        var imgurl   = getNodeValue(xmldoc, "url",0);
        var maxtemp  = getNodeValue(xmldoc, "celsius",0);
        var mintemp  = getNodeValue(xmldoc, "celsius",1);
        var title    = doctitle.split(" ");
//        ctt += '<p class="basyo f1614">' + title[0] + '　' + title[1] + '</p>';
//        ctt += '<p class="title f1214">' + title[3] + '</p>';
        ctt += '<p class="tenki f1614">' + doctelop + '</p>';
        ctt += '<p class="tkimg"><img class="tkimg" src="' + imgurl + '" alt="' + imgtitle + '"/></p>';
        ctt += '<p class="maxod f1214">最高気温<span class="span1">：</span><span class="span2">' + maxtemp + '℃</span></p>';
        ctt += '<p class="minod f1214">最低気温<span class="span1">：</span><span class="span2">' + mintemp + '℃</span></p>';
        msg.innerHTML=ctt;
      } else { msg.innerHTML='<p class="error f1414">通信に失敗しました。</p>'; }
    } else { msg.innerHTML='<p class="rding f1414">通信中…</p>'; }
  }
// サーバーとの通信を開始
  WeatherReq.open("GET","http://zion-futsal-club.tattin.com/weather/weather.php?keycity=" + encodeURI(document.fm.keycity.value) + "&keyday=" + encodeURI(document.fm.keyday.value), true);
  WeatherReq.send(null);
}
//------------------------------------------------------------------------------
// ノードcurrent配下に含まれる要素nameのテキスト値を取得する関数
//------------------------------------------------------------------------------
function getNodeValue(current ,name,itino){
// getElementsByTagName：書内の指定したタグすべての配列を作る
// 配列なので[0]で先頭ノードにアクセスする
  var nodes=current.getElementsByTagName(name);
  var node=nodes[itino];
  var txtNode=node.firstChild;
  if (txtNode){
    return txtNode.nodeValue;
  } else {
    return "--";
  }
}
//------------------------------------------------------------------------------
// 半角数字を全角数字に置き換える関数
//------------------------------------------------------------------------------
function chgNum(bfstring){
  var zennum = new Array("０", "１", "２", "３", "４", "５", "６", "７", "８", "９");
  for (l=0; l<bfstring.length; l++) {
    for (i=0; i<10; i++) {
      bfstring = bfstring.replace(i,zennum[i]);
    }
  }
  return bfstring;
}
