//  Cookie Functions -- "Night of the Living Cookie" Version (25-Jul-96)
// 10.09.2004	TT	#01	use cookie-expire of notepad
//	21.12.2006	TT	#02	alles.de-changes


// "Internal" function to return the decoded value of a cookie
function getCookieVal (offset) 
	{
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
	}

//  Function to return the value of the cookie specified by "name".
//  name - String object containing the cookie name.
//  returns - String object containing the cookie value, or null if
//  the cookie does not exist.
function GetCookie (name) 
	{
	if(is.ie) {var arg = name}
	else {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 to create or update a cookie.
function SetCookie (name,value,expires,path,domain,secure) 
	{document.cookie = name + "=" + escape (value) +
	 ((expires) ? "; expires=" + expires.toGMTString() : "") +
	 ((path) ? "; path=" + path : "") +
	 ((domain) ? "; domain=" + domain : "") +
	 ((secure) ? "; secure" : "");
	 // alert(document.cookie);
	}

//  Function to delete a cookie. (Sets expiration date to start of epoch)
function DeleteCookie (name,path,domain) 
	{
	if (GetCookie(name))
		{
    	document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-1970 00:00:01 GMT";
		}
	}

// Notizbuch Funktionen Version (2001-05-04: TG)	
//
//  Notizbuch lesen
function notepad_load()
	{
	var lstNOTEPAD = GetCookie('NOTEPADADS');
	if(is.ie){lstNOTEPAD = lstNOTEPAD.substr(1,lstNOTEPAD.length-1);}
	if (lstNOTEPAD == null)
		{alert('Notizbuch ist leer'); return null;}
	return lstNOTEPAD;
	}

// Angebot in Notizbuch legen
// TT 16.11.2001	No origin needed
function notepad_add(adno)
	{
	var s_cookie, n_check, n_l, n_r, key = adno;	
	if (is.ns)
		{if (isCookie() == false)
			{alert("Bitte aktivieren Sie 'Cookies akzeptieren' in Ihrem Browser.");
			 return;
			}
		}
	s_cookie = GetCookie('NOTEPADADS');	
	if (s_cookie == null) s_cookie='';
	if(is.ie)
		{s_cookie = s_cookie.substr(1,s_cookie.length-1);}
	n_check = s_cookie + key;
	if (n_check.length > 2048){alert('Notizbuch ist voll. Eintrag kann nicht gespeichert werden.');return;}
	if (s_cookie != '') s_cookie += ",";
	s_cookie += key;
	if (notepad_find(adno) != true)
		{SetCookie('NOTEPADADS',s_cookie,GetNotepadExpireDate(),'/',CookieDomain);
		 // alert("Angebot hinzugefügt.");
		}
	else
		{alert("Das Angebot befindet sich bereits im Notizbuch.");}
	}

// Angebot aus Notiuzbuch entfernen
// TT 16.11.2001	No origin needed
function notepad_remove(adno)
	{
	var s_cookie, n_l, n_r, key = adno;
	s_cookie = GetCookie('NOTEPADADS');
	if(is.ie){s_cookie = s_cookie.substr(1,s_cookie.length-1);}
   if (s_cookie.indexOf(key) != -1)
	   {
	   n_l = s_cookie.indexOf(key);
	   n_r = n_l + key.length;
		s_cookie = s_cookie.substring(0,n_l) +  s_cookie.substring(n_r,s_cookie.length);
		if(s_cookie.charAt(0) == ',') s_cookie = s_cookie.substring(1,s_cookie.length+1);
		if(s_cookie.charAt(s_cookie.length) == ',') s_cookie = s_cookie.substring(0,s_cookie.length);
		s_cookie = s_cookie.replace(/,,/,",","ALL");
      SetCookie('NOTEPADADS',s_cookie,GetNotepadExpireDate(),'/',CookieDomain);
  	  }
	}
	
// Angebot schon in Notizbuch?
// TT 16.11.2001	No origin needed
function notepad_find(adno)
	{
	var s_cookie, key = adno;	
	s_cookie = GetCookie('NOTEPADADS');
	if(s_cookie == null)	return false;
	if(is.ie){s_cookie = s_cookie.substr(1,s_cookie.length-1);}
	if (s_cookie.indexOf(key) != -1)return true;
	else return false;
	}

// Notizbuch entfernen	
function notepad_del()
	{DeleteCookie('NOTEPAD','/',null);	// delete old NOTEPAD without DOMAIN
	 DeleteCookie('NOTEPAD','/',CookieDomain);	// delete old NOTEPAD with DOMAIN
	 DeleteCookie('NOTEPADADS','/',CookieDomain);
	}

// Notizbuch hinzufügen / löschen
// TT 22.08.2001
// TT 16.11.2001	No origin needed
function notepad_modify(adno,b_CONFIRMandRELOAD)
	{// DeleteCookie('NOTEPAD','/',null); 	// delete old NOTEPAD without DOMAIN
	 if (b_CONFIRMandRELOAD == null)
		{b_CONFIRMandRELOAD = false;}
	 if (notepad_find(adno))		// remove ad
	 	{if (b_CONFIRMandRELOAD)
			{if (confirm("Wollen Sie die Anzeige aus dem Notizbuch löschen?"))
			 	{notepad_remove(adno);
			 	 location.reload();
				}
			 else
			 	// {eval("document.form_NOTEPAD.MODIFY" + adno + ".checked=true");}
				// {eval("document.formNOTEPAD_" + adno + ".MODIFY" + adno + ".checked=true");}
				{alert(adno); eval("document.formNOTEPAD.MODIFY" + adno + ".checked=true");}
			}
		 else
		 	{notepad_remove(adno);
			 // alert("Angebot aus Notizbuch gelöscht.");
			}
		}
	 else			// append ad
		{notepad_add(adno);}
	 return true;
	}

// Cookiedaten anzeigen
function ShowCookie()
	{
	alert('Cookie:\n'+document.cookie);
	return true;
	}

// clear complete notepad
function clearNotepad()
	{if (confirm("Wollen Sie das Notizbuch wirklich löschen?"))
	 	{notepad_del();
	 	 location.reload();
		}
	}


// #01 if checkbox clicked, set expire-cookie or delete it
	function SetNotepadExpire()
		{var expireDays = 14;
		 var expireDate = new Date(); 
		 expireDate.setTime(expireDate.getTime()+(expireDays*24*60*60*1000));
		 
		 if(document.EXPIREFORM.COOKIEEXPIRE.checked)
			{if (GetCookie("NOTEPADEXPIRE"))
			 	{DeleteCookie("NOTEPADEXPIRE","/",CookieDomain);}
			 SetCookie("NOTEPADEXPIRE",expireDays,expireDate,"/",CookieDomain);
			}
		 else
		 	{DeleteCookie("NOTEPADEXPIRE","/",CookieDomain);
			 expireDate=null;
			}
		 // ResetNotepadCookie with new expire
		 s_cookie = GetCookie('NOTEPADADS');
		 if(is.ie)	{s_cookie=s_cookie.substr(1,s_cookie.length-1);}
		 if(s_cookie == null)	return false;
		 SetCookie("NOTEPADADS",s_cookie,GetNotepadExpireDate(),"/",CookieDomain);
		 return true;
		}

// #01 if expire-cookie existes, use value (days) or return null (session) for expiredate
	function GetNotepadExpireDate()
		{var expireDate = new Date(); 
		 var expireDays = 0;
		 // #02 alles.de
		 if (ViewName != "alles")
		 	 expireDays=GetCookie("NOTEPADEXPIRE");
		 else
			expireDays = 360;
		 if (expireDays == null)
		 	{expireDate = null;}
		 else {
		 	if(is.ie) {
				strExpireDays = new String( expireDays );
				expireDays = strExpireDays.substr( 1, strExpireDays.length-1 );
			}
			expireDate.setTime( expireDate.getTime() + ( expireDays*24*60*60*1000 ) );
		 }
		 return expireDate;
		}
		
