jQuery.fn.log = function (msg) {
	console.log("%s: %o", msg, this);
	return this;
};

$(document).ready(function(){
	mpReady();
});

function mpReady() {
	$(".mpValideerEMail").each(function() {
		var VeldId = $(this).attr("id");
		if(!$('#' + VeldId + 'Validatie').length) $(this).after('<img style="height: 11px; width: 11px;" src="\/Images\/ValidatieOnbekend.png" class="mpValideerEMailImg" id="' + VeldId + 'Validatie">')
		
		$(this).keypress(function() {
			$(this).css({border: '1px solid'});
			if($('#' + VeldId + 'Validatie').attr("src")!=="\/Images\/ValidatieOnbekend.png") $('#' + VeldId + 'Validatie').attr("src","\/Images\/ValidatieOnbekend.png");
		});
		$(this).blur(function() {
			Ajax('/?Ajax=CheckEmailadres', 'Id=' + VeldId + '&Emailadres=' + $(this).val());
		});
		if($(this).val()!=='') $(this).blur();
	});
	$(".mpScroll").click(function(event){
		event.preventDefault();
		var full_url = this.href;
		var parts = full_url.split('#');
		var trgt = parts[1];
		var target_offset = $('a[name=' + trgt + ']').offset();
		var target_top = target_offset.top;
		$('html, body').animate({scrollTop:target_top}, 500);
	});
	$(".BetaalOptie").each(function() {
		$(this).addClass('Hidden');
	});
	$(".BetaalOptieRij").click(function() {
		BetaalOptieId = $(this).children('.BetaalOptieRadio').children("input[type=radio]").attr("id").replace("bo_", "");
		OpenBetaalOptie();
		$("#bo_" + BetaalOptieId).blur();
	});
	$("#frmWinkelwagenBezorgEnBetaalinfo").submit(function() {
		$("#Submit").attr("disabled", "true");
	});
	if(typeof(BetaalOptieId)!=="undefined") OpenBetaalOptie();
	$('.Foto').lightBox();
	$('#ProductAantal').change(function() {
		BerekenProductPrijs();
	});
	$('.ProductCombinaties').change(function() {
		BerekenProductPrijs();
	});
	$('.ProductMeebestellers').change(function() {
		BerekenProductPrijs();
	});
	if($('#ProductCategorieId').length!==0) BerekenProductPrijs();
}

function BerekenProductPrijs() {
	var ProductAantal = $('#ProductAantal').val();
	var ProductTotaalPrijs, c, d, e;
	var IsKortingAantal;
	var aantGeenKorting = 0;
	var ProductTotaalPrijs = 0;
	
	if(ProductKortingAantal!==0 && ProductAantal >= ProductKortingAantal) {
		var aantGeenKorting, aantVoorKorting;
		aantGeenKorting = ProductAantal % ProductKortingAantal;
		aantVoorKorting = (ProductAantal - aantGeenKorting) / ProductKortingAantal;
		ProductTotaalPrijs = (aantVoorKorting * ProductKortingPrijs) + (aantGeenKorting * ProductPrijs);
		IsKortingAantal = true;
	} else {
		ProductTotaalPrijs = ProductAantal * ProductPrijs;
		IsKortingAantal = false;
	}
	if(AantalCombies!==0) {
		var PrijsCorrectie = 0;
		for(c=1;c<=5;c++) {
			if(c<=ProductAantal) {
				var a, CombinatieId;
				$('#ProductCombinatie' + c).attr('style', '');
				for (a=1;a<=AantalCombies;a++) {
					PrijsCorrectie = PrijsCorrectie + mpGetProductCombinatiePrijsCorrectie($('#ProductCombinatie' + c + '_' + a).val())
				}
			} else {
				$('#ProductCombinatie' + c).attr('style', 'display: none');
			}
		}
		ProductTotaalPrijs = ProductTotaalPrijs + PrijsCorrectie;
	}
	if(AantalMeebestellers!==0) {
		for(d=1;d<=AantalMeebestellers;d++) {
			var ProductMeebestellerMaxAantal = mpGetProductMeebestellerMaxAantal(d);
			if(ProductMeebestellerMaxAantal == "1PerProduct") {
				if($('#ProductMeebesteller' + d + 'Aantal').attr('checked')) {
					var AantalPerMeebestellers = 1;
				} else {
					var AantalPerMeebestellers = 0;
				}
			} else {
				var AantalPerMeebestellers = $('#ProductMeebesteller' + d + 'Aantal').val();
			}
			if(ProductMeebestellerMaxAantal == "Bestelaantal") {
				if(AantalPerMeebestellers>ProductAantal) {
					var PullDownMeebesteller = document.getElementById('ProductMeebesteller' + d + 'Aantal');
					PullDownMeebesteller.options.length = 0;
					for(e=0;e<=5;e++) {
						PullDownMeebesteller.options[e] = new Option(e, e, e==ProductAantal, e==ProductAantal);
					}
					AantalPerMeebestellers = ProductAantal;
				}
			} else {
				AantalPerMeebestellers = AantalPerMeebestellers;
			}
			ProductTotaalPrijs = ProductTotaalPrijs + (AantalPerMeebestellers * mpGetProductMeebestellerPrijs(d));
		}
	}
	$('#ProductTotaalPrijs').html(mpMaskValuta(ProductTotaalPrijs));
}

function OpenBetaalOptie() {
	$("#bo_" + BetaalOptieId).attr("checked", true);
	$(".BetaalOptie").each(function() {
		$(this).addClass('Hidden');
	});
	//$(".BetaalOptie").hide();
	$("#BetaalOptie_" + BetaalOptieId).removeClass('Hidden');
	BerekenTotaal();
}

function BerekenTotaal() {
	var TotaalBedrag = TotaalPrijs + Verzendkosten + ExtraVerzendkosten - Korting;
	var BetaalToeslag = mpGetBetaalToeslag(BetaalOptieId);
	TotaalBedrag = TotaalBedrag + BetaalToeslag;
	$('#htmlBetaalToeslag').html(mpMaskValuta(BetaalToeslag));
	$('#htmlTotaalBedrag').html(mpMaskValuta(TotaalBedrag));
}

function mpGetBetaalToeslag(BetaalOptieId) {
	for (var i = 0; i < JSONBetaalOpties.BetaalOpties.length; i++) {
		if(JSONBetaalOpties.BetaalOpties[i].BetaalOptieId==BetaalOptieId) {
			var BetaalToeslag = JSONBetaalOpties.BetaalOpties[i].BetaalToeslag;
			break;
		}
	}
	return BetaalToeslag;
}

function mpGetProductCombinatiePrijsCorrectie(ProductCombinatieId) {
	for (var i = 0; i < JSONProductCombinatiePrijsCorrectie.Records.length; i++) {
		if(JSONProductCombinatiePrijsCorrectie.Records[i].ProductCombinatieId==ProductCombinatieId) {
			var PrijsCorrectie = JSONProductCombinatiePrijsCorrectie.Records[i].PrijsCorrectie;
			break;
		}
	}
	return PrijsCorrectie;
}

function mpGetProductMeebestellerMaxAantal(MeebestellerOrder) {
	for (var i = 0; i < JSONProductMeebestellers.Records.length; i++) {
		if(JSONProductMeebestellers.Records[i].MeebestellerOrder==MeebestellerOrder) {
			var MaxAantal = JSONProductMeebestellers.Records[i].MaxAantal;
			break;
		}
	}
	return MaxAantal;
}

function mpGetProductMeebestellerPrijs(MeebestellerOrder) {
	for (var i = 0; i < JSONProductMeebestellers.Records.length; i++) {
		if(JSONProductMeebestellers.Records[i].MeebestellerOrder==MeebestellerOrder) {
			var MeebestellerPrijs = JSONProductMeebestellers.Records[i].MeebestellerPrijs;
			break;
		}
	}
	return MeebestellerPrijs;
}

function mpMaskValuta(Valuta) {
	var num = Valuta;
	num = num.toString().replace(/\$|/g, '');
	num = num.toString().replace(',', '.')
	if(isNaN(num)) num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10) cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+'.'+ num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + num + ',' + cents);
}

function ToggleWinkelwagenInloggen(Tabblad) {
	if(Tabblad=='Winkelwagen') {
		document.getElementById('GridWinkelwagen').style.display = '';
		document.getElementById('GridInloggen').style.display = 'none';
		document.getElementById('HeaderGrid').style.backgroundPosition = '0 0';
		
	} else {
		document.getElementById('GridWinkelwagen').style.display = 'none';
		document.getElementById('GridInloggen').style.display = '';
		document.getElementById('HeaderGrid').style.backgroundPosition = '0 -72px';
	}
}

function MaakZoekveldBlanco(Veld) {
	if(Veld.value==document.getElementById('DefaultZoektekst').value) {
		Veld.value = "";
	} else if (Veld.value=="") {
		Veld.value = document.getElementById('DefaultZoektekst').value;
	}
}

function MaakOpmerkingenveldBlanco(Veld) {
	if(Veld.value=="Vul hier eventuele opmerkingen bij uw bestelling in.") {
		Veld.value = "";
	} else if (Veld.value=="") {
		Veld.value = "Vul hier eventuele opmerkingen bij uw bestelling in.";
	}
}

function PreviewAfbeelding(Afbeelding, FotoNr) {
	if(YouTubeId!='') {
		document.getElementById('YouTubeFilm').style.display='none';
		document.getElementById('Preview').style.display='block';
	}
	document.getElementById('Preview').src='/Images/Thumbnail/Fixed250/' + Afbeelding;
	FotoSelectedId = FotoNr;
}

function ShowYouTube() {
	document.getElementById('YouTubeFilm').style.display='block';
	document.getElementById('Preview').style.display='none';
}

function Zoeken(Pagina) {
	if(Pagina!='') {
		document.getElementById('frmZoeken').action = "/Zoeken/Pagina-" + Pagina + ".html";
	} else {
		document.getElementById('frmZoeken').action = "/Zoeken.html";
	}
	document.getElementById('frmZoeken').submit();
}

function VeranderMediumCode(MediumCode) {
	MediumCode = MediumCode.slice(MediumCode.indexOf('|') + 1);
	if(MediumCode=="1") {
		//document.getElementById('WinkelwagenBezorgEnBetaalinfoMediumCodeCatalogusCode').style.display = '';
		document.getElementById('MediumCode').style.display = '';
	} else {
		//document.getElementById('WinkelwagenBezorgEnBetaalinfoMediumCodeCatalogusCode').style.display = 'none';
		document.getElementById('MediumCode').style.display = 'none';
	}
}

function GratisVerzenden(JaNee) {
	var StandaardVerzendkosten = document.getElementById('StandaardVerzenkosten').value;
	var SpecialeVerzenkosten = document.getElementById('SpecialeVerzenkosten').value;
	var NormaalTotaalBedrag = document.getElementById('NormaalTotaalBedrag').value;
	var GratisVerzendenTotaalBedrag = document.getElementById('GratisVerzendenTotaalBedrag').value;
	
	if(JaNee) {
		document.getElementById('htmlStandaardVerzenkosten').innerHTML = "0,00";
		document.getElementById('htmlSpecialeVerzenkosten').innerHTML = "0,00";
		document.getElementById('TotaalBedrag').innerHTML = GratisVerzendenTotaalBedrag;
	} else {
		document.getElementById('htmlStandaardVerzenkosten').innerHTML = StandaardVerzendkosten;
		document.getElementById('htmlSpecialeVerzenkosten').innerHTML = SpecialeVerzenkosten;
		document.getElementById('TotaalBedrag').innerHTML = document.getElementById('TotaalBedrag' + document.getElementById('BetaalOptie').value).value;
	}
}

function ToggleLogin() {
	if (document.getElementById('InloggenEMailadres').value == 'Uw e-mailadres') {
		document.getElementById('InloggenEMailadres').value = '';
		document.getElementById('InloggenWachtwoord').value = '';
	} else if (document.getElementById('InloggenEMailadres').value == '') {
		document.getElementById('InloggenEMailadres').value = 'Uw e-mailadres';
		document.getElementById('InloggenWachtwoord').value = 'Wachtwoord';		
	}
}

function OpenProducten() {
	document.getElementById('VerborgenArtikelen').style.display = 'block';
	document.getElementById('ProductenOpenen').style.display = 'none';
}

function CreateBookmarkLink() {
	title = document.title;
	url = window.location.href;

	if (window.sidebar) {
		window.sidebar.addPanel(title, url, "");
	} else if(window.external) {
		window.external.AddFavorite( url, title);
	} else {
		alert('Uw browser ondersteund geen mogelijkheid\nom deze pagina automatisch aan uw\nfavorieten/bookmarks toe te voegen.\n\nU kunt dit zelf doen via het menu van uw browser.');
	}
}

function WinkelwagenToevoegen(ProductCategorieId, ProductId, ProductAantal) {
	frm = document.createElement("form");
	frm.setAttribute("method", "post");
	frm.setAttribute("id", "tmpWinkelwagen");
	frm.setAttribute("name", "tmpWinkelwagen");
	frm.setAttribute("action", "/Winkelwagen.html");
	
	MaakFormInput("a", "ProductToevoegen", "hidden", frm);
	MaakFormInput("ProductCategorieId", ProductCategorieId, "hidden", frm);
	MaakFormInput("ProductId", ProductId, "hidden", frm);
	MaakFormInput("ProductAantal", ProductAantal, "hidden", frm);
	document.body.appendChild(frm);
	frm.submit()
}

function MaakFormInput(Name, Value, Type, Form) {
	inp = document.createElement("input");
	inp.setAttribute("name", Name);
	inp.setAttribute("value", Value);
	inp.setAttribute("type", Type);
	Form.appendChild(inp);
}

function Ajax(Actie, Data) {
	var xmlHttp=null;

	if (window.XMLHttpRequest) { 
		xmlHttp = new XMLHttpRequest();     // Firefox, Safari, ...
	} else if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  // Internet Explorer 
	}
	
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			if(!xmlHttp.responseText=='') {eval(xmlHttp.responseText)}
		}
	}
	xmlHttp.open('POST', Actie, true);
	xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	xmlHttp.send(Data);
}

var MailaFriendAanNaam = '';
var MailaFriendAanEMail = '';
var MailaFriendVanNaam = '';
var MailaFriendVanEMail = '';

var MailaFriendFrame = '<div id="cMailaFriend"><div id="MailaFriendBoven"><h2>Mail vriend of vriendin</h2><a href="#" onclick="javascript:MailaFriendSluiten(); return false;">Sluiten</a></div><div id="MailaFriendOnder"><p>Breng een vriend of vriendin op de hoogte van dit product. Vul hieronder de gegevens in en klik op verzenden.</p><div id="MailaFriendContent"></div><div id="MailaFriendPrivacy"><p>Ingevoerde gegevens worden éénmalig gebruikt en worden niet verstrekt aan derden.</p></div></div></div>'
var MailaFriendForm = '<form id="MailaFriend" action="/MailVriendVriendin.html?a=MailVriendVriendin"><div id="MailaFriendAan"><h3>Aan</h3><div class="MailaFriendInvoer"><label for="MailaFriendAanNaam">Naam *</label><input type="text" name="MailaFriendAanNaam" id="MailaFriendAanNaam" value="' + MailaFriendAanNaam + '" /></div><div class="MailaFriendInvoer"><label for="MailaFriendAanEMail">E-mail *</label><input type="text" name="MailaFriendAanEMail" id="MailaFriendAanEMail" class="mpValideerEMail" value="' + MailaFriendAanEMail + '" /></div></div><div id="MailaFriendVan"><h3>Van</h3><div class="MailaFriendInvoer"><label for="MailaFriendVanNaam">Naam *</label><input type="text" name="MailaFriendVanNaam" id="MailaFriendVanNaam" value="' + MailaFriendVanNaam + '" /></div><div class="MailaFriendInvoer"><label for="MailaFriendVanEMail">E-mail *</label><input type="text" name="MailaFriendVanEMail" id="MailaFriendVanEMail" class="mpValideerEMail" value="' + MailaFriendVanEMail + '" /></div></div><div id="MailaFriendSubmit"><input type="button" onclick="javascript:MailaFriend();" name="Submit" value="Verzenden" id="MailaFriendVerzenden" /></div></form>'

function MailaFriendOpen() {
	if($('#cMailaFriend').length==0) {
		$('#MailaFriendButton').after(MailaFriendFrame);
		MailaFriendOpnieuw();
	}
}

function MailaFriendOpnieuw() {
	$('#MailaFriendContent').html(MailaFriendForm);
	$('#MailaFriendAanNaam').val(MailaFriendAanNaam);
	$('#MailaFriendAanEMail').val(MailaFriendAanEMail);
	$('#MailaFriendVanNaam').val(MailaFriendVanNaam);
	$('#MailaFriendVanEMail').val(MailaFriendVanEMail);
	mpReady();
}

function MailaFriend() {
	MailaFriendAanNaam = $('#MailaFriendAanNaam').val();
	MailaFriendAanEMail = $('#MailaFriendAanEMail').val();
	MailaFriendVanNaam = $('#MailaFriendVanNaam').val();
	MailaFriendVanEMail = $('#MailaFriendVanEMail').val();
	var ProductId = $('#ProductId').val();
	var ProductCategorieId = $('#ProductCategorieId').val();
	$('#MailaFriendContent').html('<div id="MailaFriendVerzend"><p class="MailaFriendCenter"><img src="/Images/ImagePopUp-loading.gif" /></p><p class="MailaFriendCenter">Een ogenblik geduld a.u.b.</p></div>');
	Ajax('/MailaFriend.html', 'MailaFriendAanNaam=' + MailaFriendAanNaam + '&MailaFriendAanEMail=' + MailaFriendAanEMail + '&MailaFriendVanNaam=' + MailaFriendVanNaam + '&MailaFriendVanEMail=' + MailaFriendVanEMail + '&ProductId=' + ProductId + '&ProductCategorieId=' + ProductCategorieId);
}

function MailaFriendSucces() {
	MailaFriendAanNaam = '';
	MailaFriendAanEMail = '';
	$('#MailaFriendContent').html('<div id="MailaFriendSucces"><p class="MailaFriendCenter">E-mail succesvol verzonden!</p><p class="MailaFriendCenter"><input type="button" value="Sluiten" onclick="javascript:MailaFriendSluiten();"><input type="button" value="Opnieuw" onclick="javascript:MailaFriendOpnieuw();"></div>');
}

function MailaFriendFout() {
	$('#MailaFriendContent').html('<div id="MailaFriendFout"><p class="MailaFriendCenter">Fout tijdens verzenden!</p><p class="MailaFriendCenter"><input type="button" value="Sluiten" onclick="javascript:MailaFriendSluiten();"><input type="button" value="Opnieuw" onclick="javascript:MailaFriendOpnieuw();"></div>');
}

function MailaFriendSluiten() {
	$('#cMailaFriend').remove();
}


