

/* function : check null */
function isNull(tObj) {
	if (tObj == null || tObj == "undefined") return true;
	return false;
}


/*
 * function : define popup window
 * @param url(page url), wname(popup name), w(width), h(height)
 *        ptype(popup type), t(top position), l(left position)
 */
function popWin(url,wname,w,h,ptype,t,l){
	var popupWin;
	var popOpt = "width="+w+",height="+h+"";
	if (!isNull(t)) popOpt += ",top="+t;
	if (!isNull(l)) popOpt += ",left="+l;
	if(ptype == 0){
		popOpt += ",scrollbars=no";
	}
	if(ptype == 1){
		popOpt += ",scrollbars=yes";
	}
	if(ptype == 2){
		popOpt += ",scrollbars=yes,resizable=yes";
	}
	if(ptype == 3){
		popOpt += ",scrollbars=no,resizable=yes";
	}
	popupWin = window.open(url,wname,popOpt);
	popupWin.focus();
}


/*
 * function : define popup close
 */
function popClose() {
	try {
		if (window.opener != null && window.opener.closed) window.opener = self;
	} catch (e) {
		window.opener = self;
	}
	self.close();
}


/*
 * function : define mouseover image
 */
function imgOver(tObj) {
	chgImg(tObj, "o");
}


/*
 * function : define mouseout image
 */
function imgOut(tObj) {
	chgImg(tObj, "x");
}


/*
 * function : define change image
 */
function chgImg(tObj, mState) {
	if (tObj && tObj.src) {
		tObj.src = (mState == "o")?tObj.src.replace("x.gif","o.gif"):tObj.src.replace("o.gif","x.gif");
	}
}




/* change */
var curr_tab = new Array(0, 0);

function over_select(idx) {
	var tmpObj = document.getElementById(idx);
	if (is_null(tmpObj)) return;
	tmpObj.style.display = "";
}

function out_select(idx) {
	var tmpObj = document.getElementById(idx);
	if (is_null(tmpObj)) return;
	tmpObj.style.display = "none";
}


function chg_tab(tID, iState) {
	if (is_null(tID)) return;
	var mnuObj = document.getElementById(tID);
	mnuObj.src = mnuObj.src.substring(0,mnuObj.src.length - 5)+iState+".gif";
}

function sel_tab(tgid, idx) {
	var vDiv;
	if (curr_tab[tgid] == idx) return;
	chg_tab("tab"+tgid+curr_tab[tgid],"x");
	vDiv = document.getElementById("layer"+tgid+curr_tab[tgid]);
	chg_view(vDiv);
	curr_tab[tgid] = idx;
	chg_tab("tab"+tgid+idx,"o");
	vDiv = document.getElementById("layer"+tgid+idx);
	chg_view(vDiv);
}

function chg_view(tObj) {
	if (is_null(tObj)) return;
	tObj.style.display = (tObj.style.display == "none")?"":"none";
}

/* null check */
function is_null(sVal, cCode) {
	if (sVal == null || sVal == "undefined") return true;
	return (cCode == 1 && sVal == "")?true:false;
}

/*
==============================================================================
시작일자와 종료일자를 입력받아 두 일자사이의 차이를 일수로 리턴
입력 : 시작일자, 종료일자
결과 : 차이 일수
==============================================================================
*/
function getDaysDiff(fromD, toD)
{
    var fmat = "-";  //구분자

    fromD = isYYYYMMDD(fromD);
    toD = isYYYYMMDD(toD);

    if (fromD == "" || toD == "") return '0';
    if (toD < fromD) return '0';

    var s_dt = fromD.split(fmat);
    var e_dt = toD.split(fmat);

    s_dt[1] = (Number(s_dt[1]) - 1) + "";
    e_dt[1] = (Number(e_dt[1]) - 1) + "";

    var from_dt = new Date(s_dt[0], s_dt[1], s_dt[2]);
    var to_dt = new Date(e_dt[0], e_dt[1], e_dt[2]);

    return (to_dt.getTime() - from_dt.getTime()) / 1000 / 60 / 60 / 24 + 1;
}

/*
==================================================================================
년월일 8자리 날짜 valid Check
valid이면 YYYY-MM-DD의 형식으로 리턴
invalid이면 ""로 리턴
==================================================================================
*/
function isYYYYMMDD(sDate) {
    sDate = getRawDate(sDate);

    var sYear = "", sMonth = "", sDay = "";
    var iYear = 0, iMonth = 0, iDay = 0;

    if(sDate.length != 8) {
        return "";
    } else {

        sYear = sDate.substring(0,4);
        sMonth = sDate.substring(4,6);
        if(parseFloat(sMonth) < 10) sMonth = "0" + parseFloat(trim(sMonth));
        sDay = sDate.substring(6,8);
        if(parseFloat(sDay) < 10) sDay = "0" + parseFloat(trim(sDay));
    }

    if(isNaN(sYear) || isNaN(sMonth) || isNaN(sDay)) return "";

    iYear = parseInt(sYear,'10');
    iMonth = parseInt(sMonth,'10');
    iDay = parseInt(sDay,'10');

    if (iYear < 1) iYear = 0;
    if (iMonth < 1 || iMonth > 12)  iMonth = 0;
    if (iDay < 1) iDay = 0;

    if ( iMonth == 1 || iMonth == 3 || iMonth == 5 || iMonth == 7 || iMonth == 8 ||
         iMonth == 10 || iMonth == 12)  {
        if (iDay > 31) iDay = 0;
    } else if (iMonth == 4 || iMonth == 6 ||  iMonth == 9 || iMonth == 11) {
        if (iDay > 30) iDay = 0;
    } else if (iMonth == 2 )  {
        if (iYear % 4 != 0 || (iYear % 100 == 0 && iYear % 400 != 0)) {
            if (iDay > 28) iDay = 0;
        } else if (iDay > 29) iDay = 0;
    }

    if(iYear == 0 || iMonth == 0 || iDay == 0) return "";

    return sYear+"-"+sMonth+"-"+sDay;
}

/*
==================================================================================
날짜입력값에서 구분자인 '/', '.', '-',':' 등을 제거하여 리턴
==================================================================================
*/
function getRawDate(sDate) {
    if(sDate==null || sDate == "") return "";

    sDate = sDate.replace(/\//g,"");
    sDate = sDate.replace(/\-/g,"");
    sDate = sDate.replace(/\./g,"");
    sDate = sDate.replace(/\:/g,"");
    return sDate;
}

/*
==================================================================================
문자열내의 왼쪽, 오른쪽의 공백을 제거
==================================================================================
*/
function trim(str) {
    str = ltrim( str );
    str = rtrim( str );
    return str;
}
/*
==================================================================================
문자열내의 왼쪽 공백을 제거
==================================================================================
*/
function ltrim( str ) {
    var iLen = str.length;
    var idx = 0;
    for(idx=0 ; idx<iLen; idx++ ) {
        if( str.charAt(idx)!=' ' ) break
    }
    return str.substring( idx,iLen );
}
/*
==================================================================================
문자열내의 오른쪽 공백을 제거
==================================================================================
*/
function rtrim( str ) {
    var iLen = str.length;
    var idx = 0;
    for(idx=iLen-1 ; idx>=0; idx-- ) {
        if( str.charAt(idx)!=' ' ) break;
    }
    return str.substring( 0,idx+1 );
}

function otrim(str){
 temp = trim(str);
 return temp;
}


function checkbox_all(num) {
var frm=document.frm;
	 for(var i=0; i<frm.check.length; i++){
		if(frm.mk1.checked==true){
			frm.check[i].checked = true;
		}
		else{
			frm.check[i].checked = false;
		}
	}
}

//12-09 Raul 추가
function replaceAll( str, from, to ) {
   //str -> 문장
   //from -> 바꾸고자 하는 문자
   //to -> 치환될 문자
    var idx = str.indexOf( from );
  
    while ( idx > -1 ) {
     str = str.replace( from, to );
     idx = str.indexOf( from );
    }
    
    return str;
} 

function getFileSize(path)
{
	var img = new Image();
	try{
		img.dynsrc = path;
	}catch(e){}
	return img.fileSize;
 }


/**
 * 온 블러시 숫자만 입력 가능하도록 alert 노출 
 * @param field
 * @return
 */
function common_validate(field) {
	var valid = "0123456789"
	var ok = "yes";
	var temp;
	for (var i=0; i<field.value.length; i++) {
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
	}
	if (ok == "no") {
		alert("숫자만 입력할 수 있습니다");
		try{
			field.focus();
			field.select();
		}catch(e){
			
		}
	}
}

/**
 * 온블러시 2번째 인자(true면 영문+숫자 , false면 숫자만)에 따라 
 * 숫자만 ,혹은 영문자와 숫자만 입력하도록 체크
 * @param field
 * @param bool
 * @return
 */
function common_vali_Chk(field , bool) {
	var valid_num = "0123456789"
	var valid_eng = "abcdefghijklmnopqrstuvwxyz0123456789"
	var msg_num = "숫자만 입력할 수 있습니다.";
	var msg_eng = "영문자와 숫자만 입력할 수 있습니다.";
	
	var valid = "";
	var msg = "";
	
	if(bool){
		valid = valid_eng;
		msg = msg_eng;
	}else {
		valid = valid_num;
		msg = msg_num;
	}
	
	var ok = "yes";
	var temp;
	for (var i=0; i<field.value.length; i++) {
	temp = "" + field.value.substring(i, i+1);
	if (valid.indexOf(temp) == "-1") ok = "no";
	}
	if (ok == "no") {
	alert(msg);
	try{
		field.focus();
		field.select();
	}catch(e){
		
	}
	   }
}

/**
 *  이메일 셋팅 스크립트
 * @param obj
 * @return
 */
function setEmail(obj){
	var form = document.frm;
	var mailaddr = document.getElementById('mailaddr');
	
	if(obj.length ==0){
		mailaddr.value = "";
	}else{
		if(obj == 'direct'){
			mailaddr.style.display = '';
			mailaddr.value = '';
		}else{
			mailaddr.style.display = 'none';
			mailaddr.value = obj;
		}
	}
}


/**
 *  이미지 리사이징 스크립트
 * @param MaxWidth
 * @return
 */
function AutoResize(MaxWidth , div_id) {
  //document.images.display = "none";
  document.getElementById(div_id).style.display ="block";
  for(var i=0;i<document.images.length;i++) {
    if(document.images[i].width > MaxWidth) {
      document.images[i].width=MaxWidth;
      document.images[i].height-=document.images[i].height*(document.images[i].width-MaxWidth)/document.images[i].width;
    }
  }
}

/**
 * 에디터에서 제일 첫번째의 이미지 src 리턴 (썸네일 에서 사용하려고)
 * @param contents_value = 에디터 의 내용 값 .document.getElementById("ir1").value;
 * @return
 */
function getFirst_ImgURL( contents_value ){
	var start_index = contents_value.indexOf('<IMG');
	var end_index = contents_value.indexOf('nodeName="editor"');	//에디터 쓰는 페이지에서 넣어주어야함.
	var src_index = contents_value.indexOf('src=');
	
	var img_src = contents_value.substring((src_index + 4) , (end_index - 1) ).replace(/"/g , '');
	
	return img_src;

}

/**
 * IE에서 8 일경우 플래쉬 메뉴로 인해 웹로그에서
 * referrer 를 잡아내지 못해서
 * 이동할 페이지에 이전페이지의 URL 을 파라미터로 넘김.
 * 스크립트 선언하여 사용함.
 * (2009-09-22) 엄동현
 
 (2009-09-24) 네이게이션 문제로 현재 사용안함. 엄동현
 * @param param = 이동할 page 경로
 * @return
 */
function flash_url( param ){
	
	var before_page_url = document.location.href;
	
	before_page_url = before_page_url;
	
	var pos = param.indexOf('?');
	var foword_url;
	
	if( pos > 0 ){	//?가 있으면
		foword_url = param + "&before_page_url=" + before_page_url;
	} else {
		foword_url = param + "?before_page_url=" + before_page_url;
	}
	document.location.href = foword_url;
}


/**
 * 자바스크립트로 파라미터 받기
 * @param strParamName
 * @return
 */
function fnjavaRequest(strParamName)
{
 var fullUrl = location.href;

 var intStartNum = fullUrl.indexOf("?") + 1;
 var intEndNum = fullUrl.length;
 var arrParam = fullUrl.substr(intStartNum,intEndNum).split("&");

 for (i = 0 ; i < arrParam.length ; i++ )
 {
  if (arrParam[i].split("=")[0] == strParamName)
  {
   return arrParam[i].split("=")[1];
  }
 }
}

/**
문자열을 인코딩 할때 사용한다. 다음과 같이 디코딩 하여 사용한다.
JAVA : URLEncoder.decode(str, "UTF-8")
JS : decodeURL(str)
*/

function encodeURL(str){
	var s0, i, s, u;
	s0 = "";                // encoded str
	for (i = 0; i < str.length; i++){   // scan the source
	    s = str.charAt(i);
	    u = str.charCodeAt(i);          // get unicode of the char
	    if (s == " "){s0 += "+";}       // SP should be converted to "+"
	    else {
	        if ( u == 0x2a || u == 0x2d || u == 0x2e || u == 0x5f || ((u >= 0x30) && (u <= 0x39)) || ((u >= 0x41) && (u <= 0x5a)) || ((u >= 0x61) && (u <= 0x7a))){       // check for escape
	            s0 = s0 + s;            // don't escape
	        }
	        else {                  // escape
	            if ((u >= 0x0) && (u <= 0x7f)){     // single byte format
	                s = "0"+u.toString(16);
	                s0 += "%"+ s.substr(s.length-2);
	            }
	            else if (u > 0x1fffff){     // quaternary byte format (extended)
	                s0 += "%" + (0xf0 + ((u & 0x1c0000) >> 18)).toString(16);
	                s0 += "%" + (0x80 + ((u & 0x3f000) >> 12)).toString(16);
	                s0 += "%" + (0x80 + ((u & 0xfc0) >> 6)).toString(16);
	                s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
	            }
	            else if (u > 0x7ff){        // triple byte format
	                s0 += "%" + (0xe0 + ((u & 0xf000) >> 12)).toString(16);
	                s0 += "%" + (0x80 + ((u & 0xfc0) >> 6)).toString(16);
	                s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
	            }
	            else {                      // double byte format
	                s0 += "%" + (0xc0 + ((u & 0x7c0) >> 6)).toString(16);
	                s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
	            }
	        }
	    }
	} 
	
	
	
	
	return s0;


}


/** 문자열을 디코딩 할때 사용한다. 
 * 다음과 같이 인코딩 하여 사용한다.  
 * JAVA : URLEncoder.encode(str, "UTF-8") JS : encodeURL(str) 
*/
function decodeURL(str)
{
    var s0, i, j, s, ss, u, n, f;
    s0 = "";                // decoded str
    for (i = 0; i < str.length; i++){   // scan the source str
        s = str.charAt(i);
        if (s == "+"){s0 += " ";}       // "+" should be changed to SP
        else {
            if (s != "%"){s0 += s;}     // add an unescaped char
            else{               // escape sequence decoding
                u = 0;          // unicode of the character
                f = 1;          // escape flag, zero means end of this sequence
                while (true) {
                    ss = "";        // local str to parse as int
                        for (j = 0; j < 2; j++ ) {  // get two maximum hex characters for parse
                            sss = str.charAt(++i);
                            if (((sss >= "0") && (sss <= "9")) || ((sss >= "a") && (sss <= "f"))  || ((sss >= "A") && (sss <= "F"))) {
                                ss += sss;      // if hex, add the hex character
                            } else {--i; break;}    // not a hex char., exit the loop
                        }
                    n = parseInt(ss, 16);           // parse the hex str as byte
                    if (n <= 0x7f){u = n; f = 1;}   // single byte format
                    if ((n >= 0xc0) && (n <= 0xdf)){u = n & 0x1f; f = 2;}   // double byte format
                    if ((n >= 0xe0) && (n <= 0xef)){u = n & 0x0f; f = 3;}   // triple byte format
                    if ((n >= 0xf0) && (n <= 0xf7)){u = n & 0x07; f = 4;}   // quaternary byte format (extended)
                    if ((n >= 0x80) && (n <= 0xbf)){u = (u << 6) + (n & 0x3f); --f;}         // not a first, shift and add 6 lower bits
                    if (f <= 1){break;}         // end of the utf byte sequence
                    if (str.charAt(i + 1) == "%"){ i++ ;}                   // test for the next shift byte
                    else {break;}                   // abnormal, format error
                }
            s0 += String.fromCharCode(u);           // add the escaped character
            }
        }
    }
    return s0;



}


/**
 * 에디터 submit 시 특수문자 치환
 * 특수문자 치환해서 넘기자.
 * @param obj = contents 내용
 * @return 치환된 contents
 */
function Change_etcFor_Editor( obj ){
	 
	obj = replaceAll( obj , "&#8228;" , ",");	// , . 이런게아니고  &#8228; 인 이상한 점이다. 치환하자
	obj = replaceAll( obj , "&#8228" , ",");
	obj = replaceAll( obj , "·" , "&middot;");
	obj = replaceAll( obj , "–" , "-");
	obj = replaceAll( obj , "&#8211" , "-");
	obj = replaceAll( obj , "․" , "&middot;"); 	
	obj = replaceAll( obj , "☐" , "□"); 	
	obj = replaceAll( obj , "·" , "&middot;"); 	
	obj = replaceAll( obj , "©" , "&#169;"); 	
	
	return obj;
}

