var xmlHttp = false;

// create XMLHttpRequest-Instance
// ... for Internet Explorer
try {
    xmlHttp  = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
    try {
        xmlHttp  = new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e) {
        xmlHttp  = false;
    }
}
// ... for Mozilla, Opera and Safari
if (!xmlHttp  && typeof XMLHttpRequest != 'undefined') {
    xmlHttp = new XMLHttpRequest();
}

/* Rechnet Preis fuer Darstellung aus*/
function getPreis(ausfuehrung, e, edit) {
	var anzahl = document.getElementById("ausfuehrung_anzahl_"+ausfuehrung).value;
	if (anzahl != '') {
		if(IsNumeric(anzahl) && IsNumeric(ausfuehrung)) {
			if (anzahl > 0) {
				if (xmlHttp) {
					xmlHttp.open("POST", '/ajax/default.cfm', true);
					xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
					xmlHttp.send("action=1&ausfuehrung_ID="+ausfuehrung+"&anzahl="+anzahl);
					xmlHttp.onreadystatechange = getPreisRSC;
					if(e.keyCode == 13 || e.keyCode == 108) {
						if(edit) {
							addArticle(ausfuehrung, 1);
						}
						else {
							addArticle(ausfuehrung);
						}
					}
				}
				
			}
			else {
				var ausgabe = document.getElementById("ausgabe");
				ausgabe.className = "fehlermeldung";
				ausgabe.style.display = "block";
				ausgabe.innerHTML = "Bitte eine Menge angegeben";
				var preisText = document.getElementById("preistext_"+ausfuehrung);
				var preisWert = document.getElementById("preiswert_"+ausfuehrung);
				preisText.innerHTML = "";
				preisWert.innerHTML = "";
				setTimeout('Effect.Fade(\'ausgabe\');', 2000);
			}
		}
		else {
			var ausgabe = document.getElementById("ausgabe");
			ausgabe.className = "fehlermeldung";
			ausgabe.style.display = "block";
			ausgabe.innerHTML = "Es sind nur Zahlen erlaubt";
			setTimeout('Effect.Fade(\'ausgabe\');', 2000);
		}
	}
	else {
		var preisText = document.getElementById("preistext_"+ausfuehrung);
		var preisWert = document.getElementById("preiswert_"+ausfuehrung);
		preisText.innerHTML = "";
		preisWert.innerHTML = "";
	}
}

/* Wertet den Preis aus */

function getPreisRSC() {
	if (xmlHttp.readyState == 4) {
		var xmlDocument = xmlHttp.responseXML;
		if(xmlDocument.getElementsByTagName("ausf")[0]) {
			var ausfuehrung = xmlDocument.getElementsByTagName("ausf")[0].firstChild.nodeValue;
			var ausgabe = document.getElementById("ausgabe");
			var preisText = document.getElementById("preistext_"+ausfuehrung);
			var preisWert = document.getElementById("preiswert_"+ausfuehrung);
			if(ausfuehrung) {
				//Pruefen ob Fehler vorhanden
				var fehler = xmlDocument.getElementsByTagName("error");
				if (fehler[0]) {
					ausgabe.className = "fehlermeldung";
					ausgabe.style.display = "block";
					var wert = fehler[0].childNodes[0].nodeValue;
					try { 
						wert = decodeURIComponent(wert); 
					} 
					catch(e) { 
						wert = unescape(wert); 
					}
					ausgabe.innerHTML = wert;
					preisText.innerHTML = "";
					preisWert.innerHTML = "";
					setTimeout('Effect.Fade(\'ausgabe\');', 2000);
				}
				else {
					var preis = xmlDocument.getElementsByTagName("preis");
					if(preis[0]) {
						var wert = preis[0].getElementsByTagName("wert")[0].firstChild.nodeValue;
						try { 
							wert = decodeURIComponent(wert); 
						} 
						catch(e) { 
							wert = unescape(wert); 
						}
						preisText.innerHTML = "";
						ausgabe.style.display = "none";
						preisText.innerHTML = "";
						preisWert.innerHTML = wert;
					}
					else {
						ausgabe.className = "fehlermeldung";
						ausgabe.style.display = "block";
						ausgabe.innerHTML = "Keinen Preis mitgegeben";
						preisText.innerHTML = "";
						preisWert.innerHTML = "";
						setTimeout('Effect.Fade(\'ausgabe\');', 2000);
					}
				}
			}
		}
	}
}

/* Fuegt dem Warenkorb einen Artikel hinzu */

function addArticle(ausfuehrung,spezial) {
	var anzahl = document.getElementById("ausfuehrung_anzahl_"+ausfuehrung).value;
	if(IsNumeric(anzahl) && IsNumeric(ausfuehrung)) {
		if (anzahl > 0) {
			if (xmlHttp) {
				xmlHttp.open("POST", '/ajax/default.cfm', true);
				xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				if(spezial) {
				xmlHttp.send("action=2&ausfuehrung_ID="+ausfuehrung+"&anzahl="+anzahl+"&spezial=1");
				}
				else {
				xmlHttp.send("action=2&ausfuehrung_ID="+ausfuehrung+"&anzahl="+anzahl);
				}
				xmlHttp.onreadystatechange = addArticleRSC;
			}
		}
		else {
			var ausgabe = document.getElementById("ausgabe");
			ausgabe.className = "fehlermeldung";
			ausgabe.style.display = "block";
			ausgabe.innerHTML = "Bitte eine Menge angegeben";
			var preisText = document.getElementById("preistext_"+ausfuehrung);
			var preisWert = document.getElementById("preiswert_"+ausfuehrung);
			preisText.innerHTML = "";
			preisWert.innerHTML = "";
			setTimeout('Effect.Fade(\'ausgabe\');', 2000);
		}
	}
	else {
		var ausgabe = document.getElementById("ausgabe");
		ausgabe.className = "fehlermeldung";
		ausgabe.style.display = "block";
		ausgabe.innerHTML = "Es sind nur Zahlen erlaubt";
		setTimeout('Effect.Fade(\'ausgabe\');', 2000);
	}
}

function addArticleRSC() {
	if (xmlHttp.readyState == 4) {
		var xmlDocument = xmlHttp.responseXML;
		var ausfuehrung = xmlDocument.getElementsByTagName("ausf")[0].firstChild.nodeValue;
		if (ausfuehrung) {
			var ausgabe = document.getElementById("ausgabe");
			ausgabe.className = "okmeldung";
			ausgabe.style.display = "block";
			setTimeout('Effect.Fade(\'ausgabe\');', 2000);
			if(xmlDocument.getElementsByTagName("spezial")[0]) {
				ausgabe.innerHTML = "Artikelposition erfolgreich ge&auml;ndert";
			}
			else {
				//Felder leeren
				ausgabe.innerHTML = "Artikel erfolgreich in den Warenkorb gelegt";
				document.getElementById("ausfuehrung_anzahl_"+ausfuehrung).value = "";
				document.getElementById("preistext_"+ausfuehrung).innerHTML = "";
				document.getElementById("preiswert_"+ausfuehrung).innerHTML= "";
			}
			getWarenkorb();
		}
	}
}

/* Erstellt den Warenkorb */

function getWarenkorb(reload) {
	if (xmlHttp) {
		xmlHttp.open("POST", '/ajax/default.cfm', true);
		xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		if (reload) {
			xmlHttp.send("action=3&reload=true");
		}
		else {
			xmlHttp.send("action=3");
		}
		xmlHttp.onreadystatechange = WarenkorbRSC;
	}
}

function WarenkorbRSC() {
	if (xmlHttp.readyState == 4) {
		var xmlDocument = xmlHttp.responseXML;
		if (xmlDocument) {
			//Pruefen ob Fehler vorhanden
			var fehler = xmlDocument.getElementsByTagName("error");
			if (fehler[0]) {
				ausgabe.className = "fehlermeldung";
				ausgabe.style.display = "block";
				var wert = fehler[0].childNodes[0].nodeValue;
				try { 
					wert = decodeURIComponent(wert); 
				} 
				catch(e) { 
					wert = unescape(wert); 
				}
				ausgabe.innerHTML = wert;
				preisText.innerHTML = "";
				preisWert.innerHTML = "";
				setTimeout('Effect.Fade(\'ausgabe\');', 2000);
			}
			else {
				var preis = "";
				var anzahl = "";
				if(xmlDocument.getElementsByTagName("preis")[0]) {
					var preis = xmlDocument.getElementsByTagName("preis");
					preis = preis[0].childNodes[0].nodeValue;
				}
				if(xmlDocument.getElementsByTagName("anzahl")[0]) {
					var anzahl = xmlDocument.getElementsByTagName("anzahl");
					anzahl = anzahl[0].childNodes[0].nodeValue;
				}
				if(xmlDocument.getElementsByTagName("total")[0]) {
					var total = xmlDocument.getElementsByTagName("total");
					total = total[0].childNodes[0].nodeValue;
				}
				if(anzahl && preis && IsNumeric(anzahl) && IsNumeric(preis) && total && IsNumeric(total)) {
					var sideBar = document.getElementById("shopnavigation");
					var Warenkorbdelete = document.getElementById("Warenkorb");
					if(Warenkorbdelete) {
						sideBar.removeChild(Warenkorbdelete);
					}
					var Warenkorb = document.createElement("li");
					Warenkorb.id = "Warenkorb";
					Warenkorb.innerHTML = '<a class="head" href="/warenkorb/">Dein Warenkorb<span class="rechts">&gt;</span></a>';
				
					var ul = document.createElement("ul");
					
					//Anzahl Artikel
					var links = document.createElement("li");
					links.appendChild(document.createTextNode("Anzahl Artikel"));
					var rechts = document.createElement("span");
					rechts.className = "rechts";
					rechts.appendChild(document.createTextNode(anzahl +" Stk"));
					links.appendChild(rechts);
					ul.appendChild(links);
					
					//Auf Warenkorbseite updaten
					var anzahl_warenkorb = document.getElementById("anzahl_artikel");
					if(anzahl_warenkorb) {
						anzahl_warenkorb.innerHTML = anzahl + " Stk";
					}
					
					//Total Kosten
					var links = document.createElement("li");
					links.appendChild(document.createTextNode("Total Kosten"));
					var rechts = document.createElement("span");
					rechts.className = "rechts";
					rechts.appendChild(document.createTextNode(preis +" CHF"));
					links.appendChild(rechts);
					ul.appendChild(links);
					
					//Auf Warenkorbseite updaten
					var preis_warenkorb = document.getElementById("total_kosten");
					if(preis_warenkorb) {
						preis_warenkorb.innerHTML = preis + " CHF";
					}
					
					//Porto
					if(xmlDocument.getElementsByTagName("porto")[0]) {
						var porto = xmlDocument.getElementsByTagName("porto");
						porto = porto[0].childNodes[0].nodeValue;
						
							var links = document.createElement("li");
							links.appendChild(document.createTextNode("Porto"));
							var rechts = document.createElement("span");
							rechts.className = "rechts";
							if(IsNumeric(porto)) {
								rechts.appendChild(document.createTextNode(porto+" CHF"));
							}
							else {
								rechts.appendChild(document.createTextNode(porto));
							}
							links.appendChild(rechts);
							ul.appendChild(links);
							
							//Auf Warenkorbseite updaten
							var porto_warenkorb = document.getElementById("porto");
							if(porto_warenkorb) {
								if(IsNumeric(porto)) {
									porto_warenkorb.innerHTML = porto + " CHF";
								}
								else {
									porto_warenkorb.innerHTML = porto;
								}
							}
						
					}
					
					//MWST
					if(xmlDocument.getElementsByTagName("mwst")[0]) {
						var mwst = xmlDocument.getElementsByTagName("mwst");
						mwst = mwst[0].childNodes[0].nodeValue;
						if(IsNumeric(mwst)) {
							var links = document.createElement("li");
							links.appendChild(document.createTextNode("Mwst"));
							var rechts = document.createElement("span");
							rechts.className = "rechts";
							rechts.appendChild(document.createTextNode(mwst+" CHF"));
							links.appendChild(rechts);
							ul.appendChild(links);
							
							//Auf Warenkorbseite updaten
							var mwst_warenkorb = document.getElementById("mehrwertsteuer");
							if(mwst_warenkorb) {
								mwst_warenkorb.innerHTML = mwst + " CHF";
							}
						}
					}
					
					//Total
					var links = document.createElement("li");
					links.appendChild(document.createTextNode("Total"));
					var rechts = document.createElement("span");
					rechts.className = "rechts";
					rechts.appendChild(document.createTextNode(total+" CHF"));
					links.appendChild(rechts);
					ul.appendChild(links);
					
					//Auf Warenkorbseite updaten
					var total_komplett_warenkorb = document.getElementById("total_kosten_komplett");
					if(total_komplett_warenkorb) {
						total_komplett_warenkorb.innerHTML = total + " CHF";
					}
					
					Warenkorb.appendChild(ul);
					//Link Kasse unten
					var linker = document.createElement("li");
					linker.innerHTML = '<a class="head" href="/warenkorb/benutzerdaten/">Zur Kasse gehen<span class="rechts">&gt;</span></a>';
					Warenkorb.appendChild(linker);
					
					/*//Link Warenkorb leeren
					var linker = document.createElement("li");
					var linkingul = document.createElement("ul");
					var linkingli = document.createElement("li");
					if(xmlDocument.getElementsByTagName("reload")[0]) {
						linkingli.innerHTML = '<a href="javascript:flushWarenkorb(\'reload\');" class="subhead">Warenkorb leeren<span class="rechts">&gt;</span></a>';
					}
					else {
						linkingli.innerHTML = '<a href="javascript:flushWarenkorb();" class="subhead">Warenkorb leeren<span class="rechts">&gt;</span></a>';
					}
					linkingul.appendChild(linkingli);
					linker.appendChild(linkingul);
					Warenkorb.appendChild(linker);*/
				
					sideBar.appendChild(Warenkorb);
				}
				else {
					var sideBar = document.getElementById("shopnavigation");
					var Warenkorbdelete = document.getElementById("Warenkorb");
					if(Warenkorbdelete) {
						sideBar.removeChild(Warenkorbdelete);
					}
					var ausgabe = document.getElementById("ausgabe");
					if (ausgabe.innerHTML != 'Warenkorb erfolgreich geleert') {
						flushWarenkorb('reload');
						/*ausgabe.className = "fehlermeldung";
						ausgabe.style.display = "block";
						ausgabe.innerHTML = "Keine Artikel im Warenkorb";
						setTimeout('Effect.Fade(\'ausgabe\');', 2000);*/
					}
				}
			}
		}
	}
}

/* Leert den Warenkorb */

function flushWarenkorb(reload) {
	
	if (xmlHttp) {
		xmlHttp.open("POST", '/ajax/default.cfm', true);
		xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		if (reload) {
			xmlHttp.send("action=4&reload=true");
		}
		else {
			xmlHttp.send("action=4");
		}
		xmlHttp.onreadystatechange = flushWarenkorbRSC;
	}
}
function flushWarenkorbRSC() {
	if (xmlHttp.readyState == 4) {
		if (document.getElementById("Warenkorb")) {
			Effect.Fade('Warenkorb', { duration: 0.5, from: 1, to: 0 });
		}
		var xmlDocument = xmlHttp.responseXML;
		if(xmlDocument.getElementsByTagName("reload")[0]) {
			Effect.Fade('content_warenkorb', { duration: 0.5, from: 1, to: 0 });
			setTimeout('window.location.reload();', 500);
		}
		/*else {
			var ausgabe = document.getElementById("ausgabe");
			ausgabe.className = "okmeldung";
			ausgabe.style.display = "block";
			ausgabe.innerHTML = "Warenkorb erfolgreich geleert";
			setTimeout('Effect.Fade(\'ausgabe\');', 500);
			//setTimeout('getWarenkorb();', 600);
		}*/
		
	}
}

/* Loescht Artikel */

function deleteArticle(ausfuehrung) {
	if (xmlHttp && IsNumeric(ausfuehrung)) {
		xmlHttp.open("POST", '/ajax/default.cfm', true);
		xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlHttp.send("action=5&ausfuehrung_ID="+ausfuehrung);
		xmlHttp.onreadystatechange = deleteArticleRSC;
	}
}

function deleteArticleRSC() {
	if (xmlHttp.readyState == 4) {
		var xmlDocument = xmlHttp.responseXML;
		if (xmlDocument) {
			if(xmlDocument.getElementsByTagName("ausfuehrung")[0]) {
				var ausfuehrung = xmlDocument.getElementsByTagName("ausfuehrung")[0].childNodes[0].nodeValue;
				Effect.DropOut('ausfuehrung_'+ausfuehrung);
				getWarenkorb();
			}
			else {
				var ausgabe = document.getElementById("ausgabe");
				ausgabe.className = "fehlermeldung";
				ausgabe.style.display = "block";
				ausgabe.innerHTML = "Artikel konnte nicht entfernt werden";
				setTimeout('Effect.Fade(\'ausgabe\');', 2000);
			}
		}
	}
}

/* Stellt Frage fuer Duplikat oder nicht */
/*function editArticleAusfuehrung(alte_Ausfuehrung, selected) {
	var selectFeld = document.getElementById("Ausfuehrungselect_"+alte_Ausfuehrung);
	if(selectFeld.selectedIndex > 1) {
		var closeLink = document.getElementById("closeLink");
		closeLink.href = "javascript:closeArticleAusfuehrung('"+alte_Ausfuehrung+"', '"+selected+"')";
		var aendern = document.getElementById("aendernButton");
		aendern.onclick = function() {editAusfuehrung(alte_Ausfuehrung)};
		
		new Effect.Appear('greyScreen', {duration: 0.5, from: 0, to: 0.6});
		new Effect.Appear('addNewArticle', {duration: 0.5, from: 0, to: 1});
		setTimeout('ausrichten();', 100);
	}
	else {
		selectFeld.selectedIndex = selected;
	}
}*/

/* Aendert eine Ausfuehrung mit AJAX im Warenkorb */ 

function editAusfuehrung(alte_Ausfuehrung) {
	var formDiv = document.getElementById("ausfuehrung_"+alte_Ausfuehrung);
	var selected = document.getElementById("Ausfuehrungselect_"+alte_Ausfuehrung).selectedIndex;
	var ausgewaehlt = document.getElementById("Ausfuehrungselect_"+alte_Ausfuehrung)[selected].value;
	if (xmlHttp && IsNumeric(ausgewaehlt)) {
		xmlHttp.open("POST", '/ajax/default.cfm', true);
		xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlHttp.send("action=6&neue_ausfuehrung_ID="+ausgewaehlt+"&alte_ausfuehrung_ID="+alte_Ausfuehrung);
		xmlHttp.onreadystatechange = editAusfuehrungRSC;
	}
}

function editAusfuehrungRSC() {
	if (xmlHttp.readyState == 4) {
		window.location="/warenkorb/";
	}
}

/* Schliesst ArticleAusfuehrung */ 
/*function closeArticleAusfuehrung(ausfuehrung_ID, selected) {
	document.getElementById("Ausfuehrungselect_"+ausfuehrung_ID).selectedIndex = selected;
	new Effect.Fade('greyScreen', {duration: 0.5, from: 0.6, to: 0});
	new Effect.Fade('addNewArticle', {duration: 0.5, from: 1, to: 0});
}*/
/* Leitet automatisch je nach Auswahl zu einer Ausfuehrung weiter */

function updateProdukteausfuehrung() {
	var selected = document.getElementById("Ausfuehrung").selectedIndex;
	var ausgewaehlt = document.getElementById("Ausfuehrung")[selected].value;
	if (ausgewaehlt != ""){
		window.location="/"+ausgewaehlt;
	}
}
/* Fuegt ein Element nach einem Element ein */

function insertAfter( referenceNode, newNode )
{
	referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}
/* Prueft ob Zahl eine Nummer ist */

function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }

function sendForm(form) {
	document.getElementsByName(form)[0].submit();
}

// Wechselt das Register

function changeRegister(position) {
	var register_1 = document.getElementById("register_1");
	var register_2 = document.getElementById("register_2");
	var register_3 = document.getElementById("register_3");
	var inhalt_register_1 = document.getElementById("inhalt_register_1");
	var inhalt_register_2 = document.getElementById("inhalt_register_2");
	var inhalt_register_3 = document.getElementById("inhalt_register_3");
	
	if (position == 1) {
		register_1.className = "register_aktiv";
		register_2.className = "register_inaktiv";
		register_3.className = "register_inaktiv";
		inhalt_register_1.style.display = "block";
		inhalt_register_2.style.display = "none";
		inhalt_register_3.style.display = "none";
	}
	else if (position == 2) {
		register_1.className = "register_inaktiv";
		register_2.className = "register_aktiv";
		register_3.className = "register_inaktiv";
		inhalt_register_1.style.display = "none";
		inhalt_register_2.style.display = "block";
		inhalt_register_3.style.display = "none";
	}
	else if (position == 3) {
		register_1.className = "register_inaktiv";
		register_2.className = "register_inaktiv";
		register_3.className = "register_aktiv";
		inhalt_register_1.style.display = "none";
		inhalt_register_2.style.display = "none";
		inhalt_register_3.style.display = "block";
	}
}



