﻿    var d = new Date(); 
    var zero_date = new Date(0, 0, 0);
    d.setTime(d.getTime() - zero_date.getTime());
    var cookie_expire_date = new Date();
    cookie_expire_date.setMonth(12);

    var session_exp = IncreaseMin(60);
   
   
    var MachineTag;
    var sessionid;
    var Ref = document.referrer;
    var Browser = navigator.appName;
    var VUrl = location.href;
    var VWebsite = location.host;
    var PName = location.pathname;
    var SW = screen.width;
    var SH = screen.height;
    var q = location.search;
    soapit();
    
    function IncreaseMin(min) {

        var dt = new Date();
        var hr = dt.getHours();
        var mn = dt.getMinutes();

        if (mn + min > 59) {
            hr += 1;
            mn = (mn + min) - 59;
        }
        else {
            mn += min;
        }
        dt.setHours(hr, mn, 0);
        return dt;
    
    }  
       
function soapit() {

    randomString();
    setVisitorID();
    if (SimpleGetCookie('leaving') == null) {
        Delete_Cookie('SessionTag');        
        SetSession();
    }
    else {
        SetSession();
    }
    
    	
    var ProjID = proid;
    var data = "ProjectID=" + ProjID + "&Referrer=" + Ref.replace(/&/gi,"spn") + "&Browser=" + Browser + "&VisitUrl=" + VUrl + "&VisitWebsite=" + VWebsite + "&PageName=" + PName + "&ScreenHeight=" + SH + "&ScreenWidth=" + SW + "&QueryString=" + q + "&MachineTag="+ MachineTag+"&SessionID="+ SessionTag;
    //document.write("<img id='hui1' src='http://www.softpronet.co.uk/Images/softpronet-tracker.gif' alt='stat.softpronet' ></img>");
    document.write("<img id='hui' src='http://www.softpronet.co.uk/test.aspx?"+ data+"' alt='spnt' style='visibility:hidden;'  ></img>");
    //document.write("<img id='hui' src='http://localhost:2395/SoftProNet_Revised/test.aspx?" + data + "' alt='stat.softpronet' style='visibility:hidden;' ></img>");
    

    //setInterval(Change_Src, 10000);
    
    return data;
    
}



function Change_Src() {
    
    var data ="Referrer=" + Ref.replace(/&/gi,"spn") + "&Browser=" + Browser + "&VisitUrl=" + VUrl + "&VisitWebsite=" + VWebsite + "&PageName=" + PName + "&ScreenHeight=" + SH + "&ScreenWidth=" + SW + "&QueryString=" + q + "&MachineTag="+ MachineTag+"&SessionID="+ SessionTag;
    var img = document.getElementById("hui");
    //img.src = "http://localhost:4529/SoftProNet_Revised/test.aspx?Exiting=true&ProjectId=" + proid + "MachineTag=" + MachineTag + "&SessionID=" + SessionTag + "&PageName=" + PName;
   
        img.src = "http://www.softpronet.co.uk/test.aspx?Exiting=true&"+data;
    
}

function SimpleGetCookie(name) {
    var start = document.cookie.indexOf(name + "=");
    var len = start + name.length + 1;
    if ((!start) && (name != document.cookie.substring(0, name.length))) return null;
    if (start == -1) return null;
    var end = document.cookie.indexOf(";", len);
    if (end == -1) end = document.cookie.length;   
    return unescape(document.cookie.substring(len, end));
}

function SimpleSetCookie(name, value, expires, path, domain, secure) {
    var cookieString = name + "=" + escape(value) +
       ((expires) ? ";expires=" + expires.toGMTString() : "") +
       ((path) ? ";path=" + path : "") +
       ((domain) ? ";domain=" + domain : "") +
       ((secure) ? ";secure" : "");
    document.cookie = cookieString;
}

function Get_Cookie(name) {
    var start = document.cookie.indexOf(name + "=");
    var len = start + name.length + 1;
    if ((!start) && (name != document.cookie.substring(0, name.length))) return null;
    if (start == -1) return null;
    var end = document.cookie.indexOf(";", len);
    if (end == -1) end = document.cookie.length;
    MachineTag = (document.cookie.substring(len, end));
    return unescape(document.cookie.substring(len, end));
 }
 
 function Set_Cookie(name, value, expires, path, domain, secure) {
    var cookieString = name + "=" + escape(value) +
       ((expires) ? ";expires=" + expires.toGMTString() : "") +
       ((path) ? ";path=" + path : "") +
       ((domain) ? ";domain=" + domain : "") +
       ((secure) ? ";secure" : "");
    document.cookie = cookieString;
    Get_Cookie('MachineTag')
}

function setVisitorID() {
    if (Get_Cookie('MachineTag')) {
        var MachineTag = Get_Cookie('MachineTag');
    } else {
        Set_Cookie('MachineTag', Math.random(), cookie_expire_date);
    }
}

function Get_Session(name) {
    var start = document.cookie.indexOf(name + "=");
    var len = start + name.length + 1;
    if ((!start) && (name != document.cookie.substring(0, name.length))) return null;
    if (start == -1) return null;
    var end = document.cookie.indexOf(";", len);
    if (end == -1) end = document.cookie.length;
    SessionTag = (document.cookie.substring(len, end));
    return unescape(document.cookie.substring(len, end));
}

function Set_Session(name, value, expires, path, domain, secure) {
    var cookieString = name + "=" + escape(value) +
       ((expires) ? ";expires=" + expires.toGMTString() : "") +
       ((path) ? ";path=" + path : "") +
       ((domain) ? ";domain=" + domain : "") +
       ((secure) ? ";secure" : "");
    document.cookie = cookieString;
    Get_Session('SessionTag')
}

function SetSession() {
    if (Get_Session('SessionTag')) {
        var SessionTag = Get_Session('SessionTag');
    } else {
    Set_Session('SessionTag', sessionid , session_exp);
    }
}

//Generate the random machine tag for identification
function randomString() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 20;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	
 sessionid=randomstring;
}


//window.onbeforeunload = Delete_Cookie('SessionTag');
// 
function Delete_Cookie(name) {
   
    document.cookie = name + "=" +
      ";expires=Thu, 01-Jan-70 00:00:01 GMT";
  
     
}

var hook = true;
window.onbeforeunload = function() {
    if (hook) {
        Change_Src();        
        SimpleSetCookie("leaving", "now", IncreaseMin(1));


    }
}
function unhook() {
    hook = false;
} 

