//==============================================================================================================
//	Get Clinet Info
//==============================================================================================================
var clientBwName = getBroswerName();
function getBroswerName() {
    var agt=navigator.userAgent.toLowerCase();
    if (agt.indexOf("opera") != -1)			return 'Opera';
    if (agt.indexOf("staroffice") != -1)	return 'Star Office';
    if (agt.indexOf("webtv") != -1)			return 'WebTV';
    if (agt.indexOf("beonex") != -1)		return 'Beonex';
    if (agt.indexOf("chimera") != -1)		return 'Chimera';
    if (agt.indexOf("netpositive") != -1)	return 'NetPositive';
    if (agt.indexOf("phoenix") != -1)		return 'Phoenix';
    if (agt.indexOf("firefox") != -1)		return 'Firefox';
    if (agt.indexOf("chrome") != -1)		return 'Chrome';
    if (agt.indexOf("safari") != -1)		return 'Safari';
    if (agt.indexOf("skipstone") != -1)		return 'SkipStone';
    if (agt.indexOf("msie") != -1)			return 'Internet Explorer';
    if (agt.indexOf("netscape") != -1)		return 'Netscape';
    if (agt.indexOf("mozilla/5.0") != -1)	return 'Mozilla';
    if (agt.indexOf('\/') != -1) {
		if (agt.substr(0,agt.indexOf('\/')) != 'mozilla') {
			return navigator.userAgent.substr(0,agt.indexOf('\/'));
		} else return 'Netscape';
	} else if (agt.indexOf(' ') != -1) return navigator.userAgent.substr(0,agt.indexOf(' '));
    else return navigator.userAgent;
}

//==============================================================================================================
//	Get Height and Width
//==============================================================================================================
function getBrowserHeight() {	
	var myHeight = 0; 
	if ( typeof( window.innerWidth ) == 'number' ) { 
		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 myHeight;
}

function getBrowserWidth() {	
	var myWidth = 0; 
	if ( typeof( window.innerWidth ) == 'number' ) { 
		myWidth = window.innerWidth; 
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientWidth ) ) { 
		//IE 6+ in 'standards compliant mode' myWidth = document.documentElement.clientWidth; 
		myWidth = document.documentElement.clientWidth; 
	} else if( document.body && ( document.body.clientWidth || document.body.clientWidth ) ) { 
		//IE 4 compatible myWidth = document.body.clientWidth; 
		myWidth = document.body.clientWidth; 
	}
	return myWidth;
}

function getElementHeight(id) {	
	if ( document.getElementById(id) ) {
		return document.getElementById(id).clientHeight;
	} else {
		return -1;
	}
}
		
function getElementWidth(id) {
	if ( document.getElementById(id) ) {
		return document.getElementById(id).clientWidth;
	} else {
		return -1;
	}
}
		
//==============================================================================================================
//	Popup Window Open Function
//==============================================================================================================
function winCenterOpen(strUrl, strWinName, intWidth, intHeight, strOption){
    if (screen.width <= 800){
        intTop=0;
        intLeft=0;
    } 
    else {
        intTop = (screen.height)?(screen.height-intHeight)/2:100;
        intLeft = (screen.width)?(screen.width-intWidth)/2:100;
    }
    if(strOption != null && strOption.length > 0) strOption = "," + strOption;
    window.open(strUrl, strWinName, "top="+intTop+", left="+intLeft+", width="+intWidth+", height="+intHeight + strOption);
}

function modalWinCenterOpen (strUrl, strWinName, intWidth, intHeight, strOption){
	if (screen.width <= 800){
        intTop=0;
        intLeft=0;
    } 
    else {
        intTop = (screen.height)?(screen.height-intHeight)/2:100;
        intLeft = (screen.width)?(screen.width-intWidth)/2:100;
    }
    if(strOption != null && strOption.length > 0) strOption = "," + strOption;
	var returnVal = window.showModalDialog(strUrl, strWinName, "dialogTop="+intTop+"px;dialogLeft="+intLeft+"px;dialogWidth="+intWidth+"px;dialogHeight="+intHeight+"px;"+strOption);
	return returnVal;
}

function scrollWinCenterOpen(strUrl, strWinName, intWidth, intHeight, strOption){
    if (screen.width <= 800){
        intTop=0;
        intLeft=0;
    } 
    else {
        intTop = (screen.height)?(screen.height-intHeight)/2:100;
        intLeft = (screen.width)?(screen.width-intWidth)/2:100;
    }
    if(strOption != null && strOption.length > 0) strOption = "," + strOption;
    window.open(strUrl, strWinName, "top="+intTop+", left="+intLeft+", width="+intWidth+", height="+intHeight + strOption+",scrollbars=yes");
}

//==============================================================================================================
// 검색어 반전
//==============================================================================================================
function reversedWord( srchKeyword ) {
	wordFind={
		x:function(){
			if (navigator.appName.indexOf("Microsoft")!=-1) {
			// EXPLORER-SPECIFIC CODE
				var TRange, keyword;
				var srchWordArray = new Array();
				if( srchKeyword.indexOf("·") == -1 && srchKeyword.indexOf("ㆍ") == -1){
					srchWordArray[0] = srchKeyword;
				}else{
					srchWordArray[0] = srchKeyword.split("ㆍ").join("·");
					srchWordArray[1] = srchKeyword.split("·").join("ㆍ");
				}

				for (var i = 0; i < srchWordArray.length ; i++) {
					keyword = srchWordArray[i];
					if(keyword) {
						TRange = document.body.createTextRange();
						for(var j = 0; TRange.findText(keyword); j++) {
							TRange.execCommand("ForeColor","false","#FF8D11");
							TRange.collapse(false);
						}
		        	}
		        }
	        } else {
			// NETSCAPE CODE - INCOMPLETION
				if(window.find(tempword))
					while(window.find(tempword, false, true)) n++;
				else n++;
			}
        }
	}
	wordFind.x();
}

//==============================================================================================================
//	TextBox의 입력값에 대한 null 및 공백문자 체크
//==============================================================================================================
function checkText(val){
	var isok = false;
	if(val != null){
		var len = val.length;
		for(var i=0;i<len;i++){
			var ch = val.charAt(i);
			if(ch != ' '){
				isok = true;
				break;
			}
		}
	}
	return isok;
}

//==============================================================================================================
//	날짜여부 체크
//==============================================================================================================
function isValidDate(str){
	if ( gfIsDate(str) == true ) {
		return true;
	} else {
		return false;
	}
}

//==============================================================================================================
//	문자 길이 체크
//==============================================================================================================
function checkLength(temp,length){	
	if ( temp.length != length ) {
		return false;
	} else {
		return true;
	}
}

//==============================================================================================================
//	력 문자열의 좌측을 trim하여 리턴한다.
//==============================================================================================================
function trimLeft(str){
	var s = new String(str);
	if (s.substr(0,1) == " ")
		return trimLeft(s.substr(1));
	else
		return s;
}

//==============================================================================================================
//	입력 문자열의 우측을 trim하여 리턴한다.
//==============================================================================================================
function trimRight(str){
	var s = new String(str);
	if(s.substr(s.length-1,1) == " ")
		return trimRight(s.substring(0, s.length-1))
	else
		return s;
}

//==============================================================================================================
//	입력 문자열의 좌,우측을 trim하여 리턴한다.
//==============================================================================================================
function trim(str){
	return trimLeft(trimRight(str));
}

//==============================================================================================================
//	E-mail 주소의 유효성 체크
//==============================================================================================================
function isValidEmail(email) {
	var arrMatch = email.match(/^(\".*\"|[A-Za-z0-9_-]([A-Za-z0-9_-]|[\+\.])*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z0-9][A-Za-z0-9_-]*(\.[A-Za-z0-9][A-Za-z0-9_-]*)+)$/);
	if (arrMatch == null) {
		return false;
	}

	var arrIP = arrMatch[2].match(/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/);
	if (arrIP != null) {
		for (var i = 1; i <= 4; i++) {
			if (arrIP[i] > 255) {
				return false;
			}
		}
	}
	return true;
}

function chkPattern(str,type)	//형식 체크
{
  switch(type)
  {
     case "EMAIL": //메일
    //pattern = /^[._a-zA-Z0-9-]+@[._a-zA-Z0-9-]+\.[a-zA-Z]+$/;
    	pattern = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z]{2,4}$/;
        break;
         
    default :
      return false;
  }
	return pattern.test(str);
}

//==============================================================================================================
//	주민 번호 체크
//	파라미터 : 13자리 주민 번호 또는 14자리 ('-') 포함한 주민번호
//==============================================================================================================
function juminCheck( juminStr ){
	var idnm1,idnm2,a,b,c,d,e,f,g,h,i,j,k,l,sum,pivot,modulus,endnumber;
	if (juminStr.indexOf("-") != -1) {
		if (juminStr.indexOf("-") != 6) {
			return false;
		}
	}
	var juminStr = juminStr.replace( "-","");
	if (juminStr.length != 13) {
		return false;
	}
	idnm1 = juminStr.substring(0,6);
	idnm2 = juminStr.substring(6,13);
	if( idnm1.length != 6 && idnm2.length != 7 ){
		return false;
	}

	a = idnm1.substring(0, 1)
	aa = a * 2;
	b = idnm1.substring(1, 2)
	bb = b * 3;
	c = idnm1.substring(2, 3)
	cc = c * 4;
	d = idnm1.substring(3, 4)
	dd = d * 5;
	e = idnm1.substring(4, 5)
	ee = e * 6;
	f = idnm1.substring(5, 6)
	ff = f * 7;
	g = idnm2.substring(0, 1)
	gg = g * 8;
	h = idnm2.substring(1, 2)
	hh = h * 9;
	i = idnm2.substring(2, 3)
	ii = i * 2;
	j = idnm2.substring(3, 4)
	jj = j * 3;
	k = idnm2.substring(4, 5)
	kk = k * 4;
	l = idnm2.substring(5, 6)
	ll = l * 5;

	pivot = idnm2.substring(6,7)
	sum = aa + bb + cc + dd + ee + ff + gg + hh + ii + jj + kk + ll;
	modulus = sum % 11
	endnumber = 11 - modulus

	if(endnumber == 11) {
	   endnumber = 1;
	} else if (endnumber == 10) {
	   endnumber = 0;
	} else {
	   endnumber = endnumber;
	}

	 if (pivot != endnumber) {
		 return false;
	 }
	 return true;
}

//==============================================================================================================
//	전화번호 체크
//	파라미터 : 전화번호
//==============================================================================================================
function checkPhoneNumber( str ){
	var numPattern = /0\d+\-\d\d+\-\d\d\d\d/g;
	numPattern = str.match(numPattern);
	if(numPattern == null){
		numPattern = /([^0-9])/;
		numPattern = str.match(numPattern);
		if(numPattern == null && str.length >= 4 ){
			return true;
		}
		var numP2 = /\d+/g;
		if( str.match( numP2 ) != str || str.length < 5 ){
			if( str == null || str == "" ) return true;
			return false;
		}
	}
	return true;
}

//==============================================================================================================
//	특수문자 체크
//==============================================================================================================
function checkSChar(str) { 
	var strlen = str.length;
	for ( var i = 0 ; i < strlen ; i++ ) {
		ch = str.charAt(i);
		if ( ch == "[" || ch == "]" || ch == "(" || ch == ")" || ch == "<"
			 || ch == ">" || ch == "/" || ch == "'" || ch == '"' ) {
			return false;
		}
	}
	return true;
}

//==============================================================================================================
//	날짜 문자열 체크
//	파라미터 : YYYY.MM.DD or YYYY-MM-DD or YYYY/MM/DD
//==============================================================================================================
function gfIsDate(str){		
	 var src = str.replace(/-/g, "").replace(/\./g, "");
	 if(gfIsNumber(src) == false)
	  return false;
	 if(src.length==0)
	  return false;
	 if(src.length!=8)
	  return false;
	
	 yea = src.substring(0, 4); // year
	 mon = src.substring(4, 6); // month
	 da  = src.substring(6, 8); // day
	
	 //기본적인 일, 월, 년 에러 검색
	 if(mon < 1 || mon > 12)
	  return false;
	 if(da  < 1 || da  > 31)
	  return false;
	
	 d = new Date(yea, mon - 1, da);
	
	 if(yea<'2000')
	  yea = src.substring(2, 4);
	
	 if(yea!=d.getYear() || mon!=(d.getMonth()+1) || da!=d.getDate())
	  return false;
	
	 return true;
}

//==============================================================================================================
//	숫자 문자열 체크
//==============================================================================================================
function gfIsNumber(str) {
	var src = new String(str);
	var tar = true;
	var i;
	var len = src.length;

	for (i=0; i < len; i++) {
		if((src.charAt(i) < '0') | (src.charAt(i) > '9')){
			if(i==0) {
				if(src.charAt(i) != '-') return false;
			} else {
				if(src.charAt(i) != '.') return false;
			}
		}
	}
	return true;
}

//==============================================================================================================
//	한글 체크
//==============================================================================================================
function isHangul (str) {
    for ( var i = 0 ; i < str.length ; i++ ) {
		var code = str.charCodeAt(i)
        var ch = str.substr(i,1).toUpperCase()
        code = parseInt(code)

        if ( (ch<"0"||ch>"9") && (ch<"A"||ch>"Z") && ((code>255)||(code<0))) {
			return true;
		}
	}
    return isHangul2(str);
}

// 한글인지 아닌지 구별
function isHangul2(str) {
     var len = str.length;
     for (var i = 0; i < len; i++)  {
         if (str.charCodeAt(i) != 32 && (str.charCodeAt(i) < 44032 || str.charCodeAt(i) > 55203))
             return false;
     }
     return true;
}

//==============================================================================================================
//	체크박스 모드 체크
//==============================================================================================================
function checkCheckBox(chkAll,chkOne) {
	var isChecked = chkAll.checked;
	var inputObj = document.getElementsByName(chkOne);
	if ( inputObj != null ) {
		for ( var i = 0 ; i < inputObj.length ; i++ ) {
			var innerObj = inputObj[i];
			if ( innerObj.name == chkOne ) {				
				innerObj.checked = isChecked;
			}			
		}
	}
}
 
//==============================================================================================================
//	체크박스 갯수 카운드

//==============================================================================================================
function returnCheckBoxCount(chkOne) {
	var checkCount = 0;
	var inputObj = document.getElementsByName(chkOne);	
	if ( inputObj != null ) {
		checkCount = inputObj.length;
	}
	return checkCount;
}

//==============================================================================================================
//	선택된 체크박스 수 체크

//==============================================================================================================
function returnCheckBoxCheckCount(chkOne) {
	var checkCount = 0;
	var inputObj = document.getElementsByName(chkOne);
	if ( inputObj != null ) {
		for ( var i = 0 ; i < inputObj.length ; i++ ) {
			var innerObj = inputObj[i];
			if ( innerObj.checked ) {				
				checkCount++;
			}			
		}
	}
	return checkCount;
}

//==============================================================================================================
//	Popup Window Close
//==============================================================================================================
function closeWin() {
	window.close();
}


/*******************************************************
 * Ov 이미지 관련 스크립트
 *******************************************************/
function resizeImage(element,level){
	var elementWidth;
	try { 
		elementWidth = ovDivWidth;
	} catch(e) {
		elementWidth = getElementWidth("ovDiv");
		if ( elementWidth == 0 ) elementWidth = getBrowserWidth();
    }
	
	var maxWidth = elementWidth - (level*18);
	var width = element.width;
	var height = element.height;
	
	if ( width > maxWidth ) {  // 이미지가 maxWidth다 크다면 너비를 maxWidth으로 맞우고 비율에 맞춰 세로값을 변경한다.      
		height = height/(width / maxWidth);
		element.width = maxWidth;
		element.height = height;
	}
}

function resizeImageForEdit(element,level){
	var elementWidth;
	try { 
		elementWidth = ovDivWidth;
	} catch(e) {
		elementWidth = getElementWidth("ovDiv");
		if ( elementWidth == 0 ) elementWidth = getBrowserWidth();
    }

	var maxWidth = getElementWidth("ovDiv")-(level*18)-50;
	var width = element.width;
	var height = element.height;
	
	if ( width > maxWidth ) {  // 이미지가 maxWidth다 크다면 너비를 maxWidth으로 맞우고 비율에 맞춰 세로값을 변경한다.      
		height = height/(width / maxWidth);
		element.width = maxWidth;
		element.height = height;
	}
}
