//필수입력항목 값 검사
function checkValue(obj, msg, afterFocus) {
	var result = false;
	if(afterFocus == null) afterFocus = true;
	if(obj.value.length == 0) {
		alert(msg);
		if(afterFocus) obj.focus();
		result = true;
	}
	return result;
}

//숫자검사
function checkIsNumber(obj, msg, afterFocus) {
	var result = false;
	if(afterFocus == null) afterFocus = true;
	if(isNaN(obj.value)) {
		alert(msg);
		if(afterFocus) obj.focus();
		result = true;
	}
	return result;
}

//정규식으로 유효성을 검사한다, 유효하면 false, 유효하지 않으면 true
function checkExp(str, exp) {
	//새 대소문자 구별하지 않는(gi) 정규식을 만든다
	reg = new RegExp(exp, "gi");
	return !reg.test(str);
}

//선택된 라디오값 가져오기
function getRadioValue(rdo) {
	var value = "";
	for(var i = 0; i < rdo.length; i++) {
		if(rdo[i].checked) {
			value = rdo[i].value;
			break;
		}
	}
	return value;
}

//선택된 콤보박스값 가져오기
function getComboValue(combo) {
	var value = "";
	for(var i = 0; i < combo.length; i++) {
		if(combo[i].selected) {
			value = combo[i].value;
			break;
		}
	}
	return value;
}

//바 없는 새창 열기
function openWindow(url, width, height, scroll, resize, winName) {
	if(scroll == null) scroll = "no";
	if(resize == null) resize = "no";
	winpos = "left=" + ((window.screen.width-width)/2) + ",top=" + ((window.screen.height-height-28)/2);
	winstyle="width=" + width + ",height=" + height + ",status=no,toolbar=no,menubar=no,location=no,resizable=" + resize + ",copyhistory=no,scrollbars=" + scroll + "," + winpos;
	if(winName == null) winName = "_blank";
	window.open(url, winName, winstyle);
}

//모달창을 띄운다
function openModal(url, width, height, title, scroll) {
	var winstyle = "dialogWidth=" + width + "px; dialogHeight:" + height + "px; center:yes; help:no; scroll:no; status:no";
	var value = window.showModalDialog("/_Public/Modal.aspx?url=" + url + "&title=" + title + "&scroll=" + scroll,'_blank', winstyle);	
	return value;
}

//YYYYMM 유효성검사
function checkYearMonth(obj, objName, afterFocus) {
	if(obj.value.length < 6) {
		alert(objName + "를 입력하여 주십시오.");
		if(afterFocus) obj.focus();
		return true;
	} else {
		var year = eval(obj.value.substring(0, 4));
		if(year < 1900 || year > 2100) {
			alert(objName + "의 년도는 1900년~2100년 사이의 년도를 입력하여 주십시오.");
			if(afterFocus) obj.focus();
			return true;
		}
		var month = eval(obj.value.substring(4, 6));
		if(month < 0 || month > 12) {
			alert(objName + "의 월이 잘못 입력되었습니다.");
			if(afterFocus) obj.focus();
			return true;
		}
	}
	return false;
}

//이메일 체크
function checkEmail(email, msg) {
	if(checkExp(email, "^[_0-9a-zA-Z-]+[.]{0,1}([_0-9a-zA-Z-]+)*@[0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*$")) {
		alert(msg);
		return true;
	}
	return false;
}

//아이디 체크
function checkValidID(obj, afterFocus) {
	if(checkExp(obj.value, "^[a-zA-Z0-9]{4,12}$")) {
		alert("아이디는 영문자와 숫자의 조합으로 최소 4자에서 최대 12자까지 사용하실 수 있습니다.");
		if(afterFocus) obj.focus();
		return true;
	}
	return false;
}

//주민번호 체크
function checkPeopleNo(peopleNo1, peopleNo2) {
	var chk = 0;
	var yy = peopleNo1.substring(0, 2); //출생년도 추출
	var mm = peopleNo1.substring(2, 4); //출생월 추출
	var dd = peopleNo1.substring(4, 6); //출생일 추출
	var sex = peopleNo2.substring(0, 1); //성별 추출

	//첫 번째필드가 6자리가 아니거나 출생월일이 날짜 형식에 위배될때
	if((peopleNo1.length!=6) || (mm < 1 || mm > 12 || dd < 1 || dd > 31)) {
		return true;
	}

	//성별형식이 틀리거나 두 번째 필드가 7자리가 아닐때
	if((sex != 1 && sex != 2 ) || (peopleNo2.length != 7 )) {
		return true;
	}

	//주민등록번호 체크
	for(var i = 0; i <= 5; i++) {
		chk = chk + ((i%8+2) * parseInt(peopleNo1.substring(i, i + 1)));
	}

	for(var i=6; i<=11; i++) {
		chk = chk + ((i%8+2) * parseInt(peopleNo2.substring(i-6, i - 5)));
	}

	chk = (11 - (chk % 11)) % 10;

	if(chk != peopleNo2.substring(6, 7)) {
		return true;
	}

	return false;
}

//선택된 갯수
function getCheckedCount(rdo) {
	var cnt = 0;
	for(var i = 0; i < rdo.length; i++) {
		if(rdo[i].checked) cnt++;
	}
	return cnt;
}

//쿠키 읽기
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 ""; 
}

//쿠키 쓰기
function setCookie(name, value, expiredays) {
	var todayDate = new Date();
	todayDate.setDate(todayDate.getDate() + expiredays);
	document.cookie = name + "=" + escape(value) + "; path=/; expires=" + todayDate.toGMTString() + ";"
}

function showFlash(path, width, height) {
	var text ='';
	text = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + width + '" height="' + height + '">';
	text += '<param name="movie" value="' + path + '">';
	text += '<param name="allowScriptAccess" value="sameDomain">';
	text += '<param name="quality" value="high">';
	text += '<embed src="' + path + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + width + '" height="' + height + '">';
	text += '<param name="wmode" value="transparent">';
	text += '</embed>';
	text += '</object>';
	document.write(text);
}

//Ajax 페이징
function pageMove(n) {
	var hdnPage = $("ctl00_hdnPage");
	if (hdnPage == null) hdnPage = $("hdnPage");
	hdnPage.value = n;
	$("btnPageMove").click();
}

function checkAllTo(checked, name) {
	var chk = document.getElementsByName(name);
	for(var i = 0; i < chk.length; i++) {
		chk[i].checked = checked;
	}
}

//달력
function showCalendar(target) {
	openWindow("/Public/Calendar.jsp?target=" + target, 181, 230);
}
