/* BROTKASTL JS*/

function formatAsMoney(mnt) {
    mnt -= 0;
    mnt = (Math.round(mnt*100))/100;
    return (mnt == Math.floor(mnt)) ? mnt + ',00' 
              : ( (mnt*10 == Math.floor(mnt*10)) ? 
                       mnt + '0' : mnt);
}

$(document).ready(function() {
  var toggleMinus = 'images/sizeMinus.png';
  var togglePlus = 'images/sizePlus.png';
  
  var toggleStyleSwitcher = function() {
	 $('body').toggleClass('large'); 
  };
  $('#size').css('display','inline');
  $('#size img').click(function() {
	 var toggleSource = $(this).attr('src');
	 //alert("toggleSource is:" + toggleSource);
	 if (toggleSource == togglePlus) {
		 //alert("toggleSource is:" + toggleSource);
		 $(this).attr('src', toggleMinus);
	 } else {
		 $(this).attr('src', togglePlus);
	 }; 
  });
  $('#size').click(toggleStyleSwitcher);
});




/* assignment: #dropdown buttons show .droplist on hovering */
$(document).ready(function() {
						   
  $('#dropdown1 h5').hover(function() {
	$('.droplist').css('display','none');	
    $('#dropdown1 .droplist').slideDown('fast');
   }, function() {
	$('.droplist').css('display','none');	
  });

$('#dropdown2 h5').hover(function() {
    $('.droplist').css('display','none');	
	$('#dropdown2 .droplist').slideDown('fast');
  }, function() {
	$('.droplist').css('display','none');	
  });

$('#dropdown3 h5').hover(function() {
    $('.droplist').css('display','none');	
	$('#dropdown3 .droplist').slideDown('fast');
  }, function() {
	$('.droplist').css('display','none');	
  });

$('#dropdown4 h5').hover(function() {
    $('.droplist').css('display','none');	
	$('#dropdown4 .droplist').slideDown('fast');
  }, function() {
	$('.droplist').css('display','none');	
  });					   
						   
  $('#dropdown5 h5').hover(function() {
    $('.droplist').css('display','none');	
	$('#dropdown5 .droplist').slideDown('fast');
   }, function() {
	$('.droplist').css('display','none');	
  });						   
						   
   $('#dropdown6 h5').hover(function() {
    $('.droplist').css('display','none');	
	$('#dropdown6 .droplist').slideDown('fast');
  }, function() {
	$('.droplist').css('display','none');	
  });
   
   $('#dropdown1 .droplist, #dropdown2 .droplist, #dropdown3 .droplist, #dropdown4 .droplist, #dropdown5 .droplist, #dropdown6 .droplist').hover(function() {
    $('.droplist').css('display','none');
	$(this).show();
   }, function() {
	$('.droplist').css('display','none');	
  });
   
   $('#mainWrap, #menuActive, #top').mouseover(function() {
    $('.droplist').css('display','none');
  });
 
   /* assignment: image rollover: showing text paragraph */ 
  /*  $('.logo img').hover(function() {
	$('.logo p').css('display','inline');	
   }, function() {
	$('.logo p').css('display','none');	
  }); */
   
 
	/* assign id for each li item and mark active produktkategorie*/
	var $activeItem = $('#katId').text();
	if ($activeItem == '') {
		$('#submenu ul li:eq(0)').addClass('selected');
		//alert("no Id passed: " + $activeItem);
	}
	//alert("$activeItem: " + $activeItem);
	$('#submenu ul li').each(function(index) {
		var $itemNr = index + 1;
		//alert ("$itemNr: " + $itemNr);
		$(this).attr('id',$itemNr);

		if ($activeItem == $itemNr) {
			//alert ("doing activeItem == this.id: " + this.id);
			//$(this).css('background-color', '#eee', 'color', '#2b2b2b'); // this works!
			//$(this).css('color', '#2b2b2b','background-image', 'url("images/nav/arrowSm.png"'); // this is not working
			$(this).addClass('selected');	 // this works now, watch 
		} 
		//$('#submenu ul li:eg($itemNr)').addClass('selected');
		//alert("$activeItem: " + $activeItem);
	}); 
	/* check how many items in list and make two colums with same length */
   	var $listItems = $('div.textColumn1 ul li').length;
	//alert ("total $listItems: " + $listItems);
	var $firstItemSecondCol = $listItems/2;
	//alert ("$firstItemSecondCol: " + $firstItemSecondCol);
   	$('div.textColumn1 ul:eq(0)').after('</ul><ul class="secondColumn"><li>&nbsp;</li>'); 
	$('div.textColumn1 ul li:gt(' + $firstItemSecondCol + ')').insertBefore('div.textColumn1 ul li:last');
	  /* OLD assignment: #textColumn2 ul positioning: 17 items in 1 column */
	/*$('div.textColumn1 ul li:gt(18)').insertBefore('div.textColumn1 ul li:last'); */ 
	
//	$firstItemSecondCol.insertBefore('div.textColumn1 ul li:last'); 
	$('div.textColumn1 ul li:last').hide();// works 
	//$('div.textColumn1 ul:eq(1)').hide(); // needs this to work correctly in IEs, but problem with rest
	//$('div.textColumn1 ul:eq(2)').hide(); 
	//$('div.textColumn1 ul:eq(1)').css('height','1px');
	
	
	/* assignment: .subnav img buttons functionality like in unternehmen, verarbeitung, filialen*/
   $('.subnav').css('display','inline');
   $('.subnav img:eq(0)').addClass('active');
   $('.subnav img:eq(0)').attr('id', 'current');				   
   $('.subnav img').hover(function() {
     if (this.id != 'current') {
	 	var $image = $(this);	
		$(this).fadeTo("slow", 0.5);
		var imagePath = $image.attr('src');
		var imageTitle = $image.attr('alt');
		//alert("imagePath: " + imagePath);
		//alert("imageTitle: " + imageTitle);
		var imageLargePath = imagePath.slice(0,-6);
		var imageEnding = imagePath.slice(-4);
		var imageLarge = imageLargePath + imageEnding;
		var $imgLg = $('#image0');
		var imgLgCurrentPath = $imgLg.attr('src');
		//alert("imageLarge: " + imageLarge + "imgLgCurrentPath: " + imgLgCurrentPath);
		$(this).click(function() {
		  if (imageLarge !=  imgLgCurrentPath) {
			  $('.subnav img').removeClass('active');
			  $('.subnav img').attr('id', '');
			  $(this).addClass('active').attr('id', 'current');
			  $('#image0,').fadeTo('slow', 0.2, function() {
	    		 $('#image0').attr('src',imageLarge).fadeTo('slow', 0.99);
				 $('.mainImgTitle').text(imageTitle).fadeTo('slow', 0.99);
	   	 	  });
	  	   }
		});
	
	 }
  }, function() {
	 // for Safari we need to set opacitiy to 0.99, else imgage disapears
	$(this).fadeTo("fast", 0.99);
  });	
   
  /* assignment: .subnav list buttons functionality in filialen*/
  // problem: onla changes first pic,
/*	$('.textColumn li').hover(function() {
		//alert('hover:' + this);
		if ($('.textColumn li:eq(0)')) {
		 $('.subnav img:eq(1)').fadeTo("slow", 0.5);
		} else if ($('.textColumn li:eq(1)')) {
		 $('.subnav img:eq(2)').fadeTo("slow", 0.5);
		} else if ($('.textColumn li:eq(2)')) {
		 $('.subnav img:eq(3)').fadeTo("slow", 0.5);
		} else if ($('.textColumn li:eq(3)')) {
		 $('.subnav img:eq(3)').fadeTo("slow", 0.5);
		} else if ($('.textColumn li:eq(4)')) {
		 $('.subnav img:eq(5)').fadeTo("slow", 0.5);
		} else if ($('.textColumn li:eq(5)')) {
		 $('.subnav img:eq(0)').fadeTo("slow", 0.5);
		};
  	}, function() {
	 // for Safari we need to set opacitiy to 0.99, else imgage disapears
	 	$('.subnav img').fadeTo("fast", 0.99);
	}); */
  
  // long version; but it works
    $('.textColumn li:eq(0)').hover(function() {
		$('.subnav img:eq(4)').fadeTo("slow", 0.5);
	}, function() {
	 $('.subnav img:eq(4)').fadeTo("fast", 0.99);
	});
	
    $('.textColumn li:eq(1)').hover(function() {
		$('.subnav img:eq(3)').fadeTo("slow", 0.5);
	}, function() {
	 $('.subnav img:eq(3)').fadeTo("fast", 0.99);
	});
	
    $('.textColumn li:eq(2)').hover(function() {
		$('.subnav img:eq(2)').fadeTo("slow", 0.5);
	}, function() {
	 $('.subnav img:eq(2)').fadeTo("fast", 0.99);
	});

    $('.textColumn li:eq(3)').hover(function() {
		$('.subnav img:eq(1)').fadeTo("slow", 0.5);
	}, function() {
	 $('.subnav img:eq(1)').fadeTo("fast", 0.99);
	});

    $('.textColumn li:eq(4)').hover(function() {
		$('.subnav img:eq(0)').fadeTo("slow", 0.5);
	}, function() {
	 $('.subnav img:eq(0)').fadeTo("fast", 0.99);
	});
	
	// deactivatet right now, if new store comes activate again!
   /* $('.textColumn li:eq(5)').hover(function() {
		$('.subnav img:eq(0)').fadeTo("slow", 0.5);
	}, function() {
	 $('.subnav img:eq(0)').fadeTo("fast", 0.99);
	}); */
	
	
	
   /* assignment: list item rollovers showing pictures of products in produkte page */
	$('.textColumn1 ul li').hover(function() {	
		var $listItem = $(this).find('.imgIcon').text();
		var itemPath = 'images/produkte/' + $listItem + '.png';	
		//alert("$listItem: " + $listItem);
		//alert("itemPath: " + itemPath);

		$('#image0').attr('src',itemPath).css('display', 'inline'); 
	}, function() {
		$('#image0').attr('src','images/produkte/pic0.png').css('display', 'inline');
  });
	
	 /* Wochentageangebote: assignment: list item rollovers showing pictures of products in produkte page */
	$('.textColumn1A ul.product li').hover(function() {	
		var $listItem = $(this).find('.imgIcon').text();
		var itemPath = 'images/produkte/' + $listItem + '.png';	
		//alert("$listItem: " + $listItem);
		//alert("itemPath: " + itemPath);

		$('#image0').attr('src',itemPath).css('display', 'inline'); 
	}, function() {
		$('#image0').attr('src','images/produkte/pic0.png').css('display', 'inline');
  });
	
	
	/* assignment: .kategorie buttons functionality */
	$('.auswahl').css('display', 'none'); 
	$('.kategorie h4').css('cursor', 'pointer'); 
	
	/* assign id for each h4 item and       mark active produktkategorie*/

	$('.kategorie').each(function(index) {
		var $katNr = index + 1;
		var $listNr = 'list' + $katNr;
		//alert ("$katNr: " + $katNr + " $listNr: " + $listNr);
		$(this).attr('id', $katNr);
		//$(this).parent('.kategorie').attr('id', $katNr);
		$(this).find('.auswahl').attr('id', $listNr);
		/* $(this).click(function() {
			//var $currentKat = $(this).id				   
			$('.auswahl').slideUp('slow'); 
			//$(this).parent('.auswahl').slideDown('slow');
			$(this).find('.auswahl').slideDown('slow');
			//alert("$this id is: " + this.id);
			alert("$('.auswahl')id is: " + $('.auswahl').id + " $('.kategorie')id is: " + $('.kategorie').id);
 			
 		});*/
		/* $(this).toggle(function() {
			//var $currentKat = $(this).id				   
			$('.auswahl').slideUp('slow'); 
			$(this).find('.auswahl').slideDown('slow');
			//alert("$this id is: " + this.id);
		},function() {
			$(this).find('.auswahl').slideUp('slow'); 
			//$(this).find('.auswahl').show('slow');
			//	$(this).find('.auswahl').css('display', 'inline');
 			
 		}); */
		
	});  
	
	$('#1 h4').toggle(function() {
			//var $currentKat = $(this).id				   
			$('.auswahl').slideUp('slow'); 
			$('#list1').slideDown('slow');
			//alert("$this id is: " + this.id);
	},function() {
			$('#list1').slideUp('slow'); 
			//$(this).find('.auswahl').show('slow');
			//	$(this).find('.auswahl').css('display', 'inline');
 			
 	});

	$('#2 h4').toggle(function() {		   
			$('.auswahl').slideUp('slow'); 
			$('#list2').slideDown('slow');
	},function() {
			$('#list2').slideUp('slow'); 
 			
 	});
	
	$('#3 h4').toggle(function() {		   
			$('.auswahl').slideUp('slow'); 
			$('#list3').slideDown('slow');
	},function() {
			$('#list3').slideUp('slow'); 
 			
 	});

	$('#4 h4').toggle(function() {		   
			$('.auswahl').slideUp('slow'); 
			$('#list4').slideDown('slow');
	},function() {
			$('#list4').slideUp('slow'); 
 			
 	});
	
	$('#5 h4').toggle(function() {		   
			$('.auswahl').slideUp('slow'); 
			$('#list5').slideDown('slow');
	},function() {
			$('#list5').slideUp('slow'); 
 			
 	});
	
	$('#6 h4').toggle(function() {		   
			$('.auswahl').slideUp('slow'); 
			$('#list6').slideDown('slow');
	},function() {
			$('#list6').slideUp('slow'); 
 			
 	});

	$('.kategorie h4').hover(function() {	
		$(this).addClass('hover'); 
		var $currentKat = $(this).parent();
		
		//alert("$currentKat is: " + $currentKat.id);
	}, function() {
		$(this).removeClass('hover'); 
 	});


/*	$('#3').click(function() {
		//alert("$this id is: " + this.id);
		$('.auswahl').css('display', 'none'); 

	//	$('#1 .auswahl').show();
		$(this).find('.auswahl').css('display', 'inline');

		

  });*/

	$('#filiale').css('display','none');
	$('#uhrzeit').css('display','none');
	$('#weiter').css('cursor', 'pointer');
	$('#weiter').css('display','none');
	$('#weiterSo').css('cursor', 'pointer'); 
	$('#weiterSo').css('display','none');
	

	
	$('#weiter').hover(function() {	
		$(this).addClass('hover'); 
		 if ($day == 'Sonntag') {
			//alert('Abholtag: '+ $day);
			$('#auswahl').slideUp('slow');
			$('#weiter').empty();
			$('<a href="bestellungSo.php" onFocus="this.blur()"> weiter >></a>').prependTo('#weiter');
		/*} else {
			$('#filiale').slideDown('slow');	
			$('#auswahl').slideDown('slow');
			$('#weiter').slideUp('slow'); */
		};
	}, function() {
		$(this).removeClass('hover'); 
 	});
	
	$('#weiterSo').hover(function() {	
		$(this).addClass('hover'); 
	}, function() {
		$(this).removeClass('hover'); 
 	});
	
		
	$('#weiter').click(function() {	
		//var $tag = $('form :input').value;	
		//alert($day);
		if ($day == 0) {
			alert('Bitte waehlen Sie einen Tag: '+ $day);
			//$('#inlineDay :input').focus();
			//$(':input#tag').focus();
			//$(':input').hide();  // this hides all input fields!!
			//$(':input Abholtag').hide();
			//$('#inlineDay :input').hide();
		/* } else {
			$('#filiale').slideDown('slow');	
			$('#auswahl').slideDown('slow');
			$('#weiter').slideUp('slow'); */
		};
	});
	

		
	// setup for warenkorb
	$('#kontakt').css('display','none');
	$('#senden').css('display','none');
	$('#kontaktdaten').css('display','inline');
	$('#kontaktdaten').css('cursor', 'pointer'); 
	
	$('#kontaktdaten').hover(function() {	
		$(this).addClass('hover'); 
	}, function() {
		$(this).removeClass('hover'); 
 	});
	
	$('#kontaktdaten').click(function() {	
		$('#kontakt').slideDown('slow');
		$('#senden').slideDown('slow');
		$('#kontaktdaten').slideUp('slow');
		
	});
	
	

	$('#submit').click(function() {
		$('#warning p').remove();
		if ($("#Anrede").val() != '' && $("#Vorname").val() != '' && $("#Nachname").val() != '' && $("#Telefon").val() != '' && $("#Email").val() != '') {
			return true;
		} else {
			if ($("#Anrede").val() == 'null') {
				$('<p>Bitte die gew&uuml;nschte Anrede angeben</p>').prependTo('#warning');
				return false;
			} else 	if ($("#Vorname").val() == '') {
				//alert('Bitte den Vornamen angeben');
				$('<p>Bitte Ihren Vornamen angeben</p>').prependTo('#warning');
				return false;
			} else if ($("#Nachname").val() == '') {
				$('<p>Bitte Ihren Nachnamen angeben</p>').prependTo('#warning');
				return false;
			} else if ($("#Telefon").val() == '') {
				$('<p>Bitte Ihre Telefonnummer angeben</p>').prependTo('#warning');
				return false;
			} else if ($("#Email").val() == '') {
				$('<p>Bitte Ihre Email-Adresse angeben</p>').prependTo('#warning');
				return false;
			};
		};
	});
	

	

	
	
	//$('#check').hover(function() {	 // problems in Safari with hover; won#t trigger until mouse is moved off and then on again!
	$('#check').mousemove(function() {  // mousemove works also in Safari
		//alert('Doing check: Abholtag: '+ $day);
		if ($picupMonth != $monatNr) {
		  if ($day == 'Sonntag') {
			//alert('Doing check: Abholtag: '+ $day);
			$('#filiale').slideUp('slow');
		//	$('#auswahl').slideUp('slow');
			$('#weiter').slideUp('slow');
			$('#weiterSo').empty();
			$('<a href="bestellungSo2.php?Abholtag=' + $day + '&Datum=' + $picupDate + '" onFocus="this.blur()"> Sonntags-Auswahl >></a>').prependTo('#weiterSo');
			//or add Filiale too? how to get varialbel Filiale?
			//$('<a href="bestellungSo2.php?Abholtag=' + $day + '&Datum=' + $picupDate + '&Filiale=' + $filiale + '" onFocus="this.blur()"> weiter >></a>').prependTo('#weiterSo');
		//	$('<a href="bestellungSo2.php?Abholtag=' + $day + '&Datum=' + $picupDate + '&Filiale=1 " onFocus="this.blur()"> weiter >></a>').prependTo('#weiterSo');
			$('#weiterSo').slideDown('slow');
			
			} else if ($day == 'Montag') {
				// Store La Mart is closed on Mondays, so hide it
				$('form #filiale select option:last').hide();
				$('#filiale').slideDown('slow');
				$('#uhrzeit').slideUp('slow');
				$('#weiterSo').slideUp('slow');
		
		  } else if ($day != 0) {
				//alert($picupDate);
		//	valFiliale();
			//alert('no Sunday and month '+ $picupMonth);
			$('form #filiale select option:last').show();
			$('#filiale').slideDown('slow');
			$('#uhrzeit').slideUp('slow');
		//	$('#auswahl').slideUp('slow');
			
			$('#weiterSo').slideUp('slow');
		//	$('#weiter').empty();
			//$('<a href="bestellung2.php?Abholtag=' + $day + '&Datum=' + $picupDate + '" onFocus="this.blur()"> weiter >></a>').prependTo('#weiter');
			//or add Filiale too? how to get varialbel Filiale?
			//$('<a href="bestellung2.php?Abholtag=' + $day + '&Datum=' + $picupDate + '&Filiale=' + $filiale + '" onFocus="this.blur()"> weiter >></a>').prependTo('#weiter');
		//	$('<a href="bestellung2.php?Abholtag=' + $day + '&Datum=' + $picupDate + '&Filiale=3 " onFocus="this.blur()"> weiter >></a>').prependTo('#weiter');
		//	$('#weiter').slideDown('slow');
		  };
		} else if ($day == 'Sonntag') {
			//alert('Abholtag: '+ $day);
			$('#filiale').slideUp('slow');
		//	$('#auswahl').slideUp('slow');
			$('#weiter').slideUp('slow');
			$('#weiterSo').empty();
			$('<a href="bestellungSo.php?Abholtag=' + $day + '&Datum=' + $picupDate + '" onFocus="this.blur()"> Sonntags-Auswahl >></a>').prependTo('#weiterSo');
			//or add Filiale too? how to get varialbel Filiale?
			//$('<a href="bestellungSo.php?Abholtag=' + $day + '&Datum=' + $picupDate + '&Filiale=' + $filiale + '" onFocus="this.blur()"> weiter zur Sonntagsbestellung >></a>').prependTo('#weiterSo');
		//	$('<a href="bestellungSo.php?Abholtag=' + $day + '&Datum=' + $picupDate + '&Filiale=3 " onFocus="this.blur()"> weiter zur Sonntagsbestellung  >></a>').prependTo('#weiterSo');
			$('#weiterSo').slideDown('slow');
		
		 } else if ($day == 'Montag') {
			// alert('date: ' + $day);
			// Store La Mart is closed on Mondays, so hide it
			$('form #filiale select option:last').hide();
			//	var $findLast = $("#Filiale").find(":last")
			//	alert('findLast: ' + $findLast);
			$('#filiale').slideDown('slow');
			$('#weiterSo').slideUp('slow');
		
		} else if ($day != 0) {
			if  ($picupDate != 0) {
				// alert('date: ' + $picupDate);
				/* var $timeValue = $("#Uhrzeit").val();
				if  ($timeValue != 0) { 	
					alert('$timeValue: ' + $timeValue);
				} else { 
					alert('Bitte geben Sie die Abholzeit ein');
				};*/
			}
			 $('form #filiale select option:last').show();
			 $('#filiale').slideDown('slow');
			$('#weiterSo').slideUp('slow');
		};
	}, function() {
		//$(this).removeClass('hover'); 
 	});
	

	function valTag() {
      //var dayValue = $("#tag").val();
	 // var dayText = $("#tag").text();
	  var dayValue = $("#datum").val();
	  var dayText = $("#datum").text();
	  if ($day == 'Sonntag') {
		  //alert('$day:' + $day);
		  //$("#Filiale").find('option:gt(3)').remove();
		 //alert('$day: ' + $day + 'dayValue:' + dayValue + 'dayText:' + dayText);
	  	$("#selectTester p").empty();
      	$("#selectTester p").html("<b>$day:</b> " + $day + "<b>dayValue:</b> " + dayValue + " <b>dayText:</b> " + dayText);
		
	  } else {
		 //alert('$day: ' + $day + 'dayValue:' + dayValue + 'dayText:' + dayText);
	  	$("#selectTester p").empty();
      	$("#selectTester p").html("<b>$day:</b> " + $day + "<b>dayValue:</b> " + dayValue + " <b>dayText:</b> " + dayText);
	  };
	  
    }

	$("#datum").blur(valTag);
    //valTag();
	

	

	// show auswahl after user made a choice of time in input select Uhrzeit, 
	function valTime() {
		//alert('user made choice');
      var timeValue = $("#Uhrzeit").val();
      if (timeValue != '') {
		 $('#auswahl').slideDown('slow');
	 	 //$('#weiter').slideDown('slow');
	  } 
    } 
	

    $("#Uhrzeit").change(valTime);
	

	
	// calculate Total after clicking "berechnen" or changes in input .angebot or .stueckzahlAngebot
	//$total = 0;
	$formatedTotal = 0;
	$('#result').css('display', 'inline');
	
	var calculate = function() {	
		//$('#result li').remove();		
		$('#result li').val(0);	
		if ($(this).is('.berechnen') || $(this).is(':input.angebot') || $(this).is(':input.belag') || $(this).is(':input.stueckzahlAngebot')  ) {
			var $total = 0;
			$('#result').find('li').remove();

				
			 $('input.angebot').each(function() {
				if (this.value != '') {
				//	alert('this.value: ' + this.value);
					//var $bestellPrice = $(this).next('span').find('.bestellpreis').text(); // problem arose after adding marker NEU to produktname (next to produktname)
					var $bestellPrice = $(this).parent('.angebot').find('.bestellpreis').text(); // this way we find the bestellpreis even after adding extra element in produkt label
					var $amount = this.value * $bestellPrice;
				//	alert('$amount: ' + $amount);
					$total = $total + $amount;
				};									  
												  
			 });		
			  $('input.belag').each(function() {
				if (this.value != '') {
					var $bestellPrice = $(this).parent('label').parent('.padd').next().next().find('.bestellpreis').text();
				//	alert($bestellPrice);
					var $amount = this.value * $bestellPrice;
				//	alert('$amount: ' + $amount);
					$total = $total + $amount;
			
				};									  
													  
			 });
			  
			 $('input.stueckzahlAngebot').each(function() {
				if (this.value != '') {
					//alert('this.value: ' + this.value);
					if (this.value%2 == 0) {
					var $bestellPrice = $(this).parent('.stueckzahlAngebot').find('.bestellpreis').text();
					//alert($bestellPrice);
					var $amount = this.value/2 * $bestellPrice;
					//alert('$amount: ' + $amount);
					$total = $total + $amount;
					} else {
						$(':input.stueckzahlAngebot').focus();
						$('<li>Unser Tagesangebot ist nur paarweise erhaeltlich.</li>').prependTo('#result');
						//alert('Bitte geben Sie eine gerade Zahl ein');
						//$(':input:last').focus();
					};	
			
				};		
													  
			 });

			//Format numbers for currency
			$total = (Math.round(($total-0)*100))/100;
            $total = ($total == Math.floor($total)) ? $total + ",00" : (($total*10 == Math.floor($total*10)) ? $total + "0" : $total);
			$total = String($total);
            var ps = $total.split('.');
            var whole = ps[0];
            var sub = ps[1] ? ','+ ps[1] : ',00';
            var r = /(\d+)(\d{3})/;
            while (r.test(whole)) {
                whole = whole.replace(r, '$1' + '.' + '$2');
            }
			//alert(whole + sub + " &euro;");
			$formatedTotal = whole + sub;
			//alert($total + ' Test v: ' + v);	
			//alert($total);
			if ($total != "0,00") {
				$('<li>Gesamtbetrag: ' + $formatedTotal + ' &euro;</li>').prependTo('#result');
				//$('#result').css('display', 'inline');
				$('<input type=hidden name=Betrag value="' + $formatedTotal + '">').insertBefore('#marker');
			} else {
				$('<li>Warenkorb ist leer</li>').prependTo('#result');
				//$('#result').css('display', 'inline');
			};
		};
	};
	
	
	$('#frm_bestellung :button').click(calculate) ;
	$('#frm_bestellung :input').change(calculate) ;
	//$('form :button').click(calculate) ;	
	//$('form :input').change(calculate) ;
	
	
	$('form :button').click(function() {		
		//$('#result li').remove();		
		$('#result li').val(0);	
		if ($(this).is('.berechnen') ) {
			var $total = 0;
			$('#result').find('li').remove();

				
			 $('input.angebot').each(function() {
				if (this.value != '') {
				//	alert('this.value: ' + this.value);
					//var $bestellPrice = $(this).next('span').find('.bestellpreis').text(); // problem arose after adding marker NEU to produktname (next to produktname)
					var $bestellPrice = $(this).parent('.angebot').find('.bestellpreis').text(); // this way we find the bestellpreis even after adding extra element in produkt label
					var $amount = this.value * $bestellPrice;
				//	alert('$amount: ' + $amount);
					$total = $total + $amount;
				};									  
												  
			 });		
			  $('input.belag').each(function() {
				if (this.value != '') {
					var $bestellPrice = $(this).parent('label').parent('.padd').next().next().find('.bestellpreis').text();
				//	alert($bestellPrice);
					var $amount = this.value * $bestellPrice;
				//	alert('$amount: ' + $amount);
					$total = $total + $amount;
			
				};									  
													  
			 });
			  
			 $('input.stueckzahlAngebot').each(function() {
				if (this.value != '') {
					//alert('this.value: ' + this.value);
					if (this.value%2 == 0) {
					var $bestellPrice = $(this).parent('.stueckzahlAngebot').find('.bestellpreis').text();
					//alert($bestellPrice);
					var $amount = this.value/2 * $bestellPrice;
					//alert('$amount: ' + $amount);
					$total = $total + $amount;
					} else {
						$('<li>Bitte geben Sie eine gerade Zahl ein</li>').prependTo('#result');
						//alert('Bitte geben Sie eine gerade Zahl ein');
						//$(':input:last').focus();
						$('input.stueckzahlAngebot').focus();
					};	
			
				};		
													  
			 });

			//Format numbers for currency
			$total = (Math.round(($total-0)*100))/100;
            $total = ($total == Math.floor($total)) ? $total + ",00" : (($total*10 == Math.floor($total*10)) ? $total + "0" : $total);
			$total = String($total);
            var ps = $total.split('.');
            var whole = ps[0];
            var sub = ps[1] ? ','+ ps[1] : ',00';
            var r = /(\d+)(\d{3})/;
            while (r.test(whole)) {
                whole = whole.replace(r, '$1' + '.' + '$2');
            }
			//alert(whole + sub + " &euro;");
			$formatedTotal = whole + sub;
			//alert($total + ' Test v: ' + v);	
			//alert($total);
			if ($total != "0,00") {
				$('<li>Gesamtbetrag: ' + $formatedTotal + ' &euro;</li>').prependTo('#result');
				//$('#result').css('display', 'inline');
				$('<input type=hidden name=Betrag value="' + $formatedTotal + '">').insertBefore('#marker');
			} else {
				$('<li>Warenkorb ist leer</li>').prependTo('#result');
				//$('#result').css('display', 'inline');
			};
		};
	});
	
	$('form :input').change(function() {		
		//$('#result li').remove();							 
		if ($(this).is('.angebot') || $(this).is('.stueckzahlAngebot')  ) {
			var $total = 0;
			$('#result').find('li').remove();

				
			 $('input.angebot').each(function() {
				if (this.value != '') {
				//	alert('this.value: ' + this.value);
					//var $bestellPrice = $(this).next('span').find('.bestellpreis').text(); // problem arose after adding marker NEU to produktname (next to produktname)
					var $bestellPrice = $(this).parent('.angebot').find('.bestellpreis').text(); // this way we find the bestellpreis even after adding extra element in produkt label
					var $amount = this.value * $bestellPrice;
				//	alert('$amount: ' + $amount);
					$total = $total + $amount;
				};									  
												  
			 });		
			  $('input.belag').each(function() {
				if (this.value != '') {
					var $bestellPrice = $(this).parent('label').parent('.padd').next().next().find('.bestellpreis').text();
				//	alert($bestellPrice);
					var $amount = this.value * $bestellPrice;
				//	alert('$amount: ' + $amount);
					$total = $total + $amount;
			
				};									  
													  
			 });
			  
			  $('input.stueckzahlAngebot').each(function() {
				if (this.value != '') {
					//alert('this.value: ' + this.value);
					if (this.value%2 == 0) {
					var $bestellPrice = $(this).parent('.stueckzahlAngebot').find('.bestellpreis').text();
					//alert($bestellPrice);
					var $amount = this.value/2 * $bestellPrice;
					//alert('$amount: ' + $amount);
					$total = $total + $amount;
					} else {
						$('<li>Bitte geben Sie eine gerade Zahl ein</li>').prependTo('#result');
						//alert('Bitte geben Sie eine gerade Zahl ein');
						$(':input:last').focus();
					};	
			
				};		
													  
			 });
			    

			//Format numbers for currency
			$total = (Math.round(($total-0)*100))/100;
            $total = ($total == Math.floor($total)) ? $total + ",00" : (($total*10 == Math.floor($total*10)) ? $total + "0" : $total);
			$total = String($total);
            var ps = $total.split('.');
            var whole = ps[0];
            var sub = ps[1] ? ','+ ps[1] : ',00';
            var r = /(\d+)(\d{3})/;
            while (r.test(whole)) {
                whole = whole.replace(r, '$1' + '.' + '$2');
            }
			//alert(whole + sub + " &euro;");
			$formatedTotal = whole + sub;
			//alert($total + ' Test v: ' + v);	
			//alert('$total: ' + $total);
			if ($total != "0,00") {
				$('<li>Gesamtbetrag: ' + $formatedTotal + ' &euro;</li>').prependTo('#result');
				//$('#result').css('display', 'inline');
				$('<input type=hidden name=Betrag value="' + $formatedTotal + '">').insertBefore('#marker');
			} else {
				$('<li>Warenkorb ist leer</li>').prependTo('#result');
				//$('#result').css('display', 'inline');
			};
		};
	});
	

	$('#clear').click(function() {
		$('#result li').val(0);	
		//alert('#result li: ' + $('#result li').val());
		$('#result li').remove();	
		$('#auswahl').slideUp('slow');
	});

	
 // set up for setting month automatically	
	 // array with german months names

 var $monatsNamen = ['Januar','Februar','März','April','Mai','Juni',
		'Juli','August','September','Oktober','November','Dezember'];
	
	
 // assign elements of date-objekt
 var heute = new Date();
 var $datum = heute.getDate();
 var $tag = heute.getDate(); // getDay doesn't work! $tag = heute.getDay();

 var $monatNr = heute.getMonth() + 1; // add one to show correkt month nr!!!
// var $monat = $monatsNamen[$monatNr]; //long Version, but need month nr when checking order anyways!
 var $monat = $monatsNamen[heute.getMonth()]; // but here we need zero based index!
 var $jahr = heute.getFullYear();
 
 	//alert($monatsNamen[heute.getMonth()]); 
	//alert("$tag: " + $tag + "$monatNr: " + $monatNr + " $jahr: " + $jahr);
	//alert($monat);

	
	// add this month in navigation home "Unsere Angebote des Monats ..."
	$('<span> ' + $monat + '</span>').prependTo('#monat');
	// add this month in index/ home "Unsere Angebote des Monats ..."
	$('<span> ' + $monat + '</span>').prependTo('#monatIndex');
	
	// add oder date in warenkorb
	//alert($tag + "." + $monatNr + "." + $jahr );
	$('<h3>&Uuml;berpr&uuml;fung Ihrer Bestellung vom ' + $tag + '.' + $monatNr + '.' + $jahr + '</h3>').prependTo('#bestellTag');
	
	
	
	// get value of input select Filialen, after user made a choice; had to move it here, after $monatNr has been assigned
	function valFiliale() {
      var storeValue = $("#Filiale").val();
	  var storeText = $("#Filiale").text();
	  //alert('storeValue:' + storeValue + 'storeText:' + storeText);
	  if (storeValue != null) {
       if ($picupMonth != $monatNr) {
		   //alert("$picupMonth != $monatNr");
	 	 $("#selectTester p").empty();
     	 $("#selectTester p").html("$picupMonth : " + $picupMonth + "<b> storeValue:</b> " + storeValue + " <b> storeText:</b> " + storeText);
	 	 $('#weiter').empty();
	 	 $('<a href="bestellung2.php?Abholtag=' + $day + '&Datum=' + $picupDate + '&Filiale=' + storeValue + ' " onFocus="this.blur()"> Auswahl >></a>').prependTo('#weiter');
	 	 $('#weiter').slideDown('slow');
		 //alert('storeValue:' + storeValue + 'storeText:' + storeText);
	   } else if ($picupMonth == $monatNr) {
		 $("#selectTester p").empty();
      	$("#selectTester p").html("$picupMonth : " + $picupMonth + "<b> storeValue:</b> " + storeValue + " <b> storeText:</b> " + storeText);
	  	$('#weiter').empty();
	 	$('<a href="bestellung1.php?Abholtag=' + $day + '&Datum=' + $picupDate + '&Filiale=' + storeValue + ' " onFocus="this.blur()"> Auswahl >></a>').prependTo('#weiter');
	 	$('#weiter').slideDown('slow');
	//	$('#uhrzeit').slideDown('slow');
		//$('#auswahl').slideDown('slow');
		//alert('storeValue:' + storeValue + 'storeText:' + storeText);
	   }
	   } else {
		   $('#weiter').slideUp('slow');
	  };
    } 
	

    $("#Filiale").change(valFiliale);
    //valFiliale();
	
	//$("#datum").change(valFiliale); // not good: when date changes, slides down weiter button, before store is chosen!!
 
});


