// Start Cookie Handling
function setWarenkorb(name, wert, domain, path, secure){
   var cook = (name) ? (name + "=" + unescape(wert) ): ("name=" + unescape(wert));
   cook += (domain) ? "; domain="+ domain : "";
   //cook += (discard) ? "; discard " : "";
   //cook += (expires) ? "; expires="+expires : "";
   cook += (path) ? "; path="+path : "/;";
   cook += (secure) ? "; secure " : "";
   document.cookie = cook;
}
function delWarenkorb(name, wert, domain, expires, path, secure){
   var cook = (name) ? (name + "=" + unescape(wert) ): ("name=" + unescape(wert));
   cook += (domain) ? "; domain="+ domain : "";
   //cook += (discard) ? "; discard " : "";
   cook += (expires) ? "; expires="+expires : "";
   cook += (path) ? "; path="+path : "/;";
   cook += (secure) ? "; secure " : "";
   document.cookie = cook;
}
function getWarenkorb( check_name ) {
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		a_temp_cookie = a_all_cookies[i].split( '=' );
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}
// Ende Cookie Handling
// Prüft ob Cookies erlaubt sind
function cookiecheck() {
 if (navigator.cookieEnabled == true) {
   //alert("Cookies erlaubt");
 } else if (navigator.cookieEnabled == false) {
   alert("Um Artikel in den Warenkorb zu legen, erlauben Sie bitte Cookies in Ihren Browsereinstellungen.");
 } else {
   //alert("Verrate ich nicht.");
}
}
// Prüft ob Cookie gesetzt andernfalls wird es gesetzt
function artikelhinzu(status, produkt, plz, kwhht, kwhnt, zaehler, nenn, prodtype, jahr, monat, vpreis, gpreis, intype) {
	//cookiecheck();
	if(getWarenkorb('warenkorb_id')==null) {
		setWarenkorb('warenkorb_id','ESAG-87U8DL', null,"/");
		var wid = getWarenkorb('warenkorb_id');
		if(getWarenkorb('warenkorb_id')==null) {
			alert("Um Artikel in den Warenkorb zu legen, erlauben Sie bitte Cookies in Ihren Browsereinstellungen.");
		}
		else {
		$('bestelldialog_wrapper').style.display = "none";
		$('bestelldialog_wrapper2').style.display = "block";
		sendArtikel(status, produkt, wid, plz, kwhht, kwhnt, zaehler, nenn, prodtype, jahr, monat, vpreis, gpreis, intype);
		}
	} 
	else 
	{
		$('bestelldialog_wrapper').style.display = "none";
		$('bestelldialog_wrapper2').style.display = "block";
		var wid = getWarenkorb('warenkorb_id');
		sendArtikel(status, produkt, wid, plz, kwhht, kwhnt, zaehler, nenn, prodtype, jahr, monat, vpreis, gpreis, intype)
	}
}
// Agent für Warenkorb und Artikel hinzufügen per AJAX gestartet
function sendArtikel(status, produkt, wid, plz, kwhht, kwhnt, zaehler, nenn, prodtype, jahr, monat, vpreis, gpreis, intype) {
if(produkt!=null) {
	var thiswarenkorb = wid;
	var thisartikel = produkt;
	var thisplz = plz;
	var thisstatus = status;
	var thiskwhht = kwhht;
	var thiskwhnt = kwhnt;
	var thistyp = prodtype;
	var thiszaehlertyp = zaehler;
	var thisnenn = nenn;
	var thisjahr = jahr;
	var thismonat = monat;
	var thisvpreis = vpreis;
	var thisgpreis = gpreis;
	var thisintype = intype;
	var url = 'asavewarenkorb.html?OpenAgent&art='+thisartikel+'&wid='+thiswarenkorb+'&plz='+thisplz+'&status='+thisstatus+'&kwhht='+thiskwhht+'&kwhnt='+thiskwhnt+'&typ='+thistyp+'&zaehlertyp='+thiszaehlertyp+'&nenn='+thisnenn+'&jahr='+thisjahr+'&monat='+thismonat+'&vpreis='+thisvpreis+'&gpreis='+thisgpreis+'&intype='+thisintype; 
// AJAX REQUEST UM ARTIKEL ANZULEGEN
	new Ajax.Request(url, {
		method: 'post',
		//parameters:  pars,
		onSuccess: function(originalRequest) {
   			var newData = originalRequest.responseText;
			//alert(newData);
			refreshCount();
		},
		onFailure: function(request) {
		}
	});
} else {alert("kein Artikel ausgewählt");}
}
function refreshCount() {
// AJAX REQUEST WARENKORB ZU ZÄHLEN
var url2 = 'countwarenkorb.html?ReadForm'; 
	new Ajax.Request(url2, {
		method: 'post',
		//parameters:  pars,
		onSuccess: function(originalRequest) {
   			var newData = originalRequest.responseText;
			$('warenkorb_top_wrapper').innerHTML = "<div id='warenkorb_top'><div id='warenkorb_inhalt'><span id='warenkorb_count'>Warenkorb ("+newData.substring(0,newData.length-1)+")</span></div></div>";
		},
		onFailure: function(request) {
		}
	});
}
// Agent für Warenkorb und Artikel hinzufügen per AJAX gestartet
function refreshArtikel(aid,artikelid) {
	var thiszaehlertyp = $(artikelid+'_tmp_tZaehler').value;
	var thiskwhht = $(artikelid+'_tmp_tKWH_HT').value;
	var thiskwhnt = $(artikelid+'_tmp_tKWH_NT').value;
//alert(aid+" "+thiszaehlertyp+" "+thiskwhht+" "+thiskwhnt);
var url ='arefreshwarenkorb.html?OpenAgent&aid='+aid+'&zaehler='+thiszaehlertyp+'&kwhht='+thiskwhht+'&kwhnt='+thiskwhnt;
// AJAX REQUEST ARTIKEL ZU AKTUALISIEREN
	new Ajax.Request(url, {
		method: 'post',
		//parameters:  pars,
		onSuccess: function(originalRequest) {
   			//var newData = originalRequest.responseText;
			//alert(newData);
			location.reload()
			//$(artikelid+'_preise').innerHTML = newData;
		},
		onFailure: function(request) {
		}
	});
}
// WARENKORB AN DIREKT-SERVICE ÜBERMITTELN
function popSubmit(form) 
{ 
    var popName = "DirektService"; 
    var popStyle = "screenX=0,screenY=0,left=100,top=50,dependent=yes,status=yes,scrollbars=yes,toolbar=no,width=1050,height=750,location=yes,resizable=yes"; 
    form.action = "https://www2.enso.de/powercommerce/action/pcNewCustomer"; 
    form.target = popName; 
    window.open('about:blank',popName,popStyle); 
	// Warenkorb Cookie löschen
	var wid = getWarenkorb('warenkorb_id');
	var now = new Date();
	delWarenkorb('warenkorb_id',wid, null, now.toGMTString(),"/");
   location.href = '/enso/home.nsf/';
}
