function setzeCookie(name, wert, domain, expires, path, secure){

 	expires = 365;
	expires = new Date((new Date()).getTime() + 60000 * 60 * 24 * expires).toGMTString();

    var cook = name+"="+unescape(wert);
	cook += (domain)  ? "; domain="  + domain : "";
	cook += (expires) ? "; expires=" + expires : "";
	cook += (path)    ? "; path="    + path : "; path=/";
	cook += (secure)  ? "; secure" : "";
	document.cookie = cook;
}

function eraseCookie(name, domain, path){
// Cookie wieder löschen

    Check = confirm("Wollen Sie die Merkliste und den Warenkorb wirklich löschen?");
    if (Check==true) {
        var cook=name+"=; expires=Thu, 01-Jan-70 00:00:01 GMT";
        cook += (domain) ? "domain="+domain : "";
        cook += (path) ? "path="+path : "; path=/";
        document.cookie = cook;
        window.location.href=document.URL; // wieder Seite anzeigen weil über href aufgerufen
    }
}

function getCookie(name){
// Cookieinhalt auslesen

   var i=0;  // Suchposition im Cookie
   var suche = name+"=";
   while (i<document.cookie.length){

      if (document.cookie.substring(i, i+suche.length)==suche){
         var ende = document.cookie.indexOf(";", i+suche.length);
         ende = (ende>-1) ? ende : document.cookie.length;
         var cook = document.cookie.substring(i+suche.length, ende);
         return (unescape(cook));
      }
      i++;
   }
   return "";
}

function checkCookie(){
	setzeCookie("CookieTest", "OK");
	if (!getCookie("CookieTest"))
		return false;
	else{
		eraseCookie("CookieTest")
		return true;
	}
}

function istZahl(n){
	s = ""+n;
	var ziffern = "0123456789";
	for (var i=0; i<s.length; i++)
		if (ziffern.indexOf(s.charAt(i))<0) return false;
	return true;
}

function einfuegen(anzahl, teppich_id, stil, typ, neueseite){

	var anzahl = (istZahl(anzahl) ? anzahl : "0"); // Auf Zahl prüfen
	var wert = getCookie("Warenkorb") + "";
	// Doppelte Einträge verhindern
    var checkstr = "1_" + teppich_id + "_" + stil + "_" + typ;    
	var checkdoppelt = wert.match(checkstr);
	if (checkdoppelt != null) {
        if (typ == "m") {
            if (neueseite != 1) { alert("Der Teppich ist bereits in der Merkliste."); }
            if (neueseite == 1) { window.location.href="/php_files/merkliste.php"; }
        } else {
            alert("Der Teppich ist bereits im Warenkorb.");
            if (neueseite == 1) { window.location.href="/php_files/bestellen.php"; } 
        }
	 	return
	}

    // einzelne Artikel mit @ trennen und Werte mit _ trennen
    if (wert == "undefined") {wert = "";}
	wert += "@" + anzahl + "_" + teppich_id + "_" + stil + "_" + typ; // an bisherigen Cookieinhalt dranhängen

    setzeCookie("Warenkorb", wert)    
    
	if (!document.cookie) {
        alert("Um diese Funktion nutzen zu können, müssen Sie Cookies aktivieren!"); 
    } else {
        if (typ == "m" && neueseite != 1) {
            //alert("Der Teppich wurde der Merkliste hinzugefügt."); 
        } else {
            //alert("Der Teppich wurde der Bestellliste hinzugefügt."); 
        }
    }
          
    if (neueseite == 1) {
        if (typ == "m") {
            window.location.href="/php_files/merkliste.php";  // wieder Seite anzeigen um Anzahl im Menü zu aktualisieren 
        } else {
            window.location.href="/php_files/bestellen.php";  // wieder Seite anzeigen um Anzahl im Menü zu aktualisieren 
        }
    } else {
        window.location.reload(false);
        //window.location.href=document.URL; // wieder Seite anzeigen um Anzahl im Menü zuakualisieren
    }
}

function einfuegenAnfragen(anzahl, teppich_id, stil, typ){

	var anzahl = (istZahl(anzahl) ? anzahl : "0") // Auf Zahl prüfen
	var wert = getCookie("Warenkorb") + "";
	// Doppelte Einträge verhindern
	var checkdoppelt = wert.match("_" + teppich_id + "_" + stil + "_" + typ);
	if (checkdoppelt != null) {
        window.location.href="/php_files/merkliste.php";
	 	return;
	}

    // einzelne Artikel mit @ trennen und Werte mit : trennen
    if (wert == "undefined") {wert = ""}
	wert += "@" + anzahl + "_" + teppich_id + "_" + stil + "_" + typ; // an bisherigen Cookieinhalt darnhängen

    setzeCookie("Warenkorb", wert);

	if (!document.cookie)
        {alert("Um diese Funktion nutzen zu können, müssen Sie Cookies aktivieren!"); }

    window.location.href="/php_files/merkliste.php";  // wieder Seite anzeigen um Anzahl im Menü zuakualisieren
}

function ersetzen(alteAnzahl, neueAnzahl, teppich_id) {

    var wert = getCookie("Warenkorb");
    wert = wert.replace("@" + alteAnzahl + "_" + teppich_id, "@" + neueAnzahl + "_" + teppich_id);
    setzeCookie("Warenkorb", wert);

    if (!document.cookie) {alert("Aktivieren Sie Cookies in Ihrem Browser!") ;}


}

function entfernen(anzahl, teppich_id, stil, typ){

    var anzahl = (istZahl(anzahl) ? anzahl : "0"); // Auf Zahl prüfen
    var wert = getCookie("Warenkorb");

    wert = wert.replace("@" + anzahl + "_" + teppich_id + "_" + stil + "_" + typ,"");    
    wert = wert.replace("@" + anzahl + "_" + teppich_id + "__" + typ,"");
    setzeCookie("Warenkorb", wert);
    
    if (!document.cookie) { alert("Aktivieren Sie Cookies in Ihrem Browser!"); }

    window.location.href=document.URL; // Seite neu laden
}

function countCookie(name) {
    // Anzahl der Teppich Merkliste
    if (name == "Merkliste") {
        var wert = getCookie("Warenkorb");
        Ergebnis = wert.match(/(@*_m)/gi);
        if (Ergebnis) {return Ergebnis.length;} else {return 0;}
    } else {
        var wert = getCookie("Warenkorb");
        Ergebnis = wert.match(/(@*_w)/gi);
        if (Ergebnis) {return Ergebnis.length;} else {return 0;}
    }
}

function countCookieW(name) {
    // Anzahl der Teppich Bestellliste
    var wert = getCookie(name);
    Ergebnis = wert.match(/@*_w/gi);
    if (Ergebnis) {return Ergebnis.length;} else {return 0;}
}

function indexPicHover(id) {
    element = document.getElementById("tb"+id);
    element.className="teppich_detail_bildbereich_spezial";  
    element = document.getElementById("tt"+id);
    element.className="teppich_detail_text_spezial";  
}
function indexPicOut(id) {
    element = document.getElementById("tb"+id);
    element.className="teppich_detail_bildbereich";  
    element = document.getElementById("tt"+id);
    element.className="teppich_detail_text";  
}

function changeFlag() {            
    window.location.href=window.location.href;  
}

function setAnzahlTeppiche(sObj) {
   var i = sObj.options[sObj.options.selectedIndex].value;
   setzeCookie("anzahlteppiche", i);
   if ( document.URL.search(/index.php.+/) != -1 ) { 
        window.location.href=document.URL; // Seite neu laden  
   } else {
        document.volltextSuche.submit();   
   }
  
}

function setSortierung(sObj) {
   var i = sObj.options[sObj.options.selectedIndex].value;
   setzeCookie("sort", i);
   if ( document.URL.search(/index.php.+/) != -1 ) { 
        window.location.href=document.URL; // Seite neu laden    
   } else {
        document.volltextSuche.submit();
   }
  
}  
