/** ------------------------------------------------------------------------------------------------
 fnAddLoadEvent(함수명) - window.onload 에 함수 추가
 ------------------------------------------------------------------------------------------------**/
function fnAddLoadEvent(func){
	var oldonload = window.onload;

	if(typeof window.onload != 'function'){
			window.onload = func;
	}
	else{
		window.onload = function(){
			oldonload();
			func();
		}
	}
}


/** ------------------------------------------------------------------------------------------------
 fnComma(숫자) - 100,000 이렇게 리턴
 ------------------------------------------------------------------------------------------------**/
 /*
function fnComma(pStrText){
	var objRegExp = new RegExp('(^[+-]?\d+)(\d{3})');
	while(objRegExp.test(pStrText))
	{
		pStrText = pStrText.replace(objRegExp, '$1,$2');
	}
	return pStrText;
}
*/

function fnComma(n) {
	 var reg = /(^[+-]?\d+)(\d{3})/;   // 정규식
	n += '';                          // 숫자를 문자열로 변환

	while (reg.test(n))
	n = n.replace(reg, '$1' + ',' + '$2');

	return n;
}

/** ------------------------------------------------------------------------------------------------
 fnAddZero(숫자) - "0숫자"로 리턴
 ex) 날짜나 시간에서 한자리 숫자로만 나올때 앞자리에 0을 붙임
 ------------------------------------------------------------------------------------------------**/
function fnAddZero(pIntNum) {
	if (pIntNum < 10) {
		pIntNum = '0'+pIntNum;
	}
	return pIntNum;
}

/** ------------------------------------------------------------------------------------------------
 fnCheckBoxAll(선택체크박스, 전체체크박스배열) - 전체체크박스의 배열을 선택체크박스에
 checked 값으로 모두 바꾼다.
 p.s 일괄 선택, 해제
 ------------------------------------------------------------------------------------------------**/
function fnCheckBoxAll(pIntId, pStrId){
	var length = document.getElementsByName(pStrId).length;
	var flag = document.getElementById(pIntId).checked;
	for(i=0;i<length;i++){
		document.getElementsByName(pStrId)[i].checked = flag;
	}
}


/** ------------------------------------------------------------------------------------------------
fnOpenWindow (URL, 가로, 세로, 리사이즈여부(yes/no), 스크롤여부( yes/no), 새창이름)
 URL을 새창으로 정해진 사이즈로 띄운다 (정가운데에)
 ------------------------------------------------------------------------------------------------**/
function fnOpenWindow(strURL, intWidth, intHeight, blnResize, blnScroll, windowName) {
	var left = (screen.width - intWidth) / 2;
	var top = (screen.height - intHeight) / 2;
	var settings = "width=" + intWidth;
		settings = settings + ", height=" + intHeight;
		settings = settings + ", left=" + left;
		settings = settings + ", top=" + top;
		settings = settings + ", scrollbars=" + blnScroll;
		settings = settings + ", resizable=" + blnResize;
		settings = settings + ",status=yes"

	var win = window.open(strURL, windowName, settings).focus();
//	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}


/** ------------------------------------------------------------------------------------------------
fnOpenWindowPoint (URL, 가로, 세로, 리사이즈여부(yes/no), 스크롤여부( yes/no), 새창이름, 위, 좌)
 URL을 새창으로 정해진 사이즈로 띄운다
 ------------------------------------------------------------------------------------------------**/
function fnOpenWindowPoint(strURL, intWidth, intHeight, blnResize, blnScroll, windowName, intTop, intLeft) {
	var left = (screen.width - intWidth) / 2;
	var top = (screen.height - intHeight) / 2;
	var settings = "width=" + intWidth;
		settings = settings + ", height=" + intHeight;
		settings = settings + ", left=" + intLeft;
		settings = settings + ", top=" + intTop;
//		settings = settings + ", scrollbars=" + blnScroll;
		settings = settings + ", scrollbars= yes"
		settings = settings + ", resizable=" + blnResize;
		settings = settings + ",status=yes"

	var win = window.open(strURL, windowName, settings);
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }

	return win;
}


/** ------------------------------------------------------------------------------------------------
fnOpenWindowSubmit (URL, 가로, 세로, 리사이즈여부(yes/no), 스크롤여부( yes/no), Target, form)
 URL을 새창으로 정해진 사이즈로 띄운다 (정가운데에)
 ------------------------------------------------------------------------------------------------**/
function fnOpenWindowSubmit(strURL, intWidth, intHeight, blnResize, blnScroll, pTarget, pObjForm) {
	var left = (screen.width - intWidth) / 2;
	var top = (screen.height - intHeight) / 2;
	var settings = "width=" + intWidth;
		settings = settings + ", status=yes, height=" + intHeight;
		settings = settings + ", left=" + left;
		settings = settings + ", top=" + top;
		settings = settings + ", scrollbars=" + blnScroll;
		settings = settings + ", resizable=" + blnResize;

	var win = window.open("", pTarget, settings);

	pObjForm.method = "post";
	pObjForm.action = strURL;
	pObjForm.target = pTarget;
	pObjForm.submit();

	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

/** ------------------------------------------------------------------------------------------------
	쿠키제어 스크립트
 ------------------------------------------------------------------------------------------------**/
 function openPopup(cookiename, url, winname, w, h, blnResize, blnScroll) {
	var openok = getCookie(cookiename)
	if ( openok == "no" ) {
	} else {
		openWindow(url, winname, w, h, blnResize, blnScroll);
	}
	return false;
}

function getCookie(name) {
	var prefix = name + "=";

	var cookieStartIndex = document.cookie.indexOf(prefix);
	if (cookieStartIndex == -1) return null;

	var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
	if (cookieEndIndex == -1) cookieEndIndex = document.cookie.length;
	return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

function setCookie(cookiename, value, expiredays)
{
	var todayDate = new Date();
	todayDate.setDate( todayDate.getDate() + expiredays );
	document.cookie = cookiename + "=" + escape( value ) + "; path=/; expires=" + todayDate.toGMTString() + ";"
}
/** 일반 팝업창 그냥 닫기 **/
function fnClosePopup() {
	self.close();
}

/** 부모창 리로드하고 일반 팝업창 그냥 닫기 **/
function fnReloadClosePopup() {
	opener.location.reload();
	opener.focus();
	self.close();
}

/** 쿠기생성하고 창닫기 **/
//-- setCookieCloseWindow(쿠키이름, 쿠키기간)
function setCookieCloseWindow(cookiename, idName) {
	setCookie(cookiename, "no" , idName);
	window.close()
}

/** 레이어 팝업 창닫기(쿠키 생성 후 닫기) **/
//-- closePopCookie(쿠키명, id, 쿠키기간)
//-- 쿠키 값은 무조건 no
function closePopupCookie(cookieName, idName, intDay) {
	setCookie(cookieName, "no" , intDay);
	document.getElementById(idName).style.display = "none";
}
/** 레이어 팝업 창닫기(그냥 닫기) **/
function closePopup(idName) {
	document.getElementById(idName).style.display = "none";
}

/** ModalessDialog open **/
function ModalessDialog( url, args, width, height) {
	var env_options = "dialogHeight: " + height + "px; dialogWidth: " + width + "px;  edge: Raised; center: Yes; help: No; scroll: No; resizable: No; status: No;";
	window.showModelessDialog( url, args, env_options);
}

/** ModalessDialog open **/
function ModalessDialog( url, args, width, height) {
	var env_options = "dialogHeight: " + height + "px; dialogWidth: " + width + "px;  edge: Raised; center: Yes; help: No; scroll: No; resizable: No; status: No;";
	window.showModelessDialog( url, args, env_options);
}

/** 팝업 사이즈 조절하는거.. **/
function onPopupResize() {
	window.moveTo(100,100);
	window.resizeTo(document.body.scrollWidth + 10, document.body.scrollHeight + 69);
}

// 이미지 보기
function popupView (img_view) {
	if(img_view.length == 0) return;
	//var img_view = this;
	var x = x + 20 ;
	var y = y + 30 ;
	htmlz = "<html><head><style>body{margin:0;cursor:hand;}</style></head><body scroll=auto><img src='"+img_view+"' onload='width1=this.width;if(width1>800)width1=800;height1=this.height;if(height1>600)height1=600;top.window.resizeTo(width1+30,height1+40);' ondblclick='top.window.close();' title='Close Click.'></body></html>"
	imagez = window.open('', "image", "width="+ 100 +", height="+ 100 +", top=0,left=0,scrollbars=auto,resizable=1,toolbar=0,menubar=0,location=0,directories=0,status=0");
	imagez.document.open();
	imagez.document.write(htmlz)
	imagez.document.close();
}

// 이미지 보기
function popupImgView(img_view) {
	if(img_view.length == 0) return;
	//var img_view = this;
	var x = x + 30 ;
	var y = y + 50 ;

	htmlz = "<html><head><style>body{margin:0;cursor:pointer;}</style></head><body scroll=auto><img src='"+img_view+"' onload='width1=this.width; if (width1<100) {width1=100}; height1=this.height; if (height1<100) {height1=100}; top.window.resizeTo(width1+30,height1+50);' onclick='self.close();' title='Close Click.'></body></html>"
	imagez = window.open('', "image", "width="+ 100 +", height="+ 100 +", top=0,left=0,scrollbars=auto,resizable=1,toolbar=0,menubar=0,location=0,directories=0,status=0");
	imagez.document.open();
	imagez.document.write(htmlz)
	imagez.document.close();
}

// 이미지 사이즈 조절 ex) fnAddLoadEvent(fnBbodyInitResize);
function fnImgInitResize(idName) {
	var obj = document.getElementById(idName);
	if(!obj) return;

	var objImg = obj.getElementsByTagName("img");
	var oMaxWidth = obj.getAttribute("maxwidth");

	if(objImg.length > 0){
		for(var i=0; i<objImg.length; i++){
			if(objImg[i].width > oMaxWidth){
				objImg[i].height = parseInt(oMaxWidth / objImg[i].width * objImg[i].height);
				objImg[i].width = oMaxWidth;
			}

			objImg[i].style.cursor = "pointer";

			objImg[i].onclick = function(){
				popupView(this.src);
			}
		}
	}
}

/** ------------------------------------------------------------------------------------------------
 fnOnCheck(선택체크박스, 전체체크박스배열) - 전체체크박스의 배열을 선택체크박스에
 checked 값으로 모두 바꾼다.
 p.s 일괄 선택, 해제
 ------------------------------------------------------------------------------------------------**/
function fnOnCheck(pIntId, pStrId){
	var length = document.getElementsByName(pStrId).length;
	var flag = document.getElementById(pIntId).checked;
	for(i=0;i<length;i++){
		document.getElementsByName(pStrId)[i].checked = flag;
	}
}


/** ------------------------------------------------------------------------------------------------
자바스크립트 문자열 치환함수(replace(str, s, d))
  Function : 문자열 치환
  Return   :
  examples : var str = replace("2006-01-01", "-", "");
 ------------------------------------------------------------------------------------------------**/
function replace(str, s, d){
	var i=0;

	while(i > -1){
		i = str.indexOf(s);
		str = str.substr(0,i) + d + str.substr(i+1,str.length);
	}
	return str;
}
/** ------------------------------------------------------------------------------------------------
자바스크립트
  Function : 숫자만 입력
  Return   :
 ------------------------------------------------------------------------------------------------**/
function OnlyNumber(){
	var key = event.keyCode;
	if(!(key==144||(key>=48&&key<=57))){
		event.returnValue = false;
	}
}