var request;
var state_name;
var city_name ;
var state; 
var route;
var station;

function init(){
	state_name = document.getElementById("state_name");
	state_name.onchange = city_kensaku;
	city_name = document.getElementById("city_name");
	state = document.getElementById("state");
	route = document.getElementById("route");
	station = document.getElementById("station");
	
	
	//AJAXで路線情報を取得	
	if(window.XMLHttpRequest){
		request = new window.XMLHttpRequest();
	}else if(window.ActiveXObject){
		try{
			request = new window.ActiveXObject('Msxml2.XMLHTTP');
		}catch(e){
			request = new window.ActiveXObject('Microsoft.XMLHTTP');
		}
		
	}else{
		throw new Error('XMLHttpRequestがサポートされていません');
	}
	
}


function route_kensaku(){
	//routeとstationの子ノードを全て削除する
	var route_child_num = route.childNodes.length;
	for(var i = 0;i < route_child_num;i++){
		route.removeChild(route.firstChild);
	}
	
	var station_child_num = station.childNodes.length;
	for(var i = 0;i < station_child_num;i++){
		station.removeChild(station.firstChild);
	}
	
	request.onreadystatechange = get_route;
	query = "/return_route_info.php?state=" + encodeURI(state.value);
	request.open('GET',query,'true');
	request.send(null);
}


//都道府県と路線名から駅検索を行う
function station_kensaku(){
	//stationの子ノードを削除する
	var station_child_num = station.childNodes.length;
	for(var i = 0;i < station_child_num;i++){
		station.removeChild(station.firstChild);
	}	
	
	if(state.value && route.value){
		request.onreadystatechange = get_station;
		query = "/return_route_info.php?state=" + encodeURI(state.value) + "&route=" + encodeURI(route.value);
		request.open('GET',query,'true');
		request.send(null);
	}
	
}


//サーバから帰ってきた路線情報を表示する
function get_route(){
	
	if(request.readyState == 4 && request.status == 200){
		json = request.responseText;
		//document.write(json);
		var route_value = eval('(' + json + ')');
		
		option = document.createElement("option");
		option.value = "";
		option_value = document.createTextNode("指定しない");
		option.appendChild(option_value);
		route.appendChild(option);
		
		for(var i = 0;i < route_value.length;i++){
			route_name = route_value[i];
			option = document.createElement("option");
			option.value = route_name;
			option_value = document.createTextNode(route_name);
			option.appendChild(option_value);
			route.appendChild(option);
		}
		
	}
}


//サーバから帰ってきた駅情報を表示する
function get_station(){
	if(request.readyState == 4 && request.status == 200){
		json = request.responseText;
		//document.write(json);
		var station_value = eval('(' + json + ')');
		
		option = document.createElement("option");
		option.value = "";
		option_value = document.createTextNode("指定しない");
		option.appendChild(option_value);
		station.appendChild(option);
		
		for(var i = 0;i < station_value.length;i++){
			station_name1 = station_value[i];
			option = document.createElement("option");
			option.value = station_name1;
			option_value = document.createTextNode(station_name1);
			option.appendChild(option_value);
			station.appendChild(option);
		}
		
	}
	
}


//ナースキャリアのsearchへ飛ばす
function submit_station(){
	if(station.value){
		var url = "http://kango-shigoto.com/search_check_new/" + encodeURI(station.value) + "_4";
		location.href = url;
	}
	
}


//市区町村情報をAJAXで検索する
function city_kensaku(){
	//city_nameの子ノードを削除する
	var city_name_childs_num= city_name.childNodes.length;
	for(var i = 0;i < city_name_childs_num;i++){
		city_name.removeChild(city_name.firstChild);
	}
	
	if(state_name.value){
		request.onreadystatechange = get_city;
		query = "/return_city.php?state=" + encodeURI(state_name.value);
		request.open('GET',query,'true');
		request.send(null);
	}
	
}


//市区町村情報のAJAXからのレスポンス関数
function get_city(){
	if(request.readyState == 4 && request.status == 200){
		var json = request.responseText;
		//document.write(json);
		var city_value = eval('(' + json + ')');
		
		//何も指定しないノードを加える
		var option = document.createElement("option");
		option.value = "";
		option_value = document.createTextNode("指定しない");
		option.appendChild(option_value);
		city_name.appendChild(option);
		for(var i = 0;i < city_value.length;i++){
			city_name1 = city_value[i];
			option = document.createElement("option");
			option.value = city_name1;
			option_value = document.createTextNode(city_name1);
			option.appendChild(option_value);
			city_name.appendChild(option);
		}
		
	}
	
}

window.onload = init;
