﻿/*
	上海蓝速数码科技有限公司 网站访问统计系统JS v1.0
	author:追梦客
	qq:16991200
*/
(function (){
    //timeKeep : 分钟
	function SetCookie(sName, sValue, timeKeep)
	{
		var now=new Date();
		var expireTime= new Date(now.valueOf()+timeKeep*60000);
		document.cookie = sName + "=" + escape(sValue) + "; path=/; expires=" + expireTime.toGMTString() + ";";
	}
	
	function GetCookie(sName)
	{
		var aCookie = document.cookie.split("; ");
		for (var i=0; i < aCookie.length; i++)
		{
			var aCrumb = aCookie[i].split("=");
			if (sName == aCrumb[0]) 
				return unescape(aCrumb[1]);
		}
		return null;
	}
	
	function GetCurrentDateTime()
	{
		var date = new Date();
		var current=new String("");
		current += date.getFullYear()+"-";
		current += date.getMonth() + 1+"-";
		current += date.getDate()+" ";
		current += date.getHours()+":";
		current += date.getMinutes()+":";
		current += date.getSeconds();
		return current;
	}
	function GetUserCode(len)
	{
		var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
		var code = "";
		for(var i=0;i<len;i++){
			code+= str.substr(parseInt(Math.random()*str.length),1);
		}
		return code;
	}
	
	//对变量进行处理
	var user,style,charset
	try
	{
		user = dmk_user;
	}catch(ex){
		user = 0;
	}
	try
	{
		style = dmk_style;
	}catch(ex){
		style = 0;
	}
	try
	{
		charset = dmk_charset;
	}catch(ex){
		charset = "UTF-8";
	}
	
	//访问会话超时限制，默认为24小时
	var timeout;
	try
	{
	    timeout = dmk_timeout || (24 * 60);
	    timeout = timeout > 0 ? timeout : 24 * 60;
	}catch(ex){
	    timeout = 24 * 60
	}

	if(user<=0)//如果是无效用户
	{
		return;
	}
		
	//	Cookie名称
	var dmk_stats_user = "dmk_stats_user_" + user;
	var dmk_stats_online = "dmk_stats_online_" + user;
	var dmk_host_url = "http://fs.soonet.com.cn/dmkstats/log.html";	
	
	//当前时间
	var dmk_datetime = GetCurrentDateTime();
	
	// 存储判断COOKIES是否有效的值
	var user_unique = 0;
	
	// 判断是否写了进行了统计,如果没有,写入一个COOKIE,并进行计数统计
	// 每24小时统计一次
	if (GetCookie(dmk_stats_user)==null)
	{
		SetCookie(dmk_stats_user,"1",timeout);
		user_unique = 1
	}
	
	// 对流量统计进行改良测试
	if(user_unique==1)
	{
		var s = "<iframe src=\"" + dmk_host_url + "?ac=visit";
		s += "&user_unique=" + user_unique;
		s += "&user=" + user;
		s += "&dpi=" + escape(screen.width)+"*" + escape(screen.height);
		s += "&color=" + escape(screen.colordepth);
		s += "&browserLanguage=" + escape(navigator.browserLanguage);
		s += "&location=" + escape(location.href);
		s += "&refer=" + escape(document.referrer);
		s += "&platform=" + escape(navigator.platform);
		s += "&cpuClass=" + escape(navigator.cpuClass);
		s += "&datetime=" + escape(dmk_datetime);
		s += "\" style=\"display:none;width:0px;height:0px;\"></iframe>"
		document.write(s);
	}
	
	//以下对在线用户列表进行维护
    var online_unique = 0;
    
	var sessionid = GetCookie(dmk_stats_online);
	// 存储判断COOKIES是否有效的值
	if (sessionid == null)
	{
	    sessionid = GetUserCode(8);
		SetCookie(dmk_stats_online,sessionid,10);
		online_unique = 1
	}
	if(online_unique==1)
	{
		// 对流量统计进行改良测试
		var s = "<iframe src=\"" + dmk_host_url + "?ac=online";
		s += "&online_unique=" + online_unique;
		s += "&sessionid=" + escape(sessionid);
		s += "&user=" + user;
		s += "&location=" + escape(location.href);
        s += "&refer=" + escape(document.referrer);
		s += "&datetime=" + escape(dmk_datetime);
		s += "\" style=\"display:none;width:0px;height:0px;\"></iframe>";
		document.write(s);
	}
	
	if(style>0)
	{
			try{
				var id = "dmk_stats_count_" + user + "_" + style;
				var _count = GetCookie(id);
				if (_count==null)
	            {
		            var s = "" + dmk_host_url + "?ac=count";
				    s += "&user=" + user;
				    s += "&location=" + escape(location.href);
				    s += "&refer=" + escape(document.referrer);
				    s += "&style=" + escape(style);
				    s += "&datetime=" + escape(dmk_datetime);
				    s += "&charset=" + escape(charset);
				    s += "&id=" + escape(id);
				    
				    document.write("<span id=\"" + id + "_count\"></span><script type=\"text/javascript\" language=\"javascript\" id=\"" + id + "\"></script>");
				    
				    var func = window.onload;
				    window.onload = function (){
					    if(typeof(func)=="function")
					    {
						    func();
					    }
					    var _stats = document.getElementById(id);
					    _stats.src = s;
				    }
	            }else{
	                document.write("<span>" + _count + "</span>");
	            }	            
				
			}catch(ex){
				//DO NOTHING
				alert(ex);
			}
	}
})();
