	
function checkDialogOpen(){
	if(checkTimeOut(30) == false){
		//alert("noch nicht");
		 return;
	}
	var result = hasParticpated();
	if(result == true){
		// kein Dialog öffnen	
	}else{
		// öffne Dialog
		$("#dialog-confirm").dialog('open');
	}
}

// ====== ====== ====== Cookie Functions ====== ====== ====== ======

function setCookie(c_name, value, exdays)
{
	if(exdays == 0) alert("exdays is null");
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays == 0) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name)
{
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++)
	{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}

/*
	Speichert einen Cookie für einen User ab,
	sodass nur einmal das Dialogfenster geöffnet wird
*/
function saveUser(){
	// Der letzte Wert bestimmt wie viele Tage der Cookie gespeichert wird
	setCookie("teilgenommen", "true", 180);
}

/*
	Überprüft der Cookie "teilgenommen" für einen User vorhanden ist und ob er auf "true" steht
*/
function hasParticpated(){
	var cok = getCookie("teilgenommen");
	if(cok != null && cok == "true"){
		//alert("der user hat teilgenommen");
		return true;
	}else{
		//alert("der user hat nicht teilgenommen");
		return false;
	}
}


/*
 check cookie "sid" which holds a timestamp when the user started
 the website.
 
 @argument int secs amount of seconds which determines the difference between cookie-
 and current-time
 
 return boolean true when difference between cookie-time and current-time is
 bigger than the given seconds
 
 example content of sid cookie: 
 201112201305052946669718
 201112201312534960219729
 
 die letzten 10 stellen sind zufällig
 davor die 6 stellen die uhrzeit
*/
function checkTimeOut(secs){
	var sidval = getCookie("sid");
	//var sidval = "201112201351494960219729"; // For Test
	var timestring = sidval.substring(sidval.length-16, sidval.length-10);
	//alert(timestring);
	var hours = timestring.substring(0,2);
	var minutes = timestring.substring(2,4);
	var seconds = timestring.substring(4,6);
	var oldTotal = parseInt(seconds) + parseInt(minutes) * 60 + parseInt(hours) * 60 * 60;
	//alert(hours+":"+minutes+":"+seconds);
	
	var now = new Date();
	var nowH = now.getHours();
	var nowM = now.getMinutes();
	var nowS = now.getSeconds();
	
	//alert(nowH+":"+nowM+":"+nowS);
	
	var nowTotal = nowH * 60 * 60 + nowM * 60 + nowS;
	
	//alert(nowTotal+"-"+oldTotal+"="+(nowTotal - oldTotal));
	
	return nowTotal - oldTotal >= secs;
}

