jQuery.fn.debug = function() {
  return this.each(function(){
    $.log(this);
  });
};
jQuery.log = function(message) {
  if( window.console && $.browser.mozilla ) {
	console.debug(message);
  } else {
 //    alert(message);
  }
};
jQuery.numFormat = function( float, decimals, sep1, sep2 ){
	var out = '';
	var arr = String( float ).split('.');
	
	for( var i=0; i<arr[0].length; i++ ){
		if( (arr[0].length-i)==3 && i>0 ){
			out += sep2;
		}
		out += arr[0].charAt(i);	
	}
	out += sep1;
	if( arr.length == 1 ){
		out += '00';
	} else {
		for( var i=0; i<decimals; i++ ){
			if( !arr[1].charAt(i) ){
				out += '0';
			} else {
				out += arr[1].charAt(i);
			}
		}
	}
	return out;
}
jQuery.moneyFormat = function( float ){
	return $.numFormat( float, 2, ',', '.' );	
}

// Create Namespace
Vandergoes = {};
// MainClass
Vandergoes.Main = function(){
	//$.log( 'Vandergoes.Main' );
	this.initialize();	
}

Vandergoes.Main.prototype = {
	initialize: function(){
		//$.log( 'Vandergoes.Main.initialize' );
		this.keyWordSearch();
		switch( window.template ){
			case "home":
                // Creating hoverscroll with fixed arrows
                $('#listslider').hoverscroll({
                    vertical: false,    // Display the list vertically or horizontally
                    width:    880,        // Width of the list container
                    height:   50,        // Height of the list container
                    arrows:   true,        // Display direction indicator arrows or not
                    arrowsOpacity: 0.7,    // Max possible opacity of the arrows
                    fixedArrows: false,  // Fixed arrows on the sides of the list (disables arrowsOpacity)
                    rtl:      true     // Print images from right to left
                });
                // Starting the movement automatically at loading
                // @param direction: right/bottom = 1, left/top = -1
                // @param speed: Speed of the animation (scrollPosition += direction * speed)
                var direction = -1,
                    speed = 2;
                $("#listslider")[0].startMoving(direction, speed);

				this.showHome();
				break;
			case "tips":
			case "faq":
				this.showFaq();
				break;
			case "producten":
				this.showProducten();
				break;
			case "producten_detail":
				if ($('#infodialog').length > 0) {
					$('#infodialog').dialog({ autoOpen: false, height: 300, width: 450 });
				}
				if( $('#prijsaanvraag_form').length > 0 ){
					this.showProducten_opaanvraag();	
				} else {
					this.showProducten_detail();
					this.addToCart();
				}
				break;
			case "accessoires":
				if ($('#infodialog').length > 0) {
					$('#infodialog').dialog({ autoOpen: false, height: 300, width: 450 });
				}		
				/*
				$('#q').quicksearch('#accessoirelist div.items div.item', {
					'onAfter' : function() {
						$('#accessoirelist div.items').show();
						//$.log($('#accessoirelist div.items:parent'));	
						//if ($('#accessoirelist div.items div.item:visible').size() > 0) {	
						//	$('#accessoirelist div.accessoiregroep div.plus').toggle();
						//	$('#accessoirelist div.accessoiregroep div.min').toggle();	
						//}					
					},
					'show' : function () {
						$('#accessoirelist div.accessoiregroep div.plus').toggle();
						$('#accessoirelist div.accessoiregroep div.min').toggle();
					},
					'hide' : function () {
						$('#accessoirelist div.accessoiregroep div.plus').toggle();
						$('#accessoirelist div.accessoiregroep div.min').toggle();
					}
					
				});
				*/
				this.showAccessoires();
				this.addAccessoiresToCart();
				break;
			case "cart_checkout":
				this.showCheckout();
				break;
			case "cart_overview_ideal":
			    this.submitToIdeal(); 
			    break;	
            case "cart_overview":
                this.confirmOrder(); 
                break;			    
		}
		
		if( $('#cartitemlist').length > 0 ){
			this.showCart();	
		}
		
		this.fixLinks();
		
	},
	showCart: function(){
		$("input.frm_aantal").each( function(i,e){
			$(e).parent().append('<span class="id" style="display:none;">' + i + '</span>');
		}  );
		
		$("input.frm_aantal").replaceNumInputs();
		var holder = this;
		$('input.frm_aantal').bind('change',function(e){
			holder.onChangeCartAantal( e );
		} );
		
	},
	onChangeCartAantal: function( e ){
		//$.log("onChangeCartAantal " + $(e.target).html() + " price: " + $('span.itemprice', $(e.target).parent() ).html() );
		
		var aantal = Number( $(e.target).val() );
		if( aantal == 0 ){
			$(e.target).val(1);
			aantal = 1;
			$(e.target).triggerHandler("change");
		}
		
		var newprice = Number( $('span.itemprice', $(e.target).parent().parent() ).html().replace(',','.') ) * aantal ;
		
		$('span.itemsum', $(e.target).parent().parent() ).html( $.moneyFormat(newprice) );
		
		// send ajax request to update amounts in cart
		var index =$('span.id',$(e.target).parent().parent() ).html();
		
		var update_str = {index:index,amount:aantal};
		
		var holder = this;
		$.post("winkelwagen?action=update",update_str,function( data ){
			holder.onAjaxUpdateReady( data );				 
		},"text");
		
		// update totals
		this.updateCartTotal();
	},
	onAjaxUpdateReady: function( data ){
		//$.log( 'ajax call ready' );
	},
	updateCartTotal: function(){
		var price = 0;
		$('span.itemsum').each(function(i,e){
			price += Number( ( ( $(e).html() ).replace('.','')).replace(',','.') );
		});
		
		$('#totalexshipping').html( $.moneyFormat(price) );
		$('#totalamounttop').html( $.moneyFormat(price) );
		if( price < 500 ){
			var shipping = 50;
		} else {
			var shipping = 0;
		}
		$('#shippingcost').html( shipping );
		$('#totalincshipping').html( $.moneyFormat( price+shipping ) );
	},
	showHome: function(){
		//$.log( 'showHome' );
		$( 'div.home_aanbieding' ).each(function( i, e ){
			//$.log( i + e );
			$(e).click( function(){
				var url = $(this).find('a').attr('href');
				//$.log( 'click:' + url );
				document.location = url;
			});
			$(e).hover(
					function(){
						//$.log( 'hover' + e );
						$(this).addClass("hover");
					},
					function(e){
						$(this).removeClass("hover");
					}
			);
		} );
	},
	showFaq: function(){
		//$.log( 'showFaq' );
		$( '#faqlist div.faq div.question' ).each(function( i, e ){
			//$.log( i + e );
			$(e).click( function(){
				$(e).parent().find('div.answer').toggle("fast");
			});
			$(e).hover(
					function(){
						//$.log( 'hover' + e );
						$(this).addClass("hover");
					},
					function(e){
						$(this).removeClass("hover");
					}
			);
		} );
	},
	showProducten_opaanvraag: function(){
		//$.log( 'showProducten_opaanvraag' );
		var holder = this;
		$('div.prijsaanvraag_form_submit').click(function(){
			if( holder.checkAanvraagForm() ){
				$('#prijsaanvraag_form').submit();
			}
		});
	},
	checkAanvraagForm: function(){
		//$.log('checkAanvraagForm');
		// 	return true;
		var check = true;		
		// check dhr mevr.
		if ($("#frm_code").val() == '') {
		if( !$('#titel_dhr').is(':checked') && !$('#titel_mevr').is(':checked') ){
			check = false;
			$( '#lbl_dhr' ).addClass( 'error' );
			$( '#lbl_mevr' ).addClass( 'error' );
		} else {
			$( '#lbl_dhr' ).removeClass( 'error' );
			$( '#lbl_mevr' ).removeClass( 'error' );
		}
		
		// factuuradres
		var form =$( '#prijsaanvraag_form' );
		var labels = $( 'div.frm_label', form );
		for( i=0; i<labels.length; i++ ){
			if( labels[i].innerHTML.indexOf( '*' ) > -1 ){
				var id = (labels[i]).getAttribute( 'for' );
				//$.log('id: ' + id);
				var input = $( '#'+id );
				if( input.val() == '' ){
					$(labels[i]).addClass( 'error' );
					input.addClass( 'error' );
					check = false;
				} else {
					$(labels[i]).removeClass( 'error' );
					input.removeClass( 'error' );
				}
				if( id=="frm_email" && !Vandergoes.CheckOutForm.prototype.validateEmail( input.val() ) ){
					$(labels[i]).addClass( 'error' );
					input.addClass( 'error' );
					check = false;
				} else if( id=="frm_email" ) {
					$(labels[i]).removeClass( 'error' );
					input.removeClass( 'error' );
				}
             
			}
		}
		}
		
		return check;
	},
	
	showProducten: function(){
		//$.log( 'showProducten' );
		$("select").replaceSelectboxes();
		
		$("#productfilter_form").bind( "reset", function(){
			window.setTimeout( function(){
									$("#trefwoord").val('');
									$("#productfilter_form").submit();
										},250 );
		});
		
		$("select").change(function(i,e){
			//$.log('change event ' + $(this).attr("name") );
			if( $(this).attr("name") == "merk" ){
				$("select").each(function(i,e){
					if( $(e).attr("name")!="merk" ){
						$(e).val($("option", e).val());	
					}
				});
			}
			$("#productfilter_form").submit();
		});
		
		$( '#search_results div.search_result' ).each(function( i, e ){
			//$.log( i + e );
			$(e).click( function(){
				//$.log( 'click on ' + e );
				document.location = $('a:first',e).attr('href');
			});
			$(e).hover(
					function(){
						//$.log( 'hover' + e );
						$(this).addClass("hover");
					},
					function(e){
						$(this).removeClass("hover");
					}
			);
		} );
	},
	showCheckout: function(){
		new Vandergoes.CheckOutForm();
	},
	fixLinks: function(){
		$('a').each(function(i,e){
			if( $(e).attr("target")==null ){
				if( $(e).attr("href").substring(0,5)=="http:" && $(e).attr("href").indexOf("vandergoesoutlet") == -1 && $(e).attr("href").indexOf("beta.xd.nl") == -1 ){
					$(e).attr("target","_blank");
				} else {
					$(e).attr("target","_self");
				}
				if( !$(e).attr("title") ){
					$(e).attr("title","Direct naar: "+$(e).html());
				}
			}
		});
	},
	showProducten_detail: function(){
		//$.log('showProducten_detail');
		$("#accessoirelist").show();
		$("input.numinput").replaceNumInputs();
		var holder = this;
		// handle aantalpakken input
		$('#frm_aantalpakken').bind('change',function(e){
			var zaagverlies = Number( $('#frm_zaagverlies').val() );
			var zaagpercent = 1 + zaagverlies/100;
			var aantalpakken = Math.ceil( Number( $('#frm_aantalpakken').val().replace(',','.') ) );
			if( isNaN(aantalpakken) ){
				$('#frm_aantalpakken').val('');
				return;
			}
			$('#frm_aantalpakken').parent().removeClass('error');
			$( '.errorcomment', $('#frm_aantalpakken').parent().next() ).addClass( 'displaynone' );
			$('#frm_aantalpakken').val(aantalpakken);
			var m2pakken = Math.round( aantalpakken * Number( $('#frm_pakinhoud').val() ) *100 )/100;
			if( m2pakken > 0 ){
				$('#frm_m2pakken').html( '&nbsp;= ' + m2pakken + ' m2' );
			} else {
				$('#frm_m2pakken').html( '' );
			}
			//$('#frm_benodigdm2').val(Math.round( Number($('#frm_pakinhoud').val())*aantalpakken*1.05*100 )/100 );
		  	var prijsperpak = Number( $("#frm_prijspak").val() );
			var subtotal = Math.round( aantalpakken*prijsperpak*100)/100;
			$('#frm_subtotal_num').val( subtotal );
			holder.updateProductDetailPrice();
		});
		// handle aantalm2 input
		$('#frm_benodigdm2').bind('change',function(e){
			var aantalm2 = Math.ceil( Number( $('#frm_benodigdm2').val().replace(',','.') ) );
			if( isNaN(aantalm2) ){
				$('#frm_benodigdm2').val('');
				return;
			}
			var zaagverlies = Number( $('#frm_zaagverlies').val() );
			var zaagpercent = 1 + zaagverlies/100;
			$('#frm_benodigdm2').val(aantalm2);
			var aantalpakken = Math.ceil( (aantalm2*zaagpercent)/Number($('#frm_pakinhoud').val() ) );
			$('#frm_aantalpakken').val(aantalpakken)
								  .triggerHandler("change");
								  
			var m2pakken = Math.round( aantalpakken * Number( $('#frm_pakinhoud').val() ) *100 )/100;
			if( m2pakken > 0 ){
				$('#frm_m2pakken').html( '&nbsp;= ' + m2pakken + ' m2' );
			} else {
				$('#frm_m2pakken').html( '' );
			}
		  	var prijsperpak = Number( $("#frm_prijspak").val() );
			
			var subtotal = Math.round( aantalpakken*prijsperpak*100)/100;
			
			$('#frm_subtotal_num').val( subtotal );
			holder.updateProductDetailPrice();
		});
		// handle zaagverlies change
		$('#frm_zaagverlies').bind('change',function(e){
			$('#frm_benodigdm2').triggerHandler("change");
		} );
		
		// handle accessoire amount changes
		$('#accessoirelist div.aantal input' ).change( function(e,i){
			holder.updateProductDetailPrice();
		});
		
		var pos = $('#accessoirelist').position();
		$('#accessoirelist div.item div.extrainfo' ).hide()
													.css('left', pos.left + 'px');
		/*					
		$('#accessoirelist div.item div.infobutton' ).hover( function(){  $(this).next().fadeIn(); },
															function(){  $(this).next().fadeOut(); });
		*/
		$('#accessoirelist div.item div.infobutton' ).bind('click', function () {
			if ($('#infodialog').dialog('isOpen') ) {
				$('#infodialog').dialog('close');
			}
			$('#infodialog').html($(this).next().html());
			$('#infodialog').dialog('open');
		});
		
		
		$('#accessoirelist div.items').hide();
		
		$('#accessoirelist div.accessoiregroep').bind('click', function(){
			$(this).next().toggle();
			$('div.plus', this).toggle();
			$('div.min', this).toggle();
		});
		
	},
	updateProductDetailPrice: function(){
		this.updateAccessoiresPrice();
		var subtotal = Number( $('#frm_subtotal_num').val() );	
		
		var acctotal = Number( $('#frm_acctotal_num').val() );
		
		var total = subtotal + acctotal;
		//alert(  total );
		$('#frm_total_num').val( total );
		$('#frm_subtotal').html( '&euro; '+ $.moneyFormat(subtotal) );
		$('#frm_acctotal').html( '&euro; '+ $.moneyFormat(acctotal) );
		$('#frm_total').html( '&euro; '+ $.moneyFormat(total) );
	},
	updateAccessoiresPrice: function(){
		var total = 0;
		$('#accessoirelist div.aantal input.numinput' ).each( function(i,e){
			var price = $('input.access_prijs', $(e).parent().parent() ).val();
			total += Number( $(e).val() ) * price;
			if( Number( $(e).val() ) > 0 ){
				$(e).parent().parent().addClass('selected');	
			} else {
				$(e).parent().parent().removeClass('selected');	
			}
		} );
		$('#frm_acctotal_num').val( total );
	},
	addToCart: function(){
		$("#addtocart").bind("click", function(){
			//$.log('ajax addtochart');
			// check on aantalpakken
			if( $('#frm_aantalpakken').val() == 0 && $("#frm_acctotal").html() == "" ){
				$('#frm_aantalpakken').focus();
				$('#frm_aantalpakken').parent().addClass('error');
				$( '.errorcomment', $('#frm_aantalpakken').parent().next() ).removeClass( 'displaynone' );
				return;	
			}
			if( Number( $('#frm_acctotal_num').val() ) == 0  ){
				var gadoor = confirm("U heeft nog geen extra benodigdheden gekozen. Wilt u toch doorgaan?");
			} else {
				var gadoor = true;	
			}
			if( gadoor ){
				$('#artikel_opties_frm').submit();
			}
		});
	},
	addAccessoiresToCart: function(){
		$("#addtocart").bind("click", function(){
			//$.log('ajax addtochart');
			// check on aantalpakken
			if( Number( $('#frm_acctotal_num').val() ) == 0  ){
				var gadoor = confirm("U heeft nog geen extra benodigdheden gekozen. Wilt u toch doorgaan?");
			} else {
				var gadoor = true;	
			}
			if( gadoor ){
				$('#artikel_opties_frm').submit();
			}
		});
	},
	submitToIdeal: function() {
		$("#submit_to_ideal").bind("click", function(){
			$("#idealform").submit();
		});
	},
	confirmOrder: function() {
		$("#cart_overview_add_to_order").bind("click", function(){
			//$.log('click add to order');
			document.location = "winkelwagen?stage=confirm_order";
		});
	},
	keyWordSearch: function() {
		$("#trefwoord_submit").hover( function(){ $(this).addClass("submit_hover"); }, function(){ $(this).removeClass("submit_hover"); } );
		$("#trefwoord_submit").bind("click", function() {
			if( $("#trefwoord_query").val() == "trefwoord" ) return;
			$("#trefwoord_form").submit();
		});
		$("#trefwoord_query").bind("focus", function() {
			$("#trefwoord_query").val('');
			$("#trefwoord_query").removeClass('trefwoord_query');
		});
	},
	showAccessoires: function(){
		//$.log('showProducten_detail');
		$("#accessoirelist").show();
		$("input.numinput").replaceNumInputs();
		var holder = this;

		
		// handle accessoire amount changes
		$('#accessoirelist div.aantal input' ).change( function(e,i){
			holder.updateProductDetailPrice();
		});
		
		var pos = $('#accessoirelist').position();
		$('#accessoirelist div.item div.extrainfo' ).hide()
													.css('left', pos.left + 'px');
		/*					
		$('#accessoirelist div.item div.infobutton' ).hover( function(){  $(this).next().fadeIn(); },
															function(){  $(this).next().fadeOut(); });
		*/
		$('#accessoirelist div.item div.infobutton' ).bind('click', function () {
			if ($('#infodialog').dialog('isOpen') ) {
				$('#infodialog').dialog('close');
			}
			$('#infodialog').html($(this).next().html());
			$('#infodialog').dialog('open');
		});
		
		if (window.search_accessoires == "1") {
			//$('#accessoirelist div.accessoiregroep').toggle();
			$('#accessoirelist div.accessoiregroep div.plus').toggle();
			$('#accessoirelist div.accessoiregroep div.min').toggle();		
		} else {
			$('#accessoirelist div.items').hide();
		}
		
		$('#accessoirelist div.accessoiregroep').bind('click', function(){
			$(this).next().toggle();
			$('div.plus', this).toggle();
			$('div.min', this).toggle();
		});
		
	}
	
}

Vandergoes.CheckOutForm = function(){
	//$.log( "CheckOutForm -> construct" );
	this.initialize();
	
}

Vandergoes.CheckOutForm.prototype = {
	initialize: function(){
		var self = this;
		$('#rembours').html(0);
		$('#rembours').removeClass( 'displaynone' ); 
        $( '#bezorg_gelijk' ).attr('checked', true);
		price = Number( ( ( $('#totalexshipping').html() ).replace('.','')).replace(',','.') );
		//$.log(price);
		if (price < 500) {
			//$.log('shippingcost')
			$('#shippingcost').html(50);
		} else {			
			//$.log('no shippingcost')
			$('#shippingcost').html(0);
		}		
		
		if( price > 2500 ){
			//$.log('no rembours')
        	$('#rembours').html(0);
        	$('#rembours').removeClass( 'displaynone' ); 
            $('#paymethod_rembours').attr("disabled","disabled");            
		}		
		self.updateTotal(price);
		$( '#bezorg_gelijk' ).click( function() { 
            $('#form_bezorgadreshidden').addClass( 'displaynone' ); 
           //$('#paymethod_bank').removeAttr("disabled");
            $('#paymethod_rembours').removeAttr("disabled");
			if ($('#paymethod_rembours').is(':checked')) {
            	$('#rembours').html(25);
            	$('#rembours').removeClass( 'displaynone' ); 			
			}            
			if (price < 500) {
				$('#shippingcost').html(50);
			} else {
				$('#shippingcost').html(0);
			}
			self.updateTotal(price);
        });
		$( '#bezorg_anders' ).click( function() { 
            $('#form_bezorgadreshidden').removeClass( 'displaynone' ); 
            //$('#paymethod_bank').removeAttr("disabled");
            $('#paymethod_rembours').removeAttr("disabled");
			if ($('#paymethod_rembours').is(':checked')) {
            	$('#rembours').html(25);
            	$('#rembours').removeClass( 'displaynone' ); 			
			}   
			if (price < 500) {
				$('#shippingcost').html(50);
			} else {
				$('#shippingcost').html(0);
			}
			self.updateTotal(price);
        
        });
		$("#bezorg_ophalen").click(function () { 
            $('#form_bezorgadreshidden').addClass( 'displaynone' );
            if( $('#bezorg_ophalen').is(':checked') ){
                //paymethod_bank
                //$('#paymethod_bank').attr("disabled","disabled");

                //paymethod_rembours
            	$('#rembours').html(0);
            	$('#rembours').removeClass( 'displaynone' ); 
            	$('#paymethod_rembours').removeAttr("checked");
            	$('#paymethod_ideal').attr("checked", "checked");
                $('#paymethod_rembours').attr("disabled","disabled");
            	$('#shippingcost').html(0);
            	self.updateTotal(price);
            }
        });  
		if( $('#bezorg_ophalen').is(':checked') ){
        	$('#rembours').html(0);
        	$('#rembours').removeClass( 'displaynone' ); 
        	$('#paymethod_rembours').removeAttr("checked");
        	$('#paymethod_ideal').attr("checked", "checked");
        	$('#paymethod_rembours').attr("disabled","disabled");            
        	$('#shippingcost').html(0);
        	self.updateTotal(price);            
		}
		$('#paymethod_rembours').click(function(){
			if ($('#paymethod_rembours').is(':checked')) {
            	$('#rembours').html(25);	
            	self.updateTotal(price);
			}
		});
		$('#paymethod_ideal').click(function(){
			if ($('#paymethod_rembours').is(':unchecked')) {
            	$('#rembours').html(0);		
            	self.updateTotal(price);
			}
		});
		$('#paymethod_bank').click(function(){
			if ($('#paymethod_rembours').is(':unchecked')) {
            	$('#rembours').html(0);
            	self.updateTotal(price);		
			}
		});		
        
        
		
		if( $( '#bezorg_anders' ).checked ){
			$( '#form_bezorgadreshidden' ).removeClass('displaynone');
		}
				
		$( '#submit_checkout' ).click( function() { self.onClickSubmit_checkout() } );
	},
	updateTotal: function(price) {
		//$.log("Prijs in functie:" + price);
		var shipping = 0;
		if ($('#shippingcost').length) {
			shipping = parseFloat($('#shippingcost').html());
		}
		var rembours = 0;
		if ($('#rembours').length) {
			rembours = parseFloat($('#rembours').html());
		}
		$('#totalincshipping').html(  $.moneyFormat(price+shipping+rembours)  );	
		$('#totalamounttop').html( $.moneyFormat(price+shipping+rembours ) );
		//$.log("totaal Prijs in functie:"+ price+shipping+rembours);
	},	
	onClickSubmit_checkout: function(){
		this.submitForm();
	},	
	submitForm: function(){
		if( this.checkForm() ){
			$( '#checkout_form' ).submit();
		} else {
			alert( 'U heeft niet alle velden ingevuld!' );	
		}
	},
	validateEmail: function( email ){  
		var RE_EMAIL = /^[a-zA-Z.0-9]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+.[a-z]{2,4}$/;
		return RE_EMAIL.test( email );
	},
    validateZip: function(zip) {
        var RE_ZIP = /^[0-9]{4}\s*[a-zA-Z]{2}$/;
        return RE_ZIP.test( zip );
    },
	checkForm: function(){
		// 	return true;
		var check = true;		
		// check dhr mevr.
		if( !$('#titel_dhr').is(':checked') && !$('#titel_mevr').is(':checked') ){
			check = false;
			$( '#lbl_dhr' ).addClass( 'error' );
			$( '#lbl_mevr' ).addClass( 'error' );
		} else {
			$( '#lbl_dhr' ).removeClass( 'error' );
			$( '#lbl_mevr' ).removeClass( 'error' );
		}
		
		// factuuradres
		var form =$( '#form_factuuradres' );
		var labels = $( 'div.frm_label', form );
		for( i=0; i<labels.length; i++ ){
			if( labels[i].innerHTML.indexOf( '*' ) > -1 ){
				var id = (labels[i]).getAttribute( 'for' );
				var input = $( '#'+id );
				if( input.val() == '' ){
					$(labels[i]).addClass( 'error' );
					input.addClass( 'error' );
					check = false;
				} else {
					$(labels[i]).removeClass( 'error' );
					input.removeClass( 'error' );
				}
				if( id=="frm_email" && !this.validateEmail( input.val() ) ){
					$(labels[i]).addClass( 'error' );
					input.addClass( 'error' );
					check = false;
				} else if( id=="frm_email" ) {
					$(labels[i]).removeClass( 'error' );
					input.removeClass( 'error' );
				}
				if( id=="frm_postcode" && !this.validateZip( input.val() ) ){
					$(labels[i]).addClass( 'error' );
					input.addClass( 'error' );
					check = false;
				} else if( id=="frm_postcode" ) {
					$(labels[i]).removeClass( 'error' );
					input.removeClass( 'error' );
				}                
			}
		}
		
		
		// bezorgadres
		if( $('#bezorg_anders').is(':checked') ){
			// check dhr mevr.
			if( !$('#bezorg_titel_dhr').is(':checked') && !$('#bezorg_titel_mevr').is(':checked') ){
				check = false;
				$('#lbl_bezorg_dhr').addClass( 'error' );
				$('#lbl_bezorg_mevr').addClass( 'error' );
			} else {
				$('#lbl_bezorg_dhr').removeClass( 'error' );
				$('#lbl_bezorg_mevr').removeClass( 'error' );	
			}
			
			// check bezorgadres
			var form = $( '#form_bezorgadres' );
			var labels = $( 'div.frm_label', form );
			var check = true;
			for( i=0; i<labels.length; i++ ){
				if( labels[i].innerHTML.indexOf( '*' ) > -1 ){
					var id = (labels[i]).getAttribute( 'for' );
					var input = $( '#'+id );
		
					if( input.val() == '' ){
						$(labels[i]).addClass( 'error' );
						input.addClass( 'error' );
						check = false;
					} else {
						$(labels[i]).removeClass( 'error' );
						input.removeClass( 'error' );
					}
    				if( id=="frm_bezorg_postcode" && !this.validateZip( input.val() ) ){
    					$(labels[i]).addClass( 'error' );
    					input.addClass( 'error' );
    					check = false;
    				} else if( id=="frm_bezorg_postcode" ) {
    					$(labels[i]).removeClass( 'error' );
    					input.removeClass( 'error' );
    				}                    
				}
			}
		}
        //form_paymethod
        if (!$("#frm_av").is(':checked') ) {
            $("#frm_av").addClass('error');
            $("#frm_av_label").addClass('error');
            check = false;
        } else {
            $("#frm_av").removeClass('error');
            $("#frm_av_label").removeClass('error');        
		}
		return check;
	}
}



/* ----- Start Vandergoes ----- */

$(document).ready(function() {
	//$.log( 'ready' );
	window.site = new Vandergoes.Main();
});

