function trim(s)
{
	return s.replace(/^\s+|\s+$/g, "");
}

function isFloat(s)
{
	var n = trim(s);
	return n.length >0 && !(/[^0-9.]/).test(n) && (/\.\d/).test(n);
}

function isInteger(s)
{
	var n = trim(s);
	return n.length > 0 && !(/[^0-9]/).test(n);
}

var done = false;
$(document).ready(function(){

	$.ajaxSetup ({ 
	    cache: false,
	    global: false
	});
	
	
	var h = $(window).height() - 195 - 26 - 6;
	if ($('#left').height() < h) $('#left').height(h);
	h = $(window).height() - 195 - 6;
	if ($('#right').height() < h) $('#right').height(h);
	
	h = 0;
	$('#nieuws div').each(function() {
		h = Math.max($(this).height(), h);
	});
	$('#nieuws').height(h);
	h = 0;
	$('ul.products li').each(function() {
		h = Math.max($(this).height(), h);
	});
	$('ul.products li').height(h);
	h = 0;
	$('#aanbieding div').each(function() {
		h = Math.max($(this).height(), h);
	});
	$('#aanbieding').height(h);
	$('#aanbieding').cycle();
	$('#nieuws').cycle();
	
	$('#content img').each(function() {
		var p = $(this).parent();
		if (p.attr('align') == "right") $(this).css('float', 'right');
		if (p.attr('align') == "left") $(this).css('float', 'left');
		if ($(this).attr('align') == "right") $(this).css('float', 'right');
		if ($(this).attr('align') == "left") $(this).css('float', 'left');
	});
	
	$('#content td').each(function() {
		if ($(this).attr('valign')) $(this).css('vertical-align', $(this).attr('valign'));
	});
	
	$('#content input[alt=required]').each(function() {
		$(this).addClass('required');
		if ($(this).attr('name') == 'email') $(this).addClass('email');
	});
	
	$('#generixform').validate({
		errorClass: "error",
		messages: {
		}, 
		errorPlacement: function(error, element) {
			if (done == false) alert('Graag uw aandacht voor de met rood gemarkeerde velden');
			done = true;
		},
		submitHandler: function(form) {
			done = false;
			$.post("/includes/contact-engine.php", $('#generixform').serializeArray(),
			function(data){
				$('#datastore').html(data);
			});
		}
	});

	$('#loginform').validate({
		errorClass: "error",
		messages: {
		}, 
		errorPlacement: function(error, element) {
			if (done == false) alert('Graag uw aandacht voor de met rood gemarkeerde velden');
			done = true;
		},
		submitHandler: function(form) {
			done = false;
			$.post("/includes/validate-user.php", $('#loginform').serializeArray(),
			function(data){
				$('#datastore').html(data);
			});
		}
	});

	$('#zoeken').validate({
		errorClass: "error",
		rules: {
			zoeken: {
				required: true,
				minlength: 4
			}
		},
		messages: {
		}, 
		errorPlacement: function(error, element) {
			if (done == false) alert('Minimale lengte voor een zoekterm is 4 tekens');
			done = true;
		},
		submitHandler: function(form) {
			done = false;
			$.post("/zoeken.php", $('#zoeken').serializeArray(),
			function(data){
				$('#datastore').html(data);
			});
		}
	});

	
	$('.cloud-zoom, .cloud-zoom-gallery').CloudZoom();
	
	$('#cart_add').validate({
		errorClass: "error",
		rules: {
			aantal: {
				required: true,
				range: [1, 9999]
			}
		},
		messages: {
			aantal: 'Ongeldig aantal, vul een getal in, minimaal 1'
		},
		errorPlacement: function(error, element) {
			alert(error.html());
		},
		submitHandler: function(form) {
			$.post("/cart.add.php", $('#cart_add').serializeArray(), function(data){
				$('#datastore').html(data);
			});
		}
	});
	
	$('#content #product_info #bestellen').click(function() {
		$('#cart_add').submit();
		return false;
	});
	
	$('#wagen a.update').click(function() {
		if (isInteger($('#aantal_' + $(this).attr('rel')).val()))
		{
			$.post($(this).attr('href'), { product_id: $(this).attr('rel'), aantal: $('#aantal_' + $(this).attr('rel')).val() }, function(data) {
				$('#datastore').html(data);
			});
		} else
		{
			alert('Ongeldig getal ingevoerd');
		}
		return false;
	});

	$('#wagen a.delete').click(function() {
		$.post($(this).attr('href'), { product_id: $(this).attr('rel') }, function(data) {
			$('#datastore').html(data);
		});
		return false;
	});
	
	$('#gegevens').validate({
		errorClass: "error",
		rules: {
			aanhef: {
				required: true
			},
			voorletters: {
				required: true
			},
			achternaam: {
				required: true
			},
			straat: {
				required: true
			},
			huisnummer: {
				required: true
			},
			postcode: {
				required: true
			},
			plaats: {
				required: true
			},
			telefoon1: {
				required: "input[name=telefoon2]:blank",
				minlength: 5
			},
			telefoon2: {
				required: "input[name=telefoon1]:blank",
				minlength: 5
			},
			email: {
				required: true,
				email: true
			},
			email_check: {
				required: true,
				email: true,
				equalTo: "input[name='email']"
			},
			bank_id: {
				required: function(element) {
        				return ($("#betaalmethode").val() == "I");
      			},
				minlength: 1
			},
			betaalmethode: {
				required: true,
				minlength: 1
			}
		}, 
		errorPlacement: function(error, element) {
			if ((element.attr("type") == "hidden") && (done == false)) 
			{
				alert('Kies een betaalmethode');
				done = true;
			} else
			{
				if (done == false) alert('Graag uw aandacht voor de met rood gemarkeerde velden');
				done = true;
			}
		},
		submitHandler: function(form) {
			done = false;
			$.post("/cart.save.php", $('#gegevens').serializeArray(),
			function(data){
				$('#datastore').html(data);
			});
		}
	});

	$('#bank_id').change(function() {
		$('#betaalmethode').val('I');
	});
	
	$('#vooruitbetalen').change(function() {
		if ($(this).attr('checked'))
			$('#betaalmethode').val('V');
		else
			$('#betaalmethode').val('');
	});

	$('#controle_button').click(function() {
		$('#gegevens').submit();
		return false;
	});
	
	$('#controle').validate({
		errorClass: "error",
		rules: {
			voorwaarden: {
				required: true
			}
		},
		messages: {
			voorwaarden: 'Je moet akkoord gaan met de algemene voorwaarden om te kunnen bestellen'
		},
		errorPlacement: function(error, element) {
			alert(error.html());
		},
		submitHandler: function(form) {
			done = false;
			$.post("/cart.betalen.php", $('#controle').serializeArray(),
			function(data){
				$('#datastore').html(data);
			});
		}
	});
	
	$('#betalen_button').click(function() {
		$('#controle').submit();
		return false;
	});
	
	$('#prijs_tooltip').tooltip();
	
	$('#product_tabs li a').click(function() {
		$('#product_tabs li').removeClass('showing');
		$('div.prodtab').removeClass('showing');
		$('#' + $(this).attr('rel')).addClass('showing');
		$(this).parent().addClass('showing');
		return false;
	});
});

