/*																	**
**		common.js													**
**																	**
**		Version 1.01  -  2002/01/28									**
**																	**
**		Copyright (c) Absurd Technology Lab 2002					**
**																	**
**						http://www5b.biglobe.ne.jp/~hokko2nd/		**
**																	*/

var IncludeBorder = false;
var DPI;
var PixelOnly = true;
var Support_instanceof = false;

{
	var AppVer = navigator.appVersion.charAt(0);
	var AppName= navigator.appName;

	if(navigator.userAgent.indexOf('Opera') != -1) // Opera
	{
		if(AppVer >= 6)
		{
			Support_instanceof = true;
			PixelOnly = false;
		}
	}
	else if(AppName.indexOf('Microsoft') != -1) // IE
	{
		var Pos;

		Pos = navigator.appVersion.indexOf('MSIE ') +5;
		if( navigator.appVersion.charAt(Pos+1) == '.' )
			AppVer = navigator.appVersion.substr( Pos, 3 );
		else
			AppVer = navigator.appVersion.charAt(Pos);

		IncludeBorder = true;
		if(AppVer >= 3)
			PixelOnly = false;
		if(AppVer >= 5){
			Support_instanceof = true;}
	}
	else if(AppName.indexOf('Netscape') != -1) // Gecko
	{
		if(AppVer >= 5)
		{
			PixelOnly = false;
			Support_instanceof = true;
		}
	}


	if(navigator.platform.match(/Win/i))
	{
		DPI = 96;
	//	DPI = 120;
	}
	else
	{
		DPI = 72;
	}

}

function changeDPIDialog()
{
	var	inputValue = "";

	while(true)
	{
		inputValue = prompt(String.fromCharCode(0x753B,0x9762,0x89E3,0x50CF,0x5EA6,0x3092,0x5165,0x529B,
												0x3057,0x3066,0x304F,0x3060,0x3055,0x3044), inputValue);
		if(inputValue == null)
			return;
		if(!isNaN(inputValue))
			break;
		alert(String.fromCharCode(0x6570,0x5024,0x3092,0x5165,0x529B,0x3057,0x3066,0x304F,0x3060,0x3055,0x3044));
	}

	DPI = inputValue-0;
}

function _getElementById(Window, id)
{
	var elem = null;
	if(document.getElementById)
		elem = Window.document.getElementById(id)
	else if(document.all)
		elem = Window.document.all(id);
	else if(document.layers)
		elem = Window.document.layers[id];

	if(elem)
		return elem;
	return null;
}

function _getElementsByTagName(Window, tagName)
{
	var elems = null;

	if(document.getElementsByTagName)
		elems = Window.document.getElementsByTagName(tagName);
	else if(document.all && document.all.tags)
		elems = Window.document.all.tags(tagName);
	else if(document.layers && document.layers.tags)
		elems = Window.document.tags[tagName];

	if(!elems || !elems.length)
		return null;
	return elems;
}

function _getElementsByClassName(Window, tagName, className)
{
	var tagArray;
	var returnArray = new Array();

	if(document.all && tagName == "*")
		tagArray = Window.document.all;
	else
		tagArray = _getElementsByTagName(Window, tagName);

	if(!tagArray)
		return null;

	for(var i = 0; i < tagArray.length; i++)
	{
		if(tagArray[i].className == className)
			returnArray[returnArray.length] = tagArray[i];
	}
	return returnArray;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function _getDocumentWidth(Window)
{
	if(!Window)
		Window = window;

	if(document.body)
	{
		if(document.body.scrollWidth || document.body.scrollWidth == 0)
			return document.body.scrollWidth;

		if(document.documentElement)
		{
			return document.documentElement.offsetWidth;
		}
		return document.body.offsetWidth;
	}

	if(document.width || document.width == 0)
		return document.width;

	return 0;
}

function _getDocumentHeight(Window)
{
	if(!Window)
		Window = window;

	if(document.body)
	{
		if(document.body.scrollHeight || document.body.scrollHeight == 0)
			return document.body.scrollHeight;

		if(document.documentElement)
		{
			return document.documentElement.offsetHeight;
		}
		return document.body.offsetHeight;
	}

	if(document.height || document.height == 0)
		return document.height;

	return 0;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function _getClientWidth(Window)
{
	if(!Window)
		Window = window;
	if(Window.innerWidth || Window.innerWidth == 0)
		return Window.innerWidth;
	else if(Window.document.body && (Window.document.body.clientWidth || Window.document.body.clientWidth == 0))
			return Window.document.body.clientWidth;
	return 0;
}

function _getClientHeight(Window)
{
	if(!Window)
		Window = window;
	if(Window.innerHeight || Window.innerHeight == 0)
		return Window.innerHeight;
	else if(Window.document.body && (Window.document.body.clientHeight || Window.document.body.clientHeight == 0))
			return Window.document.body.clientHeight;
	return 0;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function _getScrollPosX(Window)
{
	if(!Window)
		Window = window;
	if(Window.document.body && (Window.document.body.scrollLeft || Window.document.body.scrollLeft == 0))
			return Window.document.body.scrollLeft;
	else if(Window.pageXOffset || Window.pageXOffset == 0)
		return Window.pageXOffset;
	return 0;
}

function _getScrollPosY(Window)
{
	if(!Window)
		Window = window;
	if(Window.document.body && (Window.document.body.scrollTop || Window.document.body.scrollTop == 0))
		return Window.document.body.scrollTop;
	else if(Window.pageYOffset || Window.pageYOffset == 0)
		return Window.pageYOffset;
	return 0;
}

function _setScrollPosX(Window, newX)
{
	if(isNaN(newX))
	{
		newX = _getPixel(newX);
	}
	if(Window.document.body && (Window.document.body.scrollLeft || Window.document.body.scrollLeft == 0))
		Window.document.body.scrollLeft = newX;
	else if(Window.pageYOffset || Window.pageYOffset == 0)
		Window.scrollTo(newX, Window.pageYOffset);
	else
		return false;

	return true;
}

function _setScrollPosY(Window, newY)
{
	if(isNaN(newY))
	{
		newY = _getPixel(newY);
	}
	if(Window.document.body && (Window.document.body.scrollTop || Window.document.body.scrollTop == 0))
		Window.document.body.scrollTop = newY;
	else if(Window.pageXOffset || Window.pageXOffset == 0)
		Window.scrollTo(Window.pageXOffset, newY);
	else
		return false;

	return true;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function _getElementPos(element)
{
	if(element.style)
	{
		if(IncludeBorder)
			return new POSITION(_getPixel(element.style.left), _getPixel(element.style.top),
							 _getPixel(element.style.width),
							 _getPixel(element.style.height),
							 element.style.zIndex);
		else
			return new POSITION(_getPixel(element.style.left), _getPixel(element.style.top),
							 _getPixel(element.style.width) + _getPixel(element.style.borderLeftWidth) + _getPixel(element.style.borderRightWidth),
							 _getPixel(element.style.height) + _getPixel(element.style.borderTopWidth) + _getPixel(element.style.borderBottomWidth),
							 element.style.zIndex);
	}
	else
		return new POSITION(element.left, element.top, element.width, element.height, element.zIndex);
}

function _setElementPos(element, left, top, width, height, zIndex)
{
	if(Support_instanceof)
	{
		if( eval("left instanceof POSITION") )
		{
			top = left.top
			width = left.width;
			height = left.height;
			zIndex = left.zIndex;
			left = left.left;
		}
	}
	else
	{
		if( typeof(left) == 'object' )
		{
			top = left.top
			width = left.width;
			height = left.height;
			zIndex = left.zIndex;
			left = left.left;
		}
	}

	if(PixelOnly)
	{
		left = _getPixel(left);
		top = _getPixel(top);
		width = _getPixel(width);
		height = _getPixel(height);
	}

	if(element.style)
	{
		element.style.left	= left;
		element.style.top	= top;
		element.style.zIndex= zIndex;

		if(IncludeBorder)
		{
			element.style.width	= width;
			element.style.height= height;
		}
		else
		{
			element.style.width	= _getPixel(width) - _getPixel(element.style.borderLeftWidth) - _getPixel(element.style.borderRightWidth);
			element.style.height= _getPixel(height) - _getPixel(element.style.borderTopWidth) - _getPixel(element.style.borderBottomWidth);
		}
	}
	else
	{
		element.left	= left;
		element.top		= top;
		element.width	= width;
		element.height	= height;
		element.zIndex	= zIndex;
	}
}

function POSITION(left, top, width, height, zIndex)
{
	this.left = left;
	this.top = top;
	this.width = width;
	this.height = height;
	this.zIndex = zIndex;
}

POSITION.prototype.toSource = function ()
{
	return("left : " +this.left+ " top : " +this.top+ " width : " +this.width+ " height : " +this.height+ " zIndex : " +this.zIndex);
}

function _showElement(element, flag)
{
	if(element.style)
	{
		if(flag)
			element.style.visibility = 'visible';
		else
			element.style.visibility = 'hidden';
	}
	else
	{
		if(flag)
			element.visibility = 'show';
		else
			element.visibility = 'hide';
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function _getPixel(length)
{
	var minus = 1;

	if(!length)
		return 0;
	if(!length.match)
		return length;
	if(length.match(/-\d+/))
		minus = -1;

	if(length.match(/\d+\D/))
	{
		if(length.match(/(\d+\.*\d*)px/i))
			return ((RegExp.$1)-0) * minus;
		else if(length.match(/(\d+\.*\d*)pt/i))
			return ((RegExp.$1 *DPI) /72) * minus;
		else if(length.match(/(\d+\.*\d*)pc/i))
			return ((RegExp.$1 *DPI) /6) * minus;
		else if(length.match(/(\d+\.*\d*)in/i))
			return (RegExp.$1 *DPI) * minus;
		else if(length.match(/(\d+\.*\d*)cm/i))
			return ((RegExp.$1 *DPI) /2.54) * minus;
		else if(length.match(/(\d+\.*\d*)mm/i))
			return ((RegExp.$1 *DPI) /25.4) * minus;
		else 
			return 0;
	}
	else
		return(length -0);
}

function _getPixelEx(string)
{
	while(string.match(/(\d+\.*\d*px)/i))
		string = string.replace(RegExp.$1, _getPixel(RegExp.$1));

	while(string.match(/(\d+\.*\d*pt)/i))
		string = string.replace(RegExp.$1, _getPixel(RegExp.$1));

	while(string.match(/(\d+\.*\d*pc)/i))
		string = string.replace(RegExp.$1, _getPixel(RegExp.$1));

	while(string.match(/(\d+\.*\d*in)/i))
		string = string.replace(RegExp.$1, _getPixel(RegExp.$1));

	while(string.match(/(\d+\.*\d*cm)/i))
		string = string.replace(RegExp.$1, _getPixel(RegExp.$1));

	while(string.match(/(\d+\.*\d*mm)/i))
		string = string.replace(RegExp.$1, _getPixel(RegExp.$1));

	return eval(string);
}


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


var FloatElements = new Array();
function RegisterFloatElement(Window, id, left, top)
{
	FloatElements[FloatElements.length] = new FLOATELEMENT(Window, _getElementById(Window, id), left, top);
	AdjustFloatElementPos();
}

function AdjustFloatElementPos()
{
	var Left;
	var Top;

	for(var i in FloatElements)
	{
		var PosInfo = _getElementPos(FloatElements[i].element);

		if(isNaN(FloatElements[i].left))
		{
			Left = FloatElements[i].left.replace("left", "0");
			Left = Left.replace("center", "(" + _getPixel(_getDocumentWidth(FloatElements[i].window))+"px-" + PosInfo.width + ")/2");
			Left = Left.replace("right", _getPixel(_getDocumentWidth(FloatElements[i].window))+"px-" + PosInfo.width);
			Left = Left.replace("viewLeft", _getScrollPosX(FloatElements[i].window)+"px" );
			Left = Left.replace("viewCenter", _getScrollPosX(FloatElements[i].window)+"px + (" + _getClientWidth(FloatElements[i].window)+"px -" + PosInfo.width + ") /2");
			Left = Left.replace("viewRight", _getScrollPosX(FloatElements[i].window)+"px +" + _getClientWidth(FloatElements[i].window)+"px -" + PosInfo.width);
			Left = _getPixelEx(Left);
		}
		else
			Left = FloatElements[i].left;

		if(isNaN(FloatElements[i].top))
		{
			Top = FloatElements[i].top.replace("top", "0");
			Top = Top.replace("center", "(" + _getPixel(_getDocumentHeight(FloatElements[i].window))+"px-" + PosInfo.height + ")/2");
			Top = Top.replace("bottom", _getPixel(_getDocumentHeight(FloatElements[i].window))+"px-" + PosInfo.height);
			Top = Top.replace("viewTop", _getScrollPosY(FloatElements[i].window)+"px" );
			Top = Top.replace("viewCenter", _getScrollPosY(FloatElements[i].window)+"px + (" + _getClientHeight(FloatElements[i].window)+"px -" + PosInfo.height + ") /2");
			Top = Top.replace("viewBottom", _getScrollPosY(FloatElements[i].window)+"px +" + _getClientHeight(FloatElements[i].window)+"px -" + PosInfo.height);
			Top = _getPixelEx(Top);
		}
		else
			Top = FloatElements[i].top;

		_setElementPos(FloatElements[i].element, Left+'px', Top+'px', PosInfo.width, PosInfo.height, PosInfo.zIndex);
	}
}

function FLOATELEMENT(Window, element, left, top)
{
	this.window = Window;
	this.element= element;
	this.left	= left;
	this.top	= top;
}
FLOATELEMENT.prototype.toSource = function ()
{
	return("Element : " +this.Element+ " left : " +this.left+ " top : " +this.top);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function _isOnElement(element, posX, posY, marginX, marginY)
{
	var PosInfo = _getElementPos(element);

	if(isNaN(posX))
		posX = _getPixel(posX);
	if(isNaN(posY))
		posY = _getPixel(posY);
	if(isNaN(marginX))
		marginX = _getPixel(marginX);
	if(isNaN(marginY))
		marginY = _getPixel(marginY);

	if(PosInfo.left-marginX <= posX && posX <= PosInfo.left+PosInfo.width+marginX
	 && PosInfo.top-marginY <= posY && posY <= PosInfo.top+PosInfo.height+marginY)
		return true;

	return false;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function sprintf()
{
	var returnString = '';
	var nPos = 0;
	var nPosPrevEnd = 0;
	var arguNum = 1;

	while(nPos = sprintf.arguments[0].indexOf('%', nPosPrevEnd) +1)
	{
		var alignL = false;
		var addHexOct = false;
		var spaceChar = ' ';
		var widthMax = 0;
		var widthMin = 0;
		var wide = false;
		var replaceString ='';

		if(nPos != nPosPrevEnd)
			returnString = returnString + sprintf.arguments[0].substring(nPosPrevEnd, nPos-1);

		if(sprintf.arguments[0].charAt(nPos) == '%')
		{
			returnString = returnString + '%';
			nPosPrevEnd = nPos+1;
			continue;
		}

		if(sprintf.arguments[0].charAt(nPos) == '-')
		{
			alignL = true;
			nPos++;
		}
		if(sprintf.arguments[0].charAt(nPos) == '#')
		{
			addHexOct = true;
			nPos++;
		}
		if(sprintf.arguments[0].charAt(nPos) == 'W')
		{
			wide = true;
			spaceChar = String.fromCharCode(0x3000);
			nPos++;
		}
		if(sprintf.arguments[0].charAt(nPos) == '0')
		{
			if(!alignL)
			{
				if(wide)
					spaceChar = String.fromCharCode(0xFF10);
				else
					spaceChar = '0';
			}
			nPos++;
		}
		while( '0' <= sprintf.arguments[0].charAt(nPos) && sprintf.arguments[0].charAt(nPos) <= '9')
		{
			widthMin = widthMin * 10 + sprintf.arguments[0].charAt(nPos);
			nPos++;
		}
		if(sprintf.arguments[0].charAt(nPos) == '.')
		{
			nPos++;
			while( '0' <= sprintf.arguments[0].charAt(nPos) && sprintf.arguments[0].charAt(nPos) <= '9')
			{
				widthMax = widthMax * 10 + sprintf.arguments[0].charAt(nPos);
				nPos++;
			}
		}

		if(arguNum >= sprintf.arguments.length)
		{
			nPosPrevEnd = nPos +1;
			arguNum++;
			continue;
		}

		switch(sprintf.arguments[0].charAt(nPos))
		{
		case 'c':
			if(isNaN(sprintf.arguments[arguNum]))
				break;
			if(sprintf.arguments[arguNum] < 0)
				break;
			replaceString = String.fromCharCode(sprintf.arguments[arguNum]);
			var addNum = widthMin - replaceString.length;
			if(addNum > 0)
			{
				for(;addNum;addNum--)
				{
					if(alignL)
						replaceString = replaceString + spaceChar;
					else
						replaceString = spaceChar + replaceString;
				}
			}
			break;

		case 'd':
			if(isNaN(sprintf.arguments[arguNum]))
				break;
			var minus = false;
			if(sprintf.arguments[arguNum] < 0)
			{
				minus = true;
				sprintf.arguments[arguNum] = -sprintf.arguments[arguNum];
			}
			if(wide)
			{
				var str = String(sprintf.arguments[arguNum]);
				for(var i=0; i < str.length; i++)
					replaceString = replaceString + String.fromCharCode(0xFF10+Number(str.charAt(i)));
			}
			else
			{
				replaceString = String(sprintf.arguments[arguNum]);
			}
			var addNum = widthMin - replaceString.length;
			if(addNum > 0)
			{
				for(;addNum;addNum--)
				{
					if(alignL)
						replaceString = replaceString + spaceChar;
					else
						replaceString = spaceChar + replaceString;
				}
			}
			if(minus)
			{
				if(wide)
					replaceString = String.fromCharCode(0xFF0D) + replaceString;
				else
					replaceString = '-' + replaceString;
			}
			break;

		case 'o':
			if(isNaN(sprintf.arguments[arguNum]))
				break;
			var Num = parseInt(sprintf.arguments[arguNum]);
			var minus = false;
			if(Num < 0)
			{
				minus = true;
				Num = -Num;
			}
			if(Num)
			{
				while(Num)
				{
					if(wide)
						replaceString = String.fromCharCode(0xFF10 + Num%8) + replaceString;
					else
						replaceString = Num%8 + replaceString;
					Num = (Num - Num%8) / 8;
				}
			}
			else
			{
				if(wide)
					replaceString = String.fromCharCode(0xFF10);
				else
					replaceString = '0';
			}
			var addNum = widthMin - replaceString.length;
			if(addNum > 0)
			{
				for(;addNum;addNum--)
				{
					if(alignL)
						replaceString = replaceString + spaceChar;
					else
						replaceString = spaceChar + replaceString;
				}
			}
			if(addHexOct)
			{
				if(wide)
					replaceString = String.fromCharCode(0xFF4F) + replaceString;
				else
					replaceString = 'o' + replaceString;
			}
			if(minus)
				if(wide)
					replaceString = String.fromCharCode(0xFF0D) + replaceString;
				else
					replaceString = '-' + replaceString;
			break;

		case 'x':
		case 'X':
			if(isNaN(sprintf.arguments[arguNum]))
				break;
			var Num = parseInt(sprintf.arguments[arguNum]);
			var minus = false;
			if(Num < 0)
			{
				minus = true;
				Num = -Num;
			}
			if(Num)
			{
				while(Num)
				{
					var ch = Num % 16;
					if(ch < 10)
					{
						if(wide)
							replaceString = String.fromCharCode(0xFF10 + ch) + replaceString;
						else
							replaceString = ch + replaceString;
					}
					else
					{
						if(wide)
						{
							if(sprintf.arguments[0].charAt(nPos) == 'x')
								replaceString = String.fromCharCode(0xFF37 + ch) + replaceString;
							else
								replaceString = String.fromCharCode(0xFF17 + ch) + replaceString;
						}
						else
							if(sprintf.arguments[0].charAt(nPos) == 'x')
								replaceString = String.fromCharCode(0x0057 + ch) + replaceString;
							else
								replaceString = String.fromCharCode(0x0037 + ch) + replaceString;
					}
					Num = (Num - ch) / 16;
				}
			}
			else
			{
				if(wide)
					replaceString = String.fromCharCode(0xFF10);
				else
					replaceString = '0';
			}
			var addNum = widthMin - replaceString.length;
			if(addNum > 0)
			{
				for(;addNum;addNum--)
				{
					if(alignL)
						replaceString = replaceString + spaceChar;
					else
						replaceString = spaceChar + replaceString;
				}
			}
			if(addHexOct)
			{
				if(sprintf.arguments[0].charAt(nPos) == 'x')
				{
					if(wide)
						replaceString = String.fromCharCode(0xFF4F, 0xFF58) + replaceString;
					else
						replaceString = '0x' + replaceString;
				}
				else
				{
					if(wide)
						replaceString = String.fromCharCode(0xFF4F, 0xFF38) + replaceString;
					else
						replaceString = '0X' + replaceString;
				}
			}
			if(minus)
				if(wide)
					replaceString = String.fromCharCode(0xFF0D) + replaceString;
				else
					replaceString = '-' + replaceString;
			break;

		case 's':
			if(isNaN(sprintf.arguments[arguNum]))
				replaceString = sprintf.arguments[arguNum];
			else
				replaceString = String(sprintf.arguments[arguNum]);
			var addNum = widthMin - replaceString.length;
			if(addNum > 0)
			{
				for(;addNum;addNum--)
				{
					if(alignL)
						replaceString = replaceString + spaceChar;
					else
						replaceString = spaceChar + replaceString;
				}
			}
			if(widthMax)
				replaceString = replaceString.substr(0, widthMax);
			break;
		}
		returnString = returnString + replaceString;
		nPosPrevEnd = nPos +1;
		arguNum++;
	}

	return returnString + sprintf.arguments[0].substr(nPosPrevEnd);
}
