//get the true position of the element
function getPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

//get inner window size
function getInnerSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [myWidth, myHeight];
}

//get scroll X Y
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Non-IE
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //IE 4 compatible
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE 6+ in 'standards compliant mode'
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [scrOfX, scrOfY];
}

//get page size with scroll
function getPageSize(){
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;
		xWithScroll = document.body.offsetWidth;
  	}
	return [xWithScroll,yWithScroll];
}

/*
	@param msg string, displayed message
	@param type int, 0:information, 1:error
*/
function msgbox(msg, type) {
	//init type and width
	var width = 0;
	if(type == '0') {
		type = 'info';
		width = 212;
	} else {
		type = 'error';
		width = 146;
	}
	var pageSize = getPageSize();
	/*create a transparent iframe to cover controls,
	   This is a bug in IE below ver 7, thanks Microsoft= =*/
	var frmTemp = document.createElement('iframe');
	frmTemp.id = 'msgboxFrame';
	frmTemp.src = 'javascript:false;';
	frmTemp.style.position = 'absolute';
	frmTemp.style.left = '0px';
	frmTemp.style.top = '0px';
	frmTemp.style.width = pageSize[0] + 'px';
	frmTemp.style.height = pageSize[1] + 'px';
	frmTemp.style.filter = "alpha(opacity=0)";
	frmTemp.style.opacity = '0';
	document.body.appendChild(frmTemp);
	//make an alpha layer to cover all elements
	var mask = document.createElement('div');
	mask.id = 'msgboxMask';
	mask.style.position = 'absolute';
	mask.style.left = '0px';
	mask.style.top = '0px';
	mask.style.width = pageSize[0] + 'px';
	mask.style.height = pageSize[1] + 'px';
	mask.style.backgroundColor = "#000000";
	mask.style.filter = "alpha(opacity=70)";
	mask.style.opacity = '0.7';
	document.body.appendChild(mask);
	//create message dialog
	var innerSize = getInnerSize();
	var divDialog = document.createElement('div');
	divDialog.id = 'msgbox';
	divDialog.style.position = 'absolute';
	/*divDialog.innerHTML = '<table><tr><td><img src="msgboxFile/' + type + '.png" width="' + width + 
		'" height="80" /></td><td valign="middle"><br /><font style="color:#FFFFFF;font-size:13px">' + msg + 
		'</font></td></tr><tr><td colspan="2" align="middle"><a href="#" onClick="clearMsgbox();"><img border="0" src="msgboxFile/' + type + 
		'_ok.png" width="90" height="40" /></a></td></tr></table>';*/
	//standard style
	divDialog.innerHTML = '<table><tr><td></td><td valign="middle"><br /><font style="color:#FFFFFF;font-size:16px">' + msg + 
		'</font></td></tr><tr><td colspan="2" style="text-align:center;"><span onClick="clearMsgbox();" style="color:#FF0000;font-size:13px;cursor:pointer;">確認</span></td></tr></table>';
	document.body.appendChild(divDialog);
	divDialog.style.left = (innerSize[0] / 2 - divDialog.offsetWidth / 2) + 'px';
	divDialog.style.top = (innerSize[1] / 2 - divDialog.offsetHeight / 2) + 'px';
}

function clearMsgbox() {
	document.body.removeChild(document.getElementById('msgboxFrame'));
	document.body.removeChild(document.getElementById('msgboxMask'));
	document.body.removeChild(document.getElementById('msgbox'));
}