msinactiveDurationLimit = 180000;

startMsdoc = 0;
endMsdoc = 0;
msdocDuration = 0;

startMsinactive = 0;
endMsinactive = 0;
msinactiveDuration = 0;
msmaxinactive = 0;
msinactiveOverLimit = 0;

cookieid = "";
msdoc = 0;

sumimgsopen = 0;
//in xsl
//sumimgs = 0;
sumchaptersopen = 0;
//in xsl
//sumchapters = 0;
//in xsl
sumimgsanimshow = 0;

sumimgsopenArr = new Array();
sumchaptersopenArr = new Array();
sumimgsanimshowArr = new Array();

/* for debugging
*/
function doTrace(text){
	traceNd = document.getElementById("trace");
	traceNd.innerHTML = traceNd.innerHTML + text;
}

function stat_process(type){
	if(type == "start"){
		stat_start_msdocDuration();
		stat_start_msinactiveDuration();
	}
	if(type == "move"){
		stat_stop_msinactiveDuration();
	}
	if(type == "stop"){
		stat_stop_msinactiveDuration()
		stat_stop_msdocDuration();
	}
}

//-------------------------------
// body onload || body onfocus
function stat_start_msdocDuration(){
	startMsdoc = stat_bf_getTime();
}
// body onunload || body onblur
function stat_stop_msdocDuration(){
	endMsdoc = stat_bf_getTime();
	//alert(endMsdoc);
	msdocDuration = endMsdoc - startMsdoc;
}
// body onload || body onfocus || body onmousemove
function stat_start_msinactiveDuration(){
	startMsinactive = stat_bf_getTime();
}
// body onunload || body onblur || body onmousemove
function stat_stop_msinactiveDuration(){
	endMsinactive = stat_bf_getTime();
	msinactiveDuration = endMsinactive - startMsinactive;
	msmaxinactive = (msinactiveDuration > msmaxinactive)?msinactiveDuration:msmaxinactive;
	if(msinactiveDuration > msinactiveDurationLimit){
		msinactiveOverLimit += msinactiveDuration - msinactiveDurationLimit;
	}
	startMsinactive = stat_bf_getTime();
}
function stat_bf_getTime(){
	var actDate = new Date();
	var actTime = actDate.getTime();
	return actTime;
}

function callDb(actMode,actPageType,actDocid,actMediaId){
	msdoc = msdocDuration - msinactiveOverLimit;
	//from unknown reason, sometimes msdoc is minus value...
	if(msdoc < 0){ 
		msdoc = 1;
		msmaxinactive = 1;
	}
	var urlbeforeQM = "stat-insert.xsp?";	
	var urlAfterQM = getUrl(actMode,actPageType,actDocid,actMediaId);
	var url = urlbeforeQM + urlAfterQM;
	//alert(url);
	loadXMLDoc(url);
}

//-----------------------------------------------

function getUrl(actMode,actPageType,actDocid,actMediaId){
	actSelcrit = "";
	selcritCond = "lod,lop";
	actMsdoc = 0;
	msdocCond = "art,epg,gan,gbk,lod,lop,tan,tra";
	actMsmaxinactive = 0;
	msmaxinactiveCond = "art,epg,gan,gbk,lod,lop,tan,tra";
	actSumimgsopen = 0;
	sumimgsopenCond = "art,epg,gan,lop,tra";
	actSumimgs = 0;
	sumimgsCond = "art,epg,gan,lop,tan,tra";
	actSumchaptersopen = 0;
	sumchaptersopenCond = "tra";
	actSumchapters = 0;
	sumchaptersCond = "tra";
	actSumimgsanimshow = 0;
	sumimgsanimshowCond = "tan";
	
	// getSelCrit() asi s parametrem, aby se rozlisilo pagetype
	if (selcritCond.indexOf(actPageType) != -1) actSelcrit = getSelCrit(actPageType);
	if (vid != "") actSelcrit = actSelcrit + "vid=" + vid; 
	if (msdocCond.indexOf(actPageType) != -1) actMsdoc = msdoc;
	if (msmaxinactiveCond.indexOf(actPageType) != -1) actMsmaxinactive = msmaxinactive;	
	if (sumimgsopenCond.indexOf(actPageType) != -1) actSumimgsopen = sumimgsopen;	
	if (sumimgsCond.indexOf(actPageType) != -1) actSumimgs = sumimgs;	
	if (sumchaptersopenCond.indexOf(actPageType) != -1) actSumchaptersopen = sumchaptersopen;	
	if (sumchaptersCond.indexOf(actPageType) != -1) actSumchapters = sumchapters;	
	if (sumimgsanimshowCond.indexOf(actPageType) != -1) actSumimgsanimshow = sumimgsanimshow;	

	var url = "";
	url += "cookieid="+getCookieId();//getCookieId();TBD
	url += "&sessionid="+sessionid;
	url += "&pagetype="+actPageType;
	url += "&docid="+actDocid;
	url += "&mediaid="+actMediaId;
	url += "&language="+language;
	url += "&browser="+browser;
	url += "&selcrit="+actSelcrit;
	url += "&msdoc="+actMsdoc;
	url += "&msimgs=0";// bude vyhozeno
	url += "&msmaxinactive="+actMsmaxinactive;
	url += "&sumimgsopen="+actSumimgsopen;
	url += "&sumimgs="+actSumimgs;
	url += "&sumchaptersopen="+actSumchaptersopen;
	url += "&sumchapters="+actSumchapters;
	url += "&sumimgsanimshow="+actSumimgsanimshow;
	
	return url;
}

function getSelCrit(pagetype){
	var selcritStr = "";
	for (i in formFields){
		if(formFields[i][1].indexOf(pagetype) != -1){
			var fieldId = formFields[i][0];
			if(formFields[i][2] == "C"){
				fieldNd = document.getElementById(fieldId);
				if (fieldNd.checked == true){
					selcritStr += fieldId+"="+fieldNd.value+";";
				}
			}
			if(formFields[i][2] == "I"){
				fieldNd = document.getElementById(fieldId);
				fieldIdVal = fieldNd.value;
				if(fieldIdVal != "") selcritStr += fieldId+"="+fieldIdVal+";";
			}
			if(formFields[i][2] == "L"){
				fieldNd = document.getElementById(fieldId);
				for (j=0; j < fieldNd.length; j++){
					fieldIdVal = fieldNd[j].value;
					if(fieldNd[j].selected == true){
						if(fieldIdVal != "") selcritStr += fieldId+"="+fieldIdVal+";";
						break;
					}
				}
			}
		}
	}
	return selcritStr;
}

/* */
function addToSum(type, id){
	//alert(type+","+id);
	//lod?
	if(type == "I"){
		sumimgsopenArrStr = sumimgsopenArr.toString();
		if(sumimgsopenArrStr.indexOf(id)){ 
			sumimgsopenArr.push(id);
			sumimgsopen++;
		}
	} else if(type == "T") {
		sumchaptersopenArrStr = sumchaptersopenArr.toString();
		if(sumchaptersopenArrStr.indexOf(id)){
			sumchaptersopenArr.push(id);
			sumchaptersopen++;
		}
	} else {
		sumimgsanimshowArrStr = sumimgsanimshowArr.toString();
		if(sumimgsanimshowArrStr.indexOf(id)){
			sumimgsanimshowArr.push(id);
			sumimgsanimshow++;
		}
	}
}

function getCookieId(){
	var cookies = document.cookie;
	var cookieid = "";
	if(cookies.indexOf("cookieid") == -1){
		document.cookie = "cookieid="+cookieidGen+"; expires=Sat, 8 May 2049 11:00:00 UTC; path=/"
		cookieid = cookieidGen;
	} else {
		var startCookieidStr = cookies.indexOf("cookieid");
		var startCookied = startCookieidStr+9;
		var endCookied = startCookied+17;
		cookieid = cookies.substring(startCookied,endCookied);
	}
	return cookieid;
} 

//---------------------------------------------
//xmlhttp...
function loadXMLDoc(url) {
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
        //alert(xmlhttp);
        xmlhttp.onreadystatechange = processReqChange;
        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
    } else if (window.ActiveXObject) {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        if (xmlhttp) {
            xmlhttp.onreadystatechange = processReqChange;
            xmlhttp.open("GET", url, true);
            xmlhttp.send();
        }
    }
}

function processReqChange() {
	try {
	    if (xmlhttp.readyState==4) {
	        if (xmlhttp.status==200) {
		   		return true;
	        } else {
	           // alert("There was a problem retrieving the XML data:\n" + xmlhttp.statusText);
	        }
	    }
	}
	catch(err){
		return;
	}	
}

//---------------------------------------------

/* Analysis...
2 zpusoby volani stat javascriptu:
	dbCall(actMode,actPageType,actDocid,actMediaId)
- onunload:	dbCall('unload','tra','00001',0);
- onclick: 	dbCall('click','img','00001','1');
GNP = globalne nastavena promenna
art epg	gan	gbk	img	lod	lop	pdf	tan	tra	wrd	gmp	xml

	
id 						- all - plni db
cookieid				- all - dotahne funkce behem sestavovani url
sessionid				- all - GNP
pagetype				- img,pdf,gmp,xml (funkci)
docid					- art,gan,tan,tra (GNP)
						- epg,gbk,lod,lop (GNP podle pagetype)
						- img,pdf,wrd,gmp (aktualni docid - funkci)
						- xml (GNP)
	obecne: docid by se melo nastavit podle pagetype - jestlize art,gan,tan,tra, pak cislo, jinak kod pagetype
mediaid					- img,gmp funkci else 0
language				- GNP
rh						- all - plni xsp
date					- all - plni xsp
selcrit					- lod,lop (dotahne funkce) else ""
msdoc					- art,epg,gan,gbk,lod,lop,tan,tra - GNP else 0
msimgs					- art,epg,gan,lop,tan,tra - GNP else 0
msmaxinactive			- art,epg,gan,gbk,lod,lop,tan,tra - GNP else 0
sumimgsopen				- art,epg,gan,lop,tan,tra - GNP else 0
sumimgs					- art,epg,gan,lop,tan,tra - GNP else 0
sumchaptersopen			- tra - GNP else 0
sumchapters				- tra - GNP else 0
sumimgsanimshow			- tan - GNP else 0
*/

//---------------------------------------------
/* analysis - counting 
duration:
.........
1/ msdocDuration (doba cteni)
	start: body onload || body onfocus
	stop: body onunload || body onblur
	
	util variables:
		startMsdoc: milis. start time
		endMsdoc: milis. end time
		
	msdocDuration = endMsdoc - startMsdoc
		
2/ msinactiveDuration (doba neaktivity, ktera presahuje urcitou mez)
	start: body onload || body onfocus || body onmousemove
	stop: body onunload || body onblur || body onmousemove

	util variables:
		startMsinactive: milis. start time
		endMsinactive: milis. end time
		msinactiveOverLimit //nascitana doba pres limit, ktera se pak odecte od msdoc
		
	msinactiveDuration = endMsinactive - startMsinactive
	msmaxinactive = (msinactiveDuration > msmaxinactive)?msinactiveDuration:msmaxinactive;

	if(msinactiveDuration > msinactiveDurationLimit){
		msinactiveOverLimit += msinactiveDuration - msinactiveDurationLimit;
	}
	
	//nastavi se nejdelsi doba, pokud je vyssi nez limit, tak se zkrouhne a pricte k dosavadni dobe

3/ msimgDuration (doba prohlizeni obrazku)
	start: img onclick || body onfocus
	stop: imgDet onclick || body onblur || body onunload

	util variables:
		startMsimg: milis. start time
		endMsimg: milis. end time
		
	msimgDuration = endMsimg - startMsimg
	if(msimgDuration > msimgDurationLimit) msimgDuration = msimgDurationLimit;

msdocDirty += msdocDuration
// posila se...
msdoc = msdocDirty - msinactiveOverLimit
msimgs += msimgDuration
msmaxinactive
*/

//---------------------------------------------
