var debugflg = false;


/**
 * デバッグ用alertメッセージを表示する。
 */
function debugAlert(mes) {
	if (debugflg == true) {
		alert(mes);
	}
}




/**
 * 検索中メッセージの表示
 * 
 */
function displaySearchingMessage() {
	$("#searchResult").remove();
	$a = "<div class='clDivConte570' id='searchResult'><br><br><br><br>　　　検索中です・・・・・・・</div>";
	$("#searchResultTop").append($a);
}


/**
 * バス停検索をPHPに依頼
 * 
 * @param int selectType 検索タイプ1:目的地/2:住所名/3:バス停名
 */
function doBusstopsearch() {
	debugAlert("call doBusstopsearch");

	if ( ($('#startbusstopname').val() != "") && ($('#endbusstopname').val() != "") )   {
		debugAlert("バス停名が選択されました。3");
		$.post("busstop2.php", {"stype" : "busstopnamekensaku", "startbusstopname" : $('#startbusstopname').val(), "endbusstopname" : $('#endbusstopname').val()}, getResult);
		displaySearchingMessage();

	} else {
		alert("バス停名を選択してください。");
	}

	return false;
}






/**
 * バス停検索のバス停名を取得結果が返って来た時の処理
 */
function getBusstopName2(data) {
	debugAlert("getBusstopName2:" + data);


	$("#busstopnameSelectoptions2").remove();
	if (data.substr(0,40) == "<Content-Type: text/xml; charset=UTF8>OK") {
		$("#busstopnameSelect2").append(data.substr(41, 1000000));

	} else {
		optiontext = makeNoneSelecttag("busstopnameSelectoptions2", "busstopname2");
		$("#busstopnameSelect2").append(optiontext);
	}

	return false;
}



/**
 * バス停検索で乗車バス停名(50音)が選択された時の処理
 * @param int stype 1:乗車/2:降車
 */
function doBusstopNameSelect(stype) {
	debugAlert("選択されました。" + stype);

	if (stype == 1) {
		if ($('#startbusstopnameon50').val() != "") {
			$.post("busstop2.php", {"stype" : "startbusstopnameon50", "startbusstopnameon50" : $('#startbusstopnameon50').val()}, getBusstopName1);
		} else {
			getBusstopName1("");
		}

	} else {
		if ($('#endbusstopnameon50').val() != "") {
			$.post("busstop2.php", {"stype" : "endbusstopnameon50", "endbusstopnameon50" : $('#endbusstopnameon50').val()}, getBusstopName2);
		} else {
			getBusstopName2("");
		}
	}

	return false;
}


/**
 * 空のselectタグを作成する
 */
function makeNoneSelecttag(id, name) {
	$optiontext = '<div id="' + id + '">';
	$optiontext += '<select name="' + name + '" size="1">';
	$optiontext += '</select>';
	$optiontext += '</div>';

	return $optiontext;
}




/**
 * バス停検索の乗車バス停名を取得結果が返って来た時の処理
 */
function getBusstopName1(data) {
	debugAlert("getBusstopName1:" + data);


	$("#startbusstopnameSelectoptions").remove();
	if (data.substr(0,40) == "<Content-Type: text/xml; charset=UTF8>OK") {
		$("#startbusstopnameSelect").append(data.substr(41, 1000000));

	} else {
		optiontext = makeNoneSelecttag("startbusstopnameSelectoptions", "startbusstopname");
		$("#startbusstopnameSelect").append(optiontext);
	}

	return false;
}


/**
 * バス停検索の乗車バス停名を取得結果が返って来た時の処理
 */
function getBusstopName2(data) {
	debugAlert("getBusstopName2:" + data);


	$("#endbusstopnameSelectoptions").remove();
	if (data.substr(0,40) == "<Content-Type: text/xml; charset=UTF8>OK") {
		$("#endbusstopnameSelect").append(data.substr(41, 1000000));

	} else {
		optiontext = makeNoneSelecttag("endbusstopnameSelectoptions", "endbusstopname");
		$("#endbusstopnameSelect").append(optiontext);
	}

	return false;
}


/**
 * 検索の結果がが返って来た時の処理
 */
function getResult(data) {
	debugAlert("getResult:" + data);

	if (data.substr(0,40) == "<Content-Type: text/xml; charset=UTF8>OK") {
		$("#searchResult").remove();
		$a = data.substr(41, 1000000);
		$("#searchResultTop").append($a);

	} else if (data.substr(0,40) == "<Content-Type: text/xml; charset=UTF8>NG") {
		$("#searchResult").remove();
		alert(data.substr(41, 1000000));

	} else {
		$("#searchResult").remove();
		alert("該当データはありません。");
	}

	return false;
}

