/*--------------------------------------
 *  郵便番号検索用 JavaScript
 *  (c) 2009-2009 Fujimori Production
 *--------------------------------------*/

/*
 * 郵便番号から住所表示
 */
function dispZipAddress(baseURL) {
  // 一覧画面を消去
  $('ziplist').hide();
  if (!($('townlist') == null || $('townlist') == undefined)) {
    $('townlist').hide();
  }

  // 入力した郵便番号を受入
  var zip = $F('zip');
  if (zip == '') {
    return;
  }
  // 郵便番号の"-"を削除
  zip = zip.replace("-", "");
  // 郵便番号の桁数をチェック
  if (zip.length == 7) {
    // 7桁で検索
    getZipAddress(baseURL, zip);
  } else if (zip.length > 2) {
    // 一覧表示
    getZipList(baseURL, zip);
  } else {
    return;
  }
}

/*
 * 郵便番号から住所を取得(ajax)
 */
function getZipAddress(baseURL, zip) {
  var url = baseURL + "/zip/search/getzipaddress";
  var parm = "zip=" + zip;

  // 郵便番号データ取得
  new Ajax.Request(
      url, {
           method: "post",
           parameters: parm,
           onSuccess: function(transport){
               // JSON データを取得
               var json = transport.responseText.evalJSON();
               // 複数件存在する場合
               if (json.count > 1) {
                 getZipList(baseURL, zip);
               }
               // 表示
               if(zip.length == 7 && zip.substr(3, 1) != "-") {
                 $("zip").value  = zip.substr(0, 3) + "-" + zip.substr(3, 4);
               }
               $("pref").value = json.pref;
               if ($("city") == undefined && $("town") == undefined) {
                 $("cityTown").value = json.city + json.town;
               } else {
                 $("city").value = json.city;
                 $("town").value = json.town;
               } 
           }, 

           onFailure : ajaxShowErrorMsg
      }
  );

  function ajaxShowErrorMsg() {
      alert('getZipAddress : エラー発生');
  }

}

/*
 * 郵便番号一覧表示
 */
function getZipList(baseURL, zip) {
  var url = baseURL + "/zip/search/dispzipaddress";
  var parm = "zip=" + zip;
  // 郵便番号一覧表示
  dispZipList(url, parm, 'ziplist');
}

/*
 * 町名(市区郡)から郵便番号一覧を表示
 */
function dispTownZip(baseURL) {
  // 一覧画面を消去
  $('ziplist').hide();
  $('townlist').hide();
  // 郵便番号未入力チェック
  var zip = $F("zip");
  if (zip.length > 6) {
    return;
  }
  // 入力市区郡・町名
  var city = $F("city");
  var town = $F("town");
  if (town == '') {
    return;
  }
  // URL
  var url = baseURL + "/zip/search/dispziptown";
  var parm = "city=" + city + "&town=" + town;
  // 郵便番号一覧表示
  dispZipList(url, parm, 'townlist');
}

/*
 * 郵便番号一覧表示(ajax)
 */
function dispZipList(url, parm, listname) {

  // 郵便番号一覧データ取得
  new Ajax.Request(
      url, {
           method: "post",
           parameters: parm,
           onSuccess: function(request){
             if(request.responseText == ''){
               $(listname).hide();
             }else{
               $(listname).show();
               $(listname).innerHTML = request.responseText;
             }
           },

           onComplete: function(request) {
             // コンテナ内のリンク処理
             $$('#' + listname + ' a').each(function(anchor){
               if(anchor.identify() == 'auto_zip_credit'){
                 return;
               }
               anchor.observe('click', function(){
                 var text = anchor.innerHTML;
                 var a = text.split(' ');
                 if(a[0]) $("zip").value  = a[0].substr(0, 3) + "-" + a[0].substr(3, 4);
                 if(a[1]) $("pref").value = a[1];
                 if ($("city") == undefined && $("town") == undefined) {
                   if(a[2]) $("cityTown").value = a[2] ;
                   if(a[3]) $("cityTown").value = $("cityTown").value + a[3];
                 } else {
                   if(a[2]) $("city").value = a[2];
                   if(a[3]) $("town").value = a[3];
                 }
                 $(listname).hide();
               });
             });
           },

           onFailure : ajaxShowErrorMsg
      }
  );

  function ajaxShowErrorMsg() {
      alert('dispZipList : エラー発生');
  }
}

