function Nethru_getCookieVal(offset)
{
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function Nethru_SetCookie(name, value){
   var argv = Nethru_SetCookie.arguments;
   var argc = Nethru_SetCookie.arguments.length;
   var expires = (2 < argc) ? argv[2] : null;
   var path = (3 < argc) ? argv[3] : null;
   var domain = (4 < argc) ? argv[4] : null;
   var secure = (5 < argc) ? argv[5] : false;

  // alert("DOMAIN = " + domain);
   document.cookie = name + "=" + escape (value) +
        ((expires == null) ? "" : ("; expires="+expires.toGMTString())) +
     ((path == null) ? "" : ("; path=" + path)) +
     ((domain == null) ? "" : ("; domain=" + domain)) +
        ((secure == true) ? "; secure" : "");

//	alert(document.cookie);
}

function Nethru_GetCookie(name){
   var arg = name + "=";
   var alen = arg.length;
   var clen = document.cookie.length;
   var i = 0;
   while (i < clen)
      {
      var j = i + alen;
      if (document.cookie.substring(i, j) == arg)
         return Nethru_getCookieVal (j);
      i = document.cookie.indexOf(" ", i) + 1;
      if (i == 0)
         break;
      }
  return null;
}

function Nethru_makePersistentCookie(name,length,path,domain)
{
    var today = new Date();
    var expiredDate = new Date(2011,1,1);
    var cookie;
	var value;

    cookie = Nethru_GetCookie(name);
    if ( cookie ) {
//		alert(cookie);
        return 1;
	}

	var values = new Array();
	for ( i=0; i < length ; i++ ) {
		values[i] = "" + Math.random();
	}

	value = today.getTime();

	// use first decimal
	for ( i=0; i < length ; i++ ) {
		value += values[i].charAt(2);
	}

    Nethru_SetCookie(name,value,expiredDate,path,domain);
}

function Nethru_getDomain() {
	var _host   = document.domain;
	var so      = _host.split('.');
	var dm    = so[so.length-2] + '.' + so[so.length-1];
	return (so[so.length-1].length == 2) ? so[so.length-3] + '.' + dm : dm;
}

var Nethru_domain  = Nethru_getDomain();

Nethru_makePersistentCookie("PCID",10,"/",Nethru_domain);

// Wemade ActiveX Controller 
// ±âº»ÀÌ µÇ´Â Handling Function
function comDomHandling(){
	this.config = {
		isIE : (navigator.userAgent.toLowerCase().indexOf("msie") != -1)
	};
	this.$I = function(strId){
		return document.getElementById(strId);
	};
	this.$C = function(tag){
		return document.createElement(tag);
	};
	this.setCSS = function(oEl, strStyle) {
		if (!strStyle.charAt(strStyle.length - 1) == ';')
			return;
		if(this.config.isIE){
			var tmpStrSplit;
			tmpStrSplit = strStyle.slice(0,-1);
			tmpStrSplit = tmpStrSplit.split(";");
			for(var i = 0; i < tmpStrSplit.length; i++){
				oEl.style[tmpStrSplit[i].split(":")[0]] = tmpStrSplit[i].split(":")[1];
			}
		}else{
			oEl.setAttribute("style",strStyle);
		}
	};
	this.setAttr = function(oEl, arr){
		for(var prop in arr){
			oEl.setAttribute(prop, arr[prop]);
		}
	};
	this.dataPack = function(){
		return {
			add : function(pName){
				this[pName] = null;
			},
			remove : function(pName){
				(typeof(pName) != "function") ? delete this[pName] : "";
			},
			removeAll : function(){
				for(var pName in this)
					this.remove(pName);
			}
		}
	};
	this.setData = function(arrDpack, pName, pValue){
		arrDpack.add(pName);
		arrDpack[pName] = pValue;
	}
}

// Wemade ActiveX Controller 
comDomHandling.prototype = {
	baseData : {
		axInstallManager : {classid:"clsid:BD68328E-1222-4A62-BA16-E6F42CA49A64",id:"WMInstallMgr",codebase:"WMInstallMgr.cab#version=1,0,0,4", fileName:"WMInstallMgr.dll", viewTxt:"ActiveX Manager", fcount:"1", width:"0", height:"0"},
		weblauncher : {classid:"clsid:B8339132-E751-452b-87F5-5F3D4365638B",id:"objWeGameRun",codebase:"weGameLauncher.cab#version=1,0,2,25", fileName:"WemadeWebLauncher.dll", viewTxt:"Wemade Web Launcher", fcount:"6", width:"0", height:"0"},
		comicviewer : {classid:"clsid:D08593B1-B6F7-4A53-9E34-178E06568312",id:"WMCartoonViewer",codebase:"WMCartoonViewer.cab#version=1,0,0,22", fileName:"WMCartoonViewer.ocx", viewTxt:"Wemade Comic Viewer", fcount:"4", width:"100%", height:"100%"},
		systemobserver : {classid:"clsid:D22EEF80-9A66-4A52-8361-670EBBD5EF2B",id:"WMSysObserver",codebase:"WMSysInfo.cab#version=1,0,0,35", fileName:"WMSysInfo.ocx", viewTxt:"Wemade System Observer", fcount:"5", width:"0", height:"0"}
	},
	setAXObject : function(strName){
		var t = this;
		var b = this.baseData;
		var strActiveRoot = this.rtnCurrent();

		if(t.$I("objHiddenFrame") == undefined){
			var I = t.$C("iframe");
			t.setAttr(I,{id:"objHiddenFrame", name:"txtHiddenFrame"});
			t.setCSS(I,"width:0;height:0;position:absolute;left:-100;");
			(t.config.isIE) ? document.insertBefore(I) : document.body.appendChild(I);
		}

		var iWindow = t.$I("objHiddenFrame").contentWindow;
		iWindow.document.clear();
		iWindow.document.open("text/html", "replace");
		iWindow.document.write("<html><head>");
		iWindow.document.write("</head><body>");
		iWindow.document.write('<OBJECT classid="' + eval("b." + strName + ".classid") + '" width=0 height=0 ');
		iWindow.document.write('id="' + eval("b." + strName + ".id") + '" codebase="');
		iWindow.document.write(strActiveRoot + eval("b." + strName + ".codebase") + '" VIEWASTEXT></OBJECT>');
		iWindow.document.write("</body></html>");
		iWindow.document.close();

		// ¼³Ä¡ ¿©ºÎ ¹ÝÈ¯ ÇØÁØ´Ù.
		var tmpDataPack = t.dataPack();
		t.setData(tmpDataPack, "obj", iWindow.document.getElementById(eval("b." + strName + ".id")));
		t.setData(tmpDataPack, "bitInstall", t.getInstall(strName, iWindow.document.getElementById(eval("b." + strName + ".id"))));
		// ¸¸¾à¿¡ ¼³Ä¡ µÇ¾î ÀÖ´Ù¸é µî·ÏÀ» À§ÇØ Àá±ñ º¸¿©ÁÖ°í Áö¿öÁØ´Ù.
		if (tmpDataPack.bitInstall && strName == "axInstallManager"){
			var tmpObj = t.setDocument(strName);
		}
		return tmpDataPack;
	},
	setInstall : function(strName){
		var t = this;
		var objAXManager = t.setAXObject("axInstallManager");
		if(objAXManager.bitInstall == true){
			var tmpStrData = eval("this.baseData." + strName);
			// Version Data 
			var tmpStrVersion = tmpStrData.codebase.substring(tmpStrData.codebase.lastIndexOf("#version=") + 9).split(",");
			// AX URL 
			var tmpStrUrl = "http://" + document.location.hostname + this.rtnCurrent() + tmpStrData.codebase.substring(0, tmpStrData.codebase.indexOf("#"));

			// ActiveX ÆÄÀÏ¸í
			objAXManager.obj.szAXFileName = tmpStrData.fileName;
			objAXManager.obj.Version01 = tmpStrVersion[0];
			objAXManager.obj.Version02 = tmpStrVersion[1];
			objAXManager.obj.Version03 = tmpStrVersion[2];
			objAXManager.obj.Version04 = tmpStrVersion[3];
			objAXManager.obj.szModName = tmpStrData.viewTxt;
			objAXManager.obj.lFileCount = tmpStrData.fcount;
			objAXManager.obj.StartInstall(tmpStrUrl);
			// »ç¿ëÀ» À§ÇØ ÇØ´ç ÆäÀÌÁö¿¡ È÷µç ·¹ÀÌ¾î ÇüÅÂ·Î ¼ÂÆÃ
			return t.setDocument(strName);
		}else{
			return null;
		}
	},
	setDocument : function(strName){
		var obj = "";
		var t = this;
		var b = t.baseData;

		obj = "<OBJECT CLASSID='" + eval("b." + strName + ".classid") + "' ID='" + eval("b." + strName + ".id") + "' WIDTH='" + eval("b." + strName + ".width") + "' HEIGHT='" + eval("b." + strName + ".height") + "' VIEWASTEXT></OBJECT>" ;

		t.removeDocument(t.$I(strName+'layer'));

		var o = t.$C("div");
		t.setAttr(o, {id:strName+'layer'});
		(t.config.isIE) ? document.insertBefore(o) : document.body.appendChild(o);
		t.$I(strName+'layer').innerHTML = obj;

		return t.$I(eval("b." + strName + ".id"));
	},
	removeDocument : function(obj){
		if (obj){
			obj.parentNode.removeChild(obj);
		}
	},
	getInstall : function(strName, obj){
		var bitWork = false;
		if (obj == undefined || obj == null){
			obj = this.setDocument(strName);
		}
		switch(strName){
			case "axInstallManager" :
				try{ obj.szModName.length == 0; bitWork = true; }
				catch(ex){ bitWork = false; }
				break;
			case "weblauncher" :
				bitWork = (obj.build_date == undefined) ? false : true;
				break;
			case "comicviewer" :
				bitWork = (obj.value == null) ? false : true;
				break;
			case "systemobserver" :
				try{ obj.GetInformation(); bitWork = true; }
				catch(ex){ bitWork = false; }
				break;
			default : return false; break;
		}
		return bitWork;
	},
	rtnCurrent : function(){
		var strHrefHost = String(document.location.hostname).replace("dev.wemade.com","");
		strHrefHost = strHrefHost.replace("test.wemade.com","");
		strHrefHost = strHrefHost.replace(".wemade.com","");
		var strActiveXRoot = "/comsso/active/";
		switch (strHrefHost){
			case "cs" :
				strActiveXRoot = "/ActiveX/";
				break;
			default : 
				strActiveXRoot = "/comsso/active/";
				break;
		}
		return strActiveXRoot;
	}
};

// Wemade Cookie Control
function wmCookie(name){
	this.tmpName = name;					// ÄíÅ°¸í ÀúÀå
	var allcookies = document.cookie;		// ÄíÅ°¸¦ °¡Á®¿Â´Ù.
	if(allcookies == "") return;			// ÄíÅ°°¡ ¾øÀ¸¸é Ã¼Å© ÇÒ ºÎºÐÀÌ ¾øÀ¸´Ï ¸ØÃá´Ù.
	var cookies = allcookies.split(";");	// ÄíÅ°º° ¹è¿­È­ ÇÑ´Ù.
	var cookie = null;						// ±âº»ÀûÀ¸·Î »ç¿ëÇÒ ÄíÅ° º¯¼ö ¼±¾ð
	for (var i = 0; i < cookies.length ; i++){
		// ¹è¿­È­ÇÑ ÄíÅ° ¸¸Å­ Loop µ¹¸é¼­
		if(cookies[i].substring(0, name.length + 1) + "=" == (' ' + name + '=')){
			// ÄíÅ°¸í°ú °°Àº ÄíÅ°°¡ ÀÖÀ¸¸é ÄíÅ° º¯¼ö¿¡ ´ëÀÔÇÑ´Ù.
			cookie = cookies[i];
			break;
		}
	}

	if(cookie == null) return;	// ±âº»ÀûÀ¸·Î »ç¿ëÇÒ ÄíÅ°°¡ ÃÊ±âÈ­ »óÅÂ¸é ¸ØÃá´Ù.

	var cookieval = cookie.substring(name.length+2);	// ÄíÅ°°ªÀ» ÀúÀåÇÑ´Ù.

	var a = cookieval.split('|||');	// ±¸ºÐÀÚ¸¦ ÀÌ¿ëÇÏ¿© ¹è¿­È­ ÇÑ´Ù.
	for (var i = 0; i < a.length ; i++){
		a[i] = a[i].split(":");		// ÄíÅ°ÀÇ ¹è¿­½Ö °ªÀ» 2Â÷¿ø ¹è¿­·Î º¯È¯
	}
	for (var i = 0; i < a.length ; i++){
		if (a[i][0] != "")
			this[a[i][0]] = decodeURIComponent(a[i][1]);	// ¿¬°ü ¹è¿­ ÇüÅÂ·Î ÄíÅ°°ªÀ» ÀúÀåÇÑ´Ù.
	}
 };

 wmCookie.prototype.store = function(daysToLive, path, domain, secure){
	var cookieval = "";
	for (var prop in this){
		// tmp·Î ½ÃÀÛÇÏ´Â ¸â¹ö º¯¼ö¿Í function ¸â¹ö´Â ÄíÅ° ´ë»ó¿¡¼­ Á¦¿ÜÇÑ´Ù.
		if((prop.substring(0,3) == 'tmp') || ((typeof this[prop]) == 'function'))
			continue;
		if(cookieval != "") cookieval += '|||'; // ÄíÅ° ½ÖÀ» È®ÀÎÇÏ±â À§ÇÑ ±¸ºÐÀÚ Ãß°¡
		cookieval += prop + ":" + encodeURIComponent(this[prop]);	// ÄíÅ° °ªÀº Æ¯¼ö¹®ÀÚ ´ëºñÇÏ¿© encoding ÇØµÐ´Ù.
	}
	var cookie = this.tmpName + '=' + cookieval;	// ÄíÅ° ½Ö °ªÀ» ÀÏ°ý ÀúÀå
	if(daysToLive || daysToLive == 0){	// ÄíÅ° À¯Áö±â°£ ¼³Á¤ÀÌ µÇ¾î ÀÖ´Ù¸é ±â°£À» ¼³Á¤ÇØÁØ´Ù. (null)ÀÏ °æ¿ì¿¡´Â OneTimeÀ¸·Î »ý¼º
		var expire_date = new Date();
		expire_date.setDate(expire_date.getDate() + daysToLive);
		cookie += "; expires=" + expire_date.toGMTString()
		// cookie += "; max-age=" + (daysToLive * 24 * 60 * 60);	// max-age¸¦ ÀÌ¿ë ÃÊ´ÜÀ§ ¼³Á¤µµ °¡´ÉÇÏ´Ù.
	}
	if(path) cookie += "; path=" + path;
	if(domain) cookie += "; domain=" + domain;
	if(secure) cookie += "; secure";
	document.cookie = cookie;
 };

wmCookie.prototype.remove = function(path, domain, secure){
	// »ý¼ºÇÑ ÄíÅ° ÀüÃ¼¸¦ »èÁ¦ÇÑ´Ù.
	for (var prop in this){
		if(prop.substring(0,3) != 'tmp' && typeof this[prop] != 'function')
			delete this[prop];
	}
	this.store(0, path, domain, secure);
};
