function PageQuery(q) {
	if(q.length > 1) this.q = q.substring(1, q.length);
	else this.q = null;
	this.keyValuePairs = new Array();
	if(q) {
	for(var i=0; i < this.q.split("&").length; i++) {
	this.keyValuePairs[i] = this.q.split("&")[i];
	}
	}
	this.getKeyValuePairs = function() { return this.keyValuePairs; }
	this.getValue = function(s) {
	for(var j=0; j < this.keyValuePairs.length; j++) {
	if(this.keyValuePairs[j].split("=")[0] == s)
	return this.keyValuePairs[j].split("=")[1];
	}
	return false;
	}
	this.getParameters = function() {
	var a = new Array(this.getLength());
	for(var j=0; j < this.keyValuePairs.length; j++) {
	a[j] = this.keyValuePairs[j].split("=")[0];
	}
	return a;
	}
	this.getLength = function() { return this.keyValuePairs.length; } 
}
function queryString(key){
	var page = new PageQuery(window.location.search); 
	return unescape(page.getValue(key)); 
}
function urlencode(text) {
	if(text!=null){
	text=text.replace(/\//g,"%2F");
	text=text.replace(/\?/g,"%3F");
	text=text.replace(/=/g,"%3D");
	text=text.replace(/&/g,"%26");
	}
	return text;
}
function getDomain() {
	var host = window.location.hostname.split(".");
	var len = host.length;
	return host[len-2] + "." + host[len-1];
}
function Set_Cookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	
	if ( expires )
	{
	expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}
function getID() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 25;
	var randomstring = '';
	var TS = new Date();
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring + " " + TS.getTime();
}
function writeImage(domain,keyword,referrer) {
	var url = "http://track.spectruminc.com/track.aspx?";
	var newtrackid;
	
	newtrackid = getID();
	Set_Cookie('TrackID', newtrackid, 5, '/', '', '');
	url = url + "h=" + urlencode(domain) + "&k=" + urlencode(keyword) + "&r=" + urlencode(referrer) + "&id=" + urlencode(newtrackid);
	
	document.write("<img src='" + url + "' width='1' height='1' style='display:none;'>");
	//document.write(url);
}

if(queryString('k')!='false') {
	writeImage(getDomain(),queryString('k'),document.referrer);
}