var SearchStation = function() {};

SearchStation.src_pref = "";
SearchStation.src_line = "";
SearchStation.dst_station = "";
SearchStation.dst_station_num = 0;

// Update station list when selected line
SearchStation.changeStationListSync = function(src_pref, src_line, dst_station) {
	SearchStation.src_pref = src_pref;
	SearchStation.src_line = src_line;
	SearchStation.dst_station = dst_station;
	
	var _pref = $(SearchStation.src_pref).value;
	var _line = $(SearchStation.src_line).value;	
	var myAjax = new Ajax.Request("/?act=public_getstationlist", {method: "post", asynchronous:false, parameters: "pref="+_pref+"&line="+_line});
	
	SearchStation.Callback(myAjax.transport);
}

//コールバック
SearchStation.Callback = function(request) {
    var eval_obj = eval(request.responseText);
    SearchStation._setOptions(eval_obj,SearchStation.dst_station);
}

//optionセット
SearchStation._setOptions = function(eval_obj, dst_station) {
    //一度クリアする
    SearchStation._clearOptions(dst_station);
    var dst_obj = $(dst_station);
    if (eval_obj) {
        eval_obj.each(
            function(val,idx){
                dst_obj.options[dst_obj.length] = new Option(val.station_name, val.station_cd);
            }
        );
    }

    //SearchStation.dst_station_numが０以外ならその番号に該当する市区町村を表示状態にする
    if(SearchStation.dst_station_num != 0){
        $(SearchStation.dst_station).selectedIndex = SearchStation.dst_station_num;
        SearchStation.dst_station_num = 0;
    }
}

//optionクリア
SearchStation._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);
}
