﻿var pvUrl = "http://pv.webservice.99114.com/PV/PV.aspx";

 /*  
 
JavaScript的Cookie类  
 
注：js设定cookie的时间是以毫秒为单位  
 
*/  
  
function jsCookie(){   
  
    var COOKIE_LIFE ={year:31536000,month:2592000,week:604800,day:86400,hour:3600,browser:0}   

    this.expires = 'browser';   
    this.path = null;   
    this.domain = null;   


    this.get=function(fieldName){       
  
        var regexp = window.eval("/"+fieldName+"=([\\w%,]+)(|;)/");   
        var result = document.cookie.match(regexp);   
        return (result?unescape(result[1]):null);   
    }       
  
    this.set=function(fieldName,fieldValue){   
  
        var cookie_list;   
        cookie_list = fieldName+"="+escape(fieldValue);           
        cookie_list += setExpires(this.expires);   
        cookie_list += setPath(this.path);   
        cookie_list += setDomain(this.domain);   
        document.cookie = cookie_list;   
    }   
  
    function setExpires(expires){   
  
        var tm=0;   
          if(typeof(expires)=='string'){   
            if(!COOKIE_LIFE[expires]) return "";       
            tm = COOKIE_LIFE[expires]*1000;           
        }else if(typeof(expires)=='number'){               
            tm = expires*1000;   
        }else{return "";}   
        expires = ";expires="+new Date(new Date().getTime()+ tm).toUTCString();   
        return expires;   
    }   
  
    function setPath(path){   
        if(!path) return "";   
        return (";path="+path);   
    }   
  
    function setDomain(domain){   
        if(!domain) return "";   
        return (";domain="+domain);   
    }   
  
}   

ck = new jsCookie();   
function pageView()
{
    var arrItems = new Array();
    arrItems[0] = new Array("站点统计","1","^http:\\/\\/([\\w-]+\\.)+[\\w-]+/$","http:\\/\\/([\\w-]+\\.)+[\\w-]+\\/((default)|(index))\\.+[\\w]+$");
    arrItems[1] = new Array("免费会员","2","http:\\/\\/free\\.([\\w-]+\\.)+[\\w-]+/(index)+\\/([0-9]+)\\.shtml@$3");
    arrItems[2] = new Array("商机统计","3","http:\\/\\/free\\.([\\w-]+\\.)+[\\w-]+/((supplydetail)|(buydetail))+\\/[0-9\\/?]+\\/([0-9\\/?]+)\\.shtml@$5"
                            ,"http:\\/\\/([\\w-]+\\.)+[\\w-]+/((supply)|(buy)|(information))+\\/detail\\.shtml\\?+id\\=([0-9]+).*@$6"
                            ,"http:\\/\\/([\\w-]+\\.)+[\\w-]+/((supply)|(buy)|(information))\\/detail_(\\d+)+(_.*)*\\.shtml@$6"
                            ,"http:\\/\\/([\\w-]+\\.)+[\\w-]+/((supplydetail)|(buydetail)|(businessdetail))+\\.shtml\\?+id\\=([0-9]+)@$6"
                            ,"http:\\/\\/([\\w-]+\\.)+[\\w-]+/((supplydetail)|(buydetail)|(businessdetail))+_([\\d]+)\\.shtml@$6"
                            );
    arrItems[3] = new Array("资讯统计","4","http:\\/\\/([\\w-]+\\.)+[\\w-]+\\/(ArticleDetail)+\\.shtml\\?id\\=([0-9]+).*@$3"
                                          ,"http:\\/\\/([\\w-]+\\.)+[\\w-]+\\/((article)\\d*|(tool))+\\/detail\\.shtml\\?+id\\=([0-9]+).*@$5"
                                          ,"http:\\/\\/([\\w-]+\\.)+[\\w-]+\\/(ArticleDetail)+_([\\d]+)\\.shtml@$3"
                                          ,"http:\\/\\/([\\w-]+\\.)+[\\w-]+/((article)\\d*|(tool))\\/detail_(\\d+)+(_.*)*\\.shtml@$5"
                                          );
    arrItems[4] = new Array("其它统计","10","http:\\/\\/([\\w-]+\\.)+[\\w-]+\\/[\\w-\.]+","http:\\/\\/([\\w-]+\\.)+[\\w-]+\\/[\\w]+\\.[\\w]+");
    this.arrItem = arrItems;//数组    
    this.sourceID =1;
    this.sourceEnum = 10;
    this.urlFull  = window.location.host +  window.location.pathname ;
    this.urlSearch = "";
    this.urlAction = pvUrl;
    
  
    if(window.location.search)
    {
        this.urlSearch=window.location.search;
        this.urlFull =  this.urlFull +  window.location.search;
     }
    this.setSource(window.location.protocol +"//"+this.urlFull);
    
}


pageView.prototype.save=function(){
    //   this.print();
    //   return;
var key = this.getKey();
    if(!ck.get(key))
    {
         this.print();
         ck.set(key,1);
    }
}
pageView.prototype.getKey=function(){

var cookieKey = "";

    switch(parseInt(this.sourceEnum))
    {
        case 1:
            cookieKey = "S_"+this.fillterName(window.location.host);
            break;
        case 2:
            cookieKey = "F_"+this.sourceID;
            break;
        case 3:
            cookieKey = "P_"+this.sourceID;
            break;
        case 4:
            cookieKey = "A_"+this.sourceID;
            break;   
        case 10:
        default:
            cookieKey = "U_"+this.fillterName(window.location.pathname);
            break;    
    }
    return cookieKey;
}

pageView.prototype.fillterName=function(url){
 url= url.replace(/[\W0-9]/ig,"")
 url = url.replace("aspx","");
 return url;
}
    
pageView.prototype.print=function()
{

    var jsScript = "<script src=\"{Page}?SourceID={SourceID}&SourceEnum={SourceEnum}&Title={Title}&URL={URL}&URLFull={URLFull}&URLPath={URLPath}&URLSearch={URLSearch}&BrowerEnum={BrowerEnum}&Referrer={Referrer}\"></script>";

    jsScript = jsScript.replace("{Page}",this.urlAction);
    jsScript = jsScript.replace("{SourceID}",this.sourceID);
    jsScript = jsScript.replace("{SourceEnum}",this.sourceEnum);
    jsScript = jsScript.replace("{Title}",encodeURIComponent(document.title));
    jsScript = jsScript.replace("{URL}",encodeURIComponent(window.location.host));
    jsScript = jsScript.replace("{URLFull}",encodeURIComponent(this.urlFull));
    jsScript = jsScript.replace("{URLPath}",encodeURIComponent(window.location.pathname));
    jsScript = jsScript.replace("{URLSearch}",encodeURIComponent(this.urlSearch));
    jsScript = jsScript.replace("{BrowerEnum}",this.browseVersion());
    jsScript = jsScript.replace("{Referrer}",encodeURIComponent(document.referrer));

    //alert(jsScript);
    document.write(jsScript);

}
pageView.prototype.browseVersion=function()
{
        var Sys = {};
        var ua = navigator.userAgent.toLowerCase();
        if (window.ActiveXObject)
            Sys.ie = ua.match(/msie ([\d.]+)/)[1]
        else if (document.getBoxObjectFor)
            Sys.firefox = ua.match(/firefox\/([\d.]+)/)[1]
        else if (window.MessageEvent && !document.getBoxObjectFor)
            Sys.chrome = ua.match(/chrome\/([\d.]+)/)[1]
        else if (window.opera)
            Sys.opera = ua.match(/opera.([\d.]+)/)[1]
        else if (window.openDatabase)
            Sys.safari = ua.match(/version\/([\d.]+)/)[1];
        if(Sys.ie) return 1;
        if(Sys.firefox) return 2;
        if(Sys.opera) return 3;
        return 4;
        
}
pageView.prototype.setSource=function(url)
{
    for(var i = 0 ; i < this.arrItem.length ; i++)
    {
        for(var j = 2; j < this.arrItem[i].length ; j++)
        {
            var tempReg =new RegExp(this.arrItem[i][j].split("@")[0],"ig");
            if(tempReg.test(url))
            {
                /*document.write("模式:"+ this.arrItem[i][0]);*/
                if(this.arrItem[i][j].split("@")[1])
                {
                eval("this.sourceID=" + url.replace(tempReg,this.arrItem[i][j].split("@")[1])+";");
                /*document.write( this.SourceID +"<br/>");*/
                }
                /*document.write(url + "<br/><br/>");*/
                this.sourceEnum = this.arrItem[i][1];
                return;
            }           
        }
    }
}

var p = new pageView();
p.save();