function getIDServerHost(isSSL) {
	if(location.hostname.indexOf("test") >= 0 || location.hostname.indexOf("alpha-") >= 0) {
		return ((isSSL == true) ? "https" : "http") + "://alpha-id.hangame.co.jp";
	} else if(location.hostname.indexOf("dev") >= 0) {
		return ((isSSL == true) ? "http" : "http") + "://alpha-id.hangame.co.jp";
	} else {
		return ((isSSL == true) ? "https" : "http") + "://id.hangame.co.jp";
	}
}

function setStrCookieExpire(strName, strValue, intExpireDay) {
	var exDate = new Date;
	exDate.setMonth(exDate.getMonth() + intExpireDay);
	document.cookie = strName + "=" + strValue + ";expires=" + exDate.toGMTString() + ";domain=" + getFixDomain() + ";path=/";
	return "";
}

// フォームの初期設定を行う。
// ログインフォームのあるページのonloadイベントで呼び出す必要がある。
//
// form : 通常はnull。ログインフォームの名前を変えないといけない場合のみ指定
// isFocuesd : 通常はnull。ログインパネルにフォーカスを移したくない場合のみfalseを指定
var formLogin
function initForm(form, isFocused){
	formLogin = (form == null) ? document.loginform : form;
	var focusedObj; 
	
	if(typeof(formLogin) == "object" && typeof(formLogin.strmemberid) == "object"){
		if(getStrCookie("LOGIN_IDSAVE") != "" && typeof(formLogin.chkIdSave) == "object") {
			formLogin.strmemberid.value = getStrCookie("LOGIN_IDSAVE");
			formLogin.chkIdSave.checked = true;
			try {
				focusedObj = formLogin.strpassword;
			} catch(e) {
			}
		} else {
			try {
				focusedObj = formLogin.strmemberid;
			} catch(e) {
			}
		}
		
		if (focusedObj != null && isFocused != false) {
			focusedObj.focus();
		}
	}
}

var isLoginProcessing = false;
var loginTimerId = 0;
// ログイン前のvalidationチェックおよびCookieへのID書き込み処理を行う。
function entersubmit() {
	if (isLoginProcessing && loginTimerId != 0) {
		return;
	}
	
	if (formLogin.strmemberid.value.length == 0 || formLogin.strpassword.value.length == 0) {
		alert("IDまたはパスワードが入力されていません。");
		formLogin.strmemberid.focus();
	} else {
		if (formLogin.strmemberid.value.indexOf(',') >= 0) {
			alert("IDに特殊文字は入力できません。");
		} else if (formLogin.strpassword.value.indexOf(',') >= 0) {
			alert("パスワードに特殊文字は入力できません。");
		} else {
			if (typeof(formLogin.chkSSLLogin) == "object" && formLogin.chkSSLLogin.checked) {
				formLogin.action = getIDServerHost(true) + "/login.asp";
			} else {
				formLogin.action = getIDServerHost(false) + "/login.asp";
			}
			if (typeof(formLogin.chkIdSave) == "object") {
				if (formLogin.chkIdSave.checked) {
					setStrCookieExpire("LOGIN_IDSAVE", formLogin.strmemberid.value, 365);
				} else {
					setStrCookieExpire("LOGIN_IDSAVE", "", 0);
				}
			}
			formLogin.strpassword.value = formLogin.strpassword.value.substring(0, 8);
			formLogin.submit();
			isLoginProcessing = true;
			loginTimerId = setTimeout(loginTimeout, 5000);
		}
	}
}

// 5秒以上レスポンスがない場合、ログイン処理中フラグを初期化する
function loginTimeout() {
	isLoginProcessing = false;
	loginTimerId = 0;
}

// ID, パスワード入力フォームで「Enter」キーを押されたときの操作を行う。
// n : 0〜1の数値が入る。0はID入力フォーム、1はパスワード入力フォームをあらわす
function entersubmit_key(n, keyCode){
	if(typeof(keyCode) == "undefined"){
		keyCode = event.keyCode;
	}
	if( keyCode==13 ){
		if( n == 0 ){
			formLogin.strpassword.focus();
		} else {
			entersubmit();
		}
	}
}


// 汎用ログインフォームの表示を行う。
// デザインが合わないなどの理由でこのfunctionを使用しないときは、
// formオブジェクトの実装、javascriptの設定などは、各自行わなければならない。
//
// intType       : 1〜3の数値が入る。これによって、表示するログインフォームのタイプが違う。
// strEntryURL   : 「無料会員登録」ボタンが押下されたときのURL。空文字のだと、「無料会員登録」ボタン自体が表示されない
// strNextURL    : ログイン処理後にリダイレクトする先のURL
// strTarget     : 「ID/パスワードを忘れた方はこちら」ボタンおよび「無料会員登録」ボタンが押下されたときのリンク先のtarget(_self, _top, _blankなど)
// strFormID     : どこからログインしたのかを識別するための、ページごとに一意なIDが入る
// strFormTarget : ログイン処理を行うフォームのtarget=ログイン処理後のtarget(_self, _top, _blankなど)
// strFormName   : 通常は空文字もしくはnull。ログインフォームの名前を変えないといけない場合のみ指定
//
// 例)
//	<script type="text/javascript">
//	<!--
//	writeLoginForm(
//                 1,
//                 "https://member.hangame.co.jp/registration/index.nhn?kind=at",
//                 "http://alteil.hangame.co.jp/",
//                 "_top",
//                 "pubat",
//                 "_self"
//                 );
//	//-->
//	</script>
function writeLoginForm(intType, strEntryURL, strNextURL, strTarget, strFormID, strFormTarget, strFormName) {
	strFormName = (strFormName == null || strFormName == "") ? "loginform" : strFormName;

	var strFormHTML = "";
	var arrSizeList01 = ["146", "160", "180"];
	var arrSizeList02 = ["135", "145", "160"];
	var arrSizeList03 = [ "70",  "80", "100"];
	var arrSizeList04 = [  "2",   "3",   "6"];
	var arrImgSrcList01 = [
		["http://images.hangame.co.jp/_images/top200506/loginbox_160/btn_login_off.gif", "http://images.hangame.co.jp/_images/top200506/loginbox_160/btn_login_on.gif", "140", "22"],
		["http://images.hangame.co.jp/_images/top200506/loginbox_170/btn_login_off.gif", "http://images.hangame.co.jp/_images/top200506/loginbox_170/btn_login_on.gif", "147", "22"],
		["http://images.hangame.co.jp/_images/info/login/common/btn_login_off.gif",      "http://images.hangame.co.jp/_images/info/login/common/btn_login_on.gif",      "168", "22"]
		];
	var arrImgSrcList02 = [
		["http://images.hangame.co.jp/_images/top200506/loginbox_160/btn_entry_off.gif",   "http://images.hangame.co.jp/_images/top200506/loginbox_160/btn_entry_on.gif",   "140", "40"],
		["http://images.hangame.co.jp/_images/top200506/loginbox_170/btn_entry_s_off.gif", "http://images.hangame.co.jp/_images/top200506/loginbox_170/btn_entry_s_on.gif", "147", "40"],
		["http://images.hangame.co.jp/_images/top200506/loginbox_170/btn_entry_s_off.gif", "http://images.hangame.co.jp/_images/top200506/loginbox_170/btn_entry_s_on.gif", "147", "40"]
		];

	strFormHTML += "<style type=\"text/css\">";
	strFormHTML += "<!--";
	strFormHTML += "input.stat_input { width:98px;";
	strFormHTML += " height:20px;";
	strFormHTML += " font-size: 12px; color: #666666; line-height: 120%;";
	strFormHTML += "}";
	strFormHTML += "-->";
	strFormHTML += "</style>";
	strFormHTML += "<div align=\"center\">";
	strFormHTML += "<table width=\"" + arrSizeList01[intType-1] + "\" height=\"164\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
	strFormHTML += "<form name=\"" + strFormName + "\" method=\"post\" action=\"" + getIDServerHost(true) + "/login.asp\" target=\"" + strFormTarget + "\">";
	strFormHTML += "<tr>";
	strFormHTML += "<td align=\"center\"><table width=\"" + arrSizeList02[intType-1] + "\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
	strFormHTML += "<tr>";
	strFormHTML += "<td><img src=\"http://images.hangame.co.jp/_images/top200506/img_top/login_id.gif\" alt=\"ハンゲームID\" width=\"62\" height=\"11\"></td>";
	strFormHTML += "<td><img src=\"http://images.hangame.co.jp/_images/top200506/img_top/shim.gif\" width=\"4\" height=\"3\"></td>";
	strFormHTML += "<td><input type=\"text\" class=\"stat_input\" style=\"width:" + arrSizeList03[intType-1] + "px;height:17px;\" name=\"strmemberid\" tabindex=\"1\" onKeyDown=\"entersubmit_key(0)\"></td>";
	strFormHTML += "</tr>";
	strFormHTML += "<tr>";
	strFormHTML += "<td colspan=\"3\"><img src=\"http://images.hangame.co.jp/_images/top200506/img_top/shim.gif\" width=\"2\" height=\"1\"></td>";
	strFormHTML += "</tr>";
	strFormHTML += "<tr>";
	strFormHTML += "<td><img src=\"http://images.hangame.co.jp/_images/top200506/img_top/login_pw.gif\" alt=\"パスワード\" width=\"62\" height=\"12\"></td>";
	strFormHTML += "<td><img src=\"http://images.hangame.co.jp/_images/top200506/img_top/shim.gif\" width=\"4\" height=\"5\"></td>";
	strFormHTML += "<td><input type=\"password\" class=\"stat_input\" style=\"width:" + arrSizeList03[intType-1] + "px;height:17px;\" maxlength=\"8\" name=\"strpassword\" tabindex=\"2\" onKeyDown=\"entersubmit_key(1)\"></td>";
	strFormHTML += "</tr>";
	strFormHTML += "</table></td>";
	strFormHTML += "</tr>";
	strFormHTML += "<tr>";
	strFormHTML += "<td align=\"center\"><a href=\"javascript:void(entersubmit());\"><img src=\"" + arrImgSrcList01[intType-1][0] + "\" alt=\"ハンゲームにログイン\" width=\"" + arrImgSrcList01[intType-1][2] + "\" height=\"" + arrImgSrcList01[intType-1][3] + "\" border=\"0\" tabindex=\"3\" onMouseOver=\"this.src='" + arrImgSrcList01[intType-1][1] + "'\" onMouseOut=\"this.src='" + arrImgSrcList01[intType-1][0] + "'\" onkeydown=\"entersubmit_key(1)\"></a></td>";
	strFormHTML += "</tr>";
	strFormHTML += "";
	strFormHTML += "<tr>";
	strFormHTML += "<td style=\"padding:0px 0px 0px " + arrSizeList04[intType-1] + "px\"><input type=\"checkbox\" name=\"chkIdSave\" id=\"chkIdSave\" style=\"margin:0px\" tabindex=\"4\"><label for=\"chkIdSave\"><img src=\"http://images.hangame.co.jp/_images/top200506/img_top/btn_save.gif\" width=\"77\" height=\"9\" vspace=\"2\" border=\"0\" name=\"idsave\" onclick=\"chkIdSave.checked=(chkIdSave.checked==true)?false:true;\"></label></td>";
	strFormHTML += "</tr>";
	strFormHTML += "<tr>";
	strFormHTML += "<td height=\"10\" style=\"padding:0px 0px 0px " + arrSizeList04[intType-1] + "px\"><input type=\"checkbox\" name=\"chkSSLLogin\" id=\"chkSSLLogin\" style=\"margin:0px\" checked tabindex=\"5\"><label for=\"chkSSLLogin\"><img src=\"http://images.hangame.co.jp/_images/top200506/img_top/btn_ssl.gif\" name=\"idsave\" width=\"114\" height=\"9\" vspace=\"2\" border=\"0\" onclick=\"chkSSLLogin.checked=(chkSSLLogin.checked==true)?false:true;\"></label></td>";
	strFormHTML += "</tr>";
	strFormHTML += "<tr>";
	strFormHTML += "<td align=\"right\"><a href=\"https://member.hangame.co.jp/forgot/\" target=\"" + strTarget + "\"><img src=\"http://images.hangame.co.jp/_images/top200506/img_top/btn_idpwforget_off.gif\" width=\"115\" height=\"10\" hspace=\"7\" vspace=\"2\" border=\"0\" tabindex=\"6\" onMouseOver=\"this.src='http://images.hangame.co.jp/_images/top200506/img_top/btn_idpwforget_on.gif'\" onMouseOut=\"this.src='http://images.hangame.co.jp/_images/top200506/img_top/btn_idpwforget_off.gif'\"></a></td>";
	strFormHTML += "</tr>";
	if(strEntryURL != "") {
		strFormHTML += "<tr>";
		strFormHTML += "<td align=\"center\"><a href=\"" + strEntryURL + "\" target=\"" + strTarget + "\"><img src=\"" + arrImgSrcList02[intType-1][0] + "\" alt=\"無料会員登録はこちら\" width=\"" + arrImgSrcList02[intType-1][2] + "\" height=\"" + arrImgSrcList02[intType-1][3] + "\" border=\"0\" tabindex=\"7\" onMouseOver=\"this.src='" + arrImgSrcList02[intType-1][1] + "'\" onMouseOut=\"this.src='" + arrImgSrcList02[intType-1][0] + "'\"></a></td>";
		strFormHTML += "</tr>";
	}
	strFormHTML += "<input type=\"hidden\" name=\"nxtURL\" value=\"" + strNextURL + "\">";
	strFormHTML += "<input type=\"hidden\" name=\"from\"   value=\"" + strFormID + "\">";
	strFormHTML += "</form>";
	strFormHTML += "</table>";
	strFormHTML += "</div>";

	document.write(strFormHTML);
}