function doload()
{
   var index;
   var search = window.location.search;
   var adword;
   var customer;
   var email;
   var findbar = document.getElementById("findbar");

   if (search > "")
   {
      adword = getparam(search, "adword");
      if (adword > "") document.cookie = "adword=" + decodeURIComponent(adword) + "; path=/; expires=Thu, 31-Dec-2099 23:59:59 GMT;";

      customer = getparam(search, "customer");
      if (customer > "")
      {
         email = getparam(search, "email");
         if (email > "") document.cookie = "adresse=" + customer + "$$" + email + "$" + "; path=/; expires=Thu, 31-Dec-2099 23:59:59 GMT;";
      }
   }

   if (findbar != undefined) findbar.style.display = "block";

   showcard();
}

function dobuy()
{
   var rows = document.getElementById("variants").rows;
   var row;
   var index;
   var key;
   var caption;
   var specs;
   var quantity;
   var price;
   var count = 0;
   var item;
   var items = "";
   var soldout = true;

   if (window.navigator.cookieEnabled)
   {

      items = getitems();

      for (var i = 1; i < rows.length; i++)
      {
         row = rows[i];

         key = row.cells.item(0).getElementsByTagName("a");
         if (key.length == 0) key = row.cells.item(0).getElementsByTagName("p");
         key = key.item(0).innerHTML;

         caption = row.cells.item(1).getElementsByTagName("a");
         if (caption.length == 0) caption = row.cells.item(1).getElementsByTagName("p");
         caption = uncode(caption.item(0).innerHTML);

         index = caption.toLowerCase().indexOf("<br>");
         if (index > 0) caption = caption.substring(0, index);

         index = caption.toLowerCase().indexOf("</em> ");
         if (index > 0) caption = caption.substring(index + 6);

         specs = row.cells.item(1).getElementsByTagName("input").item(0);

         with (row.cells.item(2).getElementsByTagName("input").item(0))
         {
            quantity = value;
            if (disabled == false) soldout = false;
         }
         price = row.cells.item(3).getElementsByTagName("p").item(0).innerHTML;

         if (quantity.length > 0)
         {
            quantity = val(quantity);
            quantity = (isNaN(quantity)) ? 0 : quantity;
            quantity = (quantity >= 1) ? quantity.toString() : "";
         }

         if (price.length > 0)
         {
            price = val(price);
            price = (isNaN(price)) ? "" : format(price);
         }

         if (quantity.length > 0)
         {
            if (specs != undefined)
            {
               if (specs.value.substring(0, 1) == "<")
               {
                  specs.style.backgroundColor = "#FF9999";
                  alert("Bitte geben Sie die fehlenden Daten bei " + specs.value + " ein.");
                  return;
               }
               specs.style.backgroundColor = "";
            }
            count = count + parseFloat(quantity);
            items = (items.length == 0) ? "" : (items + "$");
            items = items + escape(caption) + "$" + escape(key) + "$" + escape(price) + "$" + escape(quantity);
            item = key + " (" + caption + ")";
            if (specs != undefined)
            {
              items = items + "$" + escape(specs.value) + "$$$";
            }
         }
      }
      if (count == 0)
      {
         if (soldout == true)
         {
            alert("Alle Artikel dieser Seite sind bereits ausverkauft.");
         }
         else
         {
            alert("Bitte geben Sie eine Menge ein.");
         }
      }
      else
      {
         for (var i = 1; i < rows.length; i++)
         {
            rows[i].cells.item(2).getElementsByTagName("input").item(0).value = "";
         }
         alert((count == 1) ? "Sie haben den Artikel " + item + " in den Warenkorb gelegt." : "Sie haben " + count.toString() + " Artikel in den Warenkorb gelegt.");
         document.cookie = "Artikel=" + items + "; path=/;"
         showcard();
      }
   }
}

function dobuynow(key, caption, price, specs)
{
   var items= "";

   if (window.navigator.cookieEnabled)
   {
      items = getitems();
      items = (items.length == 0) ? "" : (items + "$");
      items = items + escape(caption) + "$" + escape(key) + "$" + escape(price) + "$1";
      if (specs > '')
      {
         items = items + "$" + escape(specs) + "$$$";
         alert("Sie haben den Artikel " + key + " (" + caption + ") in den Warenkorb gelegt. Für den Artikel benötigen wir zusätzliche Angaben. Bitte ergänzen Sie diese später auf der Bestellkarte.");
      }
      else
      {
         alert("Sie haben den Artikel " + key + " (" + caption + ") in den Warenkorb gelegt.");
      }
      document.cookie = "Artikel=" + items + "; path=/;"
      showcard();
   }
}

function doclear()
{
   if (window.navigator.cookieEnabled)
   {
      document.cookie = "Artikel=; path=/";
   }
}

function dofind(event, force)
{
   var search = "?" + escape(document.getElementById("findstring").value);

   if (force == false)
   {
      if (event.keyCode != 13) return;
   }
   if (document.getElementById("findstring").value != "")
   {
      window.location = ((window.location.protocol == "file:") ? "suchen.html" : "http://www.toepferei-langerwehe.de/suchen.html") + search;
   }
}

function showcard()
{
   var items;
   var i;
   var price;
   var quantity;
   var count = 0;
   var total = 0;

   if (window.navigator.cookieEnabled)
   {
      items = getitems();
      if (items.length > 0)
      {
         items = items.split("$");
         for (i = 1; i <= items.length; i++)
         {
            items[i - 1] = unescape(items[i - 1]);
         }
         for (i = 0; i < items.length; i = i + 4)
         {
            if (items[i + 3].length > 0)
            {
               price = parseFloat(items[i + 2].replace(",", "."));
               quantity = parseFloat(items[i + 3].replace(",", "."));
               if (isNaN(quantity) == false)
               {
                  count = count + quantity;
                  if (isNaN(price) == false)
                  {
                     total = total + Math.round(price * quantity * 100) / 100;
                  }
               }
            }
         }
      }
   }
   document.getElementById("basketcount").innerHTML = count.toString() + "&nbsp;Position" + ((count == 1) ? "" : "en");
   document.getElementById("basketvalue").innerHTML = format(total) + " EUR *";

}

function getitems()
{
   return getvalue("Artikel");
}

function getparam(search, param)
{
   var index = search.indexOf(param + "=");
   var value = "";

   if (index > 0)
   {
      value = search.substring(index + param.length + 1);
      index = value.indexOf("&");
      if (index > 0) value = value.substring(0, index);
   }
   return value;
}

function getvalue(cookie)
{
   var value = document.cookie;
   var i;

   i = value.indexOf(cookie + "=");

   if (i >= 0)
   {
      value = value.substring(i + cookie.length + 1);
      i = value.indexOf(";");
      if (i >= 0)
      {
         value = value.substr(0, i);
      }
      return value;
   }
   else
   {
      return "";
   }

}

function format(number)
{
   var s;

   s = Math.floor(Math.round(number * 100)).toString();

   while (s.length < 3)
   {
      s = "0" + s;
   }

   return s.substring(0, s.length - 2) + "," + s.substring(s.length - 2, s.length);
}

function trim(text)
{
   return text.replace(/^\s+/, "").replace(/\s+$/, "");
}

function uncode(text)
{
  return unescape(text.replace(/&amp;/g, "&").replace(/&lt;/g, "<").replace(/&gt;/g, ">"));
}

function val(text)
{
  var ch;
  var value = "";
  for (var i = 1; i <= text.length; i++)
  {
     ch = text.substr(i - 1, 1);
     if ((ch == ",") || (ch == "."))
     {
        ch = ".";
     }
     else
     {
        ch = ((ch >= "0") && (ch <= "9")) ? ch : "";
     }
     value = value + ch;
  }
  return parseFloat(value);
}

function createrequest()
{
   var xmlhttp;
   if (window.XMLHttpRequest)
   {
      try
      {
         xmlhttp = new XMLHttpRequest();
      }
      catch(e)
      {
         xmlhttp = false;
      }
   }
   else
   {  
     try
     {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     }
     catch(e)
     {
        try
        {
           xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e)
        {
           xmlhttp = false;
        }
     }
   }
   return xmlhttp;
}
