/* ************************************************************************ */
/* 																	[TAB:4]	*/
/* 	画像選択 JavaScript														*/
/* 																			*/
/* ************************************************************************ */
var ScrWidth		= screen.availWidth;
var ScrHeight		= screen.availheight;
var ImgWinWidth		= "700";
var ImgWinHeight	= "500";
var ImgWinLeft		= (ScrWidth -ImgWinWidth )/2;
var ImgWinTop		= (ScrHeight-ImgWinHeight)/2;
var ImgWin;

/* ************************************************************************ */
/* 	画像選択Window表示														*/
/* ************************************************************************ */
function ImageSelectWindow( cgipath , tg1, tg2, dmy )
{
	var url = cgipath 	+ '&img='   + tg1.value
						+ '&tg1='	+ tg1.id 
						+ '&tg2='	+ tg2.id 
						+ '&dmy='	+ dmy 
						+ '';
	
	if ( !ImgWin || ImgWin.closed ) 
	{ 
		ImgWin = window.open( url, '', 
			'width='	+	ImgWinWidth	+ ',' +
			'height='	+	ImgWinHeight+ ',' +
			'left='		+	ImgWinLeft	+ ',' +
			'top='		+	ImgWinTop	+ ',' +
			'scrollbars = yes'
			); 
	}
	else
	{
		ImgWin.location.href= url;
	}
}

/* ************************************************************************ */
/* 	画像の決定																*/
/* ************************************************************************ */
function ImageSelectPaste( objFrm, filename, fullpath, tg1, tg2, img_id_width, img_id_height ) 
{
	var i;
	var str;
	var set_width
	var set_height;
	
	try
	{
		if (filename.length >= 0)
		{
			for( i = 0; i <= filename.length; i++ )
			{
				if (filename[i].checked)
				{
					str = filename[i].value;
					break;
				}
			}
		}
		else
		{
			if (filename.checked)
			{
				str = filename.value;
			}
		}
	}
	catch(e)
	{
		str = '';
	}
	
	if (! str)
	{
		window.alert('画像を選択してください。');
		return false;
	}
	
	// 画像サイズの読み込み
	set_width  = document.getElementById( img_id_width  ).value;
	set_height = document.getElementById( img_id_height ).value;
	
	// 画像のセット
	opener.document.getElementById(tg1).value = str;
	opener.document.getElementById(tg1 + '_width'  ).value = set_width;
	opener.document.getElementById(tg1 + '_height' ).value = set_height;
	opener.document.getElementById(tg2).src = fullpath + str;
	opener.document.getElementById(tg2).width  = set_width;
	opener.document.getElementById(tg2).height = set_height;
	window.close();
}
/* ************************************************************************ */
/*	選択画像のキャンセル（初期化）											*/
/* ************************************************************************ */
function ImageSelectInit( objFrm, dummy, fullpath, tg1, tg2, max_width, max_height ) 
{
	opener.document.getElementById(tg1).value = '';
	opener.document.getElementById(tg2).src = fullpath + dummy;
	opener.document.getElementById(tg2).width  = max_width;
	opener.document.getElementById(tg2).height = max_height;
	window.close();
}

/* ************************************************************************ */
/*	選択画像のキャンセル（クリア）											*/
/* ************************************************************************ */
function ImageSelectClear( objFrm, dummy, fullpath, tg1, tg2, max_width, max_height ) 
{
	opener.document.getElementById(tg1).value = '';
	opener.document.getElementById(tg2).src = opener.document.getElementById(dummy).src;
	opener.document.getElementById(tg2).width  = max_width;
	opener.document.getElementById(tg2).height = max_height;
	window.close();
}
/* ************************************************************************ */
/*	画像サイズの設定														*/
/* ************************************************************************ */
function ImageSelectSetSize( id, sel_id, max_width, max_height, mode ) 
{

	// sel_idが設定されていない場合は自動で取得
	if ( sel_id == '' )
	{
		var filename = document.getElementsByName('filename');
		try
		{
			if (filename.length >= 0)
			{
				for( i = 0; i <= filename.length; i++ )
				{
					if (filename[i].checked)
					{
						sel_id = filename[i].id;
						break;
					}
				}
			}
			else
			{
				if (filename.checked)
				{
					sel_id = filename.id;
				}
			}
		}
		catch(e)
		{
			sel_id = 'filename_1';
		}
		
	}
	
	// 各idからサイズ保存用の場所を判断する
	if (! document.getElementById( id + '_width'     ) ) { return; }
	if (! document.getElementById( id + '_height'    ) ) { return; }
	if (! document.getElementById( sel_id + '_width' ) ) { return; }
	if (! document.getElementById( sel_id + '_height') ) { return; }
	
	var set_width;
	var set_height;
	
	// 画像のサイズを設定（推奨サイズより大きい場合は自動で縮小）
	img_width  = document.getElementById( sel_id + '_width' ).value
	img_height = document.getElementById( sel_id + '_height').value

	// 初期値
	set_width  = img_width;
	set_height = img_height;
	
	if ( mode == '' )		// プリセット画像は自動でサイズ変更しない
	{
		diff_width  = img_width  / max_width;
		diff_height = img_height / max_height;

		
		if ( diff_width >= diff_height )
		{
			if ( diff_width > 0 )
			{
				set_width  = max_width;
				set_height = img_height / diff_width;
			}
		}
		else if ( diff_width < diff_height )
		{
			if ( diff_height > 0 )
			{
				set_width  = img_width / diff_height;
				set_height = max_height;
			}
		}
	}
	document.getElementById( id + '_width' ).value = set_width;
	document.getElementById( id + '_height').value = set_height;
}
/* ************************************************************************ */
/* [EOF]																	*/
/* ************************************************************************ */
