var httpObject;
var urlTarget = "logic_check.php";
var timeoutSecondDefault = 10000;		// Timeout (ms)
var timeoutSecond = timeoutSecondDefault;		// Timeout (ms)
var timeInterval = 50;	// ms
var timerId;				// Timer
var timerIdStatus;		// Timer for Status


// タイムアウト処理
function checkTimeout()
{
	timeoutSecond -= timeInterval;
	if( timeoutSecond <= 0 )
	{
		// タイマーをストップ
		clearInterval( timerId );
		clearInterval( timerIdStatus );
		timerId = null;
		timerIdStatus = null;
		// HTTPリクエストを中断
		httpObject.abort();
		// エラーダイアログを表示
		timerIdStatus = cheltenhamText_displayTextGradually( 'LabelStatus', 'ファイルの読み込みに失敗しました。', 500, 20 );

		// 検索ボタンを有効
		activateButton( true );

		return( false );
	}
}

// POSTデータ
function sendPostData( objectEvent )
{
	// パスワードから暗号キーを作成
	var id = document.getElementById( 'ID' ).value;
	var password = document.getElementById( 'Password' ).value;
	var ip = document.getElementById( 'AddressRemote' ).value;
	var seed = document.getElementById( 'Seed' ).value;
	var key = hex_md5( hex_md5( password ) + ip + seed );
	// Validate
	if( id == '' )
	{
		alert( 'ログインIDを入力してください。' );
		return;
	}
	if( password == '' )
	{
		alert( 'パスワードを入力してください。\nパスワードは暗号化されて送信されるので、\nご安心ください。' );
		return;
	}

	// ラベルに生成したキーを表示
	cheltenhamText_displayTextGradually( 'KeySend', key, 200, 20 );

	// 送信POSTデータ
	var postData = 'ID=' + encodeURIComponent( id ) + '&Key=' + encodeURIComponent( key );

	// HTTP通信を開始
	var functionRefference = function()
	{
		httpPostRequest( urlTarget, postData, printResultData );
	};
	setTimeout( functionRefference, 200 );
}

// サーバーへデータを送信する関数
function httpPostRequest( urlTarget, postData, functionNext )
{
	// タイマーをリセット
	timeoutSecond = timeoutSecondDefault;

	// タイマーをストップ
	if( timerId != null )
	{
		clearInterval( timerId );
	}
	if( timerIdStatus != null )
	{
		clearInterval( timerIdStatus );
	}

	// XMLHttpリクエストオブジェクト生成
	httpObject = cheltenhamXmlHttpObject_createXmlHttpRequest()
	if( !httpObject )
	{
		cheltenhamXmlHttpObject_noticeFailureCreateXmlHttpRequest();
	}

	// タイマーをセット
	timerId = setInterval( 'checkTimeout()', timeInterval );
	httpObject.open( "post", urlTarget, true );
	httpObject.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
	timerIdStatus = cheltenhamText_displayTextGradually( 'LabelStatus', 'Now Loading .............................................................', 2000, 20 );
	httpObject.onreadystatechange = function()
	{
		if( httpObject.readyState == 4 )
		{
			// タイマーをストップ
			clearInterval( timerId );
			timerId = null;
			document.getElementById( 'LabelStatus' ).innerHTML = '　';
			printResultData( '' );
			if(  httpObject.status == 200 )
			{
				// 処理を実行
				functionReference = function()
				{
					// タイマーをストップ
					clearInterval( timerIdStatus );
					timerIdStatus = null;
					// ステータスを表示
					cheltenhamText_displayTextGradually( 'LabelStatus', '認証結果を表示します。', 300, 20 );
				}
				setTimeout( functionReference, 0 );
				functionReference = function()
				{
					functionNext( httpObject.responseText );
				}
				setTimeout( functionReference, 500 );
			}
		}
	}
	httpObject.send( postData );
}

// 検索結果を表示する関数
function printResultData( textData )
{
	document.getElementById( 'PanelResult' ).innerHTML = textData;
}

// 暗号化キーを表示する関数
function printCryptKey( x )
{
	document.getElementById( 'KeySend' ).innerHTML = x;
}

// EventListener
function setListeners( e )
{
	var objectPassword = document.getElementById( 'Password' );
	var objectButtonLogin = document.getElementById( 'ButtonLogin' );
	cheltenhamEvent_addListener( objectPassword, 'keypress', function(){ printCryptKey(''); }, false );
	cheltenhamEvent_addListener( objectButtonLogin, 'click', sendPostData, false );
}


// タイトル文字
cheltenhamText_displayTextGradually( 'LabelGreeting', 'MD5による暗号化ログイン認証です。ログインIDとパスワードを入力してください。', 500, 20 );
// ステータス
cheltenhamText_displayTextGradually( 'LabelStatus', 'ログイン待機中です。', 200, 20 );

// リスナー設定
cheltenhamEvent_addListener( window, 'load', setListeners, false );


