/*
 * 공통으로 사용하는 상수, 함수 등을 정의함
 */
///////////////////////////////////////////////////////////////////////////
// popup window
var arrWndOpenWindow = new Array();

function push(array, arg){
	array[array.length] = arg;
}
function openWin(sURL, sWindowName, w, h, sScroll, reSize, status)
{
	// 화면 중앙으로 Popup 띄우기.. 스크롤바는 옵션..
	// ex)
	//			openWin("test.asp", "winTest", 400, 300);			☞ 스크롤바 없음
	//			openWin("test.asp", "winTest", 400, 300, "yes");	☞ 스크롤바 있음
	//			openWin("test.asp", "winTest", 400, 300, "auto");	☞ 스크롤바 자동

	var x = (screen.width - w) / 2;
	var y = (screen.height - h) / 2;

	if (sScroll==null) sScroll = "no";
	if (screen.width == 800 && screen.height== 600 ) sScroll = "yes"; // 해상도 800*600일 때 스크롤 생기도록 함 
	
	var sOption = "";
	sOption = sOption + "toolbar=no, channelmode=no, location=no, directories=no, menubar=no";
	sOption = sOption + ", scrollbars=" + sScroll + ", resizable=" + reSize + ", status=" + status +", left=" + x + ", top=" + y + ", width=" + w + ", height=" + h;

	var win = controlOpenWindow(sURL, sWindowName, sOption);
	return win;
}

function openWinFreePos(sURL, sWindowName, w, h, x, y, sScroll)
{
	// 화면 특정위치로 Popup 띄우기.. 스크롤바는 옵션..
	// ex)
	//			openWin("test.asp", "winTest", 400, 300, 0, 0);			☞ 스크롤바 없음
	//			openWin("test.asp", "winTest", 400, 300, 0, 0 "yes");	☞ 스크롤바 있음
	//			openWin("test.asp", "winTest", 400, 300, 0, 0 "auto");	☞ 스크롤바 자동

	if (sScroll==null) sScroll = "no";
	if (screen.width == 800 && screen.height== 600 ) sScroll = "yes"; // 해상도 800*600일 때 스크롤 생기도록 함 
	
	var sOption = "";
	sOption = sOption + "toolbar=no, channelmode=no, location=no, directories=no, resizable=no, menubar=no";
	sOption = sOption + ", scrollbars=" + sScroll + ", left=" + x + ", top=" + y + ", width=" + w + ", height=" + h;

	var win = controlOpenWindow(sURL, sWindowName, sOption);
	return win;
}

function controlOpenWindow(strURL, strWndName, strOptions) {
	var arrTemp = new Array();
	var wndTemp;
	var wndName;
	
	var arrWndOpenWindow = getArrWndOpenWindow();
	if ( arrWndOpenWindow == null) {
		arrWndOpenWindow = this.arrWndOpenWindow;
	}
	for ( i = 0; i < arrWndOpenWindow.length; i++ ) {
		wndTemp = arrWndOpenWindow[i];
		if ( wndTemp == null) {
			continue;
		}
		if ( !wndTemp['window'] ) {
			continue;
		}
		if ( wndTemp['window'].closed ) {
			arrWndOpenWindow[i] = null;
			continue;
		}
		wndName = wndTemp['name'];
		if ( wndName != "" && strWndName == wndName ) {
			wndTemp['window'].location.replace(strURL);

			push(arrTemp, wndTemp);
			for ( j = 0; j < arrTemp.length; j++ ) {
				push(arrWndOpenWindow, arrTemp[j]);
			}
			return null;
		}
		push(arrTemp, wndTemp);
	}
	for ( i = 0 ; i < arrTemp.length; i++ ) {
		push(arrWndOpenWindow, arrTemp[i]);
	}

	var	wndNewWindow = window.open(strURL, '', strOptions);
	
	var wndSaveWindow = new Array();
	wndSaveWindow['name'] = strWndName;
	wndSaveWindow['window'] = wndNewWindow;

	push(arrWndOpenWindow, wndSaveWindow);
	return wndNewWindow;
}
function getArrWndOpenWindow() {
	return top.arrWndOpenWindow;
}

// popup window
///////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////
// popup resizing
/**
 * example:
 *  fitPopupSize( [doMove] );                  <- ID가 'page_content'인 object의 clientWidth, clientHeight 값에 따라 사이즈 조절
 *  fitPopupSize( object, [doMove] );          <- 주어진 object의 clientWidth, clientHeight값에 따라 사이즈 조절
 *  fitPopupSize( "pageOuter", [doMove] );     <- ID가 'pageOuter'인 object의 clientWidth, clientHeight 값에 따라 사이즈 조절
 *  fitPopupSize( 300, 400, [doMove] );        <- 팝업 크기를 300 x 400으로 조절
 */
function fitPopupSize( arg1, arg2, arg3 ) {
	var toWidth = null;
	var toHeight = null;
	var objOut = null;
	var positionRequired = true;

	var typeArg1 = typeof(arg1);
	var typeArg2 = typeof(arg2);
	var typeArg3 = typeof(arg3);
	
	if( typeArg1 == "undefined" || typeArg1 == "boolean" ) {
		objOut = document.getElementById("page_content");
		
		if( typeArg1 == "boolean" ) {
			positionRequired = arg1;
		}
	}
	if( typeArg1 == "string" ) {
		objOut = document.getElementById(arg1);
		if( objOut == null ) { return; }
				
		if( typeArg2 == "boolean" ) {
			positionRequired = arg2;
		}
	}

	if( typeArg1 == "object" ) {
		objOut = arg1;
				
		if( typeArg2 == "boolean" ) {
			positionRequired = arg2;
		}
	}
	
	if( objOut != null ) {
		var objParent = objOut.parentNode;
		var getStyle = function(obj) {
			if( obj.currentStyle ) {
				return obj.currentStyle;
			} else if( document.defaultView.getComputedStyle ) {
				return document.defaultView.getComputedStyle(obj, null);
			}
			}

		if( objParent != null && objParent.tagName == "BODY" && getStyle(objOut).position == "absolute" ) {
			try {
				var bodyPaddingTop = 0; var outerPaddingTop = 0;
				var bodyPaddingBottom = 0; var outerPaddingBottom = 0;
				var bodyPaddingLeft = 0; var outerPaddingLeft = 0;
				var bodyPaddingRight = 0; var outerPaddingRight = 0;
				
				if( document.body.currentStyle ) {
					var bodyCurrentStyle = document.body.currentStyle;
					bodyPaddingTop = parseInt(bodyCurrentStyle.paddingTop);
					bodyPaddingBottom = parseInt(bodyCurrentStyle.paddingBottom);
					bodyPaddingLeft = parseInt(bodyCurrentStyle.paddingLeft);
					bodyPaddingRight = parseInt(bodyCurrentStyle.paddingRight);
					
					var outerCurrentStyle = objOut.currentStyle;
					outerPaddingTop = parseInt(outerCurrentStyle.paddingTop);
					outerPaddingBottom = parseInt(outerCurrentStyle.paddingBottom);
					outerPaddingLeft = parseInt(outerCurrentStyle.paddingLeft);
					outerPaddingRight = parseInt(outerCurrentStyle.paddingRight);
					
				} else if( document.defaultView.getComputedStyle ) {
					var objBodyStyle = document.defaultView.getComputedStyle(document.body, null);
					bodyPaddingTop = parseInt(objBodyStyle.getPropertyValue("padding-top"));
					bodyPaddingBottom = parseInt(objBodyStyle.getPropertyValue("padding-bottom"));
					bodyPaddingLeft = parseInt(objBodyStyle.getPropertyValue("padding-left"));
					bodyPaddingRight = parseInt(objBodyStyle.getPropertyValue("padding-right"));
					
					var outerCurrentStyle = document.defaultView.getComputedStyle(objOut, null);
					outerPaddingTop = parseInt(outerCurrentStyle.getPropertyValue("padding-top"));
					outerPaddingBottom = parseInt(outerCurrentStyle.getPropertyValue("padding-bottom"));
					outerPaddingLeft = parseInt(outerCurrentStyle.getPropertyValue("padding-left"));
					outerPaddingRight = parseInt(outerCurrentStyle.getPropertyValue("padding-right"));
				}
				
				var adjustHeight = bodyPaddingTop + bodyPaddingBottom;
				var adjustWidth = bodyPaddingLeft + bodyPaddingRight;
				
				var oWidth = objOut.clientWidth;
				var oHeight = objOut.clientHeight;
				
				// objOut.style.height = oHeight + adjustHeight;				
				objOut.style.paddingTop = outerPaddingTop + bodyPaddingTop;
				objOut.style.paddingBottom = outerPaddingBottom + bodyPaddingBottom;

				// objOut.style.width = oWidth + adjustWidth;
				objOut.style.paddingLeft = outerPaddingLeft + bodyPaddingLeft;
				objOut.style.paddingRight = outerPaddingRight + bodyPaddingRight;
			} catch(e) {
				// do nothing
				// alert( e.message );
			}
		}
		toWidth = objOut.clientWidth;
		toHeight = objOut.clientHeight;
	}

	if( typeArg1 == "number" && typeof(arg2) == "number" ) {
		toWidth = arg1;
		toHeight = arg2;
		
		if( typeArg3 == "boolean" ) {
			positionRequired = arg3;
		}
	}

	if( toWidth == null && toHeight == null ) { return; }
	
	if (toWidth > screen.availWidth) toWidth = screen.availWidth;
	if (toHeight > screen.availHeight) toHeight = screen.availHeight;


	if( positionRequired ) {
		fitPopupSize_adjustPosition( toWidth, toHeight );
	}

	fitPopupSize_resize( toWidth, toHeight );
}

function fitPopupSize_adjustPosition( toWidth, toHeight ) {
	var posLeft = (window.screenLeft) ? window.screenLeft : window.screenX;
	var posTop = (window.screenTop) ? window.screenTop : window.screenY;
	var adjustLeft = 0;	var marginWidth = 50;
	var adjustTop = 0;	var marginHeight = 50;
	var movingRequired = false;

	if( posTop + toHeight + marginHeight > screen.availHeight ) {
		adjustTop = -(posTop + toHeight + marginHeight - screen.availHeight);
		movingRequired = true;
	}
	if( posLeft + toWidth + marginWidth > screen.availWidth ) {
		adjustLeft = -(posLeft + toWidth + marginWidth - screen.availWidth);
		movingRequired = true;
	}
	if( movingRequired ) {
		window.moveBy( adjustLeft, adjustTop );
	}
}

function fitPopupSize_resize( toWidth, toHeight ) {
	var oBody = document.body;
	if( oBody == null ) { return; }

	if( typeof(window.innerHeight) != "undefined" && typeof(window.innerWidth) != "undefined" ) {
		window.innerHeight = toHeight;
		window.innerWidth = toWidth;
	} else {
		var clientWidth = Math.max( document.documentElement.clientWidth, oBody.clientWidth );
		var clientHeight = Math.max( document.documentElement.clientHeight, oBody.clientHeight );
	
		var diffX = toWidth - clientWidth;
		var diffY = toHeight - clientHeight;

		window.resizeBy( diffX, diffY );
	}
/*	
	var posLeft = (window.screenLeft) ? window.screenLeft : window.screenX;
	var posTop = (window.screenTop) ? window.screenTop : window.screenY;

	if( oBody.clientHeight > screen.availHeight ) {
		var adjustHeight = screen.availHeight - (oBody.clientHeight + 50);
		window.moveTo(posLeft, 0);
		window.resizeBy( 0, adjustHeight );
	}
*/	
}

function fitPopupSizeEx() {
	var oBody = document.body;
	if( oBody == null ) { return; }
	
	var idPrefix = "page_content";
	var objId = "";
	var chkResult = false;
	var objChk = null;
	
	for( var i=65; i<=90; i++ ) {
		objId = idPrefix + String.fromCharCode(i);
		objChk = document.getElementById(objId);
		if( objChk == null ) { break; }
	}
	if( objChk != null ) { return; }

	var objOut = window.document.createElement("<div id='" + objId + "' style='position:absolute;left:0;top:0;'>");

	var existNodeList = window.document.body.childNodes;
	while( existNodeList.length > 0 ) {
		objOut.appendChild( existNodeList[0] );
	}
	document.body.appendChild( objOut );

	var toWidth = objOut.clientWidth;
	var toHeight = objOut.clientHeight;
	if( toWidth == null && toHeight == null ) { return; }

	fitPopupSize_adjustPosition( toWidth, toHeight );

	fitPopupSize_resize( toWidth, toHeight );
}
// popup resizing
///////////////////////////////////////////////////////////////////////////


//이벤트 핸들러 wrapper function
function addEventHandler( eventName, handler ) {
	if( window.addEventListener ) {
		window.addEventListener( eventName, handler, false );
	} else if( window.attachEvent ) {
		window.attachEvent( eventName, handler );
	}
}


// 문자열에 공백제거..
// ex) s = "       asdf     ".trim();	--> s = "asdf"
String.prototype.trim = function() {

    return this.replace(/(^\s*)|(\s*$)/g, "");
}

//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
function getEnvPrefixFrom(url) {
	url = url.toLowerCase().trim();
	var prefixes = ['local-', 'dev-', 'alpha-', 'beta-'];
	for (var i=0; i<prefixes.length; i++) {		
		if (url.indexOf('http://' + prefixes[i]) == 0) {
			return prefixes[i];
		}
	}
	return '';
}
