// ジャンル選択時
function changeCategory(selCategory)
{
	document.getElementById('genre_id_back').value = '';
    setGenreOption(selCategory.value);
    
    //document.getElementById("genre_id").focus();
}

// ジャンルオプション設定
function setGenreOption(category_id)
{
    var idx;
    
    len = document.search_form.genre_id.options.length;
    for (i=len-1; i>=0; i--){
        document.search_form.genre_id.options[i] = null;
    }
    
    if (category_id != "") {
        var data = "";
        data += "category_id=" + category_id;
        data += "&date=" + (new Date).getTime();
        url = "genre_select.php";
        var myAjax = new Ajax.Request(
            url,
            {
                method: 'get',
                parameters: data,
                onComplete: function(xmlhttp) {
                    var result = eval("(" + xmlhttp.responseText + ")");
                    if (result.sts != "0") {
                        location.href = "/error.php";
                    }
                    else {
                        for (i=0; i < result.genre.length; i++) {
                            idx = (i+1);
                            document.search_form.genre_id.options[idx] =
                                    new Option(result.genre[i].val, result.genre[i].key);
                        }
                        if (document.getElementById('genre_id_back').value != '') {
                        	document.search_form.genre_id.selectedIndex = document.getElementById('genre_id_back').value;
                        }
                    }
                }
            });
    }
    
    document.search_form.genre_id.options[0] =
            new Option("選択して下さい", "");
    if (document.getElementById('genre_id_back').value == '') document.search_form.genre_id.selectedIndex = 0;
}

// 沿線区分選択時
function changeRailwayDivision(selRailwayDivision)
{
    var pref = document.form1.prefecture_id.value;
    setRailwayOption(selRailwayDivision.value, pref);
}

// 沿線設定
function setRailwayOption(railway_division_id, pref)
{
    var idx;
    
    len = document.move_form.railway_id.options.length;
    for (i=len-1; i>=0; i--){
        document.move_form.railway_id.options[i] = null;
    }
    
    if (railway_division_id != '') {
        var data = "";
        data += "railway_division_id=" + railway_division_id;
        data += "&prefecture_id=" + pref;
        url = "railway_select.php";
        var myAjax = new Ajax.Request(
            url,
            {
                method: 'post',
                parameters: data,
                onComplete: function(xmlhttp) {
                    var result = eval("(" + xmlhttp.responseText + ")");
                    if (result.sts != "0") {
                        location.href = "/error.php";
                    }
                    else {
                        for (i=0; i < result.railway.length; i++) {
                            idx = (i+1);
                            document.move_form.railway_id.options[idx] =
                                new Option(result.railway[i].val, result.railway[i].key);
                        }
					    if (document.move_form.railway_id.style.visibility == 'visible') document.move_form.railway_id.focus();
					    if (document.getElementById('railway_id_back').value != '') {
					    	document.getElementById('railway_id').selectedIndex = document.getElementById('railway_id_back').value;
					    	changeRailway(document.getElementById('railway_id'));
					    } else {
					    	document.move_form.railway_id.selectedIndex = 0;
					    }
                    }
                }
            });
    }

    document.move_form.railway_id.options[0] =
            new Option("選択して下さい", "");
    // document.move_form.railway_id.selectedIndex = 0;

    //駅プルダウンの初期化
    len = document.move_form.station_id.options.length;
    for (i=len-1; i>=0; i--){
        document.move_form.station_id.options[i] = null;
    }
    document.move_form.station_id.options[0] =
            new Option("選択して下さい", "");
    document.move_form.station_id.selectedIndex = 0;
}

// 沿線選択時
function changeRailway(selRailway)
{
    var pref = document.form1.prefecture_id.value;
    setStationOption(selRailway.value, pref);
    
    document.move_form.station_id.focus();

}

// 沿線設定
function setStationOption(railway_id, pref)
{
    var idx;

    len = document.move_form.station_id.options.length;
    for (i=len-1; i>=0; i--){
        document.move_form.station_id.options[i] = null;
    }
    
    if (railway_id != '') {
        var data = "";
        data += "railway_id=" + railway_id;
        data += "&prefecture_id=" + pref;
        url = "station_select.php";
        var myAjax = new Ajax.Request(
            url,
            {
                method: 'post',
                parameters: data,
                onComplete: function(xmlhttp) {
                    var result = eval("(" + xmlhttp.responseText + ")");
                    if (result.sts != "0") {
                        location.href = "/error.php";
                    }
                    else {
                        for (i=0; i<result.station.length; i++) {
                            idx = (i+1);
                            document.move_form.station_id.options[idx] =
                                new Option(result.station[i].val, result.station[i].key);
                        }
					    if (document.getElementById('station_id_back').value != '') {
					    	document.getElementById('station_id').selectedIndex = document.getElementById('station_id_back').value;
					    } else {
					    	document.move_form.station_id.selectedIndex = 0;
					    }
                    }
                }
            });
    }
    
    document.move_form.station_id.options[0] =
            new Option("選択して下さい", "");
    // document.move_form.station_id.selectedIndex = 0;

}

