function detectBrowser(){
    if (navigator.appName.indexOf("Opera")>=0)
        return new OperaDetect();
    else if (navigator.appName.indexOf("Netscape")>=0)
        return new NetscapeDetect();
    else if (navigator.appName.indexOf("Explorer")>=0)
        return new IEDetect();
    else 
        return new UnknownDetect();
}


function SimpleDetector(){
}

SimpleDetector.prototype.getVersion=function(){
    var tmp=navigator.appVersion;
    var ret=tmp.split(" ");
    return ret[0];
}

SimpleDetector.prototype.getBrowser=function(){
    return navigator.appName;
}


SimpleDetector.prototype.getScreenInfo=function(){
    return new ScreenInfo();
}

SimpleDetector.prototype.getCPUInfo=function(){
    return new CPUInfo();
}


SimpleDetector.prototype.getComeFrom=function(){
    return document.referrer;
}

SimpleDetector.prototype.getComeTo=function(){
    return document.location;
}

SimpleDetector.prototype.getSession=function(){
    var sessionId=null;
    var cookies=document.cookie.split("; ");
    var i;
    for (i=0;i<cookies.length;i++){
        if (cookies[i].indexOf("JSESSIONID=")>0){
            sessionId=cookies[i].substring("JSESSIONID=".length);
        }
    }
    var tmp;
    var endIdx;
    var startIdx;
    /*if (sessionId==null){
        var url=document.location.href;
        if ((startIdx=url.indexOf(";jsessionid="))>=0){
            tmp=url.substring(startIdx+";jsessionid=".length);
            var id1=tmp.indexOf("?");
            id1=id1<0?9999999:id1;
            var id2=tmp.indexOf("&");
            id2=id2<0?9999999:id2;
            endIdx=id1<id2?id1:id2;
            sessionId=endIdx==9999999?tmp:tmp.substring(0,endIdx);
        }
    }*/
    if (sessionId==null){
        if (jsessionid){
            sessionId=jsessionid;
            this.saveSession();
        }
    }
    return sessionId;
}
SimpleDetector.prototype.getDomain=function(link){
    var startIndex=link.indexOf("http://");
    var domain=link.substring(startIndex+"http://".length);
    var endIndex=domain.indexOf("/");
    if (endIndex>0){
        domain=domain.substring(0,endIndex);
    }
    return domain;
}
SimpleDetector.prototype.saveSession=function(){
    this.updateElements("A");
    this.updateElements("AREA");
}

SimpleDetector.prototype.updateElements=function(elementName){
    var elems=document.getElementsByTagName(elementName);
    var node;
    var href;
    var thisDomain=this.getDomain(document.location.href);
    var tmp;
    var startIdx,endIdx;
    for (var i=0;i<elems.length;i++){
        node=elems.item(i);
        href=node.getAttribute("href");
        if (href!=null && href.indexOf("mailto:")<0){
            if (href.indexOf(";jsessionid=")<0){
                if (href.indexOf("http://")==0){
                
                    var linkDomain=this.getDomain(href);
                    
                    if (linkDomain.indexOf(thisDomain)>=0){
                        tmp=href;
                        startIndex=tmp.indexOf("?");
                        if (startIndex>0){
                            href=tmp.substring(0,startIndex)+";jsessionid="+jsessionid+tmp.substring(startIndex);
                        } else {
                            href=href+";jsessionid="+jsessionid;
                        }
                        node.setAttribute("href",href);
                    }
                } else if (href.indexOf("://")<0 && href.indexOf("javascript:")<0){
                        tmp=href;
                        startIndex=tmp.indexOf("?");
                        if (startIndex==0){
                            href=";jsessionid="+jsessionid+href;
                        } else if (startIndex>0){
                            href=tmp.substring(0,startIndex)+";jsessionid="+jsessionid+tmp.substring(startIndex);
                        } else if (startIndex<0){
                            href=href+";jsessionid="+jsessionid;
                        }
                        //href=href+";jsessionid="+jsessionid;
                        node.setAttribute("href",href);
                }
            }
        }
    }
}
SimpleDetector.prototype.getAgentVersion=function(){
    var agent=navigator.userAgent;
    var opIdx=agent.indexOf("(");
    var cloIdx=agent.indexOf(")");
    var agInfoStr=agent.substring(opIdx+1,cloIdx);
    var agInfoStrings=agInfoStr.split("; ");
    return agInfoStrings[4].split(":")[1];
}

function ScreenInfo(){

}

ScreenInfo.prototype.getWidth=function(){
    return screen.width;
}


ScreenInfo.prototype.getHeight=function(){
    return screen.height;
}

ScreenInfo.prototype.getBpp=function(){
    return screen.pixelDepth;
}


function CPUInfo(){
    // Principe kad nerasineti kelis kartus 
    //    cia imami duomenys pagal mozilla
    var agent=navigator.userAgent;
    var opIdx=agent.indexOf("(");
    var cloIdx=agent.indexOf(")");
    var agInfoStr=agent.substring(opIdx+1,cloIdx);
    var agInfoStrings=agInfoStr.split("; ");
    var cpuStrings=agInfoStrings[2].split(" ");
    this.os=cpuStrings[0];
    this.cpu=cpuStrings[1];
}

CPUInfo.prototype.getCPUModel=function(){
    return this.cpu;
}

CPUInfo.prototype.getOs=function(){
    return this.os;
}



// ------------------------------------------Operos detektorius
function OperaDetect(){

}
OperaDetect.prototype=new SimpleDetector;

OperaDetect.prototype.getCPUInfo=function(){
    return new OperaCPUInfo();
}
OperaDetect.prototype.getAgentVersion=function(){
    return this.getVersion();
}
function OperaCPUInfo(){
    var agent=navigator.userAgent;
    var opIdx=agent.indexOf("(");
    var cloIdx=agent.indexOf(")");
    var agInfoStr=agent.substring(opIdx+1,cloIdx);
    var agInfoStrings=agInfoStr.split("; ");
    var cpuStrings=agInfoStrings[1].split(" ");
    this.os=cpuStrings[0];
    this.cpu=cpuStrings[1];
}
OperaCPUInfo.prototype=new CPUInfo;



// ----------------------------------Netskeipo ir mozilos detektorius
function NetscapeDetect(){
}
NetscapeDetect.prototype=new SimpleDetector;

NetscapeDetect.prototype.getBrowser=function(){
    var version=this.getVersion();
    if (version.indexOf(5.0)>=0){
        return "Mozilla"
    } else return "Netscape";
}






//------------------------------------Internet eksplorerio detektorius
function IEDetect(){
}
IEDetect.prototype=new SimpleDetector;

IEDetect.prototype.getBrowser=function(){
    return "Microsoft Internet Explorer";
}

IEDetect.prototype.getScreenInfo=function(){
    return new IEScreenInfo();
}

function IEScreenInfo(){
}

IEScreenInfo.prototype=new ScreenInfo;

IEScreenInfo.prototype.getBpp=function(){
    return screen.colorDepth;
}


IEDetect.prototype.getCPUInfo=function(){
    return new IECPUInfo();
}

IEDetect.prototype.getAgentVersion=function(){
    var agent=navigator.userAgent;
    var opIdx=agent.indexOf("(");
    var cloIdx=agent.indexOf(")");
    var agInfoStr=agent.substring(opIdx+1,cloIdx);
    var agInfoStrings=agInfoStr.split("; ");
    return agInfoStrings[1].split(" ")[1];
}

function IECPUInfo(){
    var agent=navigator.userAgent;
    var opIdx=agent.indexOf("(");
    var cloIdx=agent.indexOf(")");
    var agInfoStr=agent.substring(opIdx+1,cloIdx);
    var agInfoStrings=agInfoStr.split("; ");
    this.os=agInfoStrings[2];//.split(" ");
    //this.os=cpuStrings[0];
    this.cpu="x86";//cpuStrings[1];
}
IECPUInfo.prototype=new CPUInfo;



function UnknownDetect(){
}

UnknownDetect.prototype=new SimpleDetector;


var browser=detectBrowser();
var session=browser.getSession();


var cpuInfo=browser.getCPUInfo();
var screenInfo=browser.getScreenInfo();
var qString="?"+
    "domain="+domain+
    (session!=null?"&session="+session:"")+
    "&browser="+escape(browser.getBrowser())+
    "&resolution="+screenInfo.getWidth()+"x"+screenInfo.getHeight()+" "+screenInfo.getBpp()+
    "&cpu="+escape(cpuInfo.getCPUModel())+
    "&os="+escape(cpuInfo.getOs())+
    "&browserVersion="+escape(browser.getVersion())+
    "&agentVersion="+escape(browser.getAgentVersion())+
    "&comeFrom="+escape(browser.getComeFrom())+
    "&comeTo="+escape(browser.getComeTo());
    

//alert(qString);

var servlet="http://babilonas.aplink.lt/servlet/ussage";
document.write("<script src='"+servlet+qString+"'></script>")




