document.domain = "nate.com";

var xGlobal = {
	CookieDomain		: "nate.com",	// 도메인
	CookieMain			: "LOGIN",		// 메인 쿠키
	CookieXecureLevel	: "xlevel",		// 보안 레벨 쿠키
	CookieSaveId		: "saveid",		// 로그인 아이디 저장 여부 쿠키
	CookieLoginId		: "loginid",	// 로그인 아이디
	XecureActivexURL	: "/nate/common/secure/xecure_frame/xecure_activex_nate.html",	// CKKeyPro 동작관련 URL
	XecureBlankURL		: "/nate/common/secure/xecure_frame/xecure_blank.html",			// CKKeyPro 동작관련 빈 페이지
	XecureInstallURL	: "/nate/common/secure/install/CKKeyPro_Installer.exe"			// CKKeyPro 설치파일
 }

var xRSA = {
	evalue : 'D1E4297D787302003419A2F758BD9C79A341255031E758D85D8FDA4E4577DEA0A0FEA7408B0E11A0505791BCA4E8E8DD1CA122873318F231A621C3C971B2BBAF1668BEAE76DAED7B2A1E510EE1292FDAF09BB4C930242BCB26ADBD7762BD02AC7D99E1BA2BCDDB4C5AC15D97CC5B0E31133D2C702BE762D29EADC6A12104B46B',
	nvalue : '10001',
	
	encrypt : function(id_obj, pwd_obj, pwd_rsa_obj){
		
		try {
	    var rsa = new RSAKey();        
	    rsa.setPublic(this.evalue,this.nvalue);
	    var fullData = xCommon.getFullToday()+'|^|'+id_obj.value+'|^|'+pwd_obj.value;
	    var res = rsa.encrypt(fullData);
	    if(res) {
	        pwd_rsa_obj.value = hex2b64(res);
	        return true;
	    	}
	   	else{return false};
	    
		}catch (e) {
		    return false;
		}finally {
			pwd_obj.value = "";
		}
	},
	
	encryptE2E : function(id_obj, frm_nm, pwd_nm, pwd_rsa_obj){
		try {
		    var rsa = new RSAKey();        
		    rsa.setPublic(this.evalue,this.nvalue);
			
			var CKKey_obj =  document.CKKeyPro;
	    	
	    	if (CKKey_obj == null || typeof (CKKey_obj) == "undefined" || CKKey_obj.object == null){
				return false;
			}else{
		        CKKey_obj.E2EInitEx("rsa" , "" , xRSA.evalue, xRSA.nvalue, id_obj.value );
				var res = CKKey_obj.GetEncData("",frm_nm,pwd_nm);
			    if(res) {
			        pwd_rsa_obj.value = hex2b64(res);
			        return true;
			    	}
			   	else{return false};
			}
		} catch (e) {
		    return false;
		} finally {
			document.getElementById(pwd_nm).value = "";
		}
	}
}

var xXecurePop = {
	PopWin : null,
	PopWinURL : "/nate/common/secure/login_desc_pop.html",
	TabNo : 1 ,
	
 	openWin : function(tabno){
		if (parseInt(tabno) == 1){ // 설명 팝업
			this.TabNo = tabno;
			this.PopWin = window.open(this.PopWinURL, 'popXeWin', 'scrollbars=no, toolbar=no, location=no, status=no, menubar=no, resizable=no, , width=500, height=650');
			this.PopWin.focus();
		} else if (parseInt(tabno) == 2) {	// OTP 페이지로 이동
			window.open("https://member.nate.com/Uotp/Intro.sk");
		}
	},

	resizePop : function(W,H){
		
		var browser = navigator.userAgent.toLowerCase();
		//alert(browser);
		var ie = (browser.indexOf("msie") != -1);
		var nt = (browser.indexOf("nt") != -1);
		var ie6 = (browser.indexOf("msie 6") != -1);
		var ie7 = (browser.indexOf("msie 7") != -1);
		var ie8 = (browser.indexOf("msie 8") != -1);
		var etc = (browser.indexOf("konqueror") != -1 || browser.indexOf("safari") != -1);
		var ff = (browser.indexOf("firefox") != -1);

		if (nt){
			if (ie7){
            	window.resizeTo(W, H);
			}else if (ie6){
				window.resizeTo(W, H - 19);
			}else if (ie8){
            	window.resizeTo(W, H + 2);
			}else if (ff){
            	window.resizeTo(W - 2, H + 6);
			}else{
            	window.resizeTo(W, H - 19);
			}
		}else{
            window.resizeTo(W, H + 0);
		}
	}
}

var xXecureLayer = { 
	LayerClose : function() {
	    document.getElementById('layerXKDisabled').style.display = "none";
	    if (document.getElementById('domain').style.position != "absolute") {
	    	document.getElementById('domain').style.visibility = "visible";
	    }
	}
}

var xLogin = {
	cSaveId		: "off",
	cLoginId	: "",
	
	getCookie : function ()	{
		
		if ( (! xCommon.getSubCookie(xGlobal.CookieMain, xGlobal.CookieSaveId)) ||  xCommon.getSubCookie(xGlobal.CookieMain, xGlobal.CookieSaveId) == this.cSaveId ){
			if (! this.getOldCookie()){
				xCommon.setSubCookie(xGlobal.CookieMain, xGlobal.CookieSaveId, this.cSaveId);
				this.cSaveId = "off";
				this.cLoginId = "";
			}
		}else{
			this.cSaveId = xCommon.getSubCookie(xGlobal.CookieMain, xGlobal.CookieSaveId);
			this.cLoginId = xCommon.getSubCookie(xGlobal.CookieMain, xGlobal.CookieLoginId);
		}
	},
	
	setCookie : function(saveid_obj, id_obj, domain_obj){
		
		this.setNateId(id_obj, domain_obj);
		
		if(saveid_obj.checked == true){
			this.cSaveId = "on";
			saveid_obj.value = this.cSaveId;
			this.cLoginId  = id_obj.value;
		}else{
			this.cSaveId = "off";
			saveid_obj.value = this.cSaveId;
			this.cLoginId = "";
		}
		
		xCommon.setSubCookie(xGlobal.CookieMain, xGlobal.CookieSaveId, this.cSaveId);
		xCommon.setSubCookie(xGlobal.CookieMain, xGlobal.CookieLoginId, this.cLoginId);
		
		this.setOldCookie();
	},
	
	setNateId : function (id_obj, domain_obj) {
		if (domain_obj[domain_obj.selectedIndex].text != "nate.com" ){
			id_obj.value = id_obj.value + "@" + domain_obj[domain_obj.selectedIndex].text;
		}
	},

	setLoginForm : function (saveid_obj, id_obj, domain_obj) {
		
		this.getCookie();

		if(this.cSaveId == "on"){
			
			saveid_obj.checked = true;
			
			var loginid = "";
			var logindomain = "";
			
			var nIdx = this.cLoginId.indexOf("@");

			if (nIdx != -1) { 
				loginid = this.cLoginId.substring(0, nIdx);
				logindomain = this.cLoginId.substring(nIdx+1, this.cLoginId.length);
			} else {
				loginid = this.cLoginId;
				logindomain = "nate.com";
			}

			id_obj.value = loginid;
			
			for (i=0; i< domain_obj.length; i++){
				if (domain_obj[i].text == logindomain){
					domain_obj.selectedIndex = i;
					break;
				}
			}
			
		}else{
			saveid_obj.checked = false;
			id_obj.value = "";
		}
	},
	

	setOldCookie : function(){
		if (this.cSaveId == "on"){
			var loginid = xOldBase64.base64encode(this.cLoginId);
			xCommon.setCookie("SAVED_NATEID",loginid + "|1");
		}else{
			xCommon.setCookie("SAVED_NATEID","|0");
		}
		
		xCommon.setCookie("SSL_LOGIN","1");
	},
	
	getOldCookie : function(){
		var oldLoginId = xCommon.getCookie("SAVED_NATEID");
		
		if (oldLoginId){
			var nIdx = oldLoginId.indexOf("|");
	
			if (nIdx != -1) { 
				this.cLoginId = xOldBase64.base64decode(oldLoginId.substring(0, nIdx));
	
				if(oldLoginId.substring(nIdx+1, oldLoginId.length) == "1"){
					this.cSaveId = "on";
				}else{
					this.cSaveId = "off";
				}
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}	
	
}

var xOldBase64 = {
	base64list : 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',

	base64encode : function(s)
	{
	  var t = '', p = -6, a = 0, i = 0, v = 0, c;
	
	  while ( (i < s.length) || (p > -6) ) {
	    if ( p < 0 ) {
	      if ( i < s.length ) {
	        c = s.charCodeAt(i++);
	        v += 8;
	      } else {
	        c = 0;
	      }
	      a = ((a&255)<<8)|(c&255);
	      p += 8;
	    }
	    if ( v > 0 )
	      t += this.base64list.charAt((a>>p)&63);
	    else
	      t += this.base64list.charAt(64);
	    p -= 6;
	    v -= 6;
	  }
	  return t;
	},

	base64decode : function(s)
	{
	  var t = '', p = -8, a = 0, q = 0, c, m, n;
	
	  for( var i = 0; i < s.length; i++ ) {
	    c = this.base64list.indexOf(s.charAt(i));
	    if ( c < 0 )
	      continue;
	    a = (a<<6)|(c&63);
	    p += 6;
	    if ( p >= 0 ) {
	      c = (a>>p)&255;
	      if ( c > 0 )
	        t += String.fromCharCode(c);
	      a &= 63;
	      p -= 8;
	    }
	  }
	  return t;
	}
}

// Common 
var xCommon = {

	getCookie : function (name)	{
		var nameOfCookie  = name + "=";

		var x = 0;
		while ( x <= document.cookie.length )
		{
			var y = ( x + nameOfCookie.length );
			if ( document.cookie.substring( x, y ) == nameOfCookie )
			{
				if ((endOfCookie = document.cookie.indexOf( ";", y )) == -1 ) endOfCookie = document.cookie.length;
				return unescape (document.cookie.substring( y, endOfCookie));
			}
			x = document.cookie.indexOf( " ", x ) + 1;
			if (x == 0) break;
		}
		return;
	},

	getSubCookie : function (name, name2) {
		var CookieValue = this.getCookie(name);

		if (CookieValue == '' || CookieValue == null) { return false; }

		var x = 0;
		var y = 0;
		while ( x <= CookieValue.length && x != -1)
		{
			if (x != 0) { x = CookieValue.indexOf('&', x) + 1; }
			y = CookieValue.indexOf('=', y + 1);

			if (CookieValue.substring(x, y) == name2) {
				z = CookieValue.indexOf('&', x) + 0;
				if (z <= 0) { z = CookieValue.length; }
				return (CookieValue.substring(y + 1, z));
				break;
			}
			else {
				x = y;
			}
		}
	},

	setCookie : function (name, value, isEscape) {

		isEscape = (isEscape == undefined) ? true : false;

		if (isEscape) {
			value = escape(value);
		}

		var todayDate = new Date();
		todayDate.setDate( todayDate.getDate() + 9999 );
		document.cookie = name + "=" + value + "; domain="+ xGlobal.CookieDomain+"; path=/;expires=" + todayDate.toGMTString() + ";";
	},

	setSubCookie : function (name, name2, value) {
		var _value = this.getCookie(name);

		var _subvalue = this.getSubCookie(name, name2);

		if (_value == undefined || _value == "") {
			_value = name2 + "=" + value;
		}
		else if (_subvalue == undefined) {
			_value += "&" + name2 + "=" + value;
		}
		else {
			var CookieDic = _value.split("&");
			for (var i = 0; i < CookieDic.length; i++) {
				var CookiePair = CookieDic[i].split("=");
				if (CookiePair[0] == name2) {
					CookiePair[1] = escape(value);
				}
				CookieDic[i] = CookiePair.join("=");
			}

			_value = CookieDic.join("&");
		}

		this.setCookie(name, _value, false);
	},
	
	checkBrowser : function() {
		if (navigator.userAgent.toUpperCase().indexOf("MSIE") != -1) return "IE";
		else if (navigator.userAgent.toUpperCase().indexOf("MOZILLA") != -1) return "FF";
		else return null;
	},
			
	trim : function(str) { 
		if (!str) return ''; 
		return str.replace(/^\s*|\s*$/g, ''); 
	}, 
	
	getFullToday : function()
	{
	    var today = new Date();
	    var buf = "";
	
	    buf += today.getYear() + "y";
	    buf += (today.getMonth() + 1) + "m";
	    buf += today.getDate() + "d ";
	    buf += today.getHours() + "h";
	    buf += today.getMinutes() + "m";
	    buf += today.getSeconds() + "s";
		
		return buf;
	}
}

// Xecure
var xXecure = {
	preXlevel  : 2,
	
	getCookie : function ()	{
			var cXecureLevel = 2;

			if ( (! xCommon.getSubCookie(xGlobal.CookieMain, xGlobal.CookieXecureLevel)) ||  xCommon.getSubCookie(xGlobal.CookieMain, xGlobal.CookieXecureLevel) == cXecureLevel ){
				xCommon.setSubCookie(xGlobal.CookieMain, xGlobal.CookieXecureLevel, cXecureLevel);
			}else{
				cXecureLevel = xCommon.getSubCookie(xGlobal.CookieMain, xGlobal.CookieXecureLevel);
				
				if (! (cXecureLevel == 1 || cXecureLevel == 2 || cXecureLevel == 3)){
					cXecureLevel = 2;
					xCommon.setSubCookie(xGlobal.CookieMain, xGlobal.CookieXecureLevel, cXecureLevel);
				}
			};
			return cXecureLevel ;
	},
	
	setCookie : function (value)	{
		xCommon.setSubCookie(xGlobal.CookieMain, xGlobal.CookieXecureLevel, value);
	},
	
	checkBrowser : function ()	{
		if (xCommon.checkBrowser() == "IE")
			return true;
		else
			return false;
	},
	
	isCKKeyPro : function () {
			
		if (document.CKKeyPro == null || typeof (document.CKKeyPro) == "undefined" || document.CKKeyPro.object == null)
			return false;
		else
			return true;
	},

	installActiveX : function () {
		var ckKey = null;
		var ret = false;
	
		try {
			ckKey = new ActiveXObject("CKSKComm.CKCommInst");
		}
		catch (e) { }

		return (ckKey != null);
	},
		
	excCKKeyPro : function (preXlevel) {
		document.getElementById("XecureFrame").src=xGlobal.XecureActivexURL;
		this.preXlevel = preXlevel;
	},
	
	clearCKKeyPro : function(){
		if (document.getElementById("XecureFrame").src != xGlobal.XecureBlankURL)
			document.getElementById("XecureFrame").src = xGlobal.XecureBlankURL;
			
		if (parent.document.CKKeyPro == null || typeof (parent.document.CKKeyPro) == "undefined" || parent.document.CKKeyPro.object == null){
			return;
		}else{
			parent.document.getElementById("XecureDiv").innerHTML = "";

		try { parent.document.CKKeyPro.Clear("f_login", "XecureDiv", 0); } catch (e) { }

		}
	},

	getCKKeyProInstall : function () {
		location.href = xGlobal.XecureInstallURL;
	},
	
	getCKKeyProInstallPop : function () {
		window.open(xGlobal.XecureInstallURL, 'popInstall', 'scrollbars=no, toolbar=no, location=no, status=no, menubar=no, resizable=no, , width=0, height=0');
	},
	
	showCKKeyProPopup : function () {
		var x, y;
		x = screen.width - 2;
		y = screen.height - 45; // 43 is popup image height
		try { 
			document.CKKeyPro.ShowPopup(100, 8000, 100, 5, x, y);
		} catch (err) { }
	},
	
	hideCKKeyProPopup : function () {
		try { 
			if(document.CKKeyPro.HidePopup)
				document.CKKeyPro.HidePopup();
			} catch (err) { }
	},
	
	clearPASSWD : function() {
		parent.document.f_login.PASSWD.value="";
	},
	
	clearAutoInput : function() {
		var pwd = parent.f_login.PASSWD.value;
		if ( ! (pwd.indexOf("A") != -1 || pwd.indexOf("0") != -1 || pwd.indexOf("_") != -1 ) ) { 
			parent.f_login.PASSWD.value = "";
		}	
	
	}
}

// CKKeyPro OnKeyUp Bug Fix
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) {}
}
