var httpObject;
var urlTarget = "./logic_check.php";
var urlRedirect = "./logic_redirect.php";
var urlSound = "./logic_sound.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 )
{
	activateButton( false );

	// パスワードから暗号キーを作成
	var email = document.getElementById( 'Email' ).value;
	var password = document.getElementById( 'Password' ).value;
	var ip = document.getElementById( 'AddressRemote' ).value;
	var period = document.getElementById( 'Period' ).value;
	var seed = document.getElementById( 'Seed' ).value;
	var key = hex_md5( hex_md5( password ) + ip + seed );
	// Validate
	if( email == '' )
	{
		alert( 'メールアドレスを入力してください。' );
		activateButton( true );
		return;
	}
	if( password == '' )
	{
		alert( 'パスワードを入力してください。\nパスワードは暗号化されて送信されるので、\nご安心ください。' );
		activateButton( true );
		return;
	}

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

	// 送信POSTデータ
	var postData = 'Email=' + encodeURIComponent( email ) + '&Key=' + encodeURIComponent( key ) + '&Period=' + encodeURIComponent( period );

	// 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 .............................................................', 100, 20 );
	httpObject.onreadystatechange = function()
	{
		if( httpObject.readyState == 4 )
		{
			// タイマーをストップ
			clearInterval( timerId );
			timerId = null;
			document.getElementById( 'LabelStatus' ).innerHTML = '　';
			if( httpObject.status == 200 )
			{
				// 処理を実行
				functionReference = function()
				{
					// タイマーをストップ
					clearInterval( timerIdStatus );
					timerIdStatus = null;
					// ステータスを表示
					cheltenhamText_displayTextGradually( 'LabelStatus', '認証結果を表示します................................................', 200, 20 );
				}
				setTimeout( functionReference, 0 );
				functionReference = function()
				{
					functionNext( httpObject.responseText );
				}
				setTimeout( functionReference, 300 );
			}
		}
	}
	httpObject.send( postData );
}

// 検索結果を表示する関数
function printResultData( flagAuthorized )
{
	if( eval( flagAuthorized ) )
	{
		timerIdStatus = cheltenhamText_displayTextGradually( 'LabelStatus', 'ログイン認証されました。ログイン前の画面に戻ります。', 200, 20 );
		cheltenhamXmlHttpObject_sendHttpRequest( urlSound, 'post', 'Sound=' + encodeURIComponent( 'Completed' ), 'text', getSound );
		functionReference = function()
		{
			document.location.href = urlRedirect;
		}
		setTimeout( functionReference, 1000 );
	}
	else
	{
		timerIdStatus = cheltenhamText_displayTextGradually( 'LabelStatus', '認証されませんでした。メールアドレスまたはパスワードが違います。', 300, 20 );
		cheltenhamXmlHttpObject_sendHttpRequest( urlSound, 'post', 'Sound=' + encodeURIComponent( 'Error' ), 'text', getSound );
		activateButton( true );
	}
	return;
}

// ボタンをアクティブにする関数
function activateButton( flagActive )
{
	if( flagActive == true )
	{
		document.getElementById( 'ButtonLogin' ).disabled = false;
	}
	else
	{
		document.getElementById( 'ButtonLogin' ).disabled = true;
	}
}

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

function getSound( x )
{
	document.getElementById( 'LabelSound' ).innerHTML = x;
}

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


// タイトル文字
cheltenhamText_displayTextGradually( 'LabelGreeting', 'ログイン画面です。メールアドレスとパスワードを入力してください。', 500, 20 );
// ステータス
cheltenhamText_displayTextGradually( 'LabelStatus', 'ログイン待機中です。', 200, 20 );

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


