﻿var USING_CUSTOM_IMAGE = 1;
var CUSTOM_IMAGE_URL = "http://ck.softforum.co.kr/CKKeyPro/gangwon/gangwon_logo_sign.bmp|C2467A510286FB44BFD7AF55702C3B6CB512ECDB|3C343FC027D863A59CC384CB1F16DD9C2407F61C770A018112BBF0F210686CF768757A7D4D18E6DA3F26A45950FA2B43EBAC1C0FE05B964A9AB1E91C0CA9CC495C4F7232C2F4FCB36937E10B367878EA8EE90359B017CDF04F0766309B8335247A854B0571AFF424E8DBFCD73C3F5D97825FB38FBA567FE6501AE963007EAA70";
var USING_CKKeyPro = "1";		

var CKKeyPro_CLSID="clsid:6CE20149-ABE3-462E-A1B4-5B549971AA38";
var CKKeyPro_CODEBASE_x64 = "http://ck.softforum.co.kr/CKKeyPro/gangwon/CKKeyPro3025_64k.cab";
var CKKeyPro_CODEBASE_x86 = "http://ck.softforum.co.kr/CKKeyPro/gangwon/CKKeyPro3025_32k.cab";
var CKKeyPro_VERSION 			= "version=3,0,2,5";
var Multi_InstallBinary 	= "http://ck.softforum.co.kr/CKKeyPro/gangwon/CKKeyPro_Installer.exe";
var Multi_Version 				= "1.0.0.31";

var Base = {
	ua		: navigator.userAgent.toLowerCase(),
  ie		: navigator.appName == 'Microsoft Internet Explorer',
  ns		: navigator.appName == 'Netscape',
  ff		: navigator.userAgent.match('Firefox') == 'Firefox',
  sf		: navigator.userAgent.match('Safari') == 'Safari',
  op		: navigator.userAgent.match('Opera') == 'Opera',
  cr		: navigator.userAgent.match('Chrome') == 'Chrome',
  win		: navigator.platform.match('Win') == 'Win'
}

var getPluginType = {
	cCKFFMIME		: ((Base.win) && (Base.ff ||Base.op)),
	cCKSFMIME		: ((Base.win) && (Base.sf ||Base.cr)),
	cACTIVEX		: ((Base.win) && (Base.ie)),
	cNPRUNTIME	: (Base.win && ((Base.ff || Base.sf || Base.cr) || Base.op)),
	cOtherNP		: (Base.win && (Base.cr || Base.op))
}

function checkCKKeyProMime() {
	var result = "";
	if (getPluginType.cCKFFMIME) {
		var CKmType = navigator.mimeTypes['application/ClientKeeperKeyPro'];
		if (CKmType == undefined) {
			return false;
		} else {
			return true;
		}
	} else if (getPluginType.cCKSFMIME) {
		var CKmType = navigator.mimeTypes['application/clientkeeperkeypro'];
		if (CKmType == undefined) {
			return false;
		} else {
			return true;
		}
	}
}

function PrintXecureCKActiveXTag() {
		if (USING_CKKeyPro == "1") {
				var Str="";
				
				Str+= '<object classid="' + CKKeyPro_CLSID + '"';
				if(navigator.cpuClass.toLowerCase() == "x64")	{	
					Str+= '\n\t codebase="' + CKKeyPro_CODEBASE_x64 + '#' + CKKeyPro_VERSION + '"';
				}	else {
					Str+= '\n\t codebase="' + CKKeyPro_CODEBASE_x86 + '#' + CKKeyPro_VERSION + '"';
				}
				Str+= '\n\tvspace="0" hspace="0" width="0" id="CKKeyPro" style="display:none;">';
				Str+= '\n\t <PARAM name="PKI" value="Simple">';
				Str+= '\n\t <PARAM name="DefaultEnc" value="Off">';
				Str+= '\n\t <PARAM name="DefaultPaste" value="On">';
				Str+= '\n\t <PARAM name="AllowDuplicates" value="true">';
				//Str+= '\n\t <PARAM name="ClearBufferOnEmpty" value="true">';
				if(USING_CUSTOM_IMAGE){
					Str+= '\n\t <PARAM name="ImageURL" value="' + CUSTOM_IMAGE_URL + '">';
				}		
				Str+= '</object>';	
				document.write(Str);
		}
}

function PrintXecureCKEmbedTag() {
		if (USING_CKKeyPro == "1") {
				var Str="";
				Str+= '<EMBED id="CKKeyPro" type="application/ClientKeeperKeyPro" width=0 height=0 hidden="true" ';
				if(USING_CUSTOM_IMAGE) {
					Str+= 'ImageURL="' + CUSTOM_IMAGE_URL + '" ';
				}
				//Str+= 'FireFoxUseDirect="on"';
				Str+= '>';
				Str+= '</EMBED>';
				Str+= '<NOEMBED>no ClientKeeper KeyPro</NOEMBED>';
				
		   	document.write(Str);
		}
}

function CK_PrintXecureCKTag()
{
	if (getPluginType.cACTIVEX){
		PrintXecureCKActiveXTag();
	} else if (getPluginType.cNPRUNTIME){
		var mTypeRet = checkCKKeyProMime();
		if (mTypeRet) {
			PrintXecureCKEmbedTag();
		} else {
			CK_ConfirmInstall();
		}
	}
}

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(!getPluginType.cACTIVEX) return;
	if(document.CKKeyPro==null || typeof(document.CKKeyPro) == "undefined" || document.CKKeyPro.object==null)	{
		return;
	}
	document.CKKeyPro.ReScanDocument();
}


function HaveControl_CK() {
	if(!getPluginType.cACTIVEX) return;
	if(document.CKKeyPro==null || typeof(document.CKKeyPro) == "undefined" || document.CKKeyPro.object == null)
		window.location = Multi_InstallBinary;
	else
		return true;
}

var CK_objFocused;
function CK_Start(nsEvent)
{
	if(!getPluginType.cNPRUNTIME) return;

	var theEvent;
	var inputObj;

	if (nsEvent.type == "text" || nsEvent.type == "password") {
		inputObj = nsEvent;
	}
	else {
		theEvent = nsEvent ? nsEvent : window.event;
		inputObj = theEvent.target ? theEvent.target : theEvent.srcElement;
	}

	try{
		document.getElementById('CKKeyPro').StartCK(inputObj);
		CK_objFocused = inputObj;
	}catch(e){
		//alert("CK_Start catch");
	}
}

function CK_Stop()
{
	if(!getPluginType.cNPRUNTIME) return;
	
	try{
		document.getElementById('CKKeyPro').StopCK();
	}catch(e){
		//alert("CK_Stop catch");
	}
}

function CK_PatchKey()
{
	if(!getPluginType.cNPRUNTIME) return;
	
 	try{
		document.getElementById('CKKeyPro').PatchKey(CK_objFocused);
	}catch(e){
		//alert("CK_Dummy catch");
	}
}

function CK_Blur()
{
	if(!getPluginType.cNPRUNTIME) return;
	
	try{
		CK_objFocused.blur();
	}catch(e){
		//alert("CK_Blur catch");
	}
}

function CK_ConfirmInstall() {
	if(confirm("키보드 보안 프로그램이 설치 되어있지 않습니다. 설치 하시겠습니까?")) {
		location.href = Multi_InstallBinary;
	}
}

function CK_CheckPlugInVersion() {
	if(!getPluginType.cNPRUNTIME) return;
 	try{
		IsNeedUpdate = document.getElementById('CKKeyPro').NeedUpdate(Multi_Version);	
		if (IsNeedUpdate==false){
		} else {
			alert("키보드보안 프로그램이 업데이트 되었습니다.\n\n 안전한 사이트 이용을 위해 설치 후 진행하시기 바랍니다.");
			location.href = Multi_InstallBinary;
		}
	}	catch(e){
			CK_ConfirmInstall();
	}
}


function CK_ApplySecurity()
{
	if(!getPluginType.cNPRUNTIME) return;
	
	try{
		for(var i=0;i<document.forms.length;i++){
			for (var j=0;j < document.forms[i].elements.length;j++){				
				if(document.forms[i].elements[j].tagName == "INPUT" && (document.forms[i].elements[j].type == "text" || document.forms[i].elements[j].type == "password")){
			  	  if(document.forms[i].elements[j].addEventListener){
			  	  	document.forms[i].elements[j].addEventListener("focus", CK_Start, false);		//w3c
			  	  	document.forms[i].elements[j].addEventListener("blur", CK_Stop, false);		//w3c
			  	  }
		  	  	else if (document.forms[i].elements[j].attachEvent){
	  	  			 document.forms[i].elements[j].attachEvent("onfocus", CK_Start);					//msdom
	  	  			 document.forms[i].elements[j].attachEvent("onblur", CK_Stop);					//msdom
	  	  		}
					}	//end if
				} // end for
			} // end for
	}catch(e){		
			//	alert("error");	
	} // end try
}


var sKeyProXPIURL = "http://ck.softforum.co.kr/CKKeyPro/gangwon/keypro-1.0.0.xpi";
var sKeyProDMGURL = "http://ck.softforum.co.kr/CKKeyPro/gangwon/keypro.dmg";

var sKeyProNewName = new Array(2);
sKeyProNewName[0]="CKKeyPro Plugin 1.0.0"; // linux
sKeyProNewName[1]="keypro text editbox";  // mac
var sKeyProOldName = "CKKeyPro Plugin 0.9.0";

var dwOsType=0;;
var OldInputElemetArray = new Array();
var NewInputElemetArray = new Array();
var arrIndex=0;

function CK_Start_MOS(nsEvent) {
	var theEvent;
	var inputObj;

	if (nsEvent.type == "text" || nsEvent.type == "password") {
		inputObj = nsEvent;
	}
	else {
		theEvent = nsEvent ? nsEvent : window.event;
		inputObj = theEvent.target ? theEvent.target : theEvent.srcElement;
	}

	setTimeout("CK_GetText()",100);
		CK_objFocused = inputObj;
	
		var k=document.getElementById('CKKeyPro');
		k.startCk(inputObj);
		CK_objFocused = inputObj;
		try{
		
		
		CK_GetText();
		}catch(e){
		alert("CK_Start catch "+e.discription);
	}
}

function CK_Stop_MOS() {
	try{
		CK_objFocused=0;
		document.getElementById('CKKeyPro').stopCk();
	}catch(e){

	}
}

function CK_PatchKey_MOS(key) {
	try{
		var orgKey=CK_objFocused.value;
		orgKey=orgKey+key;
		CK_objFocused.value=orgKey;
	}catch(e){

	}
}

function CK_GetText() {
    if(checkEnv()==2) {
   	    try{
            if(CK_objFocused)
            {
               CK_objFocused.value=CK_objFocused.value+document.getElementById('CKKeyPro').getText();
            }
            setTimeout("CK_GetText()",100);
	    }catch(e){
    	
	    }
	 }
}


function CK_ApplySecurity_MOS() {
	try{
		document.addEventListener("blur", CK_Stop_MOS, false);	
		for(var i=0;i<document.forms.length;i++){
			for (var j=0;j < document.forms[i].elements.length;j++){				
				if(document.forms[i].elements[j].tagName == "INPUT" && (document.forms[i].elements[j].type == "text" || document.forms[i].elements[j].type == "password")){
			  	  if(document.forms[i].elements[j].addEventListener){
			  	  	document.forms[i].elements[j].addEventListener("focus", CK_Start_MOS, false);		//w3c
			  	  	document.forms[i].elements[j].addEventListener("blur", CK_Stop_MOS, false);		//w3c
			  	  }
		  	  	else if (document.forms[i].elements[j].attachEvent){
	  	  			 document.forms[i].elements[j].attachEvent("onfocus", CK_Start_MOS);					//msdom
	  	  			 document.forms[i].elements[j].attachEvent("onblur", CK_Stop_MOS);					//msdom
	  	  		}
					}	//end if
				} // end for
			} // end for
	}catch(e){		
			//	alert("error");	
	} // end try
}

function getStyle(element, style) {
    if(element.currentStyle) return element.currentStyle.getAttribute(style); // IE
    else{
        var css = document.defaultView.getComputedStyle(element, null);
        return css.getPropertyValue(style);
    }
}

function statusPlugin(OsType) {
	var pluginArray = navigator.mimeTypes.length;	
	var pluginName;
	
	for(var i=0; i < pluginArray; i++) 
	{
		pluginName = navigator.mimeTypes[i].description;
        //alert(pluginName);
		if ( pluginName == sKeyProNewName[OsType-1] ) {
			return 0;
		}
		else if ( pluginName == sKeyProOldName ) {
			return 1;
		}
	}
	return -1;
}

function checkEnv() {
    if(navigator.userAgent.match("Linux") && navigator.userAgent.match("Firefox")) {
        dwOsType=1;
        return 1; // linux type
    } else if(navigator.userAgent.match("Mac") && navigator.userAgent.match("Safari")) {
       dwOsType=2;
       return 2; // mac type
    }
}

function checkPlugin() {
    switch (checkEnv()) {
    	case 1:
    		if(statusPlugin(1) != 0) {
    			if(confirm("키보드 보안 프로그램이 설치 되어있지 않습니다. 설치 하시겠습니까?")) {
    				location.href = sKeyProXPIURL;
	          return;
	        }
	      } else {
	      	DrawElements(1);
     		}
        break;
            
      case 2:
        if(statusPlugin(2) != 0) {
        	if(confirm("키보드 보안 프로그램이 설치 되어있지 않습니다. 설치 하시겠습니까?")) {
        		location.href = sKeyProDMGURL;
            DrawElements(2);
          }
        } else {
        	DrawElements(2);
        }
        break;
    }
}

function initPlugin() {
  	var iStatus = statusPlugin(checkEnv());

	switch(iStatus) {
		case 1:
			if ( InstallTrigger.UpdateEnabled() ) {
				InstallTrigger.startSoftwareUpdate(sKeyProXPIURL);
			}
			break;
		case -1:
			if ( InstallTrigger.UpdateEnabled() ) {
				InstallTrigger.install( {"CKKeyPro":sKeyProXPIURL} );
			}
			break;
		case 0:
			break;
	}
}

function DrawElements(OsType) {
	for(var i=0;i<document.forms.length;i++) {
    for (var j=0;j < document.forms[i].elements.length;j++) {
    	if(document.forms[i].elements[j].tagName == "INPUT" && (document.forms[i].elements[j].type == "text" || document.forms[i].elements[j].type == "password")) {
    		var newInput=document.createElement("embed");
        if(OsType==1) {
        	newInput.setAttribute("type","application/mozilla-KeyPro");
          newInput.setAttribute("width","0px");
          newInput.setAttribute("height","0px");
          newInput.setAttribute("id", "CKKeyPro")
    
          document.forms[i].elements[j].parentNode.insertBefore(newInput,document.forms[i].elements[j]);
          CK_ApplySecurity_MOS();
					return 0;
				}
        else if(OsType==2) {
          newInput.setAttribute("type","application/x-keypro");
          newInput.setAttribute("pluginspage","http://ck.softforum.co.kr/CKKeyPro/gangwon/keypro.dmg");
          newInput.setAttribute("width","0px");
          newInput.setAttribute("height","0px");
          newInput.setAttribute("id", "CKKeyPro")
    
          document.forms[i].elements[j].parentNode.insertBefore(newInput,document.forms[i].elements[j]);
          CK_ApplySecurity_MOS();
          return 0;
        }
      }
    }
  }
}

function KeySubmit(event) 
{        
    for(var i=0;i<arrIndex;i++)
    {
        OldInputElemetArray[i].value=NewInputElemetArray[i].getText();
        NewInputElemetArray[i].startCk(OldInputElemetArray[i]);
    }
}

if (!Base.win) {
	if(navigator.userAgent.match("Linux") && navigator.userAgent.match("Firefox")) {
		initPlugin();
	} else if(navigator.userAgent.match("Mac") && navigator.userAgent.match("Safari")){
		checkPlugin();
	}
} else {
	CK_PrintXecureCKTag();
	if(!getPluginType.cACTIVEX) {
		var mTypeRet = checkCKKeyProMime();
		if (mTypeRet) {
			CK_CheckPlugInVersion();
		}
	}
	CK_ApplySecurity();
}
