function check(which, verplicht, errorOld, emailerrorOld){
	var empty 	   		   	  = false;
	var veld  	   		   	  = "";
	var error 	   		   	  = false;
	var errorClass 		   	  = "error";
	var email	   		   	  = /^[_\.0-9a-z-]+\@([0-9a-z][0-9a-z-]*\.)+([a-z]{2,4})+$/i; //e-mail check
	var postcode  		   	  = /^[1-9]{1}[0-9]{3}[A-Z]{2}$/; // van 1000AA tot 8999ZZ - Let op, geen spatie!
	var oneSelected		   	  = false;
	
	var errorTekst 		   	  = Array();
	errorTekst['tekst']    	  = "- U heeft een of meerdere velden leeg gelaten\n";
	errorTekst['email']    	  = "- Het door u ingevoerde e-mailadres is niet geldig\n";
	errorTekst['postcode'] 	  = "- De door u ingevoerde postcode is niet geldig. Vul de postcode in met notatie 1234AB\n";
	errorTekst['checkbox'] 	  = "- U heeft bij een of meerdere checkboxen, geen checkbox aangevinkt.";
	errorTekst['radio'] 	  = "- U heeft bij een of meerdere radiobuttons, geen radiobutton aangevinkt.";
	
	var soortErrors 	   	  = Array();
	
	for (j=0;j<verplicht.length;j++){
		if(which[verplicht[j][0]]){
			veld  = which[verplicht[j][0]];
			error = false;
			
		    if(verplicht[j][1]=="string" || verplicht[j][1]=="textarea"){
				if(veld.className){
					className 	   = veld.className;
					
					//remove error class if exists
					veld.className = veld.className.replace(" "+errorClass,"");
				}else{
					veld.className = "";
				}
				
				if(veld.value=='' || veld.value==' '){
					error = true;
					empty = true;
					
					if(!soortErrors['tekst']){
						soortErrors['tekst'] = 'tekst';
					}
				}
				if((verplicht[j][0]=='email' || verplicht[j][0]=='e-mail' || verplicht[j][0]=='e-mailadres' || verplicht[j][0]=='emailadres') && !(email.test(veld.value))){
					error = true;
					empty = true;
					
					if(!soortErrors['email']){
						soortErrors['email'] = ['email'];
					}
				}
				if(verplicht[j][0]=='postcode' && !(postcode.test(veld.value))){
					veld.value = veld.value.toUpperCase();
											
					if (!postcode.exec(postcode)) {
						error = true;
						empty = true;
						
						if(!soortErrors['postcode']){
							soortErrors['postcode'] = 'postcode';
						}
					}
				}
				
				if(error){
					veld.className = veld.className+' '+errorClass;
					error = false;
				}
			}
			else if(verplicht[j][1]=="checkbox" || verplicht[j][1]=="checkboxset"){
				oneSelected = false;
				
				if(veld.length>1){	
					for(i=0; i<veld.length;i++){
						if(veld[i].checked){
							oneSelected = true;
						}
					}
				}
				else{
					if(veld.checked==true){
						oneSelected = true;	
					}
				}	
					
				if(!oneSelected){
					empty = true;		
					if(!soortErrors['checkbox']){
						soortErrors['checkbox'] = 'checkbox';
					}
				}					
			}
			else if(verplicht[j][1]=="radio"){
				oneSelected = false;	
				
				for(i=0; i<veld.length;i++){
					if(veld[i].checked){
						oneSelected = true;
					}
				}	
					
				if(!oneSelected){
					empty = true;		
					if(!soortErrors['radio']){
						soortErrors['radio'] = 'radio';
					}
				}
			}					
		}
	}
	
	if(empty){
		var message = "U heeft de volgende onderdelen van het formulier niet correct ingevoerd:\n";		
		for(i in soortErrors){
			if(errorTekst[soortErrors[i]]){
				message += errorTekst[soortErrors[i]];
			}
		}
		alert(message);
		return false;
	} else {
		which.spamchecker.value = "GEEN_SPAM";
		return true;
	}
}

(function($, undefined) {
	
	// More
	$.widget('vm.more', {
		options: {
			duration: 300,
			handler: '.handler',
			content: '.content',
			scroll: true,
			easing: 'easeOutCubic',
			animating: false
		},
		
		_create: function () {
			var self = this;
			
			this.handler = this.element.find(this.options.handler);
			this.content = this.element.find(this.options.content);
			this.inner = $('<div>');
			
			this.content.html(this.inner.hide().html(this.content.html()));
			this.content.css({
				'height': 0,
				'overflow': 'hidden'
			});
			
			this.handler.removeClass('opend').addClass('closed').click(function (event) {
				event.preventDefault();
				self.toggle();
			});
		},
		
		toggle: function () {
			if (!this.options.animating) {
				var self = this,
					status = (this.content.height() > 0) ? 'opend' : 'closed';
				
				this.content.css('overflow', 'visible').animate({
					'height': (status == 'opend') ? 0 : this.inner.height()
				}, this.options.duration, this.options.easing, function () {
					self.content.css('overflow', 'hidden');
					self.options.animating = false;
				});
				
				this.inner.fadeTo(this.options.duration, (status == 'opend') ? 0 : 1, this.options.easing);
				
				if (this.options.scroll) {
					var height = ($(window).scrollTop() + $(window).height()) + this.inner.height();
					$('html, body').animate({scrollTop: height}, this.options.duration, this.options.easing);
				}
				
				this.options.animating = true;
				this._trigger('toggle', {type:'toggle'}, ((status == 'opend') ? 'closed' : 'opend'));
			}
		}
	});
	
	// Thumbshow
	$.widget('vm.thumbshow', {
		options: {
			container: '.container',
			thumbs: '.thumb',
			activeClass: 'active',
			duration: 500
		},
		_create: function () {
			var self = this;
			
			this.container = this.element.find(this.options.container);
			this.thumbs = this.element.find(this.options.thumbs);
			
			this.thumbs.click(function (event) {
				event.preventDefault();
				self._set($(this));
			});
		},
		_set: function (thumb) {
			var self = this,
				href = thumb.attr('href'),
				img = new Image();
			
			$(img).load(function () {
				self.container.stop(true).animate({
					'opacity': 0
				}, self.options.duration, function () {
					$(this).css('background-image', 'url('+ href +')').animate({
						'opacity': 1
					}, self.options.duration);
				});
			}).attr('src', href);
			
			this.thumbs.removeClass(this.options.activeClass);
			thumb.addClass(this.options.activeClass);
		}
		
	});
	
	// Google Maps
	$.widget('snap.gmaps', {
		options: {
			options: new Array(0,0),
			zoom: 8,
			type: 'roadmap',
			scrollwheel: false,
			panControl: false,
    		zoomControl: true,
    		scaleControl: false,
    		mapTypeControl: false,
    		streetViewControl: false,
    		overviewMapControl: false,
    		zoomControlStyle: 'small',
    		icon: null,
    		title: null
		},
		
		_create: function () {
			var latlng = new google.maps.LatLng(this.options.position[0], this.options.position[1]);
			
			this.goptions = $.extend({
		    	center: latlng,
		      	mapTypeId: google.maps.MapTypeId[this.options.type.toUpperCase()],
		      	zoomControlOptions: {
      				style: google.maps.ZoomControlStyle[this.options.zoomControlStyle.toUpperCase()]
    			}
		    }, this.options);
		    
		    this.gmap = new google.maps.Map(this.element[0], this.goptions);
		    
		    this.gmarker = new google.maps.Marker({
			 	position: latlng,
				map: this.gmap,
				icon: this.options.icon,
				title: this.options.title
			});
		}
	});
	
})(jQuery);

$(document).ready(function () {
	
	$('#brands').more({
		duration: 500,
		handler: '.handler',
		content: '.brands-wrapper',
		
		toggle: function (event, status) {
			if (status == 'opend') $('.handler', this).removeClass('closed').addClass('opend');
			else $('.handler', this).removeClass('opend').addClass('closed');
		}
	});
	
	$('.listbrands li a').hover(function () {
		$('.grayscale', this).fadeOut(150);
		$('img[class!=grayscale]', this).fadeIn(150);
	}, function () {
		$('.grayscale', this).fadeIn(150);
		$('img[class!=grayscale]', this).fadeOut(150);
	});
	
	$('.listimages li').click(function () {
		window.location.href = $('a:first', this).attr('href');
	});
	
	$('#thumbshow').thumbshow({
		'container': '.thumbshow-big',
		'thumbs': '.thumbshow-thumbs a'
	});
	
	$('select#sidebar_merk').change(function () {
		var selectbrandel = $(this),
			selecttypeel = $('select#sidebar_type'),
			selectpartel = $('select#sidebar_onderdeel');
		
		$.getJSON('/selectbox/', {merk_id: $(this).val(), ajax: 'true'}, function(j) {
			if (j != null) {
				var options = '<option value="0">'+ translate['Kies een type'] +'</option>';	   		   
				for (var i = 0; i < j.length; i++) {
					options += '<option value="' + j[i].slug + '">' + j[i].typenaam + '</option>';
				}
				selecttypeel.attr('disabled', false).html(options);
			} else {
				var options = '';
				if (selectbrandel.val() != 0) {
					options = '<option value="0">'+ translate['Geen types gevonden'] +'</option>';
				} else {
					options = '<option value="0">'+ translate['Kies een type'] +'</option>';
				}
				selecttypeel.attr('disabled', true).html(options);
			}
		});
		
		selectpartel.attr('disabled', true).html('<option value="0">'+ translate['Kies een onderdeel'] +'</option>');
	});

	$('select#sidebar_type').change(function () {
		var selecttypeel = $(this),
			selectpartel = $('select#sidebar_onderdeel');
		
		$.getJSON('/selectbox/', {type_id: $(this).val(), ajax: 'true'}, function(j) {
			if (j != null) {
				var options = '<option value="0">'+ translate['Kies een onderdeel'] +'</option>';
				for (var i = 0; i < j.length; i++) {
					options += '<option value="' + j[i].slug + '">' + j[i].naam + '</option>';
				}
				selectpartel.attr('disabled', false).html(options);
			} else {
				var options = '';
				if (selecttypeel.val() != 0) {
					options = '<option value="0">'+ translate['Geen onderdelen gevonden'] +'</option>';
				} else {
					options = '<option value="0">'+ translate['Kies een onderdeel'] +'</option>';
				}
				selectpartel.attr('disabled', true).html(options);
			}
		});
	});

	$('select#sidebar_onderdeel').change(function () {
		$.getJSON('/selectbox/', {onderdeel_id: $(this).val(), ajax: 'true'});
		if (!isNaN($(this).val()) && $(this).val() > 0) {
			//$('#aanvraagbutton').show();
		} else {
			//$('#aanvraagbutton').hide();
		}
	});
	
	if ($('select#sidebar_merk').val() != 0) {
		$('select#sidebar_type').attr('disabled', false);
	}
	
	if ($('select#sidebar_type').val() != 0) {
		$('select#sidebar_onderdeel').attr('disabled', false);
	}
});


function createUrl (which){
	var location = '',
		selectbrandel = $('select#sidebar_merk'),
		selecttypeel = $('select#sidebar_type'),
		selectpartel = $('select#sidebar_onderdeel');
	
	if (selecttypeel.is(':disabled') && selectpartel.is(':disabled')) {
		location = '/onderdeel/';
	} else if (selectpartel.is(':disabled')) {
		location = '/onderdeel/'+ selectbrandel.val() +'/';
	} else {
		if (selectpartel.val() != 0) {
			location = '/onderdeel/'+ selectbrandel.val() +'/'+ selecttypeel.val() +'/'+ selectpartel.val() +'/#down';
		} else {
			location = '/onderdeel/'+ selectbrandel.val() +'/'+ selecttypeel.val() +'/';
		}
	}
	
	window.location.href = location;
	return false;
} 
