var xmlhttp=false; 
/*@cc_on @*/ 
/*@if (@_jscript_version >= 5) 
// JScript gives us Conditional compilation, we can cope with old IE versions. 
// and security blocked creation of the objects. 
try { 
	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
} catch (e) { 
	try { 
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
	} catch (E) { 
		xmlhttp = false; 
	} 
} 
@end @*/ 
if (!xmlhttp && typeof XMLHttpRequest!='undefined') { 
	xmlhttp = new XMLHttpRequest(); 
}

var linknum=null;

/*
*	Send the link details
*/
function sendLink(link,ref) {
	if (xmlhttp.readyState!=0 && xmlhttp.readyState!=4) {
		setTimeout("sendLink("+link+","+num+")",250);
		return;
	}

	var now=new Date();
	var inc=now.getTime();

	var url = "http://"+document.location.host+"/res/sendlink.php?link="+encodeURIComponent(link.href)+"&src="+encodeURIComponent(document.location.href)+"&ref="+ref+"&inc="+inc;

	xmlhttp.open('GET', url, false); 

	xmlhttp.send(null);

	return true;
}

/*
*	Add tracking to all links on page
*/
function initSendLink () {
	linknum=new Array(document.links.length);
	for (var i=0;i<document.links.length;i++) {
		addTracking(document.links[i]);
	}
}

/*
*	Add tracking to individual links
*	Exclude links that already have an onclick event since we can't be sure not to lose the parameters
*/
function addTracking(link) {
	var href=link.href;
	href=href.replace('index.php?section=home','')
	href=href.replace('index.php','');

	var ref=link.id;

	if (ref=='') {
		if (linknum[href]) linknum[href]++;
		else linknum[href]=1;	
		ref=linknum[href];
	}

	 if (typeof link.onclick != 'function') {
		eval("link.onclick =function(e) {if (!e) {var link = window.event.srcElement} else {var link=this} sendLink(this,'"+ref+"')}");
	 }
}

/*
*	Add the onload function to any other onload function
*/
function addLoadEvent(func) {
	var oldonload = window.onload;

	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}

}

addLoadEvent(initSendLink);