function createCookie(name,value,days) {
	if (days) 
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	value = value.replace(/ /,"_").replace(/'/,""); /* gets rid of blanks and apostrophes */
	document.cookie = name+"="+value+expires+"; path=/";
}

function noPercent(x)
{
	x = unescape(x);
	x = x.replace(/\+/g," ").replace(/^\s\s*/, '').replace(/\s\s*$/, '');
	return x.toLowerCase(); 
}

function getRef()
{
	ref = document.referrer;
	re = /(\?|&)(q|p|query)=([^&]*)/;
	searchq = re.exec(ref);
	if(searchq) 
	{
		searchq[3] = noPercent(searchq[3]);
		sleuthTracker._setVar(searchq[3]);
	}
}

function sleuth()
{
	if(document.location.search.indexOf("gclid")!=-1||document.location.search.indexOf("cpc")!=-1)
		getRef();
	var cook = '';
	if (document.cookie) cook = document.cookie;
	if (cook.indexOf('referrer') == -1)
	{
	    if (document.referrer && document.referrer!="")
		    createCookie ('referrer', document.referrer, 1);
	    else
		    createCookie ('referrer', 'direct', 1);
    }
    if (cook.indexOf('firstpg') == -1)
    {
	    if (document.location && document.location.href !="")
            createCookie ('firstpg', document.location.href, 1);
        else
            createCookie ('firstpg', 'none', 1);
    }
}

var sleuthTracker = _gat._getTracker("UA-1");
sleuthTracker._initData();
sleuth();
