//UTF-8

//Show Block

var oldSelectedRegion = null;

function showDefault(){
	// quit if this function has already been called
	if (arguments.callee.done) return;

	// flag this function so we don't do the same thing twice
	arguments.callee.done = true;
	
	showBlock("theContent");
}

function showBlock(name){

	//ブロックを表示
	var location = new String(document.location);
	if (location){
		if (location.indexOf("post/") > 0){
			//投稿
//			document.getElementById("theRegion").style.display = "none";
			document.getElementById("theContent").style.display = "none";
			document.getElementById("theConfirm").style.display = "none";
//			document.getElementById("theAccount").style.display = "none";
		
			document.getElementById(name).style.display = "block";
		}
		//管理
		var theRegion = document.getElementById("theRegion");
		if (theRegion != undefined){
			var elms = theRegion.getElementsByTagName("A");
			for(var i = 0; i < elms.length; i++){
		//		alert(elms.item(i).firstChild.nodeValue);
				var region = elms.item(i).firstChild.nodeValue;
				if (region == document.form.region.value){
					//選択：背景を塗りつぶす
					elms.item(i).parentNode.className = "redbold";
					oldSelectedRegion = elms.item(i);
				}
			}
		}

	}

}



//バリデーション
function validateContent(){
	
	//イベント内容をチェック
	if (document.form.region != undefined){
		if (document.form.region.value == ""){
			alert ("地域は？");
			return false;
		}
	}
	if (document.form.year != undefined){
		if (document.form.year.value == "" || document.form.month.value == "" 
			 || document.form.day.value == "")
		{
			alert ("日時を正確に入力してね");
			return false;
		}
	}
	if (document.form.title != undefined){
		if (document.form.title.value == ""){
			alert ("イベント名は？");
			return false;
		}
	}
	if (document.form.text != undefined){
		if (document.form.text.value == ""){
			alert ("内容を書いてね。告知文だよ。時間も忘れずに。");
			return false;
		}
	}
	if (document.form.promoter != undefined){
		if (document.form.promoter.value == ""){
			alert ("主催者はだれ？");
			return false;
		}
	}
	if (document.form.email != undefined){
		if (document.form.email.value == ""){
			alert ("あなたのメールアドレスは？");
			return false;
		}
		else if (!document.form.email.value.match(/[a-zA-Z0-9_-]+\@[a-zA-Z0-9_-]+\.\w+/))
		{
			alert ("メールアドレスの書き方が間違っています。");
			return false;
		}
	}
	if (document.form.password != undefined){
		if (document.form.password.value == ""){
			alert ("編集・削除用のパスコードが必要です。");
			return false;
		}
	}
	if (document.form.place != undefined){
		if (document.form.place.value == ""){
			alert ("くわしい場所を教えて");
			return false;
		}
	}
	//条件をチェック
	if (document.form.charge != undefined){
		if (document.form.charge.value == ""){
			alert ("参加料はいるの？");
			return false;
		}
	}
	if (document.form.charge != undefined){
		if (document.form.charge.value == ""){
			alert ("参加料はいるの？");
			return false;
		}
	}
	if (document.form.abstruct != undefined){
		if (document.form.abstruct.value == ""){
			alert ("３行キャッチフレーズを入力してください。");
			return false;
		}
	}
	return true;
}

function setConfirmValue(){
	//確認値をセット

//	if (document.form.elements["private"] != undefined){
//		var str = document.form.elements["private"].value ? "非公開" : "公開";
//		setNodeText("confirmPrivate",str);
//	}
	if (document.form.title != undefined){
		setNodeText("confirmTitle",document.form.title.value);
	}
	if (document.form.place != undefined){
		setNodeText("confirmPlace",document.form.place.value);
	}
	if (document.form.text != undefined){
		setNodeText("confirmText",document.form.text.value);
	}
	if (document.form.year != undefined){
		setNodeText("confirmYear",document.form.year.value);
	}
	if (document.form.month != undefined){
		setNodeText("confirmMonth",document.form.month.value);
	}
	if (document.form.day != undefined){
		setNodeText("confirmDay",document.form.day.value);
	}
	if (document.form.year2 != undefined){
		setNodeText("confirmYear2",document.form.year2.value);
	}
	if (document.form.month2 != undefined){
		setNodeText("confirmMonth2",document.form.month2.value);
	}
	if (document.form.day2 != undefined){
		setNodeText("confirmDay2",document.form.day2.value);
	}
	if (document.form.name != undefined){
		setNodeText("confirmName",document.form.name.value);
	}
	if (document.form.email != undefined){
		setNodeText("confirmEmail",document.form.email.value);
	}
	if (document.form.promoter != undefined){
		setNodeText("confirmPromoter",document.form.promoter.value);
	}
	if (document.form.buzz != undefined){
		var display = document.form.buzz.checked ? "block":"none";
		document.getElementById("theAgree").style.display = display;
/*		if (document.form.buzz.checked){
			alert("ViewBuzz");
			document.getElementById("theAgree").style.display = "block";
		}else{
			document.getElementById("theAgree").style.display = "none";
		}
*/
	}
}


function clickContinue(){
	//バリデーション
	if (!validateContent()){
		return;
	}
	//デフォルト値の入力
	if (!document.form.year2.value){
		document.form.year2.value = document.form.year.value;
	}
	if (!document.form.month2.value){
		document.form.month2.value = document.form.month.value;
	}
	if (!document.form.day2.value){
		document.form.day2.value = document.form.day.value;
	}

	//確認用の値を代入
	setConfirmValue();

	//戻るボタンを有効にする
	window.navifrm.location.href="navifrm?bk=theConfirm";

	showBlock("theConfirm");

}


function clickSubmit(){
	//バリデーション
	if (!validateContent()){
		return;
	}

	if (confirm("この内容で投稿しますか？")){
		document.form.submit();
	}
}



function clickUpdate(){
	//バリデーション
	if (!validateContent()){
		return;	
	}

	if (confirm("この内容で更新しますか？")){
		document.form.submit();
	}

}

function clickRegion(e){
	//地域を選択
	if (!e) var e = window.event;
	
	//エレメントを特定
	var theRegion = e.srcElement?e.srcElement:e.target;	
	
	// IfでアクセスするだけでIEは落ちる
//	if (e.srcElement)
//		theRegion = e.srcElement;
//	else if (e.target)
//		theRegion = e.target;
	if (theRegion.nodeType == 3 && theRegion.tagName != "A") // defeat Safari bug
		theRegion = theRegion.parentNode;

	//前の選択を解除
	if (oldSelectedRegion){
		oldSelectedRegion.parentNode.className = "";
	}

	//選択：背景を塗りつぶす
	theRegion.parentNode.className = "redbold";
	//地名取得
	var region = theRegion.firstChild.nodeValue;
	oldSelectedRegion = theRegion;
	//地名セット
	document.form.region.value = region;

}


function deleteEvent(entryid){
	//イベント削除
	if (confirm("このイベントを削除してもよろしいですか？\nイベント紹介ページや、参加者リストも削除され、\n以後、アクセスできなくなります。\nこの処理を行う前に、参加者に連絡しておくことをおすすめします。")){
		document.formDelete.submit();
	}

}


function addTag(tag)
{
	//タグをクリックして、追加
	if (document.getElementById("tags").value != ""){
		document.getElementById("tags").value += " ";	
	}
	document.getElementById("tags").value += tag;

}


//エレメントのテキスト操作
function getNodeText(id)
{
	// Attempt to retrieve it from the store
	var element = document.getElementById(id);
	if(element != null && element.firstChild != null)
		return(element.firstChild.nodeValue);
	else
		return(null);
}
function setNodeText(id,text)
{
	// Convert \n to <br> tag
//	text = text.replace(/\x0D\x0A|\x0D|\x0A/g,"<br>");

	// Attempt to retrieve it from the store
	var element = document.getElementById(id);
	if(element != null){
		if (element.firstChild != null){
			element.removeChild(element.firstChild);
		}
		element.appendChild(document.createTextNode(text));
	}
}
//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}

}



addLoadEvent(showDefault);	// run initLightbox onLoad
//showDefault();



