var http = null;
function update_cart() {
   var feld = new Array();
   var feld2 = new Array();
   for (i=0;i<document.form.elements.length;i++) {
      if (document.form.elements[i].type == 'text') {
         feld[i] = document.form.elements[i].value;
         feld2[i] = document.form.elements[i].name;
      }
   }

   var feld_neu = feld.join("@");
   //alert(feld_neu);
   var feld2_neu = feld2.join("@");
   //alert(feld2_neu);

   put_in_cart(feld2_neu,"update_cart",feld_neu);

}
function put_in_cart(produkt,action,anzahl) {
   if (window.XMLHttpRequest) {
      http = new XMLHttpRequest();
   }
   else if (window.ActiveXObject) {
      http = new ActiveXObject("Microsoft.XMLHTTP");
   }
   if (http != null) {
      if(action == "ins") {
         url = "warenkorb.php?action=add&prod_id="+produkt+"&quantity="+anzahl;
      }

      if(action == "update_cart") {
         url = "warenkorb.php?action=edit&index="+produkt+"&quantity="+anzahl;
      }

       if(action == "del") {
         url = "warenkorb.php?action=delete&index="+produkt;
      }
      
      if(action == "del_all") {
         url = "warenkorb.php?action=delete_all";
      }

      http.open("GET", url, true);
      http.onreadystatechange = function() { ausgeben(action,produkt); };
      http.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
      http.setRequestHeader('If-Modified-Since','Sat, 20 May 2006 08:00:00 GMT');
      //verhindert cachen durch setzen auf ein datum aus der vergangenheit: dokument veraltet also neuladen
      http.send(null);
   }
}

function ausgeben(action,produkt) {
   if (http.readyState == 1 || http.readyState == 2 || http.readyState == 3) {
      if(action == "del") {
         document.getElementById("mini").innerHTML = "";
         document.getElementById("bugfix").innerHTML = "<img src='images/cart.gif' style='float:left;margin-right:3px;' /><b>Removing article</b><br /><img src='images/ajax-loader.gif' />";

      }
      if(action == "ins") {
         document.getElementById("mini").innerHTML = "";
         document.getElementById("bugfix").innerHTML = "<img src='images/cart.gif' style='float:left;margin-right:3px;' /><b>Adding article</b><br /><img src='images/ajax-loader.gif' />";
      }

      if(action == "del_all") {
         document.getElementById("bugfix").innerHTML = "<img src='images/cart.gif' style='float:left;margin-right:3px;' /><b>Clearing cart</b><br /><img src='images/ajax-loader.gif' />";
         document.getElementById("mini").innerHTML = '<img src="images/cart.gif" style="float:left;margin-right:3px;" onmousedown="dragstart(this),draginit()" /><b>Your cart:</b><br />No articles! <br /><span class="cart_font">(cart is draggable)</span>';
      }

      if(action == "update_cart") {
         document.getElementById("mini").innerHTML = "";
         document.getElementById("bugfix").innerHTML = "<img src='images/cart.gif' style='float:left;margin-right:3px;' /><b>Updating cart</b><br /><img src='images/ajax-loader.gif' />";
      }
      if(action == "reload") {
         document.getElementById("bugfix").innerHTML = "<img src='images/cart.gif' style='float:left;margin-right:3px;' /><b>Loading cart</b><br /><img src='images/ajax-loader.gif' />";
      }
   }
   if (http.readyState == 4) {
      if(http.status != 200) {
         alert("A failure occured: "+http.status);
      }
      else {
         xml = http.responseXML;
         laenge = xml.getElementsByTagName("prod").length;

         if(laenge == 0) {
            text = '<img src="images/cart.gif" style="float:left;margin-right:3px;" onmousedown="dragstart(this),draginit()" /><b>Your cart:</b><br />No articles! <br /><span class="cart_font">(cart is draggable)</span>';
            this.document.getElementById("tr_indPayPalPayment").style.display = "";
         }

         else {

         test = new Array();
         test2 = new Array();
         test3 = new Array();
         test4 = new Array();

         text2 = "";

         test5 = xml.getElementsByTagName("anzahlArtikel")[0].firstChild.nodeValue;
         gesPreisBrutto = xml.getElementsByTagName("gesamtPreisBrutto")[0].firstChild.nodeValue;
         gesPreisBrutto = isNaN(gesPreisBrutto) ? gesPreisBrutto : Number(gesPreisBrutto).toFixed(2);
         text2 += "<tr><td colspan='4'>"+test5+" Articles marked!</td></tr>";
         text2 += "<tr><td class='cart_font' colspan='2'>total price (gross): </td>";
         text2 += "<td class='cart_font' align='right' nowrap='nowrap'>"+gesPreisBrutto+" \u20AC</td><td>&nbsp;</td></tr>";
         text2 += "<tr><td colspan='4'><hr /></td></tr>";
         document.getElementById("mini").innerHTML ="<table border='0'><tr><td colspan='3'><img src='images/cart.gif' style='float:left;margin-right:3px;' /><b>Your cart:</b></td></tr>"+text2+"</table>";

         text = "";

         text += "<form name='form'><table border='0'>";
         text += "<tr><td colspan='3'><img src='images/cart.gif' style='float:left;margin-right:3px;' /><b>Your cart:</b></td></tr>";

         for(i=0;i<laenge;i++) {
            text += "<tr>";
            test4[i] = xml.getElementsByTagName("anzahl")[i].firstChild.nodeValue;
            test[i] = xml.getElementsByTagName("prod")[i].firstChild.nodeValue;
            test2[i] = xml.getElementsByTagName("preis")[i].firstChild.nodeValue;
            test3[i] = xml.getElementsByTagName("prodIndex")[i].firstChild.nodeValue;
            preis = isNaN(test2[i]) ? test2[i] : parseInt(test2[i]).toFixed(2);
            text = text+"<td class='cart_font' width='25%'><input type='text' name="+test3[i]+" style='width:25px;border:1px solid black;' value='"+test4[i]+"' />";
            text = text+"<td class='cart_font' width='50%'>"+test[i]+"</td>";
            text = text+"<td class='cart_font' width='25%' align='right' nowrap='nowrap'>"+preis+" \u20AC</td>";
            text = text+"<td width='25%'><a href='javascript:put_in_cart(\""+test3[i]+"\",\"del\")'><img src='images/del2.gif' border='0' /></a></td>";
            text += "</tr>";
         }

         text += "<tr><td colspan='4'><a href='javascript:update_cart()'><img src='images/reload.gif' border='0' /></a></td></tr>";
         text += "<tr><td colspan='4'><hr /></td></tr>";
         gesPreis = xml.getElementsByTagName("gesamtPreis")[0].firstChild.nodeValue;
         gesPreis = isNaN(gesPreis) ? gesPreis : Number(gesPreis).toFixed(2);
         text += "<tr><td class='cart_font' colspan='2'>total price (net): </td>";
         text += "<td class='cart_font' align='right' nowrap='nowrap'>"+gesPreis+" \u20AC</td><td>&nbsp;</td></tr>";
         text += "<tr><td class='cart_font' colspan='2'>total price (gross): </td>";
         text += "<td class='cart_font' align='right' nowrap='nowrap'>"+gesPreisBrutto+" \u20AC</td><td>&nbsp;</td></tr>";
         text += "<tr><td colspan='4'><hr /></td></tr>";
         text += "<tr><td colspan='4'><table><tr><td><a href='javascript:put_in_cart(\"\",\"del_all\")'><img src='images/del.gif' border='0' /></a></td>";
         text += "<td><a href='cash_point.php'><img src='images/weiter.gif' border='0' /></a></td></tr></td></table>";
         text += "<tr><td colspan='4'><span class='cart_font'>(cart is draggable)</span></td></tr>";
         text += "</table></form>";
            this.document.getElementById("tr_indPayPalPayment").style.display = "none";
         }

         document.getElementById("bugfix").innerHTML = text;

         if(action == "del_all" ) {
            document.getElementById("bugfix").innerHTML = "";
            kleiner();
         }

         mom_hoehe = document.getElementById("Ausgabe").offsetHeight;
         if(action == "ins" && document.getElementById("mini").style.display == "none") {
            hoehe_schnell("bugfix");
         }
      }
   }
}

function reload_check() {
   if (window.XMLHttpRequest) {
      http = new XMLHttpRequest();
   }
   else if (window.ActiveXObject) {
      http = new ActiveXObject("Microsoft.XMLHTTP");
   }
   if (http != null) {
      http.open("GET", "warenkorb.php", true);
      http.onreadystatechange = function() { ausgeben("reload"); };
      http.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
      http.send(null);
   }
}

function open_window() {
   win = window.open("print_cart.php","Cart","height=400,width=440,left=300.resizable=yes");
   win.focus();
}