//package단에서 학습창으로 미리보기(학습창 런칭)

var g_strCMIURL= "/ebs/LCMS/cmi/index.jsp?";
var g_strURL   = "http://"+document.domain+"/ebs";
var g_wndStudy = null;

String.prototype.trim = function (){
		var objRegExp = /^(\s*[　]*[ ]*)$/;
		if(objRegExp.test(this))
		{
			var strValue = this.replace(objRegExp,'');
			if( strValue.length == 0 ) return strValue;
		}

		objRegExp = /^(\s*[　]*[ ]*)([\w\W]*)(\b\s*[　]*[ ]*$)/;
		if(objRegExp.test(this))
		{
			return this.replace(objRegExp,'$2');
		}

		return this;
}

function replaceString (strText,strOld,strNew) {
	while(strText.indexOf(strOld)>-1)
		strText= strText.replace(strOld,strNew);
	return strText;
}

function checkStudyWnd() {
	
	try {
		if(g_wndStudy==null)
			return false;
		var strName = g_wndStudy.name;
		return true;
	} catch(e) {
		g_wndStudy=null;
		return false;
	}
}

function isFile(strLocal) {
	
	var strURL = "/ebs/LCMS/common/isFile.jsp";
	var strParem = "filename="+strLocal;
	var strText	= parseInt(getWebText(strURL,strParem));
	
	if(strText!=0)
		return false;

	return true;
}


function getXMLHttpObject() {
	
	var obj=null;
	
	if (document.getElementById) 
		var obj = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
	
	return obj;
}

function getWebText(strURL,strParam) {
	var objXMLHttp=getXMLHttpObject();
	var strText = "";
	
	if (objXMLHttp) {
		
		objXMLHttp.open("POST", strURL, false);
		objXMLHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		objXMLHttp.setRequestHeader("Content-length", strParam.length); 
        objXMLHttp.setRequestHeader("Connection", "close"); 
		objXMLHttp.send(strParam);
		strText=objXMLHttp.responseText;
		strText=strText.trim();
		
    }
	
	return strText+"";
    
}

function sendWeb(strURL,strParam) {
	return getWebText(strURL,strParam);
/*
	var objXMLHttp=getXMLHttpObject();

	if (objXMLHttp) {
		
		objXMLHttp.open("POST", strURL, true);
		objXMLHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		objXMLHttp.setRequestHeader("Content-length", strParam.length); 
        objXMLHttp.setRequestHeader("Connection", "close"); 
		objXMLHttp.send(strParam);
    }
*/
}

function getLecturePackage(strCourseID) {
	var strURL = "/ebs/LCMS/search/index.jsp?page=/learn/LCMS/course/modifyCourse.jsp?crs_id="+strCourseID;
	var objReturn=window.showModalDialog(strURL,null,getParameter(800,600));
	return objReturn;
	
}

function getMetaCourse() {
	//
	var strURL = "/ebs/LCMS/search/index.jsp?page=selectCourse.jsp";
	var objReturn=window.showModalDialog(strURL,null,getParameter(800,600));
	
	if(typeof(objReturn)!="number")
		return -1;
	return objReturn;
}

function recoverID(strID) {
	
	var nIndex = strID.indexOf("___");

	for(var i=0 ;nIndex>-1 ;i++) {		
		strID=strID.replace("___","-");
		nIndex = strID.indexOf("___");
	}
	
	return strID;
}

function replaceID(strID) {
	
	var nIndex = strID.indexOf("-");
	
	for(var i=0 ;nIndex>-1 ;i++) {		
		strID=strID.replace("-","___");
		nIndex = strID.indexOf("-");
	}
	return strID;
}

function getUniqueID() {
	var charArray=new Array("A","B","C","E","E","F","0","1","2","3","4","5","6","7","8","9");
	var strID="";
	var nIndex;
	
	for(var i=0;i<32;i++) {
		if(i==8||i==12||i==16||i==20)
			strID+="-";
		
		if(i==0) {
			nIndex=parseInt(Math.random() * 6);
		} else {
			nIndex=parseInt(Math.random() * 16);
		}
		
		strID += charArray[nIndex];
	}
	return strID;
}

function convertInt(strInt) {
	try {
		return parseInt(strInt);
	} catch(e) {
		return 0;
	}
}

function convertDouble(strDouble) {
	try {
		
		var fl= parseFloat(strDouble);
		
		if(fl.toString()=="NaN")
			return 0;
		
		return fl;
	} catch(e) {
		return 0;
	}
}

function CheckInt(strInt) {
	try {
		var nInt=parseInt(strInt);
		if(nInt==0) {
			for(var i=0;i<strInt.length;i++) {
				if(strInt.substring(i,i+1)!="0")
					return false;
			}
		}
		
		return true;
	} catch(e) {
		return false;
	}
}

function CheckDouble(strDouble) {
	try {
		
		var fl= parseFloat(strDouble);
		if(fl.toString()=="NaN")
			return false;
		return true;
	} catch(e) {
		return false;
	}
}



function GetEncodeString(strValue) {
	
	if(strValue==null)
		return "";
	
	if(strValue=="")
		return "";
	
	strValue=strValue.toString();
	strValue=strValue.replace("&","&amp;");
	strValue=strValue.replace("\"","&quot;");
	strValue=strValue.replace("<","&lt;");
	strValue=strValue.replace(">","&gt;");
	strValue= encodeURI(strValue);
	strValue =replaceString(strValue,'%','_DPY_');

	return strValue;
}

function getParameter(width,height) {
	
	var nMaxWidth=width;
	var nMaxHeight=height;
	var nHeight=screen.availHeight>nMaxHeight?nMaxHeight:screen.availHeight;
	var nWidth=screen.availWidth>nMaxWidth?nMaxWidth:screen.availWidth;
	var strEdge="Raised";
	var strCenter="Yes";
	var strHelp="No";
	var strResizeable="No";
	var strStatus="No";
	var strParameter;
	strParamenter="dialogHeight:"+ nHeight +"px;";
	strParamenter+="dialogWidth:"+ nWidth +"px;";
	strParamenter+="edge:"+ strEdge +";";
	strParamenter+="center:"+ strCenter +";";
	strParamenter+="help:"+ strHelp +";";
	strParamenter+="resizable:"+ strResizeable +";";
	strParamenter+="status:"+ strStatus +";";
	return strParamenter;
}

function GetCMIXML(cmidata) {
	var strXML= "<?xml version=\"1.0\" encoding=\"euc-kr\"?><cmi>"+
				"<tempid>"+cmidata.strTempID+"</tempid>"+
				"<nowcompleted>"+cmidata.bNowcompleted+"</nowcompleted>"+
				"<index>"+GetEncodeString(cmidata.nIndex)+"</index>"+
				"<completion_threshold>"+GetEncodeString(cmidata.dbCompletionThreshold)+"</completion_threshold>"+
				"<completion_status>"+GetEncodeString(cmidata.strCompletionStatus)+"</completion_status>"+
				"<credit>"+GetEncodeString(cmidata.strCredit)+"</credit>"+
				"<entry>"+GetEncodeString(cmidata.strEntry)+"</entry>"+
				"<exit>"+GetEncodeString(cmidata.strExit)+"</exit>"+
				"<launch_data>"+GetEncodeString(cmidata.strLaucnData)+"</launch_data>"+
				"<learner_id>"+GetEncodeString(cmidata.strLearnerID)+"</learner_id>"+
				"<learner_name>"+GetEncodeString(cmidata.strLearnerName)+"</learner_name>"+
				"<location>"+GetEncodeString(cmidata.strLocation)+"</location>"+
				"<max_time_allowed>"+GetEncodeString(cmidata.strMaxTimeAllowed)+"</max_time_allowed>"+
				"<mode>"+GetEncodeString(cmidata.strMode)+"</mode>"+
				"<progress_measure>"+GetEncodeString(cmidata.dbProgressMeasure)+"</progress_measure>"+
				"<scaled_passing_score>"+GetEncodeString(cmidata.dbScaledPassingScore)+"</scaled_passing_score>"+
				"<session_time>"+GetEncodeString(cmidata.strSessionTime)+"</session_time>"+
				"<success_stataus>"+GetEncodeString(cmidata.strSuccessStatus)+"</success_stataus>"+
				"<suspend_data>"+GetEncodeString(cmidata.strSuspendData)+"</suspend_data>"+
				"<time_limit_action>"+GetEncodeString(cmidata.strTimeLimitAction)+"</time_limit_action>"+
				"<total_time>"+GetEncodeString(cmidata.strTotalTime)+"</total_time>"+
				"<courseid>"+GetEncodeString(cmidata.strCourseID)+"</courseid>"+
				"<lectureid>"+GetEncodeString(cmidata.strLectureID)+"</lectureid>";
	
		strXML+="<comments_from_learners>";
		
		var nSize =	cmidata.CommentsFromLearners.Comments.length;
		
		for (var i=0;i<nSize ; i++ ) {
			var objComment = cmidata.CommentsFromLearners.Comments[i];
			strXML+="<comment>";
			strXML+="<comment>"+GetEncodeString(objComment.strComment)+"</comment>"+
					"<location>"+GetEncodeString(objComment.strLocation)+"</location>"+
					"<timestamp>"+GetEncodeString(objComment.strTimestamp)+"</timestamp>";
			strXML+="</comment>";
		}
		strXML+="</comments_from_learners>";
		strXML+="<comments_from_lms>";
		
		nSize =	cmidata.CommentsFromLMS.Comments.length;
		
		for (var i=0;i<nSize ;i++ ) {
			
			var objComment = cmidata.CommentsFromLearners.Comments[i];
			strXML+="<comment>";
			strXML+="<comment>"+GetEncodeString(objComment.strComment)+"</comment>"+
					"<location>"+GetEncodeString(objComment.strLocation)+"</location>"+
					"<timestamp>"+GetEncodeString(objComment.strTimestamp)+"</timestamp>";
			strXML+="</comment>";
		}
		
		strXML+="</comments_from_lms>";
		strXML+="<interactions>";
		nSize = cmidata.Interactions.Interactions.length;
		
		for(var i=0;i<nSize;i++) {
			var objInteraction= cmidata.Interactions.Interactions[i];
			strXML += "<interaction>";
			strXML +="<id>"+GetEncodeString(objInteraction.strID)+"</id>"+
			"<type>"+GetEncodeString(objInteraction.strType)+"</type>"+
			"<timestamp>"+GetEncodeString(objInteraction.strTimestamp)+"</timestamp>"+
			"<weighting>"+GetEncodeString(objInteraction.dbWeight)+"</weighting>\n"+
			"<learner_response>"+GetEncodeString(objInteraction.strLearnerResponse)+"</learner_response>"+
			"<result>"+GetEncodeString(objInteraction.strResult)+"</result>"+
			"<latency>"+GetEncodeString(objInteraction.strLatency)+"</latency>"+
			"<description>"+GetEncodeString(objInteraction.strDescription)+"</description>";
			
			var objectives = objInteraction.Objectives;
			var nObjSize = objectives.length;
			
			strXML+="<objectives>";
			
			for(var j=0;j<nObjSize;j++) {
				var objective = objectives[i];
				strXML+="<objective>"+
					"<id>"+objective.strID+"</id></objective>";
			}
			strXML+="</objectives>";
			
			var crs = objInteraction.CorrectResponses;
			nObjSize = crs.length;
			strXML+="<correct_responses>";
			
			for(var j=0;j<nObjSize;j++) {
				var cr = crs[i];
				strXML+="<correct_response><pattern>"+cr.strID+"</pattern></correct_response>";
			}
			strXML+="</correct_responses>";
			strXML += "</interaction>";
		}
		strXML+="</interactions>";

		var objetives = cmidata.Objectives.Objectives;
		nSize = objetives.length;

		for(var i=0;i<nSize;i++) {
			var objetive=objetives[i];
			strXML+="<objective>"+
					"<id>"+objective.strID+"</id>";
			strXML+="<success_status>"+GetEncodeString(objetive.strSuccessStatus)+"</success_status>"+
					"<completion_status>"+GetEncodeString(objetive.strCompletionStatus)+"</completion_status>"+
					"<prgress_measure>"+objetive.dbProgressMeasure+"</prgress_measure>"+
					"<description>"+GetEncodeString(objetive.strDescription)+"</description>";
			var objScore = cmidata.Objectives.Score;
			strXML+="<score>"+
				"<scaled>"+objScore.dbScaled+"</scaled>"+
				"<raw>"+objScore.dbRaw+"</raw>"+
				"<min>"+objScore.dbMin+"</min>"+
				"<max>"+objScore.dbMax+"</max>"+
				"</score>";
			strXML+="</objective>";

		}
		
		var objScore = cmidata.Score;		
		strXML+="<score>"+
				"<scaled>"+objScore.dbScaled+"</scaled>"+
				"<raw>"+objScore.dbRaw+"</raw>"+
				"<min>"+objScore.dbMin+"</min>"+
				"<max>"+objScore.dbMax+"</max>"+
				"</score>";
				
		strXML += "</cmi>";
		return strXML;

}

function getCenterLeft(nWidth) {
	var nAvail= getMaxWidth()-nWidth;
	if(nAvail<=0)
		return 0;
	return  parseInt(nAvail/2);

}

function getCenterTop(nHeight) {
	var nAvail= getMaxHeight()-nHeight;
	if(nAvail<=0)
		return 0;
	return parseInt(nAvail/2);

}

function getMaxWidth() {
	return screen.availWidth;
}

function getMaxHeight() {
	return screen.availHeight-28;
}

function showStudyWindow(strURL,strTemplate) {
		var nWidth=getMaxWidth();
	var nHeight=getMaxHeight();
	var nTop=getCenterLeft(nWidth);
	var nLeft=getCenterTop(nHeight);
	
	if(strTemplate=="L08L01") {
		
	} else if(strTemplate=="L08L02") { //sat 
		
	} else if(strTemplate=="L08L03") { //sat 
		
	} else if(strTemplate=="L08L04") { //sat

	} else if(strTemplate=="L08L05") { //sat

	}
	
	strParameter="fullscreen=no,top="+nTop+",left="+nLeft+",width="+nWidth+",height="+nHeight+",toolbar=0,location=0,menubar=0,status=0";
	g_wndStudy=window.open(strURL,"stdwindow",strParameter);
	if(g_wndStudy==null) {
		alert("팝업이 차단되어 학습을 진행 할 수 없습니다");
		return;
	}
	g_wndStudy.focus();
}

//학습하기
function cmiShow(strCrsID,strUserID,strTemplate) {
	var strURL = g_strCMIURL+"mode=0&crs_id="+strCrsID+"&userid="+strUserID+"&template="+strTemplate;
	//var objReturn=window.showModalDialog(strURL,null,getParameter(1024,710));
	showStudyWindow(strURL,strTemplate);
	/*if(objReturn!=null)
	{
		var strXML = GetCMIXML(objReturn);
		strCommit= "/ebs/LCMS/common/setCMIData.jsp";
		getWebText(strCommit,"xml="+strXML);
	}*/
}
//학습하기 Flz
function cmiFlzShow(strCrsID,strUserID,strTemplate,strOrgID) {

	var strURL = g_strCMIURL+"mode=0&crs_id="+strCrsID+"&userid="+strUserID+"&template="+strTemplate+"&org_id="+strOrgID+"&ch_yn=y";
	//var objReturn=window.showModalDialog(strURL,null,getParameter(1024,710));
	showStudyWindow(strURL,strTemplate);
	/*if(objReturn!=null)
	{
		var strXML = GetCMIXML(objReturn);
		strCommit= "/ebs/LCMS/common/setCMIData.jsp";
		getWebText(strCommit,"xml="+strXML);
	}*/
}

//과정 미리보기
function courseShow(strCrsID,strTemplate) {
	var strURL = g_strCMIURL+"mode=1&crs_id="+strCrsID+"&template="+strTemplate;
	//var objReturn=window.showModalDialog(strURL,null,getParameter(1024,710));
	showStudyWindow(strURL,strTemplate);

}

//과정 맛보기
function coursePreview(strCrsID,strTemplate) {
		var strURL = g_strCMIURL+"mode=2&crs_id="+strCrsID+"&template="+strTemplate;
	//var objReturn=window.showModalDialog(strURL,null,getParameter(1024,710));
	//g_wndStudy=window.open(strURL,"stdwindow","top=0,left=0,width=1024,height=710,toolbar=0,location=0,menubar=0,status=0");
	showStudyWindow(strURL,strTemplate);

}
// 컨텐츠 미리보기
function packageShow(strOrgIdentifier,strTemplate) {
	var strURL = g_strCMIURL+"mode=3&org_id="+strOrgIdentifier+"&template="+strTemplate;
//	var objReturn=window.showModalDialog(strURL,null,getParameter(1024,710));
	showStudyWindow(strURL,strTemplate);
}
//패키지 삭제


function getOrgID(strStepID) {
	var strParam = "?uri="+GetEncodeString("/ebs/LCMS/common/getOrgID.jsp?stepid="+strStepID);
	var strURL="/ebs/LCMS/common/dlgPage.jsp"+strParam;
	return window.showModalDialog(strURL,null,getParameter(800,600));

}

function createFileObject() {
	var objFileUploader = document.getElementById("fileUploader");
	if(objFileUploader==null) {
		var objUploader = document.createElement("<object id=\"fileUploader\" CLASSID=\"CLSID:24AD2E29-67F9-41F6-9614-9373A4F045A3\" width=0 height=0 >");
		objUploader.CODEBASE="/ebs/ActiveX/SionixFileUploader.cab#version=1,0,0,0";
		
		//document.documentElement.appendChild(objUploader);
		document.write(objUploader.outerHTML);
	}
}

function uploadPackage() {

	if(typeof(g_strUserID)=="undefined"||g_strUserID==null||g_strUserID=="")
		return;
	//alert("Sorry, there is no uploader\n\nyou have to install uploader first");

	if(typeof(g_strUserID)=="undefined"||g_strUserID==null||g_strUserID=="")
		return;
	try{
		//createFileObject();
		fileUploader.CLASSID="CLSID:24AD2E29-67F9-41F6-9614-9373A4F045A3";
		fileUploader.USERID=g_strUserID;
		fileUploader.SetHTTPActionURL(g_strURL +"/LCMS/packages/uploadContents.jsp");
		fileUploader.LOADACTIONPAGE=g_strURL +"/LCMS/packages/loadpackages.jsp";
		fileUploader.SetLocalFile();
		if(fileUploader.FILESIZE==0)
			return;
		if(fileUploader.Upload()==false)
		{
			alert("파일 업로드에 실패했습니다");
			return;
		}
		alert("패키지 업로드에 성공했습니다");
		location.reload(true);
	}
	catch(e)
	{
		alert("EBS 외국어 사이트에서 배포하는 파일 업로드 컨퍼넌트가 설치되어 있지 않습니다.");		
	}
}

function deletePackage(strManifestIdentifier,strUri) {
	
	var bDelete = confirm("패키지를 삭제하면 복구 할 수 없습니다\n\n삭제를 계속 하시겠습니까?");
	if(bDelete==false)
		return;
	var strURL="/ebs/LCMS/packages/deletemanifest.jsp";
	var strParam="manifest=" + strManifestIdentifier+ "&ruri="+strUri;
	var strText=getWebText(strURL,strParam);
	if(strText=="0") {
		alert("삭제에 성공하였습니다..");
	} else {
		alert("삭제에 실패하였습니다..");
	}
	
	location.href=strUri;
}

function setLCMSMappID(strStepID) {
	var strURL="/ebs/LCMS/common/setMappID.jsp";
	var strParam="step_id="+strStepID;
	var strText=getWebText(strURL,strParam);
	return strText=="0";
}

//과정 외부 맛보기 
function courseOutPreview(strCrsID,strTemplate) {
		var strURL = "http://www.ebslang.co.kr"+g_strCMIURL+"mode=2&crs_id="+strCrsID+"&template="+strTemplate;
	//var objReturn=window.showModalDialog(strURL,null,getParameter(1024,710));
	//g_wndStudy=window.open(strURL,"stdwindow","top=0,left=0,width=1024,height=710,toolbar=0,location=0,menubar=0,status=0");
	showStudyWindow(strURL,strTemplate);

}
