function GetXmlHttpObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest(); // code for IE7+, Firefox, Chrome, Opera, Safari
	}
	if (window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP"); // code for IE6, IE5
	}
	return null;
}

var sharpTracker=false;
function SharpTracker() {
	var xmlhttp=null;
	var site;
	var sessionID=null;
	var sfPathHistory=null;
	
	this.init=function () {
		//document.getElementsByTagName("body")[0].addEventListener('beforeunload', function () { alert('bye'); this.pageExit(); }, false);
		site=window.location.protocol+'//'+window.location.host+'/';
	}
	
	this.getCookie=function (cookieName) {
		if (document.cookie.length>0) {
			c_start=document.cookie.indexOf(cookieName+"=");
			if (c_start!=-1) {
				c_start=c_start+cookieName.length+1;
				c_end=document.cookie.indexOf(";",c_start);
				if (c_end==-1) c_end=document.cookie.length;
				return unescape(document.cookie.substring(c_start,c_end));
			}
		}
		return "";
	}
	this.setCookie=function (cookieName,value,expiredays) {
		var exdate=new Date();
		exdate.setDate(exdate.getDate()+expiredays);
		document.cookie=cookieName+"="+escape(value)+((expiredays==null)?"":";expires="+exdate.toGMTString());
	}

	this.track=function () {
		this.sessionID=this.getCookie('sf_session');
		if (this.sessionID==null || this.sessionID=="") {
			this.sessionID='';
			for (var i=0;i<18;i++) {
				this.sessionID=this.sessionID+((Math.floor(Math.random()*16)+1).toString(36));
			}
			this.sessionID=this.sessionID+(String((new Date()).getTime()).replace(/\D/gi,''));
			this.setCookie('sf_session',this.sessionID,null);
			this.sendSession();
		} else {
			this.sendPath();	
		}
	}
	
	this.trackExit=function () {
		if (this.sessionID!=null && this.sessionID!='') {
			var url="http://www.sharpfeedback.com/st.php";
			var params="site="+site;
			params=params+"&request=trackExit";
			params=params+"&sessionID="+this.sessionID;
			params=params+"&pathname="+window.location.href.substring(window.location.href.indexOf(window.location.host)+window.location.host.length);
			if (false) params=params+"&target="+target;
			params=params+"&sid="+Math.random();
			var importScript=document.getElementById('sharpTracker');
			if (importScript!=null)
				document.getElementsByTagName("head")[0].removeChild(importScript);
			var fileref=document.createElement('script');
			fileref.setAttribute("type","text/javascript");
			fileref.setAttribute("id","sharpTracker");
			fileref.setAttribute("src", url+'?'+params);
			if (typeof fileref!="undefined") document.getElementsByTagName("head")[0].appendChild(fileref);	
		}
	}
	
	this.sendSession=function () {
		this.updateSession('newSession');
	}
	this.sendPath=function () {
		this.updateSession('updatePath');
	}
	this.updateSession=function (request) {
		var url="http://www.sharpfeedback.com/st.php";
		var params="site="+site;
		params=params+"&request="+request;
		params=params+"&sessionID="+this.sessionID;
		params=params+"&pathname="+window.location.href.substring(window.location.href.indexOf(window.location.host)+window.location.host.length);
		params=params+"&referrer="+document.referrer;
		params=params+"&browser="+((navigator.userAgent)?navigator.userAgent:((navigator.vendor)?navigator.vendor:((window.opera)?'Opera'+navigator.appVersion:'Unknown')));
		params=params+"&os="+navigator.platform;
		params=params+"&sid="+Math.random();
		var importScript=document.getElementById('sharpTracker');
		if (importScript!=null)
			document.getElementsByTagName("head")[0].removeChild(importScript);
		var fileref=document.createElement('script');
		fileref.setAttribute("type","text/javascript");
		fileref.setAttribute("id","sharpTracker");
		fileref.setAttribute("src", url+'?'+params);
		if (typeof fileref!="undefined") document.getElementsByTagName("head")[0].appendChild(fileref);
	}
}

sharpTracker=new SharpTracker();
sharpTracker.init();
if (! document.getElementsByTagName("body")[0].getAttribute('onUnload')) document.getElementsByTagName("body")[0].setAttribute('onUnload', 'sharpTracker.trackExit();');
