
var KeyPro_Container_Object = document.createElement("div");
KeyPro_Container_Object.setAttribute("id","KeyPro_Container_Div");

var CKKeyPro_Inst_CLSID="clsid:9951188A-D5AD-4047-A4E0-598E6892E014";
var CKKeyPro_Inst_VERSION="version=1,0,0,6";
var CKKeyPro_CLSID="clsid:6CE20149-ABE3-462E-A1B4-5B549971AA38";	
var CUSTOM_IMAGE_URL = "http://md.egloos.com/img/www/signin/egloos_security_toast.bmp";

var threadXecureCnt = 0;
var threadXecureKey = null;

function isCKKeyProInstalled()
{
	if(document.CKKeyProInstaller==null) { 
		return false;	
	}
	else if(typeof(document.CKKeyProInstaller) == "undefined") {			
		return false;		
	} 
	else if(document.CKKeyProInstaller.object == null) {
		return false;	
	}	
	return true;
}

function isCKInstalled()
{
	var ret;
	
	ret = isCKKeyProInstalled();
	if(ret != true)	{

		var Str="";		

		Str+= '<object classid="' + CKKeyPro_Inst_CLSID + '"';
		Str+= '\n\t codebase="#'+CKKeyPro_Inst_VERSION+'"';
		Str+= '\n\tvspace="0" hspace="0" width="0" id="CKKeyProInstaller" style="display:none;">';
		Str+= '\n</object>';
		
		document.body.appendChild(KeyPro_Container_Object);

		if(KeyPro_Container_Object != null) {
			KeyPro_Container_Object.innerHTML = Str;
		}

		ret = isCKKeyProInstalled();

		if(ret != true) {
			document.body.removeChild(KeyPro_Container_Object);
		}
	}

	return ret;
}

function CreateCKKeyProObject()
{	
	if (isCKKeyProInstalled())
	{
		var Str="";

		Str+= '<object classid="' + CKKeyPro_CLSID + '"';
		Str+= '\n\tvspace="0" hspace="0" width="0" id="CKKeyPro" style="display:none;">';
		Str+= '\n\t <PARAM name="PKI" value="SKComm">';
		Str+= '\n\t <PARAM name="DefaultEnc" value="Off">';
		Str+= '\n\t <PARAM name="TrayIcon" value="Egloos">';
		Str+= '\n\t <PARAM name="TrayPopup" value="On">';
		Str+= '\n\t <PARAM name="ImageURL" value="' + CUSTOM_IMAGE_URL + '">';
		Str+= '</object>';		
		
		if(KeyPro_Container_Object != null)
			KeyPro_Container_Object.innerHTML = Str;
	} 
}

function XecureCK_UIEevents(frm,ele,event,keycode)
{
	var obj;
	var eventObj;

	try{
		obj=document.forms[frm].elements[ele];
		if( document.createEventObject )
		{
			eventObj = document.createEventObject();
			eventObj.keyCode=keycode;
			if(obj)
			{
				obj.fireEvent(event,eventObj);
			}
		}
	}
	catch(e) {}
}

function CKKeyPro_ReScan()
{
	if(document.CKKeyPro==null || typeof(document.CKKeyPro) == "undefined" || document.CKKeyPro.object==null)
	{
		return;
	}
	document.CKKeyPro.ReScanDocument();
}

function CKKeyPro_ShowPopup(TimeToShow, TimeToStay, TimeToHide, PixelIncrement, PosX, PosY)
{
	if(document.CKKeyPro==null || typeof(document.CKKeyPro) == "undefined" || document.CKKeyPro.object==null)
	{
		return;
	}
	document.CKKeyPro.ShowPopup(TimeToShow, TimeToStay, TimeToHide, PixelIncrement, PosX, PosY);
}

function CKKeyPro_hidePopup()
{
	if(document.CKKeyPro==null || typeof(document.CKKeyPro) == "undefined" || document.CKKeyPro.object==null)
	{
		return;
	}
	document.CKKeyPro.HidePopup();
}

function CKKeyPro_OnError(ErrorCode , ErrorSource , Reserved)
{
}

function getCookie(cookieName)
{
	if (document.cookie.length > 0)
 	{
 		cookie_start = document.cookie.indexOf(cookieName + "=");
  			
 		if (cookie_start != -1)
		{ 
			cookie_start = cookie_start + cookieName.length + 1; 
			cookie_end = document.cookie.indexOf(";",cookie_start);

			if (cookie_end == -1) cookie_end = document.cookie.length;

			return unescape(document.cookie.substring(cookie_start,cookie_end));
		} 
  	}
	return "";
}

function initKeycrypt()
{
	if(getCookie('EKS') == "1")
	{	
	
		setKeycryptBtn(true);
		if(navigator.appName == "Microsoft Internet Explorer")	
		{
			var boolInstall = isCKInstalled();

			if (boolInstall) 
			{
				CreateCKKeyProObject();	
			}
			else {
				setKeycryptBtn(false);
			}
		}
		else {
			setKeycryptBtn(false);
		}
	}
	else {
		setKeycryptBtn(false);
	}
}	

function setKeycryptBtn(mode)	
{
	if(mode)
	{
		document.getElementById("KeycryptBtn").src="http://md.egloos.com/img/www/signin/btn_on.gif";
		document.getElementById("KeycryptBtn").alt = "키보드보안 ON";
		document.getElementById("KeycryptBtn").onclick = DeActivateKeycrypt;
	}
	else
	{
		document.getElementById("KeycryptBtn").src="http://md.egloos.com/img/www/signin/btn_off.gif";
		document.getElementById("KeycryptBtn").alt = "키보드보안 OFF";
		document.getElementById("KeycryptBtn").onclick = ActivateKeycrypt;
	}
	
	return true;
}

function ActivateKeycrypt(type) {

	if (type == "x") {
		threadXecureCnt++;
	}

	if (threadXecureCnt > 50) {
		clearThreadXecureKey();
	}

	if(navigator.appName == "Microsoft Internet Explorer")	
	{
		var boolInstall = isCKInstalled();

		if (boolInstall) 
		{
			setKeycryptBtn(true);		
			setKeycryptCookie("EKS", "1");
			CreateCKKeyProObject();
			if (type == "x") {
				try {				
					clearThreadXecureKey();		
					self.location.reload();	
				} 
				catch(e) {}			
			}			
		} 
		else 
		{
			if(type != "x") {
				showEKSInfo();
			}		
			setKeycryptBtn(false);
			setKeycryptCookie("EKS", "0");
		}
	}
	else
	{			
		setKeycryptBtn(false);	
		showEKSNoIE();
		setKeycryptCookie("EKS", "0");			
	}
}

function DeActivateKeycrypt()
{
	var result = clearKeycryptApp();
	if(result) {
		setKeycryptBtn(false);
		setKeycryptCookie("EKS", "0");
		document.getElementById('userid').focus();
		isCKInstalled();
	}
}

function showEKSInfo()
{
	document.getElementById("EKS_LOGIN").style.display="none";
	document.getElementById("EKS_NOIE").style.display="none";
	document.getElementById("EKS_INFO").style.display="block";
}

function showEKSNoIE()
{
	document.getElementById("EKS_LOGIN").style.display="none";
	document.getElementById("EKS_INFO").style.display="none";
	document.getElementById("EKS_NOIE").style.display="block";
}

function showLoginForm()
{
	document.getElementById("EKS_INFO").style.display="none";
	document.getElementById("EKS_NOIE").style.display="none";
	document.getElementById("EKS_LOGIN").style.display="block";
}

function setKeycryptCookie(cookieName, cookieValue)
{
	var today = new Date();
	var expireDate = today.setDate(today.getDate() + 365);

	document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + today.toGMTString();
}

function clearKeycryptApp() 
{
	if(KeyPro_Container_Object) {
		return document.body.removeChild(KeyPro_Container_Object);	
	}
	return false;
}

function keycryptInstall()
{
	if(isCKInstalled())	{
		alert("키보드보안 프로그램을 이미 설치 하셨습니다.");
	}
	else {		
		document.location.href = "http://www.egloos.com/login/CKKeyPro_Installer.php";
		checkInstallTimer();		
	}
}

function checkInstallTimer() 
{	
	threadXecureKey = window.setInterval("ActivateKeycrypt('x');", 500);
}

function clearThreadXecureKey() 
{
	window.clearInterval(threadXecureKey);
	threadXecureKey = null;
}

function popup_open() {
	if(navigator.appName == "Microsoft Internet Explorer") {
		window.open('/popup_keyboard_security.php','keySecurity','width=400,height=390,scrollbars=no,resizable=no,status=yes');
	}
	else {
		showEKSNoIE();
	}		
}
