var g_target_url_param = "";
var g_top_menu_toggle = "";

function setCookie(name, value) 
{ 
	var todayDate = new Date() ;
	todayDate.setHours(24) ;
	document.cookie = name + "=" + escape(value) + "; path=/; expires=" +   todayDate.toGMTString() + ";";
} 

function getCookie(name) {

	var Found = false
	var start, end
	var i = 0
	
	while(i <= document.cookie.length) {
		start = i
		end = start + name.length
		
		if(document.cookie.substring(start, end) == name) {
		  Found = true
		  break 
		}
	i++
	}

	if(Found == true) {
	start = end + 1
	end = document.cookie.indexOf(";", start)
	  if(end < start)
		end = document.cookie.length
	return document.cookie.substring(start, end)
	}
	return ""
} 

function cfn_movePageSelf( url ){
	if ( url.length>0 ){
		location.href = ''+ url;
	}
}

function cfn_movePageBlank( url ){
	if ( url.length>0 ){
		window.open(''+url, 'bannerLink', '');
	}
}

function confirmLayerWindowTime(msg, func, type, stime){
	setTimeout("confirmLayerWindow('" + msg + "','" + func + "','" + type + "')", stime);
}

function confirmLayerWindow(msg, func, type){
	
	var htmlTag = "";

	htmlTag +="<div id=\"div_modal\" >";
	htmlTag +="		<div class=\"layerpop_wrapper\" >";
	htmlTag +="			<div class=\"content_area\">";
	htmlTag += "			<BR>" + msg ; 
	htmlTag += "		</div>";
	htmlTag +="			<div class=\"btn_area\"><div class=\"btn_list\">";
	if (type == 1)
	{
		htmlTag +="				<a href=\"javascript:" + func + ";\" class=\"btn_apply\">확인</a>";
		htmlTag +="				<a href=\"javascript:closeLayerWindow();\" class=\"btn_close\">닫기</a>";
	} else if (type == 3) {
		htmlTag +="				<a href=\"javascript:" + func + ";\" class=\"btn_apply\">확인</a>";
	} else if (type == "back") {
		htmlTag +="				<a href=\"javascript:historyBack();\" class=\"btn_close\">닫기</a>";
	} else if (type == 4) {
		htmlTag +="				<a href=\"javascript:" + func + ";\" class=\"btn_close\">닫기</a>";
	} else {
		htmlTag +="				<a href=\"javascript:closeLayerWindow();\" class=\"btn_close\">닫기</a>";
	}
	htmlTag +="			</div></div>";
	htmlTag +="			</div>";
	htmlTag +="		</div>";
	htmlTag +="</div>";
	
	$('body').prepend(htmlTag);

}

function closeLayerWindow(){
		//$("#div_modal").hide();		// 닫기버튼 동작하지 않음. 주석처리. 20220327. 마정용
		$("#div_modal").remove();
}

function historyBack(){
	history.back(-1);
}

function xmlhttp(ischekURL){
	var xmlhttp;

	if (window.XMLHttpRequest)	{	
		// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp = new XMLHttpRequest();
	} else {
		try{
			//win ie4, ie5, ie6
			return new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				return new ActiveXObject("Microsoft.XMLHTTP");
			}catch (e2) {
				return null;
			}
		}
	}

	xmlhttp.open("GET", ischekURL, false);
	xmlhttp.send();

	return xmlhttp.responseText;
}

function cfn_layerTopMyPageMenu( jFlag ){
	if ( jFlag == "open" && $("#div_myPageMenu").css("display")=="none" ){
		$("#div_myPageMenu").show();
	} else {
		$("#div_myPageMenu").hide();
	}
}

function cfn_comfirmLogOut(){
	confirmLayerWindowTime("로그아웃 하시겠습니까?", "cfn_movePageLogOut();", 1, 100);
}
function cfn_movePageLogOut(){
	
	location.href='/member/logout/';
	
}

function cfn_moveLoginPageParam(){
	var tmp_param = g_target_url_param;
	g_target_url_param = "";	//초기화

	if ( tmp_param != ""){
		location.href = "/member/login/?"+ tmp_param;
	} else {
		location.href = "/member/login/";
	}
}

function cfn_movePageQna(){
	location.href='/center/qna/';
}

function cfn_handleGnbExpand() {
	$('#wrapper').toggleClass('expanded_aside');

	if ( $('#wrapper').hasClass('expanded_aside') == true ){
		setCookie("dp_left_toogle", "Y");
	} else {
		setCookie("dp_left_toogle", "");
	}
}


// 별점
function x_handleClickRating(element) {
  const clickedElement = $(element)
  $(element).siblings().removeClass('active')
  $(clickedElement).addClass('active').prevAll().addClass('active')
}
function handleClickRating(element, star_id, num) {
	const clickedElement = $(element);
	$(element).siblings().removeClass('active');
	$(clickedElement).addClass('active').prevAll().addClass('active');

	$("#"+ star_id).val( ''+ num);
}


function cfn_copyUrl(){
	var tmpTextarea = document.createElement('textarea');
	tmpTextarea.value = ""+ location.href;
 
	document.body.appendChild(tmpTextarea);
	tmpTextarea.select();
	tmpTextarea.setSelectionRange(0, 9999);  // 셀렉트 범위 설정
 
	document.execCommand('copy');
	document.body.removeChild(tmpTextarea);
	//alert("URL 복사가 완료되었습니다."); 

	var msg = "URL 복사가 완료되었습니다."
	confirmLayerWindowTime(msg, "closeLayerWindow()", 3, 100);

	cfn_closeDivModal();

}


$(window).resize(function(){
	var winWidth = $(window).width();
	var menuFlag = getCookie("dp_left_toogle")

	if ( winWidth < 1500 ){
		$('#wrapper').removeClass('expanded_aside')
	} else {
		if ( menuFlag == "Y" ){
			$('#wrapper').addClass('expanded_aside')
		} else {
			$('#wrapper').removeClass('expanded_aside')
		}
	}
})


function cfn_course_tracert(cors_id, cors_user_id, cors_user_lsn_id, learning_stat, complete_stat , msg){

	param = "?cors_site=1" 
		+ "&cors_id="		+ cors_id 
		+ "&cors_user_id="	+ cors_user_id 
		+ "&cors_user_lsn_id="	+ cors_user_lsn_id 
		+ "&learning_stat="	+ learning_stat 
		+ "&complete_stat="	+ complete_stat 
		+ "&msg="			+ escape(msg) 

	var result = xmlhttp("/comn/proc/proc.course.trace.asp" + param);
	return ; 

}


function course_tracert(cors_id, cors_user_id, cors_user_lsn_id, learning_stat, complete_stat , msg){
	
		param = "?cors_site=2" 
			+ "&cors_id="		+ cors_id 
			+ "&cors_user_id="	+ cors_user_id 
			+ "&cors_user_lsn_id="	+ cors_user_lsn_id 
			+ "&learning_stat="	+ learning_stat 
			+ "&complete_stat="	+ complete_stat 
			+ "&msg="			+ escape(msg) 

		var result = xmlhttp("/comn/proc/proc.course.trace.asp" + param);
		return ; 

	}


function cfn_movePagePCourseQna(){
	location.href='/pcourse/qna/';
}

function cfn_movePagePCourseInquiry(){
	location.href='/pcourse/inquiry/';
}

//사용자 정의 레이어 팝업
function customLayerWindow(strUrl, title, width, height, idx)
{
	var popcloseAction = "file_closeLayerWindow()"
	
	if (title == "알림")
	{
		popcloseAction = "layer_popup();"
	}

	var margin_top = height / 2 * -1;
	var margin_left = width / 2 * -1;

	//alert($(this).data("targetUrl"));
	var strHtml = "<div class=\"pop_title\" style=\"cursor:pointer;\" >" + title + " <img src=\"/comn/images/btn/btn_popup_closeIcon.gif\" alt=\"닫기\" title=\"닫기\" id=\"pop_close\" class=\"pop_close\" onclick=\""+popcloseAction+"\"></div>"
	strHtml += "<iframe id=\"popupframe\" src=\"" + strUrl + "\" style=\"width:100%;height:100%;\" frameborder=\"0\" scrolling=\"auto\"></iframe>"

	$("#mw_popup").html(strHtml);
	$("#mw_temp").css("display", "block");

	$("#mw_popup").css("top", "47%");
	$("#mw_popup").css("left", "50%");
	$("#mw_popup").css("width", width + "px");
	$("#mw_popup").css("height", height + "px");
	$("#mw_popup").css("margin-top", margin_top + "px");
	$("#mw_popup").css("margin-left", margin_left + "px");
	$("#mw_popup").css("padding-bottom", "30px");

	//alert(strHtml);
	//location.href=$(this).attr("targetUrl");
}
function file_closeLayerWindow()
{
	$("#popupframe").attr("src","");
	$("#mw_popup").html("");
	$("#mw_temp").css("display", "none");
}

function _doPostback(str) {

	frm.method = "post" ;
//	frm.action = gMypage + "?curpage=" + str + "" ;
	frm.action = "./?curpage=" + str + "" ;
	frm.submit() ;
}


function cfn_closeLayerWindow(){
	$("#div_modal").remove();
}

function cfn_fileDownload( url ){
	location.href = ""+ url;
}

function cfn_download_data(filepath, filename){

		if (confirm("다운로드 하시겠습니까? \n용량이 큰화일은 시간이 걸립니다")){
			var result = xmlhttp("/comn/proc/proc.download.asp?filepath=" + filepath + "&filename=" + filename);
			//alert(result) ; 
			$("#objframe").attr("src","/mw3/downloader/?filepath=" + filepath + "&filename=" + filename); 
		}
	}

// idx값을 넘겨야 하는데 함수호출시 파라미터를 줄 수 없음.
var attachfile_idx = "";
function cfn_confirm_attarchFileClear( idx ){
	attachfile_idx = idx
	confirmLayerWindowTime("첨부파일을 삭제 하시겠습니까?", "cfn_attarchFileClear()", 1, 100);
}
function cfn_attarchFileClear(){
	idx = attachfile_idx;
	attachfile_idx = "";

	$("#span_filename_"+ idx).html('');
	$("#fileurl_"+ idx).val('');
	$("#filename_"+ idx).val('');

	closeLayerWindow();
}
function cfn_windowClose(){
	window.close();
}