﻿var Seconds=0;
var PageSecondsTimer=window.setInterval("WriteNewSecond()", 1000);
window.onunload=function() {
    var NowCookie = getCookie("VisitsTime");
    if (NowCookie==null) NowCookie="";
    var NowDate=new Date();
    var y=NowDate.getFullYear();
    var m=NowDate.getMonth()+1;
    var d=NowDate.getDate();
    var date=d+"."+m+"."+y;
    NowCookie += "["+document.location+"{;}"+date+"{;}"+MyUserId+"{;}"+Seconds+"]";
    setCookie("VisitsTime", NowCookie, "Mon, 01-Jan-2030 00:00:00 GMT", "/");
}

window.onfocus=function() {
    if (PageSecondsTimer!=null) {window.clearInterval(PageSecondsTimer);}
    PageSecondsTimer=null;
    PageSecondsTimer=window.setInterval("WriteNewSecond()", 1000);
}

window.onblur=function() {
    if (PageSecondsTimer!=null) {window.clearInterval(PageSecondsTimer);}
    PageSecondsTimer=null;
}

function WriteNewSecond(){
    Seconds++;
}
function setCookie (name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

function delCookie(name, path, domain) {
	if (getCookie(name)) {
		document.cookie = name + "=" + 
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}