// JavaScript Document

var httpObj;
var timerId;
var timeout_sec = 10;//タイムアウト
var infoStr1 = "";
var infoStr2 = "";
//XMLHttpRequestオブジェクト生成
function createHttpRequest(){
	//Win ie用
	if(window.ActiveXObject){
		try {
			//MSXML2以降用
			return new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				//旧MSXML用
				return new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				httpObjGenerateFail();
			}
		 }
	} else if(window.XMLHttpRequest){
		//Win ie以外のXMLHttpRequestオブジェクト実装ブラウザ用
		return new XMLHttpRequest();
	} else {
		httpObjGenerateFail();
	}
}


//コールバック関数 ( 受信時に実行されます )
function on_loaded(oj){
	//レスポンスを取得
	res  = httpObj.responseText;
	document.getElementById('main').innerHTML =res;
	//ダイアログで表示
	alert(res);
}


function requestFile( data , method , fileName , async ) {
	//XMLHttpRequestオブジェクト生成
	httpObj = createHttpRequest();

	// タイマー
	timerId = setInterval('timeoutCheck()', 1000);
	
	httpObj.open(method, fileName, async);
	httpObj.onreadystatechange = function() {
		if (httpObj.readyState == 4) {
			clearInterval(timerId);
			if (httpObj.status == 200) {
				
				on_loaded(httpObj);
				
				
				
			} else {
				alert(httpObj.status + ' : ' + httpObj.statusText);
				return false;
			}
		}
	}
	httpObj.send(data);
}

// 失敗時の処理
function httpObjGenerateFail() {
	alert('ご利用のブラウザーでは、当サイトの運行情報をご利用頂けません。');
	return false;
}

// タイムアウト
function timeoutCheck() {
	timeout_sec --;
	if(timeout_sec <= 0) {
		// タイマーストップ
		clearInterval(timerId);
		// HTTPリクエスト中断
		httpObj.abort();
		// エラーダイアログ
		alert('運行情報の取得処理がタイムアウト致しました。\nご利用のブラウザーをリロードしてもう一度おためしいただくか、ご利用方法ページからご利用方法をご確認ください。');
		return false;
	}
}

function requestPHP1( data , method , fileName , async ) {
	//XMLHttpRequestオブジェクト生成
	httpObj = createHttpRequest();

	// タイマー
	timerId = setInterval('timeoutCheck()', 1000);
	
	httpObj.open(method, fileName, async);
	httpObj.onreadystatechange = function() {
		if (httpObj.readyState == 4) {
			clearInterval(timerId);
			if (httpObj.status == 200) {
				
				infoStr1 = on_loaded1(httpObj);
				requestPHP2( '' , 'GET', 'http://www.toyokosoku.co.jp/unko/ShowUnkoJyokyo.php?info_type=data' , true );
				
				
			} else {
				alert(httpObj.status + ' : ' + httpObj.statusText);
				return false;
			}
		}
	}
	httpObj.send(data);
}
function requestPHP2( data , method , fileName , async ) {
	//XMLHttpRequestオブジェクト生成
	httpObj = createHttpRequest();

	// タイマー
	timerId = setInterval('timeoutCheck()', 1000);
	
	httpObj.open(method, fileName, async);
	httpObj.onreadystatechange = function() {
		if (httpObj.readyState == 4) {
			clearInterval(timerId);
			if (httpObj.status == 200) {
				
				infoStr2 = on_loaded2(httpObj);
				
				
				
			} else {
				alert(httpObj.status + ' : ' + httpObj.statusText);
				return false;
			}
		}
	}
	httpObj.send(data);
}

function infoGet(){
	//window.confirm("開始");
	requestPHP1( '' , 'GET', 'http://www.toyokosoku.co.jp/unko/ShowUnkoJyokyo.php?info_type=time' , true );
}

//コールバック関数 ( 受信時に実行されます )
function on_loaded1(oj){
	//レスポンスを取得
	res  = httpObj.responseText;
	return res;
}

function on_loaded2(oj){
	//レスポンスを取得
	res  = httpObj.responseText;
	
	if(infoStr1 == "" || infoStr1 == " " || infoStr1 == "\n" || infoStr1 == "\t" || infoStr1 == null ){
		document.getElementById('disp').className = "normal";
		document.getElementById('disp').innerHTML =res;
	}else{
		document.getElementById('disp').className = "attention";
		document.getElementById('disp').innerHTML =res + "<br /><span class=\"date\">-"+infoStr1 + "&nbsp;現在-</span>";
	}
	//ダイアログで表示
	//alert(res);
}
