// ======================================================================
//
// JScript Source File -- copyright 2008 derStandard.at GmbH
// 
// NAME: Survey.js
//
// AUTHORS: Mario Zoth, derStandard.at GmbH & Robert Knienider, EDV-Dienstleistungen
// DATE   : 09.04.2008 v1.0
//          15.01.2010 v1.1 noframe
// 
// COMMENT: Site Surveys 2008-2010
// 
// sources are not to be used in other websites.
// for further information contact webmaster@derStandard.at
// =========================================================================

function Survey(sX, tagSrc) // sX = Teilstring für nugg.ad oder OEWA, tagSrc = Source für den Fragebogen Tag.
{
	this.cName = 'FRAGEBOGEN_'; // Cookiename für Fragebogen.
	this.cNameFull = this.cName + sX + ':'; // Voller Cookiename für Fragebogen (OEWA oder nugg.ad).
	this.cNameCount = 'a'; // Namenserweiterung für Zaehl Cookie.
	this.cNameTime = 't'; // Namenserweiterung für Zeitstempel Cookie.
	this.now = new Date(); // Aktuelles Datum.
	this.expDate = new Date();
	this.expDate.setDate(this.expDate.getFullYear() + 15, 00, 00, 00, 000);
	this.limiterRandom = Math.floor(Math.random()*20); // Zusfallszahl für den Lastminderer - 10 = Auslieferung mit 10%iger Wahrseinlichzeit.
	this.surveyTag = '<scr'+'ipt type="text/javascript" src="' + tagSrc + '"><\/scr'+'ipt>';

	// Ueberprueft ob Zaehl-Cookie schon vorhanden, wenn nein wird ein Cookie gesetzt (Variante[Anzahl oder Zeitstempel], Wert).
	this.checkCookie = function(v,startValue)
	{
		if (document.cookie.indexOf(v) == -1)
		{
			this.setCookie(v,startValue);
		}
	};
	
	// Setzt ein Cookie mit (Cookiename, Wert)
	this.setCookie = function(cName,cValue)
	{
		document.cookie = cName + '=' + cValue + '; path=/; expires=' + this.expDate.toGMTString();
	};
	
	// Liest besimmten Cookie-Wert aus (Cookiename).
	this.getCookie = function(cName)
	{
		cStart=document.cookie.indexOf(cName + "=");
		var r = '';
		if (cStart != -1)
		{ 
			cStart=cStart + cName.length+1; 
			cEnd=document.cookie.indexOf(";",cStart);
			if (cEnd==-1) cEnd=document.cookie.length;
			r = unescape(document.cookie.substring(cStart,cEnd));
		}
		return r;
	};
	
	// Ueberprueft ob die letzte Fragebogen-Auslieferung 24h zurueck liegt.
	this.validSurveyTime = function()
	{
		var valid = new Date(this.getCookie(this.cNameFull + this.cNameTime));
		valid.setDate(valid.getDate() + 1); // Funktioniert auch bei Monats- und Jahreswechsel.
		return valid <= this.now; // false wenn 'valid' kein gültiges Datum enthält.
	};
	
	// Ueberprueft ob ein Fragebogen ausgeliefert werden soll.
	this.checkSurvey = function()
	{
		var valid = false;
		// Wurde noch kein Fragebogen ausgeliefert?
		if (this.getCookie(this.cNameFull + this.cNameCount) < 1)
		{
			// Ist die Auslieferung lange genung nach der letzten und Last-gemindert?
			var d = new Date();
			d.setDate(d.getDate() - 1);
			this.checkCookie(this.cNameFull + this.cNameTime, d);
			if (this.validSurveyTime() && this.limiter())
			{
				valid = true;
			}
		}
		return valid;
	};
	
	// Lastminderer - siehe property limiterRandom
	this.limiter = function()
	{
		var valid = (this.limiterRandom == 0) ? true : false;
		return valid;
	};
	
	// Fragebogen-Aufruf merken und ausführen
	this.makeSurvey = function()
	{
		this.setCookie(this.cNameFull + this.cNameCount, Number(this.getCookie(this.cNameFull+this.cNameCount)) + 1);
		this.setCookie(this.cNameFull + this.cNameTime, this.now);
		document.write(this.surveyTag);
	};

	// Fragebogen aufrufen, wenn
	//  der Client Cookies erlaubt UND
	//  auf der Seite Prototype eingebunden ist UND
	//  die Seite die Navigation enthält (im Gegensatz zu Spezialseiten, z.B. Posten)
	if (navigator.cookieEnabled && (typeof Prototype != 'undefined') && $('pageTop'))
	{
		// Nach Zeit und Last zulässig?
		this.checkCookie(this.cNameFull + this.cNameCount,0);
		if (this.checkSurvey())
		{
			this.makeSurvey();
		}
	}

}