﻿var BSF;
var ETF;

function init(){
	var $loginName = getCookie('c_name');
	var $memtype  = getCookie('c_mem');
	if($loginName){
		var $namecell = document.getElementById('name_e');
		if($namecell){
			$namecell.value = $loginName;
		}
		var $namecell = document.getElementById('name_b');
		if($namecell){
			$namecell.value = $loginName;
		}
		var $namecell = document.getElementById('mem'+$memtype);
		if($namecell){
			$namecell.checked = true;
		}
	}
	//$namecell.appendChild(document.createTextNode($loginName));
	//$namecell.firstChild.nodeValue = $loginName;
}
function forminit(){
	if(!document.getElementById) return;
	//init();
	
	BSF = document.getElementById('bbsform');
	ETF = document.getElementById('entryform');
	if(BSF){
		BSF.style.top = '0px';
		BSF.style.position = 'absolute';
		var $bl = document.getElementById('bbslegend');
		if($bl) $bl.innerHTML += ' <input type="button" name="b_bbsformopen" value="BBSフォーム" onClick="bbsformopen(event)" />';
		document.getElementById('bbs_close').innerHTML = '<input type="button" name="button" id="button" value="×" class="closebutton" onClick="bbsformclose()" />';
	}
	if(ETF){
		ETF.style.top = '0px';
		ETF.style.position = 'absolute';
		document.getElementById('entrylegend').innerHTML += ' <input type="button" name="b_entryformopen" value="参加表明フォーム" onClick="entryformopen(event)" />';
		document.getElementById('entry_close').innerHTML = '<input type="button" name="button" id="button" value="×" class="closebutton" onClick="entryformclose()" />';
	}
}
function bbsformopen(E){
	entryformclose();
	BSF.style.display = 'block';
	openbox(BSF, E);
	//BSF.style.visibility = 'visible';
}
function entryformopen(E){
	bbsformclose();
	ETF.style.display = 'block';
	openbox(ETF, E);
	//ETF.style.visibility = 'visible';
}
function bbsformclose(){
	if(!BSF) return;
	//BSF.style.visibility = 'hidden';
	BSF.style.display = 'none';
}
function entryformclose(){
	if(!ETF) return;
	ETF.style.display = 'none';
	//ETF.style.visibility = 'hidden';
}

function openbox(BOX, E){
	if(typeof event != 'undefined') E = event;
	var $ow = BOX.offsetWidth;
	var $oh = BOX.offsetHeight;
	
	var $scrollLeft = document.body.scrollLeft || document.documentElement.scrollLeft;
	var $scrollTop  = document.body.scrollTop  || document.documentElement.scrollTop;
	
	var $cx = E.clientX;
	var $px = $scrollLeft + $cx;
	var $cy = E.clientY;
	var $py = $scrollTop + $cy;
	
	if(document.all && document.getElementById && (document.compatMode=='CSS1Compat')){
		var $ww = document.documentElement.clientWidth;
		var $wh = document.documentElement.clientHeight;
	}else if(document.all){
		var $ww = document.body.clientWidth;
		var $wh = document.body.clientHeight;
	}else{
		var $ww = window.innerWidth;
		var $wh = window.innerHeight;
		if(document.body.offsetHeight > window.innerHeight) $ww -= 16;
		if(document.body.offsetWidth > window.innerWidth) $wh -= 16;
	}
	if($cy < $oh + 5){
		var $y = $scrollTop + 5;
	}else{
		var $y = $py - $oh - 8;
	}
	$y = Math.max($y, 82);
	/*
	if($ww - $cx < $ow + 5){
		var $x = $scrollLeft + ($ww - $ow) - 5;
	}else{
		var $x = $cx + 10;
	}
	*/
	BOX.style.top = $y + 'px';
}

function evmod($sid){
	window.location.href='./?ac=mod&md=evmod&sid=' + $sid;
}
function evmodp($sid){
	window.location.href='./evpmod.php?sid=' + $sid;
}
function evmodmap($sid, $d){
	window.location.href='gmapeditor.html?sid=' + $sid + '&d=' + $d;
}
function newevent(){
	window.location.href='./?md=evmod&ac=new';
}
function resetentry($sid){
	if(confirm('BBS、参加表明を全て削除しちゃうよ？')){
		window.location.href='./?md=evmod&ac=resetentry&sid=' + $sid;
	}
}
function evdel($sid, $evname){
	if(confirm('「' + $evname + '」を削除しちゃうよ。ホントにいいの？')){
		window.location.href='./?ac=erase&md=evmod&sid=' + $sid;
	}
}
var RS = new Array();
function orderChange(){
	var $BBS = document.getElementById('bbs2');
	var $ord = document.getElementById('s_order').value;
	setCookie('bbsorder', $ord)
	//alert($BBS.childNodes[1].childNodes[0].tagName);
	var $TRC = $BBS.getElementsByTagName('tbody')[0];
	if(!RS.length){
		while($TRC.hasChildNodes()){
			var $obj = $TRC.lastChild;
			if($obj.tagName == 'TR') RS.push($obj);
			$TRC.removeChild($obj);
		}
	}else{
		while($TRC.hasChildNodes()){
			var $obj = $TRC.lastChild;
			$TRC.removeChild($obj);
		}
	}
	var $ln = RS.length;
	for(var $i=0; $i<$ln; $i++){
		var $n = $ord==1 ? $i : $ln - $i - 1;
		$TRC.appendChild(RS[$n]);
	}
}
function bbsorderInit(){
	var $ord = getCookie('bbsorder');
	if($ord == 1){
		var $obj= document.getElementById('s_order');
		$obj.options[1].selected = true;
		orderChange();
	}
}
