/* Warning: Please carry out the opening save of this file by UTF-8. */

//Copy From http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/overview/browserdetection.asp
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
function getInternetExplorerVersion()
{
  var rv = -1; // Return value assumes failure
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

// Global variables
var isCSS, isW3C, isIE4, isNN4, isIE6CSS;
// Initialize upon load to let all browsers establish content objects
function initDHTMLAPI() {
	if (document.images) {
		isCSS = (document.body && document.body.style) ? true : false;
		isW3C = (isCSS && document.getElementById) ? true : false;
		isIE4 = (isCSS && document.all) ? true : false;
		isNN4 = (document.layers) ? true : false;
		isIE6CSS = (document.compatMode && document.compatMode.indexOf("CSS1") >= 0) ? true : false;
	}

	__bInitDhtmlApi = true;
}
// Set event handler to initialize API
//window.onload = initDHTMLAPI;
var __bInitDhtmlApi = false;

// Return the available content width space in browser window
function getInsideWindowWidth() {
	if (window.innerWidth) {
		return window.innerWidth;
	} else if (isIE6CSS) {
		// measure the html element's clientWidth
		return document.body.parentElement.clientWidth
	} else if (document.body && document.body.clientWidth) {
		return document.body.clientWidth;
	}
	return 0;
}

// Return the available content height space in browser window
function getInsideWindowHeight() {
	if (window.innerHeight) {
		return window.innerHeight;
	} else if (isIE6CSS) {
		// measure the html element's clientHeight
		return document.body.parentElement.clientHeight
	} else if (document.body && document.body.clientHeight) {
		return document.body.clientHeight;
	}
	return 0;
}

function addEvent(element, eventType, fn, useCapture) {
	if (element.addEventListener) { // W3C DOM Standard - FireFox ...
		useCapture = ((useCapture) ? true : false);
		element.addEventListener(eventType, fn, useCapture);
		return true;
	}
	else if (element.attachEvent) { // Internet Explorer
		var r = element.attachEvent('on' + eventType, fn);
		return r;
	}
	else {
		element['on'+eventType] = fn;
	}
}

/* Focus is moved to the first object of the element.  */
/* 3.1.1.0 (2003/03/28) When the first element is a button, focus is not applied.  */

var _sCompat_ = document.compatMode;
// var bIsBackCompat = ( "BackCompat" == _sCompat_ );
var bIsCSS1Compat = ( "CSS1Compat" == _sCompat_ );

/* 3.5.6.4 (2004/07/16) Focus is appropriated to the selection check box. */
/* 3.6.0.0 (2004/09/17) One focus of the selection check is appropriated ahead by h_focus. */
/* 3.7.0.3 (2005/02/25) Correspondence of setScrollRowNo. two or more h_focus appears.  */
/* 3.7.0.4 (2005/03/14) Correspondence when the first line is checked.  */
/* 3.8.0.3 (2005/07/15) New Header Fix Type Add  */
/* 3.8.6.3 (2006/11/30) h_focus scrollLeft  */
/* 3.8.8.1 (2007/01/06) Old Header Fix Type scrollTop offset -25 */
function focus2() {
	var fcu = document.getElementsByName( "h_fcs" );
	if( fcu.length > 0 ) {
		var oMySelf = fcu[0];
		var oTr, oLayer, oDiv, oRun;

		oRun = oMySelf.parentNode;
		while(oRun) {
			if(oRun.tagName == "TR" ) oTr = oRun;
			if(oTr && oRun.tagName == "DIV") {
				if(oRun.id == "layer") oLayer = oRun;
				if(oRun.id == "X1" || oRun.id == "X2") { oDiv = oRun; break; }
			}
			oRun = oRun.parentNode;
		}
		if(oLayer) {
			var oTHeads = oLayer.getElementsByTagName("THEAD");
			var oDivHeader;
			if(oTHeads.length >0 && oTHeads[0].id == "divheader") oDivHeader = oTHeads[0];
			if(oDivHeader) {
				//Set Expression Style Header Fixed(compatible with old version)
				oMySelf.scrollIntoView( true );
				if( oLayer.scrollTop >= oDivHeader.offsetHeight ) {		// 3.8.0.3
						oLayer.scrollTop -= oDivHeader.offsetHeight;
				}
			}
			else {
				//Div Style Header Fixed)
				oLayer.scrollIntoView( true );
				// oLayer.scrollTop = oTr.offsetTop;
				oLayer.scrollTop = Math.max( oTr.offsetTop - 5, 0 );
				// HSibling : 左右分割の意味
				// if(oLayer.HSibling) oLayer.HSibling.scrollTop = oTr.offsetTop;
				if(oLayer.HSibling) oLayer.HSibling.scrollTop = Math.max( oTr.offsetTop - 5, 0 ); // 4.3.5.3 (2009/02/22)
			}
		}
		else if(oDiv) {
			//Gantt Scroll
			oDiv.scrollIntoView( true );
			// oDiv.scrollTop = oTr.offsetTop;
			oDiv.scrollTop = Math.max( oTr.offsetTop - 5, 0 );
			// HSibling : 左右分割の意味
			// if(oDiv.HSibling) oDiv.HSibling.scrollTop = oTr.offsetTop;
			if(oDiv.HSibling) oDiv.HSibling.scrollTop = Math.max( oTr.offsetTop - 5, 0 ); // 4.3.5.3 (2009/02/22)
		}
		else {
			//Unspecified Table View
			oMySelf.scrollIntoView( true );
		}
	}
}

/* 3.5.4.0 (2003/11/25) */
function historyBack() {
	var oAllForms = document.getElementsByTagName("FORM");
	if ( oAllForms.length > 0 ) {
		var f = oAllForms[0];
		for (i = 0; i < f.elements.length; i++) {
			var obj = f.elements[i];
			if ( obj != null && obj.name == "historyBack" ) {
				if( obj.value == "1" ) {
					obj.value = "0";
					f.submit();
				}
				else {
					obj.value = "1";
				}
				break;
			}
		}
	}
}

/*
 *  The image is replaced.
 *
 *    <a href="http://xxxxx"
 *			onMouseOver="changeImage('img01','over.gif')"
 *			onMouseOut ="changeImage('img01','up.gif')"  >
 *		<img src="up.gif" alt="IMG" border="0" name="img01">
 *	</a>
 */
function changeImage( id,src ) {
	document.images[id].src = src;
}

/*
 * 3.3.3.0 (2003/07/09) Double registration prevention of data by pushing twice
 *
 *	<form method="POST" action="forward.jsp" target="RESULT"
 *	                            onSubmit="return oneClick();">
 *	                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *	・・・
 *	</form>
 */
var isClicked = false;
function oneClick() {
	if(isClicked) {
//		alert( "Don't twice pushed." );
		alert( "Don't click twice." );
		return false;
	} else {
		isClicked = true;
		return true;
	}
}

/*
 * 3.5.0.0 (2003/09/17) It submits it.
 *
 *  renew( action ,target,[this] );
 *
 *   <og:column name="KBSAKU" onChange="renew('query.jsp','QUERY');" />
 *   <og:column name="KBSAKU" onChange="renew('query.jsp','QUERY',this);" />
 *
 * Ver 3.8.6.3 (2006/11/30) Self form submit when this argment add.
 */
function renew( action , target , home ) {
	var f = null;

	if( home != null ) {
		f = home.form;
	}
	else {
		var oAllForms = document.getElementsByTagName("FORM");
		if ( oAllForms.length > 0 ) {
			f = oAllForms[0];
		}
	}

	if( f != null ) {
		f.action= action ;
		f.target= target ;

		f.submit();
	}
}

/*
 * 3.5.4.2 (2003/12/15)
 *  The function to move the input cursor to the following field automatically is added.
 *
 * nextForm( this, nextFieldName, maxLength )
 *
 * 3.5.5.9 (2004/05/31) Correspondence to there are two or more attributes name (Example: Button etc.) the same
 * 3.8.1.2 (2005/12/19) The matter where focus cannot be moved to the pull-down menu is corrected.
 *
 *   <og:column name="A" nextForm="B" />  ⇒ <input type="text" name="A" onKeyUp="nextForm(this, 'B', 3)" maxlength="3" />
 *   <og:column name="B" nextForm="C" />  ⇒ <input type="text" name="B" onKeyUp="nextForm(this, 'C', 5)" maxlength="5" />
 *   <og:column name="C" />                  <input type="text" name="C" maxlength="3" />
 */
function nextForm( tx, nm, maxlen ) {
	if( tx.value.length >= maxlen ) {
		var e = tx.form.elements[nm];
//		if( e[0] != null ) {
		if( e[0] != null && e[0].name != null ) {
			e[0].focus();
		}
		else if( e != null ) {
			e.focus();
		}
		else {
			alert( "There is no field named [" + nm + "]" );
		}
	}
}

/*
 * 3.5.4.3 (2004/01/05) Function to select all checks of check box.
 * 3.5.4.8 (2004/02/10) Correspondence when multiple form is arranged in the same page
 *
 * When selectedType of viewFormTag is only CHECKBOX, it is possible to use it.
 *
 *   <input type="checkbox" name="ALL_CHECK" onClick="checkbox(this);" />
 */
function checkbox( cb ) {
	var f = cb.form;

	if (f != null) {
		var eles    = f.elements ;
		var elesLen = eles.length;
		for ( i=0; i<elesLen; i++ ){
			var el = eles[i];
	//		if ( el.name == "h_rowSelected" || el.name == "ALL_CHECK" ) {
			if ( el.name == "h_rowSel" || el.name == "ALL_CHECK" ) {
				 el.checked = cb.checked ;
			}
		}
	}
}

/*
 * 3.5.4.8 (2004/02/23)
 * The pull-down menu that reacts to the key input of two or more character strings is added.
 *
 * When the number of input items is 20 or more, it is set by the automatic operation.
 *
 *	<og:select name="TABLE_NAME" optionAttributes="onkeydown='setKeySelect(this);'" >
 *	<select name="XXX" onkeydown="setKeySelect(this);" >････
 */
var EMPTY_STR = "";
var strOldKey  = EMPTY_STR;
var oOldSelect = null;
var BACKSPACE_CODE = 8

function clearOldKey() {
    strOldKey  = EMPTY_STR;
}

function setKeySelect(oMySelect) {
	var bFound  = false;

	if( oOldSelect != oMySelect ) {
		strOldKey  = EMPTY_STR;
		oOldSelect = oMySelect;
        //Clear Old Data
        if(null == oMySelect.onblur) oMySelect.onblur = clearOldKey;
	}

	var oOptions = oMySelect.options;
	var nCode = event.keyCode;

	// ((nCode >= 0x25) && (nCode <= 0x28))   Arrow Key
	// ((nCode >= 0x30) && (nCode <= 0x5A))   Number and Alpha
	// ((nCode >= 0x60) && (nCode <= 0x69))   Numpad
	// ((nCode >= 0x6A) && (nCode <= 0x6F))   Numpad * + ent - . /
	var bProcessKey = (   ((nCode >= 0x25) && (nCode <= 0x28))
					||  ((nCode >= 0x30) && (nCode <= 0x5A))
					||  ((nCode >= 0x60) && (nCode <= 0x69))
					||  (BACKSPACE_CODE == nCode) );
	if( bProcessKey) {
		var bIsArrayKey = (((nCode >= 0x25 ) && (nCode <= 0x28)) && !event.shiftKey);
		if(!bIsArrayKey) {
			var strKey ;
			if( 0x5A >= nCode)	{ strKey = String.fromCharCode(nCode).toUpperCase(); }
			else				{ strKey = String.fromCharCode(nCode - 0x30);        }

			var strOldKey2;
			var nIndex;
			if( BACKSPACE_CODE == nCode) {
				strOldKey2 = ((0 < strOldKey.length) ? strOldKey.substr(0, strOldKey.length -1) : EMPTY_STR);
			}
			else {
				strOldKey2 = strOldKey + strKey;
			}
			var nKeyLen = strOldKey2.length;

			for ( nIndex = 0; nIndex < oOptions.length; nIndex++) {
				if( strOldKey2 == oOptions(nIndex).text.substr(0, nKeyLen).toUpperCase() ) {
					bFound = true;
					break;
				}
			}

			if( bFound ) {
				strOldKey = strOldKey2;
				if( oMySelect.selectedIndex != nIndex ) { oMySelect.selectedIndex = nIndex; }
			}

			event.returnValue = false;
		}
		else {
			strOldKey = EMPTY_STR;
		}
	}

	return bFound;
}

/*
 * 3.5.6.2 (2004/07/04) Window opens.
 * The event is received, and new window is opened at the clicked position.
 *
 * window_open( url,evt,name,opt )
 *   url  : Opening URL is specified.
 *   evt  : When the number of cases is a little, height is adjusted, and the scroll bar is displayed.
 *   name : It is a name when the value is returned.
 *   opt  : It is an option for the opening of window.
 *
 *  The following examples are samples of the calendar assistance menu.
 *
 *  TEST1:<input type="text" name="TEST1"  id="TEST1"  size="10" maxlength="8" value="20040201" />
 *  <img src="calendar.gif" onclick="window_open('../common/calendar.html', event,'TEST1','width=220,height=170,resizable=no' );" />
 *
 */
function window_open( url,evt,name,opt ){
	var option = opt;
	if( evt != null ) {
		var move = 'left=' + evt.x + ',top=' + evt.y ;
		if( option != null ) {
			option += ',' + move ;
		}
	}

	window.open( url, name, option );
}

/*
 * 3.8.0.8 (2005/10/03) Adjust Frame Height.(Option)
 * 3.8.6.1 (2006/10/20) SUB_FRAME Object get method changed.
 * 3.8.9.1 (2007/06/15) Correspondence of case with frame division into three or more.
 * 4.3.6.0 (2009/04/01) 3つ以上フレームが存在する場合でも自動調整できるように対応
 */
function adjustFrameHeight() {
	var oFramesets = parent.document.getElementsByName( "SUB_FRAME" );
	if( oFramesets == null ) { return; }

	var subFrmObj  = null ;
	var oFrames = null;
	if( oFramesets.length == 1 ) {
		subFrmObj = oFramesets[0];
		oFrames = parent.frames;
	}

	if( subFrmObj == null || subFrmObj.rows == null || subFrmObj.rows.length == 0
		|| oFrames == null || oFrames.length == 0 ) { return; }

	var origRows = subFrmObj.rows.split(",");
	var tempRows = null;
	var endMarkObj = null;
	for( i=0; i<oFrames.length-1; i++ ) {
		endMarkObj = oFrames[i].document.getElementById( "END_MARKER" );
		if( endMarkObj == null ) {
			return;
		}
		else {
			tempRows = tempRows + ( endMarkObj.offsetTop + endMarkObj.parentNode.offsetTop + 4 ) + ",";
		}
	}
	tempRows = tempRows + "*";

	subFrmObj.rows = tempRows;
}

/*
 * 3.8.5.0 (2006/03/06) Adjust Textarea Auto Rows Size.
 *
 * autoArea( Textarea_Object,[MaxRowsSize] )
 *
 * <textarea id="area1" cols="80" rows="10" onKeyup="autoArea( this );"    ></textarea>
 * <textarea id="area2" cols="80" rows="5"  onKeyup="autoArea( this,20 );" ></textarea>
 *
 */
var initRow = new Object();

function autoArea( ara,maxRow ) {
	if( ara == null ) { return; }
	if( initRow[ara.name] == null ) { initRow[ara.name] = ara.rows; }

	if( isCntAction( event.keyCode )  ) {
		var text = ara.value ;
		var cnt = 1;
		var idx = text.indexOf( '\n' );
		while( idx >= 0 ) {
			cnt++ ;
			idx = text.indexOf( '\n' ,idx+1 );
		}
		if( cnt < initRow[ara.name] ) { cnt = initRow[ara.name]; }
		if( maxRow != null && cnt > maxRow ) { cnt = maxRow; }
		ara.rows = cnt;
	}
}

/*
 * 3.8.5.0 (2006/03/06) Adjust Textarea Auto Rows Size. sub function
 *
 * isCntAction( val )
 */
function isCntAction( val ) {
	return ( 	val == 13		// enter(10key)
			 || val == 14		// return
			 || val == 8		// back space
			 || val == 46		// del
			 || val == 67		// ctlr + C
			 || val == 86		// ctlr + V
			 || val == 88		// ctlr + X
			 ) ;
}

/*
 * 3.8.5.0 (2006/03/20) fieldset display ON/OFF Function.
 * 3.8.5.2 (2006/05/31) fieldset default ON/OFF hidden out.
 */
function hide( id,hiddenKey ) {
	var obj = document.getElementById( id + 'A' );
	if( obj != null ) {
		var obj2 = document.getElementById( id + 'B' );
		var obj3 = null;
		if( hiddenKey != null ) {
			obj3 = document.getElementsByName( hiddenKey );
		}

		if( obj.style.display == 'none' ) {
			obj.style.display = 'inline';
			if( obj2 != null ) { obj2.innerHTML = '- ' ; }
			if( obj3 != null ) {
				for( var i=1; i<obj3.length; i++ ){
					obj3[i].value = "false" ;
				}
			}
		}
		else {
			obj.style.display = 'none';
			if( obj2 != null ) { obj2.innerHTML = '+ ' ; }
			if( obj3 != null ) {
				for( var i=1; i<obj3.length; i++ ){
					obj3[i].value = "true" ;
				}
			}
		}
	}
	adjustFrameHeight();
}

/*
 * 3.8.5.3 (2006/06/30) replace All Text
 *
 * <input name="XXX" value="YYY" onBlur="value=replaceText(value,'-','');" />
 */
function replaceText( val,oldTxt,newTxt ) {
	var rtn = val ;
	if( rtn != null ) {
		while( rtn.indexOf( oldTxt ) >= 0 ) {
			rtn = rtn.replace( oldTxt,newTxt );
		}
	}
	return rtn;
}

/*
 * 3.8.6.1 (2006/10/24)
 */
function getDimensions(obj){
	var _width	= obj.offsetWidth;
	var _height = obj.offsetHeight;
	var _top = 0, _left = 0;
	for (; obj != null; obj = obj.offsetParent) {
		_top += obj.offsetTop;
		_left += obj.offsetLeft;
	}
	return {top:_top, left:_left, width:_width, height:_height};
}

function getClientHeight() {
	var nClientHeight = 0;
	if( bIsCSS1Compat ) {
		nClientHeight =document.documentElement.clientHeight;
	}
	else {
		nClientHeight =document.body.clientHeight;
	}

	return nClientHeight;
}

function getClientWidth() {
	var nClientWidth = 0;
	if( bIsCSS1Compat ) {
		nClientWidth =document.documentElement.clientWidth;
	}
	else {
		nClientWidth =document.body.clientWidth;
	}

	return nClientWidth;
}

/* Dummy function */
function hilightRow()
{
	return ;
}

/*
 * 4.3.1.0 (2008/09/03)
 *
 * for prev,next
 */
function submitPrevNextForm( val ) {
	var frm = document.forms[0];
	var cmd = document.getElementById("h_cmd");
	if( frm != null && cmd != null ) {
		cmd.value = val;
		frm.submit();
		cmd.value = "NEW";
	}
	return false;
}

/*
 * 4.3.6.0 (2009/04/01)
 *
 * for expand menu
 * @param evt event
 * @param trg trigger (this)
 * @param hiddenClass target class name
 */
function hideClass( evt, trg, hiddenClass, imgA, imgB ){
	atsrc = $(trg).attr("src");
	if(atsrc.indexOf( imgA ) >= 0){
		$(trg).attr("src",atsrc.replace( imgA, imgB ));
	} 
	else{
		$(trg).attr('src',atsrc.replace( imgB ,imgA ));
	}
	$('.'+hiddenClass).toggle();
	adjustFrameHeight();
}
