var SearchCity = function() {};

SearchCity.src_zip = "";
SearchCity.src_pref = "";
SearchCity.dst_pref = "";
SearchCity.dst_city = "";
SearchCity.dst_city_num = 0;
SearchCity.dst_address = "";

SearchCity.defaultChecked = null;
SearchCity.columnSize = 3;
//都道府県リストが選択された時に市区町村を挿入したチェックボックス群を生成する
SearchCity.changeCityBox = function(src_pref,dst_city,defaultChecked,columnSize) {
    SearchCity.columnSize = columnSize || '3';
    SearchCity.defaultChecked = defaultChecked || new Array();
    SearchCity.src_pref = src_pref;
    SearchCity.dst_city = dst_city;
    SearchCity._getResult(SearchCity._callBack_changeCityBox);
}

//コールバック
SearchCity._callBack_changeCityBox = function(request) {
    var eval_obj = eval(request.responseText);
    if(eval_obj==null){
        return;
    }
//	console.log("_callBack_changeCityBox kita----");
    //テーブル＆チェックボックス軍作成
    CityBoxController._createCityBox(SearchCity.src_pref,SearchCity.dst_city ,eval_obj,SearchCity.defaultChecked,SearchCity.columnSize);
    //テーブル＆チェックボックス群の高さセット
    CityBoxController._setHeight(SearchCity.src_pref);
}

//都道府県リストが選択された時に市区町村を挿入したチェックボックス群を生成する(一画面に2つ在る場合用)
SearchCity.ReqchangeCityBox = function(req_src_pref,req_dst_city,req_defaultChecked) {
    SearchCity.req_defaultChecked = req_defaultChecked || new Array();
    SearchCity.req_src_pref = req_src_pref;
    SearchCity.req_dst_city = req_dst_city;
    SearchCity._req_getResult(SearchCity._callBack_ReqchangeCityBox);
}

//コールバック
SearchCity._callBack_ReqchangeCityBox = function(request) {
    var req_eval_obj = eval(request.responseText);
    if(req_eval_obj==null){
        return;
    }
    //console.log("_callBack_changeCityBox kita----");
    //テーブル＆チェックボックス軍作成
    ReqCityBoxController._createCityBox(SearchCity.req_src_pref,SearchCity.req_dst_city ,req_eval_obj,SearchCity.req_defaultChecked);

    if( $(SearchCity.req_src_pref).value == '' || $(SearchCity.req_src_pref).value == '0' ){
        $('city_selector2').style.height = 'auto';
    } else {
        $('city_selector2').style.height = '143px';
    }

}


//住所を自動で入力ボタンが押下された時に都道府県リストと市区町村リストと番地・建物名等を更新する
SearchCity.changeCityByZipCode = function(src_zip,dst_pref,dst_city,dst_address) {
    SearchCity.src_zip = src_zip;
    SearchCity.dst_pref = dst_pref;
    SearchCity.dst_city = dst_city;
    SearchCity.dst_address = dst_address;
    SearchCity._getResultByZipCode();
}
//データ取得
SearchCity._getResultByZipCode = function() {

    //郵便番号を抽出(000-0000の-は取り除く)
    var _zip = $(SearchCity.src_zip).value.replace('-','');
    //console.log(_zip);
    //ボタンの二度押しに対応するため非同期をやめる
    //var myAjax = new Ajax.Request("/?act=public_getaddress", {method: "post", parameters: "zip[]="+_zip, onComplete: SearchCity.Callback_getResultByZipCode});
    //同期版
    var myAjax = new Ajax.Request("/?act=public_getaddress", {method: "post", asynchronous:false, parameters: "zip[]="+_zip});
    SearchCity.Callback_getResultByZipCode(myAjax.transport);
}
//コールバック
SearchCity.Callback_getResultByZipCode = function(request) {
    var eval_obj_data = eval(request.responseText);
    var eval_obj = eval_obj_data[0];
    if(eval_obj==null){
        return;
    }
    //eval_obj[0]の中身
    //city_order		sys_are_city_order(sys_are_city_detailテーブル)
    //city_id			sys_are_city_id(sys_are_cityテーブル)
    //city_name			sys_are_city_name(sys_are_city_detailテーブル)
    //city_pref			sys_are_city_pref(sys_are_city_detailテーブル)
    //city_detail		sys_are_city_detail(sys_are_city_detailテーブル)
    //都道府県をセットする
    $(SearchCity.dst_pref).selectedIndex = eval_obj[0].city_pref;
    //市区町村をセットする（SearchCity.Callbackで処理が行われる）
    SearchCity.dst_city_num = eval_obj[0].city_order;

    //ボタンの二度押しに対応するため非同期をやめる
    //var myAjax2 = new Ajax.Request("/?act=public_getareacity", {method: "post", parameters: "pref[]="+eval_obj[0].city_pref, onComplete: SearchCity.Callback});
    //同期版
    
    //Begin #35 repaired by tuanpv [2011/06/22]
   // var myAjax2 = new Ajax.Request("/?act=public_getareacity", {method: "post", asynchronous:false, parameters: "pref[]="+eval_obj[0].city_pref});
   // SearchCity.Callback(myAjax2.transport);
    
    if(eval_obj[0].city_pref > 0) {
        var eval_obj_city = eval_obj_data[1];
        if(eval_obj_city != null){
            
            //選択された都道府県に該当した市区町村をセット
            SearchCity._setOptions(eval_obj_city, SearchCity.dst_city);
            SearchCity.setInputCityText(SearchCity.src_city,'mem_city_id_text');
        }
    }
    //End #35 

    //市区町村以下の情報(city_detail)があればそれを番地・建物名にセットする
    $(SearchCity.dst_address).value = eval_obj[0].city_detail;

    //city_idをcookieに登録
    var expires = new Date();
    expires.setTime(expires.getTime() + (60*30*1000));
    document.cookie = 'mem_city_id' + '=' + eval_obj[0].city_id + ';expires=' + expires.toGMTString();


}

//都道府県リストが選択された時に市区町村リストを更新する
SearchCity.changeCityList = function(src_pref,dst_city) {
    SearchCity.src_pref = src_pref;
    SearchCity.dst_city = dst_city;
    SearchCity._getResult(SearchCity.Callback);
}

SearchCity.changeCityListSync = function(src_pref,dst_city) {
    SearchCity.src_pref = src_pref;
    SearchCity.src_city = dst_city;
    SearchCity.dst_city = dst_city;
    var _pref = $(SearchCity.src_pref).value;
    var myAjax = new Ajax.Request("/?act=public_getareacity", {method: "post", asynchronous:false, parameters: "pref[]="+_pref});

    SearchCity.Callback(myAjax.transport);
}

//チェックボックス生成
SearchCity.changeCityBoxSync = function(src_pref,dst_city,defaultChecked,columnSize) {
    SearchCity.columnSize = columnSize || '3';
    SearchCity.defaultChecked = defaultChecked || new Array();
    SearchCity.src_pref = src_pref;
    SearchCity.dst_city = dst_city;
    var _pref = $(SearchCity.src_pref).value;
    var myAjax = new Ajax.Request("/?act=public_getareacity", {method: "post", asynchronous:false, parameters: "pref[]="+_pref});

    SearchCity._callBack_changeCityBox(myAjax.transport);
}


//データ取得
SearchCity._getResult = function( callback ) {

    //httpRequestオブジェクト生成
    var _pref = $(SearchCity.src_pref).value;
    var myAjax = new Ajax.Request(
                        "/?act=public_getareacity",
                        {   method: "post",
                            parameters: "pref[]="+_pref,
                            onComplete: callback
                        },false);
}

//データ取得２つ存在する場合
SearchCity._req_getResult = function( callback ) {

    //httpRequestオブジェクト生成
    var _req_pref = $(SearchCity.req_src_pref).value;
    var myAjax = new Ajax.Request("/?act=public_getareacity", {method: "post", parameters: "pref[]="+_req_pref, onComplete: callback},false);
}

//コールバック
SearchCity.Callback = function(request) {
    var eval_obj = eval(request.responseText);
    if(eval_obj==null){
        return;
    }
    //選択された都道府県に該当した市区町村をセット
    SearchCity._setOptions(eval_obj,SearchCity.dst_city);
    SearchCity.setInputCityText(SearchCity.src_city,'mem_city_id_text');
}

//optionクリア
SearchCity._clearOptions = function(dst_id) {
      var obj = $(dst_id);
      while(obj.options!=null && obj.options.length > 0){
        obj.options[obj.length - 1] = null;
      }
      obj.options[obj.length] = new Option("市区町村を選択してください", "0", true, false);
}

//optionセット
SearchCity._setOptions = function(eval_obj,dst_city){
    //一度クリアする
    SearchCity._clearOptions(dst_city);
    var dst_obj = $(dst_city);
    eval_obj.each(
            function(val,idx){
                //valに(sys_are_)city_idと(sys_are_)city_nameが入っている
                //idxにsys_are_city_orderの中身が入っている
                //optionを追加していく
//				dst_obj.options[dst_obj.length] = new Option(val.city_name,(idx+1).toString());
                dst_obj.options[dst_obj.length] = new Option(val.city_name,val.city_id);
            }
    );
    //SearchCity.dst_city_numが０以外ならその番号に該当する市区町村を表示状態にする
    if(SearchCity.dst_city_num != 0){
        $(SearchCity.dst_city).selectedIndex = SearchCity.dst_city_num;
        SearchCity.dst_city_num = 0;
    }
}

SearchCity.setInputCityText = function(src_city,dst_input) {
//	console.log("setInputCityText");
    if( $(src_city) == null || $(dst_input) == null) return;
    //選択された市区町村の文字列を取得して、hiddenのinputタグにして挿入する
    var _city = $(src_city).options[$(src_city).selectedIndex].innerHTML;
    if($(src_city).value==0){
        _city = "";
    }

    $(dst_input).setAttribute('value',_city);
//	console.log("set city = "+_city);
}

SearchCity.setSelectedIndex = function(select_city,_value) {
//	console.log("setSelectedIndex value = " + _value);
    for(index=0;index<$(select_city).options.length;index++){
        if($(select_city).options[index].value==_value){
            $(select_city).selectedIndex = index;
            break;
        }
    }
}
SearchCity.setSelectedOptions = function(city,_values) {
    for( var i=0; i<_values.length; i++ ){
        for( var index=0; index<$(city).options.length; index++ ){
            if($(city).options[index].value ==_values[i]){
                $(city).options[index].selected = 'selected';
                break;
            }
        }
    }
}

//checkboxクリア
SearchCity._clearCheckbox = function(dst_id) {
      var obj = $(dst_id);
      while(obj.ElementById!=null && obj.options.length > 0){
        obj.ElementById[obj.length - 1] = null;
      }
      obj.ElementById[obj.length] = new Option("市区町村を選択してください", "0", true, false);
}

//checkboxセット
SearchCity._setCheckbox = function(eval_obj,dst_city){
    //一度クリアする
    SearchCity._clearCheckbox(dst_city);
    var dst_obj = $(dst_city);
    eval_obj.each(
            function(val,idx){
                //valに(sys_are_)city_idと(sys_are_)city_nameが入っている
                //idxにsys_are_city_orderの中身が入っている
                //optionを追加していく
                dst_obj.ElementById[dst_obj.length] = new Option(val.city_name,val.city_id);
            }
    );
    //SearchCity.dst_city_numが０以外ならその番号に該当する市区町村を表示状態にする
    if(SearchCity.dst_city_num != 0){
        $(SearchCity.dst_city).selectedIndex = SearchCity.dst_city_num;
        SearchCity.dst_city_num = 0;
    }
}

//act=public_viewfeature専用
SearchCity.setCity = function(src_pref,dst_city) {
    var _pref = $(src_pref).value;

    if(_pref.length == 0)return;

    var myAjax = new Ajax.Request("/?act=public_getareacity", {method: "post", asynchronous:false, parameters: "pref[]="+_pref});

    //selectのoptionを全部削除
    var obj = $(dst_city);
    while(obj.options!=null && obj.options.length > 0){
        obj.options[obj.length - 1] = null;
    }
    //ひとつめには「こだわらない」をセット
    obj.options[obj.length] = new Option("こだわらない", "no", true, false);
    var eval_obj = eval(myAjax.transport.responseText);
    eval_obj.each(
            function(val,idx){
                //valに(sys_are_)city_idと(sys_are_)city_nameが入っている
                //idxにsys_are_city_orderの中身が入っている
                //optionを追加していく
//				dst_obj.options[dst_obj.length] = new Option(val.city_name,(idx+1).toString());
                obj.options[obj.length] = new Option(val.city_name,val.city_id);
            }
    );
}

/**
 * 全角半角変換
 */
SearchCity.toHankakuNum = function(motoText) {
  han = "0123456789.,-+";
  zen = "０１２３４５６７８９．，−＋";
  str = "";
  for (i=0; i<motoText.length; i++) {
    c = motoText.charAt(i);
    n = zen.indexOf(c,0);
    if (n>= 0) c = han.charAt(n);
    str += c;
  }
  return str;
}



