// 포커스 없애기 ***************************************************************
function autoBlur()
{
	if(event.srcElement.tagName=="A"||event.srcElement.tagName=="IMG")
	document.body.focus();
}

document.onfocusin=autoBlur;

// 숫자만 입력 (INPUT) *********************************************************
function num_only()
{
	if((event.keyCode < 48) || (event.keyCode > 57))
	{
		event.returnValue=false;
	}
}


// 주민등록번호 체크 ***********************************************************
function numbercheck(theform)
{
	var chk	= 0;
	var yy	= theform.id_no1.value.substring(0,2);
	var mm	= theform.id_no1.value.substring(2,4);
	var dd	= theform.id_no1.value.substring(4,6);
	var sex	= theform.id_no2.value.substring(0,1);

	// 주민등록번호 체크
	for (var i = 0; i <= 5 ; i++)
	{
		chk = chk + ((i % 8 + 2) * parseInt(theform.id_no1.value.substring(i, i + 1)));
	}
	for (var i = 6; i <= 11 ; i++)
	{
		chk = chk + ((i % 8 + 2) * parseInt(theform.id_no2.value.substring(i - 6, i - 5)));
	}
	chk = 11 - (chk % 11);
	chk = chk % 10;

	if (chk != theform.id_no2.value.substring(6, 7))
	{
		alert ("유효하지 않은 주민등록번호입니다.");
		theform.id_no1.value = "";
		theform.id_no1.focus();
		return false;
	}

	else if ((theform.id_no1.value.length != 6) || (yy < 25 || mm < 1 || mm >12 || dd < 1))
	{
		alert ("주민등록번호가 잘못되었습니다.");
		theform.id_no1.value = "";
		theform.id_no1.focus();
		return false;
	}

	else if ((sex != 1 && sex !=2 )||(theform.id_no2.value.length != 7 ))
	{
		alert ("주민등록번호가 잘못되었습니다.");
		theform.id_no2.value = "";
		theform.id_no2.focus();
		return false;
	}

	else
		return true;
}


// 이메일 형식 체크 ************************************************************
function emailcheck(value, lang_cd)
{
	if(checkSpace(value))
	{
		if (lang_cd == "kr")
			alert("이메일 주소를 빈공간 없이 넣으세요.");
		else
			alert("Please Enter Correct Email Address.");
		return false;
	}
	/* checkFormat */
	var isEmail = /[-!#$%&'*+\/^_~{}|0-9a-zA-Z]+(\.[-!#$%&'*+\/^_~{}|0-9a-zA-Z]+)*@[-!#$%&'*+\/^_~{}|0-9a-zA-Z]+(\.[-!#$%&'*+\/^_~{}|0-9a-zA-Z]+)*/;
	if( !isEmail.test(value))
	{
		if (lang_cd == "kr")
			alert("이메일 형식이 잘못 되었습니다.");
		else
			alert("Please Enter Correct Email Address.");
		return false;
	}
/*
	if( value.lastIndexOf("daum.net") >= 0 || value.lastIndexOf("hanmail.net") >= 0 )
	{
		alert("다음 메일 계정은 사용하실 수 없습니다.");
		return false;
	}
*/
     return true;
}


// 빈 공간 체크 ****************************************************************
function checkSpace(str)
{
     if(str.search(/\s/) != -1)
		return true;
     else
		return false;
}


// 숫자포맷 (,) 넣기 ***********************************************************
function number_format(number)
{
	unit		= 3;
	i			= 0;
	rst			= '';
	number		= number + '';
	len			= number.length;
	arr			= new Array(len);

	while(i <len){
		arr[i]	= number.charAt(len-i-1);
		rst		= arr[i] + rst;
		i++;
		if(i % unit == 0 && i != len)
			rst	= ',' + rst;
	}
	return rst;
}


// 팝업창 띄우기 ***************************************************************
function popup_win(url, target, width, height, left, top, scrollbars, resizable)
{
	if (left == "")
		left	= 0;
	if (top == "")
		top		= 0

	setting = 'width='+ width +', height='+ height +', left='+ left +', top='+ top +', scrollbars='+ scrollbars +', resizable='+ resizable;
	window.open(url, target, setting);
}


// 주소찾기 ********************************************************************
function popup_addressCheck(formName, siName, dongName,moreName, zipCodeName)
{
	var win = "/LIBS/TOOLKIT/NWS_address/address.asp?formName="+ formName +"&siName="+ siName +"&dongName="+ dongName +"&moreName="+ moreName +"&zipCodeName="+ zipCodeName;
	window.open(win, "address", "width=367, height=460, left=200, top=200, scrollbars=yes");
}


// 아이디 중복확인 *************************************************************
function check_id(formName, usr_id)
{
	var theform		= eval("document."+ formName);
	var usr_id		= eval("theform."+ usr_id);

	if(usr_id.value == "")
	{
		alert("ID를 입력해 주십시오.");
		usr_id.focus();
	}
	else
	{
		var win		= "none.asp??=JOIN/POPUP/check_id&formName="+ formName +"&usr_id="+ usr_id.value;
		window.open(win, "check_id", "width=350, height=253, left=200, top=200");
	}
}


// 닉네임 중복확인 *************************************************************
function check_nick(formName, nick_nm, mode)
{
	var theform		= eval("document."+ formName);
	var nick_nm		= eval("theform."+ nick_nm);

	if(nick_nm.value == "")
	{
		alert("닉네임을 입력해 주십시오.");
		nick_nm.focus();
	}
	else
	{
		var win		= "none.asp??=JOIN/POPUP/check_nick&formName="+ formName +"&nick_nm="+ nick_nm.value +"&mode="+ mode;
		window.open(win, "check_nick", "width=350, height=253, left=200, top=200");
	}
}


// 상품이미지 크게보기 *********************************************************
function popup_enlarge(item_idx)
{
	var url = 'none.asp??=item/popup_enlarge&item_idx=' + item_idx;
	window.open(url, "enlarge", "width=683, height=545");
}


// 로그인 **********************************************************************
function login(from_url)
{
	var win = "none.asp??=PASSPORT/POPUP/user_login&mode=login&from_url="+ escape(from_url);
	window.open(win, "login", "left=20, top=25, width=350, height=230");
}


// 아이디 찾기 *****************************************************************
function idsearch()
{
	var win = "none.asp??=PASSPORT/id_search";
	window.open(win, "login", "left=20, top=25, width=440, height=320");
}

// File Upload 창 띄우기 *******************************************************
function popup_upload(formName, inputName, uploadType)
{
	var win = '/LIBS/TOOLKIT/NWS_upload/fileselect.asp?formName='+ formName +'&imageName='+ inputName +'&uploadType='+ uploadType;
	window.open(win, 'popup', 'width=350, height=250');
}


// 코드관리 POPUP **************************************************************
function popup_code_manage(code_fg)
{
	var win = "none.asp??=DBSYSTEM/POPUP/code_manage&file_mode=mng&code_fg=" + code_fg;
	window.open(win, 'popup', 'width=366, height=400, top=50, left=100, scrollbars=yes');
}


// 인쇄페이지 ******************************************************************
function popup_print(query)
{
	var url	= "print.asp?" + query;
	popup_win(url, "print", 600, 600, 0, 0, 1, 0);
}

function popup_print2(url)
{
	popup_win(url, "print", 600, 600, 0, 0, 1, 0);
}


// 마이페이지 조회날짜 변경 ****************************************************
function change_date(str_date)
{
	var date	= new Date();

	var today_year		= date.getFullYear();
	var today_month		= date.getMonth() + 1;
	var today_day		= date.getDate();

	if (("" + today_month).length == 1)
		today_month		= "0" + today_month;
	if (("" + today_day).length == 1)
		today_day		= "0" + today_day;

	document.frm.from_year.value	= str_date.substring(0, 4);
	document.frm.from_month.value	= str_date.substring(5, 7);
	document.frm.from_day.value		= str_date.substring(8, 10);
	document.frm.to_year.value		= today_year;
	document.frm.to_month.value		= today_month;
	document.frm.to_day.value		= today_day;
}


// 레이어 View or Hidden *******************************************************
function show_layer(lname)
{
	var layer = (navigator.appName == 'Netscape') ? document.layers[lname] : document.all[lname];
	if (lname == '') return;
	if (navigator.appName == 'Netscape') layer.visibility = 'show';
	else layer.style.visibility = 'visible';
}

function hide_layer(lname)
{
	if (lname)
	{
		var layer = (navigator.appName == 'Netscape') ? document.layers[lname] : document.all[lname];
		if (navigator.appName == 'Netscape') layer.visibility = 'hide';
		else layer.style.visibility = 'hidden';
	}
	else
	{
		for (i = 1 ; i <= 10 ; i ++)
		{
			var lname = 'layer'+i;
			var layer = (navigator.appName == 'Netscape') ? document.layers[lname] : document.all[lname];
			if (navigator.appName == 'Netscape') layer.visibility = 'hide';
			else layer.style.visibility = 'hidden';
		}
	}
}


// TOPMENU 검색 ****************************************************************
function checkform_top_search(theform)
{
	if (theform.search_value.value == "")
	{
		alert("검색어를 입력해 주십시오.");
		theform.search_value.focus();
		return false;
	}
//	else if (theform.search_value1.value.length < 3)
//	{
//		alert("검색어는 3자 이상이어야 합니다.");
//		theform.search_value1.focus();
//		return false;
//	}
}


// 이미지 크기 조절하여 img 태그 출력 ******************************************
function change_image_size(image_url, limit_width, limit_height)
{
	imgFile					= new Image();
	imgFile.src				= image_url;

	var real_width			= imgFile.width;
	var real_height			= imgFile.height;

	if (real_width >= real_height)
	{
		if (real_width <= limit_width)
		{
			result_width	= real_width;
			result_height	= real_height;
		}
		else
		{
			result_width	= limit_width;
			result_height	= parseInt((real_height * limit_width) / real_width);
		}
	}
	else
	{
		if (real_height <= limit_height)
		{
			result_width	= real_width;
			result_height	= real_height;
		}
		else
		{
			result_width	= parseInt((real_width * limit_height) / real_height);
			result_height	= limit_height;
		}
	}

	document.write ("<img src='"+ image_url +"' width='"+ result_width +"' height='"+ result_height +"' border='0' style='cursor:hand' onclick='showPicture(\""+ image_url +"\")'>");
}


// 이미지 제크기로 팝업창 띄우기 ***********************************************
function popup_realsize(src)
{
	var imgObj = new Image();
	imgObj.src = src;
	var wopt = "scrollbars=no,status=no,resizable=no";
	wopt += ",width=" + imgObj.width;
	wopt += ",height=" + imgObj.height;
	var wbody = "<head><title>사진 보기</title>";
	wbody += "<s"+"cript language='javascript'>";
	wbody += "function finalResize(){";
	wbody += "  var oBody=document.body;";
	wbody += "  var oImg=document.images[0];";
	wbody += "  var xdiff=oImg.width-oBody.clientWidth;";
	wbody += "  var ydiff=oImg.height-oBody.clientHeight;";
	wbody += "  window.resizeBy(xdiff,ydiff);";
	wbody += "}";
	wbody += "</"+"script>";
	wbody += "</head>";
	wbody += "<body onLoad='finalResize()' style='margin:0'>";
	wbody += "<a href='javascript:window.close()'><img src='" + src + "' border=0></a>";
	wbody += "</body>";
	winResult = window.open("about:blank","",wopt);
	winResult.document.open("text/html", "replace");
	winResult.document.write(wbody);
	winResult.document.close();
	return;
}


// TRIM ************************************************************************
function trim(str)
{
	str += '';				// 숫자라도 문자열로 변환
	return str.replace(/^\s*|\s*$/g, '');
}


// input or textarea OnFocus 내용지우기 ****************************************
function delete_contents(input)
{
	if (input.value == input.defaultValue)
	{
		input.value = "";
	}
}


// input or textarea OnFocus 내용채우기 ****************************************
function fill_contents(input)
{
	if (input.value == "")
	{
		input.value = input.defaultValue;
	}
}


// input or textarea OnFocus 배경이미지 지우기 *********************************
function delete_background(input)
{
	input.style.backgroundImage = "url(about:blank)";
}


// input or textarea OnFocus 배경이미지 채우기 *********************************
function fill_background(input)
{
	if (input.value == "")
	{
		input.style.backgroundImage = input.defaultValue;
	}
}


// input 값 초기화 *************************************************************
function reset_input(el_nm)
{
	var input			= document.getElementById(el_nm);
	input.value			= "";
}

// 날짜 유효성 체크 ************************************************************
function checkDate(date_value)
{
	var getNow				= new Date();
	var sYear				= date_value.substring(0, 4);
	var sMonth				= date_value.substring(5, 7);
	var sDay				= date_value.substring(8, 10);

	if (date_value.length != 10)
		return false;
	if (sYear == null || sYear == "" || sMonth == null || sMonth == "" || sDay == null || sDay == "")
		return false;

	var iYear				= parseInt(sYear);
	var iMonth				= sMonth.substring(0, 1) == "0"	? parseInt(sMonth.substring(1, 2)) : parseInt(sMonth);
	var iDay				= sDay.substring(0, 1) == "0"	? parseInt(sDay.substring(1, 2)) : parseInt(sDay);

	if (iMonth < 1 || iMonth > 12)
		return false;

	if (iDay < 1 || iDay > 31)
		return false;

	if (iMonth == 2)
	{
		if ((iYear % 400 == 0 || (iYear % 100 != 0 && iYear % 4 == 0)))
		{
			if (iDay > 29)
				return false;
		}
		else if (iDay > 28)
			return false;
	}
	else if (iMonth==4 || iMonth==6 || iMonth==9 || iMonth==11)
	{
		if (iDay > 30)
			return false;
	}
	else
	{
		if (iDay > 31)
			return false;
	}

//	if (iYear < 1900 || iYear > getNow.getFullYear() + 1)
//		return false;

	return true;
}


// 바이트 수 세기 **************************************************************
function calculateBytes(contents, maxBytes, bytesId)
{
	var tcount	= 0;
	var tmpStr1	= new String(contents.value);
	var tmpStr2	= "";
	var temp	= tmpStr1.length;
	var onechar;

	for (k=0; k<temp; k++)
	{
		onechar = tmpStr1.charAt(k);
		if (escape(onechar).length > 4)
		{
			tcount += 2;
		}
		else
		{
			tcount += 1;
		}

		if (tcount <= maxBytes)
		{
			tmpStr2	= tmpStr2 + onechar;
		}
	}

	if (bytesId != "")
	{
		var bytesId_input	= document.getElementById(bytesId);
		replaceText(bytesId_input, tcount);
	}

	if (tcount > maxBytes)
	{
		alert("최대 "+ maxBytes +"Byte 까지만 입력하실 수 있습니다.");
		contents.value	= tmpStr2;
		calculateBytes(contents, maxBytes, bytesId)
	}
}


function replaceText(el, text)
{
	if (el != null)
	{
		clearText(el);
		var newNode = document.createTextNode(text);
		el.appendChild(newNode);
	}
}

function clearText(el)
{
	if (el != null)
	{
		if (el.childNodes)
		{
			for (var i = 0; i < el.childNodes.length; i++)
			{
				var childNode = el.childNodes[i];
				el.removeChild(childNode);
			}
		}
	}
}

function getText(el)
{
	var text = "";
	if (el != null)
	{
		if (el.childNodes)
		{
			for (var i = 0; i < el.childNodes.length; i++)
			{
				var childNode = el.childNodes[i];
				if (childNode.nodeValue != null)
				{
					text = text + childNode.nodeValue;
				}
			}
		}
	}
	return text;
}

// 쿠키 찾기 *******************************************************************
function getCookie(name)
{
	var returnCookie		= "";
	var thisCookie			= document.cookie.split("; ") ;
	for (i = 0; i < thisCookie.length; i++)
	{
		// 쿠키가 발견될때까지 찾기
		if(thisCookie[i].split("=")[0] == name)
			returnCookie	= thisCookie[i].split("=")[1]; // 쿠키를 찾아서"=" 로 분리한후 변수로 저장
	}
	return returnCookie;
}
