var SearchCity = function() {};

SearchCity.src_pref = "";
SearchCity.dst_pref = "";
SearchCity.dst_city = "";
SearchCity.dst_city_num = 0;
SearchCity.dst_address = "";
SearchCity.src_zip = "";


//都道府県リストが選択された時に市区町村リストを更新する
SearchCity.changeCityListSync = function(src_pref,dst_city) {
	SearchCity.src_pref = src_pref;
	SearchCity.dst_city = dst_city;
	var _pref = $(SearchCity.src_pref).value;
	var myAjax = new Ajax.Request("/?act=public_getcitylist", {method: "post", asynchronous:false, parameters: "addr1="+_pref});
	if(_pref == "") {
		$(SearchCity.dst_city).disabled = true;
	}else{
		$(SearchCity.dst_city).disabled = false;
	}
	SearchCity.Callback(myAjax.transport);
}

//コールバック
SearchCity.Callback = function(request) {
	var eval_obj = eval(request.responseText);
	//選択された都道府県に該当した市区町村をセット
	SearchCity._setOptions(eval_obj,SearchCity.dst_city);
}
//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.addr2,val.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.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.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() {
    var _zip = $(SearchCity.src_zip).value;
    var myAjax = new Ajax.Request("/?act=public_getcitylist", 
                                {method: "post", asynchronous:false, parameters: "zip="+_zip});
    SearchCity.Callback_getResultByZipCode(myAjax.transport);
}

//コールバック
SearchCity.Callback_getResultByZipCode = function(request) {
    var eval_obj = eval(request.responseText);
    if(eval_obj == undefined || eval_obj == null){
		return;
    }
    var city = eval_obj[0];
    //都道府県をセットする
    $(SearchCity.dst_pref).value = city.city_pref;
    //市区町村をセットする（SearchCity.Callbackで処理が行われる）
    SearchCity.dst_city_num = city.city_order;
    var myAjax = new Ajax.Request("/?act=public_getcitylist",
                    {method: "post", asynchronous:false, parameters: "addr1="+city.city_pref});
    SearchCity.Callback(myAjax.transport);
    //市区町村以下の情報(city_detail)があればそれを番地・建物名にセットする
    $(SearchCity.dst_address).value = city.city_detail;
    //郵便番号を変換した値に置き換える
    $(SearchCity.src_zip).value = city.zip_code;
}





