﻿
var TIMER_BOOT = 200;
//var BASE_URL = 'http://localhost/Stats/';
var BASE_URL = 'http://stats.demini.com/';
var timer_boot = null;
var session = '';
var ip = '';
var referrer = '';
var is_inmedia = '1';
//var client_info = '';


function track(sSession, sIP, sReferrer)
{
	session = sSession;
	ip = sIP;
	referrer = document.referrer + '||ASP%NET||' + sReferrer;
	
	timer_boot = window.setInterval("on_timer_boot()",TIMER_BOOT);
}



function on_timer_boot()
{
	if (timer_boot != null)	{
		window.clearInterval(timer_boot);
	}
	
	getStats();
}


function getStats(){
	var sUrl = BASE_URL + 'stats.aspx?action=save';
	
	sUrl += '&protocol='	+ getValue(location.protocol);
	sUrl += '&host='		+ getValue(location.host);
	sUrl += '&port='		+ getValue(location.port);
	sUrl += '&pathname='	+ getValue(location.pathname);
	sUrl += '&hash='		+ getValue(location.hash);
	sUrl += '&search='		+ getValue(location.search);
	sUrl += '&href='		+ getValue(location.href);
	sUrl += '&hostname='	+ getValue(location.hostname);
	sUrl += '&session='		+ getValue(session);
	sUrl += '&ip='			+ getValue(ip);
	sUrl += '&referrer='	+ getValue(referrer);
	sUrl += '&is_inmedia='	+ getValue(is_inmedia);
	
	//sUrl += '&parent_url='	+ getValue(window.parent.document.URL);			// from stats.js
	//sUrl += '&client_info='	+ getValue(client_info);// from stats.js
	//sUrl += '&ip='			+ getClientIP();
	
	write_img(sUrl);
}

function write_img(sUrl)
{
	var img = document.createElement('IMG');
	img.width = '1';
	img.height = '1';
	img.alt = '';
	img.title = '';
	img.src = sUrl;
	document.body.appendChild(img);
}

//todo
function getClientIP(){
	var ip = '';
	//ip = '<!-- #echo var="REMOTE_ADDR" -->';

	if (ip == '') {
		if (java && java.net) {
			//ip = ''+java.net.InetAddress.getLocalHost().getHostAddress();
			ip = new java.net.InetAddress.getLocalHost();
			ip = new java.lang.String(ip);
			//document.writeln(ipStr.substring(ipStr.indexOf("/")+1));
		}
		else ip = '';
	}

	return ip;
}


function getValue(obj) {
	if (is_set(obj)){
		return obj;
	}
	else return '';
}

function is_set(obj)
{
	if (typeof obj != 'undefined') 
	{
		if (obj) 
		{
			if (obj != null)
			{
				return true;
			}
		}
	}
	
	return false;
}


