// 日記作成・編集ページから日記表示ページに遷移を行う汎用関数
function getDiaryCommentPage(opt)
{
	var query = "opt=" + opt + "&diaryTitle=" + encodeURIComponent(document.getElementById('diaryTitle').value) +
	"&diaryText=" + encodeURIComponent(document.getElementById('diaryText').value) +
	"&authorId=" + document.getElementById('authorId').value +
	"&diaryDate=" + document.getElementById('diaryDate').value +
	"&diaryType=" + document.getElementById('diaryType').value +
	"&registVoiceId=" + document.getElementById('registVoiceId').value +
	"&voiceType=" + document.getElementById('voiceType').value +
	"&voiceURL=" + encodeURIComponent(document.getElementById('voiceURL').value);
	top.document.data.query.value=query;
	top.document.data.method.value='POST';
	top.document.data.rsync.value=false;
	top.document.data.url.value='/tinierme/diarycomment.do'

	top.main.document.location.href="/tinierme/html/dummy.html";
}

// 日記表示ページ遷移を行う汎用関数
function getDiaryCommentPage2(id,date,pageNum)
{
	var form = document.createElement('form');
	form.method = 'POST';
	form.action = "/tinierme/diarydisp.do";

	var authorId = document.createElement('input');
	authorId.name = 'authorId';
	authorId.type = 'hidden';
	authorId.value = id;
	form.appendChild(authorId);

	var diaryDate = document.createElement('input');
	diaryDate.name = 'diaryDate';
	diaryDate.type = 'hidden';
	diaryDate.value = date;
	form.appendChild(diaryDate);

	var pageNumber = document.createElement('input');
	pageNumber.name = 'pageNumber';
	pageNumber.type = 'hidden';
	pageNumber.value = pageNum;
	form.appendChild(pageNumber);

	document.getElementsByTagName('body')[0].appendChild(form);
    form.submit();
}
// 日記表示ページ遷移を行う汎用関数
function diaryDirection(id,date,diaryDirection)
{
	var form = document.createElement('form');
	form.method = 'POST';
	form.action = "/tinierme/diarydisp.do";

	var authorId = document.createElement('input');
	authorId.name = 'authorId';
	authorId.type = 'hidden';
	authorId.value = id;
	form.appendChild(authorId);

	var diaryDate = document.createElement('input');
	diaryDate.name = 'diaryDate';
	diaryDate.type = 'hidden';
	diaryDate.value = date;
	form.appendChild(diaryDate);

	var dispDir = document.createElement('input');
	dispDir.name = 'dispDirection';
	dispDir.type = 'hidden';
	dispDir.value = diaryDirection;
	form.appendChild(dispDir);

	document.getElementsByTagName('body')[0].appendChild(form);
    form.submit();
}
//日記表示ページ遷移を行う汎用関数（インターネット公開用）
function publicDiaryDirection(keyParam,diaryDirection)
{
	var form = document.createElement('form');
	form.method = 'POST';
	form.action = "/tinierme/publicdiarydisp.do";

	var key = document.createElement('input');
	key.name = 'key';
	key.type = 'hidden';
	key.value = keyParam;
	form.appendChild(key);

	var dispDir = document.createElement('input');
	dispDir.name = 'dispDirection';
	dispDir.type = 'hidden';
	dispDir.value = diaryDirection;
	form.appendChild(dispDir);

	document.getElementsByTagName('body')[0].appendChild(form);
    form.submit();
}

function gosubmit(aByte, aMsg)
{
	var strHtml = editor.getHTML();
	var re = /bgcolor\s*=\s*\"([0-9]+)\"/;
	var strRep = strHtml.match(re);
	while (strRep) {
		var colorrgb = parseInt(RegExp.$1);
		strHtml = strHtml.replace(strRep[0],"bgcolor=\""+ HTMLArea._colorToRgb(colorrgb) +"\"");
		strRep = strHtml.match(re);
	}
	document.MainForm.diaryText.value = strHtml;

	if(!checkInputTag2('diaryText', aByte, aMsg)) {
		return;
	}

	if ( checkDiary() == false ) return;
	if ( checkMagicalIMGTag() == false ) return;

	// AjaX読み込み
	document.MainForm.submit();
}


// 以前のgosubmit関数（minipageで使用）
function gosubmit2(aByte, aMsg){
	if(!checkInputTag2('diaryText', aByte, aMsg)) {
		return;
	}
	if ( checkDiary() == false ) return;
	if ( checkMagicalIMGTag() == false ) return;
	document.MainForm.submit();
}
// 以前のgoback関数（minipageで使用）
function goback2( aOpt )
{
	if ( continueWithoutSavingDiary() == false ) return;

	if ( aOpt == 2 ){
		//更新
		document.MainForm.opt.value = 0 ;
		document.MainForm.submit();
	}else{
		//新規作成(マイページ遷移)
		document.form1.submit();
	}
}

function goback( aOpt )
{
	//if ( continueWithoutSavingDiary() == false ) return;

	if ( aOpt == 2 ){
		//更新
		// AjaX読み込み
		getDiaryCommentPage(0);
	}else{
		//新規作成(マイページ遷移)
		document.form1.submit();
	}
}
function checkDiary()
{
	//禁止文字列のチェックの為に同期化
	DWREngine.setTimeout(1000);		// DWR実行タイムアウト
	DWREngine.setAsync(false);		// 同期的に実行(false で同期)

	var oDiaryTitle = document.MainForm.diaryTitle;
	var oDiaryText = document.MainForm.diaryText;
	oDiaryTitle.value = trim(oDiaryTitle.value);
	oDiaryText.value = trim(oDiaryText.value);

	if ( trim(oDiaryTitle.value) == "" )
	{
		alert( msg_notitle );
		oDiaryTitle.focus();
		return false;
	}
	else if ( trim(oDiaryText.value) == "" )
	{
		alert( msg_notext );
		try{
			oDiaryText.focus();
		}catch(e){}
		return false;
	}
	else if ( CheckKeepOutString("diaryTitle") == false )
	{
		//alert("タイトルに使用禁止文字列が含まれています");
		alert("The title contains a forbidden word.");
		return false;
	}
	else if ( CheckKeepOutString("diaryText") == false )
	{
	    //alert("Messageに使用禁止文字列が含まれています");
		alert("The message contains a forbidden word.");
		return false;
	}

	return true;
}

function continueWithoutSavingDiary ()
{
	if ( diarytitle != document.MainForm.diaryTitle.value ||
			diarytext != document.MainForm.diaryText.value )
	{
		return ( confirm( msg_confirm_abort ) );
	}
	return true;
}

function loadDiaryDefaultValue()
{
	diarytitle = document.MainForm.diaryTitle.value;
	diarytext = document.MainForm.diaryText.value;
}

/**
 * マジカルメーカ画像の件数チェックを行う
 *
 */
function checkMagicalIMGTag() {
	var checktext = document.MainForm.diaryText.value;

	ret = checktext.match(/<\w{0,}[^<>]{0,}>/g);
	var imgcount = 0;

	if(ret == null) {
		return true;
	}

	for(i=0; i<ret.length; i++) {
		if( ret[i].match(/<param/g) ) {

			if(ret[i].match(/<param name="movie" value="http:\/\/www\.magicalmaker\.com\/.{0,}" \/>/g)) {
//			if(ret[i].match(/<param name="movie" value="http:\/\/www\.nikolaschka\.jp\/.{0,}" \/>/g)) {
					imgcount = imgcount + 1;
			}
		}
	}

	if(imgcount > 3) {
		//alert("日記1件につき画像は3までです。");
		alert("The maximum number of images in a diary entry is 3.");
		return false;
	}

	return true;
}


/**
 * マジカルメーカーから起動される関数
 *
 */
function setMMTag(id, userid, url, jpg, width, height) {
	tag = createScriptTag(id, userid, url, jpg, width, height);
	setMagicalTag(tag);
}


/**
 * マジカルメーカーから渡された情報を元に
 * オブジェクトタグを生成する。
 */
function createScriptTag(id, userid, url, jpg, width, height) {
	var swf = 'http://www.magicalmaker.com' + url;
//	var swf = 'http://www.nikolaschka.jp' + url;
	var str = "";
	str += '<object width="' + width + '" height="' + height + '" id="' + id + '" align="middle" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0">';
	str += '<param name="movie"';
	str += ' value="' + swf + '" />';
	str += '</object>';

	return str;
}


/**
 * 生成したScriptタグをテキストエリアに設定する
 *
 */
function setMagicalTag(tag) {
	if(minipage_flg) {
		var diarydocument = document.MainForm.diaryText.value;
		document.MainForm.diaryText.value = diarydocument + tag;
	} else {
		if(edit_flg) {
			editor.focusEditor();
			editor.insertHTML(tag);
			var diarydocument = document.MainForm.diaryText.value;
			document.MainForm.diaryText.value = diarydocument + tag;
		} else {
			//alert('[タグの非表示]モードを選択してください。');
			alert('Please select the \"Don\'t show tags\" mode.');
		}
	}
}

