//document.write('<script type="text/javascript" src="http://www.geckno.com/gw/prototype_gw.js"></script>');

var gwLink = 'http://www.geckno.com/gw/gw2.php';
function gw2(){
	trackUser();
}




/* Get the tracking path out of the pc3LogPath-Metatag */
function getTrackingPath(){
	var trackingPath;
	var metaTags = document.getElementsByTagName('META');
	for(var i=0; i<metaTags.length; i++ ){
		if ( metaTags[i].name != 'pc3LogPath' ) continue;
		trackingPath = metaTags[i].content;
		break;
	}
	if ( !trackingPath ) return '/';
	var pattern = /\/[a-z_-]+(.*)\..*/i;
	var match = pattern.exec(trackingPath);
	if ( !match ) return '/';
	return match[1];
}

/* Get a conntected string with a own specified connector */
function getConnectedString(str,connector){
	var connectedString = '';
	for(var key in str){
		connectedString += (connectedString?connector:'') + key +'='+ encodeURIComponent(str[key]);
	}
	return connectedString;
}

/* Generates a unique ID */
function generateGuid() {
	var	result = '';
	alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'T', 'S', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
	var len = alphabet.length;
	for(var j=0; j<16; j++){
		result += alphabet[Math.floor(Math.random()*len)];
	}
	return result;
}

/* Generetes debugging output when using &dt=1 GET-parameter */
function debugOutput(objToDebug, strConnector, objTitle) {
	document.write('<div style="overflow: auto; font-family:Verdana; font-size:10px; z-index:100; padding:0px; text-align:left; border: 1px solid red; background-color: #ffffff; margin-bottom:5px;"><ul>');
	document.write('<li><b>' + objTitle + ': ' + getConnectedString(objToDebug, strConnector) + '</b></li><ul>');
	for(var key in objToDebug){
		document.write('<li>' + key + '=' + objToDebug[key]  + '</li>');
	}			
	document.write('</ul></ul></div>');
}

/* Get cookie values */
function getCookieVal (offset) {  
	var endstr = document.cookie.indexOf (";", offset);  
	if (endstr == -1) { endstr = document.cookie.length; }
		return unescape(document.cookie.substring(offset, endstr));
}

/* Get cookies */
function getCookie (name) {  
	var arg = name + "=";  
	var alen = arg.length;  
	var clen = document.cookie.length;  
	var i = 0; 
	while (i < clen) {    
		var j = i + alen;    
		if (document.cookie.substring(i, j) == arg) return getCookieVal (j);    
		i = document.cookie.indexOf(" ", i) + 1;    
		if (i == 0) break;   
	}  
	return null;
}

/* Set cookies */
function setCookie (name, value, expires, path, domain, secure) {
	var expiryDate = new Date();
	expiryDate.setTime(expiryDate.getTime() + (expires * 24 * 60 * 60 * 1000));
	
	document.cookie = name + "=" + escape (value) +
	((expires == null) ? "" : ("; expires=" + expiryDate.toGMTString())) + 
	((path == null) ? "" : ("; path=" + path)) +  
	((domain == null) ? "" : ("; domain=" + domain)) +    
	((secure == true) ? "; secure" : "");
}

/* Building cookies and tracking pathes */
function trackUser() {

	
	var new_gwuid = generateGuid();

	// Check if client accepts cookies
	var hasCookies;
	setCookie('gwCheck', new_gwuid, null, '/', null, false);
	if (getCookie('gwCheck') == new_gwuid) hasCookies = true;
	else hasCookies = false;

/*
	console.log('hasCookies = ' + hasCookies);
	console.log('new_gwuid = ' + new_gwuid);
*/
	var gwtype = 3;
	if(hasCookies){
		gwtype = 1;

		var gwuid = getCookie('gwuid');
		if(!gwuid || gwuid.length != 16){
			setCookie('gwuid', new_gwuid, 365*7, '/', null, false);
			gwuid = new_gwuid;
		}
	}	
	
	var trackingQuery = {
		'gwpath': window.location.href,
		'gwuid': gwuid,
		'gwtype': gwtype,
		'gwsite': gwsite
	}

	var gwURL = gwLink + '?'+ getConnectedString(trackingQuery, '&');
	document.write('<img src="' + gwURL + '" height="0" width="0">');	

}




