// JavaScript Document
// © Boštjan Kožuh

var sUniqueID = uniqueID();
var sPageID = uniqueID("short");
var sTrackingUrl = "track/webstat_track_1.aspx"

window.onunload = function() {
		stat_img = new Image();
		stat_img.src = sTrackingUrl + "?js=exit&pid=" + sPageID;
	}

function collectStat(_CommonID) {
	
	var statUrl = new Array();
	var statUrlQS = new Array();

	statUrl.push(sTrackingUrl);
	
	statUrlQS.push("js=1");
	statUrlQS.push("cid=" + _CommonID);
	statUrlQS.push("page=" + encodeURIComponent(document.URL.split("?",1)));
	statUrlQS.push("pid=" + sPageID);
	statUrlQS.push("from=" + encodeURIComponent(document.referrer));
	
	var documentQS = location.search;
	if (documentQS != "")
		statUrlQS.push("qs=" + encodeURIComponent(documentQS.substring(1)));
	
	statUrlQS.push("pc=" + handlePersistentCookie(sUniqueID));
	statUrlQS.push("sc=" + handleSessionCookie(sUniqueID));
	statUrlQS.push("res=" + screen.width + 'x' + screen.height);
	statUrlQS.push("color=" + screen.colorDepth);
	statUrlQS.push("platform=" + navigator.platform);
	statUrlQS.push("nav=" + encodeURIComponent(navigator.appName));
	
	statUrl.push(statUrlQS.join("&"));
								
	var statFullUrl = statUrl.join("?");
	//document.writeln(statFullUrl);
	stat_img = new Image();
	stat_img.src = statFullUrl;
}

function uniqueID(_variant) {
	var datum = new Date();
	var tmp = new Array();
	if (_variant != "short")
		tmp.push(String(datum.getFullYear()).substring(3),datum.getMonth(),datum.getDay());
	tmp.push(datum.getUTCHours(),datum.getMinutes(),datum.getSeconds(),datum.getMilliseconds());
	return tmp.join("");
}

/* Dobi ustrezne vrednosti za session in persisten piškotka */
function handleSessionCookie(_cookieValue) {
	var scValue = getCookieValue("adacta_scID");
	if (scValue != false) {
		return scValue;
	}
	else {
		return writeSessionCookie("adacta_scID",_cookieValue);
	}
}
function handlePersistentCookie(_cookieValue) {
	var pcValue = getCookieValue("adacta_pcID");
	if (pcValue != false) {
		return pcValue;
	}
	else {
		return writePersistentCookie("adacta_pcID",_cookieValue,"",365);
	}
}


/* ZAPIŠI SESSION PIŠKOTEK - vrne FALSE, če niso omogočeni */
function writeSessionCookie(cookieName, cookieValue) {
	document.cookie = escape(cookieName) + "=" + escape(cookieValue) + "; path=/";
	if (getCookieValue(cookieName) == cookieValue) {
		return cookieValue;
	}
	else {
		return false;
	}
}

/* ZAPIŠI PERSISTENT PIŠKOTEK - vrne FALSE, če niso omogočeni */
function writePersistentCookie (cookieName, cookieValue, periodType, offset) {
	var expireDate = new Date ();
	offset = offset / 1;
	if (periodType == "minute") {
		expireDate.setMinutes(expireDate.getMinutes()+offset);
	}
	else {
		expireDate.setDate(expireDate.getDate()+offset);
	}
	document.cookie = escape(cookieName) + "=" + escape(cookieValue) + "; expires=" + expireDate.toGMTString() + "; path=/";
	if (getCookieValue(cookieName) == cookieValue) {
		return cookieValue;
	}
	else {
		return false;
	}
}  


/* PREBERI VREDNOST PIŠKOTKA*/
function getCookieValue(cookieName) {
	var exp = new RegExp (escape(cookieName) + "=([^;]+)");
	if (exp.test (document.cookie + ";")) {
		exp.exec (document.cookie + ";");
		return unescape(RegExp.$1);
	}
	else {
		return false;
	}
}