$(function(){
//if(now == "index" || now == "search" || now == "object" || now == "advanced" || now == "belt" || now == "approval"){
//if(now == "index" || now == "search" || now == "object" || now == "advanced" || now == "belt" || now == "approval"){

if(now == "object" || now == "advanced" || now == "belt" || now == "approval"){
$(".prefecture").val(selected_pref);
$(".city").val(selected_city);
}

$("select.prefecture").change(function(){
var selected_pref = $(this).children("option[@selected]");
var target_city = $(this).parents("dl").siblings().children().children(".city");
if(target_city.val() == null){
var target_city = $(this).parents("li").siblings().children(".city");
}
if(selected_pref.val() != "all" && selected_pref.val() != undefined){
if(now == "belt"){
var city_list_url = "http://www.kaatsu-training.jp/func/citylist.php?prefecture="+selected_pref.val()+"&condition=belt&json=json";
}
else {
var city_list_url = "http://www.kaatsu-training.jp/func/citylist.php?prefecture="+selected_pref.val()+"&condition=facility&json=json";
}

$.getJSON(city_list_url , function(json) {

target_city.empty();
$(json).each(function(i){

if(this.count != undefined){
target_city.append("<option value=\""+this.n_code+"\">"+this.name+"("+this.count+")"+"</option>");
}
else {
target_city.append("<option value=\""+this.n_code+"\">"+this.name+"</option>");
}

});
target_city.prepend("<option value=\"all\" selected=\"selected\">市区町村</option>");

});

}

else if(selected_pref.val() == "all"){
target_city.empty();
target_city.prepend("<option value=\"all\" selected=\"selected\">市区町村</option>");
}

});

//}

if(now == "facility"){
$("#prefecture").val(selected_pref);

$("#city").val(selected_city);
}

if(now == "city"){
$("#city1").val(city);
$("#city2").val(city);
}
/*
if(now == "belt"){

$("#prefecture").change(function(){

var selected_pref = $(this).children("option[@selected]");

if($(this).children("option[@selected]") == undefined){
var selected_pref = pref;
}
var city_list_url = "http://www.kaatsu-training.jp/citylist.php?prefecture="+selected_pref.val()+"&condition=belt&json=json";

$.getJSON(city_list_url , function(json) {

$("#city_name").empty();
$(json).each(function(i){

if(this.count != undefined){
$("#city_name").append("<option value=\""+this.n_code+"\">"+this.name+"("+this.count+")"+"</option>");
}
else {
$("#city_name").append("<option value=\""+this.n_code+"\">"+this.name+"</option>");
}

});
$("#city_name").prepend("<option value=\"all\" selected=\"selected\">市区町村</option>");

});

});

$("#belt_pref_name").change(function(){

var selected_pref = $(this).children("option[@selected]");
var city_list_url = "citylist.php?prefecture="+selected_pref.val()+"&condition=belt&json=json";

$.getJSON(city_list_url , function(json) {

$("#belt_city_name").empty();
$(json).each(function(i){

if(this.count != undefined){
$("#belt_city_name").append("<option value=\""+this.n_code+"\">"+this.name+"("+this.count+")"+"</option>");
}
else {
$("#belt_city_name").append("<option value=\""+this.n_code+"\">"+this.name+"</option>");
}

});
$("#belt_city_name").prepend("<option value=\"all\" selected=\"selected\">市区町村</option>");

});

});

}*/

});
