/**
 * GAME周りで使用するJavaScript
 * @author Gportal
 * @version 1.0
 * 
 */
 

/**
 * Cookie読み出し
 * @author M.shimoda
 * @param key 読み出すキー蜷?
 * @return キー蜀?容
 */
function getCookie(key)
{
	var start,end;
	var cookie=document.cookie+";";
	var keypos=cookie.indexOf(key,0);
	if(keypos!=-1){
		cookie=cookie.substring(keypos,cookie.length);
		start=cookie.indexOf("=",0)+1;
		end=cookie.indexOf(";",start);
		return(unescape(cookie.substring(start,end)));
	}
	return("");
}

/**
 * ActiveX起動
 * @author M.shimoda
 */
function Push_ActiveX()
{
	eval("ATGames.StartGame();");
}

/**
 * ActiveXがクライアントに入っているか判断
 * Gameの各TOP画面で呼び出すこと
 * @author M.shimoda
 * @param gameID
 */
function useActiveX(gameID)
{
	var NAME  = "ATGames";
	var CLSID = "1C7FDC6D-5392-4635-B3AA-DC0ECD6A77A3";
	var CODEBASE = "@gamesPlugin.cab#version=1,0,2,0";

	document.write('<object classid="clsid:'+CLSID+'" onreadystatechange="CheckActiveX(this)" codebase="'+CODEBASE+'" width=0 height=0 name="'+NAME+'" id="'+NAME+'">');
	document.write('<param name="Game_ID" value="'+gameID+'" >');
	document.write('<param name="Session_ID" value="'+getCookie("SESSIONID")+'" ></object>');
}

// ActiveXがインストールされているかどうかチェック
function CheckActiveX(obj)
{
	if(!obj || obj.readyState < 4){
		return;
	}
	if(!obj.object){
		if(XPSP2_Check()) {
			document.getElementById('startButton').href="/atgames/html/game/activex-inst-xp.html";
			document.getElementById('startButton').target="_blank";
//			window.open("/atgames/html/game/activex-inst-xp.html","ActiveXInstall",'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,width=650,height=650');
		}else{
			document.getElementById('startButton').href="/atgames/html/game/activex-inst.html";
			document.getElementById('startButton').target="_blank";
//			window.open("/atgames/html/game/activex-inst.html","ActiveXInstall",'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,width=650,height=650');
		}
	}
}

// ActiveXがインストールされているかチェック
function CheckActiveXInstalled(obj)
{
	if (obj.readyState < 4){
		return;
	}
	if (obj.object){
		opener.location.reload();
		self.close();
	}
}

// XPのSP2を使っているかどうかチェック
function XPSP2_Check()
{
	var uAgent1 = /[^A-Z0-9]MSIE[ ]+6.0[^A-Z0-9]/i;
	var uAgent2 = /[^A-Z0-9]WINDOWS[ ]+NT[ ]+5.1[^A-Z0-9]/i;
	
	if ((navigator.appMinorVersion.replace(/\s/g,"").toUpperCase().indexOf(";SP2;") >= 0) && (uAgent1.test(navigator.userAgent)==true) && (uAgent2.test(navigator.userAgent)==true)){
		return true;
	}
	return false;
}

function exec(path, id) {
	location.href = path + "?gameId=" + id;
}

function execRes(gameid, threadid) {
	document.formthread.gameId.value = gameid;
	document.formthread.threadId.value = threadid;
	document.formthread.submit();
}

function execNewThread(id) {
	document.formnewthread.submit();
}

function gamestart(id, gameid) {
	var obj = parent.document.getElementById("submenu");
	if (gameid==0) {
		obj.height="1000";
	}
	obj.src = id + "?gameId=" + gameid;

	var obj2 = parent.document.getElementById("main");
	obj2.src = "/atgames/html/game/index.html";
}

function execResWrite(id) {
	var mf = document.MainForm;
	mf.rescomment.value = trim(mf.rescomment.value);

	document.formres.res.value = mf.rescomment.value;
	document.formres.registVoiceId.value = mf.registVoiceId.value;
	document.formres.voiceType.value = mf.voiceType.value;
	document.formres.voiceURL.value = mf.voiceURL.value;

	//入力チェック
	if(checkRes()){
		document.formres.submit();
	}else{
		return false;
	}
}

/**
 * 削除する(レス)押下時処理を行ないます。
 *
 * @param resno レス番号
 */

function execDeleteRes(URL, resno) {
	//削除してよろしいですか？の確認ダイアログ
	if(!confirm(msg_del_res)) return;
	
	document.MainForm.resNo.value = resno;
	document.MainForm.action = URL;
	document.MainForm.submit();
}

/**
 * 削除する(スレッド)押下時処理を行ないます。
 *
 * @param threadId スレッドID
 */
function execDeleteThread(URL) {;
	//削除してよろしいですか？の確認ダイアログ
	if(!confirm(msg_del_thread)) return;
	
	document.MainForm.action = URL;
	document.MainForm.submit();
}

/**
 * 取消ボタン押下時処理を行ないます。
 *
 * @param aUrl 遷移先URL
 */
function executeBack(aUrl) {
	form = document.MainForm;

	form.action = aUrl;

	form.submit();
}

/**
 * スレッド作成時の入力チェックを行います。
 *
 * @param aUrl 遷移先URL
 */
function checkGame(){
	
	//禁止文字列のチェックの為に同期化
	DWREngine.setTimeout(1000);		// DWR実行タイムアウト
	DWREngine.setAsync(false);		// 同期的に実行(false で同期)
	
	var oThreadTitle = document.MainForm.threadTitle;
	var oThreadText = document.MainForm.threadComment;

	oThreadTitle.value = trim(oThreadTitle.value);
	oThreadText.value = trim(oThreadText.value);
	
	if ( trim(oThreadTitle.value) == "" )
	{
		alert(msg_notitle);
		oThreadTitle.focus();
		return false;
	}
	else if ( trim(oThreadText.value) == "" )
	{
		alert(msg_notext);
		oThreadText.focus();
		return false;
	}
	else if ( CheckKeepOutString("threadTitle") == false )
	{
		alert("タイトルに使用禁止文字列が含まれています");
		return false;
	}
	else if ( CheckKeepOutString("threadComment") == false )
	{
	    alert("メッセージに使用禁止文字列が含まれています");
		return false;
	}
	
	return true;
}

/**
 * レス投稿時の入力チェックを行います。
 *
 * @param aUrl 遷移先URL
 */

function checkRes(){
	
	//禁止文字列のチェックの為に同期化
	DWREngine.setTimeout(1000);		// DWR実行タイムアウト
	DWREngine.setAsync(false);		// 同期的に実行(false で同期)

	var oThreadRes = document.MainForm.rescomment;
	oThreadRes.value = trim(oThreadRes.value);
	
	if ( trim(oThreadRes.value) == "" )
	{
		alert(msg_nores);
		oThreadRes.focus();
		return false;
	}
	else if ( CheckKeepOutString("rescomment") == false )
	{
		alert("コメントに使用禁止文字列が含まれています");
		return false;
	}
	
	return true;
}

function execEdit() {
	document.formedit.submit();
}

function exceThreadEdit(url){
	
	//エラーがあった場合はリターン
	if(!checkGame()){
		return false;
	}

	form = document.MainForm;

	form.action = url;

	form.submit();
}

function exceMakeThread(){
	
	//エラーがあった場合はリターン
	if(!checkGame()){
		return false;
	}

	document.MainForm.submit();
}

function execReturnTop(path) {

	location.href = path;
}

function execActiveX(game_id,objname)
{
//パチンコ　メンテナンスモード切り替え
/*
	if(game_id == 1) {
		alert("現在メンテナンス中です。");
		return;
	}
//*/
	if(!window.ActiveXObject)
	{
	alert('申し訳ございません。\nこのゲームは、Windows の InternetExplorer のみ\n対応となっております。');
	}
	else
	{
		var obj = document.getElementById(objname);
		ClickCount.countStartButtonClicked(game_id);
		if(!obj || obj.object == null){
			location.href = "/atgames/gameActiveXAction.do?gameId="+game_id+"&sessionId="+getCookie("SESSIONID")+"&result=100";
		}else{
			ATGames.StartGame();
		}
	}
}

/**
 * 貼り付けてあるボイスを外す
 *
 */
function deleteVoiceComment(actionUrl, resNo) {
	document.MainForm.resNo.value = resNo;
	document.MainForm.action = actionUrl;
	document.MainForm.submit();
}

// ルームを見る
function viewRoom(accountId) {
	location.href="/atgames/mypage.do?accountId="+accountId;
	//window.open('http://www.atgames.jp/atgames/viewRoom.do?accountId=' + accountId, 'viewRoom', 'resizable=no, toolbar=no, location=no, status=no, menubar=no, scrollbars=no, width=790, height=350');
}

// ルームへ行く
function roomOpener(accountId)
{
	var RoomUrl="/atgames/room.do?account_id="+accountId;
	setUpSelfyTownWindow(RoomUrl);
/*
if(top.document.getElementById('main_avatar')){
		var Tutrial_CheckUrl= new String(top.main_avatar.location);	//urlを格納
		if(Tutrial_CheckUrl.match(/room.do/i)){	//タウン表示の確認
			top.main_avatar.visit(accountId);
		}else{
			alert("セルフィエリアを表示させてから、\nご利用ください");
		}
 	} else {
		alert('セルフィエリアを開いてから、\nご利用ください');
	}*/
}
