/*
 * reg_union.js
 * @注册页面&LP统一跟踪一
 */
 
 if(typeof $ != "function"){function $(obj){return document.getElementById(obj);}}
/*Cookie操作*/
var oCookie = {
	
	domain : "koramgame.com",
	surl : location.href,
	
	//判断url来源类别
	urltype:function(){
		if(this.surl.search(/in\.[^\.]+\.koramgame\.com/i) != -1 
				|| this.surl.search(/in\.17sanguo\.com/i) != -1
				|| this.surl.search(/in\.17dnb\.cn/i) != -1
				|| this.surl.search(/in\.mmonweb\.com/i) != -1
			    || this.surl.search(/in\.17kunlun\.com/i) != -1
				|| this.surl.search(/in\.mhsgz\.com/i) != -1
				|| this.surl.search(/hero\.klonweb\.cn/i) != -1
				|| this.surl.search(/wx\.klonweb\.com\.cn/i) != -1
				|| this.surl.search(/[^\.]+\.2966\.com/i) != -1){
			return "landpage";
		}else if(this.surl.search(/www\.koramgame\.com/i) != -1){
			return "kunlun";
		}else{
			return null;
		}
	},
	
	K_GetCookieVal:function(offset){
	 	var endstr = document.cookie.indexOf (";", offset);
	 	if (endstr == -1)endstr = document.cookie.length;
	 	return unescape(document.cookie.substring(offset, endstr));
	},
	
	K_SetCookie:function(name, value){
	 	var expdate = new Date();
	 	var argv = this.K_SetCookie.arguments;
	 	var argc = this.K_SetCookie.arguments.length;
	 	var expires = (argc > 2) ? argv[2] : null;
	 	var path = (argc > 3) ? argv[3] : '/';
	 	this.domain = (argc > 4) ? argv[4] : this.domain;
	 	var secure = (argc > 5) ? argv[5] : false;
	 	if(expires!=null) expdate.setTime(expdate.getTime() + ( expires * 1000 ));
	 	document.cookie = name + "=" + escape (value)
		 	+((expires == null) ? "" : ("; expires="+ expdate.toGMTString()))
		 	+((path == null) ? "/" : ("; path=" + path))
		 	+("; domain=" + this.domain) 
		 	+((secure == true) ? "; secure" : "");
	},
	
	K_DelCookie:function(name){
	 	var exp = new Date();
	 	exp.setTime (exp.getTime() - 1);
	 	var cval = this.K_GetCookie (name);
	 	document.cookie = name + "=" + cval + "; expires="+ exp.toGMTString();
	},
	
	K_GetCookie:function(name){
	 	var arg = name + "=";
	 	var alen = arg.length;
	 	var clen = document.cookie.length;
	 	var i = 0;
	 	while (clen>i){
	 		var j = i + alen;
	 		if (document.cookie.substring(i, j) == arg)
	 			return this.K_GetCookieVal(j);
	 		i = document.cookie.indexOf(" ", i) + 1;
	 		if (i == 0) break;
	 	}
	 	return null;
	},
	
	getPara:function(strname){
		var hrefstr,pos,parastr,para,tempstr;
		var hrefstr = this.surl;
		var pos = hrefstr.indexOf("?")
		var parastr = hrefstr.substring(pos+1);
		var para = parastr.split("&");
		var tempstr = "";
		for(i=0;i<para.length;i++)
		{
			var tempstr = para[i];
			var pos = tempstr.indexOf("=");
	
			if(tempstr.substring(0,pos) == strname)
			{
				return tempstr.substring(pos+1);
			}
		}
	
		return null;
	
	},
	
	//注册成功后要改写cookie
	afterReg:function(){
		return;//取消掉此方法
		var uni_id_old = this.K_GetCookie("u"); //得到原联盟ID
		if(uni_id_old == 9) {
			var uni_id_old = this.K_GetCookie("u2");
		}
		this.K_SetCookie('u','9',12*30*24*3600);
		this.K_SetCookie('u2',uni_id_old,12*30*24*3600);
	},
	
	//landpage注册提交前要执行此方法,修改默认值
	getPar:function(){

		var uni_id  = this.K_GetCookie('u');
		var uni2_id = this.K_GetCookie('u2');
		//alert(uni_id+":"+uni2_id);
		if(uni2_id == "null")uni2_id = null;
		
		//LP没有u&u2则统一取32,1
		var para_u  = 32;
		var para_u2 = 1;

		if(uni_id == null && uni2_id == null) {
		//	alert(para_u);
		//	alert(para_u2);
			this.K_SetCookie('u',para_u,12*30*24*3600);
			this.K_SetCookie('u2',para_u2,12*30*24*3600);
			$("uuu0").value = para_u;
			$("uuu1").value = para_u2;
		} else {
			//	alert(uni_id);
			$("uuu0").value = uni_id;
			$("uuu1").value = uni2_id;
		}
	},
	
	//如果为landpage则要写日志文件
	writeLog:function(){
		this.getPar();
		var uni_id  = this.K_GetCookie('u');
		var uni2_id = this.K_GetCookie('u2');
		var sguni_log_url = "http://unigg.17sanguo.com/adsite/sguni_reg.php?u="+uni_id+"&u2="+uni2_id;
		
		try{
		    new Image().src=sguni_log_url;
	    } catch(e){return;}
	},
	
	//debug
	msg:function(str){
		alert(str);
	}
	
}

//得到联盟ID及子站ID
u = oCookie.K_GetCookie('u');
u2 = oCookie.K_GetCookie('u2');
if(u2 == "null")u2 = null;
utype = oCookie.urltype();

if(u != null && u2 != null){
	//u&u2均存在,则更改表单对应值
	if(utype == "landpage"){
		$("uuu0").value = u;
		$("uuu1").value = u2;
	}else if(utype == "kunlun"){
		//$("union_id").value = u;
		//$("child_union_id").value = u2;
		//document.getElementsByName("union_id")[0].value = u;
		//document.getElementsByName("child_union_id")[0].value = u2;
	}
}else if(u != null && u2 == null){
	//u存在u2为不存在置u2为null
	oCookie.K_SetCookie('u2',null,12*30*24*3600);
	//2966
	oCookie.K_SetCookie('u2',null,12*30*24*3600,'/','2966.com');
	//other
	oCookieOther.K_SetCookie('u2',null,12*30*24*3600,'/',oCookieOther.getDomain());
}else if(u == null && u2 == null){
	switch(utype){
		case 'landpage':
			//来自landpage,均不存在,置为32:1
			oCookie.K_SetCookie('u',32,12*30*24*3600);
			oCookie.K_SetCookie('u2',1,12*30*24*3600);
			//2966
			oCookie.K_SetCookie('u',32,12*30*24*3600,'/','2966.com');
			oCookie.K_SetCookie('u2',1,12*30*24*3600,'/','2966.com');
			//other
			oCookieOther.K_SetCookie('u',32,12*30*24*3600,'/',oCookieOther.getDomain());
			oCookieOther.K_SetCookie('u2',1,12*30*24*3600,'/',oCookieOther.getDomain());
			break;
		case 'kunlun'://不使用！！！！！
			//来自官网,均不存在,置为2:"类别"
			gametype = oCookie.getPara('gtype');
			oCookie.K_SetCookie('u',2,12*30*24*3600);
			oCookie.K_SetCookie('u2',gametype == null ? 1 : gametype,12*30*24*3600);
			break;
		default:
			//不做处理
			break;	
	}
}

//如果为landpage需要写日志
//if(oCookie.urltype() == "landpage"){oCookie.writeLog();}

