// ログイン処理フラグ
var loginFlg = false;

// ログイン関連関数
function CheckEnter()
{
    if(event.keyCode == 13){
        objrequire(login);
    }
}
function toLowerIDPass()
{
    document.getElementById('userID').value = document.getElementById('userID').value.toLowerCase();
    document.getElementById('password').value = document.getElementById('password').value.toLowerCase();
}

function objrequire(obj)
{
	if(loginFlg) {
		return false;
	} else {
		loginFlg = true;
	}

    var userid = obj.userID.value;
    var password  = obj.password.value;

	// cookie有効性チェックを実施する
	if(!checkCookie()){
		loginFlg = false;
		return false;
	}

    if(!objnullchk(userid)){
      account = new Array('ID');
      alert(getMessage('errors.required', account));
      loginFlg = false;
      return false;
    }

    resultnum = betweenchk(userid, 4, 12);
    if((!resultnum == 0) || (!userid.match(/^[0-9|A-Z|a-z]+$/))){
        account = new Array('ID','4','12');
        alert(getMessage('errors.min.to.maxlength', account));
        loginFlg = false;
        return false;
    }
    resultnum = passwordchk( password );
    if(resultnum == -1){
      pass = new Array('パスワード');
      alert(getMessage('errors.required', pass));
      loginFlg = false;
      return false;
    }
    if(resultnum == -2 || resultnum == -3){
        alert(getMessage('errors.min.to.maxlength', new Array('パスワード', 4, 16)));
        loginFlg = false;
        return false;
    }
    if(resultnum == -4){
        alert(getMessage('errors.invalid', new Array('パスワード')));
        loginFlg = false;
        return false;
    }

	DWREngine.setTimeout(1000);		// DWR実行タイムアウト
	DWREngine.setAsync(false);		// 同期的に実行(false で同期)

    // アカウントチェックをする
    LoginChk.existAccount(callbackExistAccount, obj.userID.value, obj.password.value);
    
	function callbackExistAccount(data) {
		if(data == 0){
			// パスワード変更が必要かチェック
			var res = chkPwUpdateRequired(obj.userID.value);
			if (res) {
				loginFlg = false;
				return; 
			}
			chkIdResult(true);
			return;
		} else if (data == 2) {
			alert(
				'パスワード入力間違いが規定回数に達しましたので、\n' +
				'不正ログイン防止のためログイン制限状態とさせていただきます。\n' +
				'登録メールアドレス宛に制限解除ページのURLを記載したメールを\n' +
				'お送りしましたので、メールをご確認ください。');
			loginFlg = false;
		} else if (data == 3) {
			alert(
				'不正ログイン防止のため、本IDはログイン制限状態となっています。\n' +
				'登録メールアドレス宛に制限解除ページのURLを記載したメールを\n' +
				'お送りしていますので、メールをご確認ください。');
			loginFlg = false;
		} else {
			chkIdResult(false);
		}
		obj.password.value = '';
	}
}

/**
 * ログイン処理時に使用します。<br>
 * パスワード変更が必要かチェックします。
 * @param accountID
 * @return
 */
function chkPwUpdateRequired(accountID) {
	 return false; // 使用する場合は以下コメントはずす（common.js、neadea_common.js、account.js）
//	DWREngine.setTimeout(1000);		// DWR実行タイムアウト
//	DWREngine.setAsync(false);		// 同期的に実行(false で同期)
//	
//	var required = false;
//	PwChk.isPwUpdateRequired(callbackChkPwUpdateRequired, accountID);
//	return required;
//	
//	function callbackChkPwUpdateRequired(res) {
//		required = res;
//		if (res) {
//			alert('■　お知らせ　■ \n' + 
//					'@gamesサポートチームです。\n' +
//					'セキュリティ確保のため登録メールにパスワード変更手続きのURLを送付いたしました。\n' +
//					'お手数ですがパスワードを変更くださるようお願いいたします。\n' +
//					'変更後は通常ログインにて@gamesをご利用いただけます。');
//			loginFlg = false;
//		}
//	}
}

function chkIdResult(data)
{
    if(data == false){
        alert('ログインできません。');
        loginFlg = false;
    }else{
    	// Closedβ対応(@gamesユーザーのログイン時にはvisit=""なのでvisit="neadea"とする)
    	var visit=document.getElementById('visit').value;
    	if(visit == '' || visit == null){
    		document.getElementById('visit').value="neadea";
    	}
        login.submit();
    }
}

function passwordchk(inString)
{
    if(!objnullchk(inString)){
        // 未入力
        return -1;
    }else{
        resultnum = betweenchk(inString, 4, 16);
        if(resultnum == -1){
            // 入力文字数不足
            return -2;
        }else if(resultnum == -2){
            // 入力文字数超過
            return -3;
        }

        if(!inString.match(/^[-0-9a-zA-Z\!\"\'\#\$\%\&\(\)\*\+\,\.\/\:\;\<\>\=\?\@\[\]\\\^\_\`\{\}\|\~]+$/)){
            // 入力禁止文字
            return -4;
        }
    }
    return 0;
}

function objnullchk(data)
{
    var result = true;
    if(data == null){
        result = false;
    }
    if(data == ""){
        result = false;
    }
    return result;
}

// ゲームスタート関連
var isExecFlg = false;
function setExec( bool ) {
    isExecFlg = bool;
}
function isExec() {
    return isExecFlg;
}

// Ｇコイン、ぐっせる所有数表示関連
function getNeaDeaData(accountId)
{
    ChangeAvatarInfo.getCoinInfo(CallbackNeaDeaInfo);
    ContentLink.getGameUserInfo(CallbackNeaDeaInfo2, accountId, '1502');
}

function CallbackNeaDeaInfo(value)
{
    var strgcoin      = null;
    var strtodaypcoin = null;
    var strpcoin      = null;

    if(value == null) {
        strgcoin      = "----";
        strtodaypcoin = "----";
        strpcoin      = "----";
    } else {
        strgcoin      = document.createTextNode(value.gcoin);
        strtodaypcoin = document.createTextNode(value.todayPcoin);
        strpcoin      = document.createTextNode(value.pcoin);
    }

    var gCoin = document.getElementById('nowGCoin');
    if(gCoin != null) {
        gCoin.removeChild(gCoin.lastChild);
        gCoin.appendChild(strgcoin);
    }

    var pCoin = document.getElementById('nowPCoin');
    if(pCoin != null) {
        pCoin.removeChild(pCoin.lastChild);
        pCoin.appendChild(strpcoin);
    }

    var todayPCoin = document.getElementById('todayPCoin');
    if(todayPCoin != null) {
        todayPCoin.removeChild(todayPCoin.lastChild);
        todayPCoin.appendChild(strtodaypcoin);
    }
}

function CallbackNeaDeaInfo2(value)
{
    var strfpoint = null;
    if(value) {
    	if(value.result == '000'){
			strfpoint = document.createTextNode(value.pointValue);
		}
    }

    if(strfpoint == null) {
		strfpoint = document.createTextNode("----");
    }
    var fPoint = document.getElementById('nowFPoint');
    if(fPoint != null) {
        fPoint.removeChild(fPoint.lastChild);
        fPoint.appendChild(strfpoint);
    }
}

/* Closedβ用ゲーム起動（ID連携を行わない） */
function startGameBeta( isEnable, accountId, gameId, registPageUrl, checkFunc ) {
	if ( !isEnable ) {
		return;
	}

	var isRegistFlg = isRegist( accountId, gameId );

	if ( isRegistFlg ) {
		bootGame( accountId, gameId, checkFunc );
	} else {
		alert("クローズドβ会員様のみ参加できます。\nオープンβ開始は8月中旬頃を予定しています。");
	}
}

function globalMenuRO(name, fileName, flg) {
	document.images[name].src = fileName + flg + ".jpg";
}
