var time=new Date();
var ac_time=time.getFullYear()+"-"+((time.getMonth()+1) < 10 ? '0' : '') + (time.getMonth()+1)+"-"+(time.getDate() < 10 ? '0' : '') + time.getDate()+" "+(time.getHours() < 10 ? '0' : '') + time.getHours()+":"+(time.getMinutes() < 10 ? '0' : '') + time.getMinutes()+":"+(time.getSeconds() < 10 ? '0' : '') + time.getSeconds();

var domain_name=document.location.toString();
var loccc=document.location.toString();

var pos=0;
var ct=0;
	
	for(var i=0;i<loccc.length;i++)
		{
			if((loccc.charAt(i))=='/')
			{
				pos=i;		
				ct=ct+1;				
			}	
			if(ct>=3)
			{	break;	}
		}

var count=0;
	
	for(var j=0;j<loccc.length;j++)
		{
			if((loccc.charAt(j))=='/')
			{	
				domain_pos=j;	
				break;
			}
		}

domain_pos=domain_pos+2;

loc_domain=loccc.substring(domain_pos,pos);

 
	if(loc_domain.indexOf("www.")!=-1)
	{
		loc_domain=loccc.substring(domain_pos+4,pos);
	}
	else
	{
		loc_domain=loccc.substring(domain_pos,pos);
	}
loccc=loccc.substring(pos+1,loccc.length);
var x=0;
if(loccc.indexOf("?")!=-1)
{
	x=loccc.indexOf("?");
}
else
{
	x=loccc.length;
}

loccc=loccc.substring(0,x);

var page_name 	=	loccc;
var ref_name	=	escape(document.referrer);

// Start Timer and Visitor Info Monitoring
var domain_name=document.location.toString();

	if(domain_name.length > 10)
		 {
		 domain_name=domain_name.substring(7,domain_name.length);
		 if(domain_name.indexOf("/")>3)
		 domain_name="http://"+domain_name.substring(0,domain_name.indexOf("/"));
		 else
		 domain_name="http://" + domain_name;
		 }

window.onload = start;

function start()
	{				
		if	(page_name	==	"")
		{
			page_name="index.php";
		}
	
		if(ref_name == "")
		{
			ref_name="NA";	
		}
	
	var _scrTrack_I="http://www.queryus.com/_INSERT_VINFO.php?ref="+ref_name+"&page="+page_name+"&ac_id="+ac_time+"&t_s=0&domain="+loc_domain;
	
	Vimg = new Image();
	Vimg.src = _scrTrack_I;
	}

// End Timer and Visitor Info Monitoring
window.onunload=end;

function end()
{
	
   	var _scrTrack_II="http://www.queryus.com/_UPDATE_VINFO.php?ac_id="+ac_time+"&t_s="+(new Date()-time);
		
	Vvimg = new Image();
	Vvimg.src = _scrTrack_II;
}

// change the following to the place where you put the linktrack script.
 linktrack = "http://www.queryus.com/clicksdbcall.php?url="+loc_domain;

 referrer = "";

// make error proof
function trackClick( e ) {
	//end();
	referrer=document.referrer;

	try {
		
    	getClickInfo(e);
  	} catch (err) {
     	return true;
  	}
  	return true;
}

function getClickInfo(e) 
   {
	   
	// this grabs the link and redirects it to the php site.
  	var link_target = null;
  	if (e != null) { 
     	link_target = e.target;
  	} else {
     	link_target = window.event.srcElement;
  	}	
  	// move on to our next site
  	if (link_target == null) 
		return true; // can't track a link that doesn't exist

	if (link_target.nodeName.toUpperCase() != 'A') 
		return true; // don't want to track anything but links.

  	if (link_target.href == null) 
		return true; // if we aren't going anywhere don't track
		
  	var link_href=link_target.href;
  	var link_atext = "";
	
  	if (link_target.innerText != null) {
     	link_atext = link_target.innerText;		
  	} 
	else if (link_target.innerHTML != null) {
     	link_atext = link_target.innerHTML;
  	} 
	else if (link_target.text != null) {
     	link_atext = link_target.text;
  	} 
  	else {
    	return true; // can't track a link with no description
  	}
	
  	if(link_href == null || link_atext == null) 
  		return true; // once more with feeling.
	
  	if(typeof(link_href) != 'string') 
  		return true;  // not sure what else it could be, except an image.
  // see if the link is off the page
  	var link_host = document.location.hostname.toLowerCase();	
	
  	if (link_host==null || link_host=="") 
  		link_host="hey hey";
	 
  	try {
		pageName=document.location.pathname;
     	var link_newurl=linktrack+"&linkName="+link_href+"&linkText="+link_atext+"&referrer="+referrer+"&pageName="+pageName+"&linkFrom="+link_from; // sometimes this fails
  	} catch ( e ) {
     	var link_newurl=linktrack+"&linkName="+link_href+"&linkText="+link_atext+"&referrer="+referrer+"&pageName="+pageName;
  	}

  	try
	{ // put it in a try/next to avoid http errors
    	var link_image = new Image();
    	link_image.src = link_newurl;
 	}
	catch(err)
	{ 
	     	return true; 
  	}
    // the pause that refreshes
  	var link_start = new Date().getTime();
  	var link_stop = link_start + 500;
	
  	while(link_start < link_stop){
    	link_start = new Date().getTime(); // nice tight loop - it might be needed on slow systems
  	}
	return true; // let link do his job 
}

// direct code that is executed immediately
document.onclick=trackClick; // this directs links to our code
