$(document).ready(function(){

	$("#selectie").change(function(){
		$(".verkooppunt").hide()
		$(document.getElementById(this.value)).show();
	});
	
	$(".verkooppunt").hide();
	
	
	/*
	
		WEBSHOP
	
	*/
	
	// format Number
	function fN(num){
	
		// formatNumber
		num += "";
		num = num.replace(",", ".");
		num *= 1;
	
		if (isNumeric(num,true,true)) {
			dnum = Math.round(num * 100)/100;
			fNString = dnum + "";
			if (fNString.indexOf(".") == -1) {fNString += ".00"}
			if (fNString.indexOf(".") == fNString.length-2) {fNString += "0"}
			return fNString.replace(".", ",");
		} else {
			return "0,00"
		}
	}
	
	// isNumeric
	function isNumeric(sText,decimals,negatives) {
			
		var isNumber=true;
		var numDecimals = 0;
		var validChars = "0123456789";
		
		if (decimals)  validChars += ".";
		if (negatives) validChars += "-";
		var thisChar;
		for (i = 0; i < sText.length && isNumber == true; i++) {  
			thisChar = sText.charAt(i); 
			if (negatives && thisChar == "-" && i > 0) isNumber = false;
			if (decimals && thisChar == "."){
				numDecimals = numDecimals + 1;
				if (i==0 || i == sText.length-1) isNumber = false;
				if (numDecimals > 1) isNumber = false;
			}
			if (validChars.indexOf(thisChar) == -1) isNumber = false;
		}
		return isNumber;
	}
	
	function calculateForm() {
		
		// Settings
		//$iAmount = 0;
		//$iTotal = 0;
		
		// Add a '0' in every amount box if it's empty
		$("#fl-produkt .item .form-amount").each(function(i) {
			
			// Name
			$aName = $(this).attr("id").split('_');
			
			// If the value is not set, place a '0'
			if($(this).val() < 1) {
				
				$(this).val(0);
				
			}
			
			// Set the correct value in the total box
			/*
			$("#prijs_"+ $aName[1]).val(fN($("#stukprijs_"+ $aName[1]).val()*$(this).val()));
			
			// Add to $iAmount
			$iAmount += ($(this).val()*1);
			
			// Add to $iTotal
			$iTotal += ($("#stukprijs_"+ $aName[1]).val()*$(this).val());
			*/
			
		});
		
		/*
		if($iAmount > 0 && $iAmount < 9) {
			$("#totaal_bedrag .bedrag").text("€  "+ fN($iTotal+($("#verzendkosten").val()*1)));
			$("#totaal").val(fN($iTotal+($("#verzendkosten").val()*1)));
		} else {
			$("#totaal_bedrag .bedrag").text("€  "+ fN($iTotal));
			$("#totaal").val(fN($iTotal));
		}
		*/
		
	}
	
	// Amount value has changed
	/*
	$("#fl-produkt .item .form-amount").change(function() {
		
		// Name
		$aName = $(this).attr("id").split('_');
		
		// Set the correct value in the total box
		$("#prijs_"+ $aName[1]).val(fN($("#stukprijs_"+ $aName[1]).val()*$(this).val()));
		
		// Calculate form
		//calculateForm();
		
	});
	*/
	
	// Amount box focus
	$("#fl-produkt .item .form-amount").focus(function() {
		
		if($(this).val() == 0) {
			$(this).val("");
		}
		
	});
	
	// Amount box blur
	$("#fl-produkt .item .form-amount").blur(function() {
		
		if($(this).val() == "") {
			$(this).val(0);
		}
		
	});
	
	// Reset the amount / price boxes
	/*
	$("#fl-produkt .item .reset-product").click(function() {
		
		// Name
		$aName = $(this).attr("id").split('_');
		
		// Reset the boxes
		$("#aantal_"+ $aName[1]).val(0);
		$("#prijs_"+ $aName[1]).val(fN(0));
		
		return false;
		
	});
	*/
	
	// Add the product (submit the form)
	$("#fl-produkt .item .add-product").click(function() {
		
		$(this).parent().parent().submit();
		return false;
		
	});
	
	// Update the product (submit the form)
	$("#cart .item .update-product").click(function() {
		
		$(this).parent().parent().submit();
		return false;
		
	});
	
	// Shipping costs
	if ($("#shippingcosts").length) {
		
		$("#cart .shippingcosts .bottom .right .costs").hide();
		$("#cart .shippingcosts .bottom .right .customs").hide();
		$selected = $("#shippingcosts").val().toLowerCase();
		
		if($selected == "") {
			
			$("#cart .shippingcosts .bottom .right .costs:first").show();
			$("#costs").val($("#cart .shippingcosts .bottom .right .costs:first input").val());
			
			$("#cart .shippingcosts .bottom .right .customs:first").show();
			$("#customs").val($("#cart .shippingcosts .bottom .right .customs:first input").val());
			
		} else {
			
			$("#cart .shippingcosts .bottom .right ."+ $selected).show();
			$("#costs").val(($("#cart .shippingcosts .bottom .right ."+ $selected +" #costs_"+ $selected).val()));
			$("#customs").val(($("#cart .shippingcosts .bottom .right ."+ $selected +" #customs_"+ $selected).val()));
			
		}
		
		$("#cart .totaal .right").text("€ "+ fN($("#subtotal").val()*1+$("#costs").val()*1+$("#customs").val()*1));
		
		// Calculate the shipping costs on change of the select
		$("#shippingcosts").change(function() {
			
			$selected = $(this).val().toLowerCase();
			$("#cart .shippingcosts .bottom .right .costs").hide();
			$("#cart .shippingcosts .bottom .right .customs").hide();
			
			if($selected == "") {
				
				$("#cart .shippingcosts .bottom .right .costs:first").show();
				$("#costs").val($("#cart .shippingcosts .bottom .right .costs:first input").val());
				
				$("#cart .shippingcosts .bottom .right .customs:first").show();
				$("#customs").val($("#cart .shippingcosts .bottom .right .customs:first input").val());
				
			} else {
				
				$("#cart .shippingcosts .bottom .right ."+ $selected).show();
				$("#costs").val(($("#cart .shippingcosts .bottom .right ."+ $selected +" #costs_"+ $selected).val()));
				$("#customs").val(($("#cart .shippingcosts .bottom .right ."+ $selected +" #customs_"+ $selected).val()));
				
			}
		
			$("#cart .totaal .right").text("€ "+ fN($("#subtotal").val()*1+$("#costs").val()*1+$("#customs").val()*1));
			
		});
	
	}
	
	// Calculate the form
	calculateForm();
	
	// Fancybox
	$("#fl-produkt .item a.product-image").fancybox();
	
	// Copy data
	$("#fl-produkt .register .copy a").click(function() {
		
		$("#aflever_straat").val($("#factuur_straat").val());
		$("#aflever_huisnummer").val($("#factuur_huisnummer").val());
		$("#aflever_postcode").val($("#factuur_postcode").val());
		$("#aflever_woonplaats").val($("#factuur_woonplaats").val());
		$("#aflever_land").val($("#factuur_land").val());		
		return false;
		
	});
	
	// Select payment type
	$("#fl-produkt .payment .right a").click(function() {
		
		$(this).parent().parent().find(".left").find("input").attr("checked", true);
		return false;
		
	});

});
