function addProduct(pid){
    var opt = {
      // Use POST
      method: 'post',
      // Send this lovely data
      postBody: 'pid='+pid+'&user_fnc=addProduct' ,
      // Handle successful response
      onSuccess: addProductSuccess
   }
   new Ajax.Request('cartserver.php', opt);
}

function addProductSuccess(t,json){
   if(json){
      //alert(json[0]["name"])
      var obTr1 = document.createElement("tr");   
      var inputAnz = document.createElement("input");    
      inputAnz.type= "hidden";
      inputAnz.name = "produkt_anz_" + json[0]["sid"];
      inputAnz.id= "produkt_anz_" + json[0]["sid"];
      inputAnz.value= "1";
      var obTd1_1 = document.createElement("td");
      var obTd1_2 = document.createElement("td");
      var obTd1_3 = document.createElement("td");
      obTd1_1.className = "small10";
      obTd1_2.className = "small10";
      obTd1_3.className = "small10";
      obTd1_1.innerHTML =1;
      obTd1_2.innerHTML = json[0]["name"];
      obTd1_3.innerHTML = json[0]["preis"] + " EUR";
      obTd1_3.style.textAlign = "right";
      obTd1_3.width = "80";
      obTr1.appendChild(inputAnz);
      obTr1.appendChild(obTd1_1);
      obTr1.appendChild(obTd1_2);
	   obTr1.appendChild(obTd1_3);
      $("productTable").appendChild(obTr1);
      $('gesamtZeile').style.display = "";
      $('zurKasse').style.display = "";
      anz_produkte++;
      $('anz_produkte').innerHTML = anz_produkte;
      produkte[json[0]["sid"]] = json[0];
      berechneGesamtPreis();
   }
}

function rmProduct(pid){
    var opt = {
      // Use POST
      method: 'post',
      // Send this lovely data
      postBody: 'pid='+pid+'&user_fnc=rmProduct' ,
      // Handle successful response
      onSuccess: rmProductSuccess
   }
   new Ajax.Request('cartserver.php', opt);
}

function rmProductSuccess(t,json){
   var p_id = t.responseText.strip();
   Element.remove('produkt_' + p_id);
   if($('productTableBody').rows.length == 0){
      $('productTableHead').style.display = "none";
      $('gesamtTable').style.display = "none";
   }
   produkte[p_id] = null;
   berechneGesamtPreis(true);
}


function setPreis(preisInput){
   var anz_value = parseInt(preisInput.value);
   if(!isNaN(anz_value)){
      if(anz_value == 0){
         anz_value++;
         preisInput.value = anz_value;
      }
      var idx = preisInput.id
      var pr_id = idx.replace(/produkt_anz_/, "");
      var pr_preis = produkte[pr_id]["preis"].replace(/,/, ".");
      var new_preis = (anz_value * pr_preis).toFixed(2);
      $('produkt_preis_' + pr_id).innerHTML = String(new_preis).replace(/\./, ",") + " EUR";
      berechneGesamtPreis(true);
   }
   else
      preisInput.value = old_anz;
}

function berechneVersandkosten(){
    if ( (art_typ=="flach") && (anzahl <= 3) ) pr_versand = "3,00";
    else if (anzahl <= 8 ) pr_versand = "4,20";
    else if (anzahl <= 36 ) pr_versand = "6,90"; 
    else pr_versand = "8,90"; 
}

function berechneGesamtPreis(versand){
   var ges_preis = (0.0), pr_preis, anz =0, anz_flach=0, anz_normal=0, anz_ges=0;
   for(var p_id in produkte){
      if(produkte[p_id]){
         pr_preis = produkte[p_id]["preis"].replace(/,/, ".");
         anz = parseInt($('produkt_anz_' + p_id).value);
         if(produkte[p_id]["versand_typ"] == "flach")
            anz_flach += anz;
         else
            anz_normal +=anz;
         ges_preis += (anz * pr_preis);
      }
   }
   if(versand){
      $('preisZwischen').innerHTML = String(ges_preis.toFixed(2)).replace(/\./, ",") + " EUR";
      $("zwischenpreis").value = ges_preis;
      anz_ges = anz_flach + anz_normal;
      if(anz_flach < 4 && anz_ges < 4 && anz_normal == 0)
         versandpreis = 3;
      else if(anz_ges < 9)
         versandpreis = 4.2;
      else if(anz_ges < 37)
         versandpreis = 6.9;
      else
         versandpreis = 8.9;
      ges_preis += versandpreis;
      $("versandkosten").value = versandpreis;
      $("preisVersand").innerHTML = String(versandpreis.toFixed(2)).replace(/\./, ",") + " EUR";
   }
   //alert(ges_preis)
   if($('preisSpalte'))
      $('preisSpalte').innerHTML = String(ges_preis.toFixed(2)).replace(/\./, ",") + " EUR";
   if(versand)
      $("gesamtpreis").value = ges_preis;
}