//Store a cookie with the supplied name/value (path and expires are optional).
function CookieSet(Name, Value, Path, Expires) {
	var CookieString;
	CookieString = Name + "=" + escape(Value)
	if (Path) CookieString += "; path=" + Path;
	if (Expires) CookieString += "; expires=" + Expires.toGMTString();
	document.cookie = CookieString
}
//Answer the stored value for the named cookie.
function CookieGetValue(Name) {
	var Cookies;
	var NameValuePair;
	var CookieString = document.cookie;
	//Get an array of cookie pairs by splitting on ;
	Cookies = CookieString.split("; ");
	for (var i=0;i<Cookies.length;i++){
		NameValuePair = Cookies[i].split("=");
		if (NameValuePair[0] == Name) {
			return unescape(NameValuePair[1]);
		}
	}
}

var cookieURL = CookieGetValue('Leserpanelreferrer');
var referrerURL = document.referrer||'Referrer unknown';
var locationURL = unescape(location.search.substring(1));

var ergebnisURL = "";
var d = new Date();
d.setMonth(d.getMonth() + 1);
var zusatz =  "";

if (cookieURL == "" || cookieURL=='undefined' || cookieURL==null || (cookieURL.substring(0, 35).indexOf('leserpanel.de') != -1)){
    if ((referrerURL == '')||(referrerURL == 'Referrer unknown')||(referrerURL.substring(0, 35).indexOf('leserpanel.de') != -1)){
       if ((locationURL == '') || (locationURL.substring(0, 26).indexOf('leserpanel.de') != -1)){
       }
       else if (locationURL.indexOf('ref=') != -1) {
           zusatz = locationURL;
           ergebnisURL = locationURL ;
       }
       else{
          zusatz = locationURL;
       }
    }
    else if (referrerURL.indexOf('.') != -1){
        ergebnisURL =  '&ref=' + referrerURL;
        CookieSet('Leserpanelreferrer', locationURL + '&ref=' + referrerURL, "/", d);
	  cookieURL = CookieGetValue('Leserpanelreferrer');
        if (cookieURL == "" || cookieURL=='undefined' || cookieURL==null){
          zusatz = locationURL + '&ref=' + referrerURL;
        }
        else{
          zusatz = locationURL ;
        }
    }
}
else if (cookieURL.indexOf('.') != -1){
     ergebnisURL = cookieURL;
     if (locationURL != ''){
        zusatz = locationURL;
     }
}
//alert('cookie:'+cookieURL+'\nlocation:'+locationURL+'\nErgebnis'+ergebnisURL);

if (zusatz == "") {
}
else{
zusatz= '?' + zusatz;
}
//document.write('<body>');
//document.write('test:'+ergebnisURL);
//document.write('</body>');

