function setFieldError(fieldId, message)
{
	var text = '';
	
	try
	{
		text = $(fieldId).qtip('api').options.content.text;
	}
	catch(ex) {}
	
	if(message != '')
	{
		if($(fieldId).data("qtip") && text != message)
		{
			$(fieldId).qtip("destroy");
		}
		
		if(message != text || text == '')
		{
			$(fieldId).qtip({
					content: message,
					position: {
						corner: {
							tooltip: 'leftBottom',
							target: 'rightTop'
						}
					},
					style: {
						border: {
							width: 2,
							radius: 4
						},
						padding: 5,
						textAlign: 'center',
						tip: true,
						name: 'red'
					},
					show: {
						delay: 0,
						ready: true
					},
					hide: {
						when: {
							event: false
						},
						delay: 0
					}
			});
			
			$(fieldId).addClass('error');
		}
	}
	else
	{
		if($(fieldId).data("qtip"))
		{
			$(fieldId).qtip('api').options.content.text = '';
			$(fieldId).qtip("destroy");
		}
		
		$(fieldId).removeClass('error');
	}
}

function addProduct(handlerUrl, productId, amount)
{
	var url = handlerUrl + '/addProduct/' + productId + '/' + amount;
	var data = {};
	
	$.ajax({
		url: url,
		dataType: 'json',
		data: data,
		success: function(data)
		{
			$('#numberOfProductsInShoppingCart').text(data.totalAmount);
			$('#totalPriceShoppingCart').text(data.subtotal);
			
			/*
			var msgDialog = $('<div></div>')
				.html(data.message)
				.dialog({
					resizable: false,
					draggable: false,
					height:140,
					modal: true,
					title: data.title,
					buttons: {
						Sluiten: function() {
							$(this).dialog('close');
						},
						Afrekenen: function() {
							window.location.href = 'winkelwagen';
						}
					}
				});
			*/
			
			//alert($('#productId_' + productId).attr('id'));
			
			var lnk = $('#productId_' + productId);
			
			lnk.qtip({
					content: {
						text: data.message + (data.code == 'success' ? ' <a href="winkelwagen">Bekijk hier uw winkelwagen</a>' : '')
					},
			        position: {
						corner: {
							tooltip: 'bottomMiddle',
							target: 'topMiddle'
						}
					},
					style: {
						border: {
							width: 2,
							radius: 4
						},
						padding: 5,
						tip: true,
						name: 'cream'
					},
					show: {
						delay: 0,
						ready: true, 
			            solo: true
					},
					hide: {
						when: {
							event: false
						},
						delay: 0
					},
					api: {
						onHide: function() {
							lnk.qtip('destroy');
						},
						onShow: function() {
							setTimeout(function() { lnk.qtip('hide'); }, 2000);
						}
					}
			});
		}
	});
}

var timer = null;

function setAmount(handlerUrl, productId, amount)
{
	if(timer != null)
	{
		clearTimeout(timer);
		timeout = 200;
	}
	else
	{
		timeout = 0;
	}
	
	timer =	setTimeout(
				function()
				{
					var url = handlerUrl + '/setProducts/' + productId + '/' + amount;
					var data = $('#winkelwagen').serialize();
					
					$.ajax({
						type: 'POST',
						url: url,
						dataType: 'json',
						data: data,
						success: function(data)
						{
							$('#numberOfProductsInShoppingCart').text(data.totalAmount);
							$('#totalPriceShoppingCart').text(data.subtotal);
							
							$('.amountField').each(function()
									{
										setFieldError('#amount_' + $(this).attr('id'), '');
									});
							
							$.each(data.products, function(index, value)
									{
										if(value.code == 'success')
										{
											$('#price_' + value.productId).text(value.subTotal);
											
											setFieldError('#amount_' + value.productId, '');
										}
										else
										{
											setFieldError('#amount_' + value.productId, value.message);
										}
										
									});
							
							timer = null;
						}
					});
				},
				timeout
		);
}

var updateShoppingCartTimer = null;

function updateShoppingCart(handlerUrl, productId, amount)
{
	if(updateShoppingCartTimer != null)
	{
		clearTimeout(updateShoppingCartTimer);
		timeout = 200;
	}
	else
	{
		timeout = 0;
	}
	
	updateShoppingCartTimer = setTimeout(
				function()
				{
					var url = handlerUrl + '/setProducts/' + productId + '/' + amount;
					var data = $('#winkelwagen').serialize();
					
					$.ajax({
						type: 'POST',
						url: url,
						dataType: 'json',
						data: data,
						success: function(data)
						{
							$('#numberOfProductsInShoppingCart').text(data.totalAmount);
							$('#totalPriceShoppingCart').text(data.subtotal);

							$('#shoppingCartSubtotal').text(data.subtotal);
							$('#shoppingCartShippingCosts').text(data.shippingCosts);
							$('#shoppingCartTotal').text(data.total);
							
							$('.amountField').each(function()
									{
										setFieldError('#amount_' + $(this).attr('id'), '');
									});
							
							$.each(data.products, function(index, value)
									{
										if(value.code == 'success')
										{
											$('#price_' + value.productId).text(value.subtotal);
											
											setFieldError('#amount_' + value.productId, '');
										}
										else
										{
											setFieldError('#amount_' + value.productId, value.message);
										}
										
									});
							
							updateShoppingCartTimer = null;
						}
					});
				},
				timeout
		);
}

function deleteProduct(handlerUrl, productId)
{
	var url = handlerUrl + '/deleteProduct/' + productId;
	var data = {};
	
	$.ajax({
		url: url,
		dataType: 'json',
		data: data,
		success: function(data)
		{
			$('#numberOfProductsInShoppingCart').text(data.totalAmount);
			$('#totalPriceShoppingCart').text(data.subtotal);
		}
	});
}

function gotoStep(url)
{
	$('#winkelwagen').attr('action', url);
	$('#winkelwagen').submit();
}

function showLogin()
{
	if($('#customerEmail').data("qtip"))
	{
		$('#customerEmail').qtip('api').options.content.text = '';
		$('#customerEmail').qtip("destroy");
	}

	$('.loginShown').slideDown();
	$('.loginHidden').slideUp();
	$('.customerShown').slideUp();
	$('.customerHidden').slideDown();

	$('#newUser').val('');
}

function showCustomer()
{
	$('.loginShown').slideUp();
	$('.loginHidden').slideDown();
	$('.customerShown').slideDown();
	$('.customerHidden').slideUp();

	$('#newUser').val('true');
	
}

var checkEmailTimer = null;

function checkEmailExistence(handlerUrl, email)
{
	if(checkEmailTimer != null)
	{
		clearTimeout(checkEmailTimer);
		timeout = 200;
	}
	else
	{
		timeout = 0;
	}
	
	checkEmailTimer = setTimeout(
				function()
				{
					var url = handlerUrl + '/checkEmail/' + email;
					
					$.ajax({
						type: 'POST',
						url: url,
						dataType: 'json',
						success: function(data)
						{
							if(data.code == 'userExists')
							{
								$('#userName').val(email);

								$('.customerData input').attr('disabled', 'disabled');
								$('.customerData select').attr('disabled', 'disabled');
								$('.customerData textarea').attr('disabled', 'disabled');
								
								$('#customerEmail').attr('disabled', '');
								

								$('#userExistsMessage input').attr('disabled', '');
								$('#userExistsMessage').show();
							}
							else
							{
								$('#userExistsMessage').hide();
								
								$('.customerData input').attr('disabled', '');
								$('.customerData select').attr('disabled', '');
								$('.customerData textarea').attr('disabled', '');
							}
							
							updateShoppingCartTimer = null;
						}
					});
				},
				timeout
		);
}
