String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/gi, "");
}

var Alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
var Num   = '0123456789';
var AlphaNum = Alpha+Num;
var ArmyNum = Num + '-';

function checkSelectBox(element, initIndex, msg, isSet) {
	var isSelect = false;

	for (var i = initIndex; i < element.options.length; i++) {
		if (element.options[i].selected == true) {
			isSelect = true;
			break;
		}
	}

	if (isSelect) {
		return true;
	} else {
		alert(msg + " 선택하여 주십시요");
		
		if (isSet == true) element.focus();
		return false;
	}
}

function checkSelectBoxById(objectName, initIndex, msg, isSet) {
	var obj = document.getElementById(objectName);
	var isSelect = false;

	for (var i = initIndex; i < obj.options.length; i++) {
		if (obj.options[i].selected == true) {
			isSelect = true;
			break;
		}
	}

	if (isSelect) {
		return true;
	} else  {
		alert(msg + " 선택하여 주십시요");
		if (isSet == 'true') obj.focus();
		return false;
	}
}

function checkEmail(objectName, limitByte, isSet) {
	var val = eval('document.'+objectName+'.value');

	if (!chkLength(objectName, '이메일', limitByte, isSet)) return false;
	if (val.length > 0) {
		var arrMatch = val.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) {
			alert('이메일주소가 올바르지 않습니다.\n다시 확인해주세요!');
			if (isSet == 'true') eval('document.'+objectName+'.focus();');
			return false;
		}
	}
	return true;
}

function checkChar(objectName, msg, isSet) {
	var val = eval('document.'+objectName+'.value');

	for (i=0; i<val.length; i++) {
		if (Alpha.indexOf(val.substring(i,i+1))<0) {
			alert(msg+'잘못된 문자가 있습니다\n특수문자는 입력할수 없습니다');
			if (isSet == 'true') eval('document.'+objectName+'.focus();');
			return false;
		}
	}
	return true;
}


function checkCharNum(objectName, msg, isSet) {
	var val = eval('document.'+objectName+'.value');

	for (i=0; i<val.length; i++) {
		if (AlphaNum.indexOf(val.substring(i,i+1))<0) {
			alert(msg+'잘못된 문자가 있습니다\n특수문자는 입력할수 없습니다');
			if (isSet == 'true') eval('document.'+objectName+'.focus();');
			return false;
		}
	}
	return true;
}


function checkNum(element, msg, isSet) {
	var val = element.value;
	for (i=0; i<val.length; i++) {
		if (Num.indexOf(val.substring(i,i+1))<0) {
			alert(msg+'잘못된 숫자가 있습니다\n숫자 이외 문자는 입력할수 없습니다');
			if (isSet == 'true') eval('document.'+objectName+'.focus();');
			return false;
		}
	}
	return true;
}


function checkKor(objectName, msg, isSet) {
	var val = eval('document.'+objectName+'.value');
	var len = val.length;
	var codeLen = 0;

	for (i=0; i<len; i++) {
		if (val.charCodeAt(i) > 255) {
			codeLen+=2
		} else {
			codeLen++;
		}
	}

	if (len != codeLen) {
		alert(msg + ' 한글은 입력할 수 없습니다');
		if (isSet == 'true') eval('document.'+objectName+'.focus();');
		return false;
	}
	return true;
}


function checkBlank(objectElement, msg, isSet) {
	var val = objectElement.value;
	if (val.indexOf(' ') >= 0) {
		alert(msg+ ' 공백이 올수 없습니다');
		if (isSet == 'true') eval('document.'+objectName+'.focus();');
		return false;
	}
	return true;
}

function checkLength(element, msg, limitByte, isSet) {
	var b = 0;
	var val = element.value;
	var len = element.value.length;

	for (i=0; i<len; i++) {
		if (val.charCodeAt(i) > 255) {
			b = b+2;
		} else {
			b = b+1;
		}
	}

	if (b > limitByte) {
		alert(msg+' 입력은 '+limitByte+'자 까지입니다');
		if (isSet == 'true') element.focus();
		return false;
	}
	return true;
}

function isSameLength(element, msg, limitByte, isSet) {
	var b = 0;
	var val = element.value;
	var len = element.value.length;

	for (i=0; i<len; i++) {
		if (val.charCodeAt(i) > 255) {
			b = b+2;
		} else {
			b = b+1;
		}
	}

	if (b != limitByte) {
		alert(msg+' 입력은 '+limitByte+'자까지 입력하셔야 합니다.');
		if (isSet == 'true') element.focus();
		return false;
	}
	return true;
}

function checkValue(element, msg, isSet) {
	var val = element.value;
	if (val.split(/\s/).join("") == "") {
		alert('Please insert ' + msg);
		if (isSet == true) {
			element.focus();
		}
		return false;
	}
	return true;
}


function checkBadContent(objectName, msg, badWord, isSet) {
	if (badWord == 'null' || badWord == '')
		return true;
	var val = eval('document.'+objectName+'.value');
	var bad = badWord.split(";");
	for(var i=0; i<bad.length; i++) {
		if (bad[i].trim().length > 0 && val.indexOf(bad[i]) > -1) {
			alert(msg+'에 '+bad[i]+'를 사용할 수 없습니다!');
			return false;
		}
	}
	return true;
}

/**
 * 비밀번호 설정 룰
 */
function checkERRule(objectName, str)
{
	var r=0;
	var re;
	//#Rule 10 - 영문자, 숫자, 특수문자
	re = /[^A-Za-z0-9`~!@#\$%\^&\*\(\)_\-\+\=\|\\\{\}\[\]\:";<>\?,\./]/;
	if (re.test(str)) {
		alert('숫자, 영문, \" `~!@#$%^&*()_-+=\|;:/?.><, \"이외의 문자가 입력되었습니다.');
		re = null;
		eval('document.'+objectName+'.focus();');
		return false;
	}

	//#Rule 02 - 6~20자이내.
	if ( (str).length<6 || (str).length>20 ) {
		alert("비밀번호는 6자이상 20자 이내로 입력하여 주십시오.");
		re = null;
		eval('document.'+objectName+'.focus();');
		return false;
	}

	//#Rule 03 - 영문, 숫자, 특수문자 혼용.
	re = /[a-z]/i;
	if(re.test(str))
		r++;
	re = /[0-9]/i;
	if(re.test(str))
		r++;
	re = /[`~!@#\$%\^&\*\(\)_\-\+\=\|\\\{\}\[\]\:\";'<>\?,\./]/i;
	if(re.test(str))
		r++;

	if(r<2) {
		alert(" 숫자, 영문, \"`~!@#$%^&*()_-+=\|'\";:/?.><,\" \n 세가지 문자그룹중 두개 이상의 그룹을 사용한 조합이어야 합니다.\n 예)asd2bb,013!45,#$%#$1");
		re = null;
		eval('document.'+objectName+'.focus();');
		return false;
	};

	//#Rule 04 - 동일문자 3회이상 연속.
	re = /(.)\1{2,}/i;
	if (re.test(str)) {
		alert("동일한 문자가 3회이상 연속되었습니다.");
		re = null;
		eval('document.'+objectName+'.focus();');
		return false;
	};

	//#Rule 05 - 순차적인 구성 검사
	re = /[\w]/;
	for(var i=0;i < (str).length-2 ; i++) {
		r=str.charCodeAt(i);
		if ( re.test(str.charAt(i)) ) {
			if (r+1==str.charCodeAt(i+1)||r-1==str.charCodeAt(i+1)) {
				r = str.charCodeAt(i+1);
				if (r+1==str.charCodeAt(i+2)||r-1==str.charCodeAt(i+2)) {
					alert("3자이상의 문자 나열이 순차적입니다. \n 예)abc, 123, 321, cba");
					re = null;
					eval('document.'+objectName+'.focus();');
					return false;
				}
			}
		}
	}
	re = null;
	return true;
}

function popup(URL,TG,W,H){
	var L 	= (screen.width - W) / 2; 
	var T 	= (screen.height - H) / 2; 
	var OPT	= " width="+W+" height="+H+" top="+T+" left="+L+" scrollbars=yes resizable=yes";
	popupWin= window.open(URL,TG,OPT );
	
	popupWin.focus();	
}


/**
 * 팝업창을 띄운다.
 */
function openWindow(url, name, left, top, width, height, toolbar, menubar, statusbar, scrollbar, resizable)
{
	toolbar_str = toolbar ? 'yes' : 'no';
	menubar_str = menubar ? 'yes' : 'no';
	statusbar_str = statusbar ? 'yes' : 'no';
	scrollbar_str = scrollbar ? 'yes' : 'no';
	resizable_str = resizable ? 'yes' : 'no';
	window.open(url, name, 'left='+left+',top='+top+',width='+width+',height='+height+',toolbar='+toolbar_str+
				',menubar='+menubar_str+',status='+statusbar_str+',scrollbars='+scrollbar_str+',resizable='+resizable_str);
}


/**
 * 팝업창을 가운데로 띄운다.
 */
function openWindowCenter(url, name, width, height, toolbar, menubar, statusbar, scrollbar, resizable)
{
	var x,y;
	//화면 한가운데로 팝업창 띄우기 위한 좌표 계산
	x=Math.floor( (screen.availWidth-(width+12))/2 );
	y=Math.floor( (screen.availHeight-(height+30))/2 );

	toolbar_str = toolbar ? 'yes' : 'no';
	menubar_str = menubar ? 'yes' : 'no';
	statusbar_str = statusbar ? 'yes' : 'no';
	scrollbar_str = scrollbar ? 'yes' : 'no';
	resizable_str = resizable ? 'yes' : 'no';
	window.open(url, name, 'left='+x+',top='+y+',width='+width+',height='+height+',toolbar='+toolbar_str+
				',menubar='+menubar_str+',status='+statusbar_str+',scrollbars='+scrollbar_str+',resizable='+resizable_str);
}


/**
 * 이미지 팝업창을 가운데로 띄운다.
 * 버그: 윈도우 리사이즈가 가끔 안된다.
 */
function openImageWinCenter(imageRef)
{
	var x,y,w,h,loadingMsg,scroll, maxWidth,maxHeight;
	var imgObj = new Image();
	w=300;h=100; // 초기 팝업창
	x=Math.floor( (screen.availWidth-(w+12))/2 );
	y=Math.floor( (screen.availHeight-(h+30))/2 );
	scroll='no';
	maxWidth = 600;
	maxHeight = 500;
	imgObj.src = imageRef;

	// 창 크기가 커지면 강제로 줄인다. (버그)
	//if(imgObj.width>maxWidth)  scroll='yes';
	//if(imgObj.height>maxHeight) scroll='yes';
	scroll='yes';

	loadingMsg="<table width=100% height=100%><tr><td valign=center align=center><font size='2' color='#ff6600' face='verdana'>NOW LOADING...</font></td></tr></table>";

	with( window.open("","",'height='+h+',width='+w+',top='+y+',left='+x+',scrollbars='+scroll+',resizable='+scroll) ) {
		document.write(
		"<body topmargin=0 rightmargin=0 bottommargin=0 leftmargin=0>",
		loadingMsg,
		"<center><img  src=\""+imageRef+"\" hspace=0 vspace=0 border=0 onClick=\"window.close();\" "+
		"onload=\"document.title=this.src;"+
		"document.body.removeChild(document.body.children[0]);"+

		// 윈도우 리사이즈
		//"window.resizeTo("+(imgObj.width>600?600:imgObj.width+12)+","+(imgObj.height>500?500:imgObj.height+30)+");"+
		"window.resizeTo(600, 500);"+


		// 윈도우를 화면 가운데로 이동
		"window.moveTo(Math.floor( (screen.availWidth -(this.width>"+maxWidth+"?"+maxWidth+":this.width+12))/2), "+
		"              Math.floor( (screen.availHeight-(this.height>"+maxHeight+"?"+maxHeight+":this.height+30))/2 ));\">"+
		"</center><br>",
		"</body>");
	focus();
	}
}


/**
 * 숫자만 입력가능하게 만든다.
 */
function onlyNum() {
	if (event.keyCode == 16) {
		alert('shift 키는 사용이 불가능합니다.');
		event.returnValue = false;
	}

	if ((event.keyCode == 46) ||  // DEL
		(event.keyCode == 8)  ||  // backspace
		(event.keyCode == 9)  ||  // tab
		(event.keyCode == 37) ||  // ← key
		(event.keyCode == 38) ||  // ↑ key
		(event.keyCode == 39) ||  // → key
		(event.keyCode == 40) ||  // ↓ key
		(event.keyCode == 35) ||  // HOME key
		(event.keyCode == 36) ||  // END key
		(event.keyCode == 13) ||  // Enter key
		((event.keyCode >= 48) && (event.keyCode <= 57 )) || // 0 ~ 9
		((event.keyCode >= 96) && (event.keyCode <= 105 ))||   // 0 ~ 9 in 숫자패드
		!isNaN(String.fromCharCode(event.keyCode)) // 숫자라면
		)
	{
		event.returnValue=true;
	} else {
		event.returnValue=false;
	}
}


/**
 * <textarea> 편집창의 크기를 조정한다.
 */
function ResizeTextarea(objectName, maxSize, minSize, iSize)
{
	var val = eval('document.'+objectName);

	if (iSize == 1) {
		if (val.rows <= maxSize) {
			val.rows += 1;
		} else {
			window.alert("더이상 크게 할 수 없습니다!");
		}
	} else {
		if (val.rows >= minSize) {
			val.rows -= 1;
		} else {
			window.alert("더이상 작게 할 수 없습니다!");
		}
	}
}


/**
 * 특정문자를 체크한다.
 */
function checkSpecialChar(str, special_char)
{
	var flag = true;

	for(i=0; i<=str.length; i++) {
		ch = str.charAt(i);
		for(j=0; j<special_char.length; j++) {
			if (ch == special_char.charAt(j)) {
				flag = false;
				break;
			}
		}
		if(flag == false)
		break;
	}
	return flag;
}

function viewProperties(obj, isAllow)
{
	if(isAllow == null)
		isAllow = false;
	var str = "";
	for(i in obj) {
		if(isAllow || (obj[i] !=null && obj[i] != false))
			str += "i : " + i + "   obj[i] : " + obj[i] + "\n";
	}
	alert(str);
}

function formReset(formName) {
	var form1 = eval('document.'+formName);
	form1.reset();
}

function closeWindow() {
	try {
		self.close();
	} catch(ex) {}
}

// 바이트 체크 하는 루틴 charCode가 128이 넘으면 한글? 그래서 2byte
String.prototype.bytes = function(str) {
	str = (this != window) ? this : str;
	var len = 0;
	for(j = 0 ; j < str.length ; j++) {
		var chr = str.charCodeAt(j);
		len += (chr > 128) ? 2 : 1;
	}
	return len;
}

function checkBytesAndDelete(aro_name, ari_max) {
	try {
		var ls_str = aro_name.value;		// 이벤트가 일어난 컨트롤의 value값
		var li_str_len = ls_str.length; 		// 전체길이
		// 변수 초기화
		var li_max = ari_max; 			// 제한할 글자수 크기
		var i = 0;						// for문에 사용
		var li_byte = 0;				// 한글일 경우는 2 그밖에는 1을 더함
		var li_len = 0;					// substring하기 위해서 사용
		var ls_one_char = "";			// 한글자씩 검사한다.
		var ls_str2 = "";				// 글자수를 초과하면 제한할 수 글자전까지만 보여준다.

		for ( i=0; i < li_str_len; i++) {
			//한글자 추출
			ls_one_char = ls_str.charAt(i);
			if (escape(ls_one_char).length > 4) {
				li_byte += 2;
			} else {
				li_byte++;
			}

			// 전체 크기가 li_max를 넘지 않으면
			if (li_byte <= li_max) {
				li_len = i + 1;
			}
		}

		// 전체길이를 초과하면
		if (li_byte > li_max)
		{
			alert("한글 " + (li_max/2) + " 글자를 초과 입력할 수 없습니다. \n 초과된 내용은 자동으로 삭제 됩니다.");
			ls_str2 = ls_str.substr(0, li_len);
			aro_name.value = ls_str2;
		}
		aro_name.focus();
	} catch(ex) { }
}

// 숫자 이외의 입력시 삭제까지 해줌 ( 2008-05-08 4:37오후 수정해야함)
function checkNumAndDelete(objectName, msg, isSet)
{
	var elObject = eval('document.'+objectName);
	var val = eval('document.'+objectName+'.value');
	for (i=0; i<val.length; i++) {
		if(Num.indexOf(val.substring(i,i+1))<0) {
			alert(msg+'잘못된 숫자가 있습니다\n숫자 이외 문자는 입력할수 없습니다');
			if (isSet == 'true') eval('document.'+objectName+'.focus();');

			var val = val.substr(0, val.length - 1);
			elObject.value = val;
			return false;
		}
	}
	return true;
}


function getRadioValue(radioElement) {
	var returnValue = 0;
	for (var i=0; i < radioElement.length; i++) {
		if (radioElement[i].checked == true) {
			returnValue = radioElement[i].value;
		}
	}
	return returnValue;
}

function checkRadio(radioElement, msg) {
	var returnValue = getRadioValue(radioElement);
	
	if (returnValue == 0) {
		alert(msg+' 선택해 주세요');
		return false;
	}
	return true;
}

function reloadPage() {
	window.location.reload(true);
}

function validateSearch(elementForm) {
	if (checkBlank(elementForm.field, '검색어에 ', 'true')) {
		return true;
	} else {
		return false;
	}
}

function getCookie( name )
{http://www.youtube.com/v/YHR_Zhzavk0
	var nameOfCookie = name + "=";
	var x = 0;
	while ( x <= document.cookie.length )
	{
		var y = (x+nameOfCookie.length);
		if ( document.cookie.substring( x, y ) == nameOfCookie )
		{
			if ( (endOfCookie=document.cookie.indexOf( ";", y )) == -1 )
				endOfCookie = document.cookie.length;
			return unescape( document.cookie.substring( y, endOfCookie ) );
		}
		x = document.cookie.indexOf( " ", x ) + 1;
		if ( x == 0 )
			break;
	}
	return "";
}



// 한식세계화 상단 메뉴에 Contact Us 링크
function openContactUs() {
    openWindowCenter('/en/contactUs.do', 'qna', '600', '520');
}	

function viewMovie(url) {
	window.open("/viewMovie.do?mainArticle="+url,"viewMovie","width=580, height=400");
}

function viewPrint(url) {
	window.open(url, "printWin", "scrollbars=yes,width=740,height=400");
}

function isID(uid) {
    var reg = /[a-z]*[0-9]*/g
    var _reg = /[0-9]*[a-z]*/g


    if (reg.test(uid)||_reg.test(uid)){
        return true;
    } else {
        return false;
    }

    if (uid.length < 4 || uid.length > 12) {
        return false;
    }
    
    for(var i = 0; i < uid.length; i++) {
        //var chr = uid.substr(i,1);
        var chr = uid.charAt(i);
        if((chr < '0' || chr > '9') && (chr < 'a' || chr > 'z')) {
            return false;
        }
    }

    return true;
}

function roll_over(img_name, img_src)
{
	document.getElementById(img_name).src = img_src;
}

