var expDays = 32;
var exp = new Date(); 
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

/*
var expShortDays = 1;
var expShort = new Date(); 
expShort.setTime(expShort.getTime() + (expShortDays*24*60*60*1000));
*/

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

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;
}

function SetCookie (name, value) { 
    var argv = SetCookie.arguments; 
    var argc = SetCookie.arguments.length; 
    var expires = (argc > 2) ? argv[2] : null; 
    var path = "/"; 
    var domain = (argc > 4) ? argv[4] : null; 
    var secure = (argc > 5) ? argv[5] : false; 
    document.cookie = name + "=" + escape (value) + 
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
    ((path == null) ? "" : ("; path=" + path)) + 
    ((domain == null) ? "" : ("; domain=" + domain)) + 
    ((secure == true) ? "; secure" : "");
}

function DeleteCookie (name) { 
    var exp = new Date(); 
    exp.setTime (exp.getTime() - 1); 
    var cval = GetCookie (name); 
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

function GetReferrer () {
	var theReferrer = document.referrer;
	return theReferrer;
}

function SetReferrerCookie (mytype) {
	// Abfrage SEM-Startseite
	// 21 ist der Seitentyp der SSE-Startseite
	if(mytype == "21" && getparameter('sem')) {
		mytype = "22";
	}
	var theReferrer = GetReferrer();
	var theReferrerCookie;
	if (GetCookie ("OnsiteReferrer") == null) {
		// Es gibt kein "intern" (1) mehr, da alle internen Seiten mit einem Seitentypen versehen sind
		if (theReferrer == "") { // || theReferrer.indexOf("wlw.de")!=-1) {
			theReferrerCookie = "OnsiteReferrer=1";
			document.cookie = theReferrerCookie + ';path=/';
		}
		else {
			theReferrerCookie = "OnsiteReferrer=" + mytype;
			document.cookie = theReferrerCookie + ';path=/';
		}
	} 
	var OnsiteReferrer = GetCookie ("OnsiteReferrer");
	return OnsiteReferrer;
}

function SetCountSitesCookie(){
	// wird hochgezaehlt bei jedem Aufruf einer WLW-Seite, wenn Umfrage aktiv.
	if(GetCookie ("OnsiteReferrerCountSites") != null) {
		var thecounts = parseInt(GetCookie ("OnsiteReferrerCountSites"));
		thecounts = thecounts +1;
		theCountCookie = "OnsiteReferrerCountSites=" + thecounts;
		document.cookie = theCountCookie + ';path=/';
	}
	//alert("Seitenanzahl: " + GetCookie ("OnsiteReferrerCountSites"));
}

function initUmfrage(land) {
	var s1 = GetCookie ("OnsiteReferrer");
	var theURL = "/sse/html/xx/umfrage.html?land=" +land + "&s1=" + s1;

	if(GetCookie ("OnsiteReferrerCountSites") == null) {
		theCountCookie = "OnsiteReferrerCountSites=1";
		document.cookie = theCountCookie + ';path=/';
	}

	if (GetCookie ("OnlineNutzerbefragung2011_03") == null && GetCookie ("OnsiteReferrerCountSites") > 1) { // == 2
		// Nutzer soll max. 2mal die Umfrage sehen
		/*if (GetCookie ("WLWFirstVisit") == null) {
			SetCookie ("WLWFirstVisit", "yes", exp);
			SetCookie ("OnlineNutzerbefragung2009", "yes", expShort);
		} else {*/
			SetCookie ("OnlineNutzerbefragung2011_03", "yes", exp);
		//}

		$("#lightboxumfrage").html('<img src="/image/wlw_dach/xx/lay_nutzerbefragung.gif?r=' + Math.random() + '" width="502" height="340" alt="" title="" usemap="#umfrage" /><map name="umfrage" id="umfrage"><area id="umfrageGo" coords="195,267,441,289" href="' + theURL + '" onclick="popupUmfrage(\'' + theURL + '\');return false;" target="_blank" alt="" title=""/><area id="umfrageNo" coords="194,300,441,320" href="#" onclick="theCookieEmpty();" alt="" title=""/><area id="umfrageZu" coords="414,0,501,13" href="#" onclick="theCookieEmpty();" alt="" title=""/></map>');
	  //$(".lightboxbackground").css("opacity", "0");
		$("#lightbox").lightbox({
			container: [
			            {id: "lightboxumfrage", onactivate: function(){}},
			            ],
			            links: []
		});
		$("#lightbox").trigger("activate", "lightboxumfrage");
		var closeit = "theCookieEmpty();return false;";
		$("#lightboxschliessen").attr("onclick", closeit);
	}
}

function initUmfrageNext() {
    var s1 = getparameter('s1');
    var land = getparameter('land');
    
    // Linkziel muss bei onclick zusammengebaut werden!
    $("#theumfrage").html('<img src="/image/wlw_dach/xx/lay_nutzerbefragung_next.gif?r=' + Math.random() + '" width="502" height="340" alt="" title="" usemap="#theumfrageImg" style="border:0"/><map name="theumfrageImg" id="theumfrageImg"><area id="theumfrageGo" coords="195,267,441,289" href="#" target="_blank" alt="" title="" onclick="UmfrageGo(\'' + land + '\',\'' + s1 + '\');window.setTimeout(\'window.close()\',1000);"/><area id="theumfrageNo" coords="194,300,441,320" href="#" onclick="window.close();" alt="" title=""/><area id="theumfrageZu" coords="414,0,501,13" href="#" onclick="window.close();" alt="" title=""/></map>');
}

function UmfrageGo(land,s1){
	var s2 = GetCookie ("OnsiteReferrerCountSites");
	var DestURL = "http://www.iqsn.de/L1/cawi/wlw1102/";
    var url;
    if(land == "DE") {
      url = "1_1_1/DE.php"
    }
    if(land == "AT") {
      url = "2_1_1/DE_AT.php"
    }
    if(land == "CH") {
      url = "3_1_1/DE_CH.php"
    }
    DestURL = DestURL + url + "?s1=" + s1 + "&s2=" + s2;
    // alert(DestURL);

	$("#theumfrageGo").attr("href", DestURL);
	theCookieEmpty();
	
}

function theCookieEmpty() {
	// Cookie wird nicht mehr gebraucht und auf leer gesetzt
	theCountCookie = "OnsiteReferrerCountSites=";
	document.cookie = theCountCookie + ';path=/';
}

/* Aufruf Umfrage */
function popupUmfrage(dokument) {
	myparams = "width=" + 520 + ",height=" + 360 + ",left=" + 50 + ",resizable=no,scrollbars=no,status=no,toolbar=no";
	myfenster = window.open(dokument, 'WLWUmfrage', myparams);
	myfenster.focus();
}

