/*------------------------------------------------
 * タイトル：共通Javascriptファイル
 * 説明    ：kyenomi用のjsライブラリ
 *
 * 著作権  ：Copyright(c) DOT80
 *
 * バージョン：1.0
-------------------------------------------------*/

/*------------------------------------------------
ログイン状態取得
 @return:ログイン状態（0:未ログイン 1:ログイン済み）
------------------------------------------------*/
function key_isLogin()
{

	var value = document.getElementById( "kyeAuth" ).contentWindow.document.loginForm.isLogin.value;

	if(value == "1")
	{
		return true;
	}
	else
	{
		return false;
	}

}

/*------------------------------------------------
ログインフォーム用バリデーション
（メッセージ埋め込み）
------------------------------------------------*/
function validLogin()
{
	mailErrFlag = false;									// メールアドレスエラーフラグを初期化
	passErrFlag = false;									// パスワードエラーフラグを初期化

	// メールアドレス　最大長＋必須、形式チェック

	maxLength = 255;										// 最大255文字
	elemTrim("username")									// トリム
	ret = lengthReqCheck( "username", maxLength );			// チェックを行う

	switch( ret )
	{
		case 0:												// 正常の場合
			// 形式チェック
			ret = validMail( "username" );
			if( ret == 0 )
			{
				document.getElementById("usernameMSG").innerHTML = "";
				document.getElementById("username").style.backgroundColor = "transparent";
			}
			else
			{
				document.getElementById("usernameMSG").innerHTML = "メールアドレスを確認してください。";
				document.getElementById("username").style.backgroundColor = "#FFCCFF";
				mailErrFlag = true;
			}
			break;
		case 1:												// 最大長オーバーの場合
			document.getElementById("usernameMSG").innerHTML = "メールアドレスは" + maxLength + "文字まで入力できます。";
			document.getElementById("username").style.backgroundColor = "#FFCCFF";
			mailErrFlag = true;
			break;
		case 2:												// 未入力の場合
			document.getElementById("usernameMSG").innerHTML = "メールアドレスが入力されていません。";
			document.getElementById("username").style.backgroundColor = "#FFCCFF";
			mailErrFlag = true;
			break;
	}

	// パスワード　必須チェック

	ret = requireCheck( "password", maxLength );			// チェックを行う

	switch( ret )
	{
		case 0:												// 正常の場合
			document.getElementById("passwordMSG").innerHTML = "";
			document.getElementById("password").style.backgroundColor = "transparent";
			break;
		case 1:												// 未入力の場合
			document.getElementById("passwordMSG").innerHTML = "パスワードが入力されていません。";
			document.getElementById("password").style.backgroundColor = "#FFCCFF";
			passErrFlag = true;
			break;
	}


	// エラーがあればPOSTを止めるためにfalseを返す

	if( (mailErrFlag == false) && (passErrFlag == false) )
	{
		return true;
	}
	else
	{
		return false;
	}
}

/*------------------------------------------------
必須チェック
 @return:チェック結果（0:正常 1:未入力）
 @param1:チェックするエレメントのid属性値
------------------------------------------------*/
function requireCheck(elementID)
{
	checkTxt = document.getElementById( elementID ).value;

	if((checkTxt == "") || (checkTxt == undefined))
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

/*------------------------------------------------
最大長チェック
 @return:チェック結果（0:正常 1:最大長オーバー）
 @param1:チェックするエレメントのid属性値
 @param2:最大文字数
------------------------------------------------*/
function lengthCheck(elementID, maxLength)
{
	checkTxt = document.getElementById( elementID ).value;

	if(checkTxt.length > maxLength)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

/*------------------------------------------------
最大長＋必須チェック
 @return:チェック結果（0:正常 1:最大長オーバー 2:未入力）
 @param1:チェックするエレメントのid属性値
 @param2:最大文字数
------------------------------------------------*/
function lengthReqCheck(elementID, maxLength)
{
	checkTxt = document.getElementById( elementID ).value;

	if((checkTxt == "") || (checkTxt == undefined))
	{
		return 2;
	}
	else
	{
		if(checkTxt.length > maxLength)
		{
			return 1;
		}
		else
		{
			return 0;
		}
	}
}

/*------------------------------------------------
トリム（文字列前後の半角スペース除去）
 @return:スペース除去後の文字列
 @param1:トリムする文字列
 ※Firefoxの場合は全角スペースもTrimされる
------------------------------------------------*/
function Trim(argValue)
{
	str = argValue.replace(/^[ ]+|[ ]+$/g, "");
    return str
}

/*------------------------------------------------
トリムＢ（文字列前後の半角全角スペース除去）
 @return:スペース除去後の文字列
 @param1:トリムする文字列
------------------------------------------------*/
function TrimB(argValue)
{
	str = argValue.replace(/^[ 　]+|[ 　]+$/g, "");
    return str
}

/*------------------------------------------------
エレメント内トリム
（指定エレメント内文字列前後の半角スペース除去）
 @return:なし
 @param1:トリムするエレメントのid属性値
 ※Firefoxの場合は全角スペースもtrimされる
------------------------------------------------*/
function elemTrim(elementID)
{
	element = document.getElementById( elementID );
	element.value = Trim( element.value );
    return
}

/*------------------------------------------------
エレメント内トリムＢ
（指定エレメント内文字列前後の半角全角スペース除去）
 @return:なし
 @param1:トリムするエレメントのid属性値
------------------------------------------------*/
function elemTrimB(elementID)
{
	element = document.getElementById( elementID );
	element.value = TrimB( element.value );
    return
}

/*------------------------------------------------
メールアドレス形式チェック
 @return:チェック結果（0:正常 1:形式エラー）
 @param1:チェックするエレメントのid属性値
 ※厳密ではないことに注意
------------------------------------------------*/
function validMail(elementID)
{
	checkTxt = document.getElementById( elementID ).value;
	if ( checkTxt.match(/^[^@]+@[^.]+\..+/) )
	{
		return 0;
	}
	else
	{
		return 1;
	}
}

/*------------------------------------------------
同一内容チェック
 @return:チェック結果（0:異なる内容 1:同一内容）
 @param1:チェックするエレメントのid属性値（ひとつめ）
 @param1:チェックするエレメントのid属性値（ふたつめ）
------------------------------------------------*/
function identityCheck(elementID1, elementID2)
{
	checkTxt1 = document.getElementById( elementID1 ).value;
	checkTxt2 = document.getElementById( elementID2 ).value;

	if(checkTxt1 == checkTxt2)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}