function __GnxMKDPlus()
{
	this.Init = GnxMKDPlus_Init;
	this.RaiseError = GnxMKDPlus_RaiseError;
	this.CreateObject = GnxMKDPlus_CreateObject;
	this.Load = GnxMKDPlus_Load;
	this.MKD20Event = GnxMKDPlus_MKD20Event;
	this.GetText = GnxMKDPlus_GetText;
	
	// ÇØ´ç °ÔÀÓ»çÀÌÆ®ÀÇ HTML ÁÖ¼Ò·Î ¸ÂÃçÁÖ½Ã¸é µË´Ï´Ù.
	this.strKeyDefenseURL = "http://www.mabinogi.com/c3/common/setupkeydefense.html";
	this.strFireWallURL = "http://www.mabinogi.com/c3/common/setupfirewall.html";
	
	this.arrNotLoginURL = new Array();
	this.arrNotLoginURL[0] = "http://www.mabinogi.com/c3/common/_logincheck_com.asp";
	this.arrNotLoginURL[1] = "http://www.mabinogi.com/c3/common/logout.asp";
	
	this.SetUseKeyDefense = GnxMKDPlus_SetUseKeyDefense;
	this.SetUseFireWall = GnxMKDPlus_SetUseFireWall;
	
	this.SetUpKeyDefense = GnxMKDPlus_SetUpKeyDefense;
	this.SetUpFireWall = GnxMKDPlus_SetUpFireWall;
	
	this.CheckLogin = GnxMKDPlus_CheckLogin;
	
	this.isKeyDefenseUse = false;
	this.isFireWallUse = false;
}

var GnxMKDPlusBrowser = {
	agt : navigator.userAgent.toLowerCase(),
	check : function(browserName) { return GnxMKDPlusBrowser.agt.indexOf(browserName) != -1 },

	//Browser Name
	msie : function() { return GnxMKDPlusBrowser.check("msie") },
	msie5 : function() { return GnxMKDPlusBrowser.check("msie 5") },
	msie55 : function() { return GnxMKDPlusBrowser.check("msie 5.5") },
	msie6 : function() { return GnxMKDPlusBrowser.check("msie 6") },
	msie7 : function() { return GnxMKDPlusBrowser.check("msie 7") },

	firefox : function() { return GnxMKDPlusBrowser.check("firefox") },
	netscape : function() { return GnxMKDPlusBrowser.check("netscape") },

	safari : function() { return GnxMKDPlusBrowser.check("safari") },
	opera : function() { return GnxMKDPlusBrowser.check("opera") },

	//Browser Engine
	gecko : function() { return GnxMKDPlusBrowser.check("gecko") },
	khtml : function() { return GnxMKDPlusBrowser.check("khtml") },
	
	//OS
	windows : function() { return GnxMKDPlusBrowser.check("windows") },
	mac : function() { return GnxMKDPlusBrowser.check("mac") },
	linux : function() { return GnxMKDPlusBrowser.check("linux") },
	
	windows7 : function()
	{
		return GnxMKDPlusBrowser.check("windows nt 6.1")
	}
}

function GnxMKDPlus_CheckLogin()
{
	var isLoadURL = true;
	for( var i = 0 ; i < GnxMKDPlus.arrNotLoginURL.length ; i++ )
	{
		if( location.href.toLowerCase().indexOf( GnxMKDPlus.arrNotLoginURL[i] ) == 0 )
			isLoadURL = false;
	}
	return isLoadURL;
}

function GnxMKDPlus_SetUpKeyDefense( isCheckBox, strWiseLogParam )
{
	if( typeof( strWiseLogParam ) == 'undefined' )
		strWiseLogParam = "";
		
	if( GnxMKDPlusBrowser.windows7() )
	{
		alert( "ÁØºñÁßÀÔ´Ï´Ù." );
		return false;
	}
		
	if( GnxMKDPlusBrowser.msie() )
	{
/*	
		if ( typeof( MKDPLUS ) != "object" )
		{
			alert("Àá½Ã ÇØ´ç ±â´ÉÀ» »ç¿ëÇÒ ¼ö ¾ø½À´Ï´Ù.");
			return false;
		}
*/		
		if( typeof( document.forms == "object" ) && ( document.forms.length > 0 ) && typeof( document.forms[0].NxCheckKeyDefense ) == "object" )
		{
			var isChecked = isCheckBox ? document.forms[0].NxCheckKeyDefense.checked : !document.forms[0].NxCheckKeyDefense.checked;

			if( isChecked )
			{
				if( GnxMKDPlusBrowser.windows7() )
				{
					alert( "ÁØºñÁßÀÔ´Ï´Ù." );
					GnxMKDPlus.SetUseKeyDefense( false );
					document.forms[0].NxCheckKeyDefense.checked = false;
					
					return false;
				}
				else
				{
					if( !mkdplus_installed() || GnxMKDPlus_Util_GetKeyDefenseSetUp() != "1" )
						window.open( GnxMKDPlus.strKeyDefenseURL + strWiseLogParam, 'SetupKeyDefense', 'width=385,height=485,toolbar=no,status=no,directories=no,scrollbars=no,location=no,resizable=no,menubar=no'); 
					else
						GnxMKDPlus.SetUseKeyDefense( true );
				}
			}
			else
			{
				GnxMKDPlus.SetUseKeyDefense( false );
			}
		}
	}
	else
	{
		alert('ÇØ´ç ±â´ÉÀ» Á¦°øÇÏÁö ¾Ê´Â ºê¶ó¿ìÀú ÀÔ´Ï´Ù.');
		return false;
	}
	
	return false;
}

function GnxMKDPlus_SetUpFireWall( isCheckBox, strWiseLogParam )
{
	if( typeof( strWiseLogParam ) == 'undefined' )
		strWiseLogParam = "";
	
	if( GnxMKDPlusBrowser.msie() )
	{
/*	
		if ( typeof( MKDPLUS ) != "object" )
		{
			alert("Àá½Ã ÇØ´ç ±â´ÉÀ» »ç¿ëÇÒ ¼ö ¾ø½À´Ï´Ù.");
			return false;
		}
*/	
		if( GnxMKDPlusBrowser.windows7() )
		{
			alert( "ÁØºñÁßÀÔ´Ï´Ù." );
			return false;
		}
		
		if( typeof( document.forms == "object" ) && ( document.forms.length > 0 ) && typeof( document.forms[0].NxCheckFireWall ) == "object" )
		{
			var isChecked = isCheckBox ? document.forms[0].NxCheckFireWall.checked : !document.forms[0].NxCheckFireWall.checked;

			if( isChecked )
			{
				if( GnxMKDPlusBrowser.windows7() )
				{
					alert( "ÁØºñÁßÀÔ´Ï´Ù." );
					GnxMKDPlus.SetUseFireWall( false );
					document.forms[0].NxCheckFireWall.checked = false;
					
					return false;
				}
				else
				{
					if( !mkdplus_installed() || GnxMKDPlus_Util_GetFireWallSetUp() != "1" )
						window.open( GnxMKDPlus.strFireWallURL + strWiseLogParam, 'SetupFireWall', 'width=385,height=485,toolbar=no,status=no,directories=no,scrollbars=no,location=no,resizable=no,menubar=no'); 
					else
						GnxMKDPlus.SetUseFireWall( true );
				}
			}
			else
			{
				GnxMKDPlus.SetUseFireWall( false );
			}
		}
	}
	else
	{
		alert('ÇØ´ç ±â´ÉÀ» Á¦°øÇÏÁö ¾Ê´Â ºê¶ó¿ìÀú ÀÔ´Ï´Ù.');
		return false;
	}
	
	return false;
}

function GnxMKDPlus_CreateObject()
{

	var isWriteObject = false; 
	
	var strKeyDefenseURL = GnxMKDPlus.strKeyDefenseURL.toLowerCase();
	var strFireWallURL = GnxMKDPlus.strFireWallURL.toLowerCase();
	
	if ( strKeyDefenseURL.indexOf( "../../" ) != -1 )
		strKeyDefenseURL = strKeyDefenseURL.replace("../../","/");
		
	if ( strFireWallURL.indexOf( "../../" ) != -1 )
		strFireWallURL = strFireWallURL.replace("../../","/");
	
	if( location.href.toLowerCase().indexOf( strKeyDefenseURL ) != -1 && GnxMKDPlus_Util_GetKeyDefense() == "1" )
	{
		if( !GnxMKDPlusBrowser.windows7() )
			isWriteObject = true;
	}
	else if( location.href.toLowerCase().indexOf( strFireWallURL ) != -1 && GnxMKDPlus_Util_GetFireWall() == "1" )
	{
		if( !GnxMKDPlusBrowser.windows7()  )
			isWriteObject = true;
	}
	else if( mkdplus_installed() && location.href.toLowerCase().indexOf( strKeyDefenseURL ) == -1 && location.href.toLowerCase().indexOf( strFireWallURL ) == -1 ) 
		isWriteObject = true;

	// ActiveX °¡ ¼³Ä¡µÇ¾î ÀÖÁö ¾Ê°í ¼³Ä¡ ÆäÀÌÁö¸é¼­ KeyDefense ¸¦ ¼³Ä¡ÇÏ°Ú´Ù°í ¼±ÅÃÇÑ °æ¿ì ÀÌ°Å³ª
	// ActiveX °¡ ¼³Ä¡µÇ¾î ÀÖÁö ¾Ê°í ¼³Ä¡ ÆäÀÌÁö¸é¼­ FireWall ¸¦ ¼³Ä¡ÇÏ°Ú´Ù°í ¼±ÅÃÇÑ °æ¿ì ÀÌ°Å³ª
	// ActiveX °¡ ¼³Ä¡µÇ¾î ÀÖ°í KeyDefense ¸¦ ¼³Ä¡ÇÏ°Ú´Ù°í ¼±ÅÃÇÑ °æ¿ì ÀÌ°Å³ª
	if( isWriteObject )
	{
		if( location.href.toLowerCase().indexOf( "https://" ) == 0 )
			mkdplus_ssl_write_object();
		else
			mkdplus_write_object();
		
		if( mkdplus_installed() ) 
		{
			GnxMKDPlus.Load();
		}
		else if( location.href.toLowerCase().indexOf( strKeyDefenseURL ) == -1 || location.href.toLowerCase().indexOf( strFireWallURL ) == -1 ) 
		{
			window.setTimeout( loading_check, 1000 );
		}
	}
	
	if( !mkdplus_installed() )
	{
		GnxMKDPlus_Util_SetKeyDefenseSetUp( false );
		GnxMKDPlus_Util_SetFireWallSetUp( false );
	}
}

function loading_check()
{
	if( mkdplus_loaded() == false )
	{
		window.setTimeout( loading_check, 1000 );
	}
	else
	{
		GnxMKDPlus.Load();	
	}
}

function GnxMKDPlus_Load()
{
	if( GnxMKDPlus.CheckLogin() )
	{
		if( mkdplus_installed() == true )
		{
			if( GnxMKDPlus_Util_GetKeyDefense() == "1" && typeof( document.forms == "object" ) && ( document.forms.length > 0 ) && typeof( document.forms[0].NxCheckKeyDefense ) == "object" )
			{
				document.forms[0].NxCheckKeyDefense.checked = true;
				document.forms[0].Defense_IMG.src = 'http://s.nx.com/s2/game/mabinogi/c3/btn/chkbox_on.gif';
			}
			if( GnxMKDPlus_Util_GetFireWall() == "1" && typeof( document.forms == "object" ) && ( document.forms.length > 0 ) && typeof( document.forms[0].NxCheckFireWall ) == "object" )
			{
				document.forms[0].NxCheckFireWall.checked = true;
				document.forms[0].Fire_IMG.src = 'http://s.nx.com/s2/game/mabinogi/c3/btn/chkbox_on.gif';
			}
			
			if( !GnxMKDPlusBrowser.windows7() )
				mkdplus_set_authprefix( 'nexon' );
				
			if( GnxMKDPlus_Util_GetKeyDefense() == "1" && GnxMKDPlus_Util_GetFireWall() == "1" )
			{
				if(  GnxMKDPlusBrowser.windows7() )
				{
					if( typeof( document.forms == "object" ) && ( document.forms.length > 0 ) && typeof( document.forms[0].NxCheckKeyDefense ) == "object" )
						document.forms[0].NxCheckKeyDefense.checked = false;
						
					if( typeof( document.forms == "object" ) && ( document.forms.length > 0 ) && typeof( document.forms[0].NxCheckFireWall ) == "object" )
						document.forms[0].NxCheckFireWall.checked = false;
					
					GnxMKDPlus.isKeyDefenseUse = false;
					GnxMKDPlus.isFireWallUse = false;
				}
				else
				{
					GnxMKDPlus.isKeyDefenseUse = true;
					GnxMKDPlus.isFireWallUse = true;
					mkdplus_start( 'mf/mkd' );
				}
			}
			else if( GnxMKDPlus_Util_GetKeyDefense() == "1" )
			{
				if(  GnxMKDPlusBrowser.windows7() )
				{
					if( typeof( document.forms == "object" ) && ( document.forms.length > 0 ) && typeof( document.forms[0].NxCheckKeyDefense ) == "object" )
						document.forms[0].NxCheckKeyDefense.checked = false;
						
					GnxMKDPlus.isKeyDefenseUse = false;
				}
				else
				{
					GnxMKDPlus.isKeyDefenseUse = true;
					mkdplus_start( 'mkd' );
				}
			}
			else if( GnxMKDPlus_Util_GetFireWall() == "1" )
			{
				if(  GnxMKDPlusBrowser.windows7()  )
				{
					if( typeof( document.forms == "object" ) && ( document.forms.length > 0 ) && typeof( document.forms[0].NxCheckFireWall ) == "object" )
						document.forms[0].NxCheckFireWall.checked = false;
					
					GnxMKDPlus.isFireWallUse = false;
				}
				else
				{
					GnxMKDPlus.isFireWallUse = true;
					mkdplus_start( 'mf' );
				}
			}
		}
		else
		{
			var strKeyDefenseURL = GnxMKDPlus.strKeyDefenseURL.toLowerCase();
			var strFireWallURL = GnxMKDPlus.strFireWallURL.toLowerCase();
			
			if ( strKeyDefenseURL.indexOf( "../../" ) != -1 )
				strKeyDefenseURL = strKeyDefenseURL.replace("../../","/");
				
			if ( strFireWallURL.indexOf( "../../" ) != -1 )
				strFireWallURL = strFireWallURL.replace("../../","/");
	
			if( ( GnxMKDPlus_Util_GetKeyDefense() == "1" || GnxMKDPlus_Util_GetFireWall() == "1" ) && 
			location.href.toLowerCase().indexOf( strKeyDefenseURL ) == -1
			&& location.href.toLowerCase().indexOf( strFireWallURL ) == -1 )
			{
				GnxMKDPlus_Util_SetKeyDefense( false );
				GnxMKDPlus_Util_SetFireWall( false );
			}
		}
	}
}

function GnxMKDPlus_RaiseError()
{
	//alert( "MKDPlus ¼³Ä¡µÇ¾î ÀÖÁö ¾ÊÀ½" );
	//GnxMKDPlus_Util_SetKeyDefenseSetUp( false );
	//GnxMKDPlus_Util_SetFireWallSetUp( false );
}

function GnxMKDPlus_Init()
{
	if( GnxMKDPlusBrowser.msie() && !GnxMKDPlusBrowser.windows7() )
	{
		if( GnxMKDPlusBrowser.windows7()  )
		{
			GnxMKDPlus_Util_SetKeyDefense( false );
			GnxMKDPlus_Util_SetFireWall( false );
		}
		else
		{
			document.write( "<script type='text/javascript' FOR='MKDPLUS' EVENT='MKD20Event( event, param1, param2 )'>" );
			document.write( "	GnxMKDPlus.MKD20Event( event, param1, param2 );" );
			document.write( "</script>" );

			document.write( "<script type='text/javascript' FOR='MKDPLUS' EVENT='onerror'>" );
			document.write( "	GnxMKDPlus.RaiseError();" );
			document.write( "</script>" );

			// ActiveX °¡ ¼³Ä¡µÇ¾î ÀÖÁö ¾Ê°í ¼³Ä¡ ÆäÀÌÁö¸é¼­ KeyDefense ¸¦ ¼³Ä¡ÇÏ°Ú´Ù°í ¼±ÅÃÇÑ °æ¿ì ÀÌ°Å³ª
			// ActiveX °¡ ¼³Ä¡µÇ¾î ÀÖÁö ¾Ê°í ¼³Ä¡ ÆäÀÌÁö¸é¼­ FireWall ¸¦ ¼³Ä¡ÇÏ°Ú´Ù°í ¼±ÅÃÇÑ °æ¿ì ÀÌ°Å³ª
			// ActiveX °¡ ¼³Ä¡µÇ¾î ÀÖ°í KeyDefense ¸¦ ¼³Ä¡ÇÏ°Ú´Ù°í ¼±ÅÃÇÑ °æ¿ì ÀÌ°Å³ª
			GnxMKDPlus.CreateObject();
		}
	}
}

function GnxMKDPlus_MKD20Event( event, param1, param2 )
{
	if( event == "start" )
	{
		if( GnxMKDPlus.isKeyDefenseUse )
			GnxMKDPlus_Util_SetKeyDefenseSetUp( true );
			
		if( GnxMKDPlus.isFireWallUse )
			GnxMKDPlus_Util_SetFireWallSetUp( true );
			
		var strKeyDefenseURL = GnxMKDPlus.strKeyDefenseURL.toLowerCase();
		var strFireWallURL = GnxMKDPlus.strFireWallURL.toLowerCase();
		
		if ( strKeyDefenseURL.indexOf( "../../" ) != -1 )
			strKeyDefenseURL = strKeyDefenseURL.replace("../../","/");
			
		if ( strFireWallURL.indexOf( "../../" ) != -1 )
			strFireWallURL = strFireWallURL.replace("../../","/");
		
		if( location.href.toLowerCase().indexOf( strKeyDefenseURL ) != -1 || location.href.toLowerCase().indexOf( strFireWallURL ) != -1 )
		{
			opener.location.href = opener.location.href;
			self.close();
		}
	}
	else if( event == "third_party_driver_need_update" )
	{
		var msg = 'ÀÌ PC´Â Å°º¸µå µå¶óÀÌ¹ö ¹öÀüÀÌ ³·¾Æ AhnLab MyKeydefense 2.0 À» »ç¿ëÇÒ ¼ö ¾ø½À´Ï´Ù.\n';
		msg += 'µå¶óÀÌ¹ö ¾÷µ¥ÀÌÆ® ÈÄ ´Ù½Ã Á¢¼ÓÇÏ½Ã±â ¹Ù¶ø´Ï´Ù.\nµå¶óÀÌ¹ö ´Ù¿î·Îµå ÆäÀÌÁö·Î ÀÌµ¿ÇÕ´Ï´Ù.';
		alert(msg);
		window.parent.location.href = param1;
	}
}

// Text Box ÀÇ °ªÀ» °¡Áö°í ¿À´Â ÇÔ¼ö. 
// GnxMKDPlus_GetText( form name, tag name ) -- ÅÂ±×¿¡ name ¼Ó¼ºÀ¸·Î ¼±¾ðµÈ °Í. ( id ¾Æ´Ô. )

function GnxMKDPlus_GetText( strFormName, strInputboxName )
{
	if( typeof( MKDPLUS ) == "object" && GnxMKDPlus.isKeyDefenseUse )
	{
		var strText = MKDPLUS.GetText( strFormName, strInputboxName );
		return strText;
	}
	else
	{
		return eval( "document.forms[ '" + strFormName + "' ]." + strInputboxName + ".value" );
	}
}

function GnxMKDPlus_SetUseKeyDefense( isUse )
{
	GnxMKDPlus_Util_SetKeyDefense( isUse );
	location.reload();
}

function GnxMKDPlus_SetUseFireWall( isUse )
{
	GnxMKDPlus_Util_SetFireWall( isUse );
	location.reload();
}

function GnxMKDPlus_Util_SetKeyDefense( isUse )
{
	var _use_KeyDefense = getCookie( "isUseMKDPlus" );
	if( _use_KeyDefense.length != 7 )
		_use_KeyDefense = "0_0_0_0";
		
	_use_KeyDefense = ( isUse ? "1_" : "0_" ) + _use_KeyDefense.substring( 2, 7 );
	setCookie_Permanent( "isUseMKDPlus", _use_KeyDefense );
}

function GnxMKDPlus_Util_GetKeyDefense()
{
	var _use_KeyDefense = getCookie( "isUseMKDPlus" );
	return _use_KeyDefense.substring( 0, 1 );
}

function GnxMKDPlus_Util_SetKeyDefenseSetUp( isUse )
{
	var _use_KeyDefense = getCookie( "isUseMKDPlus" );
	if( _use_KeyDefense.length != 7 )
		_use_KeyDefense = "0_0_0_0";
		
	_use_KeyDefense = _use_KeyDefense.substring( 0, 1 ) + ( isUse ? "_1" : "_0" ) + _use_KeyDefense.substring( 3, 7 );
	setCookie_Permanent( "isUseMKDPlus", _use_KeyDefense );
}

function GnxMKDPlus_Util_GetKeyDefenseSetUp()
{
	var _use_KeyDefense = getCookie( "isUseMKDPlus" );
	return _use_KeyDefense.substring( 2, 3 );
}

function GnxMKDPlus_Util_SetFireWall( isUse )
{
	var _use_myfirewall = getCookie( "isUseMKDPlus" );
	if( _use_myfirewall.length != 7 )
		_use_myfirewall = "0_0_0_0";
		
	_use_myfirewall = _use_myfirewall.substring( 0, 4 ) + ( isUse ? "1_" : "0_" ) + _use_myfirewall.substring( 6, 7 );
	setCookie_Permanent( "isUseMKDPlus", _use_myfirewall );
}

function GnxMKDPlus_Util_GetFireWall()
{
	var _use_myfirewall = getCookie( "isUseMKDPlus" );
	return _use_myfirewall.substring( 4, 5 );
}

function GnxMKDPlus_Util_SetFireWallSetUp( isUse )
{
	var _use_myfirewall = getCookie( "isUseMKDPlus" );
	if( _use_myfirewall.length != 7 )
		_use_myfirewall = "0_0_0_0";
		
	_use_myfirewall = _use_myfirewall.substring( 0, 5 ) + ( isUse ? "_1" : "_0" );
	setCookie_Permanent( "isUseMKDPlus", _use_myfirewall );
}

function GnxMKDPlus_Util_GetFireWallSetUp()
{
	var _use_myfirewall = getCookie( "isUseMKDPlus" );
	return _use_myfirewall.substring( 6, 7 );
}

// Singleton object »ý¼º
var GnxMKDPlus = new __GnxMKDPlus();
GnxMKDPlus.Init();

