// 都道府県→路線検索
function lineSearch(id_pref, id_line, id_station)
{
	if (document.getElementById(id_pref)[document.getElementById(id_pref).selectedIndex].value != ''){
		// 検索中かチェック
		if (wait_line == 1 || waid_station == 1){
			for (i=0; i<10; i++){
				_sleep(1000);
				if (wait_line == 0 && waid_station == 0){
					break;
				}
			}
			
			if (wait_line == 1 || waid_station == 1){
				alert('路線データの同期がタイムアウトしました。');
				return;
			}
		}
		
		wait_line = 1;
		
		pref = document.getElementById(id_pref);
		line = document.getElementById(id_line);
		station = document.getElementById(id_station);
		
		if (httpObj){
			
			httpObj.open('GET', '/tool/search_line.php?pref=' + escape(pref[pref.selectedIndex].value), true);
			httpObj.onreadystatechange = readLine;
			httpObj.send(null);
		}
		
		wait_line = 0;
		
	} else {
		if (line == null){
			line = document.getElementById(id_line);
		}
		if (station == null){
			station = document.getElementById(id_station);
		}
		selectReset(line);
		selectReset(id_station);
	}
}

function readLine()
{
	if (httpObj.readyState == 4 && httpObj.status == 200) {
		var xmlDoc = httpObj.responseXML;
		if (httpObj.responseXML){
			selectReset(line);
			selectReset(station);
			
			for (i=0; i<xmlDoc.getElementsByTagName("line").length; i++){
				line.options[i + 1] = new Option(xmlDoc.getElementsByTagName("name")[i].childNodes[0].nodeValue, xmlDoc.getElementsByTagName("id")[i].childNodes[0].nodeValue);
			}
		} else {
			//alert(httpObj.responseText);
		}
	}
}

// 路線→駅検索
function stationSearch(id_pref, id_line, id_station)
{
	if (document.getElementById(id_line)[document.getElementById(id_line).selectedIndex].value != ''){
		// 検索中かチェック
		if (wait_line == 1 || waid_station == 1){
			for (i=0; i<10; i++){
				_sleep(1000);
				if (wait_line == 0 && waid_station == 0){
					break;
				}
			}
			
			if (wait_line == 1 || waid_station == 1){
				alert('路線データの同期がタイムアウトしました。');
				return;
			}
		}
		
		waid_station = 1;
		
		pref = document.getElementById(id_pref);
		line = document.getElementById(id_line);
		station = document.getElementById(id_station);
		
		if (httpObj){
			
			httpObj.open('GET', '/tool/search_station.php?pref=' + escape(pref[pref.selectedIndex].value) + '&line=' + escape(line[line.selectedIndex].value), true);
			httpObj.onreadystatechange = readStation;
			httpObj.send(null);
		}
		
		waid_station = 0;
	} else {
		selectReset(station);
	}
}

function readStation()
{
	if (httpObj.readyState == 4 && httpObj.status == 200) {
		var xmlDoc = httpObj.responseXML;
		if (httpObj.responseXML){
			selectReset(station);
			
			for (i=0; i<xmlDoc.getElementsByTagName("station").length; i++){
				station.options[i + 1] = new Option(xmlDoc.getElementsByTagName("name")[i].childNodes[0].nodeValue, xmlDoc.getElementsByTagName("id")[i].childNodes[0].nodeValue);
			}
		} else {
			//alert(httpObj.responseText);
		}
	}
}

// 郵便番号検索
function zipSearch(zip_id, pref_id, city_id, town_id)
{
	zip_pref = pref_id;
	zip_city = city_id;
	zip_town = town_id;
	
	var zip = document.getElementById(zip_id).value.replace('-', '');
	
	if (httpObj){
		httpObj.open('GET', '/tool/search_zip.php?zip=' + escape(zip), true);
		httpObj.onreadystatechange = readZip;
		httpObj.send(null);
	}
}

function readZip()
{
	if (httpObj.readyState == 4 && httpObj.status == 200) {
		var xmlDoc = httpObj.responseXML;
		if (httpObj.responseXML){
			if (httpObj.responseText != ''){
				document.getElementById(zip_pref).selectedIndex = xmlDoc.getElementsByTagName("pref")[0].childNodes[0].nodeValue;
				document.getElementById(zip_city).value = xmlDoc.getElementsByTagName("city")[0].childNodes[0].nodeValue;
				document.getElementById(zip_town).value = xmlDoc.getElementsByTagName("town")[0].childNodes[0].nodeValue;
			}
		} else {
			
		}
	}
}


function getXmlHttpObject() {
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				xmlhttp = false;
			}
		}
	@else
		xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
			xmlhttp.overrideMimeType("text/xml"); 
		} catch (e) {
			xmlhttp = false;
		}
	}
	return xmlhttp;
}

var httpObj = getXmlHttpObject();
var pref;
var line;
var station;
var wait_line = 0;
var waid_station = 0;
var zip_pref;
var zip_city;
var zip_town;

function selectReset(obj)
{
	if (obj.type == 'select' || obj.type == 'select-one' || obj.type == 'select-multiple'){
		var len = obj.length;
		if (len > 1){
			for (i=len; i>0; i--){
				obj.options[i] = null;
			}
		}
		
		obj.options[0].defaultSelected;
	}
}

// スリープ処理
function _sleep(time){
	var d1 = new Date().getTime();
	var d2 = new Date().getTime();

	while( d2 < d1 + time ){
		d2=new Date().getTime();
	}

	return; 
}

