/*@cc_on _d=document;eval('var document=_d')@*/
// *************************************************
// ** census.jp
// ** (C) 2008 ale/takuru.
// *************************************************
var censusversion = "1.52"; 
//[last-modified: Jun 03, 2008]

//history
var hist = 
"[Jun 03, 2008] Ver. 1.52\n コードミスを修正orz\n 指摘ありがとうございました\n" +
"[May 28, 2008] Ver. 1.51\n IE以外のUAでも(一応)動作するようになったかも\n ただしクリップボード転送は無理かも？\n バグレポありがとうございました\n" +
"[Mar 11, 2008] Ver. 1.50\n いくつか修正と追記で気持ち高速化\n" +
"[Mar 11, 2008] Ver. 1.43\n 使えなくなっていたのを修正ごめんなさい\n" +
"[Mar 05, 2008] Ver. 1.42\n どうでもいい機能追加\n Another HTML-lint gatewayが100点にｗ\n" +
"[Feb 18, 2008] Ver. 1.41\n 鯖選択の不具合修正\n" +
"[Feb 17, 2008] Ver. 1.4\n 自動コピーと簡単な修正(でふぉLv35とか)\n" +
"[Jan 13, 2008] Ver. 1.3\n 今更ながらFとZ追加。\n" +
"[Mmm dd, 2007] Ver. 1.2\n 鯖内の国別人口比の出力、更新履歴追加\n" +
"[Mmm dd, 2007] Ver. 1.1\n 時刻を2桁に統一。色つけ。出力をphp化。細部修正\n" +
"[Mmm dd, 2007] Ver. 1.0\n 初版";

var tipsflg = new Array(false,false);

function tips(n)
{
	var tipsname = 'tips' + n;
	var tipsname = document.getElementById(tipsname);
	if (tipsflg[n])
	{
		tipsname.style.display = 'none';
		tipsflg[n] = false;
	}
	else
	{
		tipsname.style.display = 'block';
		tipsflg[n] = true;
	}
}

function selectText()
{
	var box_2ch_text = document.getElementById('box_2ch_text');
	box_2ch_text.focus();
	box_2ch_text.select();
}

function historylog() {
	document.write("history:<br />\n<textarea cols=\"40\" rows=\"2\">\n");
	document.write(hist);
	document.write("<\/textarea>\n");
}

function watch() {
	var monthword = new Array('Jan','Feb','Mar','Apl','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
	var now = new Date();
	var yea = now.getYear();
	var mon = now.getMonth();
	var day = now.getDate();
	var hou = now.getHours();
	var min = now.getMinutes();
	var sec = now.getSeconds();

	if (yea < 1000) { yea += 1900; }
	//digit 2
	if (day < 10) { day = '0' + day; }
	if (hou < 10) { hou = '0' + hou; }
	if (min < 10) { min = '0' + min; }
	if (sec < 10) { sec = '0' + sec; }

	//output
	document.forms['inform'].timnow.value = hou + ':' + min + ':' + sec + ' - ' + day + ' ' + monthword[mon] + ', ' + yea;

	//refresh 1000ms
	setTimeout("watch()",1000);
}

function lvlist() {
	document.write('<select id="lvlist" name="lvlist">');
	for (i=1;i<=40;i++) {
		if (i<10) {
			document.write('<option value=\"0' + i + '"');
		} else {
			document.write('<option value=\"' + i + '"');
		}
		if (i==35) { document.write(' selected="selected"'); }
		document.write('>'+ i + '<\/option>');
	}
	document.write('<\/select>');
}

function puttimer() {
	var now = new Date();
	var hou = now.getHours();
	var min = now.getMinutes();

	var flg = new Array('timbeghou','timbegmin','timendhou','timendmin');
	for (cnt=0;cnt<4;cnt++) {
		document.write('<select id="' + flg[cnt] + '" class="timestamp" name="' + flg[cnt] + "\">\n");
		if (cnt%2==0) {
			for (i=0;i<24;i++) {
				document.write('<option value=\"' + i + '"');
				if (i==hou) { document.write(' selected="selected"'); }
				document.write('>'+ i + "<\/option>\n");
			}
		} else {
			for (i=0;i<60;i++) {
				document.write('<option value=\"' + i + '"');
				if (i==min) { document.write(' selected="selected"'); }
				document.write('>'+ i + "<\/option>\n");
			}
		}
		document.write("<\/select>\n");
		if (cnt==1) { document.write('<input type="button" id="beg" name="beg" value="&lt;&lt;集計開始" onClick="javascript:timestamp(\'beg\');" />'); }
		if (cnt==3) { document.write('<input type="button" id="end" name="end" value="&lt;&lt;集計終了" onClick="javascript:timestamp(\'end\');" />'); }
	}
}

function chgsv(obj) {
	svid = obj.value;
	document.forms['inform'].server.value = svid;
}

function chgchk(obj) {
	if(obj.value == 'on')
	{
		obj.value = "off";
	}
	else
	{
		obj.value = "on";
	}
}

function nowdate() {
	var now = new Date();
	var yea = now.getYear();
	if (yea < 1000) { yea += 1900; }
	var mon = now.getMonth()+1;
	var day = now.getDate();

	document.write('<select id="ls_date_year" class="ls_date" name="year">');
	for (i=2007;i<=2009;i++) {
		document.write('<option value=\"' + i + '"');
		if (i==yea) { document.write(' selected="selected"'); }
		document.write('>'+ i + '<\/option>');
	}
	document.write('<\/select>');

	document.write('<select id="ls_date_month" class="ls_date" name="month">');
	for (i=1;i<=12;i++) {
		document.write('<option value=\"' + i + '"');
		if (i==mon) { document.write(' selected="selected"'); }
		document.write('>'+ i + '<\/option>');
	}
	document.write('<\/select>');

	document.write('<select id="ls_date_day" class="ls_date" name="day">');
	for (i=1;i<=31;i++) {
		document.write('<option value=\"' + i + '"');
		if (i==day) { document.write(' selected="selected"'); }
		document.write('>'+ i + '<\/option>');
	}
	document.write('<\/select>');
}

function timestamp(flg) {
	var now = new Date();
	var hou = now.getHours();
	var min = now.getMinutes();
	var inform = document.forms['inform'];

	if ( flg == 'beg' ) {
		inform.timbeghou.selectedIndex = hou;
		inform.timbegmin.selectedIndex = min;
	} else if ( flg == 'end' ) {
		inform.timendhou.selectedIndex = hou;
		inform.timendmin.selectedIndex = min;
	}
}

function adjust (x) {
	var flg = ""+x;
	switch (flg.length) {
		case 1:
			//x 頭に0末に.0
			x = '0' + x + '.0';
			break;
		case 2:
			//xx 末に.0つける
			x = x + '.0';
			break;
		case 3:
			//x.x 頭に0つける (100でも一致するけど無視)
			x = '0' + x;
			break;
		default:
	}
	return x;
}

function adjust2(key) {
	var key = ""+key;
	switch (key.length) {
		case 1:
			key = " . . ." + key;
			break;
		case 2:
			key = " . ." + key;
			break;
		case 3:
			key = " ." + key;
			break;
	}
	return key;
}

function calc() {
	//値読み込み
	var inform = document.forms['inform'];
	var nWar = eval(inform.nWar.value);
	var nSco = eval(inform.nSco.value);
	var nSor = eval(inform.nSor.value);
	var nLvl = eval(inform.nLvl.value);
	var cWar = eval(inform.cWar.value);
	var cSco = eval(inform.cSco.value);
	var cSor = eval(inform.cSor.value);
	var cLvl = eval(inform.cLvl.value);
	var iWar = eval(inform.iWar.value);
	var iSco = eval(inform.iSco.value);
	var iSor = eval(inform.iSor.value);
	var iLvl = eval(inform.iLvl.value);
	var hWar = eval(inform.hWar.value);
	var hSco = eval(inform.hSco.value);
	var hSor = eval(inform.hSor.value);
	var hLvl = eval(inform.hLvl.value);
	var gWar = eval(inform.gWar.value);
	var gSco = eval(inform.gSco.value);
	var gSor = eval(inform.gSor.value);
	var gLvl = eval(inform.gLvl.value);
	var sv = eval(inform.server.value);
	var ye = inform.year.value;
	var mo = inform.month.value;
	var da = inform.day.value;
	var tbh = inform.timbeghou.value;
	if (tbh < 10) { tbh = '0'+tbh; }
	var tbm = inform.timbegmin.value;
	if (tbm < 10) { tbm = '0'+tbm; }
	var teh = inform.timendhou.value;
	if (teh < 10) { teh = '0'+teh; }
	var tem = inform.timendmin.value;
	if (tem < 10) { tem = '0'+tem; }
	var autocopyflg = inform.autocopy.value;


	//入力漏れチェック
	var chklist = new Array('nWar','nSco','nSor','nLvl','cWar','cSco','cSor','cLvl','iWar','iSco','iSor','iLvl','hWar','hSco','hSor','hLvl','gWar','gSco','gSor','gLvl');
	var flg = 0;
	for (i=0;i<20;i++) {
		if ( eval(chklist[i] + ' == undefined') ) {
			flg = 1;
		}
	}
	if (flg) {
		alert("入力漏れがあります");
	} else {

		//テーブルヘッダ
		switch (sv) {
			case 1:
				sv = "A鯖: ";
				break;
			case 2:
				sv = "B鯖: ";
				break;
			case 3:
				sv = "C鯖: ";
				break;
			case 4:
				sv = "D鯖: ";
				break;
			case 5:
				sv = "E鯖: ";
				break;
			case 6:
				sv = "F鯖: ";
				break;
			case 7:
				sv = "Z鯖: ";
				break;
		}

		var headmsg = "■"+ sv + mo + "月" + da + "日 " + tbh + ":" + tbm + " ～ " + teh + ":" + tem + " 調べ";


		//国民合計
		var nTotal = nWar + nSco + nSor;
		var cTotal = cWar + cSco + cSor;
		var iTotal = iWar + iSco + iSor;
		var hTotal = hWar + hSco + hSor;
		var gTotal = gWar + gSco + gSor;

		//国別職比率(War)
		var nWarPerc = "("+adjust(Math.round(nWar * 1000 / nTotal) / 10)+"%)";
		var cWarPerc = "("+adjust(Math.round(cWar * 1000 / cTotal) / 10)+"%)";
		var iWarPerc = "("+adjust(Math.round(iWar * 1000 / iTotal) / 10)+"%)";
		var hWarPerc = "("+adjust(Math.round(hWar * 1000 / hTotal) / 10)+"%)";
		var gWarPerc = "("+adjust(Math.round(gWar * 1000 / gTotal) / 10)+"%)";

		//国別職比率(Sco)
		var nScoPerc = "("+adjust(Math.round(nSco * 1000 / nTotal) / 10)+"%)";
		var cScoPerc = "("+adjust(Math.round(cSco * 1000 / cTotal) / 10)+"%)";
		var iScoPerc = "("+adjust(Math.round(iSco * 1000 / iTotal) / 10)+"%)";
		var hScoPerc = "("+adjust(Math.round(hSco * 1000 / hTotal) / 10)+"%)";
		var gScoPerc = "("+adjust(Math.round(gSco * 1000 / gTotal) / 10)+"%)";

		//国別職比率(Sor)
		var nSorPerc = "("+adjust(Math.round(nSor * 1000 / nTotal) / 10)+"%)";
		var cSorPerc = "("+adjust(Math.round(cSor * 1000 / cTotal) / 10)+"%)";
		var iSorPerc = "("+adjust(Math.round(iSor * 1000 / iTotal) / 10)+"%)";
		var hSorPerc = "("+adjust(Math.round(hSor * 1000 / hTotal) / 10)+"%)";
		var gSorPerc = "("+adjust(Math.round(gSor * 1000 / gTotal) / 10)+"%)";

		//国別廃比率
		var nLvlPerc = "("+adjust(Math.round(nLvl * 1000 / nTotal) / 10)+"%)";
		var cLvlPerc = "("+adjust(Math.round(cLvl * 1000 / cTotal) / 10)+"%)";
		var iLvlPerc = "("+adjust(Math.round(iLvl * 1000 / iTotal) / 10)+"%)";
		var hLvlPerc = "("+adjust(Math.round(hLvl * 1000 / hTotal) / 10)+"%)";
		var gLvlPerc = "("+adjust(Math.round(gLvl * 1000 / gTotal) / 10)+"%)";

		//クラス別合計
		var WarTotal = nWar + cWar + iWar + hWar + gWar;
		var ScoTotal = nSco + cSco + iSco + hSco + gSco;
		var SorTotal = nSor + cSor + iSor + hSor + gSor;

		//鯖廃合計
		var LvlTotal = nLvl + cLvl + iLvl + hLvl + gLvl;

		//鯖合計
		var Total = WarTotal + ScoTotal + SorTotal;

		//国民比率
		var nPerc = "("+adjust(Math.round(nTotal * 1000 / Total) / 10)+"%)";
		var cPerc = "("+adjust(Math.round(cTotal * 1000 / Total) / 10)+"%)";
		var iPerc = "("+adjust(Math.round(iTotal * 1000 / Total) / 10)+"%)";
		var hPerc = "("+adjust(Math.round(hTotal * 1000 / Total) / 10)+"%)";
		var gPerc = "("+adjust(Math.round(gTotal * 1000 / Total) / 10)+"%)";

		//鯖クラス比率
		var WarTotalPerc = "("+adjust(Math.round(WarTotal * 1000 / Total) / 10)+"%)";
		var ScoTotalPerc = "("+adjust(Math.round(ScoTotal * 1000 / Total) / 10)+"%)";
		var SorTotalPerc = "("+adjust(Math.round(SorTotal * 1000 / Total) / 10)+"%)";

		//鯖廃比率
		var LvlTotalPerc = "("+adjust(Math.round(LvlTotal * 1000 / Total) / 10)+"%)";

		document.getElementById('box_out').innerHTML =
			"\t\t<table id=\"tb_result\">\n" +
			"\t\t\t<thead>\n" +
			"\t\t\t\t<tr>\n" +
			"\t\t\t\t\t<td colspan=\"6\">" + headmsg + "</td>\n" +
			"\t\t\t\t</tr>\n" +
			"\t\t\t\t<tr>\n" +
			"\t\t\t\t\t<td>&nbsp;</td><th>Warrior</th><th>Scout</th><th>Sorcerer</th><th>Over"+inform.lvlist.value+"Lvl</th><th>total</th>\n" +
			"\t\t\t\t</tr>\n" +
			"\t\t\t</thead>\n" +
			"\t\t\t<tbody>\n" +
			"\t\t\t\t<tr>\n" +
			"\t\t\t\t\t<th class=\"netz\">Netzwar</th>\n" +
			"\t\t\t\t\t<td class=\"netz\">" + nWar + nWarPerc + "</td>\n" +
			"\t\t\t\t\t<td class=\"netz\">" + nSco + nScoPerc + "</td>\n" +
			"\t\t\t\t\t<td class=\"netz\">" + nSor + nSorPerc + "</td>\n" +
			"\t\t\t\t\t<td class=\"netz\">" + nLvl + nLvlPerc + "</td>\n" +
			"\t\t\t\t\t<th class=\"netz\">" + nTotal + nPerc + "</th>\n" +
			"\t\t\t\t</tr>\n" +
			"\t\t\t\t<tr>\n" +
			"\t\t\t\t\t<th class=\"cese\">Cesedria</th>\n" +
			"\t\t\t\t\t<td class=\"cese\">" + cWar + cWarPerc + "</td>\n" +
			"\t\t\t\t\t<td class=\"cese\">" + cSco + cScoPerc + "</td>\n" +
			"\t\t\t\t\t<td class=\"cese\">" + cSor + cSorPerc + "</td>\n" +
			"\t\t\t\t\t<td class=\"cese\">" + cLvl + cLvlPerc + "</td>\n" +
			"\t\t\t\t\t<th class=\"cese\">" + cTotal + cPerc + "</th>\n" +
			"\t\t\t\t</tr>\n" +
			"\t\t\t\t<tr>\n" +
			"\t\t\t\t\t<th class=\"iels\">Ielsord</th>\n" +
			"\t\t\t\t\t<td class=\"iels\">" + iWar + iWarPerc + "</td>\n" +
			"\t\t\t\t\t<td class=\"iels\">" + iSco + iScoPerc + "</td>\n" +
			"\t\t\t\t\t<td class=\"iels\">" + iSor + iSorPerc + "</td>\n" +
			"\t\t\t\t\t<td class=\"iels\">" + iLvl + iLvlPerc + "</td>\n" +
			"\t\t\t\t\t<th class=\"iels\">" + iTotal + iPerc + "</th>\n" +
			"\t\t\t\t</tr>\n" +
			"\t\t\t\t<tr>\n" +
			"\t\t\t\t\t<th class=\"hord\">Hordaine</th>\n" +
			"\t\t\t\t\t<td class=\"hord\">" + hWar + hWarPerc + "</td>\n" +
			"\t\t\t\t\t<td class=\"hord\">" + hSco + hScoPerc + "</td>\n" +
			"\t\t\t\t\t<td class=\"hord\">" + hSor + hSorPerc + "</td>\n" +
			"\t\t\t\t\t<td class=\"hord\">" + hLvl + hLvlPerc + "</td>\n" +
			"\t\t\t\t\t<th class=\"hord\">" + hTotal + hPerc + "</th>\n" +
			"\t\t\t\t</tr>\n" +
			"\t\t\t\t<tr>\n" +
			"\t\t\t\t\t<th class=\"gebu\">Geburand</th>\n" +
			"\t\t\t\t\t<td class=\"gebu\">" + gWar + gWarPerc + "</td>\n" +
			"\t\t\t\t\t<td class=\"gebu\">" + gSco + gScoPerc + "</td>\n" +
			"\t\t\t\t\t<td class=\"gebu\">" + gSor + gSorPerc + "</td>\n" +
			"\t\t\t\t\t<td class=\"gebu\">" + gLvl + gLvlPerc + "</td>\n" +
			"\t\t\t\t\t<th class=\"gebu\">" + gTotal + gPerc + "</th>\n" +
			"\t\t\t\t</tr>\n" +
			"\t\t\t</tbody>\n" +
			"\t\t\t<tfoot>\n" +
			"\t\t\t\t<tr>\n" +
			"\t\t\t\t\t<th>Total</th>\n" +
			"\t\t\t\t\t<td>" + WarTotal + WarTotalPerc + "</td>\n" +
			"\t\t\t\t\t<td>" + ScoTotal + ScoTotalPerc + "</td>\n" +
			"\t\t\t\t\t<td>" + SorTotal + SorTotalPerc + "</td>\n" +
			"\t\t\t\t\t<td>" + LvlTotal + LvlTotalPerc + "</td>\n" +
			"\t\t\t\t\t<th>" + Total + "</th>\n" +
			"\t\t\t\t</tr>\n" +
			"\t\t\t</tfoot>\n" +
			"\t\t</table>\n";

		nWar = adjust2(nWar) + nWarPerc;
		nSco = adjust2(nSco) + nScoPerc;
		nSor = adjust2(nSor) + nSorPerc;
		nLvl = adjust2(nLvl) + nLvlPerc;
		cWar = adjust2(cWar) + cWarPerc;
		cSco = adjust2(cSco) + cScoPerc;
		cSor = adjust2(cSor) + cSorPerc;
		cLvl = adjust2(cLvl) + cLvlPerc;
		iWar = adjust2(iWar) + iWarPerc;
		iSco = adjust2(iSco) + iScoPerc;
		iSor = adjust2(iSor) + iSorPerc;
		iLvl = adjust2(iLvl) + iLvlPerc;
		hWar = adjust2(hWar) + hWarPerc;
		hSco = adjust2(hSco) + hScoPerc;
		hSor = adjust2(hSor) + hSorPerc;
		hLvl = adjust2(hLvl) + hLvlPerc;
		gWar = adjust2(gWar) + gWarPerc;
		gSco = adjust2(gSco) + gScoPerc;
		gSor = adjust2(gSor) + gSorPerc;
		gLvl = adjust2(gLvl) + gLvlPerc;
		WarTotal = adjust2(WarTotal) + WarTotalPerc;
		ScoTotal = adjust2(ScoTotal) + ScoTotalPerc;
		SorTotal = adjust2(SorTotal) + SorTotalPerc;
		LvlTotal = adjust2(LvlTotal) + LvlTotalPerc;

		nTotal = adjust2(nTotal) + nPerc;
		cTotal = adjust2(cTotal) + cPerc;
		iTotal = adjust2(iTotal) + iPerc;
		hTotal = adjust2(hTotal) + hPerc;
		gTotal = adjust2(gTotal) + gPerc;
		Total = adjust2(Total);

		table2ch = headmsg + "\n.　　 　 │ヲリ　　　　　│スカ...　　　　│サラ.　 　 　 │合計　 　 　 │Lv"+inform.lvlist.value+"以上.....｜\n..　ネツ│."+nWar+"│."+nSco+"│."+nSor+"│."+nTotal+"│."+nLvl+"｜\n......カセ│."+cWar+"│."+cSco+"│."+cSor+"│."+cTotal+"│."+cLvl+"｜\n. . エル│."+iWar+"│."+iSco+"│."+iSor+"│."+iTotal+"│."+iLvl+"｜\n.....ホル│."+hWar+"│."+hSco+"│."+hSor+"│."+hTotal+"│."+hLvl+"｜\n..　ゲブ│."+gWar+"│."+gSco+"│."+gSor+"│."+gTotal+"│."+gLvl+"｜\n　.──┼─────┼─────┼─────┼─────┼─────┘\n... 　 　 │."+WarTotal+"│."+ScoTotal+"│."+SorTotal+"│."+Total+"　　　....│."+LvlTotal;
//		table2ch = headmsg + "\n.　　 　 │ヲリ　　　　　│スカ...　　　　│サラ.　 　 　 │合計│Lv"+inform.lvlist.value+"以上.....｜\n..　ネツ│."+nWar+"│."+nSco+"│."+nSor+"│"+nTotal+"│."+nLvl+"｜\n......カセ│."+cWar+"│."+cSco+"│."+cSor+"│"+cTotal+"│."+cLvl+"｜\n. . エル│."+iWar+"│."+iSco+"│."+iSor+"│"+iTotal+"│."+iLvl+"｜\n.....ホル│."+hWar+"│."+hSco+"│."+hSor+"│"+hTotal+"│."+hLvl+"｜\n..　ゲブ│."+gWar+"│."+gSco+"│."+gSor+"│"+gTotal+"│."+gLvl+"｜\n　.──┼─────┼─────┼─────┼──┼─────┘\n... 　 　 │."+WarTotal+"│."+ScoTotal+"│."+SorTotal+"│"+Total+"│."+LvlTotal;
		document.getElementById('box_2ch').innerHTML = "<form>2ch用に自動整形:<input type=\"button\" onClick=\"javascript:selectText();\" value=\"全選択\"/><br /><textarea rows=\"11\" cols=\"100\" id=\"box_2ch_text\">"+ table2ch +"\n　　　　　　　　　　　　 　 　 　 　 　 　 　 [fezcensus ver:"+censusversion+"(ttp:\/\/awairo.net\/fez\/)]</textarea></form>";

		if (autocopyflg == 'on')
		{
			setClipbordData(autocopyflg);
		}
	}

}

function setClipbordData(text)
{
	var copytext = document.getElementById('box_2ch_text').value;
	clipboardData.setData("Text", copytext);
}
