// JavaScript Document

var stationList = new Array();

function SetSearchBox(name){
	AllHidden();
	ShowObj(name);
	return false;
}

function AllHidden(){
	HideObj('search_box01');
	HideObj('search_box02');
	HideObj('search_box03');
}

function SelectStationKind(kindID){
	var xmlhttp = createXMLHttp();
	xmlhttp.onreadystatechange = function(){
		if(xmlhttp.readyState == 4){
			var readtext = xmlhttp.responseText;
			readtext = readtext.replace(/\r\n/g, "");//IE
			readtext = readtext.replace(/\n/g, "");//Firefox
			var stations = readtext.split('\t');
			
			Elements = document.getElementById('station_list');
			if(Elements != null){
				var station = "";
				for (var i in stations) {
					tmp = stations[i].split("=");
					station += "<p id=\"station_"+tmp[0]+"\">";
					station += "<a href=\"search/index.php\" onclick=\"SelectStation('"+tmp[0]+"',this); return false;\">"+tmp[1]+"</a></p>";
					stationList["station_"+tmp[0]] = tmp[1];
				}
				Elements.innerHTML = station;
			}
			ShowObj('station_inner');
			HideObj('station_loader');
		}else{
			HideObj('station_inner');
			ShowObj('station_loader');
		}
	};
	sendData ="kindID="+encodeURIComponent(kindID);
	url = "php_script/select_station_kind.php";
	xmlhttp.open("POST", url, true);
	xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded;');
	xmlhttp.send(sendData);
}

function SelectStation(stationID,obj){
	Elements = document.getElementById('select_station_list');
	if(Elements != null && stationList["station_"+stationID] != null){
		obj.parentNode.style.display = 'none';
		stationName = stationList["station_"+stationID];
		hidden = "<input type=\"hidden\" name=\"Station[]\" id=\"station_hidden_"+stationID+"\" value=\""+stationID+"\" />";
		station = "<p><a href=\"search/index.php\" onclick=\"BackStation('"+stationID+"',this);return false;\">";
		station += "取消</a>"+stationName+hidden+"</p>";
		Elements.innerHTML += station;
	}
}

function BackStation(stationID,obj){
	ShowObj('station_'+stationID);
	obj.parentNode.style.display = 'none';
	Elements = document.getElementById('station_hidden_'+stationID);
	if(Elements != null){
		Elements.value = "";
	}
}


var areaSelectKindName;
var areaSelectName;

function ChangeAreaKind(){
	areaSelectKindName = "KindArea2";
	areaSelectName = "AreaID";
	SearchArea();
}

function SearchArea(){
	var areaKindID;
	KindElements = document.getElementById(areaSelectKindName);
	if(KindElements != null){
		areaKindID = KindElements.value;
	}
	SetAreaSelect(areaKindID);
}

function SetAreaSelect(areaKindID){
	if(areaKindID > -1){
		var xmlhttp = createXMLHttp();
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 4){
				selectElements = document.getElementById(areaSelectName);
				if(selectElements != null){
					var lngth = selectElements.options.length;
					for ( var i = 0; i < lngth; i++ )
						selectElements.options[0] = null;
					AddSelectOption(selectElements,'','▼地区',false);
					if(xmlhttp.responseText != ""){
						var tmp = xmlhttp.responseText.split('|');
						var areaIDs = tmp[0].split(',');
						var areaNames = tmp[1].split(',');
						for (var i in areaIDs) {
							AddSelectOption(selectElements,areaIDs[i],areaNames[i],false);
						}
					}
				}
			}
		};
		sendData ="s="+encodeURIComponent(areaKindID);
		url = "../php_script/area.php";
		xmlhttp.open("POST", url, true);
		xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded;');
		xmlhttp.send(sendData);
	}
}

var stationSelectName;

function SearchStation(obj){
	stationSelectName = "Station";
	var stationKindID;
	if(obj != null){
		stationKindID = obj.value;
	}
	SetStationSelect(stationKindID);
}

function SetStationSelect(stationKindID){
	if(stationKindID != "" && stationKindID > 0){
		var xmlhttp = createXMLHttp();
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 4){
				selectElements = document.getElementById(stationSelectName);
				if(selectElements != null){
					var lngth = selectElements.options.length;
					for ( var i = 0; i < lngth; i++ )
						selectElements.options[0] = null;
					AddSelectOption(selectElements,'','▼駅',false);
					
					if(xmlhttp.responseText != ""){
						var tmp = xmlhttp.responseText.split('|');
						var stationIDs = tmp[0].split(',');
						var stationNames = tmp[1].split(',');
						for (var i in stationIDs) {
							AddSelectOption(selectElements,stationIDs[i],stationNames[i],false);
						}
					}
				}
				//CountStation(stationSelectName);
			}
		};
		sendData ="s="+encodeURIComponent(stationKindID);
		url = "../php_script/station.php";
		xmlhttp.open("POST", url, true);
		xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded;');
		xmlhttp.send(sendData);
		ShowObj2(stationSelectName);
	}else{
		HideObj(stationSelectName);
	}
}