function webshop()
{

	this.updateShoppingCart = function()
	{
		req = 'f=updateShoppingCart';
		this.request(req, 'shoppingcart');
	}


	this.addProduct = function(id)
	{
		$.noticeAdd({
			text: 'Item toegevoegd aan winkelwagen',
			stayTime: 2000,
			type: 'notice',
			stay: false
		});
	
		req = 'f=addProduct&id=' + id;
		this.request(req, 'shoppingcart');
	}
	
	
	this.removeProduct = function(id)
	{
		req = 'f=removeProduct&id=' + id;
		this.request(req, 'main');
		this.updateShoppingCart();
	}
	
	
	this.setProductAmount = function(id)
	{
		if($('#' + id).val() > 0)
		{
			req = 'f=setProductAmount&id=' + id + '&amount=' + $('#' + id).val();
			this.request(req, 'main');
			this.updateShoppingCart();
		}
	}
	
	
	this.incProductAmount = function(id)
	{
		req = 'f=incProductAmount&id=' + id;
		this.request(req, 'main');
	}
	
	
	this.decProductAmount = function(id)
	{
		req = 'f=decProductAmount&id=' + id;
		this.request(req, 'main');
	}
	
	
	this.validateForm = function()
	{
		// validate signup form on keyup and submit
		$("#gegevens_form").validate({
		submitHandler: function(form) {
   		var results = $('#gegevens_form').serialize();
    	$.post('/engine/ajax/webshop_ajax.php', results, function(data) {
    		window.location = '/winkelwagen/betalingswijze/';
    	});
   	},
		rules: {
			voorletters: "required",
			geslacht: "required",
			achternaam: "required",
			adres: "required",
			huisnummer: "required",
			postcode: "required",
			woonplaats: "required",
			land: "required",
			telefoon: {
				required: true,
				minlength: 10,
				digits: true
			},
			mail: {
				required: true,
				email: true
			}
		},
		messages: {
			voorletters: "<br />Voer uw voorletters in",
			geslacht: "<br />Voer uw aanhef in",
			achternaam: "<br />Voer uw achternaam in",
			adres: "<br />Voer uw adres in",
			huisnummer: "<br />Voer uw huisnummer in",
			postcode: "<br />Voer uw postcode in",
			woonplaats: "<br />Voer uw woonplaats in",
			land: "<br />Voer uw land in",
			telefoon: {
				required: "<br />Voer uw telefoonnummer in",
				minlength: "<br />Voer minimaal 10 cijfers in",
				digits: "<br />U kunt alleen maar cijfers gebruiken in dit veld"
			},
			mail: "<br />Voer een valide e-mail adres in"
		}
		});
	}
	
	
	this.acceptPayment = function()
	{
		var results = $('#betalingswijze_form').serialize();
    $.post('/engine/ajax/webshop_ajax.php', results, function(data) {
    	window.location = '/winkelwagen/overzicht/';
    });
	}
	
	
	this.order = function()
	{	
		$('.volgende').css('background','');
		$('.volgende').html('<center><img src="/images/spinner.gif" /><br />Graag even geduld, uw bestelling wordt verwerkt. U ontvangt een (geautomatiseerde) bevestiging van uw order binnen enkele minuten.</center>');
	
		$.ajax({
			url: '/engine/ajax/webshop_ajax.php',
			type: 'POST',
			cache: false,
			data: 'f=order',
			success: function(data)
			{
				if(data == 1) $('.volgende').html('<center>U heeft geen artikelen geselecteerd !</center>');
				else if(payment == 'bank' || payment == 'pickup') $('#main').html(data);
				else $('#form').submit();
			}
		});
	}
	
	
	this.request = function(req, target)
	{
		$.ajax({
			url: '/engine/ajax/webshop_ajax.php',
			type: 'POST',
			cache: false,
			data: req,
			success: function(data)
			{
				$('#' + target).html(data);
			}
		});
	}

}


$(document).ready(function() {
	webshop = new webshop();
	webshop.validateForm();
});
