/*******************************
Author   Zeljko Peric <zeljko.peric@p3os.com>
Created  2009-11-13
Modified 2010-02-22
Purpose  Standard Script for kleila.de
********************************/
/**create p3os namespace*/
var p3os = {};
/** p3os get elements by attribute name and/or value*/
p3os.getElementsByAttribute = function(name, value)
{
  if(name == null)
  {
    return null;
  }
  var ret = new Array();
  var els = document.getElementsByTagName('*');
  if(value == '*' || value == null)
  {
    for (var i = 0; i < els.length; i++)
    {
      if (els[i].getAttribute(name) != null)
      {
        ret.push(els[i]);
      }
    }
    return ret;
  }
  var rex = new RegExp('\\b'+ String(value) +'\\b');
  for (i = 0; i < els.length; i++)
  {
    var att = els[i].getAttribute(name);
    if (els[i].getAttribute(name) != null)
    {
      if(rex.test(att))
      {
        ret.push(els[i]);
      }
    }
  }
  return ret;
};
/** define getElementsByClassName method */
p3os.getElementsByClassName = function(className)
{
  var tmp = navigator.appVersion.split(';');
  var ver = 0;
  for(var i = 0; i < tmp.length; ++i)
  {
	var li = tmp[i].lastIndexOf("MSIE");
	if(li > -1)
	{
		ver = parseInt(tmp[i].slice(5,tmp[i].length));
	}
  }  
  if(navigator.appName == 'Microsoft Internet Explorer' && ver < 8)
  {
    return p3os.getElementsByAttribute('className', className);
  }
  return p3os.getElementsByAttribute('class', className);
};
/** get coordinates of a element */
p3os.getElementCoordinates = function(element) 
{
  var coords = { x:0, y:0 };
  do 
  {
    coords.x += element.offsetLeft;
    coords.y += element.offsetTop;
  } while (element = element.offsetParent);
  return coords;
};
/** p3os request object */
p3os.Request = function()
{
  var xmlhttp = null;
  function create()
  {
    if (window.XMLHttpRequest)
    {
      // Mozilla
      if(xmlhttp == null) {xmlhttp = new XMLHttpRequest();}
    }
    else if (window.ActiveXObject)
    {
      // IE
      if(xmlhttp == null) {xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");}
    }
  };
  create();
  function getResponse(type, method, uri, onFinish, params)
  {
    if(xmlhttp == null)
    {
      create();
    }
    var async = true;
    if(method == 'POST')
    {
      var typ = 'application/x-www-form-urlencoded';
      xmlhttp.onreadystatechange = function()
      {
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
          if(typeof window[onFinish] == 'function')
          {
            if(type == 'TEXT')
            {window[onFinish](xmlhttp.responseText);}
            if(type == 'XML')
            {window[onFinish](xmlhttp.responseXML);}
          }
        }
      }
      xmlhttp.open(method, uri, async);
      xmlhttp.setRequestHeader("Content-type", typ);
      xmlhttp.setRequestHeader("Content-length", params.length);
      xmlhttp.setRequestHeader("Connection", "close");
      xmlhttp.send(params);
    }
    if(method == 'GET')
    {
      xmlhttp.onreadystatechange = function()
      {
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
          if(typeof window[onFinish] == 'function')
          {
            if(type == 'TEXT')
            {window[onFinish](xmlhttp.responseText);}
            if(type == 'XML')
            {window[onFinish](xmlhttp.responseXML);}
          }
        }
      }
      xmlhttp.open(method, uri, async);
      xmlhttp.send(null);
    }
  };
  this.getText = function(method, uri, onFinish, params)
  {
    getResponse('TEXT', method, uri, onFinish, params);
  };
  this.getXml = function(method, uri, onFinish, params)
  {
    getResponse('XML', method, uri, onFinish, params);
  };
};
function imgView(e)
{
  var evt = e == null ? window.event : e;
  var t = e == null ? evt.srcElement : evt.target;
  if(t.alt == 'Kein Bild'){return;}
  var x = 100;
  var y = 100;
  var ofy = 0;
  if(e)
  {
    x = e.clientX + window.pageXOffset;
    y = e.clientY + window.pageYOffset;
    ofy = window.pageYOffset + 10;
  }
  else
  {
    if(document.body)
	{
	  x = window.event.x + document.body.scrollLeft;
      y = window.event.y + document.body.scrollTop;
      ofy = document.body.scrollTop + 10;
	}
	else
	{
	  x = window.event.x + document.documentElement.scrollLeft;
      y = window.event.y + document.documentElement.scrollTop;
      ofy = document.documentElement.scrollTop + 10;
	}
  }
  var iview = document.getElementById('imgView1');
  var img = iview.getElementsByTagName('IMG')[0];
  img.src = t.src;
  img.setAttribute('title', 'Klicken Sie auf das Bild zum schliessen');
  img.setAttribute('alt', 'Klicken Sie auf das Bild zum schliessen');
  img.style.cursor = 'pointer';
  iview.style.display = 'block';
  if(y < (img.height / 2))
  {
    iview.style.top = '10px';
  }
  else
  {
    iview.style.top = (y - (img.height / 2)) + 'px';
  }
  if(x < img.width)
  {
    iview.style.left = '100px';
  }
  else
  {
    iview.style.left = (x - img.width) + 'px';
  }
  //iview.style.left = '100px';
  return true;
};
function closeImgView()
{
  var iview = document.getElementById('imgView1');
  iview.style.display = 'none';
};
function initPreview()
{
  var pimg = p3os.getElementsByClassName('preview1');
  var i = 0;
  if(pimg != null)
  {
    for(i = 0; i < pimg.length; ++i)
    {
      pimg[i]["onclick"] = imgView;
    }
  }
};
function showProdClass(id)
{
	var pc = p3os.getElementsByAttribute('name', 'prodClass');
	for(var i = 0; i < pc.length; ++i)
	{
		pc[i].style.display ='none';
	}
	if(id != '')
	{
		var el = document.getElementById(id);
		el.style.display = 'block';
	}
	cookies["PRODCLASS"] = id;
	writeCookies();
};
/**create product table
Artikelnummer	Farbe	Name	Art	Jahrgang	Alk	WS	RS	Geschmack	Menge	Preis	Literpreis	Beschreibung	Angebot	Lieferbar	Kategorie	KatBeschreibung
*/
var products = new Array();
products[0] = new Array('0904', 'Rotwein', 'Dornfelder', 'Qualitätswein', 2009, 12.5, 4.8, 5.4, 'trocken', 0.75, 4.50, 6.00, '', 'Nein', 'ab sofort', 'rotweine', 'Rotweine');
products[1] = new Array('0812', 'Rotwein', 'Regent', 'Qualitätswein', 2008, 13.0, 4.6, 6.13, 'trocken', 0.75, 4.60, 6.13, '', 'Nein', 'ab sofort', 'rotweine', 'Rotweine');
products[2] = new Array('0814', 'Rotwein', 'Saint Laurent', 'Qualitätswein', 2008, 12.5, 4.2, 13.6, 'feinherb', 0.75, 4.60, 6.13, '', 'Nein', 'ab sofort', 'rotweine', 'Rotweine');
products[3] = new Array('0914', 'Rotwein', 'Saint Laurent', 'Qualitätswein', 2009, 12.5, 4.2, 13.6, 'feinherb', 0.75, 4.60, 6.13, '', 'Nein', 'ab sofort', 'rotweine', 'Rotweine');
products[4] = new Array('0806', 'Rotwein', 'Dornfelder', 'Qualitätswein', 2008, 12.5, 5.2, 14.0, 'feinherb', 0.75, 4.50, 6.00, '', 'Nein', 'ab sofort', 'rotweine', 'Rotweine');
products[5] = new Array('0810', 'Rotwein', 'Dornfelder', 'Qualitätswein', 2008, 11.5, 5.4, 28.2, 'lieblich', 0.75, 4.50, 6.00, '', 'Nein', 'ab sofort', 'rotweine', 'Rotweine');
products[6] = new Array('0705', 'Rotwein', 'Blauer Portugieser', 'Qualitätswein', 2007, 11.0, 6.9, 28.0, 'lieblich', 0.75, 4.50, 6.00, '', 'Nein', 'ab sofort', 'rotweine', 'Rotweine');
products[7] = new Array('0905', 'Rotwein', 'Blauer Portugieser', 'Qualitätswein', 2009, 11.0, 4.9, 28.0, 'lieblich', 0.75, 4.50, 6.00, '', 'Nein', 'ab sofort', 'rotweine', 'Rotweine');
products[8] = new Array('0940', 'Rotwein', 'Spätburgunder', 'Auslese', 2009, 14.0, 4.8, 4.2, 'trocken', 0.75, 7.00, 9.33, '', 'Nein', 'ab sofort', 'rotweine', 'Rotweine');
products[9] = new Array('0809', 'Rosé', 'Spätburgunder', 'Qualitätswein', 2008, 12.5, 6.9, 7.0, 'trocken', 0.75, 4.00, 5.33, '', 'Nein', 'ab sofort', 'weisherbstrose', 'Weißherbste und Rosé');
products[10] = new Array('0908', 'Weißherbst', 'Blauer Portugieser', 'Qualitätswein', 2009, 11.5, 5.7, 12.9, 'feinherb', 0.75, 4.00, 5.33, '', 'Nein', 'ab sofort', 'weisherbstrose', 'Weißherbste und Rosé');
products[11] = new Array('0807', 'Weißherbst', 'Blauer Portugieser', 'Qualitätswein', 2008, 10.5, 5.2, 29.5, 'lieblich', 0.75, 4.00, 5.33, '', 'Nein', 'ab sofort', 'weisherbstrose', 'Weißherbste und Rosé');
products[12] = new Array('0941', 'Weißherbst', 'Blauer Portugieser Blanc de Noir', 'Qualitätswein', 2009, 11.5, 6.1, 13.1, 'feinherb', 0.75, 4.80, 6.40, 'Blanc de Noir - weiß gekeltert', 'Nein', 'ab sofort', 'weisherbstrose', 'Weißherbste und Rosé');
products[13] = new Array('0901', 'Weißwein', 'Silvaner', 'Qualitätswein', 2009, 11.5, 6.1, 8.0, 'trocken', 1.0, 3.60, 3.60, '', 'Nein', 'ab sofort', 'weistrocken', 'Weißweine - trocken');
products[14] = new Array('0911', 'Weißwein', 'Riesling', 'Kabinett', 2009, 11.0, 6.2, 6.6, 'trocken', 0.75, 4.20, 5.60, 'Deutscher Prädikatswein', 'Nein', 'ab sofort', 'weistrocken', 'Weißweine - trocken');
products[15] = new Array('0813', 'Weißwein', 'Weißer Burgunder', 'Qualitätswein', 2008, 12.5, 7.5, 7.5, 'trocken', 0.75, 4.60, 6.13, '', 'Nein', 'ab sofort', 'weistrocken', 'Weißweine - trocken');
products[16] = new Array('0913', 'Weißwein', 'Weißer Burgunder', 'Qualitätswein', 2009, 12.5, 7.5, 7.5, 'trocken', 0.75, 4.60, 6.13, '', 'Nein', 'ab August', 'weistrocken', 'Weißweine - trocken');
products[17] = new Array('0718', 'Weißwein', 'Chardonnay', 'Qualitätswein', 2007, 13.0, 6.0, 1.2, 'trocken', 0.75, 4.60, 6.13, '', 'Nein', 'ab sofort', 'weistrocken', 'Weißweine - trocken');
products[18] = new Array('0731', 'Weißwein', 'Weißer Burgunder', 'Qualitätswein', 2007, 12.0, 6.8, 11.6, 'classic', 0.75, 4.20, 5.60, '', 'Nein', 'ab sofort', 'weisclassic', 'Weißweine - Classic');
products[19] = new Array('0732', 'Weißwein', 'Riesling', 'Qualitätswein', 2007, 11.5, 7.5, 11.7, 'classic', 0.75, 4.20, 5.60, '', 'Nein', 'ab sofort', 'weisclassic', 'Weißweine - Classic');
products[20] = new Array('0833', 'Weißwein', 'Grauer Burgunder', 'Qualitätswein', 2008, 11.5, 7.1, 12.5, 'classic', 0.75, 4.20, 5.60, '', 'Nein', 'ab sofort', 'weisclassic', 'Weißweine - Classic');
products[21] = new Array('0802', 'Weißwein', 'Kerner', 'Qualitätswein', 2008, 11.5, 7.6, 15.0, 'feinherb', 1.0, 3.60, 3.60, '', 'Nein', 'ab sofort', 'weisfeinherb', 'Weißweine - feinherb');
products[22] = new Array('0919', 'Weißwein', 'Riesling', 'Spätlese', 2009, 11.5, 7.3, 12.5, 'feinherb', 0.75, 4.80, 6.40, 'Deutscher Prädikatswein', 'Nein', 'ab sofort', 'weisfeinherb', 'Weißweine - feinherb');
products[23] = new Array('0942', 'Weißwein', 'Scheurebe', 'Auslese', 2009, 12.5, 6.7, 15.8, 'feinherb', 0.75, 6.80, 9.05, 'Deutscher Prädikatswein', 'Nein', 'ab sofort', 'weisfeinherb', 'Weißweine - feinherb');
products[24] = new Array('0603', 'Weißwein', 'Bacchus und Müller-Thurgau', 'Qualitätswein', 2006, 10.0, 5.7, 32.0, 'lieblich', 1.0, 3.60, 3.60, '', 'Nein', 'ab sofort', 'weislieblich', 'Weißweine - lieblich');
products[25] = new Array('0903', 'Weißwein', 'Bacchus und Müller-Thurgau', 'Qualitätswein', 2009, 10.0, 5.7, 32.0, 'lieblich', 1.0, 3.60, 3.60, '', 'Nein', 'ab August', 'weislieblich', 'Weißweine - lieblich');
products[26] = new Array('0717', 'Weißwein', 'Weißer Burgunder', 'Kabinett', 2007, 10.0, 5.9, 31.0, 'lieblich', 0.75, 4.00, 5.33, '', 'Nein', 'ab sofort', 'weislieblich', 'Weißweine - lieblich');
products[27] = new Array('0915', 'Weißwein', 'Bacchus', 'Kabinett', 2009, 10.5, 6.1, 29.0, 'lieblich', 0.75, 4.20, 5.60, '', 'Nein', 'ab sofort', 'weislieblich', 'Weißweine - lieblich');
products[28] = new Array('0716', 'Weißwein', 'Huxelrebe', 'Spätlese', 2007, 10.5, 6.5, 50.0, 'lieblich', 0.75, 4.60, 6.13, '', 'Nein', 'ab sofort', 'weislieblich', 'Weißweine - lieblich');
products[29] = new Array('0916', 'Weißwein', 'Huxelrebe', 'Spätlese', 2009, 10.5, 6.5, 50.0, 'lieblich', 0.75, 4.60, 6.13, 'Deutscher Prädikatswein', 'Nein', 'ab sofort', 'weislieblich', 'Weißweine - lieblich');
products[30] = new Array('0816', 'Weißwein', 'Ortega', 'Auslese', 2008, 11.0, 7.7, 57.0, 'lieblich', 0.75, 6.50, 8.65, '', 'Nein', 'ab sofort', 'weislieblich', 'Weißweine - lieblich');
products[31] = new Array('2022', 'Weißwein', 'Silvaner', 'Eiswein', 2000, 8.0, 10.3, 132.0, 'lieblich', 0.5, 11.60, 23.20, '', 'Nein', 'ab sofort', 'weislieblich', 'Weißweine - lieblich');
products[32] = new Array('0922', 'Weißwein', 'Ortega', 'Trockenbeerenauslese', 2009, 9.0, 9.0, 212.0, 'lieblich', 0.375, 14.5, 38.65, 'der absolute Spitzenwein des Jahres 2009', 'Nein', 'ab sofort', 'weislieblich', 'Weißweine - lieblich');
products[33] = new Array('0825', '', 'Traubensaft - weiß', '', 2008, 0, 0, 0, '', 0.75, 2.50, 3.33, 'Zukauf', 'Nein', 'ab sofort', 'saefte', 'Säfte');
products[34] = new Array('0824', '', 'Traubensaft - rot', '', 2008, 0, 0, 0, '', 0.75, 2.50, 3.33, 'Zukauf', 'Nein', 'ab sofort', 'saefte', 'Säfte');
products[35] = new Array('9920', 'Sekt', 'Riesling', '', 0, 0, 0, 0, 'trocken', 0.75, 8.50, 11.33, 'Zukauf, deutscher Sekt b. A. Rheinhessen', 'Nein', 'ab sofort', 'sekte', 'Sekte');
products[36] = new Array('9921', 'Sekt', 'Morio-Muskat', '', 0, 0, 0, 0, 'halbtrocken', 0.75, 8.50, 11.33, 'Zukauf, deutscher Sekt b. A. Rheinhessen', 'Nein', 'ab sofort', 'sekte', 'Sekte');
products[37] = new Array('9922', 'Sekt', 'Weißherbst Spätburgunder', '', 0, 0, 0, 0, 'halbtrocken', 0.75, 8.90, 11.87, 'Zukauf, deutscher Sekt b. A. Rheinhessen', 'Nein', 'ab sofort', 'sekte', 'Sekte');
products[38] = new Array('0002', '', 'Weintraubenlikör', '', 0, 25, 0, 0, '', 0.5, 12.90, 25.80, 'Zukauf, mit Trauben in der Flasche', 'Nein', 'ab sofort', 'braende', 'Brände und Destillate');
products[39] = new Array('0003', '', 'Rotweinlikör', '', 0, 20, 0, 0, '', 0.5, 12.90, 25.80, 'Zukauf, etwas ganz besonderes aus Rotwein', 'Nein', 'ab sofort', 'braende', 'Brände und Destillate');
products[40] = new Array('0004', '', 'Roter Weinbergspfirsich', '', 0, 20, 0, 0, '', 0.5, 12.90, 25.80, 'Zukauf, ein Hochgenuss', 'Nein', 'ab sofort', 'braende', 'Brände und Destillate');
products[41] = new Array('0005', '', 'Alter Trester', '', 0, 40, 0, 0, '', 0.5, 12.00, 24.00, 'Zukauf, im Holzfass gelagert', 'Nein', 'ab sofort', 'braende', 'Brände und Destillate');

function inArray(arr, val)
{
	for(var i = 0; i < arr.length; ++i)
	{
		if(arr[i] == val)
		{return true}
	}
	return false;
};
function getColumnArray(marr, colIndex)
{
	var col = new Array();
	for (var i = 0; i < marr.length; ++i)
	{
		col.push(marr[i][colIndex]);
	}
	return col;
};
function arrUnique(arr)
{
	var uq = new Array();
	for (var i = 0; i < arr.length; ++i)
	{
		if(!inArray(uq, arr[i]))
		{
			uq.push(arr[i]);
		}
	}
	return uq;
};
function toMoney(str, dec)
{
	return str.toFixed(dec).replace(/\./g, ',');
};
function writeProducts()
{
	var cat = arrUnique(getColumnArray(products, 15));
	var catn = arrUnique(getColumnArray(products, 16));
	for(var i = 0; i < cat.length; ++i)
	{
		document.writeln('<div id="'+cat[i]+'" name="prodClass">');
		document.writeln('<h4>'+catn[i]+'</h4>');
		document.writeln('<table class="products">');
		document.writeln('<tbody>');
		for(var j = 0; j < products.length; ++j)
		{
			if(products[j][15] == cat[i])
			{
				document.writeln('<tr>');
				document.writeln('<td class="prodImg"></td>');
				document.writeln('<td class="prodData">');
				document.writeln('<p class="prodName">');
				if(products[j][4] == 0)
				{document.write(products[j][2]);}
				else
				{document.write(products[j][4] + ' ' + products[j][2]);}
				document.writeln('</p>');
				document.writeln('<p class="prodDescr">');
				if(products[j][1] != '') {document.write(products[j][1]);}
				if(products[j][8] != '') {document.write(' ' + products[j][8]);}
				if(products[j][1] != '' || products[j][8] != '') {document.writeln('<br/>');}
				if(products[j][3] != '') {document.writeln(products[j][3]+'<br/>');}
				if(products[j][12] != '') {document.writeln(products[j][12]+'<br/>');}
				if(products[j][4] != 0){document.writeln('Jahrgang '+products[j][4]+'<br/>');}
				if(products[j][9] != 0){document.writeln('Flasche '+toMoney(products[j][9],2)+' l<br/>');}
				if(products[j][5] != 0){document.write('Alk. '+products[j][5]+'% vol; ');}
				if(products[j][6] != 0){document.write('WS '+toMoney(products[j][6],1)+' g/l; ');}
				if(products[j][7] != 0){document.write('RS '+toMoney(products[j][7],1)+' g/l ');}
				document.writeln('</p>');
				document.writeln('</td>');
				document.writeln('<td class="prodOrder">');
				document.writeln('<p>');
				document.writeln('<span class="preis">'+toMoney(products[j][10],2)+' €</span><br/>');
				document.writeln('<span class="literpreis">Literpreis '+toMoney(products[j][11],2)+' €</span>');
				document.writeln('</p>');
				document.writeln('<p>');
				document.writeln('<input type="text" id="'+products[j][0]+
				'" maxlength="2" size="2" name="orderQty" value="1" title="Bestellmenge bitte eingeben"/>');
				document.writeln('<a href="#prodBasket" onclick="javascript:prodOrder(\''+
				products[j][0]+'\');" title="In den Warenkorb" class="orderLink">Warenkorb</a>');
				document.writeln('</p>');
				document.writeln('<p class="orderInfo">');
				document.writeln('Lieferbar ' + products[j][14] + '<br/>');
				document.writeln('<a href="agb.html" title="Unsere Versand- und Zahlungsbedinqunqen">Versandbedingungen</a><br/>');
				document.writeln('Bestell. Nr. '+products[j][0]);
				document.writeln('</p>');
				document.writeln('</td>');
				document.writeln('</tr>');
				document.writeln('<tr>');
				document.writeln('<td>&nbsp;</td>');
				document.writeln('<td></td>');
				document.writeln('<td></td>');
				document.writeln('</tr>');
			}
		}
		document.writeln('</tbody>');
        document.writeln('</table>');
		document.writeln('</div>');
	}
	if(cookies["PRODCLASS"])
	{
		showProdClass(cookies["PRODCLASS"]);
	}
};
function writeProdMenu()
{
	document.writeln('<ul>');
	var cat = arrUnique(getColumnArray(products, 15));
	var catn = arrUnique(getColumnArray(products, 16));
	for(var i = 0; i < cat.length; ++i)
	{
		document.writeln('<li>');
		document.writeln('<a href="#'+cat[i]+'" class="navLink" title="'+catn[i]+'" onclick=');
		document.writeln('"javascript:showProdClass(\''+cat[i]+'\');">'+catn[i]+'</a>');
		document.writeln('</li>');
	}
	document.writeln('</ul>');
};
var cookies = new Array();
var basket = new Array();
function getCookie(name)
{
	var tmp = null;
	var tmp1 = null;
	var tmp2 = '';
	var i = 0;
	tmp = document.cookie.split(';');
	for(i = 0; i < tmp.length; ++i)
	{
		tmp1 = tmp[i].split('=');
		tmp2 = tmp1[0].replace(/ /g, '');
		if(name == tmp2)
		{
			return decodeURI(tmp1[1]);
		}
	}
	return '';
};
function readBasket()
{
	if(cookies["PRODLIST"]) 
	{
		tmp = cookies["PRODLIST"].split('#');
		for(i = 0; i < tmp.length; ++i)
		{
			tmp1 = tmp[i].split(':');
			if(tmp1.length > 0)
			{
				basket[tmp1[0]] = parseInt(tmp1[1]);
			}
		}
	}
};
function readCookies()
{
	var expired = new Date();
	var fivedays = expired.getTime() + (5 * 24 * 60 * 60 * 1000);
	expired.setTime(fivedays);
	var tmp = null;
	var tmp1 = null;
	var tmp2 = '';
	var i = 0;
	if(!document.cookie)
	{
		document.cookie = "FIRSTNAME=; expires=" + expired.toGMTString();
		document.cookie = "LASTNAME=; expires=" + expired.toGMTString();
		document.cookie = "STREET=; expires=" + expired.toGMTString();
		document.cookie = "ZIP=; expires=" + expired.toGMTString();
		document.cookie = "CITY=; expires=" + expired.toGMTString();
		document.cookie = "PHONE=; expires=" + expired.toGMTString();
		document.cookie = "EMAIL=; expires=" + expired.toGMTString();
		document.cookie = "PRODLIST=; expires=" + expired.toGMTString();
		document.cookie = "PRODCLASS=; expires=" + expired.toGMTString();
	}
	tmp = document.cookie.split(';');
	for(i = 0; i < tmp.length; ++i)
	{
		tmp1 = tmp[i].split('=');
		if(tmp1.length > 0)
		{
			tmp2 = tmp1[0].replace(/ /g,'');
			if(tmp1[1])
			{cookies[tmp2] = decodeURI(tmp1[1]);}
			else
			{cookies[tmp2] = '';}
		}
	}
	readBasket();
};
function writeBasket()
{
	var tmp = '';
	var tmp1 = '';
	for (p in basket)
	{
		tmp1 = p + ':' + basket[p] + '#';
		tmp = tmp + tmp1;
	}
	cookies["PRODLIST"] = tmp.slice(0, tmp.length-1);
};
function writeCookies()
{
	writeBasket();
	var expired = new Date();
	var fivedays = expired.getTime() + (5 * 24 * 60 * 60 * 1000);
	expired.setTime(fivedays);
	for (c in cookies)
	{
		document.cookie = c + '=' + encodeURI(cookies[c]) + ";expires=" + expired.toGMTString();
	}
};
function getProdPrice(prodId)
{
	for(var j = 0; j < products.length; ++j)
	{
		if(products[j][0] == prodId)
		{
			return products[j][10];
		}
	}
	return null;
};
function getProdName(prodId)
{
	for(var j = 0; j < products.length; ++j)
	{
		if(products[j][0] == prodId)
		{
			if(products[j][4] == 0)
			{return products[j][2];}
			else
			{return(products[j][4] + ' ' + products[j][2]);}
		}
	}
	return null;
};
function getOrderCount()
{
	var cnt = 0;
	for (p in basket)
	{
		++cnt;
	}
	return cnt;
};
function getBasketTable()
{
	var tmp = '<table class="basket"><tbody>';
	var tmp1 = '';
	var prc = 0.0;
	var sum = 0.0;
	for (p in basket)
	{
		prc = getProdPrice(p) * basket[p];
		sum += prc;
		tmp1 = '<tr><td class="zahl">' + basket[p] + '<td>' + getProdName(p) + '</td>' + 
		'</td><td class="zahl">' + toMoney(prc,2) + 
		' €</td><td><img src="res/img/document_delete.png" widht="16" height="16" '+
		'class="unorder" alt="vom Warenkorb entfernen" title="vom Warenkorb entfernen" '+
		'onclick="javascript:removeOrder(\''+p+'\');"/></td></tr>';
		tmp = tmp + tmp1;
	}
	tmp = tmp + '<tr><td></td><td class="zahl"><p class="sum">Gesamt:</p></td><td class="zahl"><p class="sum">'+ 
	toMoney(sum,2) + ' €</p></td><td></td></tr></tbody></table>';
	return tmp;
};
function showBasket()
{
	var bs = document.getElementById('prodBasket');
	var t = document.getElementById('basketTitle');
	var l = document.getElementById('basketLink');
	if(!bs) {return;}
	if(getOrderCount() == 0)
	{
		bs.innerHTML = '';
		if(t)
		{t.innerHTML = '';}
		if(l)
		{l.innerHTML = '';}
		return;
	}
	if(t)
	{
		t.innerHTML = 'Ihr Warenkorb';
	}
	if(l)
	{
		l.innerHTML = '<a href="bestellung.html" title="Zur Kasse gehen" class="orderLink">Zur Kasse</a>';
	}
	bs.innerHTML = getBasketTable();
};
function prodOrder(prodId)
{
	var p = document.getElementById(prodId);
	if(!p) {return;}
	var q = parseInt(p.value);
	if(q == 0) {return;}
	if(basket[prodId])
	{
		basket[prodId] = basket[prodId] + q;
	}
	else
	{
		basket[prodId] = q;
	}
	writeCookies();
	showBasket();
};
function removeOrder(prodId)
{
	if(basket[prodId])
	{
		delete(basket[prodId]);
		writeCookies();
		showBasket();
	}
};
function clearBasket()
{
	for (p in basket)
	{
		removeOrder(p);
	}
};
function checkOrder()
{
	var im = document.getElementById('invalidMessage');
	var ok = true;
	if(getOrderCount() == 0)
	{
		if(im) {im.innerHTML = 'Ihr Warenkorb ist leer.'};
		return false;
	}
	var el = null;
	var fn = document.forms[0].firstname.value.replace(/ /g, '');
	var ln = document.forms[0].lastname.value.replace(/ /g, '');
	var st = document.forms[0].street.value.replace(/ /g, '');
	var ct = document.forms[0].city.value.replace(/ /g, '');
	var zp = document.forms[0].zip.value.replace(/ /g, '');
	var ph = document.forms[0].phone.value.replace(/ /g, '');
	var em = document.forms[0].email.value.replace(/ /g, '');
	var kapa = document.forms[0].kapa.value.replace(/ /g, '');
	var pc = p3os.getElementsByClassName('invalidEntry');
	for(var i = 0; i < pc.length; ++i)
	{
		pc[i].innerHTML = '';
	}
	if(!kapa)
	{
		ok = false;
		el = document.getElementById('invalidSecure');
		el.innerHTML = '*';
	}
	if(!fn)
	{
		ok = false;
		el = document.getElementById('invalidFname');
		el.innerHTML = '*';
	}
	if(!ln)
	{
		el = document.getElementById('invalidLname');
		el.innerHTML = '*';
		ok = false;
	}
	if(!st)
	{
		el = document.getElementById('invalidStreet');
		el.innerHTML = '*';
		ok = false;
	}
	if(!ct || !zp)
	{
		el = document.getElementById('invalidZip');
		el.innerHTML = '*';
		ok = false;
	}
	if(!ph)
	{
		el = document.getElementById('invalidPhone');
		el.innerHTML = '*';
		ok = false;
	}
	if(!em)
	{
		el = document.getElementById('invalidEmail');
		el.innerHTML = '*';
		ok = false;
	}
	if(document.forms[0].agb.checked == false)
	{
		el = document.getElementById('invalidAgb');
		el.innerHTML = '*';
		ok = false;
	}
	if(!ok)
	{
		im.innerHTML = 'Ihre Angaben sind nicht vollständig.';
		return ok;
	}
	cookies["FIRSTNAME"] = document.forms[0].firstname.value;
	cookies["LASTNAME"] = document.forms[0].lastname.value;
	cookies["STREET"] = document.forms[0].street.value;
	cookies["ZIP"] = document.forms[0].zip.value;
	cookies["CITY"] = document.forms[0].city.value;
	cookies["PHONE"] = document.forms[0].phone.value;
	cookies["EMAIL"] = document.forms[0].email.value;
	//clearBasket();
	writeCookies();
	document.forms[0].orderChecked.value = 'Ja';
	var prc = 0.0;
	var sum = 0.0;
	var t1 = null;
	var plist = document.getElementById ('prodlist');
	if(plist) {document.forms[0].removeChild(plist);}
	plist = null;
	var nam = '';
	var c = 0;
	plist = document.createElement('p');
	plist.setAttribute('id', 'prodlist');
	for (p in basket)
	{
		prc = getProdPrice(p) * basket[p];
		sum += prc;
		nam = getProdName(p);
		t1 = document.createElement('input');
		t1.setAttribute('type', 'hidden');
		t1.setAttribute('name', 'prodlist['+c+'][0]');
		t1.value = basket[p];
		plist.appendChild(t1);
		t1 = document.createElement('input');
		t1.setAttribute('type', 'hidden');
		t1.setAttribute('name', 'prodlist['+c+'][1]');
		t1.value = p;
		plist.appendChild(t1);
		t1 = document.createElement('input');
		t1.setAttribute('type', 'hidden');
		t1.setAttribute('name', 'prodlist['+c+'][2]');
		t1.value = nam;
		plist.appendChild(t1);
		t1 = document.createElement('input');
		t1.setAttribute('type', 'hidden');
		t1.setAttribute('name', 'prodlist['+c+'][3]');
		t1.value = prc;
		plist.appendChild(t1);
		++c;
	}
	document.forms[0].appendChild(plist);
	document.forms[0].submit();
	return ok;
};
function initOrderForm()
{
	if (cookies["FIRSTNAME"]) {document.forms[0].firstname.value = cookies["FIRSTNAME"];}
	if (cookies["LASTNAME"]) {document.forms[0].lastname.value = cookies["LASTNAME"];}
	if (cookies["STREET"]) {document.forms[0].street.value = cookies["STREET"];}
	if (cookies["ZIP"]) {document.forms[0].zip.value = cookies["ZIP"];}
	if (cookies["CITY"]) {document.forms[0].city.value = cookies["CITY"];}
	if (cookies["PHONE"]) {document.forms[0].phone.value = cookies["PHONE"];}
	if (cookies["EMAIL"]) {document.forms[0].email.value = cookies["EMAIL"];}
	showBasket();
};
readCookies();