﻿// Site urls
var srv = "http://statistics.komatsuforest.com/";
var statspage = "default.aspx";
var site = "www.komatsuforest.com";
var sUrl = document.location.href;
// Document id
var did = requestQueryString(sUrl, "id");
// Document name
var d = document.title; //fixChars(document.title);
//Referrer
var r = document.referrer;
// Defines addresses to exclude from statistics
var internalref = ['http://development.komatsuforest.com', 'http://www.komatsuforest.com', 'http://komatsuforest.com', 'http://statistics.komatsuforest.com', 'http://www.komatsuforest.ru', 'http://www.komatsuforest.se', 'http://www.komatsuforest.fi', 'http://www.komatsuforest.br'];
r = checkInternal(r);
// Screen size
var s = screen.width + "x" + screen.height;
// Screen colordepth
var c = screen.colorDepth;
// Browser
var n = getNavAndOperative(navigator.appName + " " + navigator.appVersion + "." + navigator.appMinorVersion); 
// Generated stats URL
var u;
// Cpu class, most likely is x86
var cpu=navigator.cpuClass;
// platform
var p=navigator.platform;
// Iconsize of rendered image
var iconsize = 0;
// The pages to post the data to
var postStatsPage = "info.aspx?site=";
var postLinkStatsPage = "linkinfo.aspx?site=";
//-----------------------------------------------------------------------

function setStats(lang) {
    // url where the page is submitted to
    // change this to the address the script resides on
    u = srv + postStatsPage + escape(site);

    // random, to prevent the browser from taking a cached image and not do a call to the server
    var rnd = Math.random();

    var hasFlash = ReadCookie("KF_FlashEnabled");
    if (hasFlash == null || hasFlash == "False" || hasFlash == false)
        hasFlash = "false";
    else
        hasFlash = "true";
    //u += "&d="+d+"&r="+escape(r)+"&s="+escape(s)+"&did="+escape(did)+"&c="+escape(c)+"&n="+escape(n)+"&cpu="+escape(cpu)+"&p="+escape(p)+"&rnd="+escape(rnd);
    u += "&lang=" + lang + "&d=" + d + "&r=" + escape(r) + "&s=" + escape(s) + "&did=" + escape(did) + "&c=" + escape(c) + "&n=" + escape(n) + "&cpu=" + escape(cpu) + "&p=" + escape(p) + "&rnd=" + escape(rnd) + "&flash=" + hasFlash;

    //document.write('<a class=nolink target=_blank href="'+srv+statspage + '?site='+escape(site)+'"><img align=absMiddle src="'+u+'" border=0 width='+iconsize+' height='+iconsize+' alt="Statistics"></a>');
    //document.write(u);
    document.write('<img align=absMiddle src="' + u + '" border=0 width=' + iconsize + ' height=' + iconsize + ' alt="">');

} //----------------------------------------------------------------------------

function setStatsByLink(link, linkName, idet) {
    did = escape(idet);
    d = fixChars(linkName);
    // url where the page is submitted to
    // change this to the address the script resides on

    u = srv + postLinkStatsPage + escape(site);
    //u = srv+"default.asp?id=10614&site="+escape(site);

    // random, to prevent the browser from taking a cached image and not do a call to the server
    var rnd = Math.random();
    var hasFlash = ReadCookie("KF_FlashEnabled");
    if (hasFlash == null || hasFlash == "False" || hasFlash == false)
        hasFlash = "false";
    else
        hasFlash = "true";
    u += "&d=" + escape(d) + "&r=" + escape(r) + "&did=" + did + "&s=" + escape(s) + "&c=" + escape(c) + "&n=" + escape(n) + "&cpu=" + escape(cpu) + "&p=" + escape(p) + "&rnd=" + escape(rnd) + "&flash=" + hasFlash; ;

    //För bild - 
    document.write('<img align=absMiddle src="' + u + '" border=0 width=' + iconsize + ' height=' + iconsize + ' alt="">');
    //Nytt fönster
    setTimeout(window.open(link), 2000)
    window.history.go(-1);
}

//----------------------------------------------------------------------------

function checkInternal(ref) {
    //För att ställa in interna länkar
    for (i in internalref) {
        if (r.substring(0, internalref[i].length) == internalref[i]) {
            ref = "&lt;Internal reference&gt;";
        }
        else if (ref == "" || ref == null || ref == 'undefined') {
            ref = "&lt;No referrer&gt;";
        }
    }
    return ref;
}
//-----------------------------------------------------------------------

function getNavAndOperative(nav) {
    var operativsys, navi;

    if (nav.match("Windows")) {
        if (nav.match("Microsoft Pocket Internet Explorer 4.0")) {
            operativsys = "(Windows Pocket PC)";
        }
        else {
            operativsys = "(Windows)";
        }
    }
    else if (nav.match("Macintosh")) {
        operativsys = "(Macintosh)";
    }
    else if (nav.match("Unix") || nav.match("UNIX")) {
        operativsys = "(UNIX)";
    }
    else {
        operativsys = "(Unknown)";
    }

    navi = getNavigator(nav);

    return navi + operativsys;
}

function getNavigator(nav) {
    if (nav.match("MSIE")) {
        //Det var IE, vilken version?
        if (nav.match("MSIE 9")) {
            nav = "Internet Explorer 9 ";
        }
        else if (nav.match("MSIE 8")) {
            nav = "Internet Explorer 8 ";
        }
        else if (nav.match("MSIE 7")) {
            nav = "Internet Explorer 7 ";
        }
        else if (nav.match("MSIE 6")) {
            nav = "Internet Explorer 6 ";
        }
        else if (nav.match("MSIE 5")) {
            nav = "Internet Explorer 5 ";
        }
        else if (nav.match("MSIE 4")) {
            nav = "Internet Explorer 4 ";
        }
        else {
            nav = "Unknown Internet Explorer ";
        }

    }
    else if (nav.match("Netscape")) {
        //Det var Mozilla/Netscape, vilken version?
        nav = "Mozilla/Netscape compatible " + navigator.appVersion;
    }
    else if (nav.match("Opera")) {
        //Det var Opera, vilken version?
        nav = "Opera compatible " + navigator.appVersion + " ";
    }
    else {
        nav = "(Unknown browser) ";
    }

    return nav
}

//----------------------------------------------------------------------------
// Övriga funktioner

function fixChars(str) {
    /*alert(str.indexOf("ö"));

                             str = str.replace("Ã¥", "%E4");
    str = str.replace("Ã¤", "%E5");
    str = str.replace("Ã¶", "%E6");

                             str = str.replace("Ã", "%C5");
    str = str.replace("Ã„", "%C4");
    str = str.replace("Ã–", "%D6");
                             
    str = str.replace("å", "%E4");
    str = str.replace("ä", "%E5");
    str = str.replace("ö", "%E6");

                             str = str.replace("Å", "%C5");
    str = str.replace("Ä", "%C4");
    str = str.replace("Ö", "%D6");
    str = str.replace(" ", "%20");
    
    //str = decodeUTF8(str);
                             
    //str = encodeUTF8(str);*/

    //alert(str);
    return str;
}

function requestQueryString(sURL, sParamName) {
    if (!sParamName)
        return null;
    if (!sURL)
        sURL = location.search;

    var i = sURL.indexOf("?");

    if (i == -1)
        return null;
    var sRet = null;
    sURL = sURL.substring(i);
    var regEx = new RegExp("[\\?\\&]" + sParamName + "\\=[^&#]*", "ig");
    var arr = null;
    while ((arr = regEx.exec(sURL)) != null)
        sRet = (sRet == null ? "" : sRet + ", ") + decodeUTF8(arr[0].substring(sParamName.length + 2));

    return sRet;
}
function encodeUTF8(sValue) {
    try {
        return encodeURIComponent(sValue).replace(/\_/g, "%5F").replace(/\-/g, "%2D").replace(/\+/g, "%2B");
    }
    catch (e) {
        //
    }
    return escape(sValue).replace(/\_/g, "%5F").replace(/\-/g, " ").replace(/\+/g, "%2B");
}
function decodeUTF8(sValue) {
    try {
        return decodeURIComponent(sValue.replace(/\+/g, " "));
    }
    catch (e) {
        //isNaN
    }
    return unescape(sValue.replace(/\+/g, " "));
}
function ReadCookie(cookieName) {
    var theCookie = "" + document.cookie;
    var ind = theCookie.indexOf(cookieName);
    if (ind == -1 || cookieName == "") return "";
    var ind1 = theCookie.indexOf(';', ind);
    if (ind1 == -1) ind1 = theCookie.length;
    return unescape(theCookie.substring(ind + cookieName.length + 1, ind1));
}