// dummy
var dateObj = new Date();
var dummy = dateObj.getTime();

// get cookie
function getCookie(name) 
{ 
	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 ""; 
}

// set cookie
function setCookie( name, value, expiredays )
{
	var todayDate = new Date();
	todayDate.setDate( todayDate.getDate() + expiredays );
	document.cookie = name + "=" + escape( value ) + "; path=/; expires=" + todayDate.toGMTString() + ";"
}

// window popup
function winOpen()
{
	window.open("zip_popup.html","new","width=400px,height=300px,top=100,left=100"); 
} 

// 숫자만 입력
// onkeydown="javascript:ckkOnlyNumberInput(event);" 
function ckkOnlyNumberInput(e)
{
  // IE코드
  if (window.event) 
  {
    var code = window.event.keyCode;
  }
  else // 그 외 브라우저
  {
    var code = e.which;
  }

  // 숫자(48 ~ 57, 96 ~ 105)
  // 백스페이스 : 8
  // TAB : 9
  // DELETE : 46
  if ( (code > 47 && code < 58) || (code > 95 && code < 106) || code == 8 || code == 9 || code == 13 || code == 46)
  {
    window.event.returnValue = true;
    return;
  }

  if (window.event)
  {
    window.event.returnValue = false;
  }
  else
  {
    e.preventDefault();
  }
}

// 숫자 + 알파벳
function chkOnlyNumberAlphabetInput(e)
{
  // IE코드
  if (window.event) 
  {
    var code = window.event.keyCode;
  }
  else // 그 외 브라우저
  {
    var code = e.which;
  }

  // 숫자(48 ~ 57, 96 ~ 105)
  // 알파벳(65 ~ 90)
  // 백스페이스 : 8
  // TAB : 9
  // DELETE : 46
  if ((code > 47 && code < 58) || (code > 95 && code < 106) || (code > 64 && code < 91) || code == 8 || code == 9 || code == 13 || code == 46)
  {
    window.event.returnValue = true;
    return;
  }

  if (window.event)
  {
    window.event.returnValue = false;
  }
  else
  {
    e.preventDefault();
  }
}

// 커서 이동
// onKeyUp="chkFocusMove('target1', 'target2', 6);" 
function chkFocusMove(target1, target2, number) 
{
  try
  {
    if ( $("#"+target1).val().length >= number)
    {
      $("#"+target2).focus();
    }    
  }
  catch (e)
  {}
}

// 서비스 준비중
function errMsg()
{
	alert('  죄송합니다. 현재 홈페이지 점검중입니다! \n  교육신청은 02-3482-4632~5번으로 연락주십시오! \n  빠르게복구하겠습니다.!');
	return;
}

// 숫자 체크
function checkNum(str)
{
	var isNum = true;

	if (str == null || str == '')
	{
	   isNum = false;
	   return isNum;
	}

	for (var j = 0 ; j < str.length; j++)
	{
	   if (      str.substring(j, j + 1) != "0"
			&&   str.substring(j, j + 1) != "1"
			&&   str.substring(j, j + 1) != "2"
			&&   str.substring(j, j + 1) != "3"
			&&   str.substring(j, j + 1) != "4"
			&&   str.substring(j, j + 1) != "5"
			&&   str.substring(j, j + 1) != "6"
			&&   str.substring(j, j + 1) != "7"
			&&   str.substring(j, j + 1) != "8"
			&&   str.substring(j, j + 1) != "9" )
	   {
			isNum = false;
	   }
	}
	return isNum;
}

// 숫자만 입력
// onkeypress='chkNumOnly(this.form);' 
function chkNumOnly(fform) 
{	
	if ((event.keyCode > 47) && (event.keyCode < 58)) 
	{
		event.returnValue=true;
	}
	else 
	{
		event.returnValue =false;
		fform.focus();
	}	
}
 
// 영문, 특수 문자 체크
function checkEngNum(str)
{
	var alpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	var nonChar = '_-';
	var nonKorean = alpha + nonChar; 
 	
 	var i ; 
 	for ( i=0; i < str.length; i++ )  
	{
 		if( nonKorean.indexOf(str.substring(i,i+1)) < 0) 
		{
 			break ; 
 		}
 	}
 
 	if ( i != str.length ) 
	{
 		return false ; 
 	}
 	else 
	{
 		return true ;
 	} 
 
 	return true;
}

// 특정 문자만 허용 체크
function chkContainsCharsOnly(input,chars) 
{
    for (var inx = 0; inx < input.value.length; inx++) 
	{
		if (chars.indexOf(input.value.charAt(inx)) == -1)
		{
			return false;
		}
    }
    return true;
}

// 레이어 가운대 띄우기
function centerLayerMove(strLayerid)
{                          
	var obj_centerLayer=document.getElementById(strLayerid);
	var bodywidth=document.documentElement.clientWidth;     
	var bodyheight=document.documentElement.clientHeight;   
	var divWidth=obj_centerLayer.offsetWidth;               
	var divHeight=obj_centerLayer.offsetHeight;             
	if(typeof document.body.style.maxHeight!="undefined")
	{  
		var bodyWidth=document.body.clientWidth;        
		bodyHeight=document.body.clientHeight;          
		if(!!(window.attachEvent && !window.opera))
		{    
			pageLeft=document.documentElement.scrollLeft;
			pageTop=document.documentElement.scrollTop;  
		}
		else 
		{
			pageLeft=window.pageXOffset;
			pageTop=window.pageYOffset;
		}
	}
	else 
	{
		pageLeft=document.documentElement.scrollLeft;
		pageTop=document.documentElement.scrollTop;
	}

	var divLeft=pageLeft,divTop=pageTop;
	if(bodyWidth > divWidth)
	{
		divLeft=pageLeft+Math.ceil((bodyWidth-divWidth)/2);
	}
	if(bodyHeight > divHeight)
	{
		divTop=pageTop+Math.ceil((bodyHeight-divHeight)/2);  
	}

	obj_centerLayer.style.left=divLeft+"px";
	obj_centerLayer.style.top=divTop+"px";  
	if(obj_centerLayer.style.display=="")
	{  
		setTimeout(function(){centerLayerMove(strLayerid);},100);
	}
}

// 레이어 팝업
function setLayerPopupShow(target, width, height) 
{
	// Get the screen height and width
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();

	// Set heigth and width to mask to fill up the whole screen
	$('#mask').css({'width':maskWidth,'height':maskHeight});

	// transition effect
	$('#mask').fadeTo("slow",0.4);

	// location
	var topPx = (document.documentElement.scrollTop) + width + "px";
	var leftPx = parseInt(document.body.clientWidth/2) - height + "px";

	$("#"+target).css("display","block");
	$("#"+target).css("top",topPx);
	$("#"+target).css("left",leftPx);
}

// layer popup hide
function setLayerPopupHide(target, mask) 
{
	if(mask == 'hidden')
	{
		$('#mask').hide();
	}
	
	$("#"+target).css("display","none");
}

// 알파벳 체크
function chkIsAlphabet(input) 
{
    var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    return chkContainsCharsOnly(input,chars);
}

// 숫자 체크
function chkIsNumber(input) 
{
    var chars = "0123456789";
    return containsCharsOnly(input,chars);
}

// field 유효성 check
function chkStringIsId(strValue) 
{
	if (strValue.search(/[^A-Za-z0-9_-]/) == -1)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function chkStringIsTel(strValue) 
{
	if (strValue.search(/[^0-9_-]/) == -1)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function chkStringIsMoney(strValue) 
{
	if (strValue.search(/[^0-9_,]/) == -1)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function chkStringIsAlpha(strValue) 
{
	if (strValue.search(/[^A-Za-z]/) == -1)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function chkStringIsNumber(strValue) 
{
	if (strValue.search(/[^0-9]/) == -1)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function chkStringIsJumin(strValue) 
{
	var jumin= strValue;
	if (jumin.length  != 13)
		return false;
	tval=jumin.charAt(0)*2 + jumin.charAt(1)*3 + jumin.charAt(2)*4
	+ jumin.charAt(3)*5 + jumin.charAt(4)*6 + jumin.charAt(5)*7
	+ jumin.charAt(6)*8+ jumin.charAt(7)*9 + jumin.charAt(8)*2
	+ jumin.charAt(9)*3 + jumin.charAt(10)*4 + jumin.charAt(11)*5;

	tval2=11- (tval % 11);
	tval2=tval2 % 10;

	if (jumin.charAt(12)==tval2 &&  (jumin.charAt(6)=="1" ||jumin.charAt(6)=="2")) 
	{
		return true;
	}
	else 
	{
		return false ;
	}
}

function chkStringIsEmail(strValue) 
{
	if (strValue.search(/(.+)@.+\..+/) == -1) 
	{
		return false;
	}
	else 
	{
		for(var i=0; i < strValue.length;i++)
		{
			if (strValue.charCodeAt(i) > 256)
			{
				return false;
			}
		}

		return true;
	}
}

function chkStringIsDate(strValue) 
{
	if (strValue.search(/\d{4}\.\d{2}\.\d{2}/) == -1)
	{
		return false;
	}
	else 
	{
		return true;
	}
}

// 글자수 체크 반환
function chkStringStrLen(strValue) 
{
	var temp;
	var set = 0;
	var count = 0;

	for( k = 0 ; k < strValue.length ; k++ )
	{
		temp = strValue.charAt(k);

		if( escape(temp).length > 4 ) 
		{
			count += 2
		}
		else count++;
	}

	return count;
}

// 왼쪽 공백 체크
function chkStringLeftTrim(strValue)
{
	while (strValue.length>0)
	{
		if(strValue.charAt(0)==' ')
		{
			strValue=strValue.substring(1,strValue.length);              
		}
		else
		{
			return strValue;
		}
	}
	return strValue;
}

// 오른쪽 공백 체크
function chkStringRightTrim(strValue)
{
	while (strValue.length>0)
	{
		if(strValue.charAt(strValue.length-1)==' ')
		{
			strValue=strValue.substring(0,strValue.length-1);              
		}
		else
		{
			return strValue;
		}
	}

	return strValue;
}

// 공백 체크
function chkStringTrim(strValue)
{
	strValue = chkStringLeftTrim(strValue);
	strValue = chkStringRightTrim(strValue);
	return strValue;
}

// 다음 항목 자동 이동
// onKeyUp="chkNextItemAutoMove('', '', 5);"
function chkNextItemAutoMove(curItem, nextItem, curLength)
{
	var curField = eval("document.getElementById('"+curItem+"')");
	var nextField = eval("document.getElementById('"+nextItem+"')");

	if (curField.value.length >= curLength)
	{
		nextField.focus();
	}
}

// 날짜 입력형식 체크("YYYY/MM/DD")
function chkDateFormatDiff(FromDate, ToDate)
{
	var D1,D2,Diff;						//변수를 선언합니다.
	var MinMilli = 1000 * 60;			//변수를 초기화합니다.
	var HrMilli = MinMilli * 60;
	var DyMilli = HrMilli * 24;
	D1 = Date.parse(FromDate);			//구문 분석합니다.
	D2 = Date.parse(ToDate);			//구문 분석합니다.
	Diff = Math.round(Math.abs((D2-D1) / DyMilli))
	if (Diff>-1) 
	{
		Diff= Diff + 1;
	}
	else 
	{
		Diff= Diff - 1;
	}

	return(Diff);						//결과를 반환합니다.
}

// 마우스 포커스 이동
function setMouseFocus(formobj)
{
	formobj.select();
	formobj.focus();
}

// 반복된 문자 체크
function chkRepeatChars(str, num)
{ 
	var cnt = str.length; 
	var repeat = ''; 
	var R=1;

	for(var i=0; i<cnt; i++) 
	{ 
		tmp = str.substr(i, 1); 
		key = tmp; 
		if(key == repeat) 
		{ 
			R++; 
		} 
		else 
		{ 
			R=1; 
			repeat = key; 
		}

		if(R >= num) 
		{ 
			return repeat; 
		} 
	}
	
	return '';
}

// 연속된 문자 체크
function chkSequenceChars(str, num)
{
	var cnt = str.length; 
	var repeat = 0; 
	var R=1;
	 
	for(var i=0; i<cnt; i++) 
	{ 
		asc = str.charCodeAt(i);
		key = asc; 
		if(key == repeat + 1) 
		{ 
			R++; repeat = key; 
		} 
		else 
		{ 
			R=1; repeat = key; 
		}
	  
		if(R >= num) 
		{ 
			return repeat; 
		} 
	} 

	return 0;
}

// 주민등록번호 형식 체크
function chkJuminNumberFormat(socno)
{
    var socnoStr = socno.toString();
    a = socnoStr.substring(0, 1);
    b = socnoStr.substring(1, 2);
    c = socnoStr.substring(2, 3);
    d = socnoStr.substring(3, 4);
    e = socnoStr.substring(4, 5);
    f = socnoStr.substring(5, 6);
    g = socnoStr.substring(6, 7);
    h = socnoStr.substring(7, 8);
    i = socnoStr.substring(8, 9);
    j = socnoStr.substring(9, 10);
    k = socnoStr.substring(10, 11);
    l = socnoStr.substring(11, 12);
    m = socnoStr.substring(12, 13);
    month = socnoStr.substring(2,4);
    day = socnoStr.substring(4,6);
    socnoStr1 = socnoStr.substring(0, 7);
    socnoStr2 = socnoStr.substring(7, 13);

    // 월,일 Validation Check
    if(month <= 0 || month > 12) {
        return false;
    }
    if(day <= 0 || day > 31) {
        return false;
    }

    // 주민등록번호에 공백이 들어가도 가입이 되는 경우가 발생하지 않도록 한다.
    if (isNaN(socnoStr1) || isNaN(socnoStr2)) {
        return false;
    }
    temp=a*2+b*3+c*4+d*5+e*6+f*7+g*8+h*9+i*2+j*3+k*4+l*5;
    temp=temp%11;
    temp=11-temp;
    temp=temp%10;

    if(temp == m) {
        return true;
    }
    else {
        return false;
    }
}

// 사업자등록번호 형식 체크
function chkBusinessNumberForamt(bizID) 
{ 
	var checkID = new Array(1, 3, 7, 1, 3, 7, 1, 3, 5, 1); 
	var tmpBizID, i, chkSum=0, c2, remander; 
	bizID = bizID.replace(/-/gi,'');
	for (i=0; i<=7; i++) chkSum += checkID[i] * bizID.charAt(i); 
	c2 = "0" + (checkID[8] * bizID.charAt(8)); 
	c2 = c2.substring(c2.length - 2, c2.length); 
	chkSum += Math.floor(c2.charAt(0)) + Math.floor(c2.charAt(1)); 
	remander = (10 - (chkSum % 10)) % 10 ;
	if (Math.floor(bizID.charAt(9)) == remander) 
	{	
		return true;
	}
	else 
	{
		return false; 
	}
}

function chkFormValid(formField, checkName, message, maxlength, minlength) 
{
	// 각 필드별 입력값 체크
	// 주민등록시 반드시 값으로 넘긴다.
	// 필수입력 check
	// 글자수 check
	
	// trim 
	formValue = chkStringTrim(formField.value);

	// 항목 구분
	if(checkName != 'jumin')
	{
		if(formField == null) 
		{
			return false;
		}

		if(formValue == '' && minlength > 0)
		{
			alert(message + " 필수입력 항목입니다.");
			setMouseFocus(formField);
			return false;
		}

		if(chkStringStrLen(formValue) < minlength) 
		{
			alert(message + " 최소" + minlength + "자이상 입력하세요.");
			setMouseFocus(formField);
			return false;
		}

		if(chkStringStrLen(formValue) > maxlength) 
		{
			alert(message + " 최대" + maxlength + "자(한글" + maxlength/2 + " 자)까지 입력 가능합니다.");
			setMouseFocus(formField);
			return false;
		}
	}

	switch(checkName) 
	{
		case "" :
			return true;
		case "alpha" :
			if(chkStringIsAlpha(formValue)) 
			{
				return true;
			}
			else 
			{
				alert(message + " 영문자만 입력 가능 합니다.");
				setMouseFocus(formField);
				return false;
			}
			break;
		case "number" :

			if(chkStringIsNumber(formValue)) 
			{
				return true;
			}
			else 
			{
				alert(message + " 숫자만 입력 가능 합니다.");
				setMouseFocus(formField);
				return false;
			}
			break;
		case "id" :
			if(chkStringIsId(formValue)) 
			{
				return true;
			}
			else 
			{
				alert(message + " 영문자와 숫자만 입력 가능 합니다.");
				setMouseFocus(formField);
				return false;
			}
			break;
		case "tel" :
			if (chkStringIsTel(formValue)) 
			{
				return true;
			}
			else 
			{
				alert(message + " 숫자와 - 만 입력 가능합니다.");
				setMouseFocus(formField);
				return false;
			}
			break;
		case "email" :
			if (chkStringIsEmail(formValue)) 
			{
				return true;
			}
			else 
			{
				alert(message + " 이메일 형식이 틀립니다. 다시 입력해 주세요(형식: account@localhost.com");
				setMouseFocus(formField);
				return false;
			}
			break;
		case "date" :
			if (chkStringIsDate(formValue)) 
			{
				return true;
			}
			else 
			{
				alert(message + " 날짜 형식이 틀립니다. 다시 입력해 주세요(형식: 1999.09.09)");
				setMouseFocus(formField);
				return false;
			}
			break;
		case "jumin" :
			if(chkStringStrLen(formValue) != 13)
			{
				alert("주민등록번호를 정확히 입력해주세요");
				return false
			}

			if (chkStringIsJumin(formValue)) 
			{
				return true;
			}
			else 
			{
				alert("주민등록번호를 정확히 입력해주세요");
				return false;
			}
			break;
	}
}

// 로그인폼 체크
function chkMemberLoginForm()
{
	var f = document.form_login;

	if(f.userid.value == '' || f.userid.value == '아이디')
	{
		alert("아이디를 입력해주세요.");
		f.userid.value = '';
		f.userid.focus();
		return false;
	}

	if(f.passwd.value == '' || f.passwd.value == '비밀번호')
	{
		alert("비밀번호를 입력해주세요.");
		f.passwd.value = '';
		f.passwd.focus();
		return false;
	}

	if(f.userid_save.checked == true)
	{
		setCookie('member_id_save', f.userid.value, 365);
	}

	f.action = "/login_proc.php";
	f.submit();
}

// 아이디 저장
function chkLoginSaveId(form)
{
	var f = eval("document."+form);

	if(f && getCookie("member_id_save"))
	{
		f.userid.value = getCookie("member_id_save");
		f.userid_save.checked = true;
	}
}

// 로그인폼 항목 클릭
function chkLoginFormValue(mode, form, target)
{
	var f = eval("document."+form);
	var item = eval("f."+target);

	//alert(mode + ' ==> ' + target + ' : ' + item.value);

	if(mode == 'click')
	{
		if(target == 'userid' && item.value == '아이디' )
		{
			item.value = '';
		}
		else if(target == 'passwd' && item.value == '비밀번호')
		{
			item.value = '';
		}
	}
	else if(mode == 'blur')
	{
		if(target == 'userid' && item.value == '' )
		{
			item.value = '아이디';
		}
		else if(target == 'passwd' && item.value == '')
		{
			item.value = '비밀번호';
		}
	}
	else if(mode == 'keypress')
	{
		if(target == 'passwd' && item.value == '비밀번호')
		{
			item.value = '';
		}
	}
}

// 서비스 준비중
function chkServiceReady()
{
	alert('교육준비중입니다.');
}

// 아이플레임 자동 높이
function setIframeAutoResize(arg) 
{
    arg.height = eval(arg.name+".document.body.scrollHeight");
}

// 라디오박스 체크 유무
function chkRadioChecked(form, target) 
{
	var f = eval("document."+form);
	var item = eval("f."+target);

    var i = 0, val = '';

	for(i = 0;i< item.length; i++) 
	{ 
		if(item[i].checked) 
		{ 
			val = item[i].value;
		} 
	}

	return val;
}

