function loadImage(src) {
	$('.imglarge img').attr('src',src.replace('sm_',''));	
}

function add2cart(artid,amount,color,ralcode) {
	postString = "artid="+artid+"&amount="+amount;
	if (color!=''&&color!=undefined) postString += "&color="+color;
	if (ralcode!=''&&ralcode!=undefined) postString += "&ralcode="+ralcode;
					
	$.ajax({
		type: "POST",
		url: "/services/add2cart.php",
		data: postString,
		success: function(msg){
			if (msg=='goodRequest') {
				loadCart();
			} else if (msg=='wrong') {
				alert('Je kan maximaal 10 stuks van dit artikel bestellen.');
			} else alert('Er is iets fout gegaan bij het toevoegen van het artikel aan je winkelwagen. Probeer het opnieuw of neem contact met ons op.');
		}
	});
}

function delFromCart(id,msg) {
	if (confirm(msg)) {
		id = id.split('_');
		$.ajax({
			type: "POST",
			url: "/services/delFromCart.php",
			data: "id="+id[1],
			success: function(msg){
				if (msg=='goodRequest') {
					loadCart();
				}
			}
		});
    }
}

function changeAmount(id,amount) {
	id = id.split('_');
	$.ajax({
		type: "POST",
		url: "/services/changeCart.php",
		data: "id="+id[1]+"&amount="+amount,
		success: function(msg){
			if (msg=='goodRequest') {
				loadCart(msg);
			}
		}
	});
}

function loadCart() {
	$.ajax({
		type: "POST",
		url: "/services/loadCart.php",
		success: function(msg){
			if (msg!='wrongrequest') {
				msg = msg.split('___');
				if (msg[1]=='empty') {
					$('#cart').html("<p style='padding:3px 7px'>"+msg[2]+"</p>");
					$('.paybtn').hide();
					$('.pages').hide();
				} else {
					window.parent.$('select[name=pagenum]').html(msg[2]);
					window.parent.$('#totalpages').html(msg[3]);
					window.parent.$('#cart').html(msg[1]);
					window.parent.$('.paybtn').show();
					window.parent.$('.pages').show();
					CB_Init();
				}
				window.parent.CB_Close();
			}
		}
	});
}

function setFields(shipping,gift,discount) {
	var subTotal = new Number($('input[name=subTotal]').val());
	var newTotal = parseFloat(subTotal);
	if (shipping!='0.00'&&shipping!=undefined) newTotal += eval(shipping);
	if (gift!='0.00'&&gift!=undefined) newTotal += eval(gift);
	if (discount!='0.00'&&discount!=undefined) newTotal -= eval(discount);
	
	$('#shipping_fld').html(" "+shipping.replace('.',','));
	$('#giftPrice_fld').html(" "+gift.replace('.',','));
	$('#discount_fld').html(" "+discount.replace('.',','));
	$('#totalPrice_fld').html(" "+newTotal.toFixed(2).replace('.',','));
}

function changeSetup(type,inp_val,lang) {
	$('form input, form select').attr('disabled',true);
	var subTotal = new Number($('input[name=subTotal]').val());
	$.ajax({
		type: "POST",
		url: "/services/setupCart.php",
		data: "type="+type+"&inp_val="+inp_val,
		success: function(msg){
			msg = msg.split(';');
			if (type=='shipping') setFields(msg[1],msg[2],msg[3]);
			else if (type=='gift') {
				setFields(msg[1],msg[2],msg[3]);
				if (msg[2]=='0.00') $('#giftrow').hide();
				else $('#giftrow').show();
			} else if (type=='coupon') {
				if (inp_val!='') {
					if (msg[0]=='wrongRequest') {
						if (msg[1]=='spent') alert(msg[2]);
						else if (msg[1]=='doesntexist') alert(msg[2]);
					} else if (msg[0]=='goodRequest') {
						setFields(msg[1],msg[2],msg[3]);
						$('.getcodebtn').html("[<a href='javascript:void(0);' title='"+msg[4]+"' class='disablecode'>"+msg[4]+"</a>]");
						$('input[name=coupon]').attr('disabled',true);
						$('.getcodebtn .disablecode').click(function() {
							if (confirm(msg[6])) changeSetup('coupon','');
						});
					}
				} else {
					setFields(msg[1],msg[2],msg[3]);
					$('.getcodebtn').html("[<a href='javascript:void(0);' title='"+msg[5]+"' class='getcode'>"+msg[5]+"</a>]");
					$('input[name=coupon]').val('');
					$('input[name=coupon]').attr('disabled',false);
					$('.getcodebtn .getcode').click(function() {
						var code = $('input[name=coupon]').val();
						if (code.length<5 || code == '') alert(msg[7]);
						else changeSetup('coupon',code);
					});
					
					$('input[name=coupon]').keyup(function(e) {
						if(e.keyCode == 13) {
							var code = $(this).val();
							if (code.length<5 || code == '') alert(msg[7]);
							else changeSetup('coupon',code);
						}
					});
				}
			}
			$('form input, form select').attr('disabled',false);
			if (lang!='nl') $('select[name=shipping]').attr('disabled',true);
		}
	});
}

function changePayment(val,id,lang) {
	if (lang=='nl') {
		if (val=='overmaken') var answer = confirm("Weet je zeker dat je het bedrag\nvan deze bestelling wilt overmaken?")
		else if (val=='geen') var answer = confirm("Weet je zeker dat je deze bestelling\nwilt annuleren?")
		else var answer = confirm("Weet je zeker dat je deze bestelling\nmet "+val+" wilt betalen?");
	} else if (lang=='fr') {
		if (val=='geen') var answer = confirm("Etes-vous sur de vouloir annuler cette commande?")
		else var answer = confirm("Etes-vous sur que vous voulez commander avec paiement "+val+"?");
	} else if (lang=='de') {
		if (val=='geen') var answer = confirm("Sind Sie sicher, dass Sie diese Bestellung stornieren?")
		else var answer = confirm("Sind Sie sicher, dass Sie mit "+val+" bezahlen wollen?");
	} else if (lang=='en') {
		if (val=='geen') var answer = confirm("Are you sure you want to cancel this order?")
		else var answer = confirm("Are you sure you want to pay this order with "+val+"?");
	}
	if (answer){
		$.ajax({ 
			type	: "POST", 
			url		: "/services/changePayment.php", 
			data	: "id="+id+"&val="+val,
			success	: function(html){
				if (html=='good') window.location.href = "/betalen/"+lang+"/"+id;
				else {
					if (lang=='nl') alert('Er is iets fout gegaan bij het veranderen van de betaalmethode');
					else alert('Something went wrong');
				}
			}
		});
	}
}

function getShipping(val) {
	$.ajax({
		type: "POST",
		url: "/services/setShipping.php",
		data: "val="+val,
		success: function(msg){
			if (msg!='wrongRequest') {
				var values = msg.split('@');
				$('#shipping_fld').html(' '+values[2].replace('.',','));
				$('#totalPrice_fld').html(' '+values[1].replace('.',','));				
			}
		}
	});
}