// JavaScript Document

var shop_update = function(){

	var totaal = 0;
	var verzendadm = 10.00;
	var cart = new Array();
	$$('table.shop input.aantal').each(function(e){
		var aantal = Number(e.value);
		if(isNaN(aantal)){
			e.value = '';
		}else{
			aantal = Math.round(e.value);
			var prodid = e.getProperty('name').replace('aantal_', '');
			
			var subtotaal = $('prod_'+prodid+'_prijs').value * aantal;
			var porto = $('prod_'+prodid+'_porto').value * aantal;
			if(porto > $('prod_'+prodid+'_maxporto').value){
				porto = $('prod_'+prodid+'_maxporto').value;
			}
			
			porto = Number(porto);
			
			if(!$('verzend').checked){
				verzendadm += porto;
			}
			
			var product = new Array(prodid, aantal);
			cart.push(product);
			
			var printtotaal = ($E('input', 'prod_'+prodid).value * aantal).toFixed(2);		
			$$('#prod_'+prodid+' .totaal').setHTML('&euro; '+printtotaal);
			e.value = Math.round(e.value);
			
			totaal += subtotaal;
			
		}
		
	});
			
	if(verzendadm < 10){
		verzendadm = 10;
	}else if(verzendadm > 28){
		verzendadm = 28;
	}
	
	//alert(verzendadm);
	
	verzendadm = Number(verzendadm);
	$('verzendkosten').setHTML('&euro; '+ verzendadm.toFixed(2));
	
	totaal += verzendadm; //administratie- & verzendkosten
	$('totaalexbtw').setHTML('&euro; '+ totaal.toFixed(2));
	
	var btw = totaal * 0.19;
	$('totaalbtw').setHTML('&euro; '+ btw.toFixed(2));
	
	totaal *= 1.19; //btw
	$('totaalinbtw').setHTML('&euro; '+ totaal.toFixed(2));

	//Ajax update de hele mikmak in een sessie
	new Ajax('/p/webshop.php', {method: 'post', data: 'savecart='+cart}).request();

}


var initshop = function(){
	
	if($('verzend')){ //is de winkelwagen wel zichtbaar?
		
		$$('table.shop input.aantal').each(function(e){
			e.addEvent('keyup', function(){
	
				var aantal = Number(e.value);
				if(isNaN(aantal)){
					e.value = '';
				}else{
								
					shop_update();
					
				}
			
			});
	
		});
		
		$('verzend').addEvent('change', function(){
			shop_update();										 
		});
	}
	
}

var betaalwijze = function(waarde){

	if(waarde == 2){
		$('shopform').setProperty('action', '/nl/webshop/kassa/afronden/');
	}else{
		$('shopform').setProperty('action', 'https://ideal.rabobank.nl/ideal/mpiPayInitRabo.do');
	}
	
}

var klant_valid = function(form){

	var velden = new Array('naam', 'email', 'adres', 'postcode', 'plaats');
	
	var waarde = '';
	var fout = false;
	
	for(i=0; i<velden.length; i++){
		waarde = $('f'+ velden[i]).value
		if(waarde.length < 2){
			fout = true;
			$('f'+ velden[i]).addClass("fout");
		}else{
			$('f'+ velden[i]).removeClass("fout");
		}
			
	}
	
	if(fout){
		alert('vul aub alle velden zo volledig mogelijk in');
	}
	
	return !fout;
}

window.addEvent('domready', function(){
	initshop();
});