function setProduct(id){
	if($('#'+id).hasClass('productActive')){ // indien actief, product legen en selectie uitschakelen
		$('#'+id).removeClass('productActive');
		$('#'+id).addClass('product');	
		$('#order'+id).remove();	
		makePrice();			
	}else{ // nog niet actief, dus activeren en product wegschrijven in hidden fields
		$('#'+id).removeClass('product');
		$('#'+id).addClass('productActive');
		$('#'+id).clone().insertBefore($('#'+id).find('img')).css('position','absolute').animate({ 
			marginTop: "+200px",
			opacity:0
	    }, 500, function(){$(this).remove()});		
		
		/*$('#'+id).find('img').attr('src','img/'+id+'_active.jpg');*/
		var newProduct="<div id='order"+id+"' class='prodOrder' style='padding:5px 5px 0px 5px;width:290px;background-color:#ffffff;'><strong>"+$('#'+id).attr('name')+"</strong>&nbsp;&nbsp;"+$('#'+id).attr('aantal')+"<br><br><input type='hidden' name='ProductId[]' value='"+id+"' class='ProductId'><input type='hidden' name='ProductName[]' value='"+$('#'+id).attr('name')+"' class='ProductName'><input type='hidden' name='ProductPrice[]' value='"+$('#'+id).attr('price')+"' class='ProductPrice'><div style='float:left;background-color:#ff9900;padding:5px;color:#ffffff;width:280px;'><div style='float:left;'>Aantal: <input type='text' name='buyAantal[]' size='3' class='buyAantal'></div><div style='float:right;padding:3px;cursor:pointer;' onClick='removeProduct("+id+");'><img src='img/denied.png' width='15'></div></div><br style='clear:both' /></div>";		
		$('#kiesProduct:visible').slideUp('slow');
		setTimeout(function(){$(newProduct).appendTo('#productName');$('#bgChange').css("background-color","#f9f9f9")},500);
	}
	productHovers();
	
}

function removeProduct(id){
	$('#'+id).removeClass('productActive');
	$('#'+id).addClass('product');	
	$('#'+id).css("border","2px solid #eeeeee").css("margin","4px");
	$('#order'+id).remove();	
	makePrice();	
}

function productHovers(){
	$('.product').unbind();
	$('.product').hover(function(){
		if($(this).hasClass("productActive")){
			return false;
		}else{
			var id = $(this).attr('id');
			/*$(this).find('img').attr('src','img/'+id+'_hover.jpg');*/
			$(this).css("border","4px solid #ff9900").css("margin","2px");
		}
	},function(){
		if($(this).hasClass("productActive")){
			return false;
		}else{
			var id = $(this).attr('id');
			/*$(this).find('img').attr('src','img/'+id+'.jpg');*/
			$(this).css("border","2px solid #eeeeee").css("margin","4px");			
		}
	});	
}

function updateVerzendkosten(land){
	if(land=='belgie'){
		$('#belgie').val(1);
		var landAlert=1;
		makePrice(landAlert);		
	}else{
		$('#belgie').val(0);
		makePrice();
	}
}

function makePrice(landAlert){
	var tempPrice=null;
	var tempAantal=null;	
	var post=null;
	var buisjes = 0;
	$('.prodOrder').each(function(){
		var prijs = parseInt($(this).find('.ProductPrice').val());
		var aantal = parseInt($(this).find('.buyAantal').val());		
		var name =  $(this).find('.ProductName').val();	
		var id = $(this).find('.ProductId').val();	
		
		if(id==4 || id==3|| id==8 || id==13 || id==14 || id==15 || id==16 || id==17 || id==50){ /* cup of speeksel */
			post=1;
		}
		
		if(id==6 || id==7 || id==9){ /* 5 of meer buisjes == 8.60 */
			buisjes+=aantal;	
		}
		
		
		
		if(parseInt(prijs) && parseInt(aantal)){
			tempPrice+=Math.abs(prijs*aantal);
		}
		
		if(parseInt(aantal)){
			tempAantal+=aantal;
		}		
	});
	
	if(parseInt(tempPrice)){
		$('#payments:not(":visible")').fadeIn();

		if($('#belgie').val()==1){
			if(post==1 || buisjes>4){			
				var verzendkosten = 13.10;
			}else{
				var verzendkosten = 3;	
			}
			if(landAlert==1){
				alert('LET OP!!\n\n De verzendkosten voor Belgie zijn '+ verzendkosten);
			}
		}else{
			if(post==1 || buisjes>4){
				var verzendkosten = 8.60;
			}else{
				var verzendkosten = 1.75;
			}
		}
		
		var totalprice = Math.abs(tempPrice/100)+verzendkosten;
		$('#totalPriceContainer').html('<table cellpadding="2" style="background-color:#eeeeee;width:280px;"><tr><td width="140"><strong>Prijs</strong></td><td> &euro; '+number_format((tempPrice/100),2,',','.')+'</td></tr><Tr><Td><strong>Verzendkosten</strong></td><td> &euro; '+number_format((verzendkosten),2,',','.')+'</td></tr><tr><td style="border-top:1px solid #eeeeee;padding-top:2px;"><strong>Totaal</strong></td><td style="border-top:1px solid #eeeeee;padding-top:2px;">&euro; '+number_format((totalprice),2,',','.')+'</td></tr></table>').fadeIn('normal');
		
	}else{
		$('#totalPriceContainer').fadeOut('normal').html('');
		$('#payments:not(":hidden")').hide();
	}

}

function checkemail(adres){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(adres)){ return true; }else{ return false; }
}

$(function() { 
    var options = { 
        target:        '#buyForm',  
		url:			'modules/bestellen/bestellen_send.php',		
        beforeSubmit:  checkForm,  
        success:       showResponse ,
		resetForm:		false
    }; 
    $('#buy').submit(function() { 

        $(this).ajaxSubmit(options); 
        return false; 
    }); 
	productHovers();
	
	$('.buyAantal').livequery('keyup', function(){
		makePrice();
	});
}); 


function checkForm(){
	var clear=false;
	
	if($('#totalPriceContainer').html()==''){
		alert('Kies eerst een product en geef het aantal aan!!');	
		return false;
	}
	
	$("#buy .required").each(function(){
		if($(this).attr('type')=="checkbox"){
			if(!$(this).attr('checked')){
				$(this).parent().css("background-color","#ff0000");
				$(this).click(function(){$(this).parent().css("background-color","#f9f9f9")});	
				clear=true;
			}else{
				$(this).parent().css("background-color","#f9f9f9");
			}
		}else{
			if($(this).val()==""){
				$(this).css("background-color","red").click(function(){$(this).css("background-color","transparent")});
				$(this).blur(function(){$(this).css("background-color","transparent")});
				clear=true;
			}else{
				$(this).css("background-color","transparent");
			}
		}
	});
	if(!checkemail($("#email").val())){
		$("#email").css("background-color","red").click(function(){$(this).css("background-color","transparent")});
		$("#email").blur(function(){$(this).css("background-color","transparent")});		
		clear=true;
	}	

	if(clear){
		$('#buyFormFields').parent().prepend('<div id="formError" style="color:red;font-weight:bold;padding:5px;position:absolute;margin-top:-20px;">Vul alle rood gemarkeerde velden in!</div>');
		setTimeout(function(){$('#formError').fadeOut('slow', function(){$('#formError').remove();});},4500);
		return false;
	}else{
		return true;
	}
}

function showResponse(responseText, statusText)  { 
	$('#buyTable').slideUp('slow');
	$('#content').html(responseText);
} 


function number_format( number, decimals, dec_point, thousands_sep ) {
    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
    var d = dec_point == undefined ? "." : dec_point;
    var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : "";
    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}
