$(function(){
	$('.addtocart input[type=text]').keyup(function(){
		$(this).val($(this).val().replace(',','.'));
	});

	$('.special input[type=checkbox]').mouseup(function(){
		if($(this + ':checked').val() == 1){
			$(this).parent().find('textarea').hide();
		} else {
			$(this).parent().find('textarea').show();
		}
	});

	$('.addtocart>a').each(function(){
		$(this).click(function(){
			var thisID = $(this).attr('id');
			var ID = thisID.replace('addurl','');
			var amountID = 'add' + thisID.replace('addurl','');
			var amount = $('#' + amountID).attr('value');
			var special = $('#sc' + ID + ':checked').val();
			var specialwish = $('#special' + ID).val();
			/*$('#' + thisID).attr('href',"/shop.html?cart=add&id=" + ID + "&amount=" + amount );*/
			if(amount > 0){
				if(special == 1){
					window.location.href = "/vorbestellung.html?cart=add&id=" + ID + "&amount=" + amount + "&special=" + specialwish;
				} else {
					window.location.href = "/vorbestellung.html?cart=add&id=" + ID + "&amount=" + amount ;
				}
			} else {
				alert('Bitte wählen Sie als Anzahl mindestens 1.');
			}
			/*alert($('#' + thisID).attr('href'));*/
			/*window.location.href = '/shop.html';*/
			return false;
		});
	});
	$('input[name=shipping]').each(function(){
		$(this).click(function(){
			calc_total();
		});
	});
	$('input[name=payment]').each(function(){
		$(this).click(function(){
			calc_total();
		});
	});

	$('#sendcart').submit(function(){
		var anrede = $('input[name=anrede]').attr('value');
		var vorname = $('input[name=vorname]').attr('value');
		var nachname = $('input[name=nachname]').attr('value');
		var strasse = $('input[name=strasse]').attr('value');
		var plz = $('input[name=plz]').attr('value');
		var ort = $('input[name=ort]').attr('value');
		var email = $('input[name=email]').attr('value');
		var regemail = /^.+@.+\.\w{2,3}$/;
		var agb = $('input[name=agb]:checked').attr('value');
		if(
				anrede != '' &&
				vorname != '' &&
				nachname != '' &&
				strasse != '' &&
				plz != '' &&
				ort != '' &&
				email != '' &&
				regemail.exec(email) &&
				agb == '1'
		){
			return true;
		} else {
			alert("Bitte füllen sie alle Felder aus.");
			return false;
		}
	});

	$('#ordercart').submit(function(){
		if($('input[name=agb]:hidden').attr('value')!='1' && $('input[name=agb]:checked').attr('value') != '1'){
			alert("Bitte akzeptieren Sie die AGB.");
			return false;
		}
	});

	$('#abhol_termin').change(function(){
		if($(this).val() == "Laden Hemmoor"){
			$('#abhol_datum_p').show();
		} else {
			$('#abhol_datum_p').hide();
		}
	});
});

function calc_total(){
	var shipID = $('input[name=shipping]:checked').attr('id').replace('selship','');
	/*var payID = $('input[name=payment]:checked').attr('id').replace('selpay','');*/
	var brutto = eval($('input[name=total]').attr('value'));
	/*var payment = eval($('input[name=pay'+payID+']').attr('value'));*/
	var shipping = eval($('input[name=ship'+shipID+']').attr('value'));
	/*var newTotal = brutto + payment + shipping;*/
	var newTotal = brutto + shipping;
	$('.total>span').text(newTotal.toFixed(2).replace('.',','));
}
