var fromurl="";
try {fromurl=top.document.referrer;} catch(err) {fromurl="";} finally {fromurl=(fromurl=="")?document.referrer:fromurl;}
var resource="";
resource=document.URL;
var charset = document.charset

var subsecs = 0;
var bwlastvdays = 0;
var timestamp = new Date().getTime()/1000;

function cookie_enabled(){
	var cookieEnabled=(navigator.cookieEnabled)? true : false//判断cookie是否开启
	//如果浏览器不是ie4+或ns6+
	if (typeof navigator.cookieEnabled=="undefined" && !cookieEnabled){ 
		document.cookie="testcookie"
		cookieEnabled=(document.cookie=="testcookie")? true : false
		document.cookie="" //erase dummy value
	}
	return cookieEnabled;
}

function get_guid() 
{
	var name="guid";
    var value =Math.round(Math.random()*2147483647);
	var hours=10000;
	WriteCookie(name, value, hours);
	return value;
}

function ReadCookie(name)
{
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}

function WriteCookie(name, value, hours)
{
  var expire = "";
  if(hours != null)
  {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expire + "domain=;" + "path=/;";
}

ce = cookie_enabled();
if(ce===true){
	bwlasttime = Math.round(ReadCookie("bwlasttime"));
	WriteCookie('bwlasttime','',-1);
	WriteCookie('bwlasttime',timestamp,100000);

	if(bwlasttime!='' && timestamp!=''){
		bwsubsecs = Math.round(parseInt(timestamp)-parseInt(bwlasttime));
		if(bwsubsecs<=86400){
			bwlastvdays=0;
		}else{
			bwlastvdays = Math.round(bwsubsecs/86400);
			if(bwlastvdays>10){
				bwlastvdays=0;
			}
		}
	}

	lasttime = ReadCookie("lasttime");
	WriteCookie('lasttime','',-1);
	WriteCookie('lasttime',timestamp,0.25);
	if(lasttime!='' && timestamp!=''){
		subsecs = Math.round(parseInt(timestamp)-parseInt(lasttime));
		if(subsecs<=0 || subsecs>900){
			subsecs=0;
		}
	}

	var todayfirst;
	var guid;
	guid=ReadCookie("guid");

	var isnewvisiter;
	isnewvisiter=0;

	if (guid==''){
		isnewvisiter=1;
		guid = get_guid();
	}
	todayfirst = ReadCookie("todayfirst");
	if (todayfirst==''){
		todayfirst = 1;
		WriteCookie('todayfirst','false',24);
	}else{
		todayfirst = 0;
	}

	var sessionid;
    sessionid = ReadCookie("sessionid");
	if(sessionid == ""){
		sessionid =Math.round(Math.random()*2147483647);
		WriteCookie("sessionid", sessionid, null);
	}

	var novisit;
	ckresource = escape(website+resource);
	if (ReadCookie("lastpage")==ckresource){
		novisit=false;
	} else {
		WriteCookie("lastpage",ckresource,0.5);
		novisit=true;
	}

	if(novisit)
	{
		var r="?"
		+"website="+website
		+"&referer="+escape(fromurl)
		+"&resource="+escape(resource)
		+"&charset="+charset
		+"&guid="+guid
		+"&sessionid="+sessionid
		+"&isnewvisiter="+isnewvisiter
		+"&subsecs="+subsecs
		+"&bwlastvdays="+bwlastvdays
		+"&todayfirst="+todayfirst;

		try {
			if(ztid>0) r = r+"&ztid="+ztid;
		} catch (e) {
			ztid=0;
		}

		var CountUrl ='http://stat.house365.com/hf365count.php'+ r;
		document.write("<script src='" + CountUrl + "'></script>");
	}
}
