function trim(value) {
  value = value.replace(/^\s+/,'');
  value = value.replace(/\s+$/,'');
  return value;
}

function validate_postalcode(postcode) {
    
    var valid = true;
    
    postcode = trim(postcode);
    
    if (postcode.indexOf(' ') > -1) {
        // Do split     
        p = postcode.split(' ');
        
        for( i = 0; i < p.length; i++) {
            p[i] = p[i].toUpperCase();    
        }

        if (p[0].length == 4 && p[1].length == 2) {
            if ((p[1].charAt(0) >= 'A' && p[1].charAt(0) <= 'Z') == false)
            {
                return false;
            }
            if ((p[1].charAt(1) >= 'A' && p[1].charAt(1) <= 'Z') == false)
            {
                return false;
            }
            for (i = 0; i < p[0].length; i++) {
                if ((p[0].charAt(i) >= 0 && p[0].charAt(i) <= 9) == false) {
                    return false;                        
                }
            }
        } else
        {
            return false;
        }
        
    } else {
        if (postcode.length != 6) {
            return false;
        }
        
        for(i = 0; i < 4; i++) {
            if ((postcode.charAt(i) >= 0 && postcode.charAt(i) <= 9) == false) {
                return false;
            }
        }
        
        for(i = 4; i < 6; i++) {
            p = postcode[i].toUpperCase();
            if ((p >= 'A' && p <= 'Z') == false) {
                return false;
            }
        }
    }
    return true;
}
function handleSpecialAdres() {
	if(jQuery('input.afleverAdres').is(':checked')){
		jQuery('.delivery').slideDown("slow");
		jQuery('.delivery input').each(function(index, obj) {
			if (jQuery(obj).hasClass('bedrijf') == false)
			  jQuery(obj).addClass('required');
		});
	}else{
		jQuery('.delivery').slideUp();
		jQuery('.delivery input').removeClass('required');
		jQuery('form#afrekenen').valid();
	}
}
jQuery(document).ready(function(){
	
	
	
	initializeEvents();
	
	jQuery('input.afleverAdres').change(function(){
		if (jQuery('form#afrekenen').get(0) != 'undefined')
		  handleSpecialAdres();
	});
	// aflever adres
	jQuery('input.bestelButton, button.bestelButton').click(function(){
		var form		= jQuery(this).parents('form');
		
		var productId 	= jQuery(this).attr('id').split('_')[1];
		var _aantal 	= form.find('input#aantal_' + productId).val();
		var aantal		= parseInt(_aantal);
		var attribute	= 0;
		var attributeId	= form.find('select#attr_' + productId).val();
		var zonderboter = form.find('input#boter_'+ productId).is(':checked');

				
		if(attributeId > 0){
			var attribute = attributeId;
		}
		
		// checken of er een geldig getal als aantal is ingevuld
		if( isNaN(aantal) == true || aantal < 1 ){
			alert( '"' + _aantal + '"' + ' is geen geldig aantal');
			
			return false;
		}
		else{
			addToCart(productId, aantal, attribute, zonderboter);
		}
	});
	
});

function initializeEvents(){
	jQuery('input.changeAantal').keyup(function(){
		var productId 	= jQuery(this).attr('id').split('-')[1];
		var _aantal 	= jQuery(this).val();
		var aantal		= parseInt(_aantal);
		
		// checken of er een geldig getal als aantal is ingevuld
		if( isNaN(aantal) == true || aantal < 1 ){
			alert( '"' + _aantal + '"' + ' is geen geldig aantal');
			jQuery(this).reset();
			return false;
		}
		else{
			updateAantal(productId, aantal);
		}
	});
	
	$("a.showDrinkList").click(function(){
		if( $(".drinksBox").is(':visible') ) {
			$(".drinksBox").slideUp();
		}
		else {
			$(".drinksBox").slideDown();
		}
		return false;
	});
	
	jQuery('button.bestelButton').click(function(){
		var form		= jQuery(this).parents('form');
		
		var productId 	= jQuery(this).attr('id').split('_')[1];
		var _aantal 	= form.find('input#aantal_' + productId).val();
		var aantal		= parseInt(_aantal);
		var attribute	= 0;
		var attributeId	= form.find('select#attr_' + productId).val();
		
		if(attributeId > 0){
			var attribute = attributeId;
		}
		
		// checken of er een geldig getal als aantal is ingevuld
		if( isNaN(aantal) == true || aantal < 1 ){
			alert( '"' + _aantal + '"' + ' is geen geldig aantal');
			
			return false;
		}
		else{
			addToCart(productId, aantal, attribute);
		}
		
	});
	
	

}

function addToCart(productId, aantal, attribute, zonderboter){
	$.post("/includes/winkelwagen.php", { addproductid: productId, productaantal: aantal, attribute: attribute, action: 'addproduct', zonderboter: zonderboter }, function(data) {
		$('#pr_'+productId).effect("highlight", { color: '#abfb7f' }, 1500);																																								
		$(".basketBox").replaceWith(data);
	});
	$.post("/includes/winkelwagengroot.php", function(data) {
		$(".orderHolder").html(data);
		initializeEvents();
	});
}

function deleteProduct(productId){
	$.post("/includes/winkelwagen.php", { productid: productId, action: 'deleteproduct' }, function(data) {
		$(".basketBox").replaceWith(data);
	});
}

function deleteProductFromCart(productId){
	$.post("/includes/winkelwagengroot.php", { productid: productId, action: 'deleteproduct' }, function(data) {
		$(".orderHolder").html(data);
		initializeEvents();
	});
}

function updateAantal(productId, aantal){
	$.post("/includes/winkelwagengroot.php", { productid: productId, productaantal: aantal, action: 'updateAantal' }, function(data) {
		$(".orderHolder").html(data);
		initializeEvents();
	});
}

