﻿var expa = {
	Version:	'1.0.0.0',
	test:		0,	//0:本番環境　1:社内デバッグ
	urloc:		"",	//url
	log_in:		0,	//1:ログイン中 0:ログアウト
	log_out:	0,	//1:ログアウト要求中　0:通常
	nknam:		"",	//ニックネーム
	pw:			"",	//パスワード
	rlmod:		0	//所属ページ　top:トップページ　login:ログイン　regi:登録
};

switch(expa.test) {
case 0:
	expa.urloc = 'http://www.firmring.com/expansion/';
	break;
case 1:
	expa.urloc = 'http://localhost/expansion/';
	break;
default:
	expa.urloc = 'http://localhost/expansion/';
	break;
}


/* イベント・リスナのセット関数オブジェクトを定義 */
function addListener(elm, type, func) {
  if(!elm) { return false; }
  if(elm.addEventListener) { /* W3C準拠ブラウザ用 */
   	elm.addEventListener(type, func, false);
  } else if(elm.attachEvent) { /* Internet Explorer用 */
    elm.attachEvent('on'+type, func);
  } else {
    return false;
  }
  return true;
}

function removeListener(elm, type, func) {
  if(!elm) { return false; }
  if(elm.removeEventListener) { /* W3C準拠ブラウザ用 */
   	elm.removeEventListener(type, func, false);
  } else if(elm.attachEvent) { /* Internet Explorer用 */
    elm.detachEvent('on'+type, func);
  } else {
    return false;
  }
  return true;
}

/* 要素の削除 */
function delete_node(id_txt) {//id_txt=<div>id
	var dom_obj, dom_obj_parent;
	dom_obj = $(id_txt);
	if(dom_obj) {
		dom_obj_parent = dom_obj.parentNode;
		dom_obj_parent.removeChild(dom_obj);
	}
}

/* 子要素の削除 */
function delete_childNode(id_txt) {
	var dom_obj, dom_obj_firstChild;
	dom_obj = $(id_txt);
	dom_obj_firstChild = dom_obj.firstChild;
	if(dom_obj_firstChild) {
		while(dom_obj_firstChild.nextSibling) {
			dom_obj.removeChild(dom_obj_firstChild.nextSibling);
		}
		dom_obj.removeChild(dom_obj_firstChild);
	}
}
	
/*　初期設定 */	
function exp_init() {
	expa.nknam	= get_cookie("nknam");
	expa.pw	= get_cookie("pw");
	if(expa.nknam.length > 0 && expa.pw.length > 0) {
		readInfoLogin(expa.nknam, expa.pw, 0);
	}
	else {
		print_left_menu();
	}
}

function getWindowWidth() {
	if(typeof window.innerWidth !== "undefined") {
		return window.innerWidth;
	} else {
		return document.body.clientWidth;
	}
}

function getWindowHeight() {
	if(typeof window.innerHeight !== "undefined") {
		return window.innerHeight;
	} else if(document.compatMode === 'CSS1Compat') {
		return document.documentElement.clientHeight;
	} else {
		return document.body.clientHeight;
	}
}

function exp_footer() {
	var oDiv, fDiv, txt;
	
	oDiv = $("wrap");
	fDiv = document.createElement("div");
	fDiv.setAttribute("id", "footer");
	txt = "&copy;2009 兵棋乱戦　製作実行委員会";
	fDiv.innerHTML	= txt;
	oDiv.appendChild(fDiv);
}

function exp_footer_move(mode) {
	var F_WI, F_HI, F_TOP, F_LFT, fDiv, txt, size;
	
	F_WI = getWindowWidth();
	F_LFT = ""+(Math.floor((F_WI/2))-100)+"px";
	if(mode === 0) {
		F_HI = getWindowHeight();
		F_TOP = ""+(F_HI-30)+"px";
	}
	else {
		size = $("top").getDimensions();
		F_TOP = ""+(size.height-20)+"px";
		F_LFT = "434px";
	}
	fDiv = $("footer");
	fDiv.style.top	= F_TOP;
	fDiv.style.left	= F_LFT;

	if(mode === 1) {
		fDiv.style.color	= "#666666";
	}
}

//ログイン情報
function readInfoLogin(name, password, blogin) {
	var url = expa.urloc+'php/login.php';
	var paramList = "cache=" + (new Date().getTime());
	var fmData = "nknam="	+ name
				+"&pw="		+ password
				+"&blogin="	+ blogin;

	new Ajax.Request(url, {
		method : "post",
		parameters:	paramList,
		postBody  : encodeURI(fmData),
		onSuccess :	loginChkOK,
		onFailure : loginNG,
		onException:	loginException
	});

	function loginException(transport, ex) {
		var msg = "ログイン送受信エラー："+ex.message;
		if($("result")) {
			$("result").innerHTML = msg;
		}
		else {
			window.alert(msg);
		}
	}

	function loginNG() {
		errMessage(901);
	}
	
	function errMessage(err) {
		var msg;
		var errList = {
			"1":	"",
			"2":	"",
			"31":	"",
			"32":	"",
			"101":	"未登録です\n会員登録をしてください",
			"102":	"登録抹消されています\n会員登録をしてください",
			"103":	"パスワードが違います",
			"901":	"",
			"903":	""
				
		};
		if(errList[err]) {
			msg = errList[err];
		}
		else {
			msg = err+":　エラー";
		}
		
		if($("result")) {
			$("result").innerHTML = msg;
		}
		else {
			window.alert(msg);
		}
	}

	function loginChkOK(httpObj) {
		var ret, json, val;

		ret = httpObj.responseText;
		if(!ret) {
			errMessage(903);
			return;
		}
		json = eval("(" + ret + ")");
		if(json.status.type === "ng") {
			if(!blogin) {
				expa.log_in = 0;
				print_left_menu();
			}
			else {
				errMessage(json.status.value+100);
			}
			return;
		}
		else if(json.status.type === "error") {
			errMessage(json.status.value);
			if(!blogin) {
				expa.log_in = 0;
				print_left_menu();
			}
			return;
		}
		else if(json.status.type === "ok") {
			if(!blogin) {
				expa.log_in = json.status.value-1;
				print_left_menu();
			}
			else {
				location.href = expa.urloc+"screen01.htm";
			}
		}
	}
}

function print_left_menu() {
	var leftMenu, txt;
	expa.nknam = get_cookie("nknam");
	leftMenu = $("left_menu");
	txt = 
		"<ul>"+
		"<p class='title'>メニュー</p>";
	if(expa.rlmod !== 'top') {
		txt += "<li><a href='./index.html'>トップへ</a></li>";
	}
	if(expa.rlmod !== 'screen1') {
		txt += "<li><a href='./register.htm'>登録</a></li>";
	}
	if(expa.log_in === 1) {
		txt += "<li><a href='./logout.htm'>ログアウト</a></li>";
	}
	else {
		txt += "<li><a href='./login.htm'>ログイン</a></li>";
	}
	if(expa.rlmod !== 'help') {
		txt += "<li><a href='javascript:wndHelp(0);'>ゲームの説明</a></li>";
	}
	
	leftMenu.innerHTML= txt;
	$$("a").each(function(ele) {
		ele.style.cursor = "default";
	});
}

function wndHelp(bPHP) {
	if(bPHP) {
		$htm = '../help.htm';
	}
	else {
		$htm = './help.htm';
	}
	window.open($htm, null, 'width=840, height=800, menubar=yes, toolbar=no, scrollbars=yes');
}

function clickLogOut() {
	Dialog.confirm("ログアウトしますか？", {
		windowParameters:	{ width:320, height:100 },
		okLabel:		"ログアウトする",
		cancelLabel:	"戻る",
		ok:				logOut,
		cancel:			function(win) { return true; }
	});
}

//クッキー
function get_cookie(key) {
	var i, index, splitted;
	var sstr = key + "=";
	var sstrlen = sstr.length;
	splitted = document.cookie.split("; ");

	for(i = 0; i < splitted.length; i++) {
		if (splitted[i].substring(0, sstrlen) === sstr) {
			return unescape(splitted[i].substring(sstrlen));
		}
	}
	return "";
}
function set_cookie(key, val) {
	document.cookie =
	key + "=" + escape(val) + "; expires=Wed, 01-Jan-2031 00 GMT;";
}
function set_cookies() {
	set_cookie("nknam",	document.inp_info.nknam.value);
	set_cookie("email",	document.inp_info.email.value);
	set_cookie("pw",	document.inp_info.pw.value);
}

